Commit | Line | Data |
---|---|---|
6a580c17 | 1 | /* |
2 | * Usage: as CGI script | |
3 | */ | |
a33962ba | 4 | /* |
ca2efed5 | 5 | * Copyright 1996-2013,2016 Ian Jackson <ijackson@chiark.greenend.org.uk> |
9028e234 IJ |
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> | |
93733bee | 10 | * Copyright 2013,2016 Mark Wooding <mdw@distorted.org.uk> |
a33962ba | 11 | * |
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 | |
9028e234 | 14 | * the Free Software Foundation; either version 3 of the License, or |
a33962ba | 15 | * (at your option) any later version. |
16 | * | |
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. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License | |
9028e234 | 23 | * along with userv-utils; if not, see http://www.gnu.org/licenses/. |
a33962ba | 24 | */ |
6a580c17 | 25 | |
26 | #include <stdio.h> | |
27 | #include <string.h> | |
28 | #include <ctype.h> | |
29 | #include <unistd.h> | |
30 | #include <sys/types.h> | |
31 | #include <sys/wait.h> | |
32 | ||
33 | #include "ucgi.h" | |
34 | ||
f7b4be5a | 35 | static const char *const default_envok[] = { |
a8e8db26 MW |
36 | "AUTH_TYPE", |
37 | "CONTENT_TYPE", | |
38 | "CONTENT_LENGTH", | |
39 | "DOCUMENT_ROOT", | |
40 | "GATEWAY_INTERFACE", | |
41 | "HTTP_*", | |
42 | "HTTPS", | |
43 | "PATH_INFO", | |
44 | "PATH_TRANSLATED", | |
45 | "QUERY_STRING", | |
564fbf9b | 46 | "REDIRECT_*", |
a8e8db26 MW |
47 | "REMOTE_*", |
48 | "REQUEST_METHOD", | |
49 | "REQUEST_URI", | |
50 | "SCRIPT_*", | |
51 | "SERVER_*", | |
564fbf9b | 52 | "SSL_*", |
a8e8db26 MW |
53 | 0 |
54 | }; | |
55 | ||
77a36cae MW |
56 | struct buildargs { |
57 | const char **v; | |
58 | int n, max; | |
59 | }; | |
60 | ||
61 | static void addarg(struct buildargs *args, const char *a) { | |
0cd9d59d | 62 | if (args->n > args->max) error("too many arguments", 500); |
77a36cae MW |
63 | args->v[args->n++]= a; |
64 | } | |
65 | ||
f601a2c6 MW |
66 | static void add_userv_var(const char *fulln, |
67 | const char *en, const char *ev, void *p) { | |
68 | struct buildargs *args= p; | |
77a36cae MW |
69 | size_t l; |
70 | char *a; | |
71 | ||
0cd9d59d MW |
72 | l= strlen(ev); |
73 | if (l > MAX_ENVVAR_VALUE) error("environment variable too long", 500); | |
77a36cae MW |
74 | a= xmalloc(strlen(en)+l+6); |
75 | sprintf(a,"-DE_%s=%s",en,ev); | |
76 | addarg(args, a); | |
77 | } | |
78 | ||
6a580c17 | 79 | int main(int argc, const char **argv) { |
77a36cae | 80 | char *username; |
f7b4be5a | 81 | const char *slash2, *pathi, *ev, *av; |
aa0bce91 | 82 | const char *const *envok = 0; |
6a580c17 | 83 | size_t usernamelen, l; |
77a36cae | 84 | struct buildargs args; |
6a580c17 | 85 | pid_t child, rchild; |
77a36cae | 86 | int status; |
6a580c17 | 87 | |
88 | l= strlen(argv[0]); | |
89 | if (l>6 && !strcmp(argv[0]+l-6,"-debug")) debugmode= 1; | |
90 | ||
91 | if (debugmode) { | |
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); } | |
6a3086f1 | 95 | D( printf(";;; UCGI\n"); ) |
6a580c17 | 96 | } |
97 | ||
0cd9d59d | 98 | if (argc > MAX_ARGS) error("too many arguments", 500); |
6a580c17 | 99 | |
f7b4be5a MW |
100 | ev= getenv("UCGI_ENV_FILTER"); |
101 | if (ev) | |
102 | envok= load_filters(LOADF_MUST, ev, LF_END); | |
aa0bce91 | 103 | else |
f7b4be5a | 104 | envok= load_filters(0, "/etc/userv/ucgi.env-filter", LF_END); |
6a580c17 | 105 | |
106 | pathi= getenv("PATH_INFO"); | |
0cd9d59d | 107 | if (!pathi) error("PATH_INFO not found", 500); |
6a3086f1 MW |
108 | D( if (debugmode) { |
109 | printf(";; find user name...\n" | |
110 | ";; initial PATH_INFO = `%s'\n", | |
111 | pathi); | |
112 | } ) | |
0cd9d59d MW |
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); | |
6a580c17 | 117 | usernamelen= slash2-(pathi+2); |
0cd9d59d | 118 | if (usernamelen > MAX_USERNAME_LEN) error("PATH_INFO username too long", 400); |
6a580c17 | 119 | username= xmalloc(usernamelen+1); |
120 | memcpy(username,pathi+2,usernamelen); username[usernamelen]= 0; | |
6a3086f1 MW |
121 | D( if (debugmode) |
122 | printf(";; user = `%s'; tail = `%s'\n", username, slash2); ) | |
0cd9d59d MW |
123 | if (!isalpha(username[0])) |
124 | error("username 1st character is not alphabetic", 400); | |
6a580c17 | 125 | xsetenv("PATH_INFO",slash2,1); |
77a36cae | 126 | |
f601a2c6 | 127 | args.n= 0; args.max= argc + MAX_ENVVARS + 10; |
77a36cae | 128 | args.v= xmalloc(args.max * sizeof(*args.v)); |
6a580c17 | 129 | |
77a36cae MW |
130 | addarg(&args, "userv"); |
131 | if (debugmode) addarg(&args, "-DDEBUG=1"); | |
6a580c17 | 132 | |
aa0bce91 MW |
133 | filter_environment(FILTF_WILDCARD, "", envok, default_envok, |
134 | add_userv_var, &args); | |
6a580c17 | 135 | |
77a36cae MW |
136 | addarg(&args, username); |
137 | addarg(&args, "www-cgi"); | |
138 | while ((av= (*++argv))) addarg(&args, av); | |
139 | addarg(&args, 0); | |
6a580c17 | 140 | |
141 | if (debugmode) { | |
6a3086f1 | 142 | D( fflush(stdout); ) |
6a580c17 | 143 | child= fork(); if (child==-1) syserror("fork"); |
144 | if (child) { | |
145 | rchild= waitpid(child,&status,0); | |
146 | if (rchild==-1) syserror("waitpid"); | |
147 | printf("\nexit status %d %d\n",(status>>8)&0x0ff,status&0x0ff); | |
148 | exit(0); | |
149 | } | |
150 | } | |
151 | ||
6a3086f1 MW |
152 | D( if (debugmode) { |
153 | int i; | |
154 | ||
155 | printf(";; final command line...\n"); | |
77a36cae MW |
156 | for (i = 0; args.v[i]; i++) |
157 | printf(";; %s\n", args.v[i]); | |
6a3086f1 MW |
158 | fflush(stdout); |
159 | } ) | |
160 | ||
77a36cae | 161 | execvp("userv",(char*const*)args.v); |
6a580c17 | 162 | syserror("exec userv"); |
163 | return -1; | |
164 | } |