1*07f684ffSforsyth #include <sys/types.h>
2*07f684ffSforsyth #include <signal.h>
3*07f684ffSforsyth #include <pwd.h>
4*07f684ffSforsyth #include <sys/resource.h>
5*07f684ffSforsyth #include <sys/wait.h>
6*07f684ffSforsyth #include <fcntl.h>
7*07f684ffSforsyth
8*07f684ffSforsyth #include "dat.h"
9*07f684ffSforsyth #include "fns.h"
10*07f684ffSforsyth #include "error.h"
11*07f684ffSforsyth
12*07f684ffSforsyth enum
13*07f684ffSforsyth {
14*07f684ffSforsyth Debug = 0
15*07f684ffSforsyth };
16*07f684ffSforsyth
17*07f684ffSforsyth /*
18*07f684ffSforsyth * os-specific devcmd support.
19*07f684ffSforsyth * this version should be reasonably portable across Unix systems.
20*07f684ffSforsyth */
21*07f684ffSforsyth typedef struct Targ Targ;
22*07f684ffSforsyth struct Targ
23*07f684ffSforsyth {
24*07f684ffSforsyth int fd[3]; /* fd[0] is standard input, fd[1] is standard output, fd[2] is standard error */
25*07f684ffSforsyth char** args;
26*07f684ffSforsyth char* dir;
27*07f684ffSforsyth int pid;
28*07f684ffSforsyth int wfd; /* child writes errors that occur after the fork or on exec */
29*07f684ffSforsyth int uid;
30*07f684ffSforsyth int gid;
31*07f684ffSforsyth };
32*07f684ffSforsyth
33*07f684ffSforsyth extern int gidnobody;
34*07f684ffSforsyth extern int uidnobody;
35*07f684ffSforsyth
36*07f684ffSforsyth static int
childproc(Targ * t)37*07f684ffSforsyth childproc(Targ *t)
38*07f684ffSforsyth {
39*07f684ffSforsyth int i, nfd;
40*07f684ffSforsyth
41*07f684ffSforsyth if(Debug)
42*07f684ffSforsyth print("devcmd: '%s'", t->args[0]);
43*07f684ffSforsyth
44*07f684ffSforsyth nfd = getdtablesize();
45*07f684ffSforsyth for(i = 0; i < nfd; i++)
46*07f684ffSforsyth if(i != t->fd[0] && i != t->fd[1] && i != t->fd[2] && i != t->wfd)
47*07f684ffSforsyth close(i);
48*07f684ffSforsyth
49*07f684ffSforsyth dup2(t->fd[0], 0);
50*07f684ffSforsyth dup2(t->fd[1], 1);
51*07f684ffSforsyth dup2(t->fd[2], 2);
52*07f684ffSforsyth close(t->fd[0]);
53*07f684ffSforsyth close(t->fd[1]);
54*07f684ffSforsyth close(t->fd[2]);
55*07f684ffSforsyth
56*07f684ffSforsyth /* should have an auth file to do host-specific authorisation? */
57*07f684ffSforsyth if(t->gid != -1){
58*07f684ffSforsyth if(setgid(t->gid) < 0 && getegid() == 0){
59*07f684ffSforsyth fprint(t->wfd, "can't set gid %d: %s", t->gid, strerror(errno));
60*07f684ffSforsyth _exit(1);
61*07f684ffSforsyth }
62*07f684ffSforsyth }
63*07f684ffSforsyth
64*07f684ffSforsyth if(t->uid != -1){
65*07f684ffSforsyth if(setuid(t->uid) < 0 && geteuid() == 0){
66*07f684ffSforsyth fprint(t->wfd, "can't set uid %d: %s", t->uid, strerror(errno));
67*07f684ffSforsyth _exit(1);
68*07f684ffSforsyth }
69*07f684ffSforsyth }
70*07f684ffSforsyth
71*07f684ffSforsyth if(t->dir != nil && chdir(t->dir) < 0){
72*07f684ffSforsyth fprint(t->wfd, "can't chdir to %s: %s", t->dir, strerror(errno));
73*07f684ffSforsyth _exit(1);
74*07f684ffSforsyth }
75*07f684ffSforsyth
76*07f684ffSforsyth signal(SIGPIPE, SIG_DFL);
77*07f684ffSforsyth
78*07f684ffSforsyth execvp(t->args[0], t->args);
79*07f684ffSforsyth if(Debug)
80*07f684ffSforsyth print("execvp: %s\n",strerror(errno));
81*07f684ffSforsyth fprint(t->wfd, "exec failed: %s", strerror(errno));
82*07f684ffSforsyth
83*07f684ffSforsyth _exit(1);
84*07f684ffSforsyth }
85*07f684ffSforsyth
86*07f684ffSforsyth void*
oscmd(char ** args,int nice,char * dir,int * fd)87*07f684ffSforsyth oscmd(char **args, int nice, char *dir, int *fd)
88*07f684ffSforsyth {
89*07f684ffSforsyth Targ *t;
90*07f684ffSforsyth int r, fd0[2], fd1[2], fd2[2], wfd[2], n, pid;
91*07f684ffSforsyth
92*07f684ffSforsyth t = mallocz(sizeof(*t), 1);
93*07f684ffSforsyth if(t == nil)
94*07f684ffSforsyth return nil;
95*07f684ffSforsyth
96*07f684ffSforsyth fd0[0] = fd0[1] = -1;
97*07f684ffSforsyth fd1[0] = fd1[1] = -1;
98*07f684ffSforsyth fd2[0] = fd2[1] = -1;
99*07f684ffSforsyth wfd[0] = wfd[1] = -1;
100*07f684ffSforsyth if(pipe(fd0) < 0 || pipe(fd1) < 0 || pipe(fd2) < 0 || pipe(wfd) < 0)
101*07f684ffSforsyth goto Error;
102*07f684ffSforsyth if(fcntl(wfd[1], F_SETFD, FD_CLOEXEC) < 0) /* close on exec to give end of file on success */
103*07f684ffSforsyth goto Error;
104*07f684ffSforsyth
105*07f684ffSforsyth t->fd[0] = fd0[0];
106*07f684ffSforsyth t->fd[1] = fd1[1];
107*07f684ffSforsyth t->fd[2] = fd2[1];
108*07f684ffSforsyth t->wfd = wfd[1];
109*07f684ffSforsyth t->args = args;
110*07f684ffSforsyth t->dir = dir;
111*07f684ffSforsyth t->gid = up->env->gid;
112*07f684ffSforsyth if(t->gid == -1)
113*07f684ffSforsyth t->gid = gidnobody;
114*07f684ffSforsyth t->uid = up->env->uid;
115*07f684ffSforsyth if(t->uid == -1)
116*07f684ffSforsyth t->uid = uidnobody;
117*07f684ffSforsyth
118*07f684ffSforsyth signal(SIGCHLD, SIG_DFL);
119*07f684ffSforsyth switch(pid = fork()) {
120*07f684ffSforsyth case -1:
121*07f684ffSforsyth goto Error;
122*07f684ffSforsyth case 0:
123*07f684ffSforsyth setpgid(0, getpid());
124*07f684ffSforsyth if(nice)
125*07f684ffSforsyth oslopri();
126*07f684ffSforsyth childproc(t);
127*07f684ffSforsyth _exit(1);
128*07f684ffSforsyth default:
129*07f684ffSforsyth t->pid = pid;
130*07f684ffSforsyth if(Debug)
131*07f684ffSforsyth print("cmd pid %d\n", t->pid);
132*07f684ffSforsyth break;
133*07f684ffSforsyth }
134*07f684ffSforsyth
135*07f684ffSforsyth close(fd0[0]);
136*07f684ffSforsyth close(fd1[1]);
137*07f684ffSforsyth close(fd2[1]);
138*07f684ffSforsyth close(wfd[1]);
139*07f684ffSforsyth
140*07f684ffSforsyth n = read(wfd[0], up->genbuf, sizeof(up->genbuf)-1);
141*07f684ffSforsyth close(wfd[0]);
142*07f684ffSforsyth if(n > 0){
143*07f684ffSforsyth close(fd0[1]);
144*07f684ffSforsyth close(fd1[0]);
145*07f684ffSforsyth close(fd2[0]);
146*07f684ffSforsyth free(t);
147*07f684ffSforsyth up->genbuf[n] = 0;
148*07f684ffSforsyth if(Debug)
149*07f684ffSforsyth print("oscmd: bad exec: %q\n", up->genbuf);
150*07f684ffSforsyth error(up->genbuf);
151*07f684ffSforsyth return nil;
152*07f684ffSforsyth }
153*07f684ffSforsyth
154*07f684ffSforsyth fd[0] = fd0[1];
155*07f684ffSforsyth fd[1] = fd1[0];
156*07f684ffSforsyth fd[2] = fd2[0];
157*07f684ffSforsyth return t;
158*07f684ffSforsyth
159*07f684ffSforsyth Error:
160*07f684ffSforsyth r = errno;
161*07f684ffSforsyth if(Debug)
162*07f684ffSforsyth print("oscmd: %q\n",strerror(r));
163*07f684ffSforsyth close(fd0[0]);
164*07f684ffSforsyth close(fd0[1]);
165*07f684ffSforsyth close(fd1[0]);
166*07f684ffSforsyth close(fd1[1]);
167*07f684ffSforsyth close(fd2[0]);
168*07f684ffSforsyth close(fd2[1]);
169*07f684ffSforsyth close(wfd[0]);
170*07f684ffSforsyth close(wfd[1]);
171*07f684ffSforsyth error(strerror(r));
172*07f684ffSforsyth return nil;
173*07f684ffSforsyth }
174*07f684ffSforsyth
175*07f684ffSforsyth int
oscmdkill(void * a)176*07f684ffSforsyth oscmdkill(void *a)
177*07f684ffSforsyth {
178*07f684ffSforsyth Targ *t = a;
179*07f684ffSforsyth
180*07f684ffSforsyth if(Debug)
181*07f684ffSforsyth print("kill: %d\n", t->pid);
182*07f684ffSforsyth return kill(-t->pid, SIGTERM);
183*07f684ffSforsyth }
184*07f684ffSforsyth
185*07f684ffSforsyth int
oscmdwait(void * a,char * buf,int n)186*07f684ffSforsyth oscmdwait(void *a, char *buf, int n)
187*07f684ffSforsyth {
188*07f684ffSforsyth Targ *t = a;
189*07f684ffSforsyth int s;
190*07f684ffSforsyth
191*07f684ffSforsyth if(waitpid(t->pid, &s, 0) == -1){
192*07f684ffSforsyth if(Debug)
193*07f684ffSforsyth print("wait error: %d [in %d] %q\n", t->pid, getpid(), strerror(errno));
194*07f684ffSforsyth return -1;
195*07f684ffSforsyth }
196*07f684ffSforsyth if(WIFEXITED(s)){
197*07f684ffSforsyth if(WEXITSTATUS(s) == 0)
198*07f684ffSforsyth return snprint(buf, n, "%d 0 0 0 ''", t->pid);
199*07f684ffSforsyth return snprint(buf, n, "%d 0 0 0 'exit: %d'", t->pid, WEXITSTATUS(s));
200*07f684ffSforsyth }
201*07f684ffSforsyth if(WIFSIGNALED(s)){
202*07f684ffSforsyth if(WTERMSIG(s) == SIGTERM || WTERMSIG(s) == SIGKILL)
203*07f684ffSforsyth return snprint(buf, n, "%d 0 0 0 killed", t->pid);
204*07f684ffSforsyth return snprint(buf, n, "%d 0 0 0 'signal: %d'", t->pid, WTERMSIG(s));
205*07f684ffSforsyth }
206*07f684ffSforsyth return snprint(buf, n, "%d 0 0 0 'odd status: 0x%x'", t->pid, s);
207*07f684ffSforsyth }
208*07f684ffSforsyth
209*07f684ffSforsyth void
oscmdfree(void * a)210*07f684ffSforsyth oscmdfree(void *a)
211*07f684ffSforsyth {
212*07f684ffSforsyth free(a);
213*07f684ffSforsyth }
214