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