User home string to mk_pointer
[MonkeyD.git] / src / user.c
blob7b2f724251cb98f6e05ab2c1320ee7264db43a09
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2001-2002, Eduardo Silva P.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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]=='/')
62 m_build_buffer(&sr->uri.data, &sr->uri.len,
63 "%s", sr->uri_processed+offset+limit);
65 /* Extract URI portion after /~user */
66 sr->user_uri = (char*)mk_mem_malloc_z(sr->uri.len + 1);
67 char* src = sr->uri.data;
68 char* dst = sr->user_uri;
70 while (*src != ' ' && src < (sr->uri.data + sr->uri.len)){
71 *dst++ = *src++;
75 if((s_user=getpwnam(user))==NULL){
76 mk_mem_free(user);
77 mk_request_error(M_CLIENT_NOT_FOUND, cr, sr,1,sr->log);
78 return -1;
80 mk_mem_free(user);
82 m_build_buffer(&user_server_root, &len, "%s/%s",s_user->pw_dir, config->user_dir);
84 if(sr->user_uri!=NULL)
86 m_build_buffer(&sr->real_path.data, &sr->real_path.len, "%s%s",
87 user_server_root, sr->user_uri);
89 else
91 m_build_buffer(&sr->real_path.data, &sr->real_path.len, "%s",
92 user_server_root);
94 mk_mem_free(user_server_root);
95 return 0;
98 /* Cambia el usuario del proceso */
99 int mk_user_set_uidgid()
101 struct passwd *usr;
103 EGID=(gid_t) getegid();
104 EUID=(gid_t) geteuid();
106 if(geteuid()==0 && config->user) { /* Lanzado por root ?? */
107 struct rlimit rl;
109 /* Just if i'm superuser */
110 rl.rlim_max= (256 * config->maxclients);
111 rl.rlim_cur = rl.rlim_max;
112 setrlimit( RLIMIT_NOFILE, &rl );
114 /* Chequear si existe el usuario USER ... */
115 if ((usr = getpwnam( config->user )) == NULL) {
116 printf("Error: Invalid user '%s'\n", config->user);
117 exit(1);
121 if (initgroups(config->user, usr->pw_gid) != 0) {
122 exit(1);
125 /* Cambiar el UID y el GID del proceso */
126 if(setgid(usr->pw_gid)==-1) {
127 printf("I can't change the GID to %u\n", usr->pw_gid);
128 exit(1);
132 if(setuid(usr->pw_uid)==-1) {
133 printf("I can't change the UID to %u\n", usr->pw_uid);
134 exit(1);
137 egid = geteuid();
138 euid = getegid();
140 return 0;
143 /* Vuelve el proceso a su usuario original */
144 int mk_user_undo_uidgid()
146 if(EUID==0){
147 setegid(EGID);
148 seteuid(EUID);
150 return 0;