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