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