Commit | Line | Data |
---|---|---|
6a580c17 | 1 | /* |
2 | * Usage: as CGI script | |
3 | */ | |
a33962ba | 4 | /* |
711a0748 | 5 | * Copyright (C) 1998-1999,2003 Ian Jackson |
a33962ba | 6 | * |
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. | |
11 | * | |
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. | |
16 | * | |
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. | |
20 | * | |
21 | * $Id$ | |
22 | */ | |
6a580c17 | 23 | |
24 | #include <stdio.h> | |
25 | #include <string.h> | |
26 | #include <ctype.h> | |
27 | #include <unistd.h> | |
28 | #include <sys/types.h> | |
29 | #include <sys/wait.h> | |
30 | ||
31 | #include "ucgi.h" | |
32 | ||
f7b4be5a | 33 | static const char *const default_envok[] = { |
a8e8db26 MW |
34 | "AUTH_TYPE", |
35 | "CONTENT_TYPE", | |
36 | "CONTENT_LENGTH", | |
37 | "DOCUMENT_ROOT", | |
38 | "GATEWAY_INTERFACE", | |
39 | "HTTP_*", | |
40 | "HTTPS", | |
41 | "PATH_INFO", | |
42 | "PATH_TRANSLATED", | |
43 | "QUERY_STRING", | |
564fbf9b | 44 | "REDIRECT_*", |
a8e8db26 MW |
45 | "REMOTE_*", |
46 | "REQUEST_METHOD", | |
47 | "REQUEST_URI", | |
48 | "SCRIPT_*", | |
49 | "SERVER_*", | |
564fbf9b | 50 | "SSL_*", |
a8e8db26 MW |
51 | 0 |
52 | }; | |
53 | ||
77a36cae MW |
54 | struct buildargs { |
55 | const char **v; | |
56 | int n, max; | |
57 | }; | |
58 | ||
59 | static void addarg(struct buildargs *args, const char *a) { | |
0cd9d59d | 60 | if (args->n > args->max) error("too many arguments", 500); |
77a36cae MW |
61 | args->v[args->n++]= a; |
62 | } | |
63 | ||
f601a2c6 MW |
64 | static void add_userv_var(const char *fulln, |
65 | const char *en, const char *ev, void *p) { | |
66 | struct buildargs *args= p; | |
77a36cae MW |
67 | size_t l; |
68 | char *a; | |
69 | ||
0cd9d59d MW |
70 | l= strlen(ev); |
71 | if (l > MAX_ENVVAR_VALUE) error("environment variable too long", 500); | |
77a36cae MW |
72 | a= xmalloc(strlen(en)+l+6); |
73 | sprintf(a,"-DE_%s=%s",en,ev); | |
74 | addarg(args, a); | |
75 | } | |
76 | ||
6a580c17 | 77 | int main(int argc, const char **argv) { |
77a36cae | 78 | char *username; |
f7b4be5a | 79 | const char *slash2, *pathi, *ev, *av; |
aa0bce91 | 80 | const char *const *envok = 0; |
6a580c17 | 81 | size_t usernamelen, l; |
77a36cae | 82 | struct buildargs args; |
6a580c17 | 83 | pid_t child, rchild; |
77a36cae | 84 | int status; |
6a580c17 | 85 | |
86 | l= strlen(argv[0]); | |
87 | if (l>6 && !strcmp(argv[0]+l-6,"-debug")) debugmode= 1; | |
88 | ||
89 | if (debugmode) { | |
90 | if (fputs("Content-Type: text/plain\n\n",stdout)==EOF || fflush(stdout)) | |
91 | syserror("write stdout"); | |
92 | if (dup2(1,2)<0) { perror("dup stdout to stderr"); exit(-1); } | |
6a3086f1 | 93 | D( printf(";;; UCGI\n"); ) |
6a580c17 | 94 | } |
95 | ||
0cd9d59d | 96 | if (argc > MAX_ARGS) error("too many arguments", 500); |
6a580c17 | 97 | |
f7b4be5a MW |
98 | ev= getenv("UCGI_ENV_FILTER"); |
99 | if (ev) | |
100 | envok= load_filters(LOADF_MUST, ev, LF_END); | |
aa0bce91 | 101 | else |
f7b4be5a | 102 | envok= load_filters(0, "/etc/userv/ucgi.env-filter", LF_END); |
f7b4be5a | 103 | |
6a580c17 | 104 | pathi= getenv("PATH_INFO"); |
0cd9d59d | 105 | if (!pathi) error("PATH_INFO not found", 500); |
6a3086f1 MW |
106 | D( if (debugmode) { |
107 | printf(";; find user name...\n" | |
108 | ";; initial PATH_INFO = `%s'\n", | |
109 | pathi); | |
110 | } ) | |
0cd9d59d MW |
111 | if (pathi[0] != '/' || pathi[1] != '~') |
112 | error("PATH_INFO must start with /~", 400); | |
113 | slash2= strchr(pathi+2,'/'); | |
114 | if (!slash2) error("PATH_INFO must have more than one /", 400); | |
6a580c17 | 115 | usernamelen= slash2-(pathi+2); |
0cd9d59d | 116 | if (usernamelen > MAX_USERNAME_LEN) error("PATH_INFO username too long", 400); |
6a580c17 | 117 | username= xmalloc(usernamelen+1); |
118 | memcpy(username,pathi+2,usernamelen); username[usernamelen]= 0; | |
6a3086f1 MW |
119 | D( if (debugmode) |
120 | printf(";; user = `%s'; tail = `%s'\n", username, slash2); ) | |
0cd9d59d MW |
121 | if (!isalpha(username[0])) |
122 | error("username 1st character is not alphabetic", 400); | |
6a580c17 | 123 | xsetenv("PATH_INFO",slash2,1); |
77a36cae | 124 | |
f601a2c6 | 125 | args.n= 0; args.max= argc + MAX_ENVVARS + 10; |
77a36cae | 126 | args.v= xmalloc(args.max * sizeof(*args.v)); |
6a580c17 | 127 | |
77a36cae MW |
128 | addarg(&args, "userv"); |
129 | if (debugmode) addarg(&args, "-DDEBUG=1"); | |
130 | ||
aa0bce91 MW |
131 | filter_environment(FILTF_WILDCARD, "", envok, default_envok, |
132 | add_userv_var, &args); | |
6a580c17 | 133 | |
77a36cae MW |
134 | addarg(&args, username); |
135 | addarg(&args, "www-cgi"); | |
136 | while ((av= (*++argv))) addarg(&args, av); | |
137 | addarg(&args, 0); | |
6a580c17 | 138 | |
139 | if (debugmode) { | |
6a3086f1 | 140 | D( fflush(stdout); ) |
6a580c17 | 141 | child= fork(); if (child==-1) syserror("fork"); |
142 | if (child) { | |
143 | rchild= waitpid(child,&status,0); | |
144 | if (rchild==-1) syserror("waitpid"); | |
145 | printf("\nexit status %d %d\n",(status>>8)&0x0ff,status&0x0ff); | |
146 | exit(0); | |
147 | } | |
148 | } | |
149 | ||
6a3086f1 MW |
150 | D( if (debugmode) { |
151 | int i; | |
152 | ||
153 | printf(";; final command line...\n"); | |
77a36cae MW |
154 | for (i = 0; args.v[i]; i++) |
155 | printf(";; %s\n", args.v[i]); | |
6a3086f1 MW |
156 | fflush(stdout); |
157 | } ) | |
158 | ||
77a36cae | 159 | execvp("userv",(char*const*)args.v); |
6a580c17 | 160 | syserror("exec userv"); |
161 | return -1; | |
162 | } |