Update to GPLv3+; update copyright notices everywhere.
[userv-utils] / www-cgi / ucgicommon.c
1 /*
2 * Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
3 * Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
4 * Copyright 1999,2003
5 * Chancellor Masters and Scholars of the University of Cambridge
6 * Copyright 2010 Tony Finch <fanf@dotat.at>
7 *
8 * This is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with userv-utils; if not, see http://www.gnu.org/licenses/.
20 */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include "ucgi.h"
27
28 const char *const envok[]= {
29 "AUTH_TYPE",
30 "CONTENT_LENGTH",
31 "CONTENT_TYPE",
32 "DOCUMENT_ROOT",
33 "GATEWAY_INTERFACE",
34 "HTTP_ACCEPT",
35 "HTTP_ACCEPT_CHARSET",
36 "HTTP_ACCEPT_ENCODING",
37 "HTTP_ACCEPT_LANGUAGE",
38 "HTTP_CACHE_CONTROL",
39 "HTTP_CONNECTION",
40 "HTTP_CONTENT_ENCODING",
41 "HTTP_COOKIE",
42 "HTTP_DNT",
43 "HTTP_HOST",
44 "HTTP_KEEP_ALIVE",
45 "HTTP_NEGOTIATE",
46 "HTTP_PRAGMA",
47 "HTTP_REFERER",
48 "HTTP_USER_AGENT",
49 "HTTP_VIA",
50 "HTTP_X_FORWARDED_FOR",
51 "HTTPS",
52 "PATH_INFO",
53 "PATH_TRANSLATED",
54 "QUERY_STRING",
55 "REMOTE_ADDR",
56 "REMOTE_HOST",
57 "REMOTE_USER",
58 "REMOTE_IDENT",
59 "REQUEST_METHOD",
60 "REQUEST_URI",
61 "SCRIPT_FILENAME",
62 "SCRIPT_NAME",
63 "SCRIPT_URI",
64 "SCRIPT_URL",
65 "SERVER_ADDR",
66 "SERVER_ADMIN",
67 "SERVER_NAME",
68 "SERVER_PORT",
69 "SERVER_PROTOCOL",
70 "SERVER_SIGNATURE",
71 "SERVER_SOFTWARE",
72 0
73 };
74 const int nenvok= sizeof(envok)/sizeof(envok[0]);
75
76 int debugmode= 0;
77
78 static void outerror(void) {
79 perror("stdout");
80 exit(debugmode ? 0 : -1);
81 }
82
83 void syserror(const char *m) {
84 if (printf("Content-Type: text/plain\n\n"
85 "ucgi: system call error:\n"
86 "%s: %s\n",
87 m,strerror(errno))==EOF || fflush(stdout)) outerror();
88 exit(0);
89 }
90
91 void error(const char *m) {
92 if (printf("Content-Type: text/plain\n\n"
93 "ucgi: error:\n"
94 "%s\n",
95 m)==EOF || fflush(stdout)) outerror();
96 exit(0);
97 }
98
99 void *xmalloc(size_t sz) {
100 void *r;
101
102 r= malloc(sz);
103 if (!r) syserror("malloc failed");
104 return r;
105 }
106
107 void xsetenv(const char *en, const char *ev, int overwrite) {
108 if (setenv(en,ev,overwrite)) syserror("setenv");
109 }