Home | History | Annotate | Line # | Download | only in httpd
tilde-luzah-bozo.c revision 1.1
      1 /*	$eterna: tilde-luzah-bozo.c,v 1.4 2006/05/17 08:37:36 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997-2006 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  * 3. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  */
     32 
     33 /* this code implements ~user support for bozohttpd */
     34 
     35 #ifndef NO_USER_SUPPORT
     36 
     37 #include <sys/param.h>
     38 
     39 #include <errno.h>
     40 #include <pwd.h>
     41 #include <string.h>
     42 #include <unistd.h>
     43 
     44 #include "bozohttpd.h"
     45 
     46 #ifndef PUBLIC_HTML
     47 #define PUBLIC_HTML		"public_html"
     48 #endif
     49 
     50 	int	uflag;		/* allow /~user/ translation */
     51 	const char *public_html	= PUBLIC_HTML;
     52 
     53 /*
     54  * user_transform does this:
     55  *	- chdir's /~user/public_html
     56  *	- returns the rest of the file, index.html appended if required
     57  *
     58  * transform_request() is supposed to check that we have user support
     59  * enabled.
     60  */
     61 char *
     62 user_transform(request, isindex)
     63 	http_req *request;
     64 	int *isindex;
     65 {
     66 	char	c, *s, *file = NULL;
     67 	struct	passwd *pw;
     68 
     69 	*isindex = 0;
     70 
     71 	if ((s = strchr(request->hr_url + 2, '/')) != NULL) {
     72 		*s++ = '\0';
     73 		c = s[strlen(s)-1];
     74 		*isindex = (c == '/' || c == '\0');
     75 	}
     76 
     77 	debug((DEBUG_OBESE, "looking for user %s", request->hr_url + 2));
     78 	pw = getpwnam(request->hr_url + 2);
     79 	/* fix this up immediately */
     80 	if (s)
     81 		s[-1] = '/';
     82 	if (pw == NULL)
     83 		http_error(404, request, "no such user");
     84 
     85 	debug((DEBUG_OBESE, "user %s home dir %s uid %d gid %d", pw->pw_name,
     86 	    pw->pw_dir, pw->pw_uid, pw->pw_gid));
     87 
     88 	if (chdir(pw->pw_dir) < 0) {
     89 		warning("chdir1 error: %s: %s", pw->pw_dir, strerror(errno));
     90 		http_error(403, request, "can't chdir to homedir");
     91 	}
     92 	if (chdir(public_html) < 0) {
     93 		warning("chdir2 error: %s: %s", public_html, strerror(errno));
     94 		http_error(403, request, "can't chdir to public_html");
     95 	}
     96 	if (s == NULL || *s == '\0') {
     97 		file = bozostrdup(index_html);
     98 	} else {
     99 		file = bozomalloc(strlen(s) +
    100 		    (*isindex ? strlen(index_html) + 1 : 1));
    101 		strcpy(file, s);
    102 		if (*isindex)
    103 			strcat(file, index_html);
    104 	}
    105 
    106 	/* see transform_request() */
    107 	if (*file == '/' || strcmp(file, "..") == 0 ||
    108 	    strstr(file, "/..") || strstr(file, "../"))
    109 		http_error(403, request, "illegal request");
    110 
    111 	auth_check(request, file);
    112 
    113 	debug((DEBUG_FAT, "transform_user returning %s under %s", file,
    114 	    pw->pw_dir));
    115 	return (file);
    116 }
    117 #endif /* NO_USER_SUPPORT */
    118