]> diplodocus.org Git - nmh/blob - uip/send.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / send.c
1 /* send.c -- send a composed message
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <fcntl.h>
10 #include <h/utils.h>
11 #ifdef OAUTH_SUPPORT
12 # include <h/oauth.h>
13 #endif
14 #include "sbr/m_maildir.h"
15 #include "sbr/m_mktemp.h"
16
17 #ifndef CYRUS_SASL
18 # define SASLminc(a) (a)
19 #else /* CYRUS_SASL */
20 # define SASLminc(a) 0
21 #endif /* CYRUS_SASL */
22
23 #ifndef TLS_SUPPORT
24 # define TLSminc(a) (a)
25 #else /* TLS_SUPPORT */
26 # define TLSminc(a) 0
27 #endif /* TLS_SUPPORT */
28
29 #define SEND_SWITCHES \
30 X("alias aliasfile", 0, ALIASW) \
31 X("debug", -5, DEBUGSW) \
32 X("draft", 0, DRAFTSW) \
33 X("draftfolder +folder", 6, DFOLDSW) \
34 X("draftmessage msg", 6, DMSGSW) \
35 X("nodraftfolder", 0, NDFLDSW) \
36 X("filter filterfile", 0, FILTSW) \
37 X("nofilter", 0, NFILTSW) \
38 X("format", 0, FRMTSW) \
39 X("noformat", 0, NFRMTSW) \
40 X("forward", 0, FORWSW) \
41 X("noforward", 0, NFORWSW) \
42 X("mime", 0, MIMESW) \
43 X("nomime", 0, NMIMESW) \
44 X("msgid", 0, MSGDSW) \
45 X("nomsgid", 0, NMSGDSW) \
46 X("push", 0, PUSHSW) \
47 X("nopush", 0, NPUSHSW) \
48 X("split seconds", 0, SPLITSW) \
49 X("unique", -6, UNIQSW) \
50 X("nounique", -8, NUNIQSW) \
51 X("verbose", 0, VERBSW) \
52 X("noverbose", 0, NVERBSW) \
53 X("watch", 0, WATCSW) \
54 X("nowatch", 0, NWATCSW) \
55 X("width columns", 0, WIDTHSW) \
56 X("version", 0, VERSIONSW) \
57 X("help", 0, HELPSW) \
58 X("dashstuffing", -12, BITSTUFFSW) \
59 X("nodashstuffing", -14, NBITSTUFFSW) \
60 X("client host", -6, CLIESW) \
61 X("server host", 6, SERVSW) \
62 X("snoop", 5, SNOOPSW) \
63 X("sasl", SASLminc(4), SASLSW) \
64 X("nosasl", SASLminc(6), NOSASLSW) \
65 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
66 X("authservice", SASLminc(0), AUTHSERVICESW) \
67 X("user username", SASLminc(-4), USERSW) \
68 X("port server-port-name/number", 4, PORTSW) \
69 X("tls", TLSminc(-3), TLSSW) \
70 X("initialtls", TLSminc(-10), INITTLSSW) \
71 X("notls", TLSminc(-5), NTLSSW) \
72 X("certverify", TLSminc(-10), CERTVERSW) \
73 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
74 X("sendmail program", 0, MTSSM) \
75 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
76 X("messageid localname|random", 2, MESSAGEIDSW) \
77
78 #define X(sw, minchars, id) id,
79 DEFINE_SWITCH_ENUM(SEND);
80 #undef X
81
82 #define X(sw, minchars, id) { sw, minchars, id },
83 DEFINE_SWITCH_ARRAY(SEND, switches);
84 #undef X
85
86 #define USE_SWITCHES \
87 X("no", 0, NOSW) \
88 X("yes", 0, YESW) \
89 X("list", 0, LISTDSW) \
90
91 #define X(sw, minchars, id) id,
92 DEFINE_SWITCH_ENUM(USE);
93 #undef X
94
95 #define X(sw, minchars, id) { sw, minchars, id },
96 DEFINE_SWITCH_ARRAY(USE, anyl);
97 #undef X
98
99 extern int debugsw; /* from sendsbr.c */
100 extern int forwsw;
101 extern int inplace;
102 extern int pushsw;
103 extern int splitsw;
104 extern int unique;
105 extern int verbsw;
106
107 extern char *altmsg; /* .. */
108 extern char *annotext;
109 extern char *distfile;
110
111
112 int
113 main (int argc, char **argv)
114 {
115 int msgp = 0, distsw = 0, vecp;
116 int isdf = 0, mime = 0;
117 int msgnum, status;
118 char *cp, *dfolder = NULL, *maildir = NULL;
119 char buf[BUFSIZ], **ap, **argp, **arguments, *program;
120 char *msgs[MAXARGS], **vec;
121 const char *user = NULL, *saslmech = NULL;
122 struct msgs *mp;
123 struct stat st;
124 int snoop = 0;
125 char *auth_svc = NULL;
126
127 if (nmh_init(argv[0], 1)) { return 1; }
128
129 arguments = getarguments (invo_name, argc, argv, 1);
130 argp = arguments;
131
132 vec = argsplit(postproc, &program, &vecp);
133
134 vec[vecp++] = "-library";
135 vec[vecp++] = getcpy (m_maildir (""));
136
137 if ((cp = context_find ("fileproc"))) {
138 vec[vecp++] = "-fileproc";
139 vec[vecp++] = cp;
140 }
141
142 if ((cp = context_find ("mhlproc"))) {
143 vec[vecp++] = "-mhlproc";
144 vec[vecp++] = cp;
145 }
146
147 if ((cp = context_find ("credentials"))) {
148 /* post doesn't read context so need to pass credentials. */
149 vec[vecp++] = "-credentials";
150 vec[vecp++] = cp;
151 }
152
153 while ((cp = *argp++)) {
154 if (*cp == '-') {
155 switch (smatch (++cp, switches)) {
156 case AMBIGSW:
157 ambigsw (cp, switches);
158 done (1);
159 case UNKWNSW:
160 adios (NULL, "-%s unknown\n", cp);
161
162 case HELPSW:
163 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
164 print_help (buf, switches, 1);
165 done (0);
166 case VERSIONSW:
167 print_version(invo_name);
168 done (0);
169
170 case DRAFTSW:
171 msgs[msgp++] = draft;
172 continue;
173
174 case DFOLDSW:
175 if (dfolder)
176 adios (NULL, "only one draft folder at a time!");
177 if (!(cp = *argp++) || *cp == '-')
178 adios (NULL, "missing argument to %s", argp[-2]);
179 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
180 *cp != '@' ? TFOLDER : TSUBCWF);
181 continue;
182 case DMSGSW:
183 if (!(cp = *argp++) || *cp == '-')
184 adios (NULL, "missing argument to %s", argp[-2]);
185 msgs[msgp++] = cp;
186 continue;
187 case NDFLDSW:
188 dfolder = NULL;
189 isdf = NOTOK;
190 continue;
191
192 case PUSHSW:
193 pushsw++;
194 continue;
195 case NPUSHSW:
196 pushsw = 0;
197 continue;
198
199 case SPLITSW:
200 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
201 adios (NULL, "missing argument to %s", argp[-2]);
202 continue;
203
204 case UNIQSW:
205 unique++;
206 continue;
207 case NUNIQSW:
208 unique = 0;
209 continue;
210
211 case FORWSW:
212 forwsw++;
213 continue;
214 case NFORWSW:
215 forwsw = 0;
216 continue;
217
218 case VERBSW:
219 verbsw++;
220 vec[vecp++] = --cp;
221 continue;
222 case NVERBSW:
223 verbsw = 0;
224 vec[vecp++] = --cp;
225 continue;
226
227 case MIMESW:
228 mime++;
229 vec[vecp++] = --cp;
230 continue;
231 case NMIMESW:
232 mime = 0;
233 vec[vecp++] = --cp;
234 continue;
235
236 case SNOOPSW:
237 snoop++;
238 vec[vecp++] = --cp;
239 continue;
240
241 case DEBUGSW:
242 debugsw++;
243 /* FALLTHRU */
244 case NFILTSW:
245 case FRMTSW:
246 case NFRMTSW:
247 case BITSTUFFSW:
248 case NBITSTUFFSW:
249 case MSGDSW:
250 case NMSGDSW:
251 case WATCSW:
252 case NWATCSW:
253 case SASLSW:
254 case NOSASLSW:
255 case TLSSW:
256 case INITTLSSW:
257 case NTLSSW:
258 case CERTVERSW:
259 case NOCERTVERSW:
260 vec[vecp++] = --cp;
261 continue;
262
263 case USERSW:
264 vec[vecp++] = --cp;
265 if (!(cp = *argp++) || *cp == '-')
266 adios (NULL, "missing argument to %s", argp[-2]);
267 vec[vecp++] = cp;
268 user = cp;
269 continue;
270
271 case AUTHSERVICESW:
272 #ifdef OAUTH_SUPPORT
273 if (!(auth_svc = *argp++) || *auth_svc == '-')
274 adios (NULL, "missing argument to %s", argp[-2]);
275 #else
276 adios (NULL, "not built with OAuth support");
277 #endif
278 continue;
279
280 case SASLMECHSW:
281 if (!(saslmech = *argp) || *saslmech == '-')
282 adios (NULL, "missing argument to %s", argp[-1]);
283 /* FALLTHRU */
284
285 case ALIASW:
286 case FILTSW:
287 case WIDTHSW:
288 case CLIESW:
289 case SERVSW:
290 case PORTSW:
291 case MTSSM:
292 case MTSSW:
293 case MESSAGEIDSW:
294 vec[vecp++] = --cp;
295 if (!(cp = *argp++) || *cp == '-')
296 adios (NULL, "missing argument to %s", argp[-2]);
297 vec[vecp++] = cp;
298 continue;
299 }
300 } else {
301 msgs[msgp++] = cp;
302 }
303 }
304
305 /*
306 * check for "Aliasfile:" profile entry
307 */
308 if ((cp = context_find ("Aliasfile"))) {
309 char *dp = NULL;
310
311 for (ap = brkstring(dp = mh_xstrdup(cp), " ", "\n"); ap && *ap; ap++) {
312 vec[vecp++] = "-alias";
313 vec[vecp++] = *ap;
314 }
315 }
316
317 if (dfolder == NULL) {
318 if (msgp == 0) {
319 msgs[msgp++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
320 if (stat (msgs[0], &st) == NOTOK)
321 adios (msgs[0], "unable to stat draft file");
322 cp = concat ("Use \"", msgs[0], "\"? ", NULL);
323 for (status = LISTDSW; status != YESW;) {
324 if (!(argp = read_switch_multiword (cp, anyl)))
325 done (1);
326 switch (status = smatch (*argp, anyl)) {
327 case NOSW:
328 done (0);
329 case YESW:
330 break;
331 case LISTDSW:
332 showfile (++argp, msgs[0]);
333 break;
334 default:
335 inform("say what?");
336 break;
337 }
338 }
339 } else {
340 for (msgnum = 0; msgnum < msgp; msgnum++)
341 msgs[msgnum] = getcpy (m_maildir (msgs[msgnum]));
342 }
343 } else {
344 if (!context_find ("path"))
345 free (path ("./", TFOLDER));
346
347 if (!msgp)
348 msgs[msgp++] = "cur";
349 maildir = m_maildir (dfolder);
350
351 if (chdir (maildir) == NOTOK)
352 adios (maildir, "unable to change directory to");
353
354 /* read folder and create message structure */
355 if (!(mp = folder_read (dfolder, 1)))
356 adios (NULL, "unable to read folder %s", dfolder);
357
358 /* check for empty folder */
359 if (mp->nummsg == 0)
360 adios (NULL, "no messages in %s", dfolder);
361
362 /* parse all the message ranges/sequences and set SELECTED */
363 for (msgnum = 0; msgnum < msgp; msgnum++)
364 if (!m_convert (mp, msgs[msgnum]))
365 done (1);
366 seq_setprev (mp); /* set the previous-sequence */
367
368 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
369 if (is_selected (mp, msgnum)) {
370 msgs[msgp++] = mh_xstrdup(m_name (msgnum));
371 unset_exists (mp, msgnum);
372 }
373 }
374
375 mp->msgflags |= SEQMOD;
376 seq_save (mp);
377 }
378
379 #ifdef WHATNOW
380 go_to_it:
381 #endif /* WHATNOW */
382
383 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
384 if ((cp = context_find ("signature")) && *cp)
385 setenv("SIGNATURE", cp, 1);
386
387 for (msgnum = 0; msgnum < msgp; msgnum++)
388 if (stat (msgs[msgnum], &st) == NOTOK)
389 adios (msgs[msgnum], "unable to stat draft file");
390
391 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
392 annotext = NULL;
393 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
394 inplace = atoi (cp);
395 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
396 altmsg = NULL; /* used by dist interface - see below */
397
398 if ((cp = getenv ("mhdist"))
399 && *cp
400 && (distsw = atoi (cp))
401 && altmsg) {
402 vec[vecp++] = "-dist";
403 if ((cp = m_mktemp2(altmsg, invo_name, NULL, NULL)) == NULL) {
404 adios(NULL, "unable to create temporary file in %s",
405 get_temp_dir());
406 }
407 distfile = mh_xstrdup(cp);
408 (void) m_unlink(distfile);
409 if (link (altmsg, distfile) == NOTOK) {
410 /* Cygwin with FAT32 filesystem produces EPERM. */
411 if (errno != EXDEV && errno != EPERM
412 #ifdef EISREMOTE
413 && errno != EISREMOTE
414 #endif /* EISREMOTE */
415 )
416 adios (distfile, "unable to link %s to", altmsg);
417 free (distfile);
418 if ((cp = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
419 adios(NULL, "unable to create temporary file in %s",
420 get_temp_dir());
421 }
422 distfile = mh_xstrdup(cp);
423 {
424 int in, out;
425 struct stat st;
426
427 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
428 adios (altmsg, "unable to open");
429 fstat(in, &st);
430 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
431 adios (distfile, "unable to write");
432 cpydata (in, out, altmsg, distfile);
433 close (in);
434 close (out);
435 }
436 }
437 } else {
438 distfile = NULL;
439 }
440
441 #ifdef OAUTH_SUPPORT
442 if (auth_svc == NULL) {
443 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
444 adios (NULL, "must specify -authservice with -saslmech xoauth2");
445 }
446 } else {
447 if (user == NULL) {
448 adios (NULL, "must specify -user with -saslmech xoauth2");
449 }
450 }
451 #else
452 NMH_UNUSED(auth_svc);
453 NMH_UNUSED(user);
454 NMH_UNUSED(saslmech);
455 #endif /* OAUTH_SUPPORT */
456
457 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
458 st.st_mtime = 0;
459 st.st_dev = 0;
460 st.st_ino = 0;
461 }
462 if (pushsw)
463 push ();
464
465 status = 0;
466 closefds (3);
467
468 for (msgnum = 0; msgnum < msgp; msgnum++) {
469 switch (sendsbr (vec, vecp, program, msgs[msgnum], &st, 1, auth_svc)) {
470 case DONE:
471 done (++status);
472 /* FALLTHRU */
473 case NOTOK:
474 status++;
475 /* FALLTHRU */
476 case OK:
477 break;
478 }
479 }
480
481 context_save (); /* save the context file */
482 done (status);
483 return 1;
484 }