chiark / gitweb /
Fix builtins.
[userv.git] / servexec.c
1 /*
2  * userv - execserv.c
3  * daemon code which executes actual service (ie child process)
4  *
5  * Copyright (C)1996-1997 Ian Jackson
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with userv; if not, write to the Free Software
19  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 #include <stdio.h>
23 #include <limits.h>
24 #include <errno.h>
25 #include <assert.h>
26 #include <unistd.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
31 #include <signal.h>
32
33 #include "config.h"
34 #include "common.h"
35 #include "daemon.h"
36 #include "lib.h"
37 #include "version.h"
38
39 static void NONRETURNING serv_syscallfail(const char *msg) {
40   fputs("uservd(service): ",stderr);
41   perror(msg);
42   _exit(-1);
43 }
44
45 static void NONRETURNING serv_checkstdoutexit(void) {
46   if (ferror(stdout) || fclose(stdout)) serv_syscallfail("write stdout");
47   _exit(0);
48 }
49
50 void bisexec_environment(const char *const *argv) {
51   execlp("env","env",(char*)0);
52   serv_syscallfail("execute `env'");
53 }
54
55 void bisexec_parameter(const char *const *argv) {
56   always_dumpparameter(execargs[0],execargs+1);
57   serv_checkstdoutexit();
58 }
59   
60 void bisexec_version(const char *const *argv) {
61   const unsigned char *p;
62   int i;
63   
64   printf("uservd version " VERSION "; copyright (C)1996-1997 Ian Jackson.\n"
65 #ifdef DEBUG
66          "DEBUGGING VERSION"
67 #else
68          "production version"
69 #endif
70          " - protocol magic number %08lx\n"
71          "protocol checksum: ",
72          BASE_MAGIC);
73   for (i=0, p=protocolchecksumversion; i<sizeof(protocolchecksumversion); i++, p++)
74     printf("%02x",*p);
75   printf("\n"
76          "rendezvous socket: `" RENDEZVOUSPATH "'\n"
77          "system config dir: `" SYSTEMCONFIGDIR "'\n"
78          "pipe filename format: `%s' (max length %d)\n"
79          "maximums:    fd %-10d                general string %d\n"
80          "             gids %-10d              override length %d\n\n"
81          "             args or variables %-10d error message %d\n"
82          "             nested inclusion %-10d  errno string reserve %d\n",
83          PIPEFORMAT, PIPEMAXLEN,
84          MAX_ALLOW_FD, MAX_GENERAL_STRING,
85          MAX_GIDS, MAX_OVERRIDE_LEN,
86          MAX_ARGSDEFVAR, ERRMSG_RESERVE_ERRNO,
87          MAX_INCLUDE_NEST, MAX_ERRMSG_LEN);
88   serv_checkstdoutexit();
89 }
90
91 static void NONRETURNING dumpconfig(const char *string) {
92   int nspaces, c, lnl;
93   
94   nspaces= 0;
95   lnl= 1;
96   while ((c= *string++)) {
97     switch (c) {
98     case ' ': nspaces++; break;
99     case '\n':
100       if (!lnl) putchar('\n');
101       nspaces= 0; lnl= 1;
102       break;
103     default:
104       while (nspaces>0) { putchar(' '); nspaces--; }
105       putchar(c);
106       lnl= 0;
107       break;
108     }
109   }
110   assert(lnl);
111   serv_checkstdoutexit();
112 }
113
114 void bisexec_toplevel(const char *const *argv) {
115   dumpconfig(TOPLEVEL_CONFIGURATION);
116 }
117
118 void bisexec_override(const char *const *argv) {
119   dumpconfig(TOPLEVEL_OVERRIDDEN_CONFIGURATION);
120 }
121
122 void bisexec_reset(const char *const *argv) {
123   dumpconfig(RESET_CONFIGURATION);
124 }
125
126 void bisexec_execute(const char *const *argv) {
127   always_dumpexecsettings();
128   serv_checkstdoutexit();
129 }
130
131 static void serv_resetsignal(int signo) {
132   struct sigaction sig;
133   
134   sig.sa_handler= SIG_DFL;
135   sigemptyset(&sig.sa_mask);
136   sig.sa_flags= 0;
137   if (sigaction(signo,&sig,0)) serv_syscallfail("reset signal handler");
138 }
139
140 static const char *see_logname(void) { return serviceuser; }
141 static const char *see_home(void) { return serviceuser_dir; }
142 static const char *see_shell(void) { return serviceuser_shell; }
143
144 static const char *see_service(void) { return service; }
145 static const char *see_c_cwd(void) { return cwd; }
146 static const char *see_c_logname(void) { return logname; }
147 static const char *see_c_uid(void) {
148   static char buf[CHAR_BIT*sizeof(uid_t)/3+4];
149   snyprintf(buf,sizeof(buf),"%lu",(unsigned long)request_mbuf.callinguid);
150   return buf;
151 }
152
153 static const char *see_c_list(int n, const char *(*fn)(int i)) {
154   int l, i;
155   char *r;
156   
157   for (i=0, l=1; i<n; i++) l+= strlen(fn(i))+1;
158   r= xmalloc(l); r[l-1]= '*';
159   for (i=0, *r=0; i<n; i++) snytprintfcat(r,l,"%s ",fn(i));
160   assert(!r[l-1] && r[l-2]==' ');
161   r[l-2]= 0;
162   return r;
163 }
164
165 static const char *seei_group(int i) {
166   return calling_groups[i];
167 }
168 static const char *see_c_group(void) {
169   return see_c_list(request_mbuf.ngids,seei_group);
170 }
171
172 static const char *seei_gid(int i) {
173   static char buf[CHAR_BIT*sizeof(gid_t)/3+4];
174   
175   snyprintf(buf,sizeof(buf),"%d",calling_gids[i]);
176   return buf;
177 }
178 static const char *see_c_gid(void) {
179   return see_c_list(request_mbuf.ngids,seei_gid);
180 }
181
182 static const struct serv_envinfo {
183   const char *name;
184   const char *(*fn)(void);
185 } serv_envinfos[]= {
186   { "USER",           see_logname    },
187   { "LOGNAME",        see_logname    },
188   { "HOME",           see_home       },
189   { "SHELL",          see_shell      },
190   { "PATH",           defaultpath    },
191   { "USERV_SERVICE",  see_service    },
192   { "USERV_CWD",      see_c_cwd      },
193   { "USERV_USER",     see_c_logname  },
194   { "USERV_UID",      see_c_uid      },
195   { "USERV_GROUP",    see_c_group    },
196   { "USERV_GID",      see_c_gid      },
197   {  0                               }
198 };
199
200 void execservice(const int synchsocket[], int clientfd) {
201   static const char *const setenvpfargs[]= {
202     "/bin/sh",
203     "-c",
204     ". " SETENVIRONMENTPATH "; exec \"$@\"",
205     "-",
206     0
207   };
208   int fd, realfd, holdfd, newfd, r, envvarbufsize=0, targ, nargs, i, l;
209   char *envvarbuf=0;
210   const char **args, *const *cpp;
211   char *const *pp;
212   char synchmsg;
213   const struct serv_envinfo *sei;
214
215   if (dup2(fdarray[2].realfd,2)<0) {
216     static const char duperrmsg[]= "uservd(service): cannot dup2 for stderr\n";
217     write(fdarray[2].realfd,duperrmsg,sizeof(duperrmsg)-1);
218     _exit(-1);
219   }
220   serv_resetsignal(SIGPIPE);
221   serv_resetsignal(SIGCHLD);
222
223   if (close(synchsocket[0])) serv_syscallfail("close parent synch socket");
224
225   if (setpgid(0,0)) serv_syscallfail("set process group");
226   synchmsg= 'y';
227   r= write(synchsocket[1],&synchmsg,1);
228   if (r!=1) serv_syscallfail("write synch byte to parent");
229   r= synchread(synchsocket[1],'g');
230   if (r) serv_syscallfail("reach synch byte from parent");
231
232   if (close(clientfd)) serv_syscallfail("close client socket fd");
233
234   /* Now we have to make all the fd's work.  It's rather a complicated
235    * algorithm, unfortunately.  We remember in holdfd[fd] whether fd
236    * is being used to hold a file descriptor we actually want for some
237    * other real fd in the service program; holdfd[fd] contains the fd
238    * we eventually want fd to be dup'd into, so that realfd[holdfd[fd]]==fd.
239    * After setting up the holdfds we go through the fds in order of
240    * eventual fd making sure that fd is the one we want it to be.  If the
241    * holdfd tells us we're currently storing some other fd in there we
242    * move it out of the way with dup and record its new location.
243    */
244   for (fd=0; fd<fdarrayused; fd++) {
245     if (fdarray[fd].holdfd == -1) continue;
246     if (close(fdarray[fd].holdfd)) serv_syscallfail("close pipe hold fd");
247     fdarray[fd].holdfd= -1;
248   }
249   for (fd=0; fd<fdarrayused; fd++) {
250     if (fdarray[fd].realfd < fdarrayused) fdarray[fdarray[fd].realfd].holdfd= fd;
251   }
252   for (fd=0; fd<fdarrayused; fd++) {
253     realfd= fdarray[fd].realfd;
254     if (realfd == -1) continue;
255     holdfd= fdarray[fd].holdfd;
256     if (holdfd == fd) {
257       assert(realfd == fd);
258       fdarray[fd].holdfd= -1;
259       continue;
260     } else if (holdfd != -1) {
261       assert(fdarray[holdfd].realfd == fd);
262       newfd= dup(fd); if (newfd<0) serv_syscallfail("dup out of the way");
263       fdarray[holdfd].realfd= newfd;
264       if (newfd<fdarrayused) fdarray[newfd].holdfd= holdfd;
265       fdarray[fd].holdfd= -1;
266     }
267     if (dup2(fdarray[fd].realfd,fd)<0) serv_syscallfail("dup2 set up fd");
268     if (close(fdarray[fd].realfd)) serv_syscallfail("close old fd");
269     if (fcntl(fd,F_SETFD,0)<0) serv_syscallfail("set no-close-on-exec on fd");
270     fdarray[fd].realfd= fd;
271   }
272
273   for (sei= serv_envinfos; sei->name; sei++)
274     if (setenv(sei->name,sei->fn(),1)) serv_syscallfail("setenv standard");
275   for (i=0; i<request_mbuf.nvars; i++) {
276     l= strlen(defvararray[i].key)+9;
277     if (l>envvarbufsize) { envvarbufsize= l; envvarbuf= xrealloc(envvarbuf,l); }
278     snyprintf(envvarbuf,l,"USERV_U_%s",defvararray[i].key);
279     if (setenv(envvarbuf,defvararray[i].value,1)) serv_syscallfail("setenv defvar");
280   }
281
282   nargs= 0;
283   if (setenvironment) for (cpp= setenvpfargs; *cpp; cpp++) nargs++;
284   nargs++;
285   if (execargs) for (pp= execargs; *pp; pp++) nargs++;
286   if (!suppressargs) nargs+= request_mbuf.nargs;
287   args= xmalloc(sizeof(char*)*(nargs+1));
288   targ= 0;
289   if (setenvironment) for (cpp= setenvpfargs; *cpp; cpp++) args[targ++]= *cpp;
290   args[targ++]= execpath;
291   if (execargs) for (pp= execargs; *pp; pp++) args[targ++]= *pp;
292   if (!suppressargs) for (i=0; i<request_mbuf.nargs; i++) args[targ++]= argarray[i];
293   args[targ]= 0;
294
295   if (execbuiltin)
296     execbuiltin(args);
297   else
298     execv(args[0],(char* const*)args);
299
300   serv_syscallfail("exec service program");
301   _exit(-1);
302 }