1 /*- 2 * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/socket.h> 31 #include <sys/un.h> 32 33 #include <errno.h> 34 #include <fcntl.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <sys/wait.h> 39 #include <sys/uio.h> 40 #include <termios.h> 41 #include <unistd.h> 42 43 #include "layer.h" 44 #include "defs.h" 45 #include "mbuf.h" 46 #include "log.h" 47 #include "timer.h" 48 #include "lqr.h" 49 #include "hdlc.h" 50 #include "throughput.h" 51 #include "fsm.h" 52 #include "lcp.h" 53 #include "ccp.h" 54 #include "link.h" 55 #include "async.h" 56 #include "descriptor.h" 57 #include "physical.h" 58 #include "mp.h" 59 #include "chat.h" 60 #include "command.h" 61 #include "auth.h" 62 #include "chap.h" 63 #include "cbcp.h" 64 #include "datalink.h" 65 #include "id.h" 66 #include "exec.h" 67 68 static struct device execdevice = { 69 EXEC_DEVICE, 70 "exec", 71 0, 72 { CD_NOTREQUIRED, 0 }, 73 NULL, 74 NULL, 75 NULL, 76 NULL, 77 NULL, 78 NULL, 79 NULL, 80 NULL, 81 NULL, 82 NULL, 83 NULL, 84 NULL, 85 NULL 86 }; 87 88 struct device * 89 exec_iov2device(int type, struct physical *p, struct iovec *iov, 90 int *niov, int maxiov, int *auxfd, int *nauxfd) 91 { 92 if (type == EXEC_DEVICE) { 93 free(iov[(*niov)++].iov_base); 94 physical_SetupStack(p, execdevice.name, PHYSICAL_NOFORCE); 95 return &execdevice; 96 } 97 98 return NULL; 99 } 100 101 struct device * 102 exec_Create(struct physical *p) 103 { 104 if (p->fd < 0 && *p->name.full == '!') { 105 int fids[2], type; 106 107 p->fd--; /* We own the device but maybe can't use it - change fd */ 108 type = physical_IsSync(p) ? SOCK_DGRAM : SOCK_STREAM; 109 110 if (socketpair(AF_UNIX, type, PF_UNSPEC, fids) < 0) 111 log_Printf(LogPHASE, "Unable to create pipe for line exec: %s\n", 112 strerror(errno)); 113 else { 114 static int child_status; /* This variable is abused ! */ 115 int stat, argc, i, ret, wret, pidpipe[2]; 116 pid_t pid, realpid; 117 char *argv[MAXARGS]; 118 119 stat = fcntl(fids[0], F_GETFL, 0); 120 if (stat > 0) { 121 stat |= O_NONBLOCK; 122 fcntl(fids[0], F_SETFL, stat); 123 } 124 realpid = getpid(); 125 if (pipe(pidpipe) == -1) { 126 log_Printf(LogPHASE, "Unable to pipe for line exec: %s\n", 127 strerror(errno)); 128 close(fids[1]); 129 } else switch ((pid = fork())) { 130 case -1: 131 log_Printf(LogPHASE, "Unable to fork for line exec: %s\n", 132 strerror(errno)); 133 close(pidpipe[0]); 134 close(pidpipe[1]); 135 close(fids[1]); 136 break; 137 138 case 0: 139 close(pidpipe[0]); 140 close(fids[0]); 141 timer_TermService(); 142 #ifndef NOSUID 143 setuid(ID0realuid()); 144 #endif 145 146 child_status = 0; 147 switch ((pid = vfork())) { 148 case 0: 149 close(pidpipe[1]); 150 break; 151 152 case -1: 153 ret = errno; 154 log_Printf(LogPHASE, "Unable to vfork to drop parent: %s\n", 155 strerror(errno)); 156 close(pidpipe[1]); 157 _exit(ret); 158 159 default: 160 write(pidpipe[1], &pid, sizeof pid); 161 close(pidpipe[1]); 162 _exit(child_status); /* The error from exec() ! */ 163 } 164 165 log_Printf(LogDEBUG, "Exec'ing ``%s''\n", p->name.base); 166 167 if ((argc = MakeArgs(p->name.base, argv, VECSIZE(argv), 168 PARSE_REDUCE|PARSE_NOHASH)) < 0) { 169 log_Printf(LogWARN, "Syntax error in exec command\n"); 170 _exit(ESRCH); 171 } 172 173 command_Expand(argv, argc, (char const *const *)argv, 174 p->dl->bundle, 0, realpid); 175 176 dup2(fids[1], STDIN_FILENO); 177 dup2(fids[1], STDOUT_FILENO); 178 dup2(fids[1], STDERR_FILENO); 179 for (i = getdtablesize(); i > STDERR_FILENO; i--) 180 fcntl(i, F_SETFD, 1); 181 182 execvp(*argv, argv); 183 child_status = errno; /* Only works for vfork() */ 184 printf("execvp failed: %s: %s\r\n", *argv, strerror(child_status)); 185 _exit(child_status); 186 break; 187 188 default: 189 close(pidpipe[1]); 190 close(fids[1]); 191 if (read(pidpipe[0], &p->session_owner, sizeof p->session_owner) != 192 sizeof p->session_owner) 193 p->session_owner = (pid_t)-1; 194 close(pidpipe[0]); 195 while ((wret = waitpid(pid, &stat, 0)) == -1 && errno == EINTR) 196 ; 197 if (wret == -1) { 198 log_Printf(LogWARN, "Waiting for child process: %s\n", 199 strerror(errno)); 200 close(fids[0]); 201 p->session_owner = (pid_t)-1; 202 break; 203 } else if (WIFSIGNALED(stat)) { 204 log_Printf(LogWARN, "Child process received sig %d !\n", 205 WTERMSIG(stat)); 206 close(fids[0]); 207 p->session_owner = (pid_t)-1; 208 break; 209 } else if (WIFSTOPPED(stat)) { 210 log_Printf(LogWARN, "Child process received stop sig %d !\n", 211 WSTOPSIG(stat)); 212 /* I guess that's ok.... */ 213 } else if ((ret = WEXITSTATUS(stat))) { 214 log_Printf(LogWARN, "Cannot exec \"%s\": %s\n", p->name.base, 215 strerror(ret)); 216 close(fids[0]); 217 p->session_owner = (pid_t)-1; 218 break; 219 } 220 p->fd = fids[0]; 221 log_Printf(LogDEBUG, "Using descriptor %d for child\n", p->fd); 222 physical_SetupStack(p, execdevice.name, PHYSICAL_NOFORCE); 223 if (p->cfg.cd.necessity != CD_DEFAULT) 224 log_Printf(LogWARN, "Carrier settings ignored\n"); 225 return &execdevice; 226 } 227 close(fids[0]); 228 } 229 } 230 231 return NULL; 232 } 233