Commit | Line | Data |
---|---|---|
6a580c17 | 1 | /* |
2 | * Usage: as CGI script | |
3 | */ | |
a33962ba | 4 | /* |
9028e234 IJ |
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> | |
a33962ba | 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 | |
9028e234 | 13 | * the Free Software Foundation; either version 3 of the License, or |
a33962ba | 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 | |
9028e234 | 22 | * along with userv-utils; if not, see http://www.gnu.org/licenses/. |
a33962ba | 23 | */ |
6a580c17 | 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 | ||
f7b4be5a | 34 | static const char *const default_envok[] = { |
a8e8db26 MW |
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", | |
564fbf9b | 45 | "REDIRECT_*", |
a8e8db26 MW |
46 | "REMOTE_*", |
47 | "REQUEST_METHOD", | |
48 | "REQUEST_URI", | |
49 | "SCRIPT_*", | |
50 | "SERVER_*", | |
564fbf9b | 51 | "SSL_*", |
a8e8db26 MW |
52 | 0 |
53 | }; | |
54 | ||
77a36cae MW |
55 | struct buildargs { |
56 | const char **v; | |
57 | int n, max; | |
58 | }; | |
59 | ||
60 | static void addarg(struct buildargs *args, const char *a) { | |
0cd9d59d | 61 | if (args->n > args->max) error("too many arguments", 500); |
77a36cae MW |
62 | args->v[args->n++]= a; |
63 | } | |
64 | ||
f601a2c6 MW |
65 | static void add_userv_var(const char *fulln, |
66 | const char *en, const char *ev, void *p) { | |
67 | struct buildargs *args= p; | |
77a36cae MW |
68 | size_t l; |
69 | char *a; | |
70 | ||
0cd9d59d MW |
71 | l= strlen(ev); |
72 | if (l > MAX_ENVVAR_VALUE) error("environment variable too long", 500); | |
77a36cae MW |
73 | a= xmalloc(strlen(en)+l+6); |
74 | sprintf(a,"-DE_%s=%s",en,ev); | |
75 | addarg(args, a); | |
76 | } | |
77 | ||
6a580c17 | 78 | int main(int argc, const char **argv) { |
77a36cae | 79 | char *username; |
f7b4be5a | 80 | const char *slash2, *pathi, *ev, *av; |
aa0bce91 | 81 | const char *const *envok = 0; |
6a580c17 | 82 | size_t usernamelen, l; |
77a36cae | 83 | struct buildargs args; |
6a580c17 | 84 | pid_t child, rchild; |
77a36cae | 85 | int status; |
6a580c17 | 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); } | |
6a3086f1 | 94 | D( printf(";;; UCGI\n"); ) |
6a580c17 | 95 | } |
96 | ||
0cd9d59d | 97 | if (argc > MAX_ARGS) error("too many arguments", 500); |
6a580c17 | 98 | |
f7b4be5a MW |
99 | ev= getenv("UCGI_ENV_FILTER"); |
100 | if (ev) | |
101 | envok= load_filters(LOADF_MUST, ev, LF_END); | |
aa0bce91 | 102 | else |
f7b4be5a | 103 | envok= load_filters(0, "/etc/userv/ucgi.env-filter", LF_END); |
6a580c17 | 104 | |
105 | pathi= getenv("PATH_INFO"); | |
0cd9d59d | 106 | if (!pathi) error("PATH_INFO not found", 500); |
6a3086f1 MW |
107 | D( if (debugmode) { |
108 | printf(";; find user name...\n" | |
109 | ";; initial PATH_INFO = `%s'\n", | |
110 | pathi); | |
111 | } ) | |
0cd9d59d MW |
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); | |
6a580c17 | 116 | usernamelen= slash2-(pathi+2); |
0cd9d59d | 117 | if (usernamelen > MAX_USERNAME_LEN) error("PATH_INFO username too long", 400); |
6a580c17 | 118 | username= xmalloc(usernamelen+1); |
119 | memcpy(username,pathi+2,usernamelen); username[usernamelen]= 0; | |
6a3086f1 MW |
120 | D( if (debugmode) |
121 | printf(";; user = `%s'; tail = `%s'\n", username, slash2); ) | |
0cd9d59d MW |
122 | if (!isalpha(username[0])) |
123 | error("username 1st character is not alphabetic", 400); | |
6a580c17 | 124 | xsetenv("PATH_INFO",slash2,1); |
77a36cae | 125 | |
f601a2c6 | 126 | args.n= 0; args.max= argc + MAX_ENVVARS + 10; |
77a36cae | 127 | args.v= xmalloc(args.max * sizeof(*args.v)); |
6a580c17 | 128 | |
77a36cae MW |
129 | addarg(&args, "userv"); |
130 | if (debugmode) addarg(&args, "-DDEBUG=1"); | |
6a580c17 | 131 | |
aa0bce91 MW |
132 | filter_environment(FILTF_WILDCARD, "", envok, default_envok, |
133 | add_userv_var, &args); | |
6a580c17 | 134 | |
77a36cae MW |
135 | addarg(&args, username); |
136 | addarg(&args, "www-cgi"); | |
137 | while ((av= (*++argv))) addarg(&args, av); | |
138 | addarg(&args, 0); | |
6a580c17 | 139 | |
140 | if (debugmode) { | |
6a3086f1 | 141 | D( fflush(stdout); ) |
6a580c17 | 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 | ||
6a3086f1 MW |
151 | D( if (debugmode) { |
152 | int i; | |
153 | ||
154 | printf(";; final command line...\n"); | |
77a36cae MW |
155 | for (i = 0; args.v[i]; i++) |
156 | printf(";; %s\n", args.v[i]); | |
6a3086f1 MW |
157 | fflush(stdout); |
158 | } ) | |
159 | ||
77a36cae | 160 | execvp("userv",(char*const*)args.v); |
6a580c17 | 161 | syserror("exec userv"); |
162 | return -1; | |
163 | } |