1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 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.
26 #include <sys/types.h>
28 #include <sys/resource.h>
29 #include <sys/types.h>
35 #include "http_status.h"
41 int mk_user_init(struct client_request
*cr
, struct request
*sr
)
44 int offset
= mk_user_home
.len
;
45 char *user
= 0, *user_server_root
= 0;
46 struct passwd
*s_user
;
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
, "/");
55 limit
= strlen(sr
->uri_processed
) - offset
;
57 strncpy(user
, sr
->uri_processed
+ offset
, limit
);
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
)) {
74 if ((s_user
= getpwnam(user
)) == NULL
) {
76 mk_request_error(M_CLIENT_NOT_FOUND
, cr
, sr
, 1);
81 mk_string_build(&user_server_root
, &len
, "%s/%s", s_user
->pw_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
);
89 mk_string_build(&sr
->real_path
.data
, &sr
->real_path
.len
, "%s",
92 mk_mem_free(user_server_root
);
96 /* Change process user */
97 int mk_user_set_uidgid()
101 EGID
= (gid_t
) getegid();
102 EUID
= (gid_t
) geteuid();
104 /* Launched by root ? */
105 if (geteuid() == 0 && config
->user
) {
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
);
119 if (initgroups(config
->user
, usr
->pw_gid
) != 0) {
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
);
130 if (setuid(usr
->pw_uid
) == -1) {
131 printf("I can't change the UID to %u\n", usr
->pw_uid
);
141 /* Return process to the original user */
142 int mk_user_undo_uidgid()