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