chiark / gitweb /
Internal review up to end of p11.
[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
38 static void NONRETURNING serv_syscallfail(const char *msg) {
39   fputs("uservd(service): ",stderr);
40   perror(msg);
41   _exit(-1);
42 }
43
44 static void serv_resetsignal(int signo) {
45   struct sigaction sig;
46   
47   sig.sa_handler= SIG_DFL;
48   sigemptyset(&sig.sa_mask);
49   sig.sa_flags= 0;
50   if (sigaction(signo,&sig,0)) serv_syscallfail("reset signal handler");
51 }
52
53 static const char *see_logname(void) { return serviceuser; }
54 static const char *see_home(void) { return serviceuser_dir; }
55 static const char *see_shell(void) { return serviceuser_shell; }
56
57 static const char *see_path(void) {
58   return serviceuser_uid ? DEFAULTPATH_USER : DEFAULTPATH_ROOT;
59 }
60
61 static const char *see_service(void) { return service; }
62 static const char *see_c_cwd(void) { return cwd; }
63 static const char *see_c_logname(void) { return logname; }
64 static const char *see_c_uid(void) {
65   static char buf[CHAR_BIT*sizeof(uid_t)/3+4];
66   snyprintf(buf,sizeof(buf),"%lu",(unsigned long)request_mbuf.callinguid);
67   return buf;
68 }
69
70 static const char *see_c_list(int n, const char *(*fn)(int i)) {
71   int l, i;
72   char *r;
73   
74   for (i=0, l=1; i<n; i++) l+= strlen(fn(i))+1;
75   r= xmalloc(l); r[l-1]= '*';
76   for (i=0, *r=0; i<n; i++) snytprintfcat(r,l,"%s ",fn(i));
77   assert(!r[l-1] && r[l-2]==' ');
78   r[l-2]= 0;
79   return r;
80 }
81
82 static const char *seei_group(int i) {
83   return calling_groups[i];
84 }
85 static const char *see_c_group(void) {
86   return see_c_list(request_mbuf.ngids,seei_group);
87 }
88
89 static const char *seei_gid(int i) {
90   static char buf[CHAR_BIT*sizeof(gid_t)/3+4];
91   
92   snyprintf(buf,sizeof(buf),"%d",calling_gids[i]);
93   return buf;
94 }
95 static const char *see_c_gid(void) {
96   return see_c_list(request_mbuf.ngids,seei_gid);
97 }
98
99 static const struct serv_envinfo {
100   const char *name;
101   const char *(*fn)(void);
102 } serv_envinfos[]= {
103   { "USER",           see_logname    },
104   { "LOGNAME",        see_logname    },
105   { "HOME",           see_home       },
106   { "SHELL",          see_shell      },
107   { "PATH",           see_path       },
108   { "USERV_SERVICE",  see_service    },
109   { "USERV_CWD",      see_c_cwd      },
110   { "USERV_USER",     see_c_logname  },
111   { "USERV_UID",      see_c_uid      },
112   { "USERV_GROUP",    see_c_group    },
113   { "USERV_GID",      see_c_gid      },
114   {  0                               }
115 };
116
117 void execservice(const int synchsocket[], int clientfd) {
118   static const char *const setenvpfargs[]= {
119     "/bin/sh",
120     "-c",
121     ". " SETENVIRONMENTPATH "; exec \"$@\"",
122     "-",
123     0
124   };
125   int fd, realfd, holdfd, newfd, r, envvarbufsize=0, targ, nargs, i, l;
126   char *envvarbuf=0;
127   const char **args, *const *cpp;
128   char *const *pp;
129   char synchmsg;
130   const struct serv_envinfo *sei;
131
132   if (dup2(fdarray[2].realfd,2)<0) {
133     static const char duperrmsg[]= "uservd(service): cannot dup2 for stderr\n";
134     write(fdarray[2].realfd,duperrmsg,sizeof(duperrmsg)-1);
135     _exit(-1);
136   }
137   serv_resetsignal(SIGPIPE);
138   serv_resetsignal(SIGCHLD);
139
140   if (close(synchsocket[0])) serv_syscallfail("close parent synch socket");
141
142   if (setpgid(0,0)) serv_syscallfail("set process group");
143   synchmsg= 'y';
144   r= write(synchsocket[1],&synchmsg,1);
145   if (r!=1) serv_syscallfail("write synch byte to parent");
146   r= synchread(synchsocket[1],'g');
147   if (r) serv_syscallfail("reach synch byte from parent");
148
149   if (close(clientfd)) serv_syscallfail("close client socket fd");
150
151   /* Now we have to make all the fd's work.  It's rather a complicated
152    * algorithm, unfortunately.  We remember in holdfd[fd] whether fd
153    * is being used to hold a file descriptor we actually want for some
154    * other real fd in the service program; holdfd[fd] contains the fd
155    * we eventually want fd to be dup'd into, so that realfd[holdfd[fd]]==fd.
156    * After setting up the holdfds we go through the fds in order of
157    * eventual fd making sure that fd is the one we want it to be.  If the
158    * holdfd tells us we're currently storing some other fd in there we
159    * move it out of the way with dup and record its new location.
160    */
161   for (fd=0; fd<fdarrayused; fd++) {
162     if (fdarray[fd].holdfd == -1) continue;
163     if (close(fdarray[fd].holdfd)) serv_syscallfail("close pipe hold fd");
164     fdarray[fd].holdfd= -1;
165   }
166   for (fd=0; fd<fdarrayused; fd++) {
167     if (fdarray[fd].realfd < fdarrayused) fdarray[fdarray[fd].realfd].holdfd= fd;
168   }
169   for (fd=0; fd<fdarrayused; fd++) {
170     realfd= fdarray[fd].realfd;
171     if (realfd == -1) continue;
172     holdfd= fdarray[fd].holdfd;
173     if (holdfd == fd) {
174       assert(realfd == fd);
175       fdarray[fd].holdfd= -1;
176       continue;
177     } else if (holdfd != -1) {
178       assert(fdarray[holdfd].realfd == fd);
179       newfd= dup(fd); if (newfd<0) serv_syscallfail("dup out of the way");
180       fdarray[holdfd].realfd= newfd;
181       if (newfd<fdarrayused) fdarray[newfd].holdfd= holdfd;
182       fdarray[fd].holdfd= -1;
183     }
184     if (dup2(fdarray[fd].realfd,fd)<0) serv_syscallfail("dup2 set up fd");
185     if (close(fdarray[fd].realfd)) serv_syscallfail("close old fd");
186     if (fcntl(fd,F_SETFD,0)<0) serv_syscallfail("set no-close-on-exec on fd");
187     fdarray[fd].realfd= fd;
188   }
189
190   for (sei= serv_envinfos; sei->name; sei++)
191     if (setenv(sei->name,sei->fn(),1)) serv_syscallfail("setenv standard");
192   for (i=0; i<request_mbuf.nvars; i++) {
193     l= strlen(defvararray[i][0])+9;
194     if (l>envvarbufsize) { envvarbufsize= l; envvarbuf= xrealloc(envvarbuf,l); }
195     snyprintf(envvarbuf,l,"USERV_U_%s",defvararray[i][0]);
196     if (setenv(envvarbuf,defvararray[i][1],1)) serv_syscallfail("setenv defvar");
197   }
198
199   nargs= 0;
200   if (setenvironment) for (cpp= setenvpfargs; *cpp; cpp++) nargs++;
201   nargs++;
202   if (execargs) for (pp= execargs; *pp; pp++) nargs++;
203   if (!suppressargs) nargs+= request_mbuf.nargs;
204   args= xmalloc(sizeof(char*)*(nargs+1));
205   targ= 0;
206   if (setenvironment) for (cpp= setenvpfargs; *cpp; cpp++) args[targ++]= *cpp;
207   args[targ++]= execpath;
208   if (execargs) for (pp= execargs; *pp; pp++) args[targ++]= *pp;
209   if (!suppressargs) for (i=0; i<request_mbuf.nargs; i++) args[targ++]= argarray[i];
210   args[targ]= 0;
211
212   execv(args[0],(char* const*)args);
213
214   serv_syscallfail("exec service program");
215   _exit(-1);
216 }