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 | ||
77a36cae MW |
33 | struct buildargs { |
34 | const char **v; | |
35 | int n, max; | |
36 | }; | |
37 | ||
38 | static void addarg(struct buildargs *args, const char *a) { | |
39 | if (args->n > args->max) error("too many arguments"); | |
40 | args->v[args->n++]= a; | |
41 | } | |
42 | ||
43 | static void add_userv_var(const char *en, const char *ev, | |
44 | struct buildargs *args) { | |
45 | size_t l; | |
46 | char *a; | |
47 | ||
48 | l= strlen(ev); if (l > MAX_ENVVAR_VALUE) error("environment variable too long"); | |
49 | a= xmalloc(strlen(en)+l+6); | |
50 | sprintf(a,"-DE_%s=%s",en,ev); | |
51 | addarg(args, a); | |
52 | } | |
53 | ||
6a580c17 | 54 | int main(int argc, const char **argv) { |
77a36cae | 55 | char *username; |
6a580c17 | 56 | const char *slash2, *pathi, *ev, *en, *av; |
57 | const char *const *ep; | |
6a580c17 | 58 | size_t usernamelen, l; |
77a36cae | 59 | struct buildargs args; |
6a580c17 | 60 | pid_t child, rchild; |
77a36cae | 61 | int status; |
6a580c17 | 62 | |
63 | l= strlen(argv[0]); | |
64 | if (l>6 && !strcmp(argv[0]+l-6,"-debug")) debugmode= 1; | |
65 | ||
66 | if (debugmode) { | |
67 | if (fputs("Content-Type: text/plain\n\n",stdout)==EOF || fflush(stdout)) | |
68 | syserror("write stdout"); | |
69 | if (dup2(1,2)<0) { perror("dup stdout to stderr"); exit(-1); } | |
6a3086f1 | 70 | D( printf(";;; UCGI\n"); ) |
6a580c17 | 71 | } |
72 | ||
73 | if (argc > MAX_ARGS) error("too many arguments"); | |
74 | ||
75 | pathi= getenv("PATH_INFO"); | |
76 | if (!pathi) error("PATH_INFO not found"); | |
6a3086f1 MW |
77 | D( if (debugmode) { |
78 | printf(";; find user name...\n" | |
79 | ";; initial PATH_INFO = `%s'\n", | |
80 | pathi); | |
81 | } ) | |
6a580c17 | 82 | if (pathi[0] != '/' || pathi[1] != '~') error("PATH_INFO must start with /~"); |
83 | slash2= strchr(pathi+2,'/'); if (!slash2) error("PATH_INFO must have more than one /"); | |
84 | usernamelen= slash2-(pathi+2); | |
85 | if (usernamelen > MAX_USERNAME_LEN) error("PATH_INFO username too long"); | |
86 | username= xmalloc(usernamelen+1); | |
87 | memcpy(username,pathi+2,usernamelen); username[usernamelen]= 0; | |
6a3086f1 MW |
88 | D( if (debugmode) |
89 | printf(";; user = `%s'; tail = `%s'\n", username, slash2); ) | |
6a580c17 | 90 | if (!isalpha(username[0])) error("username 1st character is not alphabetic"); |
91 | xsetenv("PATH_INFO",slash2,1); | |
77a36cae MW |
92 | |
93 | args.n= 0; args.max= argc + nenvok + 10; | |
94 | args.v= xmalloc(args.max * sizeof(*args.v)); | |
6a580c17 | 95 | |
77a36cae MW |
96 | addarg(&args, "userv"); |
97 | if (debugmode) addarg(&args, "-DDEBUG=1"); | |
98 | ||
6a580c17 | 99 | for (ep= envok; (en= *ep); ep++) { |
100 | ev= getenv(en); if (!ev) continue; | |
77a36cae | 101 | add_userv_var(en, ev, &args); |
6a580c17 | 102 | } |
103 | ||
77a36cae MW |
104 | addarg(&args, username); |
105 | addarg(&args, "www-cgi"); | |
106 | while ((av= (*++argv))) addarg(&args, av); | |
107 | addarg(&args, 0); | |
6a580c17 | 108 | |
109 | if (debugmode) { | |
6a3086f1 | 110 | D( fflush(stdout); ) |
6a580c17 | 111 | child= fork(); if (child==-1) syserror("fork"); |
112 | if (child) { | |
113 | rchild= waitpid(child,&status,0); | |
114 | if (rchild==-1) syserror("waitpid"); | |
115 | printf("\nexit status %d %d\n",(status>>8)&0x0ff,status&0x0ff); | |
116 | exit(0); | |
117 | } | |
118 | } | |
119 | ||
6a3086f1 MW |
120 | D( if (debugmode) { |
121 | int i; | |
122 | ||
123 | printf(";; final command line...\n"); | |
77a36cae MW |
124 | for (i = 0; args.v[i]; i++) |
125 | printf(";; %s\n", args.v[i]); | |
6a3086f1 MW |
126 | fflush(stdout); |
127 | } ) | |
128 | ||
77a36cae | 129 | execvp("userv",(char*const*)args.v); |
6a580c17 | 130 | syserror("exec userv"); |
131 | return -1; | |
132 | } |