1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
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.
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
;
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
]=='/')
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
)){
75 if((s_user
=getpwnam(user
))==NULL
){
77 mk_request_error(M_CLIENT_NOT_FOUND
, cr
, sr
,1,sr
->log
);
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
);
91 m_build_buffer(&sr
->real_path
.data
, &sr
->real_path
.len
, "%s",
94 mk_mem_free(user_server_root
);
98 /* Cambia el usuario del proceso */
99 int mk_user_set_uidgid()
103 EGID
=(gid_t
) getegid();
104 EUID
=(gid_t
) geteuid();
106 if(geteuid()==0 && config
->user
) { /* Lanzado por root ?? */
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
);
121 if (initgroups(config
->user
, usr
->pw_gid
) != 0) {
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
);
132 if(setuid(usr
->pw_uid
)==-1) {
133 printf("I can't change the UID to %u\n", usr
->pw_uid
);
143 /* Vuelve el proceso a su usuario original */
144 int mk_user_undo_uidgid()