5 * Copyright 1996-2013,2016 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>
10 * Copyright 2013,2016 Mark Wooding <mdw@distorted.org.uk>
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with userv-utils; if not, see http://www.gnu.org/licenses/.
30 #include <sys/types.h>
35 static const char *const default_envok
[] = {
61 static void addarg(struct buildargs
*args
, const char *a
) {
62 if (args
->n
> args
->max
) error("too many arguments", 500);
63 args
->v
[args
->n
++]= a
;
66 static void add_userv_var(const char *fulln
,
67 const char *en
, const char *ev
, void *p
) {
68 struct buildargs
*args
= p
;
73 if (l
> MAX_ENVVAR_VALUE
) error("environment variable too long", 500);
74 a
= xmalloc(strlen(en
)+l
+6);
75 sprintf(a
,"-DE_%s=%s",en
,ev
);
79 int main(int argc
, const char **argv
) {
81 const char *slash2
, *pathi
, *ev
, *av
;
82 const char *const *envok
= 0;
83 size_t usernamelen
, l
;
84 struct buildargs args
;
89 if (l
>6 && !strcmp(argv
[0]+l
-6,"-debug")) debugmode
= 1;
92 if (fputs("Content-Type: text/plain\n\n",stdout
)==EOF
|| fflush(stdout
))
93 syserror("write stdout");
94 if (dup2(1,2)<0) { perror("dup stdout to stderr"); exit(-1); }
95 D( printf(";;; UCGI\n"); )
98 if (argc
> MAX_ARGS
) error("too many arguments", 500);
100 ev
= getenv("UCGI_ENV_FILTER");
102 envok
= load_filters(LOADF_MUST
, ev
, LF_END
);
104 envok
= load_filters(0, "/etc/userv/ucgi.env-filter", LF_END
);
106 pathi
= getenv("PATH_INFO");
107 if (!pathi
) error("PATH_INFO not found", 500);
109 printf(";; find user name...\n"
110 ";; initial PATH_INFO = `%s'\n",
113 if (pathi
[0] != '/' || pathi
[1] != '~')
114 error("PATH_INFO must start with /~", 400);
115 slash2
= strchr(pathi
+2,'/');
116 if (!slash2
) error("PATH_INFO must have more than one /", 400);
117 usernamelen
= slash2
-(pathi
+2);
118 if (usernamelen
> MAX_USERNAME_LEN
) error("PATH_INFO username too long", 400);
119 username
= xmalloc(usernamelen
+1);
120 memcpy(username
,pathi
+2,usernamelen
); username
[usernamelen
]= 0;
122 printf(";; user = `%s'; tail = `%s'\n", username
, slash2
); )
123 if (!isalpha(username
[0]))
124 error("username 1st character is not alphabetic", 400);
125 xsetenv("PATH_INFO",slash2
,1);
127 args
.n
= 0; args
.max
= argc
+ MAX_ENVVARS
+ 10;
128 args
.v
= xmalloc(args
.max
* sizeof(*args
.v
));
130 addarg(&args
, "userv");
131 if (debugmode
) addarg(&args
, "-DDEBUG=1");
133 filter_environment(FILTF_WILDCARD
, "", envok
, default_envok
,
134 add_userv_var
, &args
);
136 addarg(&args
, username
);
137 addarg(&args
, "www-cgi");
138 while ((av
= (*++argv
))) addarg(&args
, av
);
143 child
= fork(); if (child
==-1) syserror("fork");
145 rchild
= waitpid(child
,&status
,0);
146 if (rchild
==-1) syserror("waitpid");
147 printf("\nexit status %d %d\n",(status
>>8)&0x0ff,status
&0x0ff);
155 printf(";; final command line...\n");
156 for (i
= 0; args
.v
[i
]; i
++)
157 printf(";; %s\n", args
.v
[i
]);
161 execvp("userv",(char*const*)args
.v
);
162 syserror("exec userv");