1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/*
* Usage: as CGI script
*/
/*
* Copyright 1996-2013,2016 Ian Jackson <ijackson@chiark.greenend.org.uk>
* Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
* Copyright 1999,2003
* Chancellor Masters and Scholars of the University of Cambridge
* Copyright 2010 Tony Finch <fanf@dotat.at>
* Copyright 2013,2016 Mark Wooding <mdw@distorted.org.uk>
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with userv-utils; if not, see http://www.gnu.org/licenses/.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "ucgi.h"
static const char *const default_envok[] = {
"AUTH_TYPE",
"CONTENT_TYPE",
"CONTENT_LENGTH",
"DOCUMENT_ROOT",
"GATEWAY_INTERFACE",
"HTTP_*",
"HTTPS",
"PATH_INFO",
"PATH_TRANSLATED",
"QUERY_STRING",
"REDIRECT_*",
"REMOTE_*",
"REQUEST_METHOD",
"REQUEST_URI",
"SCRIPT_*",
"SERVER_*",
"SSL_*",
0
};
struct buildargs {
const char **v;
int n, max;
};
static void addarg(struct buildargs *args, const char *a) {
if (args->n > args->max) error("too many arguments", 500);
args->v[args->n++]= a;
}
static void add_userv_var(const char *fulln,
const char *en, const char *ev, void *p) {
struct buildargs *args= p;
size_t l;
char *a;
l= strlen(ev);
if (l > MAX_ENVVAR_VALUE) error("environment variable too long", 500);
a= xmalloc(strlen(en)+l+6);
sprintf(a,"-DE_%s=%s",en,ev);
addarg(args, a);
}
int main(int argc, const char **argv) {
char *username;
const char *slash2, *pathi, *ev, *av;
const char *const *envok = 0;
size_t usernamelen, l;
struct buildargs args;
pid_t child, rchild;
int status;
l= strlen(argv[0]);
if (l>6 && !strcmp(argv[0]+l-6,"-debug")) debugmode= 1;
if (debugmode) {
if (fputs("Content-Type: text/plain\n\n",stdout)==EOF || fflush(stdout))
syserror("write stdout");
if (dup2(1,2)<0) { perror("dup stdout to stderr"); exit(-1); }
D( printf(";;; UCGI\n"); )
}
if (argc > MAX_ARGS) error("too many arguments", 500);
ev= getenv("UCGI_ENV_FILTER");
if (ev)
envok= load_filters(LOADF_MUST, ev, LF_END);
else
envok= load_filters(0, "/etc/userv/ucgi.env-filter", LF_END);
pathi= getenv("PATH_INFO");
if (!pathi) error("PATH_INFO not found", 500);
D( if (debugmode) {
printf(";; find user name...\n"
";; initial PATH_INFO = `%s'\n",
pathi);
} )
if (pathi[0] != '/' || pathi[1] != '~')
error("PATH_INFO must start with /~", 400);
slash2= strchr(pathi+2,'/');
if (!slash2) error("PATH_INFO must have more than one /", 400);
usernamelen= slash2-(pathi+2);
if (usernamelen > MAX_USERNAME_LEN) error("PATH_INFO username too long", 400);
username= xmalloc(usernamelen+1);
memcpy(username,pathi+2,usernamelen); username[usernamelen]= 0;
D( if (debugmode)
printf(";; user = `%s'; tail = `%s'\n", username, slash2); )
if (!isalpha(username[0]))
error("username 1st character is not alphabetic", 400);
xsetenv("PATH_INFO",slash2,1);
args.n= 0; args.max= argc + MAX_ENVVARS + 10;
args.v= xmalloc(args.max * sizeof(*args.v));
addarg(&args, "userv");
if (debugmode) addarg(&args, "-DDEBUG=1");
filter_environment(FILTF_WILDCARD, "", envok, default_envok,
add_userv_var, &args);
addarg(&args, username);
addarg(&args, "www-cgi");
while ((av= (*++argv))) addarg(&args, av);
addarg(&args, 0);
if (debugmode) {
D( fflush(stdout); )
child= fork(); if (child==-1) syserror("fork");
if (child) {
rchild= waitpid(child,&status,0);
if (rchild==-1) syserror("waitpid");
printf("\nexit status %d %d\n",(status>>8)&0x0ff,status&0x0ff);
exit(0);
}
}
D( if (debugmode) {
int i;
printf(";; final command line...\n");
for (i = 0; args.v[i]; i++)
printf(";; %s\n", args.v[i]);
fflush(stdout);
} )
execvp("userv",(char*const*)args.v);
syserror("exec userv");
return -1;
}
|