94330201b4d9f9b4d83d1a65abc23810579a8d4a
[userv-utils] / www-cgi / ucgi.c
1 /*
2 * Usage: as CGI script
3 */
4 /*
5 * Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
7 * Copyright 1999,2003
8 * Chancellor Masters and Scholars of the University of Cambridge
9 * Copyright 2010 Tony Finch <fanf@dotat.at>
10 *
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.
15 *
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.
20 *
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/.
23 */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31
32 #include "ucgi.h"
33
34 static const char *const default_envok[] = {
35 "AUTH_TYPE",
36 "CONTENT_TYPE",
37 "CONTENT_LENGTH",
38 "DOCUMENT_ROOT",
39 "GATEWAY_INTERFACE",
40 "HTTP_*",
41 "HTTPS",
42 "PATH_INFO",
43 "PATH_TRANSLATED",
44 "QUERY_STRING",
45 "REDIRECT_*",
46 "REMOTE_*",
47 "REQUEST_METHOD",
48 "REQUEST_URI",
49 "SCRIPT_*",
50 "SERVER_*",
51 "SSL_*",
52 0
53 };
54
55 struct buildargs {
56 const char **v;
57 int n, max;
58 };
59
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;
63 }
64
65 static void add_userv_var(const char *fulln,
66 const char *en, const char *ev, void *p) {
67 struct buildargs *args= p;
68 size_t l;
69 char *a;
70
71 l= strlen(ev);
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);
75 addarg(args, a);
76 }
77
78 int main(int argc, const char **argv) {
79 char *username;
80 const char *slash2, *pathi, *ev, *av;
81 const char *const *envok = 0;
82 size_t usernamelen, l;
83 struct buildargs args;
84 pid_t child, rchild;
85 int status;
86
87 l= strlen(argv[0]);
88 if (l>6 && !strcmp(argv[0]+l-6,"-debug")) debugmode= 1;
89
90 if (debugmode) {
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"); )
95 }
96
97 if (argc > MAX_ARGS) error("too many arguments", 500);
98
99 ev= getenv("UCGI_ENV_FILTER");
100 if (ev)
101 envok= load_filters(LOADF_MUST, ev, LF_END);
102 else
103 envok= load_filters(0, "/etc/userv/ucgi.env-filter", LF_END);
104
105 pathi= getenv("PATH_INFO");
106 if (!pathi) error("PATH_INFO not found", 500);
107 D( if (debugmode) {
108 printf(";; find user name...\n"
109 ";; initial PATH_INFO = `%s'\n",
110 pathi);
111 } )
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;
120 D( if (debugmode)
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);
125
126 args.n= 0; args.max= argc + MAX_ENVVARS + 10;
127 args.v= xmalloc(args.max * sizeof(*args.v));
128
129 addarg(&args, "userv");
130 if (debugmode) addarg(&args, "-DDEBUG=1");
131
132 filter_environment(FILTF_WILDCARD, "", envok, default_envok,
133 add_userv_var, &args);
134
135 addarg(&args, username);
136 addarg(&args, "www-cgi");
137 while ((av= (*++argv))) addarg(&args, av);
138 addarg(&args, 0);
139
140 if (debugmode) {
141 D( fflush(stdout); )
142 child= fork(); if (child==-1) syserror("fork");
143 if (child) {
144 rchild= waitpid(child,&status,0);
145 if (rchild==-1) syserror("waitpid");
146 printf("\nexit status %d %d\n",(status>>8)&0x0ff,status&0x0ff);
147 exit(0);
148 }
149 }
150
151 D( if (debugmode) {
152 int i;
153
154 printf(";; final command line...\n");
155 for (i = 0; args.v[i]; i++)
156 printf(";; %s\n", args.v[i]);
157 fflush(stdout);
158 } )
159
160 execvp("userv",(char*const*)args.v);
161 syserror("exec userv");
162 return -1;
163 }