NeoMutt  2022-04-29-249-gaae397
Teaching an old dog new tricks
DOXYGEN
sendmail.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <errno.h>
31#include <fcntl.h>
32#include <limits.h>
33#include <signal.h>
34#include <stdbool.h>
35#include <string.h>
36#include <sys/stat.h>
37#include <sys/wait.h>
38#include <unistd.h>
39#include "mutt/lib.h"
40#include "address/lib.h"
41#include "config/lib.h"
42#include "gui/lib.h"
43#include "lib.h"
44#include "pager/lib.h"
45#include "format_flags.h"
46#include "muttlib.h"
47#include "options.h"
48#ifdef USE_NNTP
49#include "nntp/lib.h"
50#endif
51#ifdef HAVE_SYSEXITS_H
52#include <sysexits.h>
53#else
54#define EX_OK 0
55#endif
56
57struct Mailbox;
58
59/* For execvp environment setting in send_msg() */
60extern char **environ;
61
62SIG_ATOMIC_VOLATILE_T SigAlrm;
63
64ARRAY_HEAD(SendmailArgs, const char *);
65
70static void alarm_handler(int sig)
71{
72 SigAlrm = 1;
73}
74
88static int send_msg(const char *path, struct SendmailArgs *args,
89 const char *msg, char **tempfile, int wait_time)
90{
91 sigset_t set;
92 int st;
93
95
96 sigemptyset(&set);
97 /* we also don't want to be stopped right now */
98 sigaddset(&set, SIGTSTP);
99 sigprocmask(SIG_BLOCK, &set, NULL);
100
101 if ((wait_time >= 0) && tempfile)
102 {
103 struct Buffer *tmp = mutt_buffer_pool_get();
105 *tempfile = mutt_buffer_strdup(tmp);
107 }
108
109 pid_t pid = fork();
110 if (pid == 0)
111 {
112 struct sigaction act, oldalrm;
113
114 /* save parent's ID before setsid() */
115 pid_t ppid = getppid();
116
117 /* we want the delivery to continue even after the main process dies,
118 * so we put ourselves into another session right away */
119 setsid();
120
121 /* next we close all open files */
122 close(0);
123#ifdef OPEN_MAX
124 for (int fd = tempfile ? 1 : 3; fd < OPEN_MAX; fd++)
125 close(fd);
126#elif defined(_POSIX_OPEN_MAX)
127 for (int fd = tempfile ? 1 : 3; fd < _POSIX_OPEN_MAX; fd++)
128 close(fd);
129#else
130 if (tempfile)
131 {
132 close(1);
133 close(2);
134 }
135#endif
136
137 /* now the second fork() */
138 pid = fork();
139 if (pid == 0)
140 {
141 /* "msg" will be opened as stdin */
142 if (open(msg, O_RDONLY, 0) < 0)
143 {
144 unlink(msg);
145 _exit(S_ERR);
146 }
147 unlink(msg);
148
149 if ((wait_time >= 0) && tempfile && *tempfile)
150 {
151 /* *tempfile will be opened as stdout */
152 if (open(*tempfile, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0600) < 0)
153 _exit(S_ERR);
154 /* redirect stderr to *tempfile too */
155 if (dup(1) < 0)
156 _exit(S_ERR);
157 }
158 else if (tempfile)
159 {
160 if (open("/dev/null", O_WRONLY | O_APPEND) < 0) /* stdout */
161 _exit(S_ERR);
162 if (open("/dev/null", O_RDWR | O_APPEND) < 0) /* stderr */
163 _exit(S_ERR);
164 }
165
166 /* execvpe is a glibc extension, so just manually set environ */
168 execvp(path, (char **) args->entries);
169 _exit(S_ERR);
170 }
171 else if (pid == -1)
172 {
173 unlink(msg);
174 FREE(tempfile);
175 _exit(S_ERR);
176 }
177
178 /* wait_time > 0: interrupt waitpid() after wait_time seconds
179 * wait_time = 0: wait forever
180 * wait_time < 0: don't wait */
181 if (wait_time > 0)
182 {
183 SigAlrm = 0;
184 act.sa_handler = alarm_handler;
185#ifdef SA_INTERRUPT
186 /* need to make sure waitpid() is interrupted on SIGALRM */
187 act.sa_flags = SA_INTERRUPT;
188#else
189 act.sa_flags = 0;
190#endif
191 sigemptyset(&act.sa_mask);
192 sigaction(SIGALRM, &act, &oldalrm);
193 alarm(wait_time);
194 }
195 else if (wait_time < 0)
196 _exit(0xff & EX_OK);
197
198 if (waitpid(pid, &st, 0) > 0)
199 {
200 st = WIFEXITED(st) ? WEXITSTATUS(st) : S_ERR;
201 if (wait_time && (st == (0xff & EX_OK)) && tempfile && *tempfile)
202 {
203 unlink(*tempfile); /* no longer needed */
204 FREE(tempfile);
205 }
206 }
207 else
208 {
209 st = ((wait_time > 0) && (errno == EINTR) && SigAlrm) ? S_BKG : S_ERR;
210 if ((wait_time > 0) && tempfile && *tempfile)
211 {
212 unlink(*tempfile);
213 FREE(tempfile);
214 }
215 }
216
217 if (wait_time > 0)
218 {
219 /* reset alarm; not really needed, but... */
220 alarm(0);
221 sigaction(SIGALRM, &oldalrm, NULL);
222 }
223
224 if ((kill(ppid, 0) == -1) && (errno == ESRCH) && tempfile && *tempfile)
225 {
226 /* the parent is already dead */
227 unlink(*tempfile);
228 FREE(tempfile);
229 }
230
231 _exit(st);
232 }
233
234 sigprocmask(SIG_UNBLOCK, &set, NULL);
235
236 if ((pid != -1) && (waitpid(pid, &st, 0) > 0))
237 st = WIFEXITED(st) ? WEXITSTATUS(st) : S_ERR; /* return child status */
238 else
239 st = S_ERR; /* error */
240
242
243 return st;
244}
245
251static void add_args_one(struct SendmailArgs *args, const struct Address *addr)
252{
253 /* weed out group mailboxes, since those are for display only */
254 if (addr->mailbox && !addr->group)
255 {
256 ARRAY_ADD(args, addr->mailbox);
257 }
258}
259
265static void add_args(struct SendmailArgs *args, struct AddressList *al)
266{
267 if (!al)
268 return;
269
270 struct Address *a = NULL;
271 TAILQ_FOREACH(a, al, entries)
272 {
273 add_args_one(args, a);
274 }
275}
276
292int mutt_invoke_sendmail(struct Mailbox *m, struct AddressList *from,
293 struct AddressList *to, struct AddressList *cc,
294 struct AddressList *bcc, const char *msg,
295 bool eightbit, struct ConfigSubset *sub)
296{
297 char *ps = NULL, *path = NULL, *s = NULL, *childout = NULL;
298 struct SendmailArgs args = ARRAY_HEAD_INITIALIZER;
299 struct SendmailArgs extra_args = ARRAY_HEAD_INITIALIZER;
300 int i;
301
302#ifdef USE_NNTP
303 if (OptNewsSend)
304 {
305 char cmd[1024] = { 0 };
306
307 const char *const c_inews = cs_subset_string(sub, "inews");
308 mutt_expando_format(cmd, sizeof(cmd), 0, sizeof(cmd), NONULL(c_inews),
310 if (*cmd == '\0')
311 {
312 i = nntp_post(m, msg);
313 unlink(msg);
314 return i;
315 }
316
317 s = mutt_str_dup(cmd);
318 }
319 else
320#endif
321 {
322 const char *const c_sendmail = cs_subset_string(sub, "sendmail");
323 s = mutt_str_dup(c_sendmail);
324 }
325
326 /* ensure that $sendmail is set to avoid a crash. http://dev.mutt.org/trac/ticket/3548 */
327 if (!s)
328 {
329 mutt_error(_("$sendmail must be set in order to send mail"));
330 return -1;
331 }
332
333 ps = s;
334 i = 0;
335 while ((ps = strtok(ps, " ")))
336 {
337 if (i)
338 {
339 if (mutt_str_equal(ps, "--"))
340 break;
341 ARRAY_ADD(&args, ps);
342 }
343 else
344 {
345 path = mutt_str_dup(ps);
346 ps = strrchr(ps, '/');
347 if (ps)
348 ps++;
349 else
350 ps = path;
351 ARRAY_ADD(&args, ps);
352 }
353 ps = NULL;
354 i++;
355 }
356
357#ifdef USE_NNTP
358 if (!OptNewsSend)
359 {
360#endif
361 /* If $sendmail contained a "--", we save the recipients to append to
362 * args after other possible options added below. */
363 if (ps)
364 {
365 ps = NULL;
366 while ((ps = strtok(ps, " ")))
367 {
368 ARRAY_ADD(&extra_args, ps);
369 ps = NULL;
370 }
371 }
372
373 const bool c_use_8bit_mime = cs_subset_bool(sub, "use_8bit_mime");
374 if (eightbit && c_use_8bit_mime)
375 ARRAY_ADD(&args, "-B8BITMIME");
376
377 const bool c_use_envelope_from = cs_subset_bool(sub, "use_envelope_from");
378 if (c_use_envelope_from)
379 {
380 const struct Address *c_envelope_from_address = cs_subset_address(sub, "envelope_from_address");
381 if (c_envelope_from_address)
382 {
383 ARRAY_ADD(&args, "-f");
384 add_args_one(&args, c_envelope_from_address);
385 }
386 else if (!TAILQ_EMPTY(from) && !TAILQ_NEXT(TAILQ_FIRST(from), entries))
387 {
388 ARRAY_ADD(&args, "-f");
389 add_args(&args, from);
390 }
391 }
392
393 const char *const c_dsn_notify = cs_subset_string(sub, "dsn_notify");
394 if (c_dsn_notify)
395 {
396 ARRAY_ADD(&args, "-N");
397 ARRAY_ADD(&args, c_dsn_notify);
398 }
399
400 const char *const c_dsn_return = cs_subset_string(sub, "dsn_return");
401 if (c_dsn_return)
402 {
403 ARRAY_ADD(&args, "-R");
404 ARRAY_ADD(&args, c_dsn_return);
405 }
406 ARRAY_ADD(&args, "--");
407 const char **e = NULL;
408 ARRAY_FOREACH(e, &extra_args)
409 {
410 ARRAY_ADD(&args, *e);
411 }
412 add_args(&args, to);
413 add_args(&args, cc);
414 add_args(&args, bcc);
415#ifdef USE_NNTP
416 }
417#endif
418
419 ARRAY_ADD(&args, NULL);
420
421 const short c_sendmail_wait = cs_subset_number(sub, "sendmail_wait");
422 i = send_msg(path, &args, msg, OptNoCurses ? NULL : &childout, c_sendmail_wait);
423
424 /* Some user's $sendmail command uses gpg for password decryption,
425 * and is set up to prompt using ncurses pinentry. If we
426 * mutt_endwin() it leaves other users staring at a blank screen.
427 * So instead, just force a hard redraw on the next refresh. */
428 if (!OptNoCurses)
429 {
431 }
432
433 if (i != (EX_OK & 0xff))
434 {
435 if (i != S_BKG)
436 {
437 const char *e = mutt_str_sysexit(i);
438 mutt_error(_("Error sending message, child exited %d (%s)"), i, NONULL(e));
439 if (childout)
440 {
441 struct stat st = { 0 };
442
443 if ((stat(childout, &st) == 0) && (st.st_size > 0))
444 {
445 struct PagerData pdata = { 0 };
446 struct PagerView pview = { &pdata };
447
448 pdata.fname = childout;
449
450 pview.banner = _("Output of the delivery process");
452 pview.mode = PAGER_MODE_OTHER;
453
454 mutt_do_pager(&pview, NULL);
455 }
456 }
457 }
458 }
459 else if (childout)
460 unlink(childout);
461
462 FREE(&childout);
463 FREE(&path);
464 FREE(&s);
465 ARRAY_FREE(&args);
466 ARRAY_FREE(&extra_args);
467
468 if (i == (EX_OK & 0xff))
469 i = 0;
470 else if (i == S_BKG)
471 i = 1;
472 else
473 i = -1;
474 return i;
475}
Email Address Handling.
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:155
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:211
#define ARRAY_FREE(head)
Release all memory.
Definition: array.h:203
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition: array.h:57
char * mutt_buffer_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:447
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:169
const struct Address * cs_subset_address(const struct ConfigSubset *sub, const char *name)
Get an Address config item by name.
Definition: helpers.c:49
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
Convenience wrapper for the config headers.
void mutt_need_hard_redraw(void)
Force a hard refresh.
Definition: curs_lib.c:162
int mutt_do_pager(struct PagerView *pview, struct Email *e)
Display some page-able text to the user (help or attachment)
Definition: do_pager.c:123
char ** mutt_envlist_getlist(void)
Get the private environment.
Definition: envlist.c:169
Flags to control mutt_expando_format()
#define MUTT_FORMAT_NO_FLAGS
No flags are set.
Definition: format_flags.h:30
const char * nntp_format_str(char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
Expand the newsrc filename - Implements format_t -.
Definition: newsrc.c:925
void mutt_expando_format(char *buf, size_t buflen, size_t col, int cols, const char *src, format_t callback, intptr_t data, MuttFormatFlags flags)
Expand expandos (x) in a string -.
Definition: muttlib.c:777
#define mutt_error(...)
Definition: logging.h:87
Convenience wrapper for the gui headers.
#define FREE(x)
Definition: memory.h:43
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:807
const char * mutt_str_sysexit(int err_num)
Return a string matching an error code.
Definition: string.c:166
Some miscellaneous functions.
#define mutt_buffer_mktemp(buf)
Definition: muttlib.h:74
Usenet network mailbox type; talk to an NNTP server.
int nntp_post(struct Mailbox *m, const char *msg)
Post article.
Definition: nntp.c:1874
Handling of global boolean variables.
bool OptNoCurses
(pseudo) when sending in batch mode
Definition: options.h:53
bool OptNewsSend
(pseudo) used to change behavior when posting
Definition: options.h:51
GUI display a file/email/help in a viewport with paging.
#define MUTT_PAGER_NO_FLAGS
No flags are set.
Definition: lib.h:58
@ PAGER_MODE_OTHER
Pager is invoked via 3rd path. Non-email content is likely to be shown.
Definition: lib.h:139
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
#define TAILQ_FIRST(head)
Definition: queue.h:723
#define TAILQ_NEXT(elm, field)
Definition: queue.h:832
#define TAILQ_EMPTY(head)
Definition: queue.h:721
static void alarm_handler(int sig)
Async notification of an alarm signal.
Definition: sendmail.c:70
static void add_args_one(struct SendmailArgs *args, const struct Address *addr)
Add an Address to a dynamic array.
Definition: sendmail.c:251
int mutt_invoke_sendmail(struct Mailbox *m, struct AddressList *from, struct AddressList *to, struct AddressList *cc, struct AddressList *bcc, const char *msg, bool eightbit, struct ConfigSubset *sub)
Run sendmail.
Definition: sendmail.c:292
ARRAY_HEAD(SendmailArgs, const char *)
char ** environ
static int send_msg(const char *path, struct SendmailArgs *args, const char *msg, char **tempfile, int wait_time)
Invoke sendmail in a subshell.
Definition: sendmail.c:88
static void add_args(struct SendmailArgs *args, struct AddressList *al)
Add a list of Addresses to a dynamic array.
Definition: sendmail.c:265
SIG_ATOMIC_VOLATILE_T SigAlrm
true after SIGALRM is received
Definition: sendmail.c:62
#define EX_OK
Definition: sendmail.c:54
void mutt_sig_block_system(void)
Block signals before calling exec()
Definition: signal.c:183
void mutt_sig_unblock_system(bool restore)
Restore previously blocked signals.
Definition: signal.c:207
Key value store.
#define S_ERR
Definition: string2.h:41
#define NONULL(x)
Definition: string2.h:37
#define S_BKG
Definition: string2.h:42
An email address.
Definition: address.h:36
bool group
Group mailbox?
Definition: address.h:39
char * mailbox
Mailbox and host address.
Definition: address.h:38
String manipulation buffer.
Definition: buffer.h:34
A set of inherited config items.
Definition: subset.h:47
A mailbox.
Definition: mailbox.h:79
Data to be displayed by PagerView.
Definition: lib.h:158
const char * fname
Name of the file to read.
Definition: lib.h:162
Paged view into some data.
Definition: lib.h:169
struct PagerData * pdata
Data that pager displays. NOTNULL.
Definition: lib.h:170
enum PagerMode mode
Pager mode.
Definition: lib.h:171
PagerFlags flags
Additional settings to tweak pager's function.
Definition: lib.h:172
const char * banner
Title to display in status bar.
Definition: lib.h:173