1 /* $OpenBSD: mda_unpriv.c,v 1.5 2018/12/27 15:41:50 gilles 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 <sys/types.h> 20 #include <sys/queue.h> 21 #include <sys/tree.h> 22 #include <sys/socket.h> 23 24 #include <err.h> 25 #include <errno.h> 26 #include <event.h> 27 #include <imsg.h> 28 #include <paths.h> 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include <unistd.h> 33 #include <limits.h> 34 35 #include "smtpd.h" 36 37 38 void 39 mda_unpriv(struct dispatcher *dsp, struct deliver *deliver, 40 const char *pw_name, const char *pw_dir) 41 { 42 int idx; 43 char *mda_environ[10]; 44 char mda_exec[LINE_MAX]; 45 char mda_wrapper[LINE_MAX]; 46 const char *mda_command; 47 const char *mda_command_wrap; 48 49 if (deliver->mda_exec[0]) 50 mda_command = deliver->mda_exec; 51 else 52 mda_command = dsp->u.local.command; 53 54 if (strlcpy(mda_exec, mda_command, sizeof (mda_exec)) 55 >= sizeof (mda_exec)) 56 errx(1, "mda command line too long"); 57 58 if (mda_expand_format(mda_exec, sizeof mda_exec, deliver, 59 &deliver->userinfo, NULL) == -1) 60 errx(1, "mda command line could not be expanded"); 61 62 mda_command = mda_exec; 63 64 /* setup environment similar to other MTA */ 65 idx = 0; 66 xasprintf(&mda_environ[idx++], "PATH=%s", _PATH_DEFPATH); 67 xasprintf(&mda_environ[idx++], "DOMAIN=%s", deliver->rcpt.domain); 68 xasprintf(&mda_environ[idx++], "HOME=%s", pw_dir); 69 xasprintf(&mda_environ[idx++], "RECIPIENT=%s@%s", deliver->dest.user, deliver->dest.domain); 70 xasprintf(&mda_environ[idx++], "SHELL=/bin/sh"); 71 xasprintf(&mda_environ[idx++], "LOCAL=%s", deliver->rcpt.user); 72 xasprintf(&mda_environ[idx++], "LOGNAME=%s", pw_name); 73 xasprintf(&mda_environ[idx++], "USER=%s", pw_name); 74 75 if (deliver->mda_subaddress[0]) 76 xasprintf(&mda_environ[idx++], "EXTENSION=%s", deliver->mda_subaddress); 77 78 mda_environ[idx++] = (char *)NULL; 79 80 if (dsp->u.local.mda_wrapper) { 81 mda_command_wrap = dict_get(env->sc_mda_wrappers, 82 dsp->u.local.mda_wrapper); 83 if (mda_command_wrap == NULL) 84 errx(1, "could not find wrapper %s", 85 dsp->u.local.mda_wrapper); 86 87 if (strlcpy(mda_wrapper, mda_command_wrap, sizeof (mda_wrapper)) 88 >= sizeof (mda_wrapper)) 89 errx(1, "mda command line too long"); 90 91 if (mda_expand_format(mda_wrapper, sizeof mda_wrapper, deliver, 92 &deliver->userinfo, mda_command) == -1) 93 errx(1, "mda command line could not be expanded"); 94 mda_command = mda_wrapper; 95 } 96 execle("/bin/sh", "/bin/sh", "-c", mda_command, (char *)NULL, 97 mda_environ); 98 99 perror("execle"); 100 _exit(1); 101 } 102 103