NeoMutt  2024-12-12-19-ge4b57e
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
sendmail.c File Reference

Send email using sendmail. More...

#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <regex.h>
#include <signal.h>
#include <stdbool.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "sendmail.h"
#include "expando/lib.h"
#include "nntp/lib.h"
#include "pager/lib.h"
#include "globals.h"
+ Include dependency graph for sendmail.c:

Go to the source code of this file.

Macros

#define EX_OK   0
 

Functions

 ARRAY_HEAD (SendmailArgArray, const char *)
 
static void alarm_handler (int sig)
 Async notification of an alarm signal.
 
static int send_msg (const char *path, struct SendmailArgArray *args, const char *msg, char **tempfile, int wait_time)
 Invoke sendmail in a subshell.
 
static void add_args_one (struct SendmailArgArray *args, const struct Address *addr)
 Add an Address to a dynamic array.
 
static void add_args (struct SendmailArgArray *args, struct AddressList *al)
 Add a list of Addresses to a dynamic array.
 
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.
 

Variables

char ** environ
 
static volatile sig_atomic_t SigAlrm
 true after SIGALRM is received
 

Detailed Description

Send email using sendmail.

Authors
  • Pietro Cerutti
  • Richard Russon
  • Ihor Antonov
  • Tóth János

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file sendmail.c.

Macro Definition Documentation

◆ EX_OK

#define EX_OK   0

Definition at line 57 of file sendmail.c.

Function Documentation

◆ ARRAY_HEAD()

ARRAY_HEAD ( SendmailArgArray  ,
const char *   
)

◆ alarm_handler()

static void alarm_handler ( int  sig)
static

Async notification of an alarm signal.

Parameters
sigSignal, (SIGALRM)

Definition at line 73 of file sendmail.c.

74{
75 SigAlrm = 1;
76}
static volatile sig_atomic_t SigAlrm
true after SIGALRM is received
Definition: sendmail.c:65
+ Here is the caller graph for this function:

◆ send_msg()

static int send_msg ( const char *  path,
struct SendmailArgArray *  args,
const char *  msg,
char **  tempfile,
int  wait_time 
)
static

Invoke sendmail in a subshell.

Parameters
[in]pathPath to program to execute
[in]argsArguments to pass to program
[in]msgTemp file containing message to send
[out]tempfileIf sendmail is put in the background, this points to the temporary file containing the stdout of the child process. If it is NULL, stderr and stdout are not redirected.
[in]wait_timeHow long to wait for sendmail, $sendmail_wait
Return values
0Success
>0Failure, return code from sendmail

Definition at line 91 of file sendmail.c.

