exec.c revision 47061
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 * $Id: exec.c,v 1.1 1999/05/08 11:06:30 brian Exp $ 27 */ 28 29#include <sys/param.h> 30#include <sys/socket.h> 31#include <netinet/in.h> 32#include <arpa/inet.h> 33#include <netdb.h> 34#include <netinet/in_systm.h> 35#include <netinet/ip.h> 36#include <sys/un.h> 37 38#include <errno.h> 39#include <fcntl.h> 40#include <stdio.h> 41#include <stdlib.h> 42#include <string.h> 43#include <sys/wait.h> 44#include <termios.h> 45#include <unistd.h> 46 47#include "layer.h" 48#include "defs.h" 49#include "mbuf.h" 50#include "log.h" 51#include "sync.h" 52#include "timer.h" 53#include "lqr.h" 54#include "hdlc.h" 55#include "throughput.h" 56#include "fsm.h" 57#include "lcp.h" 58#include "ccp.h" 59#include "link.h" 60#include "async.h" 61#include "slcompress.h" 62#include "iplist.h" 63#include "ipcp.h" 64#include "filter.h" 65#include "descriptor.h" 66#include "physical.h" 67#include "mp.h" 68#ifndef NORADIUS 69#include "radius.h" 70#endif 71#include "chat.h" 72#include "command.h" 73#include "bundle.h" 74#include "prompt.h" 75#include "auth.h" 76#include "chap.h" 77#include "cbcp.h" 78#include "datalink.h" 79#include "exec.h" 80 81static struct device execdevice = { 82 EXEC_DEVICE, 83 "exec", 84 NULL, 85 NULL, 86 NULL, 87 NULL, 88 NULL, 89 NULL, 90 NULL, 91 NULL, 92 NULL, 93 NULL 94}; 95 96struct device * 97exec_iov2device(int type, struct physical *p, struct iovec *iov, 98 int *niov, int maxiov) 99{ 100 if (type == EXEC_DEVICE) 101 return &execdevice; 102 103 return NULL; 104} 105 106struct device * 107exec_Create(struct physical *p) 108{ 109 if (p->fd < 0 && *p->name.full == '!') { 110 int fids[2]; 111 112 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fids) < 0) 113 log_Printf(LogPHASE, "Unable to create pipe for line exec: %s\n", 114 strerror(errno)); 115 else { 116 int stat, argc; 117 pid_t pid; 118 char *argv[MAXARGS]; 119 120 stat = fcntl(fids[0], F_GETFL, 0); 121 if (stat > 0) { 122 stat |= O_NONBLOCK; 123 fcntl(fids[0], F_SETFL, stat); 124 } 125 switch ((pid = fork())) { 126 case -1: 127 log_Printf(LogPHASE, "Unable to create pipe for line exec: %s\n", 128 strerror(errno)); 129 break; 130 131 case 0: 132 close(fids[0]); 133 timer_TermService(); 134 setuid(geteuid()); 135 136 switch (fork()) { 137 case 0: 138 break; 139 140 case -1: 141 log_Printf(LogPHASE, "Unable to fork to drop parent: %s\n", 142 strerror(errno)); 143 default: 144 _exit(127); 145 } 146 147 fids[1] = fcntl(fids[1], F_DUPFD, 3); 148 dup2(fids[1], STDIN_FILENO); 149 dup2(fids[1], STDOUT_FILENO); 150 dup2(fids[1], STDERR_FILENO); 151 152 argc = MakeArgs(p->name.base, argv, VECSIZE(argv)); 153 command_Expand(argv, argc, (char const *const *)argv, 154 p->dl->bundle, 0); 155 execvp(*argv, argv); 156 fprintf(stderr, "execvp failed: %s: %s\r\n", *argv, strerror(errno)); 157 _exit(127); 158 break; 159 160 default: 161 close(fids[1]); 162 p->fd = fids[0]; 163 waitpid(pid, &stat, 0); 164 log_Printf(LogDEBUG, "Using descriptor %d for child\n", p->fd); 165 physical_SetupStack(p, PHYSICAL_FORCE_ASYNC); 166 return &execdevice; 167 } 168 } 169 } 170 171 return NULL; 172} 173