3 * routines which are different for -DDEBUG
5 * Copyright (C)1996-1997 Ian Jackson
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.
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.
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.
29 #include <sys/types.h>
39 static const char *sl_ident= "UNSET";
40 static int sl_option=0, sl_facility=0;
42 void openlog(const char *ident, int option, int facility) {
45 sl_facility= facility;
48 void syslog(int priority, const char *fmt, ...) {
50 fprintf(stderr,"syslog: %s<%d.%d>(%d): ",sl_ident,sl_facility,priority,sl_option);
52 vfprintf(stderr,fmt,al);
59 sl_option= sl_facility= 0;
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:"");
68 static void fdwantdumprw(const char *whichstr, int whichval,
69 int rw, const char *rwstr) {
73 for (fd=0; fd<fdarrayused; fd++) {
74 if (!(fdarray[fd].wantstate == whichval && fdarray[fd].wantrw == rw)) continue;
75 fdwantdumprwhead(&donehead,whichstr,rwstr);
78 if (restfdwantstate == whichval && restfdwantrw == rw) {
79 fdwantdumprwhead(&donehead,whichstr,rwstr);
80 printf(" %d-",fdarrayused);
82 if (donehead) printf("\n");
85 static void fdwantdump(const char *whichstr, int whichval, const char *rwunspecstr) {
87 fdwantdumprw(whichstr,whichval,tokv_word_read,"read");
88 fdwantdumprw(whichstr,whichval,tokv_word_write,"write");
89 fdwantdumprw(whichstr,whichval,0,rwunspecstr);
91 fdwantdumprw(whichstr,whichval,0,0);
95 static void truefalsedump(const char *whichstr, int val) {
96 printf("%s: %s\n",whichstr,val?"yes":"no");
99 void debug_dumprequest(pid_t mypid) {
102 printf("server pid: %ld\n"
105 "service user: `%s'\n"
106 "calling user: `%s'\n"
107 "calling cwd: `%s'\n"
110 (long)mypid, (long)request_mbuf.clientpid,
111 service, serviceuser, logname, cwd,
112 (long)request_mbuf.callinguid);
113 for (i=0; i<request_mbuf.ngids; i++) printf(" %ld",(long)gidarray[i]);
115 for (fd=0; fd<fdarrayused; fd++)
116 if (fdarray[fd].iswrite != -1)
117 printf(" %d%s",fd,fdarray[fd].iswrite ? "w" : "r");
118 printf("\n" "arguments:");
119 for (i=0; i<request_mbuf.nargs; i++) printf(" `%s'",argarray[i]);
120 printf("\n" "variables:");
121 for (i=0; i<request_mbuf.nvars; i++)
122 printf(" `%s'=`%s'",defvararray[i][0],defvararray[i][1]);
124 if (getenv("USERVD_SLEEP")) sleep(atoi(getenv("USERVD_SLEEP")));
127 void debug_dumpexecsettings(void) {
128 printf("configuration parsed\n");
129 if (userrcfile) printf("user-rcfile: `%s'\n",userrcfile);
130 else printf("user-rcfile: <none>\n");
131 fdwantdump("required",tokv_word_requirefd,"ERROR");
132 fdwantdump("allowed",tokv_word_allowfd,"either");
133 fdwantdump("ignored",tokv_word_ignorefd,0);
134 fdwantdump("null",tokv_word_nullfd,"both");
135 fdwantdump("rejected",tokv_word_rejectfd,0);
138 case tokv_word_reject: printf("reject"); break;
139 case tokv_word_execute: printf("`%s'",execpath); break;
140 case tokv_word_executefromdirectory: printf("from directory, `%s'",execpath); break;
141 case tokv_word_executefrompath: printf("from path"); break;
145 truefalsedump("set-environment",setenvironment);
146 truefalsedump("suppress-args",suppressargs);
147 truefalsedump("disconnect-hup",disconnecthup);
148 truefalsedump("set-environment",setenvironment);
151 case tokv_word_errorstostderr: printf("stderr"); break;
152 case tokv_word_errorstofile: printf("file"); break;
153 case tokv_word_errorstosyslog: printf("syslog %d.%d",ehlogfacility,ehloglevel); break;
159 void debug_dumpparameter(const char *parm, char **values) {
160 printf("config parameter `%s':",parm);
161 while (*values) printf(" `%s'",*values++);
165 static int groupsallin(int na, const gid_t *lista,
166 int nb, const gid_t *listb) {
168 for (i=0; i<na; i++) {
169 for (j=0; j<nb && listb[j] != lista[i]; j++);
175 int setgroups(size_t wantsize, const gid_t *wantlist) {
179 realsize= getgroups(0,0); if (realsize == -1) return -1;
180 reallist= malloc(sizeof(gid_t)*realsize); if (!reallist) return -1;
181 if (getgroups(realsize,reallist) != realsize)
182 { e= errno; free(reallist); errno= e; return -1; }
183 if (!groupsallin(wantsize,wantlist,realsize,reallist))
184 { free(reallist); errno= EPERM; return -1; }
185 if (!groupsallin(realsize,reallist,wantsize,wantlist))
186 { free(reallist); errno= EINVAL; return -1; }
187 free(reallist); return 0;
190 pid_t nondebug_fork(void) { return 0; }
191 const char *nondebug_serviceuserdir(const char *ifnondebug) { return SERVICEUSERDIR; }
195 void debug_dumprequest(pid_t mypid) { }
196 void debug_dumpexecsettings(void) { }
197 void debug_dumpparameter(const char *parm, char **values) { }
198 pid_t nondebug_fork(void) { return fork(); }
199 const char *nondebug_serviceuserdir(const char *ifnondebug) { return ifnondebug; }