]> diplodocus.org Git - nmh/blob - uip/sendsbr.c
Use existing macros min() and max() more.
[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
27 static int setup_oauth_params(char *[], int *, const char *, const char **);
28 #endif /* OAUTH_SUPPORT */
29
30 int debugsw = 0; /* global */
31 int forwsw = 1;
32 int inplace = 1;
33 int pushsw = 0;
34 int splitsw = -1;
35 int unique = 0;
36 int verbsw = 0;
37
38 char *altmsg = NULL; /* .. */
39 char *annotext = NULL;
40 char *distfile = NULL;
41
42 static jmp_buf env;
43
44 /*
45 * static prototypes
46 */
47 static void alert (char *, int);
48 static int tmp_fd (void);
49 static void anno (int, struct stat *);
50 static void annoaux (int);
51 static int splitmsg (char **, int, char *, char *, struct stat *, int);
52 static int sendaux (char **, int, char *, char *, struct stat *);
53 static void handle_sendfrom(char **, int *, char *, const char *);
54 static int get_from_header_info(const char *, const char **, const char **, const char **);
55 static const char *get_message_header_info(FILE *, char *);
56 static void merge_profile_entry(const char *, const char *, char *[], int *);
57 static void armed_done (int) NORETURN;
58
59 /*
60 * Entry point into (back-end) routines to send message.
61 */
62
63 int
64 sendsbr (char **vec, int vecp, char *program, char *draft, struct stat *st,
65 int rename_drft, const char *auth_svc)
66 {
67 int status, i;
68 pid_t child;
69 char buffer[BUFSIZ], file[BUFSIZ];
70 struct stat sts;
71 char **buildvec, *buildprogram;
72 char *volatile drft = draft;
73 /* nvecs is volatile to prevent warning from gcc about possible clobbering
74 by longjmp. */
75 volatile int nvecs = vecp;
76 int *nvecsp = (int *) &nvecs;
77
78 /*
79 * Run the mimebuildproc (which is by default mhbuild) on the message
80 * with the addition of the "-auto" flag
81 */
82
83 switch (child = fork()) {
84 case NOTOK:
85 adios("fork", "unable to");
86 break;
87
88 case OK:
89 buildvec = argsplit(buildmimeproc, &buildprogram, &i);
90 buildvec[i++] = "-auto";
91 if (distfile)
92 buildvec[i++] = "-dist";
93 buildvec[i++] = (char *) drft;
94 buildvec[i] = NULL;
95 execvp(buildprogram, buildvec);
96 fprintf(stderr, "unable to exec ");
97 perror(buildmimeproc);
98 _exit(-1);
99 break;
100
101 default:
102 if (pidXwait(child, buildmimeproc))
103 return NOTOK;
104 break;
105 }
106
107 done=armed_done;
108 switch (setjmp (env)) {
109 case OK:
110 /*
111 * If given -push and -unique (which is undocumented), then
112 * rename the draft file. I'm not quite sure why.
113 */
114 if (pushsw && unique) {
115 char *cp = m_mktemp2(drft, invo_name, NULL, NULL);
116 if (cp == NULL) {
117 adios(NULL, "unable to create temporary file in %s",
118 get_temp_dir());
119 }
120 if (rename (drft, strncpy(file, cp, sizeof(file))) == NOTOK)
121 adios (file, "unable to rename %s to", drft);
122 drft = file;
123 }
124
125 /*
126 * Add in any necessary profile entries for xoauth
127 */
128
129 if (auth_svc) {
130 #ifdef OAUTH_SUPPORT
131 const char *errmsg;
132 if (setup_oauth_params(vec, nvecsp, auth_svc, &errmsg) != OK) {
133 adios(NULL, errmsg);
134 }
135 #else
136 adios(NULL, "send built without OAUTH_SUPPORT, "
137 "so auth_svc %s is not supported", auth_svc);
138 #endif /* OAUTH_SUPPORT */
139 }
140
141 /*
142 * Rework the vec based on From: header in draft, as specified
143 * by sendfrom-address entries in profile.
144 */
145 if (context_find_prefix("sendfrom-")) {
146 handle_sendfrom(vec, nvecsp, draft, auth_svc);
147 }
148
149 /*
150 * Check if we need to split the message into
151 * multiple messages of type "message/partial".
152 */
153 if (splitsw >= 0 && !distfile && stat ((char *) drft, &sts) != NOTOK
154 && sts.st_size >= CPERMSG) {
155 status = splitmsg (vec, nvecs, program, drft,
156 st, splitsw) ? NOTOK : OK;
157 } else {
158 status = sendaux (vec, nvecs, program, drft, st) ? NOTOK : OK;
159 }
160
161 /* rename the original draft */
162 if (rename_drft && status == OK &&
163 rename (drft, strncpy (buffer, m_backup (drft),
164 sizeof(buffer))) == NOTOK)
165 advise (buffer, "unable to rename %s to", drft);
166 break;
167
168 default:
169 status = DONE;
170 break;
171 }
172
173 done=exit;
174 if (distfile)
175 (void) m_unlink (distfile);
176
177 return status;
178 }
179
180 /*
181 * Split large message into several messages of
182 * type "message/partial" and send them.
183 */
184
185 static int
186 splitmsg (char **vec, int vecp, char *program, char *drft,
187 struct stat *st, int delay)
188 {
189 int compnum, nparts, partno, state, status;
190 long pos, start;
191 time_t clock;
192 char *cp, *dp, buffer[NMH_BUFSIZ], msgid[BUFSIZ];
193 char subject[BUFSIZ];
194 char name[NAMESZ], partnum[BUFSIZ];
195 FILE *in;
196 m_getfld_state_t gstate = 0;
197
198 if ((in = fopen (drft, "r")) == NULL)
199 adios (drft, "unable to open for reading");
200
201 cp = dp = NULL;
202 start = 0L;
203
204 /*
205 * Scan through the message and examine the various header fields,
206 * as well as locate the beginning of the message body.
207 */
208 m_getfld_track_filepos (&gstate, in);
209 for (compnum = 1;;) {
210 int bufsz = sizeof buffer;
211 switch (state = m_getfld (&gstate, name, buffer, &bufsz, in)) {
212 case FLD:
213 case FLDPLUS:
214 compnum++;
215
216 /*
217 * This header field is discarded.
218 */
219 if (!strcasecmp (name, "Message-ID")) {
220 while (state == FLDPLUS) {
221 bufsz = sizeof buffer;
222 state = m_getfld (&gstate, name, buffer, &bufsz, in);
223 }
224 } else if (uprf (name, XXX_FIELD_PRF)
225 || !strcasecmp (name, VRSN_FIELD)
226 || !strcasecmp (name, "Subject")
227 || !strcasecmp (name, "Encrypted")) {
228 /*
229 * These header fields are copied to the enclosed
230 * header of the first message in the collection
231 * of message/partials. For the "Subject" header
232 * field, we also record it, so that a modified
233 * version of it, can be copied to the header
234 * of each message/partial in the collection.
235 */
236 if (!strcasecmp (name, "Subject")) {
237 size_t sublen;
238
239 strncpy (subject, buffer, BUFSIZ);
240 sublen = strlen (subject);
241 if (sublen > 0 && subject[sublen - 1] == '\n')
242 subject[sublen - 1] = '\0';
243 }
244
245 dp = add (concat (name, ":", buffer, NULL), dp);
246 while (state == FLDPLUS) {
247 bufsz = sizeof buffer;
248 state = m_getfld (&gstate, name, buffer, &bufsz, in);
249 dp = add (buffer, dp);
250 }
251 } else {
252 /*
253 * These header fields are copied to the header of
254 * each message/partial in the collection.
255 */
256 cp = add (concat (name, ":", buffer, NULL), cp);
257 while (state == FLDPLUS) {
258 bufsz = sizeof buffer;
259 state = m_getfld (&gstate, name, buffer, &bufsz, in);
260 cp = add (buffer, cp);
261 }
262 }
263
264 start = ftell (in) + 1;
265 continue;
266
267 case BODY:
268 case FILEEOF:
269 break;
270
271 case LENERR:
272 case FMTERR:
273 adios (NULL, "message format error in component #%d", compnum);
274
275 default:
276 adios (NULL, "getfld () returned %d", state);
277 }
278
279 break;
280 }
281 m_getfld_state_destroy (&gstate);
282 if (cp == NULL)
283 adios (NULL, "headers missing from draft");
284
285 nparts = 1;
286 pos = start;
287 while (fgets (buffer, sizeof buffer, in)) {
288 long len;
289
290 if ((pos += (len = strlen (buffer))) > CPERMSG) {
291 nparts++;
292 pos = len;
293 }
294 }
295
296 /* Only one part, nothing to split */
297 if (nparts == 1) {
298 free (cp);
299 mh_xfree(dp);
300
301 fclose (in);
302 return sendaux (vec, vecp, program, drft, st);
303 }
304
305 if (!pushsw) {
306 printf ("Sending as %d Partial Messages\n", nparts);
307 fflush (stdout);
308 }
309 status = OK;
310
311 vec[vecp++] = "-partno";
312 vec[vecp++] = partnum;
313 if (delay == 0)
314 vec[vecp++] = "-queued";
315
316 time (&clock);
317 snprintf (msgid, sizeof(msgid), "%s", message_id (clock, 0));
318
319 fseek (in, start, SEEK_SET);
320 for (partno = 1; partno <= nparts; partno++) {
321 char tmpdrf[BUFSIZ];
322 FILE *out;
323
324 char *cp = m_mktemp2(drft, invo_name, NULL, &out);
325 if (cp == NULL) {
326 adios(NULL, "unable to create temporary file in %s",
327 get_temp_dir());
328 }
329 strncpy(tmpdrf, cp, sizeof(tmpdrf));
330
331 /*
332 * Output the header fields
333 */
334 fputs (cp, out);
335 fprintf (out, "Subject: %s (part %d of %d)\n", subject, partno, nparts);
336 fprintf (out, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
337 fprintf (out, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD, msgid);
338 fprintf (out, "\tnumber=%d; total=%d\n", partno, nparts);
339 fprintf (out, "%s: part %d of %d\n\n", DESCR_FIELD, partno, nparts);
340
341 /*
342 * If this is the first in the collection, output the
343 * header fields we are encapsulating at the beginning
344 * of the body of the first message.
345 */
346 if (partno == 1) {
347 if (dp)
348 fputs (dp, out);
349 fprintf (out, "Message-ID: %s\n", msgid);
350 fprintf (out, "\n");
351 }
352
353 pos = 0;
354 for (;;) {
355 long len;
356
357 if (!fgets (buffer, sizeof buffer, in)) {
358 if (partno == nparts)
359 break;
360 adios (NULL, "premature eof");
361 }
362
363 if ((pos += (len = strlen (buffer))) > CPERMSG) {
364 fseek (in, -len, SEEK_CUR);
365 break;
366 }
367
368 fputs (buffer, out);
369 }
370
371 if (fflush (out))
372 adios (tmpdrf, "error writing to");
373
374 fclose (out);
375
376 if (!pushsw && verbsw) {
377 putchar('\n');
378 fflush (stdout);
379 }
380
381 /* Pause here, if a delay is specified */
382 if (delay > 0 && 1 < partno && partno <= nparts) {
383 if (!pushsw) {
384 printf ("pausing %d seconds before sending part %d...\n",
385 delay, partno);
386 fflush (stdout);
387 }
388 sleep ((unsigned int) delay);
389 }
390
391 snprintf (partnum, sizeof(partnum), "%d", partno);
392 status = sendaux (vec, vecp, program, tmpdrf, st);
393 (void) m_unlink (tmpdrf);
394 if (status != OK)
395 break;
396
397 /*
398 * This is so sendaux will only annotate
399 * the altmsg the first time it is called.
400 */
401 annotext = NULL;
402 }
403
404 free (cp);
405 mh_xfree(dp);
406
407 fclose (in); /* close the draft */
408 return status;
409 }
410
411
412 /*
413 * Annotate original message, and
414 * call `postproc' (which is passed down in "program") to send message.
415 */
416
417 static int
418 sendaux (char **vec, int vecp, char *program, char *drft, struct stat *st)
419 {
420 pid_t child_id;
421 int i, status, fd, fd2;
422 char backup[BUFSIZ], buf[BUFSIZ];
423
424 fd = pushsw ? tmp_fd () : NOTOK;
425 fd2 = NOTOK;
426
427 if (annotext) {
428 if ((fd2 = tmp_fd ()) != NOTOK) {
429 vec[vecp++] = "-idanno";
430 snprintf (buf, sizeof(buf), "%d", fd2);
431 vec[vecp++] = buf;
432 } else {
433 inform("unable to create temporary file in %s for "
434 "annotation list, continuing...", get_temp_dir());
435 }
436 }
437 vec[vecp++] = drft;
438 if (distfile && distout (drft, distfile, backup) == NOTOK)
439 done (1);
440 vec[vecp] = NULL;
441
442 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
443 sleep (5);
444
445 switch (child_id) {
446 case -1:
447 /* oops -- fork error */
448 adios ("fork", "unable to");
449 break; /* NOT REACHED */
450
451 case 0:
452 /*
453 * child process -- send it
454 *
455 * If fd is OK, then we are pushing and fd points to temp
456 * file, so capture anything on stdout and stderr there.
457 */
458 if (fd != NOTOK) {
459 dup2 (fd, fileno (stdout));
460 dup2 (fd, fileno (stderr));
461 close (fd);
462 }
463 execvp (program, vec);
464 fprintf (stderr, "unable to exec ");
465 perror (postproc);
466 _exit (-1);
467
468 default:
469 /*
470 * parent process -- wait for it
471 */
472 if ((status = pidwait(child_id, NOTOK)) == OK) {
473 if (annotext && fd2 != NOTOK)
474 anno (fd2, st);
475 } else {
476 /*
477 * If postproc failed, and we have good fd (which means
478 * we pushed), then mail error message (and possibly the
479 * draft) back to the user.
480 */
481 if (fd != NOTOK) {
482 alert (drft, fd);
483 close (fd);
484 } else {
485 inform("message not delivered to anyone");
486 }
487 if (annotext && fd2 != NOTOK)
488 close (fd2);
489 if (distfile) {
490 (void) m_unlink (drft);
491 if (rename (backup, drft) == NOTOK)
492 advise (drft, "unable to rename %s to", backup);
493 }
494 }
495 break;
496 }
497
498 return status;
499 }
500
501
502 /*
503 * Mail error notification (and possibly a copy of the
504 * message) back to the user, using the mailproc
505 */
506
507 static void
508 alert (char *file, int out)
509 {
510 pid_t child_id;
511 int i, in, argp;
512 char buf[BUFSIZ];
513 char *program;
514 char **arglist;
515
516 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
517 sleep (5);
518
519 switch (child_id) {
520 case NOTOK:
521 /* oops -- fork error */
522 advise ("fork", "unable to");
523 /* FALLTHRU */
524
525 case OK:
526 /* child process -- send it */
527 SIGNAL (SIGHUP, SIG_IGN);
528 SIGNAL (SIGINT, SIG_IGN);
529 SIGNAL (SIGQUIT, SIG_IGN);
530 SIGNAL (SIGTERM, SIG_IGN);
531 if (forwsw) {
532 if ((in = open (file, O_RDONLY)) == NOTOK) {
533 admonish (file, "unable to re-open");
534 } else {
535 lseek (out, (off_t) 0, SEEK_END);
536 strncpy (buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
537 if (write (out, buf, strlen (buf)) < 0) {
538 advise (file, "write");
539 }
540 strncpy (buf, "\n------- Unsent Draft\n\n", sizeof(buf));
541 if (write (out, buf, strlen (buf)) < 0) {
542 advise (file, "write");
543 }
544 cpydgst (in, out, file, "temporary file");
545 close (in);
546 strncpy (buf, "\n------- End of Unsent Draft\n", sizeof(buf));
547 if (write (out, buf, strlen (buf)) < 0) {
548 advise (file, "write");
549 }
550 if (rename (file, strncpy (buf, m_backup (file), sizeof(buf))) == NOTOK)
551 admonish (buf, "unable to rename %s to", file);
552 }
553 }
554 lseek (out, (off_t) 0, SEEK_SET);
555 dup2 (out, fileno (stdin));
556 close (out);
557 /* create subject for error notification */
558 snprintf (buf, sizeof(buf), "send failed on %s",
559 forwsw ? "enclosed draft" : file);
560
561 arglist = argsplit(mailproc, &program, &argp);
562
563 arglist[argp++] = getusername();
564 arglist[argp++] = "-subject";
565 arglist[argp++] = buf;
566 arglist[argp] = NULL;
567
568 execvp (program, arglist);
569 fprintf (stderr, "unable to exec ");
570 perror (mailproc);
571 _exit (-1);
572
573 default: /* no waiting... */
574 break;
575 }
576 }
577
578
579 static int
580 tmp_fd (void)
581 {
582 int fd;
583 char *tfile;
584
585 if ((tfile = m_mktemp2(NULL, invo_name, &fd, NULL)) == NULL) return NOTOK;
586
587 if (debugsw)
588 inform("temporary file %s selected", tfile);
589 else
590 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, (off_t) 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
742 void
743 handle_sendfrom(char **vec, int *vecp, char *draft, const char *auth_svc) {
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, 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 adios(NULL, message);
767 }
768 break;
769 #else
770 NMH_UNUSED(auth_svc);
771 adios(NULL, "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 const char *saslmech = NULL, *user = NULL;
788 mh_oauth_service_info svc;
789 char errbuf[256];
790 int i;
791
792 /* Make sure we have all the information we need. */
793 for (i = 1; i < *vecp; ++i) {
794 /* Don't support abbreviated switches, to avoid collisions in the
795 future if new ones are added. */
796 if (! strcmp(vec[i-1], "-saslmech")) {
797 saslmech = vec[i];
798 } else if (! strcmp(vec[i-1], "-user")) {
799 user = vec[i];
800 } else if (! strcmp(vec[i-1], "-authservice")) {
801 auth_svc = vec[i];
802 }
803 }
804
805 if (auth_svc == NULL) {
806 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
807 *message = "must specify -authservice with -saslmech xoauth2";
808 return NOTOK;
809 }
810 } else {
811 if (user == NULL) {
812 *message = "must specify -user with -saslmech xoauth2";
813 return NOTOK;
814 }
815
816 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
817 if (! mh_oauth_get_service_info(auth_svc, &svc, errbuf,
818 sizeof(errbuf)))
819 adios(NULL, "Unable to retrieve oauth profile entries: %s",
820 errbuf);
821
822 vec[(*vecp)++] = mh_xstrdup("-authservice");
823 vec[(*vecp)++] = mh_xstrdup(auth_svc);
824 vec[(*vecp)++] = mh_xstrdup("-oauthcredfile");
825 vec[(*vecp)++] = mh_xstrdup(mh_oauth_cred_fn(auth_svc));
826 vec[(*vecp)++] = mh_xstrdup("-oauthclientid");
827 vec[(*vecp)++] = getcpy(svc.client_id);
828 vec[(*vecp)++] = mh_xstrdup("-oauthclientsecret");
829 vec[(*vecp)++] = getcpy(svc.client_secret);
830 vec[(*vecp)++] = mh_xstrdup("-oauthauthendpoint");
831 vec[(*vecp)++] = getcpy(svc.auth_endpoint);
832 vec[(*vecp)++] = mh_xstrdup("-oauthredirect");
833 vec[(*vecp)++] = getcpy(svc.redirect_uri);
834 vec[(*vecp)++] = mh_xstrdup("-oauthtokenendpoint");
835 vec[(*vecp)++] = getcpy(svc.token_endpoint);
836 vec[(*vecp)++] = mh_xstrdup("-oauthscope");
837 vec[(*vecp)++] = getcpy(svc.scope);
838 }
839 }
840
841 return 0;
842 }
843 #endif /* OAUTH_SUPPORT */
844
845
846 /*
847 * Extract user and domain from From: header line in draft.
848 */
849 static
850 int
851 get_from_header_info(const char *filename, const char **addr, const char **host, const char **message) {
852 struct stat st;
853 FILE *in;
854
855 if (stat (filename, &st) == NOTOK) {
856 *message = "unable to stat draft file";
857 return NOTOK;
858 }
859
860 if ((in = fopen (filename, "r")) != NULL) {
861 /* There must be a non-blank Envelope-From or {Resent-}Sender or
862 {Resent-}From header. */
863 char *addrformat = "%(addr{Envelope-From})";
864 char *hostformat = "%(host{Envelope-From})";
865
866 if ((*addr = get_message_header_info (in, addrformat)) == NULL ||
867 !**addr) {
868 addrformat = distfile == NULL ? "%(addr{Sender})" : "%(addr{Resent-Sender})";
869 hostformat = distfile == NULL ? "%(host{Sender})" : "%(host{Resent-Sender})";
870
871 if ((*addr = get_message_header_info (in, addrformat)) == NULL) {
872 addrformat = distfile == NULL ? "%(addr{From})" : "%(addr{Resent-From})";
873 hostformat = distfile == NULL ? "%(host{From})" : "%(host{Resent-From})";
874
875 if ((*addr = get_message_header_info (in, addrformat)) == NULL) {
876 *message = "unable to find sender address in";
877 fclose(in);
878 return NOTOK;
879 }
880 }
881 }
882
883 /* Use the hostformat that corresponds to the successful addrformat. */
884 if ((*host = get_message_header_info(in, hostformat)) == NULL) {
885 fclose(in);
886 *message = "unable to find sender host in";
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
905 const char *
906 get_message_header_info(FILE *in, char *format) {
907 int dat[5];
908 struct format *fmt;
909 struct stat st;
910 int parsing_header;
911 m_getfld_state_t gstate = 0;
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 = 1;
927 do {
928 char name[NAMESZ], rbuf[NMH_BUFSIZ];
929 int bufsz = sizeof rbuf;
930 int state = m_getfld(&gstate, name, rbuf, &bufsz, in);
931
932 switch (state) {
933 case FLD:
934 case FLDPLUS: {
935 int bucket = fmt_addcomptext(name, rbuf);
936
937 if (bucket != -1) {
938 while (state == FLDPLUS) {
939 bufsz = sizeof rbuf;
940 state = m_getfld(&gstate, name, rbuf, &bufsz, in);
941 fmt_appendcomp(bucket, name, rbuf);
942 }
943 }
944
945 while (state == FLDPLUS) {
946 bufsz = sizeof rbuf;
947 state = m_getfld(&gstate, name, rbuf, &bufsz, in);
948 }
949 break;
950 }
951 default:
952 parsing_header = 0;
953 }
954 } while (parsing_header);
955 m_getfld_state_destroy(&gstate);
956
957 fmt_scan(fmt, buffer, INT_MAX, dat, NULL);
958 fmt_free(fmt, 1);
959
960 /* Trim trailing newline, if any. */
961 retval = rtrim(charstring_buffer_copy((buffer)));
962 charstring_free(buffer);
963 if (*retval)
964 return retval;
965
966 free(retval);
967 return NULL;
968 }
969
970
971 /*
972 * Look in profile for entry corresponding to addr or host, and add its contents to vec.
973 *
974 * Could do some of this automatically, by looking for:
975 * 1) access-$(mbox{from}) in oauth-svc file using mh_oauth_cred_load(), which isn't
976 * static and doesn't have side effects; free the result with mh_oauth_cred_free())
977 * 2) machine $(mbox{from}) in creds
978 * If no -server passed in from profile or commandline, could use smtp.<svc>.com for gmail,
979 * but that might not generalize for other svcs.
980 */
981 static
982 void
983 merge_profile_entry(const char *addr, const char *host, char *vec[], int *vecp) {
984 char *addr_entry = concat("sendfrom-", addr, NULL);
985 char *profile_entry = context_find(addr_entry);
986
987 free(addr_entry);
988 if (profile_entry == NULL) {
989 /* No entry for the user. Look for one for the host. */
990 char *host_entry = concat("sendfrom-", host, NULL);
991
992 profile_entry = context_find(host_entry);
993 free(host_entry);
994 }
995
996 /* Use argsplit() to do the real work of splitting the args in the profile entry. */
997 if (profile_entry && *profile_entry) {
998 int profile_vecp;
999 char *file;
1000 char **profile_vec = argsplit(profile_entry, &file, &profile_vecp);
1001 int i;
1002
1003 for (i = 0; i < profile_vecp; ++i) {
1004 vec[(*vecp)++] = getcpy(profile_vec[i]);
1005 }
1006
1007 arglist_free(file, profile_vec);
1008 }
1009 }
1010
1011
1012 static void
1013 armed_done (int status)
1014 {
1015 longjmp (env, status ? status : NOTOK);
1016 }