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