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