unixd.c revision e078b71848a5949808244305f451ea0884744842
3802a3d3d7af51ddff31943d5514382f01265770Lennart Poettering/* ====================================================================
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * The Apache Software License, Version 1.1
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * Redistribution and use in source and binary forms, with or without
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * modification, are permitted provided that the following conditions
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * 1. Redistributions of source code must retain the above copyright
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * notice, this list of conditions and the following disclaimer.
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * 2. Redistributions in binary form must reproduce the above copyright
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * notice, this list of conditions and the following disclaimer in
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * the documentation and/or other materials provided with the
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * 3. The end-user documentation included with the redistribution,
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * if any, must include the following acknowledgment:
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * "This product includes software developed by the
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * Apache Software Foundation (http://www.apache.org/)."
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * Alternately, this acknowledgment may appear in the software itself,
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * if and wherever such third-party acknowledgments normally appear.
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * 4. The names "Apache" and "Apache Software Foundation" must
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * not be used to endorse or promote products derived from this
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * software without prior written permission. For written
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * permission, please contact apache@apache.org.
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * 5. Products derived from this software may not be called "Apache",
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * nor may "Apache" appear in their name, without prior written
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * permission of the Apache Software Foundation.
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * ====================================================================
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * This software consists of voluntary contributions made by many
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * individuals on behalf of the Apache Software Foundation. For more
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * information on the Apache Software Foundation, please see
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * Portions of this software are based upon public domain software
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * originally written at the National Center for Supercomputing Applications,
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * University of Illinois, Urbana-Champaign.
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek/* Set group privileges.
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * Note that we use the username as set in the config files, rather than
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * the lookup of to uid --- the same uid may have multiple passwd entries,
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * with different sets of groups for each.
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek /* Get username if passed as a uid */
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek if (unixd_config.user_name[0] == '#') {
b8bde11658366290521e3d03316378b482600323Jan Engelhardt uid_t uid = atoi(&unixd_config.user_name[1]);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
dca348bcbb462305864526c587495a14a76bfcdeJan Engelhardt "getpwuid: couldn't determine user name from uid %u, "
dca348bcbb462305864526c587495a14a76bfcdeJan Engelhardt "you probably need to modify the User directive",
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek#if !defined(OS2) && !defined(TPF)
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek /* OS/2 and TPF don't support groups. */
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * Set the GID before initgroups(), since on some platforms
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek * setgid() is known to zap the group list.
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek "setgid: unable to set group id to Group %u",
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek (unsigned)unixd_config.group_id);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek /* Reset `groups' attributes. */
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek if (initgroups(name, unixd_config.group_id) == -1) {
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek "initgroups: unable to set groups for User %s "
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek "and Group %u", name, (unsigned)unixd_config.group_id);
effbc8e4f70fc70e4a4a8a1dc77228dd187f9c22Zbigniew Jędrzejewski-Szmek#endif /* !defined(OS2) && !defined(TPF) */
if (set_group_privs()) {
#ifdef MPE
GETPRIVMODE();
GETUSERMODE();
GETUSERMODE();
if (!geteuid() && (
#ifdef _OSD_POSIX
#ifdef AP_MPM_WANT_SET_COREDUMPDIR
if (ap_coredumpdir_configured) {
const char *arg)
return err;
"\tdirective in your httpd.conf file to list a non-root\n"
return NULL;
const char *arg)
return err;
return NULL;
#if (defined(RLIMIT_CPU) || defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_NPROC) || defined(RLIMIT_AS)) && APR_HAVE_STRUCT_RLIMIT && APR_HAVE_GETRLIMIT
char *str;
/* If your platform doesn't define rlim_t then typedef it in ap_config.h */
if (geteuid()) {
if (max) {
if (cur) {
if (max) {
const char * const *args,
const char * const *env,
apr_pool_t *p)
const char **newargs;
char *newprogname;
const char *argv0;
argv0++;
return APR_ENOMEM;
if (args) {
while (args[i]) {
return APR_EGENERAL;
} while (args[i++]);
const request_rec *r,
const char * const *args,
const char * const *env,
return APR_LOCK_SYSVSEM;
return APR_LOCK_FLOCK;
return APR_LOCK_DEFAULT;
if (!geteuid()) {
switch(mech) {
case APR_LOCK_SYSVSEM:
#if !APR_HAVE_UNION_SEMUN
union semun {
long val;
return errno;
case APR_LOCK_FLOCK:
if (lockfile) {
return errno;
return APR_SUCCESS;
int sockdes;
return APR_EINTR;
#ifdef TPF
return APR_EINTR;
return status;
return status;
switch (status) {
case ENOBUFS:
#ifdef EPROTO
case EPROTO:
#ifdef ECONNABORTED
case ECONNABORTED:
#ifdef ECONNRESET
case ECONNRESET:
#ifdef ETIMEDOUT
case ETIMEDOUT:
#ifdef EHOSTUNREACH
case EHOSTUNREACH:
#ifdef ENETUNREACH
case ENETUNREACH:
#ifdef ENETDOWN
case ENETDOWN:
return APR_EGENERAL;
#ifdef TPF
case EINACT:
return APR_EGENERAL;
return APR_EGENERAL;
return APR_EGENERAL;
return status;