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