]> diplodocus.org Git - nmh/blob - uip/sendsbr.c
1b65efb88d58ada08dffea123af5391bd6b5a911
[nmh] / uip / sendsbr.c
1 /* sendsbr.c -- routines to help WhatNow/Send along
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 "sbr/m_getfld.h"
10 #include "sbr/concat.h"
11 #include "sbr/cpydgst.h"
12 #include "sbr/trimcpy.h"
13 #include "sbr/uprf.h"
14 #include "sbr/getcpy.h"
15 #include "sbr/m_convert.h"
16 #include "sbr/m_backup.h"
17 #include "sbr/folder_read.h"
18 #include "sbr/folder_free.h"
19 #include "sbr/context_find.h"
20 #include "sbr/brkstring.h"
21 #include "sbr/pidstatus.h"
22 #include "sbr/arglist.h"
23 #include "sbr/error.h"
24 #include "h/fmt_scan.h"
25 #include "h/fmt_compile.h"
26 #include "h/signals.h"
27 #include <setjmp.h>
28 #include <fcntl.h>
29 #include "h/mime.h"
30 #include "h/tws.h"
31 #include "h/utils.h"
32 #include "h/mts.h"
33
34 #ifdef HAVE_SYS_TIME_H
35 # include <sys/time.h>
36 #endif
37 #include <time.h>
38
39 #ifdef OAUTH_SUPPORT
40 #include "h/oauth.h"
41 #endif
42 #include "h/done.h"
43 #include "sbr/m_maildir.h"
44 #include "sbr/m_mktemp.h"
45 #include "sbr/message_id.h"
46
47 #ifdef OAUTH_SUPPORT
48 static int setup_oauth_params(char *[], int *, const char *, const char **);
49 #endif /* OAUTH_SUPPORT */
50
51 int debugsw = 0; /* global */
52 bool forwsw = true;
53 int inplace = 1;
54 bool pushsw;
55 int splitsw = -1;
56 bool unique;
57 bool verbsw;
58
59 char *altmsg = NULL; /* .. */
60 char *annotext = NULL;
61 char *distfile = NULL;
62
63 static jmp_buf env;
64
65 /*
66 * static prototypes
67 */
68 static void alert (char *, int);
69 static int tmp_fd (void);
70 static void anno (int, struct stat *);
71 static void annoaux (int);
72 static int splitmsg (char **, int, char *, char *, struct stat *, int);
73 static int sendaux (char **, int, char *, char *, struct stat *);
74 static void handle_sendfrom(char **, int *, char *, const char *);
75 static int get_from_header_info(const char *, const char **, const char **, const char **);
76 static const char *get_message_header_info(FILE *, char *);
77 static void merge_profile_entry(const char *, const char *, char *[], int *);
78 static void armed_done (int) NORETURN;
79
80 /*
81 * Entry point into (back-end) routines to send message.
82 */
83
84 int
85 sendsbr (char **vec, int vecp, char *program, char *draft, struct stat *st,
86 int rename_drft, const char *auth_svc)
87 {
88 int status, i;
89 pid_t child;
90 char buffer[BUFSIZ], file[BUFSIZ];
91 struct stat sts;
92 char **buildvec, *buildprogram;
93 char *volatile drft = draft;
94 /* nvecs is volatile to prevent warning from gcc about possible clobbering
95 by longjmp. */
96 volatile int nvecs = vecp;
97 int *nvecsp = (int *) &nvecs;
98
99 /*
100 * Run the mimebuildproc (which is by default mhbuild) on the message
101 * with the addition of the "-auto" flag
102 */
103
104 switch (child = fork()) {
105 case NOTOK:
106 adios("fork", "unable to");
107 break;
108
109 case OK:
110 buildvec = argsplit(buildmimeproc, &buildprogram, &i);
111 buildvec[i++] = "-auto";
112 if (distfile)
113 buildvec[i++] = "-dist";
114 buildvec[i++] = (char *) drft;
115 buildvec[i] = NULL;
116 execvp(buildprogram, buildvec);
117 fprintf(stderr, "unable to exec ");
118 perror(buildmimeproc);
119 _exit(1);
120 break;
121
122 default:
123 if (pidXwait(child, buildmimeproc))
124 return NOTOK;
125 break;
126 }
127
128 set_done(armed_done);
129 switch (setjmp (env)) {
130 case OK:
131 /*
132 * If given -push and -unique (which is undocumented), then
133 * rename the draft file. I'm not quite sure why.
134 */
135 if (pushsw && unique) {
136 char *cp = m_mktemp2(drft, invo_name, NULL, NULL);
137 if (cp == NULL) {
138 die("unable to create temporary file");
139 }
140 if (rename (drft, strncpy(file, cp, sizeof(file))) == NOTOK)
141 adios (file, "unable to rename %s to", drft);
142 drft = file;
143 }
144
145 /*
146 * Add in any necessary profile entries for xoauth
147 */
148
149 if (auth_svc) {
150 #ifdef OAUTH_SUPPORT
151 const char *errmsg;
152 if (setup_oauth_params(vec, nvecsp, auth_svc, &errmsg) != OK) {
153 die("%s", errmsg);
154 }
155 #else
156 die("send built without OAUTH_SUPPORT, "
157 "so auth_svc %s is not supported", auth_svc);
158 #endif /* OAUTH_SUPPORT */
159 }
160
161 /*
162 * Rework the vec based on From: header in draft, as specified
163 * by sendfrom-address entries in profile.
164 */
165 if (context_find_prefix("sendfrom-")) {
166 handle_sendfrom(vec, nvecsp, draft, auth_svc);
167 }
168
169 /*
170 * Check if we need to split the message into
171 * multiple messages of type "message/partial".
172 */
173 if (splitsw >= 0 && !distfile && stat ((char *) drft, &sts) != NOTOK
174 && sts.st_size >= CPERMSG) {
175 status = splitmsg (vec, nvecs, program, drft,
176 st, splitsw) ? NOTOK : OK;
177 } else {
178 status = sendaux (vec, nvecs, program, drft, st) ? NOTOK : OK;
179 }
180
181 /* rename the original draft */
182 if (rename_drft && status == OK &&
183 rename (drft, strncpy (buffer, m_backup (drft),
184 sizeof(buffer))) == NOTOK)
185 advise (buffer, "unable to rename %s to", drft);
186 break;
187
188 default:
189 status = DONE;
190 break;
191 }
192
193 set_done(exit);
194 if (distfile)
195 (void) m_unlink (distfile);
196
197 return status;
198 }
199
200 /*
201 * Split large message into several messages of
202 * type "message/partial" and send them.
203 */
204
205 static int
206 splitmsg (char **vec, int vecp, char *program, char *drft,
207 struct stat *st, int delay)
208 {
209 int compnum, nparts, partno, state, status;
210 long pos, start;
211 time_t clock;
212 char *cp, *dp, buffer[NMH_BUFSIZ], msgid[BUFSIZ];
213 char subject[BUFSIZ];
214 char name[NAMESZ], partnum[BUFSIZ];
215 FILE *in;
216 m_getfld_state_t gstate;
217
218 if ((in = fopen (drft, "r")) == NULL)
219 adios (drft, "unable to open for reading");
220
221 cp = dp = NULL;
222 start = 0L;
223
224 /*
225 * Scan through the message and examine the various header fields,
226 * as well as locate the beginning of the message body.
227 */
228 gstate = m_getfld_state_init(in);
229 m_getfld_track_filepos2(&gstate);
230 for (compnum = 1;;) {
231 int bufsz = sizeof buffer;
232 switch (state = m_getfld2(&gstate, name, buffer, &bufsz)) {
233 case FLD:
234 case FLDPLUS:
235 compnum++;
236
237 /*
238 * This header field is discarded.
239 */
240 if (!strcasecmp (name, "Message-ID")) {
241 while (state == FLDPLUS) {
242 bufsz = sizeof buffer;
243 state = m_getfld2(&gstate, name, buffer, &bufsz);
244 }
245 } else if (uprf (name, XXX_FIELD_PRF)
246 || !strcasecmp (name, VRSN_FIELD)
247 || !strcasecmp (name, "Subject")
248 || !strcasecmp (name, "Encrypted")) {
249 /*
250 * These header fields are copied to the enclosed
251 * header of the first message in the collection
252 * of message/partials. For the "Subject" header
253 * field, we also record it, so that a modified
254 * version of it, can be copied to the header
255 * of each message/partial in the collection.
256 */
257 if (!strcasecmp (name, "Subject")) {
258 size_t sublen;
259
260 strncpy (subject, buffer, BUFSIZ);
261 sublen = strlen (subject);
262 if (sublen > 0 && subject[sublen - 1] == '\n')
263 subject[sublen - 1] = '\0';
264 }
265
266 dp = add (concat (name, ":", buffer, NULL), dp);
267 while (state == FLDPLUS) {
268 bufsz = sizeof buffer;
269 state = m_getfld2(&gstate, name, buffer, &bufsz);
270 dp = add (buffer, dp);
271 }
272 } else {
273 /*
274 * These header fields are copied to the header of
275 * each message/partial in the collection.
276 */
277 cp = add (concat (name, ":", buffer, NULL), cp);
278 while (state == FLDPLUS) {
279 bufsz = sizeof buffer;
280 state = m_getfld2(&gstate, name, buffer, &bufsz);
281 cp = add (buffer, cp);
282 }
283 }
284
285 start = ftell (in) + 1;
286 continue;
287
288 case BODY:
289 case FILEEOF:
290 break;
291
292 case LENERR:
293 case FMTERR:
294 die("message format error in component #%d", compnum);
295
296 default:
297 die("getfld () returned %d", state);
298 }
299
300 break;
301 }
302 m_getfld_state_destroy (&gstate);
303 if (cp == NULL)
304 die("headers missing from draft");
305
306 nparts = 1;
307 pos = start;
308 while (fgets (buffer, sizeof buffer, in)) {
309 long len;
310
311 if ((pos += (len = strlen (buffer))) > CPERMSG) {
312 nparts++;
313 pos = len;
314 }
315 }
316
317 /* Only one part, nothing to split */
318 if (nparts == 1) {
319 free (cp);
320 free(dp);
321
322 fclose (in);
323 return sendaux (vec, vecp, program, drft, st);
324 }
325
326 if (!pushsw) {
327 printf ("Sending as %d Partial Messages\n", nparts);
328 fflush (stdout);
329 }
330 status = OK;
331
332 vec[vecp++] = "-partno";
333 vec[vecp++] = partnum;
334 if (delay == 0)
335 vec[vecp++] = "-queued";
336
337 time (&clock);
338 snprintf (msgid, sizeof(msgid), "%s", message_id (clock, 0));
339
340 fseek (in, start, SEEK_SET);
341 for (partno = 1; partno <= nparts; partno++) {
342 char tmpdrf[BUFSIZ];
343 FILE *out;
344
345 char *cp = m_mktemp2(drft, invo_name, NULL, &out);
346 if (cp == NULL) {
347 die("unable to create temporary file");
348 }
349 strncpy(tmpdrf, cp, sizeof(tmpdrf));
350
351 /*
352 * Output the header fields
353 */
354 fputs (cp, out);
355 fprintf (out, "Subject: %s (part %d of %d)\n", subject, partno, nparts);
356 fprintf (out, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
357 fprintf (out, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD, msgid);
358 fprintf (out, "\tnumber=%d; total=%d\n", partno, nparts);
359 fprintf (out, "%s: part %d of %d\n\n", DESCR_FIELD, partno, nparts);
360
361 /*
362 * If this is the first in the collection, output the
363 * header fields we are encapsulating at the beginning
364 * of the body of the first message.
365 */
366 if (partno == 1) {
367 if (dp)
368 fputs (dp, out);
369 fprintf (out, "Message-ID: %s\n", msgid);
370 fprintf (out, "\n");
371 }
372
373 pos = 0;
374 for (;;) {
375 long len;
376
377 if (!fgets (buffer, sizeof buffer, in)) {
378 if (partno == nparts)
379 break;
380 die("premature eof");
381 }
382
383 if ((pos += (len = strlen (buffer))) > CPERMSG) {
384 fseek (in, -len, SEEK_CUR);
385 break;
386 }
387
388 fputs (buffer, out);
389 }
390
391 if (fflush (out))
392 adios (tmpdrf, "error writing to");
393
394 fclose (out);
395
396 if (!pushsw && verbsw) {
397 putchar('\n');
398 fflush (stdout);
399 }
400
401 /* Pause here, if a delay is specified */
402 if (delay > 0 && 1 < partno && partno <= nparts) {
403 if (!pushsw) {
404 printf ("pausing %d seconds before sending part %d...\n",
405 delay, partno);
406 fflush (stdout);
407 }
408 sleep ((unsigned int) delay);
409 }
410
411 snprintf (partnum, sizeof(partnum), "%d", partno);
412 status = sendaux (vec, vecp, program, tmpdrf, st);
413 (void) m_unlink (tmpdrf);
414 if (status != OK)
415 break;
416
417 /*
418 * This is so sendaux will only annotate
419 * the altmsg the first time it is called.
420 */
421 annotext = NULL;
422 }
423
424 free (cp);
425 free(dp);
426
427 fclose (in); /* close the draft */
428 return status;
429 }
430
431
432 /*
433 * Annotate original message, and
434 * call `postproc' (which is passed down in "program") to send message.
435 */
436
437 static int
438 sendaux (char **vec, int vecp, char *program, char *drft, struct stat *st)
439 {
440 pid_t child_id;
441 int status, fd, fd2;
442 char backup[BUFSIZ], buf[BUFSIZ];
443
444 fd = pushsw ? tmp_fd () : NOTOK;
445 fd2 = NOTOK;
446
447 if (annotext) {
448 if ((fd2 = tmp_fd ()) != NOTOK) {
449 vec[vecp++] = "-idanno";
450 snprintf (buf, sizeof(buf), "%d", fd2);
451 vec[vecp++] = buf;
452 } else {
453 inform("unable to create temporary file in %s for "
454 "annotation list, continuing...", get_temp_dir());
455 }
456 }
457 vec[vecp++] = drft;
458 if (distfile && distout (drft, distfile, backup) == NOTOK)
459 done (1);
460 vec[vecp] = NULL;
461
462 child_id = fork();
463 switch (child_id) {
464 case -1:
465 /* oops -- fork error */
466 adios ("fork", "unable to");
467 break; /* NOT REACHED */
468
469 case 0:
470 /*
471 * child process -- send it
472 *
473 * If fd is OK, then we are pushing and fd points to temp
474 * file, so capture anything on stdout and stderr there.
475 */
476 if (fd != NOTOK) {
477 dup2 (fd, fileno (stdout));
478 dup2 (fd, fileno (stderr));
479 close (fd);
480 }
481 execvp (program, vec);
482 fprintf (stderr, "unable to exec ");
483 perror (postproc);
484 _exit(1);
485
486 default:
487 /*
488 * parent process -- wait for it
489 */
490 if ((status = pidwait(child_id, NOTOK)) == OK) {
491 if (annotext && fd2 != NOTOK)
492 anno (fd2, st);
493 } else {
494 /*
495 * If postproc failed, and we have good fd (which means
496 * we pushed), then mail error message (and possibly the
497 * draft) back to the user.
498 */
499 if (fd != NOTOK) {
500 alert (drft, fd);
501 close (fd);
502 } else {
503 inform("message not delivered to anyone");
504 }
505 if (annotext && fd2 != NOTOK)
506 close (fd2);
507 if (distfile) {
508 (void) m_unlink (drft);
509 if (rename (backup, drft) == NOTOK)
510 advise (drft, "unable to rename %s to", backup);
511 }
512 }
513 break;
514 }
515
516 return status;
517 }
518
519
520 /*
521 * Mail error notification (and possibly a copy of the
522 * message) back to the user, using the mailproc
523 */
524
525 static void
526 alert (char *file, int out)
527 {
528 pid_t child_id;
529 int in, argp;
530 char buf[BUFSIZ];
531 char *program;
532 char **arglist;
533
534 child_id = fork();
535 switch (child_id) {
536 case NOTOK:
537 /* oops -- fork error */
538 advise ("fork", "unable to");
539 /* FALLTHRU */
540
541 case OK:
542 /* child process -- send it */
543 SIGNAL (SIGHUP, SIG_IGN);
544 SIGNAL (SIGINT, SIG_IGN);
545 SIGNAL (SIGQUIT, SIG_IGN);
546 SIGNAL (SIGTERM, SIG_IGN);
547 if (forwsw) {
548 if ((in = open (file, O_RDONLY)) == NOTOK) {
549 admonish (file, "unable to re-open");
550 } else {
551 lseek(out, 0, SEEK_END);
552 strncpy (buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
553 if (write (out, buf, strlen (buf)) < 0) {
554 advise (file, "write");
555 }
556 strncpy (buf, "\n------- Unsent Draft\n\n", sizeof(buf));
557 if (write (out, buf, strlen (buf)) < 0) {
558 advise (file, "write");
559 }
560 cpydgst (in, out, file, "temporary file");
561 close (in);
562 strncpy (buf, "\n------- End of Unsent Draft\n", sizeof(buf));
563 if (write (out, buf, strlen (buf)) < 0) {
564 advise (file, "write");
565 }
566 if (rename (file, strncpy (buf, m_backup (file), sizeof(buf))) == NOTOK)
567 admonish (buf, "unable to rename %s to", file);
568 }
569 }
570 lseek(out, 0, SEEK_SET);
571 dup2 (out, fileno (stdin));
572 close (out);
573 /* create subject for error notification */
574 snprintf (buf, sizeof(buf), "send failed on %s",
575 forwsw ? "enclosed draft" : file);
576
577 arglist = argsplit(mailproc, &program, &argp);
578
579 arglist[argp++] = getusername();
580 arglist[argp++] = "-subject";
581 arglist[argp++] = buf;
582 arglist[argp] = NULL;
583
584 execvp (program, arglist);
585 fprintf (stderr, "unable to exec ");
586 perror (mailproc);
587 _exit(1);
588
589 default: /* no waiting... */
590 break;
591 }
592 }
593
594
595 static int
596 tmp_fd (void)
597 {
598 int fd;
599 char *tfile;
600
601 if ((tfile = m_mktemp2(NULL, invo_name, &fd, NULL)) == NULL) return NOTOK;
602
603 if (debugsw)
604 inform("temporary file %s selected", tfile);
605 else if (m_unlink (tfile) == NOTOK)
606 advise (tfile, "unable to remove");
607
608 return fd;
609 }
610
611
612 static void
613 anno (int fd, struct stat *st)
614 {
615 pid_t child_id;
616 sigset_t set, oset;
617 static char *cwd = NULL;
618 struct stat st2;
619
620 if (altmsg &&
621 (stat (altmsg, &st2) == NOTOK
622 || st->st_mtime != st2.st_mtime
623 || st->st_dev != st2.st_dev
624 || st->st_ino != st2.st_ino)) {
625 if (debugsw)
626 inform("$mhaltmsg mismatch, continuing...");
627 return;
628 }
629
630 child_id = debugsw ? NOTOK : fork ();
631 switch (child_id) {
632 case NOTOK: /* oops */
633 if (!debugsw)
634 inform("unable to fork, so doing annotations by hand...");
635 if (cwd == NULL)
636 cwd = mh_xstrdup(pwd ());
637 /* FALLTHRU */
638
639 case OK:
640 /* block a few signals */
641 sigemptyset (&set);
642 sigaddset (&set, SIGHUP);
643 sigaddset (&set, SIGINT);
644 sigaddset (&set, SIGQUIT);
645 sigaddset (&set, SIGTERM);
646 sigprocmask (SIG_BLOCK, &set, &oset);
647
648 unregister_for_removal(0);
649
650 annoaux (fd);
651 if (child_id == OK)
652 _exit (0);
653
654 /* reset the signal mask */
655 sigprocmask (SIG_SETMASK, &oset, &set);
656
657 if (chdir (cwd) < 0) {
658 advise (cwd, "chdir");
659 }
660 break;
661
662 default: /* no waiting... */
663 close (fd);
664 break;
665 }
666 }
667
668
669 static void
670 annoaux (int fd)
671 {
672 int fd2, fd3, msgnum;
673 char *cp, *folder, *maildir;
674 char buffer[BUFSIZ], **ap;
675 FILE *fp;
676 struct msgs *mp;
677
678 if ((folder = getenv ("mhfolder")) == NULL || *folder == 0) {
679 if (debugsw)
680 inform("$mhfolder not set, continuing...");
681 return;
682 }
683 maildir = m_maildir (folder);
684 if (chdir (maildir) == NOTOK) {
685 if (debugsw)
686 admonish (maildir, "unable to change directory to");
687 return;
688 }
689 if (!(mp = folder_read (folder, 0))) {
690 if (debugsw)
691 inform("unable to read folder %s, continuing...", folder);
692 return;
693 }
694
695 /* check for empty folder */
696 if (mp->nummsg == 0) {
697 if (debugsw)
698 inform("no messages in %s, continuing...", folder);
699 goto oops;
700 }
701
702 if ((cp = getenv ("mhmessages")) == NULL || *cp == 0) {
703 if (debugsw)
704 inform("$mhmessages not set, continuing...");
705 goto oops;
706 }
707 if (!debugsw /* MOBY HACK... */
708 && pushsw
709 && (fd3 = open ("/dev/null", O_RDWR)) != NOTOK
710 && (fd2 = dup (fileno (stderr))) != NOTOK) {
711 dup2 (fd3, fileno (stderr));
712 close (fd3);
713 }
714 else
715 fd2 = NOTOK;
716 for (ap = brkstring (cp = mh_xstrdup(cp), " ", NULL); *ap; ap++)
717 m_convert (mp, *ap);
718 free (cp);
719 if (fd2 != NOTOK)
720 dup2 (fd2, fileno (stderr));
721 if (mp->numsel == 0) {
722 if (debugsw)
723 inform("no messages to annotate, continuing...");
724 goto oops;
725 }
726
727 lseek(fd, 0, SEEK_SET);
728 if ((fp = fdopen (fd, "r")) == NULL) {
729 if (debugsw)
730 inform("unable to fdopen annotation list, continuing...");
731 goto oops;
732 }
733 cp = NULL;
734 while (fgets (buffer, sizeof(buffer), fp) != NULL)
735 cp = add (buffer, cp);
736 fclose (fp);
737
738 if (debugsw)
739 inform("annotate%s with %s: \"%s\"",
740 inplace ? " inplace" : "", annotext, cp);
741 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
742 if (is_selected(mp, msgnum)) {
743 if (debugsw)
744 inform("annotate message %d", msgnum);
745 annotate (m_name (msgnum), annotext, cp, inplace, 1, -2, 0);
746 }
747 }
748
749 free (cp);
750
751 oops:
752 folder_free (mp); /* free folder/message structure */
753 }
754
755
756 static void
757 handle_sendfrom(char **vec, int *vecp, char *draft, const char *auth_svc)
758 {
759 const char *addr, *host;
760 const char *message;
761
762 /* Extract address and host from From: header line in draft. */
763 if (get_from_header_info(draft, &addr, &host, &message) != OK) {
764 adios(draft, "%s", message);
765 }
766
767 /* Merge in any address or host specific switches to post(1) from profile. */
768 merge_profile_entry(addr, host, vec, vecp);
769 free((void *) host);
770 free((void *) addr);
771
772 vec[*vecp] = NULL;
773
774 {
775 char **vp;
776
777 for (vp = vec; *vp; ++vp) {
778 if (strcmp(*vp, "xoauth2") == 0) {
779 #ifdef OAUTH_SUPPORT
780 if (setup_oauth_params(vec, vecp, auth_svc, &message) != OK) {
781 die("%s", message);
782 }
783 break;
784 #else
785 NMH_UNUSED(auth_svc);
786 die("send built without OAUTH_SUPPORT, "
787 "so -saslmech xoauth2 is not supported");
788 #endif /* OAUTH_SUPPORT */
789 }
790 }
791 }
792 }
793
794
795 #ifdef OAUTH_SUPPORT
796 /*
797 * For XOAUTH2, append profile entries so post can do the heavy lifting
798 */
799 static int
800 setup_oauth_params(char *vec[], int *vecp, const char *auth_svc,
801 const char **message)
802 {
803 const char *saslmech = NULL, *user = NULL;
804 mh_oauth_service_info svc;
805 char errbuf[256];
806 int i;
807
808 /* Make sure we have all the information we need. */
809 for (i = 1; i < *vecp; ++i) {
810 /* Don't support abbreviated switches, to avoid collisions in the
811 future if new ones are added. */
812 if (! strcmp(vec[i-1], "-saslmech")) {
813 saslmech = vec[i];
814 } else if (! strcmp(vec[i-1], "-user")) {
815 user = vec[i];
816 } else if (! strcmp(vec[i-1], "-authservice")) {
817 auth_svc = vec[i];
818 }
819 }
820
821 if (auth_svc == NULL) {
822 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
823 *message = "must specify -authservice with -saslmech xoauth2";
824 return NOTOK;
825 }
826 } else {
827 if (user == NULL) {
828 *message = "must specify -user with -saslmech xoauth2";
829 return NOTOK;
830 }
831
832 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
833 if (! mh_oauth_get_service_info(auth_svc, &svc, errbuf,
834 sizeof(errbuf)))
835 die("Unable to retrieve oauth profile entries: %s",
836 errbuf);
837
838 vec[(*vecp)++] = mh_xstrdup("-authservice");
839 vec[(*vecp)++] = mh_xstrdup(auth_svc);
840 vec[(*vecp)++] = mh_xstrdup("-oauthcredfile");
841 vec[(*vecp)++] = mh_oauth_cred_fn(auth_svc);
842 vec[(*vecp)++] = mh_xstrdup("-oauthclientid");
843 vec[(*vecp)++] = getcpy(svc.client_id);
844 vec[(*vecp)++] = mh_xstrdup("-oauthclientsecret");
845 vec[(*vecp)++] = getcpy(svc.client_secret);
846 vec[(*vecp)++] = mh_xstrdup("-oauthauthendpoint");
847 vec[(*vecp)++] = getcpy(svc.auth_endpoint);
848 vec[(*vecp)++] = mh_xstrdup("-oauthredirect");
849 vec[(*vecp)++] = getcpy(svc.redirect_uri);
850 vec[(*vecp)++] = mh_xstrdup("-oauthtokenendpoint");
851 vec[(*vecp)++] = getcpy(svc.token_endpoint);
852 vec[(*vecp)++] = mh_xstrdup("-oauthscope");
853 vec[(*vecp)++] = getcpy(svc.scope);
854 }
855 }
856
857 return 0;
858 }
859 #endif /* OAUTH_SUPPORT */
860
861
862 /*
863 * Extract user and domain from From: header line in draft.
864 */
865 static int
866 get_from_header_info(const char *filename, const char **addr, const char **host, const char **message)
867 {
868 struct stat st;
869 FILE *in;
870
871 if (stat (filename, &st) == NOTOK) {
872 *message = "unable to stat draft file";
873 return NOTOK;
874 }
875
876 if ((in = fopen (filename, "r")) != NULL) {
877 /* There must be a non-blank Envelope-From or {Resent-}Sender or
878 {Resent-}From header. */
879 char *addrformat = "%(addr{Envelope-From})";
880 char *hostformat = "%(host{Envelope-From})";
881
882 if ((*addr = get_message_header_info (in, addrformat)) == NULL ||
883 !**addr) {
884 addrformat = distfile == NULL ? "%(addr{Sender})" : "%(addr{Resent-Sender})";
885 hostformat = distfile == NULL ? "%(host{Sender})" : "%(host{Resent-Sender})";
886
887 if ((*addr = get_message_header_info (in, addrformat)) == NULL) {
888 addrformat = distfile == NULL ? "%(addr{From})" : "%(addr{Resent-From})";
889 hostformat = distfile == NULL ? "%(host{From})" : "%(host{Resent-From})";
890
891 if ((*addr = get_message_header_info (in, addrformat)) == NULL) {
892 *message = "unable to find sender address in";
893 fclose(in);
894 return NOTOK;
895 }
896 }
897 }
898
899 /* Use the hostformat that corresponds to the successful addrformat. */
900 if ((*host = get_message_header_info(in, hostformat)) == NULL) {
901 *message = "unable to find sender host";
902 fclose(in);
903 return NOTOK;
904 }
905 fclose(in);
906
907 return OK;
908 }
909
910 *message = "unable to open";
911 return NOTOK;
912 }
913
914
915 /*
916 * Get formatted information from header of a message.
917 * Adapted from process_single_file() in uip/fmttest.c.
918 */
919 static const char *
920 get_message_header_info(FILE *in, char *format)
921 {
922 int dat[5];
923 struct format *fmt;
924 struct stat st;
925 bool parsing_header;
926 m_getfld_state_t gstate;
927 charstring_t buffer = charstring_create(0);
928 char *retval;
929
930 dat[0] = dat[1] = dat[4] = 0;
931 dat[2] = fstat(fileno(in), &st) == 0 ? st.st_size : 0;
932 dat[3] = INT_MAX;
933
934 (void) fmt_compile(new_fs(NULL, NULL, format), &fmt, 1);
935 free_fs();
936
937 /*
938 * Read in the message and process the header.
939 */
940 rewind (in);
941 parsing_header = true;
942 gstate = m_getfld_state_init(in);
943 do {
944 char name[NAMESZ], rbuf[NMH_BUFSIZ];
945 int bufsz = sizeof rbuf;
946 int state = m_getfld2(&gstate, name, rbuf, &bufsz);
947
948 switch (state) {
949 case FLD:
950 case FLDPLUS: {
951 int bucket = fmt_addcomptext(name, rbuf);
952
953 if (bucket != -1) {
954 while (state == FLDPLUS) {
955 bufsz = sizeof rbuf;
956 state = m_getfld2(&gstate, name, rbuf, &bufsz);
957 fmt_appendcomp(bucket, name, rbuf);
958 }
959 }
960
961 while (state == FLDPLUS) {
962 bufsz = sizeof rbuf;
963 state = m_getfld2(&gstate, name, rbuf, &bufsz);
964 }
965 break;
966 }
967 default:
968 parsing_header = false;
969 }
970 } while (parsing_header);
971 m_getfld_state_destroy(&gstate);
972
973 fmt_scan(fmt, buffer, INT_MAX, dat, NULL);
974 fmt_free(fmt, 1);
975
976 /* Trim trailing newline, if any. */
977 retval = rtrim(charstring_buffer_copy((buffer)));
978 charstring_free(buffer);
979 if (*retval)
980 return retval;
981
982 free(retval);
983 return NULL;
984 }
985
986
987 /*
988 * Look in profile for entry corresponding to addr or host, and add its contents to vec.
989 *
990 * Could do some of this automatically, by looking for:
991 * 1) access-$(mbox{from}) in oauth-svc file using mh_oauth_cred_load(), which isn't
992 * static and doesn't have side effects; free the result with mh_oauth_cred_free())
993 * 2) machine $(mbox{from}) in creds
994 * If no -server passed in from profile or commandline, could use smtp.<svc>.com for gmail,
995 * but that might not generalize for other svcs.
996 */
997 static void
998 merge_profile_entry(const char *addr, const char *host, char *vec[], int *vecp)
999 {
1000 char *addr_entry = concat("sendfrom-", addr, NULL);
1001 char *profile_entry = context_find(addr_entry);
1002
1003 free(addr_entry);
1004 if (profile_entry == NULL) {
1005 /* No entry for the user. Look for one for the host. */
1006 char *host_entry = concat("sendfrom-", host, NULL);
1007
1008 profile_entry = context_find(host_entry);
1009 free(host_entry);
1010 }
1011
1012 /* Use argsplit() to do the real work of splitting the args in the profile entry. */
1013 if (profile_entry && *profile_entry) {
1014 int profile_vecp;
1015 char *file;
1016 char **profile_vec = argsplit(profile_entry, &file, &profile_vecp);
1017 int i;
1018
1019 for (i = 0; i < profile_vecp; ++i) {
1020 vec[(*vecp)++] = getcpy(profile_vec[i]);
1021 }
1022
1023 arglist_free(file, profile_vec);
1024 }
1025 }
1026
1027
1028 static void NORETURN
1029 armed_done (int status)
1030 {
1031 longjmp (env, status ? status : NOTOK);
1032 }