Home | History | Annotate | Line # | Download | only in httpd
tilde-luzah-bozo.c revision 1.1.1.3
      1 /*	$eterna: tilde-luzah-bozo.c,v 1.10 2009/04/18 05:36:04 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997-2009 Matthew R. Green
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer and
     14  *    dedication in the documentation and/or other materials provided
     15  *    with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  *
     29  */
     30 
     31 /* this code implements ~user support for bozohttpd */
     32 
     33 #ifndef NO_USER_SUPPORT
     34 
     35 #include <sys/param.h>
     36 
     37 #include <errno.h>
     38 #include <pwd.h>
     39 #include <stdlib.h>
     40 #include <string.h>
     41 #include <unistd.h>
     42 
     43 #include "bozohttpd.h"
     44 
     45 #ifndef PUBLIC_HTML
     46 #define PUBLIC_HTML		"public_html"
     47 #endif
     48 
     49 	int	uflag;		/* allow /~user/ translation */
     50 	const char *public_html	= PUBLIC_HTML;
     51 
     52 /*
     53  * user_transform does this:
     54  *	- chdir's /~user/public_html
     55  *	- returns the rest of the file, index.html appended if required
     56  *	- returned malloced file to serve in request->hr_file,
     57  *        ala transform_request().
     58  *
     59  * transform_request() is supposed to check that we have user support
     60  * enabled.
     61  */
     62 int
     63 user_transform(request, isindex)
     64 	http_req *request;
     65 	int *isindex;
     66 {
     67 	char	c, *s, *file = NULL;
     68 	struct	passwd *pw;
     69 
     70 	*isindex = 0;
     71 
     72 	if ((s = strchr(request->hr_file + 2, '/')) != NULL) {
     73 		*s++ = '\0';
     74 		c = s[strlen(s)-1];
     75 		*isindex = (c == '/' || c == '\0');
     76 	}
     77 
     78 	debug((DEBUG_OBESE, "looking for user %s", request->hr_file + 2));
     79 	pw = getpwnam(request->hr_file + 2);
     80 	/* fix this up immediately */
     81 	if (s)
     82 		s[-1] = '/';
     83 	if (pw == NULL) {
     84 		(void)http_error(404, request, "no such user");
     85 		return 0;
     86 	}
     87 
     88 	debug((DEBUG_OBESE, "user %s home dir %s uid %d gid %d", pw->pw_name,
     89 	    pw->pw_dir, pw->pw_uid, pw->pw_gid));
     90 
     91 	if (chdir(pw->pw_dir) < 0) {
     92 		warning("chdir1 error: %s: %s", pw->pw_dir, strerror(errno));
     93 		(void)http_error(403, request, "can't chdir to homedir");
     94 		return 0;
     95 	}
     96 	if (chdir(public_html) < 0) {
     97 		warning("chdir2 error: %s: %s", public_html, strerror(errno));
     98 		(void)http_error(403, request, "can't chdir to public_html");
     99 		return 0;
    100 	}
    101 	if (s == NULL || *s == '\0') {
    102 		file = bozostrdup(index_html);
    103 	} else {
    104 		file = bozomalloc(strlen(s) +
    105 		    (*isindex ? strlen(index_html) + 1 : 1));
    106 		strcpy(file, s);
    107 		if (*isindex)
    108 			strcat(file, index_html);
    109 	}
    110 
    111 	/* see transform_request() */
    112 	if (*file == '/' || strcmp(file, "..") == 0 ||
    113 	    strstr(file, "/..") || strstr(file, "../")) {
    114 		(void)http_error(403, request, "illegal request");
    115 		free(file);
    116 		return 0;
    117 	}
    118 
    119 	if (auth_check(request, file)) {
    120 		free(file);
    121 		return 0;
    122 	}
    123 
    124 	free(request->hr_file);
    125 	request->hr_file = file;
    126 
    127 	debug((DEBUG_FAT, "transform_user returning %s under %s", file,
    128 	    pw->pw_dir));
    129 	return 1;
    130 }
    131 #endif /* NO_USER_SUPPORT */
    132