]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ppp/exec.c
mdoc(7) police:
[FreeBSD/FreeBSD.git] / usr.sbin / ppp / exec.c
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 };
86
87 struct device *
88 exec_iov2device(int type, struct physical *p, struct iovec *iov,
89                 int *niov, int maxiov, int *auxfd, int *nauxfd)
90 {
91   if (type == EXEC_DEVICE) {
92     free(iov[(*niov)++].iov_base);
93     physical_SetupStack(p, execdevice.name, PHYSICAL_NOFORCE);
94     return &execdevice;
95   }
96
97   return NULL;
98 }
99
100 struct device *
101 exec_Create(struct physical *p)
102 {
103   if (p->fd < 0 && *p->name.full == '!') {
104     int fids[2], type;
105
106     p->fd--;    /* We own the device but maybe can't use it - change fd */
107     type = physical_IsSync(p) ? SOCK_DGRAM : SOCK_STREAM;
108
109     if (socketpair(AF_UNIX, type, PF_UNSPEC, fids) < 0)
110       log_Printf(LogPHASE, "Unable to create pipe for line exec: %s\n",
111                  strerror(errno));
112     else {
113       static int child_status;          /* This variable is abused ! */
114       int stat, argc, i, ret, wret, pidpipe[2];
115       pid_t pid, realpid;
116       char *argv[MAXARGS];
117
118       stat = fcntl(fids[0], F_GETFL, 0);
119       if (stat > 0) {
120         stat |= O_NONBLOCK;
121         fcntl(fids[0], F_SETFL, stat);
122       }
123       realpid = getpid();
124       if (pipe(pidpipe) == -1) {
125         log_Printf(LogPHASE, "Unable to pipe for line exec: %s\n",
126                    strerror(errno));
127         close(fids[1]);
128       } else switch ((pid = fork())) {
129         case -1:
130           log_Printf(LogPHASE, "Unable to fork for line exec: %s\n",
131                      strerror(errno));
132           close(pidpipe[0]);
133           close(pidpipe[1]);
134           close(fids[1]);
135           break;
136
137         case  0:
138           close(pidpipe[0]);
139           close(fids[0]);
140           timer_TermService();
141 #ifndef NOSUID
142           setuid(ID0realuid());
143 #endif
144
145           child_status = 0;
146           switch ((pid = vfork())) {
147             case 0:
148               close(pidpipe[1]);
149               break;
150
151             case -1:
152               ret = errno;
153               log_Printf(LogPHASE, "Unable to vfork to drop parent: %s\n",
154                          strerror(errno));
155               close(pidpipe[1]);
156               _exit(ret);
157
158             default:
159               write(pidpipe[1], &pid, sizeof pid);
160               close(pidpipe[1]);
161               _exit(child_status);      /* The error from exec() ! */
162           }
163
164           log_Printf(LogDEBUG, "Exec'ing ``%s''\n", p->name.base);
165
166           if ((argc = MakeArgs(p->name.base, argv, VECSIZE(argv),
167                                PARSE_REDUCE|PARSE_NOHASH)) < 0) {
168             log_Printf(LogWARN, "Syntax error in exec command\n");
169             _exit(ESRCH);
170           }
171
172           command_Expand(argv, argc, (char const *const *)argv,
173                          p->dl->bundle, 0, realpid);
174
175           dup2(fids[1], STDIN_FILENO);
176           dup2(fids[1], STDOUT_FILENO);
177           dup2(fids[1], STDERR_FILENO);
178           for (i = getdtablesize(); i > STDERR_FILENO; i--)
179             fcntl(i, F_SETFD, 1);
180
181           execvp(*argv, argv);
182           child_status = errno;         /* Only works for vfork() */
183           printf("execvp failed: %s: %s\r\n", *argv, strerror(child_status));
184           _exit(child_status);
185           break;
186
187         default:
188           close(pidpipe[1]);
189           close(fids[1]);
190           if (read(pidpipe[0], &p->session_owner, sizeof p->session_owner) !=
191               sizeof p->session_owner)
192             p->session_owner = (pid_t)-1;
193           close(pidpipe[0]);
194           while ((wret = waitpid(pid, &stat, 0)) == -1 && errno == EINTR)
195             ;
196           if (wret == -1) {
197             log_Printf(LogWARN, "Waiting for child process: %s\n",
198                        strerror(errno));
199             close(fids[0]);
200             p->session_owner = (pid_t)-1;
201             break;
202           } else if (WIFSIGNALED(stat)) {
203             log_Printf(LogWARN, "Child process received sig %d !\n",
204                        WTERMSIG(stat));
205             close(fids[0]);
206             p->session_owner = (pid_t)-1;
207             break;
208           } else if (WIFSTOPPED(stat)) {
209             log_Printf(LogWARN, "Child process received stop sig %d !\n",
210                        WSTOPSIG(stat));
211             /* I guess that's ok.... */
212           } else if ((ret = WEXITSTATUS(stat))) {
213             log_Printf(LogWARN, "Cannot exec \"%s\": %s\n", p->name.base,
214                        strerror(ret));
215             close(fids[0]);
216             p->session_owner = (pid_t)-1;
217             break;
218           }
219           p->fd = fids[0];
220           log_Printf(LogDEBUG, "Using descriptor %d for child\n", p->fd);
221           physical_SetupStack(p, execdevice.name, PHYSICAL_NOFORCE);
222           if (p->cfg.cd.necessity != CD_DEFAULT)
223             log_Printf(LogWARN, "Carrier settings ignored\n");
224           return &execdevice;
225       }
226       close(fids[0]);
227     }
228   }
229
230   return NULL;
231 }