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