mod_unixd.c revision 4a13940dc2990df0a798718d3a3f9cf1566c2217
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_main.h"
#include "http_log.h"
#include "mpm_common.h"
#include "os.h"
#include "ap_mpm.h"
#include "mod_unixd.h"
#include "apr_thread_proc.h"
#include "apr_strings.h"
#include "apr_portable.h"
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
/* XXX */
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_GRP_H
#include <grp.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#ifdef HAVE_SYS_SEM_H
#endif
#ifdef HAVE_SYS_PRCTL_H
#endif
#ifndef DEFAULT_USER
#define DEFAULT_USER "#-1"
#endif
#ifndef DEFAULT_GROUP
#define DEFAULT_GROUP "#-1"
#endif
#if 0
typedef struct {
const char *user_name;
const char *chroot_dir;
#else
/*
* TODO: clean up the separation between this code
* and its data structures and unixd.c, as shown
* by the fact that we include unixd.h. Create
* mod_unixd.h which does what we need and
* clean up unixd.h for what it no longer needs
*/
#include "unixd.h"
#endif
/* Set group privileges.
*
* Note that we use the username as set in the config files, rather than
* the lookup of to uid --- the same uid may have multiple passwd entries,
* with different sets of groups for each.
*/
static int set_group_privs(void)
{
if (!geteuid()) {
const char *name;
/* Get username if passed as a uid */
"getpwuid: couldn't determine user name from uid %ld, "
"you probably need to modify the User directive",
(long)uid);
return -1;
}
}
else
#if !defined(OS2)
/* OS/2 doesn't support groups. */
/*
* Set the GID before initgroups(), since on some platforms
* setgid() is known to zap the group list.
*/
"setgid: unable to set group id to Group %u",
(unsigned)ap_unixd_config.group_id);
return -1;
}
/* Reset `groups' attributes. */
"initgroups: unable to set groups for User %s "
return -1;
}
#endif /* !defined(OS2) */
}
return 0;
}
static int
{
int rv = set_group_privs();
if (rv) {
return rv;
}
if (geteuid()) {
"Cannot chroot when not started as root");
return rv;
}
return rv;
}
return rv;
}
if (chdir("/") != 0) {
"Can't chdir to new root");
return rv;
}
}
/* Only try to switch if we're running as root */
if (!geteuid() && (
#ifdef _OSD_POSIX
#endif
"setuid: unable to change to uid: %ld",
(long) ap_unixd_config.user_id);
return rv;
}
#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
/* this applies to Linux 2.4+ */
if (ap_coredumpdir_configured) {
"set dumpable failed - this child will not coredump"
" after software errors");
return rv;
}
}
#endif
return OK;
}
static const char *
const char *arg)
{
return err;
}
#if !defined (BIG_SECURITY_HOLE) && !defined (OS2)
if (ap_unixd_config.user_id == 0) {
return "Error:\tApache has not been designed to serve pages while\n"
"\trunning as root. There are known race conditions that\n"
"\twill allow any local user to read any file on the system.\n"
"\tIf you still desire to serve pages as root then\n"
"\tadd -DBIG_SECURITY_HOLE to the CFLAGS env variable\n"
"\tand then rebuild the server.\n"
"\tIt is strongly suggested that you instead modify the User\n"
"\tdirective in your httpd.conf file to list a non-root\n"
"\tuser.\n";
}
#endif
return NULL;
}
static const char*
const char *arg)
{
return err;
}
return NULL;
}
static const char*
const char *arg)
{
return err;
}
return "ChrootDir must be a valid directory";
}
return NULL;
}
static int
{
/* Check for suexec */
== APR_SUCCESS) {
}
}
return OK;
}
AP_DECLARE(int) ap_unixd_setup_child(void)
{
if (set_group_privs()) {
return -1;
}
if (geteuid()) {
"Cannot chroot when not started as root");
return -1;
}
return -1;
}
return -1;
}
if (chdir("/") != 0) {
"Can't chdir to new root");
return -1;
}
}
/* Only try to switch if we're running as root */
if (!geteuid() && (
#ifdef _OSD_POSIX
#endif
"setuid: unable to change to uid: %ld",
(long) ap_unixd_config.user_id);
return -1;
}
#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
/* this applies to Linux 2.4+ */
if (ap_coredumpdir_configured) {
"set dumpable failed - this child will not coredump"
" after software errors");
}
}
#endif
return 0;
}
{
}
static const command_rec unixd_cmds[] = {
"Effective user id for this server"),
"Effective group id for this server"),
"The directory to chroot(2) into"),
{NULL}
};
NULL,
NULL,
NULL,
NULL,
};