93{
94 sigset_t set;
95 int st;
96
98
99 sigemptyset(&set);
100 /* we also don't want to be stopped right now */
101 sigaddset(&set, SIGTSTP);
102 sigprocmask(SIG_BLOCK, &set, NULL);
103
104 if ((wait_time >= 0) && tempfile)
105 {
106 struct Buffer *tmp = buf_pool_get();
107 buf_mktemp(tmp);
108 *tempfile = buf_strdup(tmp);
109 buf_pool_release(&tmp);
110 }
111
112 pid_t pid = fork();
113 if (pid == 0)
114 {
115 struct sigaction act = { 0 };
116 struct sigaction oldalrm = { 0 };
117
118 /* save parent's ID before setsid() */
119 pid_t ppid = getppid();
120
121 /* we want the delivery to continue even after the main process dies,
122 * so we put ourselves into another session right away */
123 setsid();
124
125 /* next we close all open files */
126 close(0);
127#ifdef OPEN_MAX
128 for (int fd = tempfile ? 1 : 3; fd < OPEN_MAX; fd++)
129 close(fd);
130#elif defined(_POSIX_OPEN_MAX)
131 for (int fd = tempfile ? 1 : 3; fd < _POSIX_OPEN_MAX; fd++)
132 close(fd);
133#else
134 if (tempfile)
135 {
136 close(1);
137 close(2);
138 }
139#endif
140
141 /* now the second fork() */
142 pid = fork();
143 if (pid == 0)
144 {
145 /* "msg" will be opened as stdin */
146 if (open(msg, O_RDONLY, 0) < 0)
147 {
148 unlink(msg);
149 _exit(S_ERR);
150 }
151 unlink(msg);
152
153 if ((wait_time >= 0) && tempfile && *tempfile)
154 {
155 /* *tempfile will be opened as stdout */
156 if (open(*tempfile, O_WRONLY | O_APPEND | O_CREAT | O_EXCL, 0600) < 0)
157 _exit(S_ERR);
158 /* redirect stderr to *tempfile too */
159 if (dup(1) < 0)
160 _exit(S_ERR);
161 }
162 else if (tempfile)
163 {
164 if (open("/dev/null", O_WRONLY | O_APPEND) < 0) /* stdout */
165 _exit(S_ERR);
166 if (open("/dev/null", O_RDWR | O_APPEND) < 0) /* stderr */
167 _exit(S_ERR);
168 }
169
171
172 /* execvpe is a glibc extension, so just manually set environ */
174 execvp(path, (char **) args->entries);
175 _exit(S_ERR);
176 }
177 else if (pid == -1)
178 {
179 unlink(msg);
180 FREE(tempfile);
181 _exit(S_ERR);
182 }
183
184 /* wait_time > 0: interrupt waitpid() after wait_time seconds
185 * wait_time = 0: wait forever
186 * wait_time < 0: don't wait */
187 if (wait_time > 0)
188 {
189 SigAlrm = 0;
190 act.sa_handler = alarm_handler;
191#ifdef SA_INTERRUPT
192 /* need to make sure waitpid() is interrupted on SIGALRM */
193 act.sa_flags = SA_INTERRUPT;
194#else
195 act.sa_flags = 0;
196#endif
197 sigemptyset(&act.sa_mask);
198 sigaction(SIGALRM, &act, &oldalrm);
199 alarm(wait_time);
200 }
201 else if (wait_time < 0)
202 {
203 _exit(0xff & EX_OK);
204 }
205
206 if (waitpid(pid, &st, 0) > 0)
207 {
208 st = WIFEXITED(st) ? WEXITSTATUS(st) : S_ERR;
209 if (wait_time && (st == (0xff & EX_OK)) && tempfile && *tempfile)
210 {
211 unlink(*tempfile); /* no longer needed */
212 FREE(tempfile);
213 }
214 }
215 else
216 {
217 st = ((wait_time > 0) && (errno == EINTR) && SigAlrm) ? S_BKG : S_ERR;
218 if ((wait_time > 0) && tempfile && *tempfile)
219 {
220 unlink(*tempfile);
221 FREE(tempfile);
222 }
223 }
224
225 if (wait_time > 0)
226 {
227 /* reset alarm; not really needed, but... */
228 alarm(0);
229 sigaction(SIGALRM, &oldalrm, NULL);
230 }
231
232 if ((kill(ppid, 0) == -1) && (errno == ESRCH) && tempfile && *tempfile)
233 {
234 /* the parent is already dead */
235 unlink(*tempfile);
236 FREE(tempfile);
237 }
238
239 _exit(st);
240 }
241
242 sigprocmask(SIG_UNBLOCK, &set, NULL);
243
244 if ((pid != -1) && (waitpid(pid, &st, 0) > 0))
245 st = WIFEXITED(st) ? WEXITSTATUS(st) : S_ERR; /* return child status */
246 else
247 st = S_ERR; /* error */
248
250
251 return st;
252}
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:571
char ** EnvList
Private copy of the environment variables.
Definition: globals.c:75
#define FREE(x)
Definition: memory.h:55
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
static void alarm_handler(int sig)
Async notification of an alarm signal.
Definition: sendmail.c:73
char ** environ
#define EX_OK
Definition: sendmail.c:57
void mutt_sig_reset_child_signals(void)
Reset ignored signals back to the default.
Definition: signal.c:321
void mutt_sig_block_system(void)
Block signals before calling exec()
Definition: signal.c:245
void mutt_sig_unblock_system(bool restore)
Restore previously blocked signals.
Definition: signal.c:269
#define S_ERR
Definition: string2.h:40
#define S_BKG
Definition: string2.h:41
String manipulation buffer.
Definition: buffer.h:36
#define buf_mktemp(buf)
Definition: tmp.h:33
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ add_args_one()

static void add_args_one ( struct SendmailArgArray *  args,
const struct Address addr 
)
static

Add an Address to a dynamic array.

Parameters
[in,out]argsArray to add to
[in]addrAddress to add

Definition at line 259 of file sendmail.c.

260{
261 /* weed out group mailboxes, since those are for display only */
262 if (addr->mailbox && !addr->group)
263 {
264 ARRAY_ADD(args, buf_string(addr->mailbox));
265 }
266}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:156
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
bool group
Group mailbox?
Definition: address.h:39
struct Buffer * mailbox
Mailbox and host address.
Definition: address.h:38
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ add_args()

static void add_args ( struct SendmailArgArray *  args,
struct AddressList *  al 
)
static

Add a list of Addresses to a dynamic array.

