unixd.c revision fcdbe02c16575d9c8b6e14a52269c48f79e08baa
842ae4bd224140319ae7feec1872b93dfd491143fielding/* ====================================================================
842ae4bd224140319ae7feec1872b93dfd491143fielding * The Apache Software License, Version 1.1
842ae4bd224140319ae7feec1872b93dfd491143fielding * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
842ae4bd224140319ae7feec1872b93dfd491143fielding * reserved.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Redistribution and use in source and binary forms, with or without
04891cf70e0bfc38bfb027541dc821f04c754ff7nd * modification, are permitted provided that the following conditions
04891cf70e0bfc38bfb027541dc821f04c754ff7nd * 1. Redistributions of source code must retain the above copyright
04891cf70e0bfc38bfb027541dc821f04c754ff7nd * notice, this list of conditions and the following disclaimer.
04891cf70e0bfc38bfb027541dc821f04c754ff7nd * 2. Redistributions in binary form must reproduce the above copyright
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * notice, this list of conditions and the following disclaimer in
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * the documentation and/or other materials provided with the
3568de757bac0b47256647504c186d17ca272f85rbb * distribution.
3568de757bac0b47256647504c186d17ca272f85rbb * 3. The end-user documentation included with the redistribution,
3568de757bac0b47256647504c186d17ca272f85rbb * if any, must include the following acknowledgment:
3568de757bac0b47256647504c186d17ca272f85rbb * "This product includes software developed by the
3568de757bac0b47256647504c186d17ca272f85rbb * Apache Software Foundation (http://www.apache.org/)."
3568de757bac0b47256647504c186d17ca272f85rbb * Alternately, this acknowledgment may appear in the software itself,
3568de757bac0b47256647504c186d17ca272f85rbb * if and wherever such third-party acknowledgments normally appear.
3568de757bac0b47256647504c186d17ca272f85rbb * 4. The names "Apache" and "Apache Software Foundation" must
3568de757bac0b47256647504c186d17ca272f85rbb * not be used to endorse or promote products derived from this
3568de757bac0b47256647504c186d17ca272f85rbb * software without prior written permission. For written
3568de757bac0b47256647504c186d17ca272f85rbb * permission, please contact apache@apache.org.
3568de757bac0b47256647504c186d17ca272f85rbb * 5. Products derived from this software may not be called "Apache",
3568de757bac0b47256647504c186d17ca272f85rbb * nor may "Apache" appear in their name, without prior written
3568de757bac0b47256647504c186d17ca272f85rbb * permission of the Apache Software Foundation.
3568de757bac0b47256647504c186d17ca272f85rbb * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
3568de757bac0b47256647504c186d17ca272f85rbb * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
3568de757bac0b47256647504c186d17ca272f85rbb * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3568de757bac0b47256647504c186d17ca272f85rbb * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
3568de757bac0b47256647504c186d17ca272f85rbb * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3568de757bac0b47256647504c186d17ca272f85rbb * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3568de757bac0b47256647504c186d17ca272f85rbb * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
3568de757bac0b47256647504c186d17ca272f85rbb * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
3568de757bac0b47256647504c186d17ca272f85rbb * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3568de757bac0b47256647504c186d17ca272f85rbb * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3568de757bac0b47256647504c186d17ca272f85rbb * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3568de757bac0b47256647504c186d17ca272f85rbb * SUCH DAMAGE.
3568de757bac0b47256647504c186d17ca272f85rbb * ====================================================================
3568de757bac0b47256647504c186d17ca272f85rbb * This software consists of voluntary contributions made by many
3568de757bac0b47256647504c186d17ca272f85rbb * individuals on behalf of the Apache Software Foundation. For more
3568de757bac0b47256647504c186d17ca272f85rbb * information on the Apache Software Foundation, please see
3568de757bac0b47256647504c186d17ca272f85rbb * Portions of this software are based upon public domain software
3568de757bac0b47256647504c186d17ca272f85rbb * originally written at the National Center for Supercomputing Applications,
3568de757bac0b47256647504c186d17ca272f85rbb * University of Illinois, Urbana-Champaign.
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz/* Set group privileges.
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz * Note that we use the username as set in the config files, rather than
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * the lookup of to uid --- the same uid may have multiple passwd entries,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * with different sets of groups for each.
8f3ec4772d2aeb347cf40e87c77627bb784dd018rbbstatic int set_group_privs(void)
b6e310e482c42cc323a28fa6fec653e11e0552e5jorton const char *name;
98fb535f829e2a95aabd82420931f476661fa8e3jorton /* Get username if passed as a uid */
7cd5419264796cfeaf8215383cf0f89130a81fectrawick "getpwuid: couldn't determine user name from uid %u, "
7cd5419264796cfeaf8215383cf0f89130a81fectrawick "you probably need to modify the User directive",
7cd5419264796cfeaf8215383cf0f89130a81fectrawick (unsigned)uid);
3568de757bac0b47256647504c186d17ca272f85rbb /* OS/2 and TPF don't support groups. */
3568de757bac0b47256647504c186d17ca272f85rbb * Set the GID before initgroups(), since on some platforms
3568de757bac0b47256647504c186d17ca272f85rbb * setgid() is known to zap the group list.
936a4025e45887d9f366bf54360c51937b6bcacejim "setgid: unable to set group id to Group %u",
936a4025e45887d9f366bf54360c51937b6bcacejim return -1;
936a4025e45887d9f366bf54360c51937b6bcacejim /* Reset `groups' attributes. */
936a4025e45887d9f366bf54360c51937b6bcacejim "initgroups: unable to set groups for User %s "
3568de757bac0b47256647504c186d17ca272f85rbb return -1;
3568de757bac0b47256647504c186d17ca272f85rbb#endif /* !defined(OS2) && !defined(TPF) */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz /* Only try to switch if we're running as MANAGER.SYS */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz "setuid: unable to change to uid: %ld",
3568de757bac0b47256647504c186d17ca272f85rbb /* Only try to switch if we're running as root */
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz os_init_job_environment(server_conf, unixd_config.user_name, one_process) != 0 ||
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
3568de757bac0b47256647504c186d17ca272f85rbb "setuid: unable to change to uid: %ld",
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantzAP_DECLARE(const char *) unixd_set_user(cmd_parms *cmd, void *dummy,
3568de757bac0b47256647504c186d17ca272f85rbb const char *arg)
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
3568de757bac0b47256647504c186d17ca272f85rbb return "Error:\tApache has not been designed to serve pages while\n"
3568de757bac0b47256647504c186d17ca272f85rbb "\trunning as root. There are known race conditions that\n"
3568de757bac0b47256647504c186d17ca272f85rbb "\twill allow any local user to read any file on the system.\n"
3568de757bac0b47256647504c186d17ca272f85rbb "\tIf you still desire to serve pages as root then\n"
3568de757bac0b47256647504c186d17ca272f85rbb "\tadd -DBIG_SECURITY_HOLE to the EXTRA_CFLAGS line in your\n"
3568de757bac0b47256647504c186d17ca272f85rbb "\tsrc/Configuration file and rebuild the server. It is\n"
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz "\tstrongly suggested that you instead modify the User\n"
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz "\tdirective in your httpd.conf file to list a non-root\n"
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz "\tuser.\n";
3568de757bac0b47256647504c186d17ca272f85rbbAP_DECLARE(const char *) unixd_set_group(cmd_parms *cmd, void *dummy,
3568de757bac0b47256647504c186d17ca272f85rbb const char *arg)
3568de757bac0b47256647504c186d17ca272f85rbb const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingAP_DECLARE(void) unixd_pre_config(apr_pool_t *ptemp)
41634f717c623556a16b27b25d7d909a66fe20f8wrowe /* Check for suexec */
3568de757bac0b47256647504c186d17ca272f85rbb /* XXX - apr_stat is incapable of checking suid bits (grumble) */
6653a33e820463abd4f81915b7a1eba0f602e200brianp /* if ((wrapper.filetype & S_ISUID) && wrapper.user == 0) { */
3568de757bac0b47256647504c186d17ca272f85rbbAP_DECLARE(void) unixd_set_rlimit(cmd_parms *cmd, struct rlimit **plimit,
e8f95a682820a599fe41b22977010636be5c2717jim#if (defined(RLIMIT_CPU) || defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_NPROC) || defined(RLIMIT_AS)) && APR_HAVE_STRUCT_RLIMIT && APR_HAVE_GETRLIMIT
ca53a74f4012a45cbad48e940eddf27d866981f9dougm /* If your platform doesn't define rlim_t then typedef it in ap_config.h */
6653a33e820463abd4f81915b7a1eba0f602e200brianp *plimit = (struct rlimit *)apr_pcalloc(cmd->pool, sizeof(**plimit));
6653a33e820463abd4f81915b7a1eba0f602e200brianp ap_log_error(APLOG_MARK, APLOG_ERR, errno, cmd->server,
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, cmd->server,
12901074f5d6b36d08be84d8637b6f2c21e0da26trawick if (arg2 && (str = ap_getword_conf(cmd->pool, &arg2))) {
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz /* if we aren't running as root, cannot increase max */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, cmd->server,
f2e009134c7e279f99dfca5bd421f721bf1f7840jorton "Must be uid 0 to raise maximum %s", cmd->cmd->name);
9f979f5c8061f6f6f560d1824e0e378ff5b91931rpluem ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, cmd->server,
9f979f5c8061f6f6f560d1824e0e378ff5b91931rpluem "Platform does not support rlimit for %s", cmd->cmd->name);
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddardAP_IMPLEMENT_HOOK_RUN_FIRST(ap_unix_identity_t *, get_suexec_identity,
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddardstatic apr_status_t ap_unix_create_privileged_process(
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard const char * const *args,
f2e009134c7e279f99dfca5bd421f721bf1f7840jorton const char * const *env,
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantz const char **newargs;
cd8f8c995d415473f3bfb0b329b2450f2a722c3atrawick return apr_proc_create(newproc, progname, args, env, attr, p);
7cd5419264796cfeaf8215383cf0f89130a81fectrawick execuser = apr_psprintf(p, "%ld", (long) ugid->uid);
7cd5419264796cfeaf8215383cf0f89130a81fectrawick execgroup = apr_psprintf(p, "%ld", (long) ugid->gid);
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard while (args[i]) {
5a0f707b48da7703cbe6bc087f13a6735b1c742dgregames } while (args[i++]);
c0659e61002e9d6ff77b2dca72540e0af1b2ca64stoddard return apr_proc_create(newproc, newprogname, newargs, env, attr, p);
28c170ac8e99644de58cad454c6e0f9b4b359be6jerenkrantzAP_DECLARE(apr_status_t) ap_os_create_privileged_process(
7cd5419264796cfeaf8215383cf0f89130a81fectrawick const char * const *args,
7cd5419264796cfeaf8215383cf0f89130a81fectrawick const char * const *env,
7cd5419264796cfeaf8215383cf0f89130a81fectrawick ap_unix_identity_t *ugid = ap_run_get_suexec_identity(r);
7cd5419264796cfeaf8215383cf0f89130a81fectrawick return apr_proc_create(newproc, progname, args, env, attr, p);