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