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