Parameters
[in,out]argsArray to add to
[in]alAddresses to add

Definition at line 273 of file sendmail.c.

274{
275 if (!al)
276 return;
277
278 struct Address *a = NULL;
279 TAILQ_FOREACH(a, al, entries)
280 {
281 add_args_one(args, a);
282 }
283}
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:743
static void add_args_one(struct SendmailArgArray *args, const struct Address *addr)
Add an Address to a dynamic array.
Definition: sendmail.c:259
An email address.
Definition: address.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_invoke_sendmail()

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.

Parameters
mMailbox
fromThe sender
toRecipients
ccRecipients
bccRecipients
msgFile containing message
eightbitMessage contains 8bit chars
subConfig Subset
Return values
0Success
-1Failure
See also
$inews

Definition at line 300 of file sendmail.c.

304{
305 char *ps = NULL, *path = NULL, *s = NULL, *childout = NULL;
306 struct SendmailArgArray args = ARRAY_HEAD_INITIALIZER;
307 struct SendmailArgArray extra_args = ARRAY_HEAD_INITIALIZER;
308 int i;
309
310 if (OptNewsSend)
311 {
312 struct Buffer *cmd = buf_pool_get();
313
314 const struct Expando *c_inews = cs_subset_expando(sub, "inews");
316 if (buf_is_empty(cmd))
317 {
318 i = nntp_post(m, msg);
319 unlink(msg);
320 buf_pool_release(&cmd);
321 return i;
322 }
323
324 s = buf_strdup(cmd);
325 buf_pool_release(&cmd);
326 }
327 else
328 {
329 const char *const c_sendmail = cs_subset_string(sub, "sendmail");
330 s = mutt_str_dup(c_sendmail);
331 }
332
333 if (!s)
334 {
335 mutt_error(_("$sendmail must be set in order to send mail"));
336 return -1;
337 }
338
339 ps = s;
340 i = 0;
341 while ((ps = strtok(ps, " ")))
342 {
343 if (i)
344 {
345 if (mutt_str_equal(ps, "--"))
346 break;
347 ARRAY_ADD(&args, ps);
348 }
349 else
350 {
351 path = mutt_str_dup(ps);
352 ps = strrchr(ps, '/');
353 if (ps)
354 ps++;
355 else
356 ps = path;
357 ARRAY_ADD(&args, ps);
358 }
359 ps = NULL;
360 i++;
361 }
362
363 if (!OptNewsSend)
364 {
365 /* If $sendmail contained a "--", we save the recipients to append to
366 * args after other possible options added below. */
367 if (ps)
368 {
369 ps = NULL;
370 while ((ps = strtok(ps, " ")))
371 {
372 ARRAY_ADD(&extra_args, ps);
373 ps = NULL;
374 }
375 }
376
377 const bool c_use_8bit_mime = cs_subset_bool(sub, "use_8bit_mime");
378 if (eightbit && c_use_8bit_mime)
379 ARRAY_ADD(&args, "-B8BITMIME");
380
381 const bool c_use_envelope_from = cs_subset_bool(sub, "use_envelope_from");
382 if (c_use_envelope_from)
383 {
384 const struct Address *c_envelope_from_address = cs_subset_address(sub, "envelope_from_address");
385 if (c_envelope_from_address)
386 {
387 ARRAY_ADD(&args, "-f");
388 add_args_one(&args, c_envelope_from_address);
389 }
390 else if (!TAILQ_EMPTY(from) && !TAILQ_NEXT(TAILQ_FIRST(from), entries))
391 {
392 ARRAY_ADD(&args, "-f");
393 add_args(&args, from);
394 }
395 }
396
397 const char *const c_dsn_notify = cs_subset_string(sub, "dsn_notify");
398 if (c_dsn_notify)
399 {
400 ARRAY_ADD(&args, "-N");
401 ARRAY_ADD(&args, c_dsn_notify);
402 }
403
404 const char *const c_dsn_return = cs_subset_string(sub, "dsn_return");
405 if (c_dsn_return)
406 {
407 ARRAY_ADD(&args, "-R");
408 ARRAY_ADD(&args, c_dsn_return);
409 }
410 ARRAY_ADD(&args, "--");
411 const char **e = NULL;
412 ARRAY_FOREACH(e, &extra_args)
413 {
414 ARRAY_ADD(&args, *e);
415 }
416 add_args(&args, to);
417 add_args(&args, cc);
418 add_args(&args, bcc);
419 }
420
421 ARRAY_ADD(&args, NULL);
422
423 const short c_sendmail_wait = cs_subset_number(sub, "sendmail_wait");
424 i = send_msg(path, &args, msg, OptNoCurses ? NULL : &childout, c_sendmail_wait);
425
426 /* Some user's $sendmail command uses gpg for password decryption,
427 * and is set up to prompt using ncurses pinentry. If we
428 * mutt_endwin() it leaves other users staring at a blank screen.
429 * So instead, just force a hard redraw on the next refresh. */
430 if (!OptNoCurses)
431 {
433 }
434
435 if (i != (EX_OK & 0xff))
436 {
437 if (i != S_BKG)
438 {
439 const char *e = mutt_str_sysexit(i);
440 mutt_error(_("Error sending message, child exited %d (%s)"), i, NONULL(e));
441 if (childout)
442 {
443 struct stat st = { 0 };
444
445 if ((stat(childout, &st) == 0) && (st.st_size > 0))
446 {
447 struct PagerData pdata = { 0 };
448 struct PagerView pview = { &pdata };
449
450 pdata.fname = childout;
451
452 pview.banner = _("Output of the delivery process");
454 pview.mode = PAGER_MODE_OTHER;
455
456 mutt_do_pager(&pview, NULL);
457 }
458 }
459 }
460 }
461 else if (childout)
462 {
463 unlink(childout);
464 }
465
466 FREE(&childout);
467 FREE(&path);
468 FREE(&s);
469 ARRAY_FREE(&args);
470 ARRAY_FREE(&extra_args);
471
472 if (i == (EX_OK & 0xff))
473 i = 0;
474 else if (i == S_BKG)
475 i = 1;
476 else
477 i = -1;
478 return i;
479}
const struct Address * cs_subset_address(const struct ConfigSubset *sub, const char *name)
Get an Address config item by name.
Definition: config_type.c:272
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:212
#define ARRAY_FREE(head)
Release all memory.
Definition: array.h:204
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition: array.h:58
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:291
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:291
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:143
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
const struct Expando * cs_subset_expando(const struct ConfigSubset *sub, const char *name)
Get an Expando config item by name.
Definition: config_type.c:357
void mutt_need_hard_redraw(void)
Force a hard refresh.
Definition: curs_lib.c:100
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:122
int expando_filter(const struct Expando *exp, const struct ExpandoRenderCallback *erc, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Render an Expando and run the result through a filter.
Definition: filter.c:138
const struct ExpandoRenderCallback NntpRenderCallbacks[]
Callbacks for Newsrc Expandos.
bool OptNoCurses
(pseudo) when sending in batch mode
Definition: globals.c:69
bool OptNewsSend
(pseudo) used to change behavior when posting
Definition: globals.c:68
#define mutt_error(...)
Definition: logging2.h:92
#define _(a)
Definition: message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
const char * mutt_str_sysexit(int err_num)
Return a string matching an error code.
Definition: string.c:169
int nntp_post(struct Mailbox *m, const char *msg)
Post article.
Definition: nntp.c:1946
#define MUTT_PAGER_NO_FLAGS
No flags are set.
Definition: lib.h:60
@ PAGER_MODE_OTHER
Pager is invoked via 3rd path. Non-email content is likely to be shown.
Definition: lib.h:142
#define TAILQ_FIRST(head)
Definition: queue.h:741
#define TAILQ_NEXT(elm, field)
Definition: queue.h:850
#define TAILQ_EMPTY(head)
Definition: queue.h:739
#define MUTT_FORMAT_NO_FLAGS
No flags are set.
Definition: render.h:33
static void add_args(struct SendmailArgArray *args, struct AddressList *al)
Add a list of Addresses to a dynamic array.
Definition: sendmail.c:273
static int send_msg(const char *path, struct SendmailArgArray *args, const char *msg, char **tempfile, int wait_time)
Invoke sendmail in a subshell.
Definition: sendmail.c:91
#define NONULL(x)
Definition: string2.h:37
size_t dsize
Length of data.
Definition: buffer.h:39
Parsed Expando trees.
Definition: expando.h:41
Data to be displayed by PagerView.
Definition: lib.h:161
const char * fname
Name of the file to read.
Definition: lib.h:165
Paged view into some data.
Definition: lib.h:172
struct PagerData * pdata
Data that pager displays. NOTNULL.
Definition: lib.h:173
enum PagerMode mode
Pager mode.
Definition: lib.h:174
PagerFlags flags
Additional settings to tweak pager's function.
Definition: lib.h:175
const char * banner
Title to display in status bar.
Definition: lib.h:176
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ environ

char** environ
extern

◆ SigAlrm

volatile sig_atomic_t SigAlrm
static

true after SIGALRM is received

Definition at line 65 of file sendmail.c.