6a580c17 |
1 | /* |
2 | * Usage: as CGI script |
3 | */ |
39c4ee86 |
4 | /* |
5 | * Copyright (C) 1998-1999 Ian Jackson |
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: ucgi.c,v 1.2 1999/11/09 23:04:32 ian Exp $ |
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 | |
33 | int main(int argc, const char **argv) { |
34 | char *defarg, *username; |
35 | const char *slash2, *pathi, *ev, *en, *av; |
36 | const char *const *ep; |
37 | const char **arguments; |
38 | size_t usernamelen, l; |
39 | pid_t child, rchild; |
40 | int nargs, status; |
41 | |
42 | l= strlen(argv[0]); |
43 | if (l>6 && !strcmp(argv[0]+l-6,"-debug")) debugmode= 1; |
44 | |
45 | if (debugmode) { |
46 | if (fputs("Content-Type: text/plain\n\n",stdout)==EOF || fflush(stdout)) |
47 | syserror("write stdout"); |
48 | if (dup2(1,2)<0) { perror("dup stdout to stderr"); exit(-1); } |
49 | } |
50 | |
51 | if (argc > MAX_ARGS) error("too many arguments"); |
52 | |
53 | pathi= getenv("PATH_INFO"); |
54 | if (!pathi) error("PATH_INFO not found"); |
55 | if (pathi[0] != '/' || pathi[1] != '~') error("PATH_INFO must start with /~"); |
56 | slash2= strchr(pathi+2,'/'); if (!slash2) error("PATH_INFO must have more than one /"); |
57 | usernamelen= slash2-(pathi+2); |
58 | if (usernamelen > MAX_USERNAME_LEN) error("PATH_INFO username too long"); |
59 | username= xmalloc(usernamelen+1); |
60 | memcpy(username,pathi+2,usernamelen); username[usernamelen]= 0; |
61 | if (!isalpha(username[0])) error("username 1st character is not alphabetic"); |
62 | xsetenv("PATH_INFO",slash2,1); |
63 | |
64 | arguments= xmalloc(sizeof(const char*)*(nenvok+argc+10)); |
65 | nargs= 0; |
66 | |
67 | arguments[nargs++]= "userv"; |
68 | if (debugmode) arguments[nargs++]= "-DDEBUG=1"; |
69 | |
70 | for (ep= envok; (en= *ep); ep++) { |
71 | ev= getenv(en); if (!ev) continue; |
72 | l= strlen(ev); if (l > MAX_ENVVAR_VALUE) error("environment variable too long"); |
73 | defarg= xmalloc(strlen(en)+l+6); |
74 | sprintf(defarg,"-DE_%s=%s",en,ev); |
75 | arguments[nargs++]= defarg; |
76 | } |
77 | |
78 | arguments[nargs++]= username; |
79 | arguments[nargs++]= "www-cgi"; |
80 | while ((av= (*++argv))) arguments[nargs++]= av; |
81 | arguments[nargs++]= 0; |
82 | |
83 | if (debugmode) { |
84 | child= fork(); if (child==-1) syserror("fork"); |
85 | if (child) { |
86 | rchild= waitpid(child,&status,0); |
87 | if (rchild==-1) syserror("waitpid"); |
88 | printf("\nexit status %d %d\n",(status>>8)&0x0ff,status&0x0ff); |
89 | exit(0); |
90 | } |
91 | } |
92 | |
93 | execvp("userv",(char*const*)arguments); |
94 | syserror("exec userv"); |
95 | return -1; |
96 | } |