1 /* $OpenBSD: mda_unpriv.c,v 1.7 2020/06/01 05:21:30 chrisz 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[11]; 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", deliver->userinfo.username); 73 xasprintf(&mda_environ[idx++], "USER=%s", deliver->userinfo.username); 74 75 if (deliver->sender.user[0]) 76 xasprintf(&mda_environ[idx++], "SENDER=%s@%s", 77 deliver->sender.user, deliver->sender.domain); 78 else 79 xasprintf(&mda_environ[idx++], "SENDER="); 80 81 if (deliver->mda_subaddress[0]) 82 xasprintf(&mda_environ[idx++], "EXTENSION=%s", deliver->mda_subaddress); 83 84 mda_environ[idx++] = (char *)NULL; 85 86 if (dsp->u.local.mda_wrapper) { 87 mda_command_wrap = dict_get(env->sc_mda_wrappers, 88 dsp->u.local.mda_wrapper); 89 if (mda_command_wrap == NULL) 90 errx(1, "could not find wrapper %s", 91 dsp->u.local.mda_wrapper); 92 93 if (strlcpy(mda_wrapper, mda_command_wrap, sizeof (mda_wrapper)) 94 >= sizeof (mda_wrapper)) 95 errx(1, "mda command line too long"); 96 97 if (mda_expand_format(mda_wrapper, sizeof mda_wrapper, deliver, 98 &deliver->userinfo, mda_command) == -1) 99 errx(1, "mda command line could not be expanded"); 100 mda_command = mda_wrapper; 101 } 102 execle("/bin/sh", "/bin/sh", "-c", mda_command, (char *)NULL, 103 mda_environ); 104 105 perror("execle"); 106 _exit(1); 107 } 108 109