1 /* ====================================================================
2 * Copyright (c) 1998-1999 The Apache Group. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the Apache Group
19 * for use in the Apache HTTP server project (http://www.apache.org/)."
21 * 4. The names "Apache Server" and "Apache Group" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
26 * 5. Products derived from this software may not be called "Apache"
27 * nor may "Apache" appear in their names without prior written
28 * permission of the Apache Group.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the Apache Group
33 * for use in the Apache HTTP server project (http://www.apache.org/)."
35 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Group and was originally based
51 * on public domain software written at the National Center for
52 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
53 * For more information on the Apache Group and the Apache HTTP server
54 * project, please see <http://www.apache.org/>.
59 #include "http_config.h"
60 #include "http_main.h"
64 unixd_config_rec unixd_config;
66 void unixd_detach(void)
72 #if !defined(MPE) && !defined(OS2) && !defined(TPF)
73 /* Don't detach for MPE because child processes can't survive the death of
79 fprintf(stderr, "%s: unable to fork new process\n", ap_server_argv0);
82 RAISE_SIGSTOP(DETACH);
85 if ((pgrp = setsid()) == -1) {
87 fprintf(stderr, "%s: setsid failed\n", ap_server_argv0);
90 #elif defined(NEXT) || defined(NEWSOS)
91 if (setpgrp(0, getpid()) == -1 || (pgrp = getpgrp(0)) == -1) {
93 fprintf(stderr, "%s: setpgrp or getpgrp failed\n", ap_server_argv0);
96 #elif defined(OS2) || defined(TPF)
97 /* OS/2 and TPF don't support process group IDs */
100 /* MPE uses negative pid for process group */
103 if ((pgrp = setpgrp(getpid(), 0)) == -1) {
105 fprintf(stderr, "%s: setpgrp failed\n", ap_server_argv0);
110 /* close out the standard file descriptors */
111 if (freopen("/dev/null", "r", stdin) == NULL) {
112 fprintf(stderr, "%s: unable to replace stdin with /dev/null: %s\n",
113 ap_server_argv0, strerror(errno));
114 /* continue anyhow -- note we can't close out descriptor 0 because we
115 * have nothing to replace it with, and if we didn't have a descriptor
116 * 0 the next file would be created with that value ... leading to
120 if (freopen("/dev/null", "w", stdout) == NULL) {
121 fprintf(stderr, "%s: unable to replace stdout with /dev/null: %s\n",
122 ap_server_argv0, strerror(errno));
124 /* stderr is a tricky one, we really want it to be the error_log,
125 * but we haven't opened that yet. So leave it alone for now and it'll
126 * be reopened moments later.
130 /* Set group privileges.
132 * Note that we use the username as set in the config files, rather than
133 * the lookup of to uid --- the same uid may have multiple passwd entries,
134 * with different sets of groups for each.
137 static int set_group_privs(void)
142 /* Get username if passed as a uid */
144 if (unixd_config.user_name[0] == '#') {
146 uid_t uid = atoi(&unixd_config.user_name[1]);
148 if ((ent = getpwuid(uid)) == NULL) {
149 ap_log_error(APLOG_MARK, APLOG_ALERT, NULL,
150 "getpwuid: couldn't determine user name from uid %u, "
151 "you probably need to modify the User directive",
159 name = unixd_config.user_name;
161 #if !defined(OS2) && !defined(TPF)
162 /* OS/2 and TPF don't support groups. */
165 * Set the GID before initgroups(), since on some platforms
166 * setgid() is known to zap the group list.
168 if (setgid(unixd_config.group_id) == -1) {
169 ap_log_error(APLOG_MARK, APLOG_ALERT, NULL,
170 "setgid: unable to set group id to Group %u",
171 (unsigned)unixd_config.group_id);
175 /* Reset `groups' attributes. */
177 if (initgroups(name, unixd_config.group_id) == -1) {
178 ap_log_error(APLOG_MARK, APLOG_ALERT, NULL,
179 "initgroups: unable to set groups for User %s "
180 "and Group %u", name, (unsigned)unixd_config.group_id);
183 #endif /* !defined(OS2) && !defined(TPF) */
189 int unixd_setup_child(void)
191 if (set_group_privs()) {
195 /* Only try to switch if we're running as MANAGER.SYS */
196 if (geteuid() == 1 && unixd_config.user_id > 1) {
198 if (setuid(unixd_config.user_id) == -1) {
200 ap_log_error(APLOG_MARK, APLOG_ALERT, NULL,
201 "setuid: unable to change uid");
207 /* Only try to switch if we're running as root */
210 os_init_job_environment(server_conf, unixd_config.user_name, one_process) != 0 ||
212 setuid(unixd_config.user_id) == -1)) {
213 ap_log_error(APLOG_MARK, APLOG_ALERT, NULL,
214 "setuid: unable to change uid");
222 const char *unixd_set_user(cmd_parms *cmd, void *dummy, char *arg)
224 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
229 unixd_config.user_name = arg;
230 unixd_config.user_id = ap_uname2id(arg);
231 #if !defined (BIG_SECURITY_HOLE) && !defined (OS2)
232 if (unixd_config.user_id == 0) {
233 return "Error:\tApache has not been designed to serve pages while\n"
234 "\trunning as root. There are known race conditions that\n"
235 "\twill allow any local user to read any file on the system.\n"
236 "\tIf you still desire to serve pages as root then\n"
237 "\tadd -DBIG_SECURITY_HOLE to the EXTRA_CFLAGS line in your\n"
238 "\tsrc/Configuration file and rebuild the server. It is\n"
239 "\tstrongly suggested that you instead modify the User\n"
240 "\tdirective in your httpd.conf file to list a non-root\n"
248 const char *unixd_set_group(cmd_parms *cmd, void *dummy, char *arg)
250 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
255 unixd_config.group_id = ap_gname2id(arg);
260 void unixd_pre_config(void)
262 unixd_config.user_name = DEFAULT_USER;
263 unixd_config.user_id = ap_uname2id(DEFAULT_USER);
264 unixd_config.group_id = ap_gname2id(DEFAULT_GROUP);