chiark / gitweb /
abd36089ad5296b8c1692a2a413418d6f5c88618
[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"
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;
93   
94   assert(*string);
95   nspaces= 0;
96   while ((c= *string++)) {
97     switch (c) {
98     case ' ': nspaces++; break;
99     case '\n': nspaces= 0; putchar('\n'); break;
100     default:
101       while (nspaces>0) { putchar(' '); nspaces--; }
102       putchar(c);
103     }
104   }
105   assert(*--string == '\n');
106   serv_checkstdoutexit();
107 }
108
109 void bisexec_toplevel(const char *const *argv) {
110   dumpconfig(TOPLEVEL_CONFIGURATION);
111 }
112
113 void bisexec_override(const char *const *argv) {
114   dumpconfig(TOPLEVEL_OVERRIDDEN_CONFIGURATION);
115 }
116
117 void bisexec_reset(const char *const *argv) {
118   dumpconfig(RESET_CONFIGURATION);
119 }
120
121 void bisexec_execute(const char *const *argv) {
122   always_dumpexecsettings();
123   serv_checkstdoutexit();
124 }
125
126 static void serv_resetsignal(int signo) {
127   struct sigaction sig;
128   
129   sig.sa_handler= SIG_DFL;
130   sigemptyset(&sig.sa_mask);
131   sig.sa_flags= 0;
132   if (sigaction(signo,&sig,0)) serv_syscallfail("reset signal handler");
133 }
134
135 static const char *see_logname(void) { return serviceuser; }
136 static const char *see_home(void) { return serviceuser_dir; }
137 static const char *see_shell(void) { return serviceuser_shell; }
138
139 static const char *see_service(void) { return service; }
140 static const char *see_c_cwd(void) { return cwd; }
141 static const char *see_c_logname(void) { return logname; }
142 static const char *see_c_uid(void) {
143   static char buf[CHAR_BIT*sizeof(uid_t)/3+4];
144   snyprintf(buf,sizeof(buf),"%lu",(unsigned long)request_mbuf.callinguid);
145   return buf;
146 }
147
148 static const char *see_c_list(int n, const char *(*fn)(int i)) {
149   int l, i;
150   char *r;
151   
152   for (i=0, l=1; i<n; i++) l+= strlen(fn(i))+1;
153   r= xmalloc(l); r[l-1]= '*';
154   for (i=0, *r=0; i<n; i++) snytprintfcat(r,l,"%s ",fn(i));
155   assert(!r[l-1] && r[l-2]==' ');
156   r[l-2]= 0;
157   return r;
158 }
159
160 static const char *seei_group(int i) {
161   return calling_groups[i];
162 }
163 static const char *see_c_group(void) {
164   return see_c_list(request_mbuf.ngids,seei_group);
165 }
166
167 static const char *seei_gid(int i) {
168   static char buf[CHAR_BIT*sizeof(gid_t)/3+4];
169   
170   snyprintf(buf,sizeof(buf),"%d",calling_gids[i]);
171   return buf;
172 }
173 static const char *see_c_gid(void) {
174   return see_c_list(request_mbuf.ngids,seei_gid);
175 }
176
177 static const struct serv_envinfo {
178   const char *name;
179   const char *(*fn)(void);
180 } serv_envinfos[]= {
181   { "USER",           see_logname    },
182   { "LOGNAME",        see_logname    },
183   { "HOME",           see_home       },
184   { "SHELL",          see_shell      },
185   { "PATH",           defaultpath    },
186   { "USERV_SERVICE",  see_service    },
187   { "USERV_CWD",      see_c_cwd      },
188   { "USERV_USER",     see_c_logname  },
189   { "USERV_UID",      see_c_uid      },
190   { "USERV_GROUP",    see_c_group    },
191   { "USERV_GID",      see_c_gid      },
192   {  0                               }
193 };
194
195 void execservice(const int synchsocket[], int clientfd) {
196   static const char *const setenvpfargs[]= {
197     "/bin/sh",
198     "-c",
199     ". " SETENVIRONMENTPATH "; exec \"$@\"",
200     "-",
201     0
202   };
203   int fd, realfd, holdfd, newfd, r, envvarbufsize=0, targ, nargs, i, l;
204   char *envvarbuf=0;
205   const char **args, *const *cpp;
206   char *const *pp;
207   char synchmsg;
208   const struct serv_envinfo *sei;
209
210   if (dup2(fdarray[2].realfd,2)<0) {
211     static const char duperrmsg[]= "uservd(service): cannot dup2 for stderr\n";
212     write(fdarray[2].realfd,duperrmsg,sizeof(duperrmsg)-1);
213     _exit(-1);
214   }
215   serv_resetsignal(SIGPIPE);
216   serv_resetsignal(SIGCHLD);
217
218   if (close(synchsocket[0])) serv_syscallfail("close parent synch socket");
219
220   if (setpgid(0,0)) serv_syscallfail("set process group");
221   synchmsg= 'y';
222   r= write(synchsocket[1],&synchmsg,1);
223   if (r!=1) serv_syscallfail("write synch byte to parent");
224   r= synchread(synchsocket[1],'g');
225   if (r) serv_syscallfail("reach synch byte from parent");
226
227   if (close(clientfd)) serv_syscallfail("close client socket fd");
228
229   /* Now we have to make all the fd's work.  It's rather a complicated
230    * algorithm, unfortunately.  We remember in holdfd[fd] whether fd
231    * is being used to hold a file descriptor we actually want for some
232    * other real fd in the service program; holdfd[fd] contains the fd
233    * we eventually want fd to be dup'd into, so that realfd[holdfd[fd]]==fd.
234    * After setting up the holdfds we go through the fds in order of
235    * eventual fd making sure that fd is the one we want it to be.  If the
236    * holdfd tells us we're currently storing some other fd in there we
237    * move it out of the way with dup and record its new location.
238    */
239   for (fd=0; fd<fdarrayused; fd++) {
240     if (fdarray[fd].holdfd == -1) continue;
241     if (close(fdarray[fd].holdfd)) serv_syscallfail("close pipe hold fd");
242     fdarray[fd].holdfd= -1;
243   }
244   for (fd=0; fd<fdarrayused; fd++) {
245     if (fdarray[fd].realfd < fdarrayused) fdarray[fdarray[fd].realfd].holdfd= fd;
246   }
247   for (fd=0; fd<fdarrayused; fd++) {
248     realfd= fdarray[fd].realfd;
249     if (realfd == -1) continue;
250     holdfd= fdarray[fd].holdfd;
251     if (holdfd == fd) {
252       assert(realfd == fd);
253       fdarray[fd].holdfd= -1;
254       continue;
255     } else if (holdfd != -1) {
256       assert(fdarray[holdfd].realfd == fd);
257       newfd= dup(fd); if (newfd<0) serv_syscallfail("dup out of the way");
258       fdarray[holdfd].realfd= newfd;
259       if (newfd<fdarrayused) fdarray[newfd].holdfd= holdfd;
260       fdarray[fd].holdfd= -1;
261     }
262     if (dup2(fdarray[fd].realfd,fd)<0) serv_syscallfail("dup2 set up fd");
263     if (close(fdarray[fd].realfd)) serv_syscallfail("close old fd");
264     if (fcntl(fd,F_SETFD,0)<0) serv_syscallfail("set no-close-on-exec on fd");
265     fdarray[fd].realfd= fd;
266   }
267
268   for (sei= serv_envinfos; sei->name; sei++)
269     if (setenv(sei->name,sei->fn(),1)) serv_syscallfail("setenv standard");
270   for (i=0; i<request_mbuf.nvars; i++) {
271     l= strlen(defvararray[i].key)+9;
272     if (l>envvarbufsize) { envvarbufsize= l; envvarbuf= xrealloc(envvarbuf,l); }
273     snyprintf(envvarbuf,l,"USERV_U_%s",defvararray[i].key);
274     if (setenv(envvarbuf,defvararray[i].value,1)) serv_syscallfail("setenv defvar");
275   }
276
277   nargs= 0;
278   if (setenvironment) for (cpp= setenvpfargs; *cpp; cpp++) nargs++;
279   nargs++;
280   if (execargs) for (pp= execargs; *pp; pp++) nargs++;
281   if (!suppressargs) nargs+= request_mbuf.nargs;
282   args= xmalloc(sizeof(char*)*(nargs+1));
283   targ= 0;
284   if (setenvironment) for (cpp= setenvpfargs; *cpp; cpp++) args[targ++]= *cpp;
285   args[targ++]= execpath;
286   if (execargs) for (pp= execargs; *pp; pp++) args[targ++]= *pp;
287   if (!suppressargs) for (i=0; i<request_mbuf.nargs; i++) args[targ++]= argarray[i];
288   args[targ]= 0;
289
290   if (execbuiltin)
291     execbuiltin(args);
292   else
293     execv(args[0],(char* const*)args);
294
295   serv_syscallfail("exec service program");
296   _exit(-1);
297 }