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