5 * Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
8 * Chancellor Masters and Scholars of the University of Cambridge
9 * Copyright 2010 Tony Finch <fanf@dotat.at>
11 * This is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with userv-utils; if not, see http://www.gnu.org/licenses/.
29 #include <sys/types.h>
34 static const char *const default_envok[] = {
60 static void addarg(struct buildargs *args, const char *a) {
61 if (args->n > args->max) error("too many arguments", 500);
62 args->v[args->n++]= a;
65 static void add_userv_var(const char *fulln,
66 const char *en, const char *ev, void *p) {
67 struct buildargs *args= p;
72 if (l > MAX_ENVVAR_VALUE) error("environment variable too long", 500);
73 a= xmalloc(strlen(en)+l+6);
74 sprintf(a,"-DE_%s=%s",en,ev);
78 int main(int argc, const char **argv) {
80 const char *slash2, *pathi, *ev, *av;
81 const char *const *envok = 0;
82 size_t usernamelen, l;
83 struct buildargs args;
88 if (l>6 && !strcmp(argv[0]+l-6,"-debug")) debugmode= 1;
91 if (fputs("Content-Type: text/plain\n\n",stdout)==EOF || fflush(stdout))
92 syserror("write stdout");
93 if (dup2(1,2)<0) { perror("dup stdout to stderr"); exit(-1); }
94 D( printf(";;; UCGI\n"); )
97 if (argc > MAX_ARGS) error("too many arguments", 500);
99 ev= getenv("UCGI_ENV_FILTER");
101 envok= load_filters(LOADF_MUST, ev, LF_END);
103 envok= load_filters(0, "/etc/userv/ucgi.env-filter", LF_END);
105 pathi= getenv("PATH_INFO");
106 if (!pathi) error("PATH_INFO not found", 500);
108 printf(";; find user name...\n"
109 ";; initial PATH_INFO = `%s'\n",
112 if (pathi[0] != '/' || pathi[1] != '~')
113 error("PATH_INFO must start with /~", 400);
114 slash2= strchr(pathi+2,'/');
115 if (!slash2) error("PATH_INFO must have more than one /", 400);
116 usernamelen= slash2-(pathi+2);
117 if (usernamelen > MAX_USERNAME_LEN) error("PATH_INFO username too long", 400);
118 username= xmalloc(usernamelen+1);
119 memcpy(username,pathi+2,usernamelen); username[usernamelen]= 0;
121 printf(";; user = `%s'; tail = `%s'\n", username, slash2); )
122 if (!isalpha(username[0]))
123 error("username 1st character is not alphabetic", 400);
124 xsetenv("PATH_INFO",slash2,1);
126 args.n= 0; args.max= argc + MAX_ENVVARS + 10;
127 args.v= xmalloc(args.max * sizeof(*args.v));
129 addarg(&args, "userv");
130 if (debugmode) addarg(&args, "-DDEBUG=1");
132 filter_environment(FILTF_WILDCARD, "", envok, default_envok,
133 add_userv_var, &args);
135 addarg(&args, username);
136 addarg(&args, "www-cgi");
137 while ((av= (*++argv))) addarg(&args, av);
142 child= fork(); if (child==-1) syserror("fork");
144 rchild= waitpid(child,&status,0);
145 if (rchild==-1) syserror("waitpid");
146 printf("\nexit status %d %d\n",(status>>8)&0x0ff,status&0x0ff);
154 printf(";; final command line...\n");
155 for (i = 0; args.v[i]; i++)
156 printf(";; %s\n", args.v[i]);
160 execvp("userv",(char*const*)args.v);
161 syserror("exec userv");