/* 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.
*/
/*
* suexec.c -- "Wrapper" support program for suEXEC behaviour for Apache
*
***********************************************************************
*
* NOTE! : DO NOT edit this code!!! Unless you know what you are doing,
* editing this code might open up your system in unexpected
* ways to would-be crackers. Every precaution has been taken
* to make this code as safe as possible; alter it at your own
* risk.
*
***********************************************************************
*
*
*/
#include "apr.h"
#include "ap_config.h"
#include "suexec.h"
#include <string.h>
#include <time.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#if APR_HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifdef HAVE_GRP_H
#include <grp.h>
#endif
#ifdef AP_LOG_SYSLOG
#include <syslog.h>
#endif
#if defined(PATH_MAX)
#elif defined(MAXPATHLEN)
#else
#endif
extern char **environ;
#ifdef AP_LOG_SYSLOG
/* Syslog support. */
#if !defined(AP_LOG_FACILITY) && defined(LOG_AUTHPRIV)
#elif !defined(AP_LOG_FACILITY)
#endif
static int log_open;
#else
/* Non-syslog support. */
#endif
static const char *const safe_env_lst[] =
{
/* variable name starts with */
"SSL_",
/* "HTTP_" is handled specially in clean_env() */
/* variable name is */
"AUTH_TYPE=",
"CONTENT_LENGTH=",
"CONTENT_TYPE=",
"CONTEXT_DOCUMENT_ROOT=",
"CONTEXT_PREFIX=",
"DATE_GMT=",
"DATE_LOCAL=",
"DOCUMENT_NAME=",
"DOCUMENT_PATH_INFO=",
"DOCUMENT_ROOT=",
"DOCUMENT_URI=",
"GATEWAY_INTERFACE=",
"HTTPS=",
"LAST_MODIFIED=",
"PATH_INFO=",
"PATH_TRANSLATED=",
"QUERY_STRING=",
"QUERY_STRING_UNESCAPED=",
"REMOTE_ADDR=",
"REMOTE_HOST=",
"REMOTE_IDENT=",
"REMOTE_PORT=",
"REMOTE_USER=",
"REDIRECT_ERROR_NOTES=",
"REDIRECT_HANDLER=",
"REDIRECT_QUERY_STRING=",
"REDIRECT_REMOTE_USER=",
"REDIRECT_SCRIPT_FILENAME=",
"REDIRECT_STATUS=",
"REDIRECT_URL=",
"REQUEST_METHOD=",
"REQUEST_URI=",
"REQUEST_SCHEME=",
"SCRIPT_FILENAME=",
"SCRIPT_NAME=",
"SCRIPT_URI=",
"SCRIPT_URL=",
"SERVER_ADMIN=",
"SERVER_NAME=",
"SERVER_ADDR=",
"SERVER_PORT=",
"SERVER_PROTOCOL=",
"SERVER_SIGNATURE=",
"SERVER_SOFTWARE=",
"UNIQUE_ID=",
"USER_NAME=",
"TZ=",
};
static void log_no_err(const char *fmt,...)
{
#if defined(AP_LOG_SYSLOG)
if (!log_open) {
log_open = 1;
}
#elif defined(AP_LOG_EXEC)
if (!log) {
#if defined(_LARGEFILE64_SOURCE) && HAVE_FOPEN64
#else
#endif
perror("fopen");
exit(1);
}
}
if (is_error) {
"details\n");
}
#endif /* AP_LOG_EXEC */
return;
}
{
#ifdef AP_LOG_EXEC
#endif /* AP_LOG_EXEC */
return;
}
{
#ifdef AP_LOG_EXEC
#endif /* AP_LOG_EXEC */
return;
}
static void clean_env(void)
{
char **cleanenv;
char **ep;
int cidx = 0;
int idx;
/* While cleaning the environment, the environment should be clean.
* (e.g. malloc() may get the name of a file for writing debugging info.
* susceptible to bad locale settings....)
* (from PR 2790)
*/
log_err("failed to malloc memory for environment\n");
exit(123);
}
log_err("failed to malloc memory for environment\n");
exit(124);
}
cidx++;
/*
* HTTP_PROXY is treated as alias for http_proxy by some
* programs.
*/
}
else {
/* Other HTTP_* are safe */
cidx++;
}
continue;
}
cidx++;
break;
}
}
}
}
{
/*
* Start with a "clean" environment
*/
clean_env();
/*
* running this program. Error out if invalid.
*/
exit(102);
}
/*
* See if this is a 'how were you compiled' request, and
* comply if so.
*/
if ((argc > 1)
&& ((uid == 0)
#ifdef _OSD_POSIX
#else /* _OSD_POSIX */
#endif /* _OSD_POSIX */
) {
#ifdef AP_DOC_ROOT
#endif
#ifdef AP_GID_MIN
#endif
#ifdef AP_HTTPD_USER
#endif
#if defined(AP_LOG_SYSLOG)
#elif defined(AP_LOG_EXEC)
#endif
#ifdef AP_SAFE_PATH
#endif
#ifdef AP_SUEXEC_UMASK
#endif
#ifdef AP_UID_MIN
#endif
#ifdef AP_USERDIR_SUFFIX
#endif
exit(0);
}
/*
* If there are a proper number of arguments, set
* all of them to variables. Otherwise, error out.
*/
if (argc < 4) {
log_err("too few arguments\n");
exit(101);
}
/*
* Check to see if the user running this program
* is the user allowed to do so as defined in
* suexec.h. If not the allowed user, error out.
*/
#ifdef _OSD_POSIX
exit(103);
}
#else /*_OSD_POSIX*/
exit(103);
}
#endif /*_OSD_POSIX*/
/*
* Check for a leading '/' (absolute path) in the command to be executed,
* or attempts to back up out of the current directory,
* to protect against attacks. If any are
* found, error out. Naughty naughty crackers.
*/
exit(104);
}
/*
* Check to see if this is a ~userdir request. If
* so, set the flag, and remove the '~' from the
* target username.
*/
target_uname++;
userdir = 1;
}
/*
* Error out if the target username is invalid.
*/
exit(105);
}
}
else {
exit(121);
}
}
/*
* Error out if the target group name is invalid.
*/
exit(106);
}
}
else {
exit(106);
}
}
log_err("failed to alloc memory\n");
exit(125);
}
#ifdef _OSD_POSIX
/*
* Initialize BS2000 user environment
*/
{
int status;
case -1: /* Error */
log_err("failed to setup bs2000 environment for user %s: %s\n",
exit(150);
case 0: /* Child */
break;
default: /* Father */
;
/* @@@ FIXME: should we deal with STOP signals as well? */
if (WIFSIGNALED(status)) {
}
}
}
#endif /*_OSD_POSIX*/
/*
* Save these for later since initgroups will hose the struct
*/
log_err("failed to alloc memory\n");
exit(126);
}
/*
* Log the transaction here to be sure we have an open log
* before we setuid().
*/
log_no_err("uid: (%s/%s) gid: (%s/%s) cmd: %s\n",
cmd);
/*
* Error out if attempt is made to execute as root or as
* a UID less than AP_UID_MIN. Tsk tsk.
*/
exit(107);
}
/*
* Error out if attempt is made to execute as root group
* or as a GID less than AP_GID_MIN. Tsk tsk.
*/
exit(108);
}
/*
*
* Initialize the group access list for the target user,
* and setgid() to the target group. If unsuccessful, error out.
*/
exit(109);
}
/*
* setuid() to the target user. Error out on fail.
*/
exit(110);
}
/*
* Get the current working directory, as well as the proper
* document root (dependant upon whether or not it is a
* ~userdir request). Error out if we cannot get either one,
* or if the current working directory is not in the docroot.
* Use chdir()s and getcwd()s to avoid problems with symlinked
* directories. Yuck.
*/
log_err("cannot get current working directory\n");
exit(111);
}
if (userdir) {
if (((chdir(target_homedir)) != 0) ||
((chdir(AP_USERDIR_SUFFIX)) != 0) ||
exit(112);
}
}
else {
if (((chdir(AP_DOC_ROOT)) != 0) ||
exit(113);
}
}
exit(114);
}
/*
* Stat the cwd and verify it is a directory, or error out.
*/
exit(115);
}
/*
* Error out if cwd is writable by others.
*/
exit(116);
}
/*
* Error out if we cannot stat the program.
*/
exit(117);
}
/*
* Error out if the program is writable by others.
*/
exit(118);
}
/*
* Error out if the file is setuid or setgid.
*/
exit(119);
}
/*
*/
"with directory (%lu/%lu) or program (%lu/%lu)\n",
exit(120);
}
/*
* Error out if the program is not executable for the user.
* Otherwise, she won't find any error in the logs except for
* "[error] Premature end of script headers: ..."
*/
exit(121);
}
#ifdef AP_SUEXEC_UMASK
/*
* umask() uses inverse logic; bits are CLEAR for allowed access.
*/
if ((~AP_SUEXEC_UMASK) & 0022) {
log_err("notice: AP_SUEXEC_UMASK of %03o allows "
"write permission to group and/or other\n", AP_SUEXEC_UMASK);
}
#endif /* AP_SUEXEC_UMASK */
/* Be sure to close the log file so the CGI can't mess with it. */
#ifdef AP_LOG_SYSLOG
if (log_open) {
closelog();
log_open = 0;
}
#else
#if APR_HAVE_FCNTL_H
/*
* ask fcntl(2) to set the FD_CLOEXEC flag on the log file,
* so it'll be automagically closed if the exec() call succeeds.
*/
log_err("error: can't set close-on-exec flag");
exit(122);
}
#else
/*
* In this case, exec() errors won't be logged because we have already
* dropped privileges and won't be able to reopen the log file.
*/
#endif
}
#endif
/*
* Execute the command, replacing our image with its own.
*/
#ifdef NEED_HASHBANG_EMUL
/* We need the #! emulation when we want to execute scripts */
{
extern char **environ;
}
#else /*NEED_HASHBANG_EMUL*/
#endif /*NEED_HASHBANG_EMUL*/
/*
* (I can't help myself...sorry.)
*
* Uh oh. Still here. Where's the kaboom? There was supposed to be an
* EARTH-shattering kaboom!
*
* Oh well, log the failure and error out.
*/
exit(255);
}