xref: /openbsd/usr.sbin/smtpd/mda_unpriv.c (revision d3140113)
1 /*	$OpenBSD: mda_unpriv.c,v 1.8 2021/06/14 17:58:15 eric Exp $	*/
2 
3 /*
4  * Copyright (c) 2018 Gilles Chehade <gilles@poolp.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <err.h>
20 #include <paths.h>
21 #include <string.h>
22 #include <unistd.h>
23 
24 #include "smtpd.h"
25 
26 void
27 mda_unpriv(struct dispatcher *dsp, struct deliver *deliver,
28     const char *pw_name, const char *pw_dir)
29 {
30 	int		idx;
31 	char	       *mda_environ[11];
32 	char		mda_exec[LINE_MAX];
33 	char		mda_wrapper[LINE_MAX];
34 	const char     *mda_command;
35 	const char     *mda_command_wrap;
36 
37 	if (deliver->mda_exec[0])
38 		mda_command = deliver->mda_exec;
39 	else
40 		mda_command = dsp->u.local.command;
41 
42 	if (strlcpy(mda_exec, mda_command, sizeof (mda_exec))
43 	    >= sizeof (mda_exec))
44 		errx(1, "mda command line too long");
45 
46 	if (mda_expand_format(mda_exec, sizeof mda_exec, deliver,
47 		&deliver->userinfo, NULL) == -1)
48 		errx(1, "mda command line could not be expanded");
49 
50 	mda_command = mda_exec;
51 
52 	/* setup environment similar to other MTA */
53 	idx = 0;
54 	xasprintf(&mda_environ[idx++], "PATH=%s", _PATH_DEFPATH);
55 	xasprintf(&mda_environ[idx++], "DOMAIN=%s", deliver->rcpt.domain);
56 	xasprintf(&mda_environ[idx++], "HOME=%s", pw_dir);
57 	xasprintf(&mda_environ[idx++], "RECIPIENT=%s@%s", deliver->dest.user, deliver->dest.domain);
58 	xasprintf(&mda_environ[idx++], "SHELL=/bin/sh");
59 	xasprintf(&mda_environ[idx++], "LOCAL=%s", deliver->rcpt.user);
60 	xasprintf(&mda_environ[idx++], "LOGNAME=%s", deliver->userinfo.username);
61 	xasprintf(&mda_environ[idx++], "USER=%s", deliver->userinfo.username);
62 
63 	if (deliver->sender.user[0])
64 		xasprintf(&mda_environ[idx++], "SENDER=%s@%s",
65 		    deliver->sender.user, deliver->sender.domain);
66 	else
67 		xasprintf(&mda_environ[idx++], "SENDER=");
68 
69 	if (deliver->mda_subaddress[0])
70 		xasprintf(&mda_environ[idx++], "EXTENSION=%s", deliver->mda_subaddress);
71 
72 	mda_environ[idx++] = (char *)NULL;
73 
74 	if (dsp->u.local.mda_wrapper) {
75 		mda_command_wrap = dict_get(env->sc_mda_wrappers,
76 		    dsp->u.local.mda_wrapper);
77 		if (mda_command_wrap == NULL)
78 			errx(1, "could not find wrapper %s",
79 			    dsp->u.local.mda_wrapper);
80 
81 		if (strlcpy(mda_wrapper, mda_command_wrap, sizeof (mda_wrapper))
82 		    >= sizeof (mda_wrapper))
83 			errx(1, "mda command line too long");
84 
85 		if (mda_expand_format(mda_wrapper, sizeof mda_wrapper, deliver,
86 			&deliver->userinfo, mda_command) == -1)
87 			errx(1, "mda command line could not be expanded");
88 		mda_command = mda_wrapper;
89 	}
90 	execle("/bin/sh", "/bin/sh", "-c", mda_command, (char *)NULL,
91             mda_environ);
92 
93 	perror("execle");
94 	_exit(1);
95 }
96 
97