1 /* sendsbr.c -- routines to help WhatNow/Send along
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.
9 #include "sbr/fmt_new.h"
13 #include "sbr/m_name.h"
14 #include "sbr/m_getfld.h"
15 #include "sbr/concat.h"
16 #include "sbr/cpydgst.h"
17 #include "sbr/trimcpy.h"
19 #include "sbr/getcpy.h"
20 #include "sbr/m_convert.h"
21 #include "sbr/m_backup.h"
22 #include "sbr/folder_read.h"
23 #include "sbr/folder_free.h"
24 #include "sbr/context_find.h"
25 #include "sbr/brkstring.h"
26 #include "sbr/pidstatus.h"
27 #include "sbr/arglist.h"
28 #include "sbr/error.h"
29 #include "h/fmt_scan.h"
30 #include "h/fmt_compile.h"
31 #include "h/signals.h"
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
48 #include "sbr/m_maildir.h"
49 #include "sbr/m_mktemp.h"
50 #include "sbr/message_id.h"
53 static int setup_oauth_params(char *[], int *, const char *, const char **);
54 #endif /* OAUTH_SUPPORT */
56 int debugsw
= 0; /* global */
63 char *altmsg
= NULL
; /* .. */
64 char *annotext
= NULL
;
65 char *distfile
= NULL
;
72 static void alert (char *, int);
73 static int tmp_fd (void);
74 static void anno (int, struct stat
*);
75 static void annoaux (int);
76 static int sendaux (char **, int, char *, char *, struct stat
*);
77 static void handle_sendfrom(char **, int *, char *, const char *);
78 static int get_from_header_info(const char *, const char **, const char **, const char **);
79 static const char *get_message_header_info(FILE *, char *);
80 static void merge_profile_entry(const char *, const char *, char *[], int *);
81 static void armed_done (int) NORETURN
;
84 * Entry point into (back-end) routines to send message.
88 sendsbr (char **vec
, int vecp
, char *program
, char *draft
, struct stat
*st
,
89 int rename_drft
, const char *auth_svc
)
93 char buffer
[BUFSIZ
], file
[BUFSIZ
];
94 char **buildvec
, *buildprogram
;
95 char *volatile drft
= draft
;
96 /* nvecs is volatile to prevent warning from gcc about possible clobbering
98 volatile int nvecs
= vecp
;
99 int *nvecsp
= (int *) &nvecs
;
102 * Run the mimebuildproc (which is by default mhbuild) on the message
103 * with the addition of the "-auto" flag
106 switch (child
= fork()) {
108 adios("fork", "unable to");
112 buildvec
= argsplit(buildmimeproc
, &buildprogram
, &i
);
113 buildvec
[i
++] = "-auto";
115 buildvec
[i
++] = "-dist";
116 buildvec
[i
++] = (char *) drft
;
118 execvp(buildprogram
, buildvec
);
119 fprintf(stderr
, "unable to exec ");
120 perror(buildmimeproc
);
125 if (pidXwait(child
, buildmimeproc
))
130 set_done(armed_done
);
131 switch (setjmp (env
)) {
134 * If given -push and -unique (which is undocumented), then
135 * rename the draft file. I'm not quite sure why.
137 if (pushsw
&& unique
) {
138 char *cp
= m_mktemp2(drft
, invo_name
, NULL
, NULL
);
140 die("unable to create temporary file");
142 if (rename (drft
, strncpy(file
, cp
, sizeof(file
))) == NOTOK
)
143 adios (file
, "unable to rename %s to", drft
);
148 * Add in any necessary profile entries for xoauth
154 if (setup_oauth_params(vec
, nvecsp
, auth_svc
, &errmsg
) != OK
) {
158 die("send built without OAUTH_SUPPORT, "
159 "so auth_svc %s is not supported", auth_svc
);
160 #endif /* OAUTH_SUPPORT */
164 * Rework the vec based on From: header in draft, as specified
165 * by sendfrom-address entries in profile.
167 if (context_find_prefix("sendfrom-")) {
168 handle_sendfrom(vec
, nvecsp
, draft
, auth_svc
);
171 status
= sendaux (vec
, nvecs
, program
, drft
, st
) ? NOTOK
: OK
;
173 /* rename the original draft */
174 if (rename_drft
&& status
== OK
&&
175 rename (drft
, strncpy (buffer
, m_backup (drft
),
176 sizeof(buffer
))) == NOTOK
)
177 advise (buffer
, "unable to rename %s to", drft
);
187 (void) m_unlink (distfile
);
194 * Annotate original message, and
195 * call `postproc' (which is passed down in "program") to send message.
199 sendaux (char **vec
, int vecp
, char *program
, char *drft
, struct stat
*st
)
203 char backup
[BUFSIZ
], buf
[BUFSIZ
];
205 fd
= pushsw
? tmp_fd () : NOTOK
;
209 if ((fd2
= tmp_fd ()) != NOTOK
) {
210 vec
[vecp
++] = "-idanno";
211 snprintf (buf
, sizeof(buf
), "%d", fd2
);
214 inform("unable to create temporary file in %s for "
215 "annotation list, continuing...", get_temp_dir());
219 if (distfile
&& distout (drft
, distfile
, backup
) == NOTOK
)
226 /* oops -- fork error */
227 adios ("fork", "unable to");
228 break; /* NOT REACHED */
232 * child process -- send it
234 * If fd is OK, then we are pushing and fd points to temp
235 * file, so capture anything on stdout and stderr there.
238 dup2 (fd
, fileno (stdout
));
239 dup2 (fd
, fileno (stderr
));
242 execvp (program
, vec
);
243 fprintf (stderr
, "unable to exec ");
249 * parent process -- wait for it
251 if ((status
= pidwait(child_id
, NOTOK
)) == OK
) {
252 if (annotext
&& fd2
!= NOTOK
)
256 * If postproc failed, and we have good fd (which means
257 * we pushed), then mail error message (and possibly the
258 * draft) back to the user.
264 inform("message not delivered to anyone");
266 if (annotext
&& fd2
!= NOTOK
)
269 (void) m_unlink (drft
);
270 if (rename (backup
, drft
) == NOTOK
)
271 advise (drft
, "unable to rename %s to", backup
);
282 * Mail error notification (and possibly a copy of the
283 * message) back to the user, using the mailproc
287 alert (char *file
, int out
)
298 /* oops -- fork error */
299 advise ("fork", "unable to");
303 /* child process -- send it */
304 SIGNAL (SIGHUP
, SIG_IGN
);
305 SIGNAL (SIGINT
, SIG_IGN
);
306 SIGNAL (SIGQUIT
, SIG_IGN
);
307 SIGNAL (SIGTERM
, SIG_IGN
);
309 if ((in
= open (file
, O_RDONLY
)) == NOTOK
) {
310 admonish (file
, "unable to re-open");
312 lseek(out
, 0, SEEK_END
);
313 strncpy (buf
, "\nMessage not delivered to anyone.\n", sizeof(buf
));
314 if (write (out
, buf
, strlen (buf
)) < 0) {
315 advise (file
, "write");
317 strncpy (buf
, "\n------- Unsent Draft\n\n", sizeof(buf
));
318 if (write (out
, buf
, strlen (buf
)) < 0) {
319 advise (file
, "write");
321 cpydgst (in
, out
, file
, "temporary file");
323 strncpy (buf
, "\n------- End of Unsent Draft\n", sizeof(buf
));
324 if (write (out
, buf
, strlen (buf
)) < 0) {
325 advise (file
, "write");
327 if (rename (file
, strncpy (buf
, m_backup (file
), sizeof(buf
))) == NOTOK
)
328 admonish (buf
, "unable to rename %s to", file
);
331 lseek(out
, 0, SEEK_SET
);
332 dup2 (out
, fileno (stdin
));
334 /* create subject for error notification */
335 snprintf (buf
, sizeof(buf
), "send failed on %s",
336 forwsw
? "enclosed draft" : file
);
338 arglist
= argsplit(mailproc
, &program
, &argp
);
340 arglist
[argp
++] = getusername();
341 arglist
[argp
++] = "-subject";
342 arglist
[argp
++] = buf
;
343 arglist
[argp
] = NULL
;
345 execvp (program
, arglist
);
346 fprintf (stderr
, "unable to exec ");
350 default: /* no waiting... */
362 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) return NOTOK
;
365 inform("temporary file %s selected", tfile
);
366 else if (m_unlink (tfile
) == NOTOK
)
367 advise (tfile
, "unable to remove");
374 anno (int fd
, struct stat
*st
)
378 static char *cwd
= NULL
;
382 (stat (altmsg
, &st2
) == NOTOK
383 || st
->st_mtime
!= st2
.st_mtime
384 || st
->st_dev
!= st2
.st_dev
385 || st
->st_ino
!= st2
.st_ino
)) {
387 inform("$mhaltmsg mismatch, continuing...");
391 child_id
= debugsw
? NOTOK
: fork ();
393 case NOTOK
: /* oops */
395 inform("unable to fork, so doing annotations by hand...");
397 cwd
= mh_xstrdup(pwd ());
401 /* block a few signals */
403 sigaddset (&set
, SIGHUP
);
404 sigaddset (&set
, SIGINT
);
405 sigaddset (&set
, SIGQUIT
);
406 sigaddset (&set
, SIGTERM
);
407 sigprocmask (SIG_BLOCK
, &set
, &oset
);
409 unregister_for_removal(0);
415 /* reset the signal mask */
416 sigprocmask (SIG_SETMASK
, &oset
, &set
);
418 if (chdir (cwd
) < 0) {
419 advise (cwd
, "chdir");
423 default: /* no waiting... */
433 int fd2
, fd3
, msgnum
;
434 char *cp
, *folder
, *maildir
;
435 char buffer
[BUFSIZ
], **ap
;
439 if ((folder
= getenv ("mhfolder")) == NULL
|| *folder
== 0) {
441 inform("$mhfolder not set, continuing...");
444 maildir
= m_maildir (folder
);
445 if (chdir (maildir
) == NOTOK
) {
447 admonish (maildir
, "unable to change directory to");
450 if (!(mp
= folder_read (folder
, 0))) {
452 inform("unable to read folder %s, continuing...", folder
);
456 /* check for empty folder */
457 if (mp
->nummsg
== 0) {
459 inform("no messages in %s, continuing...", folder
);
463 if ((cp
= getenv ("mhmessages")) == NULL
|| *cp
== 0) {
465 inform("$mhmessages not set, continuing...");
468 if (!debugsw
/* MOBY HACK... */
470 && (fd3
= open ("/dev/null", O_RDWR
)) != NOTOK
471 && (fd2
= dup (fileno (stderr
))) != NOTOK
) {
472 dup2 (fd3
, fileno (stderr
));
477 for (ap
= brkstring (cp
= mh_xstrdup(cp
), " ", NULL
); *ap
; ap
++)
481 dup2 (fd2
, fileno (stderr
));
482 if (mp
->numsel
== 0) {
484 inform("no messages to annotate, continuing...");
488 lseek(fd
, 0, SEEK_SET
);
489 if ((fp
= fdopen (fd
, "r")) == NULL
) {
491 inform("unable to fdopen annotation list, continuing...");
495 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
496 cp
= add (buffer
, cp
);
500 inform("annotate%s with %s: \"%s\"",
501 inplace
? " inplace" : "", annotext
, cp
);
502 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
503 if (is_selected(mp
, msgnum
)) {
505 inform("annotate message %d", msgnum
);
506 annotate (m_name (msgnum
), annotext
, cp
, inplace
, 1, -2, 0);
513 folder_free (mp
); /* free folder/message structure */
518 handle_sendfrom(char **vec
, int *vecp
, char *draft
, const char *auth_svc
)
520 const char *addr
, *host
;
523 /* Extract address and host from From: header line in draft. */
524 if (get_from_header_info(draft
, &addr
, &host
, &message
) != OK
) {
525 adios(draft
, "%s", message
);
528 /* Merge in any address or host specific switches to post(1) from profile. */
529 merge_profile_entry(addr
, host
, vec
, vecp
);
538 for (vp
= vec
; *vp
; ++vp
) {
539 if (strcmp(*vp
, "xoauth2") == 0) {
541 if (setup_oauth_params(vec
, vecp
, auth_svc
, &message
) != OK
) {
546 NMH_UNUSED(auth_svc
);
547 die("send built without OAUTH_SUPPORT, "
548 "so -saslmech xoauth2 is not supported");
549 #endif /* OAUTH_SUPPORT */
558 * For XOAUTH2, append profile entries so post can do the heavy lifting
561 setup_oauth_params(char *vec
[], int *vecp
, const char *auth_svc
,
562 const char **message
)
564 const char *saslmech
= NULL
, *user
= NULL
;
565 mh_oauth_service_info svc
;
569 /* Make sure we have all the information we need. */
570 for (i
= 1; i
< *vecp
; ++i
) {
571 /* Don't support abbreviated switches, to avoid collisions in the
572 future if new ones are added. */
573 if (! strcmp(vec
[i
-1], "-saslmech")) {
575 } else if (! strcmp(vec
[i
-1], "-user")) {
577 } else if (! strcmp(vec
[i
-1], "-authservice")) {
582 if (auth_svc
== NULL
) {
583 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
584 *message
= "must specify -authservice with -saslmech xoauth2";
589 *message
= "must specify -user with -saslmech xoauth2";
593 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
594 if (! mh_oauth_get_service_info(auth_svc
, &svc
, errbuf
,
596 die("Unable to retrieve oauth profile entries: %s",
599 vec
[(*vecp
)++] = mh_xstrdup("-authservice");
600 vec
[(*vecp
)++] = mh_xstrdup(auth_svc
);
601 vec
[(*vecp
)++] = mh_xstrdup("-oauthcredfile");
602 vec
[(*vecp
)++] = mh_oauth_cred_fn(auth_svc
);
603 vec
[(*vecp
)++] = mh_xstrdup("-oauthclientid");
604 vec
[(*vecp
)++] = getcpy(svc
.client_id
);
605 vec
[(*vecp
)++] = mh_xstrdup("-oauthclientsecret");
606 vec
[(*vecp
)++] = getcpy(svc
.client_secret
);
607 vec
[(*vecp
)++] = mh_xstrdup("-oauthauthendpoint");
608 vec
[(*vecp
)++] = getcpy(svc
.auth_endpoint
);
609 vec
[(*vecp
)++] = mh_xstrdup("-oauthredirect");
610 vec
[(*vecp
)++] = getcpy(svc
.redirect_uri
);
611 vec
[(*vecp
)++] = mh_xstrdup("-oauthtokenendpoint");
612 vec
[(*vecp
)++] = getcpy(svc
.token_endpoint
);
613 vec
[(*vecp
)++] = mh_xstrdup("-oauthscope");
614 vec
[(*vecp
)++] = getcpy(svc
.scope
);
620 #endif /* OAUTH_SUPPORT */
624 * Extract user and domain from From: header line in draft.
627 get_from_header_info(const char *filename
, const char **addr
, const char **host
, const char **message
)
632 if (stat (filename
, &st
) == NOTOK
) {
633 *message
= "unable to stat draft file";
637 if ((in
= fopen (filename
, "r")) != NULL
) {
638 /* There must be a non-blank Envelope-From or {Resent-}Sender or
639 {Resent-}From header. */
640 char *addrformat
= "%(addr{Envelope-From})";
641 char *hostformat
= "%(host{Envelope-From})";
643 if ((*addr
= get_message_header_info (in
, addrformat
)) == NULL
||
645 addrformat
= distfile
== NULL
? "%(addr{Sender})" : "%(addr{Resent-Sender})";
646 hostformat
= distfile
== NULL
? "%(host{Sender})" : "%(host{Resent-Sender})";
648 if ((*addr
= get_message_header_info (in
, addrformat
)) == NULL
) {
649 addrformat
= distfile
== NULL
? "%(addr{From})" : "%(addr{Resent-From})";
650 hostformat
= distfile
== NULL
? "%(host{From})" : "%(host{Resent-From})";
652 if ((*addr
= get_message_header_info (in
, addrformat
)) == NULL
) {
653 *message
= "unable to find sender address in";
660 /* Use the hostformat that corresponds to the successful addrformat. */
661 if ((*host
= get_message_header_info(in
, hostformat
)) == NULL
) {
662 *message
= "unable to find sender host";
671 *message
= "unable to open";
677 * Get formatted information from header of a message.
678 * Adapted from process_single_file() in uip/fmttest.c.
681 get_message_header_info(FILE *in
, char *format
)
687 m_getfld_state_t gstate
;
688 charstring_t buffer
= charstring_create(0);
691 dat
[0] = dat
[1] = dat
[4] = 0;
692 dat
[2] = fstat(fileno(in
), &st
) == 0 ? st
.st_size
: 0;
695 (void) fmt_compile(new_fs(NULL
, NULL
, format
), &fmt
, 1);
699 * Read in the message and process the header.
702 parsing_header
= true;
703 gstate
= m_getfld_state_init(in
);
705 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
706 int bufsz
= sizeof rbuf
;
707 int state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
712 int bucket
= fmt_addcomptext(name
, rbuf
);
715 while (state
== FLDPLUS
) {
717 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
718 fmt_appendcomp(bucket
, name
, rbuf
);
722 while (state
== FLDPLUS
) {
724 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
729 parsing_header
= false;
731 } while (parsing_header
);
732 m_getfld_state_destroy(&gstate
);
734 fmt_scan(fmt
, buffer
, INT_MAX
, dat
, NULL
);
737 /* Trim trailing newline, if any. */
738 retval
= rtrim(charstring_buffer_copy((buffer
)));
739 charstring_free(buffer
);
749 * Look in profile for entry corresponding to addr or host, and add its contents to vec.
751 * Could do some of this automatically, by looking for:
752 * 1) access-$(mbox{from}) in oauth-svc file using mh_oauth_cred_load(), which isn't
753 * static and doesn't have side effects; free the result with mh_oauth_cred_free())
754 * 2) machine $(mbox{from}) in creds
755 * If no -server passed in from profile or commandline, could use smtp.<svc>.com for gmail,
756 * but that might not generalize for other svcs.
759 merge_profile_entry(const char *addr
, const char *host
, char *vec
[], int *vecp
)
761 char *addr_entry
= concat("sendfrom-", addr
, NULL
);
762 char *profile_entry
= context_find(addr_entry
);
765 if (profile_entry
== NULL
) {
766 /* No entry for the user. Look for one for the host. */
767 char *host_entry
= concat("sendfrom-", host
, NULL
);
769 profile_entry
= context_find(host_entry
);
773 /* Use argsplit() to do the real work of splitting the args in the profile entry. */
774 if (profile_entry
&& *profile_entry
) {
777 char **profile_vec
= argsplit(profile_entry
, &file
, &profile_vecp
);
780 for (i
= 0; i
< profile_vecp
; ++i
) {
781 vec
[(*vecp
)++] = getcpy(profile_vec
[i
]);
784 arglist_free(file
, profile_vec
);
790 armed_done (int status
)
792 longjmp (env
, status
? status
: NOTOK
);