1 | /*
|
---|
2 | some simple CGI helper routines
|
---|
3 | Copyright (C) Andrew Tridgell 1997-1998
|
---|
4 |
|
---|
5 | This program is free software; you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation; either version 3 of the License, or
|
---|
8 | (at your option) any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License
|
---|
16 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 |
|
---|
20 | #include "includes.h"
|
---|
21 | #include "web/swat_proto.h"
|
---|
22 | #include "secrets.h"
|
---|
23 | #include "../lib/util/util.h"
|
---|
24 |
|
---|
25 | #define MAX_VARIABLES 10000
|
---|
26 |
|
---|
27 | /* set the expiry on fixed pages */
|
---|
28 | #define EXPIRY_TIME (60*60*24*7)
|
---|
29 |
|
---|
30 | #ifdef DEBUG_COMMENTS
|
---|
31 | extern void print_title(char *fmt, ...);
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | struct cgi_var {
|
---|
35 | char *name;
|
---|
36 | char *value;
|
---|
37 | };
|
---|
38 |
|
---|
39 | static struct cgi_var variables[MAX_VARIABLES];
|
---|
40 | static int num_variables;
|
---|
41 | static int content_length;
|
---|
42 | static int request_post;
|
---|
43 | static char *query_string;
|
---|
44 | static const char *baseurl;
|
---|
45 | static char *pathinfo;
|
---|
46 | static char *C_user;
|
---|
47 | static char *C_pass;
|
---|
48 | static bool inetd_server;
|
---|
49 | static bool got_request;
|
---|
50 |
|
---|
51 | static char *grab_line(FILE *f, int *cl)
|
---|
52 | {
|
---|
53 | char *ret = NULL;
|
---|
54 | int i = 0;
|
---|
55 | int len = 0;
|
---|
56 |
|
---|
57 | while ((*cl)) {
|
---|
58 | int c;
|
---|
59 |
|
---|
60 | if (i == len) {
|
---|
61 | char *ret2;
|
---|
62 | if (len == 0) len = 1024;
|
---|
63 | else len *= 2;
|
---|
64 | ret2 = (char *)SMB_REALLOC_KEEP_OLD_ON_ERROR(ret, len);
|
---|
65 | if (!ret2) return ret;
|
---|
66 | ret = ret2;
|
---|
67 | }
|
---|
68 |
|
---|
69 | c = fgetc(f);
|
---|
70 | (*cl)--;
|
---|
71 |
|
---|
72 | if (c == EOF) {
|
---|
73 | (*cl) = 0;
|
---|
74 | break;
|
---|
75 | }
|
---|
76 |
|
---|
77 | if (c == '\r') continue;
|
---|
78 |
|
---|
79 | if (strchr_m("\n&", c)) break;
|
---|
80 |
|
---|
81 | ret[i++] = c;
|
---|
82 |
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (ret) {
|
---|
86 | ret[i] = 0;
|
---|
87 | }
|
---|
88 | return ret;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | URL encoded strings can have a '+', which should be replaced with a space
|
---|
93 |
|
---|
94 | (This was in rfc1738_unescape(), but that broke the squid helper)
|
---|
95 | **/
|
---|
96 |
|
---|
97 | static void plus_to_space_unescape(char *buf)
|
---|
98 | {
|
---|
99 | char *p=buf;
|
---|
100 |
|
---|
101 | while ((p=strchr_m(p,'+')))
|
---|
102 | *p = ' ';
|
---|
103 | }
|
---|
104 |
|
---|
105 | /***************************************************************************
|
---|
106 | load all the variables passed to the CGI program. May have multiple variables
|
---|
107 | with the same name and the same or different values. Takes a file parameter
|
---|
108 | for simulating CGI invocation eg loading saved preferences.
|
---|
109 | ***************************************************************************/
|
---|
110 | void cgi_load_variables(void)
|
---|
111 | {
|
---|
112 | static char *line;
|
---|
113 | char *p, *s, *tok;
|
---|
114 | int len, i;
|
---|
115 | FILE *f = stdin;
|
---|
116 |
|
---|
117 | #ifdef DEBUG_COMMENTS
|
---|
118 | char dummy[100]="";
|
---|
119 | print_title(dummy);
|
---|
120 | d_printf("<!== Start dump in cgi_load_variables() %s ==>\n",__FILE__);
|
---|
121 | #endif
|
---|
122 |
|
---|
123 | if (!content_length) {
|
---|
124 | p = getenv("CONTENT_LENGTH");
|
---|
125 | len = p?atoi(p):0;
|
---|
126 | } else {
|
---|
127 | len = content_length;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | if (len > 0 &&
|
---|
132 | (request_post ||
|
---|
133 | ((s=getenv("REQUEST_METHOD")) &&
|
---|
134 | strequal(s,"POST")))) {
|
---|
135 | while (len && (line=grab_line(f, &len))) {
|
---|
136 | p = strchr_m(line,'=');
|
---|
137 | if (!p) continue;
|
---|
138 |
|
---|
139 | *p = 0;
|
---|
140 |
|
---|
141 | variables[num_variables].name = SMB_STRDUP(line);
|
---|
142 | variables[num_variables].value = SMB_STRDUP(p+1);
|
---|
143 |
|
---|
144 | SAFE_FREE(line);
|
---|
145 |
|
---|
146 | if (!variables[num_variables].name ||
|
---|
147 | !variables[num_variables].value)
|
---|
148 | continue;
|
---|
149 |
|
---|
150 | plus_to_space_unescape(variables[num_variables].value);
|
---|
151 | rfc1738_unescape(variables[num_variables].value);
|
---|
152 | plus_to_space_unescape(variables[num_variables].name);
|
---|
153 | rfc1738_unescape(variables[num_variables].name);
|
---|
154 |
|
---|
155 | #ifdef DEBUG_COMMENTS
|
---|
156 | printf("<!== POST var %s has value \"%s\" ==>\n",
|
---|
157 | variables[num_variables].name,
|
---|
158 | variables[num_variables].value);
|
---|
159 | #endif
|
---|
160 |
|
---|
161 | num_variables++;
|
---|
162 | if (num_variables == MAX_VARIABLES) break;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | fclose(stdin);
|
---|
167 | open("/dev/null", O_RDWR);
|
---|
168 |
|
---|
169 | if ((s=query_string) || (s=getenv("QUERY_STRING"))) {
|
---|
170 | char *saveptr;
|
---|
171 | for (tok=strtok_r(s, "&;", &saveptr); tok;
|
---|
172 | tok=strtok_r(NULL, "&;", &saveptr)) {
|
---|
173 | p = strchr_m(tok,'=');
|
---|
174 | if (!p) continue;
|
---|
175 |
|
---|
176 | *p = 0;
|
---|
177 |
|
---|
178 | variables[num_variables].name = SMB_STRDUP(tok);
|
---|
179 | variables[num_variables].value = SMB_STRDUP(p+1);
|
---|
180 |
|
---|
181 | if (!variables[num_variables].name ||
|
---|
182 | !variables[num_variables].value)
|
---|
183 | continue;
|
---|
184 |
|
---|
185 | plus_to_space_unescape(variables[num_variables].value);
|
---|
186 | rfc1738_unescape(variables[num_variables].value);
|
---|
187 | plus_to_space_unescape(variables[num_variables].name);
|
---|
188 | rfc1738_unescape(variables[num_variables].name);
|
---|
189 |
|
---|
190 | #ifdef DEBUG_COMMENTS
|
---|
191 | printf("<!== Commandline var %s has value \"%s\" ==>\n",
|
---|
192 | variables[num_variables].name,
|
---|
193 | variables[num_variables].value);
|
---|
194 | #endif
|
---|
195 | num_variables++;
|
---|
196 | if (num_variables == MAX_VARIABLES) break;
|
---|
197 | }
|
---|
198 |
|
---|
199 | }
|
---|
200 | #ifdef DEBUG_COMMENTS
|
---|
201 | printf("<!== End dump in cgi_load_variables() ==>\n");
|
---|
202 | #endif
|
---|
203 |
|
---|
204 | /* variables from the client are in UTF-8 - convert them
|
---|
205 | to our internal unix charset before use */
|
---|
206 | for (i=0;i<num_variables;i++) {
|
---|
207 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
208 | char *dest = NULL;
|
---|
209 | size_t dest_len;
|
---|
210 |
|
---|
211 | convert_string_talloc(frame, CH_UTF8, CH_UNIX,
|
---|
212 | variables[i].name, strlen(variables[i].name),
|
---|
213 | &dest, &dest_len, True);
|
---|
214 | SAFE_FREE(variables[i].name);
|
---|
215 | variables[i].name = SMB_STRDUP(dest ? dest : "");
|
---|
216 |
|
---|
217 | dest = NULL;
|
---|
218 | convert_string_talloc(frame, CH_UTF8, CH_UNIX,
|
---|
219 | variables[i].value, strlen(variables[i].value),
|
---|
220 | &dest, &dest_len, True);
|
---|
221 | SAFE_FREE(variables[i].value);
|
---|
222 | variables[i].value = SMB_STRDUP(dest ? dest : "");
|
---|
223 | TALLOC_FREE(frame);
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | /***************************************************************************
|
---|
229 | find a variable passed via CGI
|
---|
230 | Doesn't quite do what you think in the case of POST text variables, because
|
---|
231 | if they exist they might have a value of "" or even " ", depending on the
|
---|
232 | browser. Also doesn't allow for variables[] containing multiple variables
|
---|
233 | with the same name and the same or different values.
|
---|
234 | ***************************************************************************/
|
---|
235 |
|
---|
236 | const char *cgi_variable(const char *name)
|
---|
237 | {
|
---|
238 | int i;
|
---|
239 |
|
---|
240 | for (i=0;i<num_variables;i++)
|
---|
241 | if (strcmp(variables[i].name, name) == 0)
|
---|
242 | return variables[i].value;
|
---|
243 | return NULL;
|
---|
244 | }
|
---|
245 |
|
---|
246 | /***************************************************************************
|
---|
247 | Version of the above that can't return a NULL pointer.
|
---|
248 | ***************************************************************************/
|
---|
249 |
|
---|
250 | const char *cgi_variable_nonull(const char *name)
|
---|
251 | {
|
---|
252 | const char *var = cgi_variable(name);
|
---|
253 | if (var) {
|
---|
254 | return var;
|
---|
255 | } else {
|
---|
256 | return "";
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | /***************************************************************************
|
---|
261 | tell a browser about a fatal error in the http processing
|
---|
262 | ***************************************************************************/
|
---|
263 | static void cgi_setup_error(const char *err, const char *header, const char *info)
|
---|
264 | {
|
---|
265 | if (!got_request) {
|
---|
266 | /* damn browsers don't like getting cut off before they give a request */
|
---|
267 | char line[1024];
|
---|
268 | while (fgets(line, sizeof(line)-1, stdin)) {
|
---|
269 | if (strnequal(line,"GET ", 4) ||
|
---|
270 | strnequal(line,"POST ", 5) ||
|
---|
271 | strnequal(line,"PUT ", 4)) {
|
---|
272 | break;
|
---|
273 | }
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | d_printf("HTTP/1.0 %s\r\n%sConnection: close\r\nContent-Type: text/html\r\n\r\n<HTML><HEAD><TITLE>%s</TITLE></HEAD><BODY><H1>%s</H1>%s<p></BODY></HTML>\r\n\r\n", err, header, err, err, info);
|
---|
278 | fclose(stdin);
|
---|
279 | fclose(stdout);
|
---|
280 | exit(0);
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | /***************************************************************************
|
---|
285 | tell a browser about a fatal authentication error
|
---|
286 | ***************************************************************************/
|
---|
287 | static void cgi_auth_error(void)
|
---|
288 | {
|
---|
289 | if (inetd_server) {
|
---|
290 | cgi_setup_error("401 Authorization Required",
|
---|
291 | "WWW-Authenticate: Basic realm=\"SWAT\"\r\n",
|
---|
292 | "You must be authenticated to use this service");
|
---|
293 | } else {
|
---|
294 | printf("Content-Type: text/html\r\n");
|
---|
295 |
|
---|
296 | printf("\r\n<HTML><HEAD><TITLE>SWAT</TITLE></HEAD>\n");
|
---|
297 | printf("<BODY><H1>Installation Error</H1>\n");
|
---|
298 | printf("SWAT must be installed via inetd. It cannot be run as a CGI script<p>\n");
|
---|
299 | printf("</BODY></HTML>\r\n");
|
---|
300 | }
|
---|
301 | exit(0);
|
---|
302 | }
|
---|
303 |
|
---|
304 | /***************************************************************************
|
---|
305 | authenticate when we are running as a CGI
|
---|
306 | ***************************************************************************/
|
---|
307 | static void cgi_web_auth(void)
|
---|
308 | {
|
---|
309 | const char *user = getenv("REMOTE_USER");
|
---|
310 | struct passwd *pwd;
|
---|
311 | const char *head = "Content-Type: text/html\r\n\r\n<HTML><BODY><H1>SWAT installation Error</H1>\n";
|
---|
312 | const char *tail = "</BODY></HTML>\r\n";
|
---|
313 |
|
---|
314 | if (!user) {
|
---|
315 | printf("%sREMOTE_USER not set. Not authenticated by web server.<br>%s\n",
|
---|
316 | head, tail);
|
---|
317 | exit(0);
|
---|
318 | }
|
---|
319 |
|
---|
320 | #ifndef __OS2__
|
---|
321 | pwd = Get_Pwnam_alloc(talloc_autofree_context(), user);
|
---|
322 |
|
---|
323 | if (!pwd) {
|
---|
324 | printf("%sCannot find user %s<br>%s\n", head, user, tail);
|
---|
325 | exit(0);
|
---|
326 | }
|
---|
327 |
|
---|
328 | C_user = SMB_STRDUP(user);
|
---|
329 |
|
---|
330 | if (!setuid(0)) {
|
---|
331 | C_pass = secrets_fetch_generic("root", "SWAT");
|
---|
332 | if (C_pass == NULL) {
|
---|
333 | char *tmp_pass = NULL;
|
---|
334 | tmp_pass = generate_random_str(talloc_tos(), 16);
|
---|
335 | if (tmp_pass == NULL) {
|
---|
336 | printf("%sFailed to create random nonce for "
|
---|
337 | "SWAT session\n<br>%s\n", head, tail);
|
---|
338 | exit(0);
|
---|
339 | }
|
---|
340 | secrets_store_generic("root", "SWAT", tmp_pass);
|
---|
341 | C_pass = SMB_STRDUP(tmp_pass);
|
---|
342 | TALLOC_FREE(tmp_pass);
|
---|
343 | }
|
---|
344 | }
|
---|
345 | setuid(pwd->pw_uid);
|
---|
346 | if (geteuid() != pwd->pw_uid || getuid() != pwd->pw_uid) {
|
---|
347 | printf("%sFailed to become user %s - uid=%d/%d<br>%s\n",
|
---|
348 | head, user, (int)geteuid(), (int)getuid(), tail);
|
---|
349 | exit(0);
|
---|
350 | }
|
---|
351 | TALLOC_FREE(pwd);
|
---|
352 | #endif
|
---|
353 | }
|
---|
354 |
|
---|
355 |
|
---|
356 | /***************************************************************************
|
---|
357 | handle a http authentication line
|
---|
358 | ***************************************************************************/
|
---|
359 | static bool cgi_handle_authorization(char *line)
|
---|
360 | {
|
---|
361 | char *p;
|
---|
362 | fstring user, user_pass;
|
---|
363 | struct passwd *pass = NULL;
|
---|
364 |
|
---|
365 | if (!strnequal(line,"Basic ", 6)) {
|
---|
366 | goto err;
|
---|
367 | }
|
---|
368 | line += 6;
|
---|
369 | while (line[0] == ' ') line++;
|
---|
370 | base64_decode_inplace(line);
|
---|
371 | if (!(p=strchr_m(line,':'))) {
|
---|
372 | /*
|
---|
373 | * Always give the same error so a cracker
|
---|
374 | * cannot tell why we fail.
|
---|
375 | */
|
---|
376 | goto err;
|
---|
377 | }
|
---|
378 | *p = 0;
|
---|
379 |
|
---|
380 | convert_string(CH_UTF8, CH_UNIX,
|
---|
381 | line, -1,
|
---|
382 | user, sizeof(user), True);
|
---|
383 |
|
---|
384 | convert_string(CH_UTF8, CH_UNIX,
|
---|
385 | p+1, -1,
|
---|
386 | user_pass, sizeof(user_pass), True);
|
---|
387 |
|
---|
388 | /*
|
---|
389 | * Try and get the user from the UNIX password file.
|
---|
390 | */
|
---|
391 |
|
---|
392 | pass = Get_Pwnam_alloc(talloc_autofree_context(), user);
|
---|
393 |
|
---|
394 | /*
|
---|
395 | * Validate the password they have given.
|
---|
396 | */
|
---|
397 |
|
---|
398 | if NT_STATUS_IS_OK(pass_check(pass, user, user_pass,
|
---|
399 | strlen(user_pass), NULL, False)) {
|
---|
400 |
|
---|
401 | if (pass) {
|
---|
402 | /*
|
---|
403 | * Password was ok.
|
---|
404 | */
|
---|
405 |
|
---|
406 | if ( initgroups(pass->pw_name, pass->pw_gid) != 0 )
|
---|
407 | goto err;
|
---|
408 |
|
---|
409 | become_user_permanently(pass->pw_uid, pass->pw_gid);
|
---|
410 |
|
---|
411 | /* Save the users name */
|
---|
412 | C_user = SMB_STRDUP(user);
|
---|
413 | C_pass = SMB_STRDUP(user_pass);
|
---|
414 | TALLOC_FREE(pass);
|
---|
415 | return True;
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | err:
|
---|
420 | cgi_setup_error("401 Bad Authorization",
|
---|
421 | "WWW-Authenticate: Basic realm=\"SWAT\"\r\n",
|
---|
422 | "username or password incorrect");
|
---|
423 |
|
---|
424 | TALLOC_FREE(pass);
|
---|
425 | return False;
|
---|
426 | }
|
---|
427 |
|
---|
428 | /***************************************************************************
|
---|
429 | is this root?
|
---|
430 | ***************************************************************************/
|
---|
431 | bool am_root(void)
|
---|
432 | {
|
---|
433 | if (geteuid() == 0) {
|
---|
434 | return( True);
|
---|
435 | } else {
|
---|
436 | return( False);
|
---|
437 | }
|
---|
438 | }
|
---|
439 |
|
---|
440 | /***************************************************************************
|
---|
441 | return a ptr to the users name
|
---|
442 | ***************************************************************************/
|
---|
443 | char *cgi_user_name(void)
|
---|
444 | {
|
---|
445 | return(C_user);
|
---|
446 | }
|
---|
447 |
|
---|
448 | /***************************************************************************
|
---|
449 | return a ptr to the users password
|
---|
450 | ***************************************************************************/
|
---|
451 | char *cgi_user_pass(void)
|
---|
452 | {
|
---|
453 | return(C_pass);
|
---|
454 | }
|
---|
455 |
|
---|
456 | /***************************************************************************
|
---|
457 | handle a file download
|
---|
458 | ***************************************************************************/
|
---|
459 | static void cgi_download(char *file)
|
---|
460 | {
|
---|
461 | SMB_STRUCT_STAT st;
|
---|
462 | char buf[1024];
|
---|
463 | int fd, l, i;
|
---|
464 | char *p;
|
---|
465 | char *lang;
|
---|
466 |
|
---|
467 | /* sanitise the filename */
|
---|
468 | for (i=0;file[i];i++) {
|
---|
469 | if (!isalnum((int)file[i]) && !strchr_m("/.-_", file[i])) {
|
---|
470 | cgi_setup_error("404 File Not Found","",
|
---|
471 | "Illegal character in filename");
|
---|
472 | }
|
---|
473 | }
|
---|
474 |
|
---|
475 | if (sys_stat(file, &st, false) != 0) {
|
---|
476 | cgi_setup_error("404 File Not Found","",
|
---|
477 | "The requested file was not found");
|
---|
478 | }
|
---|
479 |
|
---|
480 | if (S_ISDIR(st.st_ex_mode))
|
---|
481 | {
|
---|
482 | snprintf(buf, sizeof(buf), "%s/index.html", file);
|
---|
483 | if (!file_exist_stat(buf, &st, false)
|
---|
484 | || !S_ISREG(st.st_ex_mode))
|
---|
485 | {
|
---|
486 | cgi_setup_error("404 File Not Found","",
|
---|
487 | "The requested file was not found");
|
---|
488 | }
|
---|
489 | }
|
---|
490 | else if (S_ISREG(st.st_ex_mode))
|
---|
491 | {
|
---|
492 | snprintf(buf, sizeof(buf), "%s", file);
|
---|
493 | }
|
---|
494 | else
|
---|
495 | {
|
---|
496 | cgi_setup_error("404 File Not Found","",
|
---|
497 | "The requested file was not found");
|
---|
498 | }
|
---|
499 |
|
---|
500 | fd = web_open(buf,O_RDONLY,0);
|
---|
501 | if (fd == -1) {
|
---|
502 | cgi_setup_error("404 File Not Found","",
|
---|
503 | "The requested file was not found");
|
---|
504 | }
|
---|
505 | printf("HTTP/1.0 200 OK\r\n");
|
---|
506 | if ((p=strrchr_m(buf, '.'))) {
|
---|
507 | if (strcmp(p,".gif")==0) {
|
---|
508 | printf("Content-Type: image/gif\r\n");
|
---|
509 | } else if (strcmp(p,".jpg")==0) {
|
---|
510 | printf("Content-Type: image/jpeg\r\n");
|
---|
511 | } else if (strcmp(p,".png")==0) {
|
---|
512 | printf("Content-Type: image/png\r\n");
|
---|
513 | } else if (strcmp(p,".css")==0) {
|
---|
514 | printf("Content-Type: text/css\r\n");
|
---|
515 | } else if (strcmp(p,".txt")==0) {
|
---|
516 | printf("Content-Type: text/plain\r\n");
|
---|
517 | } else {
|
---|
518 | printf("Content-Type: text/html\r\n");
|
---|
519 | }
|
---|
520 | }
|
---|
521 | printf("Expires: %s\r\n",
|
---|
522 | http_timestring(talloc_tos(), time(NULL)+EXPIRY_TIME));
|
---|
523 |
|
---|
524 | lang = lang_tdb_current();
|
---|
525 | if (lang) {
|
---|
526 | printf("Content-Language: %s\r\n", lang);
|
---|
527 | }
|
---|
528 |
|
---|
529 | printf("Content-Length: %d\r\n\r\n", (int)st.st_ex_size);
|
---|
530 | while ((l=read(fd,buf,sizeof(buf)))>0) {
|
---|
531 | if (fwrite(buf, 1, l, stdout) != l) {
|
---|
532 | break;
|
---|
533 | }
|
---|
534 | }
|
---|
535 | close(fd);
|
---|
536 | exit(0);
|
---|
537 | }
|
---|
538 |
|
---|
539 |
|
---|
540 |
|
---|
541 |
|
---|
542 | /**
|
---|
543 | * @brief Setup the CGI framework.
|
---|
544 | *
|
---|
545 | * Setup the cgi framework, handling the possibility that this program
|
---|
546 | * is either run as a true CGI program with a gateway to a web server, or
|
---|
547 | * is itself a mini web server.
|
---|
548 | **/
|
---|
549 | void cgi_setup(const char *rootdir, int auth_required)
|
---|
550 | {
|
---|
551 | bool authenticated = False;
|
---|
552 | char line[1024];
|
---|
553 | char *url=NULL;
|
---|
554 | char *p;
|
---|
555 | char *lang;
|
---|
556 |
|
---|
557 | if (chdir(rootdir)) {
|
---|
558 | cgi_setup_error("500 Server Error", "",
|
---|
559 | "chdir failed - the server is not configured correctly");
|
---|
560 | }
|
---|
561 |
|
---|
562 | /* Handle the possibility we might be running as non-root */
|
---|
563 | sec_init();
|
---|
564 |
|
---|
565 | if ((lang=getenv("HTTP_ACCEPT_LANGUAGE"))) {
|
---|
566 | /* if running as a cgi program */
|
---|
567 | web_set_lang(lang);
|
---|
568 | }
|
---|
569 |
|
---|
570 | /* maybe we are running under a web server */
|
---|
571 | if (getenv("CONTENT_LENGTH") || getenv("REQUEST_METHOD")) {
|
---|
572 | if (auth_required) {
|
---|
573 | cgi_web_auth();
|
---|
574 | }
|
---|
575 | return;
|
---|
576 | }
|
---|
577 |
|
---|
578 | inetd_server = True;
|
---|
579 |
|
---|
580 | if (!check_access(1, lp_hostsallow(-1), lp_hostsdeny(-1))) {
|
---|
581 | cgi_setup_error("403 Forbidden", "",
|
---|
582 | "Samba is configured to deny access from this client\n<br>Check your \"hosts allow\" and \"hosts deny\" options in smb.conf ");
|
---|
583 | }
|
---|
584 |
|
---|
585 | /* we are a mini-web server. We need to read the request from stdin
|
---|
586 | and handle authentication etc */
|
---|
587 | while (fgets(line, sizeof(line)-1, stdin)) {
|
---|
588 | if (line[0] == '\r' || line[0] == '\n') break;
|
---|
589 | if (strnequal(line,"GET ", 4)) {
|
---|
590 | got_request = True;
|
---|
591 | url = SMB_STRDUP(&line[4]);
|
---|
592 | } else if (strnequal(line,"POST ", 5)) {
|
---|
593 | got_request = True;
|
---|
594 | request_post = 1;
|
---|
595 | url = SMB_STRDUP(&line[5]);
|
---|
596 | } else if (strnequal(line,"PUT ", 4)) {
|
---|
597 | got_request = True;
|
---|
598 | cgi_setup_error("400 Bad Request", "",
|
---|
599 | "This server does not accept PUT requests");
|
---|
600 | } else if (strnequal(line,"Authorization: ", 15)) {
|
---|
601 | authenticated = cgi_handle_authorization(&line[15]);
|
---|
602 | } else if (strnequal(line,"Content-Length: ", 16)) {
|
---|
603 | content_length = atoi(&line[16]);
|
---|
604 | } else if (strnequal(line,"Accept-Language: ", 17)) {
|
---|
605 | web_set_lang(&line[17]);
|
---|
606 | }
|
---|
607 | /* ignore all other requests! */
|
---|
608 | }
|
---|
609 |
|
---|
610 | if (auth_required && !authenticated) {
|
---|
611 | cgi_auth_error();
|
---|
612 | }
|
---|
613 |
|
---|
614 | if (!url) {
|
---|
615 | cgi_setup_error("400 Bad Request", "",
|
---|
616 | "You must specify a GET or POST request");
|
---|
617 | }
|
---|
618 |
|
---|
619 | /* trim the URL */
|
---|
620 | if ((p = strchr_m(url,' ')) || (p=strchr_m(url,'\t'))) {
|
---|
621 | *p = 0;
|
---|
622 | }
|
---|
623 | while (*url && strchr_m("\r\n",url[strlen(url)-1])) {
|
---|
624 | url[strlen(url)-1] = 0;
|
---|
625 | }
|
---|
626 |
|
---|
627 | /* anything following a ? in the URL is part of the query string */
|
---|
628 | if ((p=strchr_m(url,'?'))) {
|
---|
629 | query_string = p+1;
|
---|
630 | *p = 0;
|
---|
631 | }
|
---|
632 |
|
---|
633 | string_sub(url, "/swat/", "", 0);
|
---|
634 |
|
---|
635 | if (url[0] != '/' && strstr(url,"..")==0) {
|
---|
636 | cgi_download(url);
|
---|
637 | }
|
---|
638 |
|
---|
639 | printf("HTTP/1.0 200 OK\r\nConnection: close\r\n");
|
---|
640 | printf("Date: %s\r\n", http_timestring(talloc_tos(), time(NULL)));
|
---|
641 | baseurl = "";
|
---|
642 | pathinfo = url+1;
|
---|
643 | }
|
---|
644 |
|
---|
645 |
|
---|
646 | /***************************************************************************
|
---|
647 | return the current pages URL
|
---|
648 | ***************************************************************************/
|
---|
649 | const char *cgi_baseurl(void)
|
---|
650 | {
|
---|
651 | if (inetd_server) {
|
---|
652 | return baseurl;
|
---|
653 | }
|
---|
654 | return getenv("SCRIPT_NAME");
|
---|
655 | }
|
---|
656 |
|
---|
657 | /***************************************************************************
|
---|
658 | return the current pages path info
|
---|
659 | ***************************************************************************/
|
---|
660 | const char *cgi_pathinfo(void)
|
---|
661 | {
|
---|
662 | char *r;
|
---|
663 | if (inetd_server) {
|
---|
664 | return pathinfo;
|
---|
665 | }
|
---|
666 | r = getenv("PATH_INFO");
|
---|
667 | if (!r) return "";
|
---|
668 | if (*r == '/') r++;
|
---|
669 | return r;
|
---|
670 | }
|
---|
671 |
|
---|
672 | /***************************************************************************
|
---|
673 | return the hostname of the client
|
---|
674 | ***************************************************************************/
|
---|
675 | const char *cgi_remote_host(void)
|
---|
676 | {
|
---|
677 | if (inetd_server) {
|
---|
678 | return get_peer_name(1,False);
|
---|
679 | }
|
---|
680 | return getenv("REMOTE_HOST");
|
---|
681 | }
|
---|
682 |
|
---|
683 | /***************************************************************************
|
---|
684 | return the hostname of the client
|
---|
685 | ***************************************************************************/
|
---|
686 | const char *cgi_remote_addr(void)
|
---|
687 | {
|
---|
688 | if (inetd_server) {
|
---|
689 | char addr[INET6_ADDRSTRLEN];
|
---|
690 | get_peer_addr(1,addr,sizeof(addr));
|
---|
691 | return talloc_strdup(talloc_tos(), addr);
|
---|
692 | }
|
---|
693 | return getenv("REMOTE_ADDR");
|
---|
694 | }
|
---|
695 |
|
---|
696 |
|
---|
697 | /***************************************************************************
|
---|
698 | return True if the request was a POST
|
---|
699 | ***************************************************************************/
|
---|
700 | bool cgi_waspost(void)
|
---|
701 | {
|
---|
702 | if (inetd_server) {
|
---|
703 | return request_post;
|
---|
704 | }
|
---|
705 | return strequal(getenv("REQUEST_METHOD"), "POST");
|
---|
706 | }
|
---|