1 /* $OpenBSD: mda_unpriv.c,v 1.9 2024/03/15 21:52:20 op 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[12]; 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++], "ORIGINAL_RECIPIENT=%s@%s", deliver->rcpt.user, deliver->rcpt.domain); 58 xasprintf(&mda_environ[idx++], "RECIPIENT=%s@%s", deliver->dest.user, deliver->dest.domain); 59 xasprintf(&mda_environ[idx++], "SHELL=/bin/sh"); 60 xasprintf(&mda_environ[idx++], "LOCAL=%s", deliver->rcpt.user); 61 xasprintf(&mda_environ[idx++], "LOGNAME=%s", deliver->userinfo.username); 62 xasprintf(&mda_environ[idx++], "USER=%s", deliver->userinfo.username); 63 64 if (deliver->sender.user[0]) 65 xasprintf(&mda_environ[idx++], "SENDER=%s@%s", 66 deliver->sender.user, deliver->sender.domain); 67 else 68 xasprintf(&mda_environ[idx++], "SENDER="); 69 70 if (deliver->mda_subaddress[0]) 71 xasprintf(&mda_environ[idx++], "EXTENSION=%s", deliver->mda_subaddress); 72 73 mda_environ[idx++] = (char *)NULL; 74 75 if (dsp->u.local.mda_wrapper) { 76 mda_command_wrap = dict_get(env->sc_mda_wrappers, 77 dsp->u.local.mda_wrapper); 78 if (mda_command_wrap == NULL) 79 errx(1, "could not find wrapper %s", 80 dsp->u.local.mda_wrapper); 81 82 if (strlcpy(mda_wrapper, mda_command_wrap, sizeof (mda_wrapper)) 83 >= sizeof (mda_wrapper)) 84 errx(1, "mda command line too long"); 85 86 if (mda_expand_format(mda_wrapper, sizeof mda_wrapper, deliver, 87 &deliver->userinfo, mda_command) == -1) 88 errx(1, "mda command line could not be expanded"); 89 mda_command = mda_wrapper; 90 } 91 execle("/bin/sh", "/bin/sh", "-c", mda_command, (char *)NULL, 92 mda_environ); 93 94 perror("execle"); 95 _exit(1); 96 } 97 98