Remove deprecated maxclients and maxip from configuration struct
[MonkeyD.git] / src / user.c
blob24e93e2ce39c0a7efd14a0de744bfe86ae598bd1
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2001-2002, Eduardo Silva P. <edsiper@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <pwd.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <sys/resource.h>
29 #include <sys/types.h>
30 #include <grp.h>
32 #include "monkey.h"
33 #include "user.h"
34 #include "http.h"
35 #include "http_status.h"
36 #include "memory.h"
37 #include "str.h"
38 #include "utils.h"
39 #include "config.h"
41 int mk_user_init(struct client_request *cr, struct request *sr)
43 int limit;
44 int offset = mk_user_home.len;
45 char *user = 0, *user_server_root = 0;
46 struct passwd *s_user;
47 unsigned long len;
49 sr->user_home = VAR_ON;
51 user = mk_mem_malloc(strlen(sr->uri_processed) + 1);
52 limit = mk_string_search(sr->uri_processed + offset, "/");
54 if (limit == -1)
55 limit = strlen(sr->uri_processed) - offset;
57 strncpy(user, sr->uri_processed + offset, limit);
58 user[limit] = '\0';
60 if (sr->uri.data[offset + limit] == '/') {
61 mk_string_build(&sr->uri.data, &sr->uri.len,
62 "%s", sr->uri_processed + offset + limit);
64 /* Extract URI portion after /~user */
65 sr->user_uri = (char *) mk_mem_malloc_z(sr->uri.len + 1);
66 char *src = sr->uri.data;
67 char *dst = sr->user_uri;
69 while (*src != ' ' && src < (sr->uri.data + sr->uri.len)) {
70 *dst++ = *src++;
74 if ((s_user = getpwnam(user)) == NULL) {
75 mk_mem_free(user);
76 mk_request_error(M_CLIENT_NOT_FOUND, cr, sr, 1);
77 return -1;
79 mk_mem_free(user);
81 mk_string_build(&user_server_root, &len, "%s/%s", s_user->pw_dir,
82 config->user_dir);
84 if (sr->user_uri != NULL) {
85 mk_string_build(&sr->real_path.data, &sr->real_path.len, "%s%s",
86 user_server_root, sr->user_uri);
88 else {
89 mk_string_build(&sr->real_path.data, &sr->real_path.len, "%s",
90 user_server_root);
92 mk_mem_free(user_server_root);
93 return 0;
96 /* Change process user */
97 int mk_user_set_uidgid()
99 struct passwd *usr;
101 EGID = (gid_t) getegid();
102 EUID = (gid_t) geteuid();
104 /* Launched by root ? */
105 if (geteuid() == 0 && config->user) {
106 struct rlimit rl;
108 /* Just if i'm superuser */
109 rl.rlim_cur = rl.rlim_max;
110 setrlimit(RLIMIT_NOFILE, &rl);
112 /* Check if user exists */
113 if ((usr = getpwnam(config->user)) == NULL) {
114 printf("Error: Invalid user '%s'\n", config->user);
115 exit(1);
119 if (initgroups(config->user, usr->pw_gid) != 0) {
120 exit(1);
123 /* Change process UID and GID */
124 if (setgid(usr->pw_gid) == -1) {
125 printf("I can't change the GID to %u\n", usr->pw_gid);
126 exit(1);
130 if (setuid(usr->pw_uid) == -1) {
131 printf("I can't change the UID to %u\n", usr->pw_uid);
132 exit(1);
135 EUID = geteuid();
136 EGID = getegid();
138 return 0;
141 /* Return process to the original user */
142 int mk_user_undo_uidgid()
144 if (EUID == 0) {
145 setegid(EGID);
146 seteuid(EUID);
148 return 0;