5 * Copyright (C) 1998-1999,2003 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-utils; if not, write to the Free Software
19 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include <sys/types.h>
33 static const char *const default_envok[] = {
59 static void addarg(struct buildargs *args, const char *a) {
60 if (args->n > args->max) error("too many arguments");
61 args->v[args->n++]= a;
64 static void add_userv_var(const char *fulln,
65 const char *en, const char *ev, void *p) {
66 struct buildargs *args= p;
70 l= strlen(ev); if (l > MAX_ENVVAR_VALUE) error("environment variable too long");
71 a= xmalloc(strlen(en)+l+6);
72 sprintf(a,"-DE_%s=%s",en,ev);
76 int main(int argc, const char **argv) {
78 const char *slash2, *pathi, *ev, *av;
79 const char *const *envok;
80 size_t usernamelen, l;
81 struct buildargs args;
86 if (l>6 && !strcmp(argv[0]+l-6,"-debug")) debugmode= 1;
89 if (fputs("Content-Type: text/plain\n\n",stdout)==EOF || fflush(stdout))
90 syserror("write stdout");
91 if (dup2(1,2)<0) { perror("dup stdout to stderr"); exit(-1); }
92 D( printf(";;; UCGI\n"); )
95 if (argc > MAX_ARGS) error("too many arguments");
97 ev= getenv("UCGI_ENV_FILTER");
99 envok= load_filters(LOADF_MUST, ev, LF_END);
101 envok= load_filters(0, "/etc/userv/ucgi.env-filter", LF_END);
102 if (!envok) envok= default_envok;
105 pathi= getenv("PATH_INFO");
106 if (!pathi) error("PATH_INFO not found");
108 printf(";; find user name...\n"
109 ";; initial PATH_INFO = `%s'\n",
112 if (pathi[0] != '/' || pathi[1] != '~') error("PATH_INFO must start with /~");
113 slash2= strchr(pathi+2,'/'); if (!slash2) error("PATH_INFO must have more than one /");
114 usernamelen= slash2-(pathi+2);
115 if (usernamelen > MAX_USERNAME_LEN) error("PATH_INFO username too long");
116 username= xmalloc(usernamelen+1);
117 memcpy(username,pathi+2,usernamelen); username[usernamelen]= 0;
119 printf(";; user = `%s'; tail = `%s'\n", username, slash2); )
120 if (!isalpha(username[0])) error("username 1st character is not alphabetic");
121 xsetenv("PATH_INFO",slash2,1);
123 args.n= 0; args.max= argc + MAX_ENVVARS + 10;
124 args.v= xmalloc(args.max * sizeof(*args.v));
126 addarg(&args, "userv");
127 if (debugmode) addarg(&args, "-DDEBUG=1");
129 filter_environment(FILTF_WILDCARD, "", envok, add_userv_var, &args);
131 addarg(&args, username);
132 addarg(&args, "www-cgi");
133 while ((av= (*++argv))) addarg(&args, av);
138 child= fork(); if (child==-1) syserror("fork");
140 rchild= waitpid(child,&status,0);
141 if (rchild==-1) syserror("waitpid");
142 printf("\nexit status %d %d\n",(status>>8)&0x0ff,status&0x0ff);
150 printf(";; final command line...\n");
151 for (i = 0; args.v[i]; i++)
152 printf(";; %s\n", args.v[i]);
156 execvp("userv",(char*const*)args.v);
157 syserror("exec userv");