xref: /freebsd/usr.sbin/ppp/exec.c (revision f5a99677a3e38486bcd1b54cd8010e83bcf77d0c)
15d9e6103SBrian Somers /*-
25d9e6103SBrian Somers  * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
35d9e6103SBrian Somers  * All rights reserved.
45d9e6103SBrian Somers  *
55d9e6103SBrian Somers  * Redistribution and use in source and binary forms, with or without
65d9e6103SBrian Somers  * modification, are permitted provided that the following conditions
75d9e6103SBrian Somers  * are met:
85d9e6103SBrian Somers  * 1. Redistributions of source code must retain the above copyright
95d9e6103SBrian Somers  *    notice, this list of conditions and the following disclaimer.
105d9e6103SBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
115d9e6103SBrian Somers  *    notice, this list of conditions and the following disclaimer in the
125d9e6103SBrian Somers  *    documentation and/or other materials provided with the distribution.
135d9e6103SBrian Somers  *
145d9e6103SBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
155d9e6103SBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
165d9e6103SBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
175d9e6103SBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
185d9e6103SBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
195d9e6103SBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
205d9e6103SBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
215d9e6103SBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
225d9e6103SBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
235d9e6103SBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
245d9e6103SBrian Somers  * SUCH DAMAGE.
255d9e6103SBrian Somers  *
26f5a99677SBrian Somers  *	$Id: exec.c,v 1.4 1999/06/01 19:08:57 brian Exp $
275d9e6103SBrian Somers  */
285d9e6103SBrian Somers 
295d9e6103SBrian Somers #include <sys/param.h>
305d9e6103SBrian Somers #include <sys/socket.h>
315d9e6103SBrian Somers #include <netinet/in.h>
325d9e6103SBrian Somers #include <arpa/inet.h>
335d9e6103SBrian Somers #include <netdb.h>
345d9e6103SBrian Somers #include <netinet/in_systm.h>
355d9e6103SBrian Somers #include <netinet/ip.h>
365d9e6103SBrian Somers #include <sys/un.h>
375d9e6103SBrian Somers 
385d9e6103SBrian Somers #include <errno.h>
395d9e6103SBrian Somers #include <fcntl.h>
405d9e6103SBrian Somers #include <stdio.h>
415d9e6103SBrian Somers #include <stdlib.h>
425d9e6103SBrian Somers #include <string.h>
435d9e6103SBrian Somers #include <sys/wait.h>
44f5a99677SBrian Somers #include <sys/uio.h>
455d9e6103SBrian Somers #include <termios.h>
465d9e6103SBrian Somers #include <unistd.h>
475d9e6103SBrian Somers 
485d9e6103SBrian Somers #include "layer.h"
495d9e6103SBrian Somers #include "defs.h"
505d9e6103SBrian Somers #include "mbuf.h"
515d9e6103SBrian Somers #include "log.h"
525d9e6103SBrian Somers #include "sync.h"
535d9e6103SBrian Somers #include "timer.h"
545d9e6103SBrian Somers #include "lqr.h"
555d9e6103SBrian Somers #include "hdlc.h"
565d9e6103SBrian Somers #include "throughput.h"
575d9e6103SBrian Somers #include "fsm.h"
585d9e6103SBrian Somers #include "lcp.h"
595d9e6103SBrian Somers #include "ccp.h"
605d9e6103SBrian Somers #include "link.h"
615d9e6103SBrian Somers #include "async.h"
625d9e6103SBrian Somers #include "slcompress.h"
635d9e6103SBrian Somers #include "iplist.h"
645d9e6103SBrian Somers #include "ipcp.h"
655d9e6103SBrian Somers #include "filter.h"
665d9e6103SBrian Somers #include "descriptor.h"
675d9e6103SBrian Somers #include "physical.h"
685d9e6103SBrian Somers #include "mp.h"
695d9e6103SBrian Somers #ifndef NORADIUS
705d9e6103SBrian Somers #include "radius.h"
715d9e6103SBrian Somers #endif
725d9e6103SBrian Somers #include "chat.h"
735d9e6103SBrian Somers #include "command.h"
745d9e6103SBrian Somers #include "bundle.h"
755d9e6103SBrian Somers #include "prompt.h"
765d9e6103SBrian Somers #include "auth.h"
775d9e6103SBrian Somers #include "chap.h"
785d9e6103SBrian Somers #include "cbcp.h"
795d9e6103SBrian Somers #include "datalink.h"
805d9e6103SBrian Somers #include "exec.h"
815d9e6103SBrian Somers 
826815097bSBrian Somers static struct device execdevice = {
836815097bSBrian Somers   EXEC_DEVICE,
846815097bSBrian Somers   "exec",
856815097bSBrian Somers   NULL,
866815097bSBrian Somers   NULL,
876815097bSBrian Somers   NULL,
886815097bSBrian Somers   NULL,
896815097bSBrian Somers   NULL,
906815097bSBrian Somers   NULL,
916815097bSBrian Somers   NULL,
926815097bSBrian Somers   NULL,
936815097bSBrian Somers   NULL,
946815097bSBrian Somers   NULL
956815097bSBrian Somers };
966815097bSBrian Somers 
976815097bSBrian Somers struct device *
986815097bSBrian Somers exec_iov2device(int type, struct physical *p, struct iovec *iov,
996815097bSBrian Somers                 int *niov, int maxiov)
1005d9e6103SBrian Somers {
101acbd1f00SBrian Somers   if (type == EXEC_DEVICE) {
102f5a99677SBrian Somers     free(iov[(*niov)++].iov_base);
103acbd1f00SBrian Somers     physical_SetupStack(p, execdevice.name, PHYSICAL_FORCE_ASYNC);
1046815097bSBrian Somers     return &execdevice;
105acbd1f00SBrian Somers   }
1066815097bSBrian Somers 
1076815097bSBrian Somers   return NULL;
1086815097bSBrian Somers }
1096815097bSBrian Somers 
1106815097bSBrian Somers struct device *
1116815097bSBrian Somers exec_Create(struct physical *p)
1126815097bSBrian Somers {
1136815097bSBrian Somers   if (p->fd < 0 && *p->name.full == '!') {
1145d9e6103SBrian Somers     int fids[2];
1155d9e6103SBrian Somers 
1165d9e6103SBrian Somers     if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fids) < 0)
1175d9e6103SBrian Somers       log_Printf(LogPHASE, "Unable to create pipe for line exec: %s\n",
1185d9e6103SBrian Somers 	         strerror(errno));
1195d9e6103SBrian Somers     else {
1205d9e6103SBrian Somers       int stat, argc;
1215d9e6103SBrian Somers       pid_t pid;
1225d9e6103SBrian Somers       char *argv[MAXARGS];
1235d9e6103SBrian Somers 
1245d9e6103SBrian Somers       stat = fcntl(fids[0], F_GETFL, 0);
1255d9e6103SBrian Somers       if (stat > 0) {
1265d9e6103SBrian Somers         stat |= O_NONBLOCK;
1275d9e6103SBrian Somers         fcntl(fids[0], F_SETFL, stat);
1285d9e6103SBrian Somers       }
1295d9e6103SBrian Somers       switch ((pid = fork())) {
1305d9e6103SBrian Somers         case -1:
1315d9e6103SBrian Somers           log_Printf(LogPHASE, "Unable to create pipe for line exec: %s\n",
1325d9e6103SBrian Somers 	             strerror(errno));
1335d9e6103SBrian Somers           break;
1345d9e6103SBrian Somers 
1355d9e6103SBrian Somers         case  0:
1365d9e6103SBrian Somers           close(fids[0]);
1375d9e6103SBrian Somers           timer_TermService();
1385d9e6103SBrian Somers           setuid(geteuid());
1395d9e6103SBrian Somers 
1405d9e6103SBrian Somers           switch (fork()) {
1415d9e6103SBrian Somers             case 0:
1425d9e6103SBrian Somers               break;
1435d9e6103SBrian Somers 
1445d9e6103SBrian Somers             case -1:
1455d9e6103SBrian Somers               log_Printf(LogPHASE, "Unable to fork to drop parent: %s\n",
1465d9e6103SBrian Somers 	                 strerror(errno));
1475d9e6103SBrian Somers             default:
1485d9e6103SBrian Somers               _exit(127);
1495d9e6103SBrian Somers           }
1505d9e6103SBrian Somers 
1515d9e6103SBrian Somers           fids[1] = fcntl(fids[1], F_DUPFD, 3);
1525d9e6103SBrian Somers           dup2(fids[1], STDIN_FILENO);
1535d9e6103SBrian Somers           dup2(fids[1], STDOUT_FILENO);
1545d9e6103SBrian Somers           dup2(fids[1], STDERR_FILENO);
1555d9e6103SBrian Somers 
156e6923505SBrian Somers           log_Printf(LogDEBUG, "Exec'ing ``%s''\n", p->name.base);
1575d9e6103SBrian Somers           argc = MakeArgs(p->name.base, argv, VECSIZE(argv));
1585d9e6103SBrian Somers           command_Expand(argv, argc, (char const *const *)argv,
1595d9e6103SBrian Somers                          p->dl->bundle, 0);
1605d9e6103SBrian Somers           execvp(*argv, argv);
1615d9e6103SBrian Somers           fprintf(stderr, "execvp failed: %s: %s\r\n", *argv, strerror(errno));
1625d9e6103SBrian Somers           _exit(127);
1635d9e6103SBrian Somers           break;
1645d9e6103SBrian Somers 
1655d9e6103SBrian Somers         default:
1665d9e6103SBrian Somers           close(fids[1]);
1675d9e6103SBrian Somers           p->fd = fids[0];
1685d9e6103SBrian Somers           waitpid(pid, &stat, 0);
1695d9e6103SBrian Somers           log_Printf(LogDEBUG, "Using descriptor %d for child\n", p->fd);
170acbd1f00SBrian Somers           physical_SetupStack(p, execdevice.name, PHYSICAL_FORCE_ASYNC);
1716815097bSBrian Somers           return &execdevice;
1725d9e6103SBrian Somers       }
1735d9e6103SBrian Somers     }
1745d9e6103SBrian Somers   }
1755d9e6103SBrian Somers 
1766815097bSBrian Somers   return NULL;
1775d9e6103SBrian Somers }
178