|
Packit |
90a5c9 |
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
Packit |
90a5c9 |
* contributor license agreements. See the NOTICE file distributed with
|
|
Packit |
90a5c9 |
* this work for additional information regarding copyright ownership.
|
|
Packit |
90a5c9 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
Packit |
90a5c9 |
* (the "License"); you may not use this file except in compliance with
|
|
Packit |
90a5c9 |
* the License. You may obtain a copy of the License at
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Unless required by applicable law or agreed to in writing, software
|
|
Packit |
90a5c9 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
Packit |
90a5c9 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
Packit |
90a5c9 |
* See the License for the specific language governing permissions and
|
|
Packit |
90a5c9 |
* limitations under the License.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* suexec.c -- "Wrapper" support program for suEXEC behaviour for Apache
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
***********************************************************************
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* NOTE! : DO NOT edit this code!!! Unless you know what you are doing,
|
|
Packit |
90a5c9 |
* editing this code might open up your system in unexpected
|
|
Packit |
90a5c9 |
* ways to would-be crackers. Every precaution has been taken
|
|
Packit |
90a5c9 |
* to make this code as safe as possible; alter it at your own
|
|
Packit |
90a5c9 |
* risk.
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
***********************************************************************
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "apr.h"
|
|
Packit |
90a5c9 |
#include "ap_config.h"
|
|
Packit |
90a5c9 |
#include "suexec.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include <sys/param.h>
|
|
Packit |
90a5c9 |
#include <sys/stat.h>
|
|
Packit |
90a5c9 |
#include <sys/types.h>
|
|
Packit |
90a5c9 |
#include <string.h>
|
|
Packit |
90a5c9 |
#include <time.h>
|
|
Packit |
90a5c9 |
#if APR_HAVE_UNISTD_H
|
|
Packit |
90a5c9 |
#include <unistd.h>
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include <stdio.h>
|
|
Packit |
90a5c9 |
#include <stdarg.h>
|
|
Packit |
90a5c9 |
#include <stdlib.h>
|
|
Packit |
90a5c9 |
#if APR_HAVE_FCNTL_H
|
|
Packit |
90a5c9 |
#include <fcntl.h>
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#ifdef HAVE_PWD_H
|
|
Packit |
90a5c9 |
#include <pwd.h>
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#ifdef HAVE_GRP_H
|
|
Packit |
90a5c9 |
#include <grp.h>
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#ifdef AP_LOG_SYSLOG
|
|
Packit |
90a5c9 |
#include <syslog.h>
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#if defined(PATH_MAX)
|
|
Packit |
90a5c9 |
#define AP_MAXPATH PATH_MAX
|
|
Packit |
90a5c9 |
#elif defined(MAXPATHLEN)
|
|
Packit |
90a5c9 |
#define AP_MAXPATH MAXPATHLEN
|
|
Packit |
90a5c9 |
#else
|
|
Packit |
90a5c9 |
#define AP_MAXPATH 8192
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#define AP_ENVBUF 256
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
extern char **environ;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#ifdef AP_LOG_SYSLOG
|
|
Packit |
90a5c9 |
/* Syslog support. */
|
|
Packit |
90a5c9 |
#if !defined(AP_LOG_FACILITY) && defined(LOG_AUTHPRIV)
|
|
Packit |
90a5c9 |
#define AP_LOG_FACILITY LOG_AUTHPRIV
|
|
Packit |
90a5c9 |
#elif !defined(AP_LOG_FACILITY)
|
|
Packit |
90a5c9 |
#define AP_LOG_FACILITY LOG_AUTH
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int log_open;
|
|
Packit |
90a5c9 |
#else
|
|
Packit |
90a5c9 |
/* Non-syslog support. */
|
|
Packit |
90a5c9 |
static FILE *log = NULL;
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *const safe_env_lst[] =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
/* variable name starts with */
|
|
Packit |
90a5c9 |
"HTTP_",
|
|
Packit |
90a5c9 |
"SSL_",
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* variable name is */
|
|
Packit |
90a5c9 |
"AUTH_TYPE=",
|
|
Packit |
90a5c9 |
"CONTENT_LENGTH=",
|
|
Packit |
90a5c9 |
"CONTENT_TYPE=",
|
|
Packit |
90a5c9 |
"CONTEXT_DOCUMENT_ROOT=",
|
|
Packit |
90a5c9 |
"CONTEXT_PREFIX=",
|
|
Packit |
90a5c9 |
"DATE_GMT=",
|
|
Packit |
90a5c9 |
"DATE_LOCAL=",
|
|
Packit |
90a5c9 |
"DOCUMENT_ARGS=",
|
|
Packit |
90a5c9 |
"DOCUMENT_NAME=",
|
|
Packit |
90a5c9 |
"DOCUMENT_PATH_INFO=",
|
|
Packit |
90a5c9 |
"DOCUMENT_ROOT=",
|
|
Packit |
90a5c9 |
"DOCUMENT_URI=",
|
|
Packit |
90a5c9 |
"GATEWAY_INTERFACE=",
|
|
Packit |
90a5c9 |
"HTTPS=",
|
|
Packit |
90a5c9 |
"LAST_MODIFIED=",
|
|
Packit |
90a5c9 |
"PATH_INFO=",
|
|
Packit |
90a5c9 |
"PATH_TRANSLATED=",
|
|
Packit |
90a5c9 |
"QUERY_STRING=",
|
|
Packit |
90a5c9 |
"QUERY_STRING_UNESCAPED=",
|
|
Packit |
90a5c9 |
"REMOTE_ADDR=",
|
|
Packit |
90a5c9 |
"REMOTE_HOST=",
|
|
Packit |
90a5c9 |
"REMOTE_IDENT=",
|
|
Packit |
90a5c9 |
"REMOTE_PORT=",
|
|
Packit |
90a5c9 |
"REMOTE_USER=",
|
|
Packit |
90a5c9 |
"REDIRECT_ERROR_NOTES=",
|
|
Packit |
90a5c9 |
"REDIRECT_HANDLER=",
|
|
Packit |
90a5c9 |
"REDIRECT_QUERY_STRING=",
|
|
Packit |
90a5c9 |
"REDIRECT_REMOTE_USER=",
|
|
Packit |
90a5c9 |
"REDIRECT_SCRIPT_FILENAME=",
|
|
Packit |
90a5c9 |
"REDIRECT_STATUS=",
|
|
Packit |
90a5c9 |
"REDIRECT_URL=",
|
|
Packit |
90a5c9 |
"REQUEST_METHOD=",
|
|
Packit |
90a5c9 |
"REQUEST_URI=",
|
|
Packit |
90a5c9 |
"REQUEST_SCHEME=",
|
|
Packit |
90a5c9 |
"SCRIPT_FILENAME=",
|
|
Packit |
90a5c9 |
"SCRIPT_NAME=",
|
|
Packit |
90a5c9 |
"SCRIPT_URI=",
|
|
Packit |
90a5c9 |
"SCRIPT_URL=",
|
|
Packit |
90a5c9 |
"SERVER_ADMIN=",
|
|
Packit |
90a5c9 |
"SERVER_NAME=",
|
|
Packit |
90a5c9 |
"SERVER_ADDR=",
|
|
Packit |
90a5c9 |
"SERVER_PORT=",
|
|
Packit |
90a5c9 |
"SERVER_PROTOCOL=",
|
|
Packit |
90a5c9 |
"SERVER_SIGNATURE=",
|
|
Packit |
90a5c9 |
"SERVER_SOFTWARE=",
|
|
Packit |
90a5c9 |
"UNIQUE_ID=",
|
|
Packit |
90a5c9 |
"USER_NAME=",
|
|
Packit |
90a5c9 |
"TZ=",
|
|
Packit |
90a5c9 |
NULL
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void log_err(const char *fmt,...)
|
|
Packit |
90a5c9 |
__attribute__((format(printf,1,2)));
|
|
Packit |
90a5c9 |
static void log_no_err(const char *fmt,...)
|
|
Packit |
90a5c9 |
__attribute__((format(printf,1,2)));
|
|
Packit |
90a5c9 |
static void err_output(int is_error, const char *fmt, va_list ap)
|
|
Packit |
90a5c9 |
__attribute__((format(printf,2,0)));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void err_output(int is_error, const char *fmt, va_list ap)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
#if defined(AP_LOG_SYSLOG)
|
|
Packit |
90a5c9 |
if (!log_open) {
|
|
Packit |
90a5c9 |
openlog("suexec", LOG_PID, AP_LOG_FACILITY);
|
|
Packit |
90a5c9 |
log_open = 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
vsyslog(is_error ? LOG_ERR : LOG_INFO, fmt, ap);
|
|
Packit |
90a5c9 |
#elif defined(AP_LOG_EXEC)
|
|
Packit |
90a5c9 |
time_t timevar;
|
|
Packit |
90a5c9 |
struct tm *lt;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!log) {
|
|
Packit |
90a5c9 |
#if defined(_LARGEFILE64_SOURCE) && HAVE_FOPEN64
|
|
Packit |
90a5c9 |
if ((log = fopen64(AP_LOG_EXEC, "a")) == NULL) {
|
|
Packit |
90a5c9 |
#else
|
|
Packit |
90a5c9 |
if ((log = fopen(AP_LOG_EXEC, "a")) == NULL) {
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
fprintf(stderr, "suexec failure: could not open log file\n");
|
|
Packit |
90a5c9 |
perror("fopen");
|
|
Packit |
90a5c9 |
exit(1);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (is_error) {
|
|
Packit |
90a5c9 |
fprintf(stderr, "suexec policy violation: see suexec log for more "
|
|
Packit |
90a5c9 |
"details\n");
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
time(&timevar);
|
|
Packit |
90a5c9 |
lt = localtime(&timevar);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
fprintf(log, "[%d-%.2d-%.2d %.2d:%.2d:%.2d]: ",
|
|
Packit |
90a5c9 |
lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
|
|
Packit |
90a5c9 |
lt->tm_hour, lt->tm_min, lt->tm_sec);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
vfprintf(log, fmt, ap);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
fflush(log);
|
|
Packit |
90a5c9 |
#endif /* AP_LOG_EXEC */
|
|
Packit |
90a5c9 |
return;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void log_err(const char *fmt,...)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
#ifdef AP_LOG_EXEC
|
|
Packit |
90a5c9 |
va_list ap;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
va_start(ap, fmt);
|
|
Packit |
90a5c9 |
err_output(1, fmt, ap); /* 1 == is_error */
|
|
Packit |
90a5c9 |
va_end(ap);
|
|
Packit |
90a5c9 |
#endif /* AP_LOG_EXEC */
|
|
Packit |
90a5c9 |
return;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void log_no_err(const char *fmt,...)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
#ifdef AP_LOG_EXEC
|
|
Packit |
90a5c9 |
va_list ap;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
va_start(ap, fmt);
|
|
Packit |
90a5c9 |
err_output(0, fmt, ap); /* 0 == !is_error */
|
|
Packit |
90a5c9 |
va_end(ap);
|
|
Packit |
90a5c9 |
#endif /* AP_LOG_EXEC */
|
|
Packit |
90a5c9 |
return;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void clean_env(void)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
char pathbuf[512];
|
|
Packit |
90a5c9 |
char **cleanenv;
|
|
Packit |
90a5c9 |
char **ep;
|
|
Packit |
90a5c9 |
int cidx = 0;
|
|
Packit |
90a5c9 |
int idx;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* While cleaning the environment, the environment should be clean.
|
|
Packit |
90a5c9 |
* (e.g. malloc() may get the name of a file for writing debugging info.
|
|
Packit |
90a5c9 |
* Bad news if MALLOC_DEBUG_FILE is set to /etc/passwd. Sprintf() may be
|
|
Packit |
90a5c9 |
* susceptible to bad locale settings....)
|
|
Packit |
90a5c9 |
* (from PR 2790)
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
char **envp = environ;
|
|
Packit |
90a5c9 |
char *empty_ptr = NULL;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
environ = &empty_ptr; /* VERY safe environment */
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if ((cleanenv = (char **) calloc(AP_ENVBUF, sizeof(char *))) == NULL) {
|
|
Packit |
90a5c9 |
log_err("failed to malloc memory for environment\n");
|
|
Packit |
90a5c9 |
exit(123);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
sprintf(pathbuf, "PATH=%s", AP_SAFE_PATH);
|
|
Packit |
90a5c9 |
cleanenv[cidx] = strdup(pathbuf);
|
|
Packit |
90a5c9 |
if (cleanenv[cidx] == NULL) {
|
|
Packit |
90a5c9 |
log_err("failed to malloc memory for environment\n");
|
|
Packit |
90a5c9 |
exit(124);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
cidx++;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
for (ep = envp; *ep && cidx < AP_ENVBUF-1; ep++) {
|
|
Packit |
90a5c9 |
for (idx = 0; safe_env_lst[idx]; idx++) {
|
|
Packit |
90a5c9 |
if (!strncmp(*ep, safe_env_lst[idx],
|
|
Packit |
90a5c9 |
strlen(safe_env_lst[idx]))) {
|
|
Packit |
90a5c9 |
cleanenv[cidx] = *ep;
|
|
Packit |
90a5c9 |
cidx++;
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
cleanenv[cidx] = NULL;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
environ = cleanenv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
int main(int argc, char *argv[])
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
int userdir = 0; /* ~userdir flag */
|
|
Packit |
90a5c9 |
uid_t uid; /* user information */
|
|
Packit |
90a5c9 |
gid_t gid; /* target group placeholder */
|
|
Packit |
90a5c9 |
char *target_uname; /* target user name */
|
|
Packit |
90a5c9 |
char *target_gname; /* target group name */
|
|
Packit |
90a5c9 |
char *target_homedir; /* target home directory */
|
|
Packit |
90a5c9 |
char *actual_uname; /* actual user name */
|
|
Packit |
90a5c9 |
char *actual_gname; /* actual group name */
|
|
Packit |
90a5c9 |
char *cmd; /* command to be executed */
|
|
Packit |
90a5c9 |
char cwd[AP_MAXPATH]; /* current working directory */
|
|
Packit |
90a5c9 |
char dwd[AP_MAXPATH]; /* docroot working directory */
|
|
Packit |
90a5c9 |
struct passwd *pw; /* password entry holder */
|
|
Packit |
90a5c9 |
struct group *gr; /* group entry holder */
|
|
Packit |
90a5c9 |
struct stat dir_info; /* directory info holder */
|
|
Packit |
90a5c9 |
struct stat prg_info; /* program info holder */
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Start with a "clean" environment
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
clean_env();
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Check existence/validity of the UID of the user
|
|
Packit |
90a5c9 |
* running this program. Error out if invalid.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
uid = getuid();
|
|
Packit |
90a5c9 |
if ((pw = getpwuid(uid)) == NULL) {
|
|
Packit |
90a5c9 |
log_err("crit: invalid uid: (%lu)\n", (unsigned long)uid);
|
|
Packit |
90a5c9 |
exit(102);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* See if this is a 'how were you compiled' request, and
|
|
Packit |
90a5c9 |
* comply if so.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if ((argc > 1)
|
|
Packit |
90a5c9 |
&& (! strcmp(argv[1], "-V"))
|
|
Packit |
90a5c9 |
&& ((uid == 0)
|
|
Packit |
90a5c9 |
#ifdef _OSD_POSIX
|
|
Packit |
90a5c9 |
/* User name comparisons are case insensitive on BS2000/OSD */
|
|
Packit |
90a5c9 |
|| (! strcasecmp(AP_HTTPD_USER, pw->pw_name)))
|
|
Packit |
90a5c9 |
#else /* _OSD_POSIX */
|
|
Packit |
90a5c9 |
|| (! strcmp(AP_HTTPD_USER, pw->pw_name)))
|
|
Packit |
90a5c9 |
#endif /* _OSD_POSIX */
|
|
Packit |
90a5c9 |
) {
|
|
Packit |
90a5c9 |
#ifdef AP_DOC_ROOT
|
|
Packit |
90a5c9 |
fprintf(stderr, " -D AP_DOC_ROOT=\"%s\"\n", AP_DOC_ROOT);
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
#ifdef AP_GID_MIN
|
|
Packit |
90a5c9 |
fprintf(stderr, " -D AP_GID_MIN=%d\n", AP_GID_MIN);
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
#ifdef AP_HTTPD_USER
|
|
Packit |
90a5c9 |
fprintf(stderr, " -D AP_HTTPD_USER=\"%s\"\n", AP_HTTPD_USER);
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
#if defined(AP_LOG_SYSLOG)
|
|
Packit |
90a5c9 |
fprintf(stderr, " -D AP_LOG_SYSLOG\n");
|
|
Packit |
90a5c9 |
#elif defined(AP_LOG_EXEC)
|
|
Packit |
90a5c9 |
fprintf(stderr, " -D AP_LOG_EXEC=\"%s\"\n", AP_LOG_EXEC);
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
#ifdef AP_SAFE_PATH
|
|
Packit |
90a5c9 |
fprintf(stderr, " -D AP_SAFE_PATH=\"%s\"\n", AP_SAFE_PATH);
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
#ifdef AP_SUEXEC_UMASK
|
|
Packit |
90a5c9 |
fprintf(stderr, " -D AP_SUEXEC_UMASK=%03o\n", AP_SUEXEC_UMASK);
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
#ifdef AP_UID_MIN
|
|
Packit |
90a5c9 |
fprintf(stderr, " -D AP_UID_MIN=%d\n", AP_UID_MIN);
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
#ifdef AP_USERDIR_SUFFIX
|
|
Packit |
90a5c9 |
fprintf(stderr, " -D AP_USERDIR_SUFFIX=\"%s\"\n", AP_USERDIR_SUFFIX);
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
exit(0);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* If there are a proper number of arguments, set
|
|
Packit |
90a5c9 |
* all of them to variables. Otherwise, error out.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (argc < 4) {
|
|
Packit |
90a5c9 |
log_err("too few arguments\n");
|
|
Packit |
90a5c9 |
exit(101);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
target_uname = argv[1];
|
|
Packit |
90a5c9 |
target_gname = argv[2];
|
|
Packit |
90a5c9 |
cmd = argv[3];
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Check to see if the user running this program
|
|
Packit |
90a5c9 |
* is the user allowed to do so as defined in
|
|
Packit |
90a5c9 |
* suexec.h. If not the allowed user, error out.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
#ifdef _OSD_POSIX
|
|
Packit |
90a5c9 |
/* User name comparisons are case insensitive on BS2000/OSD */
|
|
Packit |
90a5c9 |
if (strcasecmp(AP_HTTPD_USER, pw->pw_name)) {
|
|
Packit |
90a5c9 |
log_err("user mismatch (%s instead of %s)\n", pw->pw_name, AP_HTTPD_USER);
|
|
Packit |
90a5c9 |
exit(103);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
#else /*_OSD_POSIX*/
|
|
Packit |
90a5c9 |
if (strcmp(AP_HTTPD_USER, pw->pw_name)) {
|
|
Packit |
90a5c9 |
log_err("user mismatch (%s instead of %s)\n", pw->pw_name, AP_HTTPD_USER);
|
|
Packit |
90a5c9 |
exit(103);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
#endif /*_OSD_POSIX*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Check for a leading '/' (absolute path) in the command to be executed,
|
|
Packit |
90a5c9 |
* or attempts to back up out of the current directory,
|
|
Packit |
90a5c9 |
* to protect against attacks. If any are
|
|
Packit |
90a5c9 |
* found, error out. Naughty naughty crackers.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if ((cmd[0] == '/') || (!strncmp(cmd, "../", 3))
|
|
Packit |
90a5c9 |
|| (strstr(cmd, "/../") != NULL)) {
|
|
Packit |
90a5c9 |
log_err("invalid command (%s)\n", cmd);
|
|
Packit |
90a5c9 |
exit(104);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Check to see if this is a ~userdir request. If
|
|
Packit |
90a5c9 |
* so, set the flag, and remove the '~' from the
|
|
Packit |
90a5c9 |
* target username.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (!strncmp("~", target_uname, 1)) {
|
|
Packit |
90a5c9 |
target_uname++;
|
|
Packit |
90a5c9 |
userdir = 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Error out if the target username is invalid.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (strspn(target_uname, "1234567890") != strlen(target_uname)) {
|
|
Packit |
90a5c9 |
if ((pw = getpwnam(target_uname)) == NULL) {
|
|
Packit |
90a5c9 |
log_err("invalid target user name: (%s)\n", target_uname);
|
|
Packit |
90a5c9 |
exit(105);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
if ((pw = getpwuid(atoi(target_uname))) == NULL) {
|
|
Packit |
90a5c9 |
log_err("invalid target user id: (%s)\n", target_uname);
|
|
Packit |
90a5c9 |
exit(121);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Error out if the target group name is invalid.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (strspn(target_gname, "1234567890") != strlen(target_gname)) {
|
|
Packit |
90a5c9 |
if ((gr = getgrnam(target_gname)) == NULL) {
|
|
Packit |
90a5c9 |
log_err("invalid target group name: (%s)\n", target_gname);
|
|
Packit |
90a5c9 |
exit(106);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
if ((gr = getgrgid(atoi(target_gname))) == NULL) {
|
|
Packit |
90a5c9 |
log_err("invalid target group id: (%s)\n", target_gname);
|
|
Packit |
90a5c9 |
exit(106);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
gid = gr->gr_gid;
|
|
Packit |
90a5c9 |
if ((actual_gname = strdup(gr->gr_name)) == NULL) {
|
|
Packit |
90a5c9 |
log_err("failed to alloc memory\n");
|
|
Packit |
90a5c9 |
exit(125);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#ifdef _OSD_POSIX
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Initialize BS2000 user environment
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
pid_t pid;
|
|
Packit |
90a5c9 |
int status;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
switch (pid = ufork(target_uname)) {
|
|
Packit |
90a5c9 |
case -1: /* Error */
|
|
Packit |
90a5c9 |
log_err("failed to setup bs2000 environment for user %s: %s\n",
|
|
Packit |
90a5c9 |
target_uname, strerror(errno));
|
|
Packit |
90a5c9 |
exit(150);
|
|
Packit |
90a5c9 |
case 0: /* Child */
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
default: /* Father */
|
|
Packit |
90a5c9 |
while (pid != waitpid(pid, &status, 0))
|
|
Packit |
90a5c9 |
;
|
|
Packit |
90a5c9 |
/* @@@ FIXME: should we deal with STOP signals as well? */
|
|
Packit |
90a5c9 |
if (WIFSIGNALED(status)) {
|
|
Packit |
90a5c9 |
kill (getpid(), WTERMSIG(status));
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
exit(WEXITSTATUS(status));
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
#endif /*_OSD_POSIX*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Save these for later since initgroups will hose the struct
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
uid = pw->pw_uid;
|
|
Packit |
90a5c9 |
actual_uname = strdup(pw->pw_name);
|
|
Packit |
90a5c9 |
target_homedir = strdup(pw->pw_dir);
|
|
Packit |
90a5c9 |
if (actual_uname == NULL || target_homedir == NULL) {
|
|
Packit |
90a5c9 |
log_err("failed to alloc memory\n");
|
|
Packit |
90a5c9 |
exit(126);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Log the transaction here to be sure we have an open log
|
|
Packit |
90a5c9 |
* before we setuid().
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
log_no_err("uid: (%s/%s) gid: (%s/%s) cmd: %s\n",
|
|
Packit |
90a5c9 |
target_uname, actual_uname,
|
|
Packit |
90a5c9 |
target_gname, actual_gname,
|
|
Packit |
90a5c9 |
cmd);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Error out if attempt is made to execute as root or as
|
|
Packit |
90a5c9 |
* a UID less than AP_UID_MIN. Tsk tsk.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if ((uid == 0) || (uid < AP_UID_MIN)) {
|
|
Packit |
90a5c9 |
log_err("cannot run as forbidden uid (%lu/%s)\n", (unsigned long)uid, cmd);
|
|
Packit |
90a5c9 |
exit(107);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Error out if attempt is made to execute as root group
|
|
Packit |
90a5c9 |
* or as a GID less than AP_GID_MIN. Tsk tsk.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if ((gid == 0) || (gid < AP_GID_MIN)) {
|
|
Packit |
90a5c9 |
log_err("cannot run as forbidden gid (%lu/%s)\n", (unsigned long)gid, cmd);
|
|
Packit |
90a5c9 |
exit(108);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Change UID/GID here so that the following tests work over NFS.
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Initialize the group access list for the target user,
|
|
Packit |
90a5c9 |
* and setgid() to the target group. If unsuccessful, error out.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (((setgid(gid)) != 0) || (initgroups(actual_uname, gid) != 0)) {
|
|
Packit |
90a5c9 |
log_err("failed to setgid (%lu: %s)\n", (unsigned long)gid, cmd);
|
|
Packit |
90a5c9 |
exit(109);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* setuid() to the target user. Error out on fail.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if ((setuid(uid)) != 0) {
|
|
Packit |
90a5c9 |
log_err("failed to setuid (%lu: %s)\n", (unsigned long)uid, cmd);
|
|
Packit |
90a5c9 |
exit(110);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Get the current working directory, as well as the proper
|
|
Packit |
90a5c9 |
* document root (dependant upon whether or not it is a
|
|
Packit |
90a5c9 |
* ~userdir request). Error out if we cannot get either one,
|
|
Packit |
90a5c9 |
* or if the current working directory is not in the docroot.
|
|
Packit |
90a5c9 |
* Use chdir()s and getcwd()s to avoid problems with symlinked
|
|
Packit |
90a5c9 |
* directories. Yuck.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (getcwd(cwd, AP_MAXPATH) == NULL) {
|
|
Packit |
90a5c9 |
log_err("cannot get current working directory\n");
|
|
Packit |
90a5c9 |
exit(111);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (userdir) {
|
|
Packit |
90a5c9 |
if (((chdir(target_homedir)) != 0) ||
|
|
Packit |
90a5c9 |
((chdir(AP_USERDIR_SUFFIX)) != 0) ||
|
|
Packit |
90a5c9 |
((getcwd(dwd, AP_MAXPATH)) == NULL) ||
|
|
Packit |
90a5c9 |
((chdir(cwd)) != 0)) {
|
|
Packit |
90a5c9 |
log_err("cannot get docroot information (%s)\n", target_homedir);
|
|
Packit |
90a5c9 |
exit(112);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
if (((chdir(AP_DOC_ROOT)) != 0) ||
|
|
Packit |
90a5c9 |
((getcwd(dwd, AP_MAXPATH)) == NULL) ||
|
|
Packit |
90a5c9 |
((chdir(cwd)) != 0)) {
|
|
Packit |
90a5c9 |
log_err("cannot get docroot information (%s)\n", AP_DOC_ROOT);
|
|
Packit |
90a5c9 |
exit(113);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if ((strncmp(cwd, dwd, strlen(dwd))) != 0) {
|
|
Packit |
90a5c9 |
log_err("command not in docroot (%s/%s)\n", cwd, cmd);
|
|
Packit |
90a5c9 |
exit(114);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Stat the cwd and verify it is a directory, or error out.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (((lstat(cwd, &dir_info)) != 0) || !(S_ISDIR(dir_info.st_mode))) {
|
|
Packit |
90a5c9 |
log_err("cannot stat directory: (%s)\n", cwd);
|
|
Packit |
90a5c9 |
exit(115);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Error out if cwd is writable by others.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if ((dir_info.st_mode & S_IWOTH) || (dir_info.st_mode & S_IWGRP)) {
|
|
Packit |
90a5c9 |
log_err("directory is writable by others: (%s)\n", cwd);
|
|
Packit |
90a5c9 |
exit(116);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Error out if we cannot stat the program.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (((lstat(cmd, &prg_info)) != 0) || (S_ISLNK(prg_info.st_mode))) {
|
|
Packit |
90a5c9 |
log_err("cannot stat program: (%s)\n", cmd);
|
|
Packit |
90a5c9 |
exit(117);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Error out if the program is writable by others.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if ((prg_info.st_mode & S_IWOTH) || (prg_info.st_mode & S_IWGRP)) {
|
|
Packit |
90a5c9 |
log_err("file is writable by others: (%s/%s)\n", cwd, cmd);
|
|
Packit |
90a5c9 |
exit(118);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Error out if the file is setuid or setgid.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if ((prg_info.st_mode & S_ISUID) || (prg_info.st_mode & S_ISGID)) {
|
|
Packit |
90a5c9 |
log_err("file is either setuid or setgid: (%s/%s)\n", cwd, cmd);
|
|
Packit |
90a5c9 |
exit(119);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Error out if the target name/group is different from
|
|
Packit |
90a5c9 |
* the name/group of the cwd or the program.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if ((uid != dir_info.st_uid) ||
|
|
Packit |
90a5c9 |
(gid != dir_info.st_gid) ||
|
|
Packit |
90a5c9 |
(uid != prg_info.st_uid) ||
|
|
Packit |
90a5c9 |
(gid != prg_info.st_gid)) {
|
|
Packit |
90a5c9 |
log_err("target uid/gid (%lu/%lu) mismatch "
|
|
Packit |
90a5c9 |
"with directory (%lu/%lu) or program (%lu/%lu)\n",
|
|
Packit |
90a5c9 |
(unsigned long)uid, (unsigned long)gid,
|
|
Packit |
90a5c9 |
(unsigned long)dir_info.st_uid, (unsigned long)dir_info.st_gid,
|
|
Packit |
90a5c9 |
(unsigned long)prg_info.st_uid, (unsigned long)prg_info.st_gid);
|
|
Packit |
90a5c9 |
exit(120);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Error out if the program is not executable for the user.
|
|
Packit |
90a5c9 |
* Otherwise, she won't find any error in the logs except for
|
|
Packit |
90a5c9 |
* "[error] Premature end of script headers: ..."
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (!(prg_info.st_mode & S_IXUSR)) {
|
|
Packit |
90a5c9 |
log_err("file has no execute permission: (%s/%s)\n", cwd, cmd);
|
|
Packit |
90a5c9 |
exit(121);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#ifdef AP_SUEXEC_UMASK
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* umask() uses inverse logic; bits are CLEAR for allowed access.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if ((~AP_SUEXEC_UMASK) & 0022) {
|
|
Packit |
90a5c9 |
log_err("notice: AP_SUEXEC_UMASK of %03o allows "
|
|
Packit |
90a5c9 |
"write permission to group and/or other\n", AP_SUEXEC_UMASK);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
umask(AP_SUEXEC_UMASK);
|
|
Packit |
90a5c9 |
#endif /* AP_SUEXEC_UMASK */
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Be sure to close the log file so the CGI can't mess with it. */
|
|
Packit |
90a5c9 |
#ifdef AP_LOG_SYSLOG
|
|
Packit |
90a5c9 |
if (log_open) {
|
|
Packit |
90a5c9 |
closelog();
|
|
Packit |
90a5c9 |
log_open = 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
#else
|
|
Packit |
90a5c9 |
if (log != NULL) {
|
|
Packit |
90a5c9 |
#if APR_HAVE_FCNTL_H
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* ask fcntl(2) to set the FD_CLOEXEC flag on the log file,
|
|
Packit |
90a5c9 |
* so it'll be automagically closed if the exec() call succeeds.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
fflush(log);
|
|
Packit |
90a5c9 |
setbuf(log, NULL);
|
|
Packit |
90a5c9 |
if ((fcntl(fileno(log), F_SETFD, FD_CLOEXEC) == -1)) {
|
|
Packit |
90a5c9 |
log_err("error: can't set close-on-exec flag");
|
|
Packit |
90a5c9 |
exit(122);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
#else
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* In this case, exec() errors won't be logged because we have already
|
|
Packit |
90a5c9 |
* dropped privileges and won't be able to reopen the log file.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
fclose(log);
|
|
Packit |
90a5c9 |
log = NULL;
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Execute the command, replacing our image with its own.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
#ifdef NEED_HASHBANG_EMUL
|
|
Packit |
90a5c9 |
/* We need the #! emulation when we want to execute scripts */
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
extern char **environ;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_execve(cmd, &argv[3], environ);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
#else /*NEED_HASHBANG_EMUL*/
|
|
Packit |
90a5c9 |
execv(cmd, &argv[3]);
|
|
Packit |
90a5c9 |
#endif /*NEED_HASHBANG_EMUL*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* (I can't help myself...sorry.)
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Uh oh. Still here. Where's the kaboom? There was supposed to be an
|
|
Packit |
90a5c9 |
* EARTH-shattering kaboom!
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Oh well, log the failure and error out.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
log_err("(%d)%s: exec failed (%s)\n", errno, strerror(errno), cmd);
|
|
Packit |
90a5c9 |
exit(255);
|
|
Packit |
90a5c9 |
}
|