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 "exec.h" 66 67 static struct device execdevice = { 68 EXEC_DEVICE, 69 "exec", 70 { CD_NOTREQUIRED, 0 }, 71 NULL, 72 NULL, 73 NULL, 74 NULL, 75 NULL, 76 NULL, 77 NULL, 78 NULL, 79 NULL, 80 NULL, 81 NULL, 82 NULL 83 }; 84 85 struct device * 86 exec_iov2device(int type, struct physical *p, struct iovec *iov, 87 int *niov, int maxiov, int *auxfd, int *nauxfd) 88 { 89 if (type == EXEC_DEVICE) { 90 free(iov[(*niov)++].iov_base); 91 physical_SetupStack(p, execdevice.name, PHYSICAL_FORCE_ASYNC); 92 return &execdevice; 93 } 94 95 return NULL; 96 } 97 98 struct device * 99 exec_Create(struct physical *p) 100 { 101 if (p->fd < 0 && *p->name.full == '!') { 102 int fids[2]; 103 104 p->fd--; /* We own the device but maybe can't use it - change fd */ 105 106 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fids) < 0) 107 log_Printf(LogPHASE, "Unable to create pipe for line exec: %s\n", 108 strerror(errno)); 109 else { 110 int stat, argc, i; 111 pid_t pid, realpid; 112 char *argv[MAXARGS]; 113 114 stat = fcntl(fids[0], F_GETFL, 0); 115 if (stat > 0) { 116 stat |= O_NONBLOCK; 117 fcntl(fids[0], F_SETFL, stat); 118 } 119 realpid = getpid(); 120 switch ((pid = fork())) { 121 case -1: 122 log_Printf(LogPHASE, "Unable to create pipe for line exec: %s\n", 123 strerror(errno)); 124 break; 125 126 case 0: 127 close(fids[0]); 128 timer_TermService(); 129 setuid(geteuid()); 130 131 switch (fork()) { 132 case 0: 133 break; 134 135 case -1: 136 log_Printf(LogPHASE, "Unable to fork to drop parent: %s\n", 137 strerror(errno)); 138 default: 139 _exit(127); 140 } 141 142 log_Printf(LogDEBUG, "Exec'ing ``%s''\n", p->name.base); 143 144 if ((argc = MakeArgs(p->name.base, argv, VECSIZE(argv))) < 0) { 145 log_Printf(LogWARN, "Syntax error in exec command\n"); 146 _exit(127); 147 } 148 149 command_Expand(argv, argc, (char const *const *)argv, 150 p->dl->bundle, 0, realpid); 151 152 dup2(fids[1], STDIN_FILENO); 153 dup2(fids[1], STDOUT_FILENO); 154 dup2(fids[1], STDERR_FILENO); 155 for (i = getdtablesize(); i > STDERR_FILENO; i--) 156 fcntl(i, F_SETFD, 1); 157 158 execvp(*argv, argv); 159 printf("execvp failed: %s: %s\r\n", *argv, strerror(errno)); 160 _exit(127); 161 break; 162 163 default: 164 close(fids[1]); 165 p->fd = fids[0]; 166 waitpid(pid, &stat, 0); 167 log_Printf(LogDEBUG, "Using descriptor %d for child\n", p->fd); 168 physical_SetupStack(p, execdevice.name, PHYSICAL_FORCE_ASYNC); 169 if (p->cfg.cd.necessity != CD_DEFAULT) 170 log_Printf(LogWARN, "Carrier settings ignored\n"); 171 return &execdevice; 172 } 173 } 174 } 175 176 return NULL; 177 } 178