chiark / gitweb /
Internal review up to end of p11.
[userv.git] / debug.c
1 /*
2  * userv - ddebug.c
3  * routines which are different for -DDEBUG
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 <stdarg.h>
23 #include <syslog.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <grp.h>
29 #include <sys/types.h>
30
31 #include "config.h"
32 #include "common.h"
33 #include "daemon.h"
34 #include "lib.h"
35 #include "tokens.h"
36
37 #ifdef DEBUG
38
39 static const char *sl_ident= "UNSET";
40 static int sl_option=0, sl_facility=0;
41
42 void openlog(const char *ident, int option, int facility) {
43   sl_ident= ident;
44   sl_option= option;
45   sl_facility= facility;
46 }
47
48 void syslog(int priority, const char *fmt, ...) {
49   va_list al;
50   fprintf(stderr,"syslog: %s<%d.%d>(%d): ",sl_ident,sl_facility,priority,sl_option);
51   va_start(al,fmt);
52   vfprintf(stderr,fmt,al);
53   va_end(al);
54   fputc('\n',stderr);
55 }
56
57 void closelog(void) {
58   sl_ident= "CLOSED";
59   sl_option= sl_facility= 0;
60 }
61
62 static void fdwantdumprwhead(int *donehead, const char *whichstr, const char *rwstr) {
63   if (*donehead) return;
64   printf("fds %s%s%s:",whichstr,rwstr?" ":"",rwstr?rwstr:"");
65   *donehead= 1;
66 }
67
68 static void fdwantdumprw(const char *whichstr, int whichval,
69                          int rw, const char *rwstr) {
70   int donehead= 0;
71   int fd;
72   
73   for (fd=0; fd<fdarrayused; fd++) {
74     if (!(fdarray[fd].wantstate == whichval && fdarray[fd].wantrw == rw)) continue;
75     fdwantdumprwhead(&donehead,whichstr,rwstr);
76     printf(" %d",fd);
77   }
78   if (restfdwantstate == whichval && restfdwantrw == rw) {
79     fdwantdumprwhead(&donehead,whichstr,rwstr);
80     printf(" %d-",fdarrayused);
81   }
82   if (donehead) printf("\n");
83 }
84
85 static void fdwantdump(const char *whichstr, int whichval, const char *rwunspecstr) {
86   if (rwunspecstr) {
87     fdwantdumprw(whichstr,whichval,tokv_word_read,"read");
88     fdwantdumprw(whichstr,whichval,tokv_word_write,"write");
89     fdwantdumprw(whichstr,whichval,0,rwunspecstr);
90   } else {
91     fdwantdumprw(whichstr,whichval,0,0);
92   }
93 }
94
95 static void truefalsedump(const char *whichstr, int val) {
96   printf("%s: %s\n",whichstr,val?"yes":"no");
97 }
98
99 static void groupsdump(int ngids, const gid_t *gids, const char *const *groups) {
100   int i;
101   
102   for (i=0; i<ngids; i++) printf(" %ld(%s)",(long)gids[i],groups[i]);
103 }
104
105 void debug_dumprequest(pid_t mypid) {
106   int i, fd;
107   
108   printf("server pid: %ld\n"
109          "client pid: %ld\n"
110          "service: `%s'\n"
111          "service user: `%s'\n"
112          "service uid: %ld\n"
113          "service user shell: `%s'\n"
114          "service user dir: `%s'\n"
115          "service groups:",
116          (long)mypid, (long)request_mbuf.clientpid,
117          service, serviceuser, (long)serviceuser_uid,
118          serviceuser_shell, serviceuser_dir);
119   groupsdump(service_ngids,service_gids,service_groups);
120   printf("\n"
121          "calling user: `%s'\n"
122          "calling uid: %ld\n"
123          "calling user shell: `%s'\n"
124          "calling groups:",
125          logname, (long)request_mbuf.callinguid,
126          callinguser_shell);
127   groupsdump(request_mbuf.ngids,calling_gids,calling_groups);
128   printf("\n"
129          "calling cwd: `%s'\n"
130          "fds:",
131          cwd);
132   for (fd=0; fd<fdarrayused; fd++)
133     if (fdarray[fd].iswrite != -1)
134       printf(" %d%s",fd,fdarray[fd].iswrite ? "w" : "r");
135   printf("\n" "arguments:");
136   for (i=0; i<request_mbuf.nargs; i++) printf(" `%s'",argarray[i]);
137   printf("\n" "variables:");
138   for (i=0; i<request_mbuf.nvars; i++)
139     printf(" `%s'=`%s'",defvararray[i][0],defvararray[i][1]);
140   printf("\n");
141   if (getenv("USERVD_SLEEP")) sleep(atoi(getenv("USERVD_SLEEP")));
142 }
143
144 void debug_dumpexecsettings(void) {
145   printf("configuration parsed\n");
146   if (userrcfile) printf("user-rcfile: `%s'\n",userrcfile);
147   else printf("user-rcfile: <none>\n");
148   fdwantdump("required",tokv_word_requirefd,"ERROR");
149   fdwantdump("allowed",tokv_word_allowfd,"either");
150   fdwantdump("ignored",tokv_word_ignorefd,0);
151   fdwantdump("null",tokv_word_nullfd,"both");
152   fdwantdump("rejected",tokv_word_rejectfd,0);
153   printf("execute: ");
154   switch (execute) {
155   case tokv_word_reject: printf("reject"); break;
156   case tokv_word_execute: printf("`%s'",execpath); break;
157   case tokv_word_executefromdirectory: printf("from directory, `%s'",execpath); break;
158   case tokv_word_executefrompath: printf("from path"); break;
159   default: abort();
160   }
161   printf("\n");
162   truefalsedump("set-environment",setenvironment);
163   truefalsedump("suppress-args",suppressargs);
164   truefalsedump("disconnect-hup",disconnecthup);
165   truefalsedump("set-environment",setenvironment);
166 }
167
168 void debug_dumpparameter(const char *parm, char **values) {
169   printf("config parameter `%s':",parm);
170   while (*values) printf(" `%s'",*values++);
171   printf("\n");
172 }
173
174 static int groupsallin(int na, const gid_t *lista,
175                        int nb, const gid_t *listb) {
176   int i,j;
177   for (i=0; i<na; i++) {
178     for (j=0; j<nb && listb[j] != lista[i]; j++);
179     if (j>=nb) return 0;
180   }
181   return 1;
182 }
183
184 int setgroups(size_t wantsize, const gid_t *wantlist) {
185   /* This is a bit of a hack really.  What we want when we're in debug mode is to
186    * have initgroups() be a no-op iff the groups are already set right (so that
187    * we notice if we're trying to change to the wrong user) but to fail if they're
188    * not.
189    *
190    * We can't just call initgroups() because it unconditionally calls
191    * setgroups, which always fails for non-root even if the two group
192    * lists are the same.  So here we have a faked-up setgroups which
193    * uses getgroups to see what the group list is and `succeeds' if
194    * the actual group list and the desired one have the same set of
195    * groups, and fails with EPERM if the real setgroups would have
196    * added group(s) or otherwise EINVAL if it would have removed some.
197    *
198    * The usual magic with dynamic linking makes the libc initgroups(3) call
199    * pick up our setgroups() rather than the real setgroups(2).
200    */
201   int realsize, e;
202   gid_t *reallist;
203
204   realsize= getgroups(0,0); if (realsize == -1) return -1;
205   reallist= malloc(sizeof(gid_t)*realsize); if (!reallist) return -1;
206   if (getgroups(realsize,reallist) != realsize)
207     { e= errno; free(reallist); errno= e; return -1; }
208   if (!groupsallin(wantsize,wantlist,realsize,reallist))
209     { free(reallist); errno= EPERM; return -1; }
210   if (!groupsallin(realsize,reallist,wantsize,wantlist))
211     { free(reallist); errno= EINVAL; return -1; }
212   free(reallist); return 0;
213 }
214
215 pid_t nondebug_fork(void) { return 0; }
216 const char *nondebug_serviceuserdir(const char *ifnondebug) { return SERVICEUSERDIR; }
217
218 #else
219
220 void debug_dumprequest(pid_t mypid) { }
221 void debug_dumpexecsettings(void) { }
222 void debug_dumpparameter(const char *parm, char **values) { }
223 pid_t nondebug_fork(void) { return fork(); }
224 const char *nondebug_serviceuserdir(const char *ifnondebug) { return ifnondebug; }
225
226 #endif