[862] | 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 | #ifndef __OS2__
|
---|
| 325 | pwd = Get_Pwnam_alloc(talloc_tos(), user);
|
---|
| 326 | if (!pwd) {
|
---|
| 327 | printf("%sCannot find user %s<br>%s\n", head, user, tail);
|
---|
| 328 | exit(0);
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | C_user = SMB_STRDUP(user);
|
---|
| 332 |
|
---|
| 333 | if (!setuid(0)) {
|
---|
| 334 | C_pass = SMB_STRDUP(cgi_nonce());
|
---|
| 335 | }
|
---|
| 336 | setuid(pwd->pw_uid);
|
---|
| 337 | if (geteuid() != pwd->pw_uid || getuid() != pwd->pw_uid) {
|
---|
| 338 | printf("%sFailed to become user %s - uid=%d/%d<br>%s\n",
|
---|
| 339 | head, user, (int)geteuid(), (int)getuid(), tail);
|
---|
| 340 | exit(0);
|
---|
| 341 | }
|
---|
| 342 | TALLOC_FREE(pwd);
|
---|
| 343 | #endif
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 |
|
---|
| 347 | /***************************************************************************
|
---|
| 348 | handle a http authentication line
|
---|
| 349 | ***************************************************************************/
|
---|
| 350 | static bool cgi_handle_authorization(char *line)
|
---|
| 351 | {
|
---|
| 352 | char *p;
|
---|
| 353 | fstring user, user_pass;
|
---|
| 354 | struct passwd *pass = NULL;
|
---|
| 355 | const char *rhost;
|
---|
| 356 | char addr[INET6_ADDRSTRLEN];
|
---|
| 357 |
|
---|
| 358 | if (!strnequal(line,"Basic ", 6)) {
|
---|
| 359 | goto err;
|
---|
| 360 | }
|
---|
| 361 | line += 6;
|
---|
| 362 | while (line[0] == ' ') line++;
|
---|
| 363 | base64_decode_inplace(line);
|
---|
| 364 | if (!(p=strchr_m(line,':'))) {
|
---|
| 365 | /*
|
---|
| 366 | * Always give the same error so a cracker
|
---|
| 367 | * cannot tell why we fail.
|
---|
| 368 | */
|
---|
| 369 | goto err;
|
---|
| 370 | }
|
---|
| 371 | *p = 0;
|
---|
| 372 |
|
---|
| 373 | convert_string(CH_UTF8, CH_UNIX,
|
---|
| 374 | line, -1,
|
---|
| 375 | user, sizeof(user), True);
|
---|
| 376 |
|
---|
| 377 | convert_string(CH_UTF8, CH_UNIX,
|
---|
| 378 | p+1, -1,
|
---|
| 379 | user_pass, sizeof(user_pass), True);
|
---|
| 380 |
|
---|
| 381 | /*
|
---|
| 382 | * Try and get the user from the UNIX password file.
|
---|
| 383 | */
|
---|
| 384 |
|
---|
| 385 | pass = Get_Pwnam_alloc(talloc_tos(), user);
|
---|
| 386 |
|
---|
| 387 | rhost = client_name(1);
|
---|
| 388 | if (strequal(rhost,"UNKNOWN"))
|
---|
| 389 | rhost = client_addr(1, addr, sizeof(addr));
|
---|
| 390 |
|
---|
| 391 | /*
|
---|
| 392 | * Validate the password they have given.
|
---|
| 393 | */
|
---|
| 394 |
|
---|
| 395 | if NT_STATUS_IS_OK(pass_check(pass, user, rhost, user_pass, false)) {
|
---|
| 396 | if (pass) {
|
---|
| 397 | /*
|
---|
| 398 | * Password was ok.
|
---|
| 399 | */
|
---|
| 400 |
|
---|
| 401 | if ( initgroups(pass->pw_name, pass->pw_gid) != 0 )
|
---|
| 402 | goto err;
|
---|
| 403 |
|
---|
| 404 | become_user_permanently(pass->pw_uid, pass->pw_gid);
|
---|
| 405 |
|
---|
| 406 | /* Save the users name */
|
---|
| 407 | C_user = SMB_STRDUP(user);
|
---|
| 408 | C_pass = SMB_STRDUP(user_pass);
|
---|
| 409 | TALLOC_FREE(pass);
|
---|
| 410 | return True;
|
---|
| 411 | }
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 | err:
|
---|
| 415 | cgi_setup_error("401 Bad Authorization",
|
---|
| 416 | "WWW-Authenticate: Basic realm=\"SWAT\"\r\n",
|
---|
| 417 | "username or password incorrect");
|
---|
| 418 |
|
---|
| 419 | TALLOC_FREE(pass);
|
---|
| 420 | return False;
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | /***************************************************************************
|
---|
| 424 | is this root?
|
---|
| 425 | ***************************************************************************/
|
---|
| 426 | bool am_root(void)
|
---|
| 427 | {
|
---|
| 428 | if (geteuid() == 0) {
|
---|
| 429 | return( True);
|
---|
| 430 | } else {
|
---|
| 431 | return( False);
|
---|
| 432 | }
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | /***************************************************************************
|
---|
| 436 | return a ptr to the users name
|
---|
| 437 | ***************************************************************************/
|
---|
| 438 | char *cgi_user_name(void)
|
---|
| 439 | {
|
---|
| 440 | return(C_user);
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | /***************************************************************************
|
---|
| 444 | return a ptr to the users password
|
---|
| 445 | ***************************************************************************/
|
---|
| 446 | char *cgi_user_pass(void)
|
---|
| 447 | {
|
---|
| 448 | return(C_pass);
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | /***************************************************************************
|
---|
| 452 | return a ptr to the nonce
|
---|
| 453 | ***************************************************************************/
|
---|
| 454 | char *cgi_nonce(void)
|
---|
| 455 | {
|
---|
| 456 | const char *head = "Content-Type: text/html\r\n\r\n<HTML><BODY><H1>SWAT installation Error</H1>\n";
|
---|
| 457 | const char *tail = "</BODY></HTML>\r\n";
|
---|
| 458 | C_nonce = secrets_fetch_generic("root", "SWAT");
|
---|
| 459 | if (C_nonce == NULL) {
|
---|
| 460 | char *tmp_pass = NULL;
|
---|
| 461 | tmp_pass = generate_random_password(talloc_tos(),
|
---|
| 462 | 16, 16);
|
---|
| 463 | if (tmp_pass == NULL) {
|
---|
| 464 | printf("%sFailed to create random nonce for "
|
---|
| 465 | "SWAT session\n<br>%s\n", head, tail);
|
---|
| 466 | exit(0);
|
---|
| 467 | }
|
---|
| 468 | secrets_store_generic("root", "SWAT", tmp_pass);
|
---|
| 469 | C_nonce = SMB_STRDUP(tmp_pass);
|
---|
| 470 | TALLOC_FREE(tmp_pass);
|
---|
| 471 | }
|
---|
| 472 | return(C_nonce);
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | /***************************************************************************
|
---|
| 476 | handle a file download
|
---|
| 477 | ***************************************************************************/
|
---|
| 478 | static void cgi_download(char *file)
|
---|
| 479 | {
|
---|
| 480 | SMB_STRUCT_STAT st;
|
---|
| 481 | char buf[1024];
|
---|
| 482 | int fd, l, i;
|
---|
| 483 | char *p;
|
---|
| 484 | char *lang;
|
---|
| 485 |
|
---|
| 486 | /* sanitise the filename */
|
---|
| 487 | for (i=0;file[i];i++) {
|
---|
| 488 | if (!isalnum((int)file[i]) && !strchr_m("/.-_", file[i])) {
|
---|
| 489 | cgi_setup_error("404 File Not Found","",
|
---|
| 490 | "Illegal character in filename");
|
---|
| 491 | }
|
---|
| 492 | }
|
---|
| 493 |
|
---|
| 494 | if (sys_stat(file, &st, false) != 0) {
|
---|
| 495 | cgi_setup_error("404 File Not Found","",
|
---|
| 496 | "The requested file was not found");
|
---|
| 497 | }
|
---|
| 498 |
|
---|
| 499 | if (S_ISDIR(st.st_ex_mode))
|
---|
| 500 | {
|
---|
| 501 | snprintf(buf, sizeof(buf), "%s/index.html", file);
|
---|
| 502 | if (!file_exist_stat(buf, &st, false)
|
---|
| 503 | || !S_ISREG(st.st_ex_mode))
|
---|
| 504 | {
|
---|
| 505 | cgi_setup_error("404 File Not Found","",
|
---|
| 506 | "The requested file was not found");
|
---|
| 507 | }
|
---|
| 508 | }
|
---|
| 509 | else if (S_ISREG(st.st_ex_mode))
|
---|
| 510 | {
|
---|
| 511 | snprintf(buf, sizeof(buf), "%s", file);
|
---|
| 512 | }
|
---|
| 513 | else
|
---|
| 514 | {
|
---|
| 515 | cgi_setup_error("404 File Not Found","",
|
---|
| 516 | "The requested file was not found");
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 | fd = web_open(buf,O_RDONLY,0);
|
---|
| 520 | if (fd == -1) {
|
---|
| 521 | cgi_setup_error("404 File Not Found","",
|
---|
| 522 | "The requested file was not found");
|
---|
| 523 | }
|
---|
| 524 | printf("HTTP/1.0 200 OK\r\n");
|
---|
| 525 | if ((p=strrchr_m(buf, '.'))) {
|
---|
| 526 | if (strcmp(p,".gif")==0) {
|
---|
| 527 | printf("Content-Type: image/gif\r\n");
|
---|
| 528 | } else if (strcmp(p,".jpg")==0) {
|
---|
| 529 | printf("Content-Type: image/jpeg\r\n");
|
---|
| 530 | } else if (strcmp(p,".png")==0) {
|
---|
| 531 | printf("Content-Type: image/png\r\n");
|
---|
| 532 | } else if (strcmp(p,".css")==0) {
|
---|
| 533 | printf("Content-Type: text/css\r\n");
|
---|
| 534 | } else if (strcmp(p,".txt")==0) {
|
---|
| 535 | printf("Content-Type: text/plain\r\n");
|
---|
| 536 | } else {
|
---|
| 537 | printf("Content-Type: text/html\r\n");
|
---|
| 538 | }
|
---|
| 539 | }
|
---|
| 540 | printf("Expires: %s\r\n",
|
---|
| 541 | http_timestring(talloc_tos(), time(NULL)+EXPIRY_TIME));
|
---|
| 542 |
|
---|
| 543 | lang = lang_tdb_current();
|
---|
| 544 | if (lang) {
|
---|
| 545 | printf("Content-Language: %s\r\n", lang);
|
---|
| 546 | }
|
---|
| 547 |
|
---|
| 548 | printf("Content-Length: %d\r\n\r\n", (int)st.st_ex_size);
|
---|
| 549 | while ((l=read(fd,buf,sizeof(buf)))>0) {
|
---|
| 550 | if (fwrite(buf, 1, l, stdout) != l) {
|
---|
| 551 | break;
|
---|
| 552 | }
|
---|
| 553 | }
|
---|
| 554 | close(fd);
|
---|
| 555 | exit(0);
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 |
|
---|
| 559 |
|
---|
| 560 | /* return true if the char* contains ip addrs only. Used to avoid
|
---|
| 561 | name lookup calls */
|
---|
| 562 |
|
---|
| 563 | static bool only_ipaddrs_in_list(const char **list)
|
---|
| 564 | {
|
---|
| 565 | bool only_ip = true;
|
---|
| 566 |
|
---|
| 567 | if (!list) {
|
---|
| 568 | return true;
|
---|
| 569 | }
|
---|
| 570 |
|
---|
| 571 | for (; *list ; list++) {
|
---|
| 572 | /* factor out the special strings */
|
---|
| 573 | if (strequal(*list, "ALL") || strequal(*list, "FAIL") ||
|
---|
| 574 | strequal(*list, "EXCEPT")) {
|
---|
| 575 | continue;
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | if (!is_ipaddress(*list)) {
|
---|
| 579 | /*
|
---|
| 580 | * If we failed, make sure that it was not because
|
---|
| 581 | * the token was a network/netmask pair. Only
|
---|
| 582 | * network/netmask pairs have a '/' in them.
|
---|
| 583 | */
|
---|
| 584 | if ((strchr_m(*list, '/')) == NULL) {
|
---|
| 585 | only_ip = false;
|
---|
| 586 | DEBUG(3,("only_ipaddrs_in_list: list has "
|
---|
| 587 | "non-ip address (%s)\n",
|
---|
| 588 | *list));
|
---|
| 589 | break;
|
---|
| 590 | }
|
---|
| 591 | }
|
---|
| 592 | }
|
---|
| 593 |
|
---|
| 594 | return only_ip;
|
---|
| 595 | }
|
---|
| 596 |
|
---|
| 597 | /* return true if access should be allowed to a service for a socket */
|
---|
| 598 | static bool check_access(int sock, const char **allow_list,
|
---|
| 599 | const char **deny_list)
|
---|
| 600 | {
|
---|
| 601 | bool ret = false;
|
---|
| 602 | bool only_ip = false;
|
---|
| 603 | char addr[INET6_ADDRSTRLEN];
|
---|
| 604 |
|
---|
| 605 | if ((!deny_list || *deny_list==0) && (!allow_list || *allow_list==0)) {
|
---|
| 606 | return true;
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | /* Bypass name resolution calls if the lists
|
---|
| 610 | * only contain IP addrs */
|
---|
| 611 | if (only_ipaddrs_in_list(allow_list) &&
|
---|
| 612 | only_ipaddrs_in_list(deny_list)) {
|
---|
| 613 | only_ip = true;
|
---|
| 614 | DEBUG (3, ("check_access: no hostnames "
|
---|
| 615 | "in host allow/deny list.\n"));
|
---|
| 616 | ret = allow_access(deny_list,
|
---|
| 617 | allow_list,
|
---|
| 618 | "",
|
---|
| 619 | get_peer_addr(sock,addr,sizeof(addr)));
|
---|
| 620 | } else {
|
---|
| 621 | DEBUG (3, ("check_access: hostnames in "
|
---|
| 622 | "host allow/deny list.\n"));
|
---|
| 623 | ret = allow_access(deny_list,
|
---|
| 624 | allow_list,
|
---|
| 625 | get_peer_name(sock,true),
|
---|
| 626 | get_peer_addr(sock,addr,sizeof(addr)));
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | if (ret) {
|
---|
| 630 | DEBUG(2,("Allowed connection from %s (%s)\n",
|
---|
| 631 | only_ip ? "" : get_peer_name(sock,true),
|
---|
| 632 | get_peer_addr(sock,addr,sizeof(addr))));
|
---|
| 633 | } else {
|
---|
| 634 | DEBUG(0,("Denied connection from %s (%s)\n",
|
---|
| 635 | only_ip ? "" : get_peer_name(sock,true),
|
---|
| 636 | get_peer_addr(sock,addr,sizeof(addr))));
|
---|
| 637 | }
|
---|
| 638 |
|
---|
| 639 | return(ret);
|
---|
| 640 | }
|
---|
| 641 |
|
---|
| 642 | /**
|
---|
| 643 | * @brief Setup the CGI framework.
|
---|
| 644 | *
|
---|
| 645 | * Setup the cgi framework, handling the possibility that this program
|
---|
| 646 | * is either run as a true CGI program with a gateway to a web server, or
|
---|
| 647 | * is itself a mini web server.
|
---|
| 648 | **/
|
---|
| 649 | void cgi_setup(const char *rootdir, int auth_required)
|
---|
| 650 | {
|
---|
| 651 | bool authenticated = False;
|
---|
| 652 | char line[1024];
|
---|
| 653 | char *url=NULL;
|
---|
| 654 | char *p;
|
---|
| 655 | char *lang;
|
---|
| 656 |
|
---|
| 657 | if (chdir(rootdir)) {
|
---|
| 658 | cgi_setup_error("500 Server Error", "",
|
---|
| 659 | "chdir failed - the server is not configured correctly");
|
---|
| 660 | }
|
---|
| 661 |
|
---|
| 662 | /* Handle the possibility we might be running as non-root */
|
---|
| 663 | sec_init();
|
---|
| 664 |
|
---|
| 665 | if ((lang=getenv("HTTP_ACCEPT_LANGUAGE"))) {
|
---|
| 666 | /* if running as a cgi program */
|
---|
| 667 | web_set_lang(lang);
|
---|
| 668 | }
|
---|
| 669 |
|
---|
| 670 | /* maybe we are running under a web server */
|
---|
| 671 | if (getenv("CONTENT_LENGTH") || getenv("REQUEST_METHOD")) {
|
---|
| 672 | if (auth_required) {
|
---|
| 673 | cgi_web_auth();
|
---|
| 674 | }
|
---|
| 675 | return;
|
---|
| 676 | }
|
---|
| 677 |
|
---|
| 678 | inetd_server = True;
|
---|
| 679 |
|
---|
| 680 | if (!check_access(1, lp_hostsallow(-1), lp_hostsdeny(-1))) {
|
---|
| 681 | cgi_setup_error("403 Forbidden", "",
|
---|
| 682 | "Samba is configured to deny access from this client\n<br>Check your \"hosts allow\" and \"hosts deny\" options in smb.conf ");
|
---|
| 683 | }
|
---|
| 684 |
|
---|
| 685 | /* we are a mini-web server. We need to read the request from stdin
|
---|
| 686 | and handle authentication etc */
|
---|
| 687 | while (fgets(line, sizeof(line)-1, stdin)) {
|
---|
| 688 | if (line[0] == '\r' || line[0] == '\n') break;
|
---|
| 689 | if (strnequal(line,"GET ", 4)) {
|
---|
| 690 | got_request = True;
|
---|
| 691 | url = SMB_STRDUP(&line[4]);
|
---|
| 692 | } else if (strnequal(line,"POST ", 5)) {
|
---|
| 693 | got_request = True;
|
---|
| 694 | request_post = 1;
|
---|
| 695 | url = SMB_STRDUP(&line[5]);
|
---|
| 696 | } else if (strnequal(line,"PUT ", 4)) {
|
---|
| 697 | got_request = True;
|
---|
| 698 | cgi_setup_error("400 Bad Request", "",
|
---|
| 699 | "This server does not accept PUT requests");
|
---|
| 700 | } else if (strnequal(line,"Authorization: ", 15)) {
|
---|
| 701 | authenticated = cgi_handle_authorization(&line[15]);
|
---|
| 702 | } else if (strnequal(line,"Content-Length: ", 16)) {
|
---|
| 703 | content_length = atoi(&line[16]);
|
---|
| 704 | } else if (strnequal(line,"Accept-Language: ", 17)) {
|
---|
| 705 | web_set_lang(&line[17]);
|
---|
| 706 | }
|
---|
| 707 | /* ignore all other requests! */
|
---|
| 708 | }
|
---|
| 709 |
|
---|
| 710 | if (auth_required && !authenticated) {
|
---|
| 711 | cgi_auth_error();
|
---|
| 712 | }
|
---|
| 713 |
|
---|
| 714 | if (!url) {
|
---|
| 715 | cgi_setup_error("400 Bad Request", "",
|
---|
| 716 | "You must specify a GET or POST request");
|
---|
| 717 | }
|
---|
| 718 |
|
---|
| 719 | /* trim the URL */
|
---|
| 720 | if ((p = strchr_m(url,' ')) || (p=strchr_m(url,'\t'))) {
|
---|
| 721 | *p = 0;
|
---|
| 722 | }
|
---|
| 723 | while (*url && strchr_m("\r\n",url[strlen(url)-1])) {
|
---|
| 724 | url[strlen(url)-1] = 0;
|
---|
| 725 | }
|
---|
| 726 |
|
---|
| 727 | /* anything following a ? in the URL is part of the query string */
|
---|
| 728 | if ((p=strchr_m(url,'?'))) {
|
---|
| 729 | query_string = p+1;
|
---|
| 730 | *p = 0;
|
---|
| 731 | }
|
---|
| 732 |
|
---|
| 733 | string_sub(url, "/swat/", "", 0);
|
---|
| 734 |
|
---|
| 735 | if (url[0] != '/' && strstr(url,"..")==0) {
|
---|
| 736 | cgi_download(url);
|
---|
| 737 | }
|
---|
| 738 |
|
---|
| 739 | printf("HTTP/1.0 200 OK\r\nConnection: close\r\n");
|
---|
| 740 | printf("Date: %s\r\n", http_timestring(talloc_tos(), time(NULL)));
|
---|
| 741 | baseurl = "";
|
---|
| 742 | pathinfo = url+1;
|
---|
| 743 | }
|
---|
| 744 |
|
---|
| 745 |
|
---|
| 746 | /***************************************************************************
|
---|
| 747 | return the current pages URL
|
---|
| 748 | ***************************************************************************/
|
---|
| 749 | const char *cgi_baseurl(void)
|
---|
| 750 | {
|
---|
| 751 | if (inetd_server) {
|
---|
| 752 | return baseurl;
|
---|
| 753 | }
|
---|
| 754 | return getenv("SCRIPT_NAME");
|
---|
| 755 | }
|
---|
| 756 |
|
---|
| 757 | /***************************************************************************
|
---|
| 758 | return the current pages path info
|
---|
| 759 | ***************************************************************************/
|
---|
| 760 | const char *cgi_pathinfo(void)
|
---|
| 761 | {
|
---|
| 762 | char *r;
|
---|
| 763 | if (inetd_server) {
|
---|
| 764 | return pathinfo;
|
---|
| 765 | }
|
---|
| 766 | r = getenv("PATH_INFO");
|
---|
| 767 | if (!r) return "";
|
---|
| 768 | if (*r == '/') r++;
|
---|
| 769 | return r;
|
---|
| 770 | }
|
---|
| 771 |
|
---|
| 772 | /***************************************************************************
|
---|
| 773 | return the hostname of the client
|
---|
| 774 | ***************************************************************************/
|
---|
| 775 | const char *cgi_remote_host(void)
|
---|
| 776 | {
|
---|
| 777 | if (inetd_server) {
|
---|
| 778 | return get_peer_name(1,False);
|
---|
| 779 | }
|
---|
| 780 | return getenv("REMOTE_HOST");
|
---|
| 781 | }
|
---|
| 782 |
|
---|
| 783 | /***************************************************************************
|
---|
| 784 | return the hostname of the client
|
---|
| 785 | ***************************************************************************/
|
---|
| 786 | const char *cgi_remote_addr(void)
|
---|
| 787 | {
|
---|
| 788 | if (inetd_server) {
|
---|
| 789 | char addr[INET6_ADDRSTRLEN];
|
---|
| 790 | get_peer_addr(1,addr,sizeof(addr));
|
---|
| 791 | return talloc_strdup(talloc_tos(), addr);
|
---|
| 792 | }
|
---|
| 793 | return getenv("REMOTE_ADDR");
|
---|
| 794 | }
|
---|
| 795 |
|
---|
| 796 |
|
---|
| 797 | /***************************************************************************
|
---|
| 798 | return True if the request was a POST
|
---|
| 799 | ***************************************************************************/
|
---|
| 800 | bool cgi_waspost(void)
|
---|
| 801 | {
|
---|
| 802 | if (inetd_server) {
|
---|
| 803 | return request_post;
|
---|
| 804 | }
|
---|
| 805 | return strequal(getenv("REQUEST_METHOD"), "POST");
|
---|
| 806 | }
|
---|