1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * suexec.c -- "Wrapper" support program for suEXEC behaviour for Apache
20 ***********************************************************************
22 * NOTE! : DO NOT edit this code!!! Unless you know what you are doing,
23 * editing this code might open up your system in unexpected
24 * ways to would-be crackers. Every precaution has been taken
25 * to make this code as safe as possible; alter it at your own
28 ***********************************************************************
34 #include "ap_config.h"
37 #include <sys/param.h>
39 #include <sys/types.h>
60 #define AP_MAXPATH PATH_MAX
61 #elif defined(MAXPATHLEN)
62 #define AP_MAXPATH MAXPATHLEN
64 #define AP_MAXPATH 8192
69 extern char **environ;
70 static FILE *log = NULL;
72 static const char *const safe_env_lst[] =
74 /* variable name starts with */
78 /* variable name is */
85 "DOCUMENT_PATH_INFO=",
94 "QUERY_STRING_UNESCAPED=",
101 "REDIRECT_QUERY_STRING=",
102 "REDIRECT_REMOTE_USER=",
125 static void err_output(int is_error, const char *fmt, va_list ap)
132 if ((log = fopen(AP_LOG_EXEC, "a")) == NULL) {
133 fprintf(stderr, "suexec failure: could not open log file\n");
140 fprintf(stderr, "suexec policy violation: see suexec log for more "
145 lt = localtime(&timevar);
147 fprintf(log, "[%d-%.2d-%.2d %.2d:%.2d:%.2d]: ",
148 lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
149 lt->tm_hour, lt->tm_min, lt->tm_sec);
151 vfprintf(log, fmt, ap);
154 #endif /* AP_LOG_EXEC */
158 static void log_err(const char *fmt,...)
164 err_output(1, fmt, ap); /* 1 == is_error */
166 #endif /* AP_LOG_EXEC */
170 static void log_no_err(const char *fmt,...)
176 err_output(0, fmt, ap); /* 0 == !is_error */
178 #endif /* AP_LOG_EXEC */
182 static void clean_env(void)
190 /* While cleaning the environment, the environment should be clean.
191 * (e.g. malloc() may get the name of a file for writing debugging info.
192 * Bad news if MALLOC_DEBUG_FILE is set to /etc/passwd. Sprintf() may be
193 * susceptible to bad locale settings....)
196 char **envp = environ;
197 char *empty_ptr = NULL;
199 environ = &empty_ptr; /* VERY safe environment */
201 if ((cleanenv = (char **) calloc(AP_ENVBUF, sizeof(char *))) == NULL) {
202 log_err("failed to malloc memory for environment\n");
206 sprintf(pathbuf, "PATH=%s", AP_SAFE_PATH);
207 cleanenv[cidx] = strdup(pathbuf);
210 for (ep = envp; *ep && cidx < AP_ENVBUF-1; ep++) {
211 for (idx = 0; safe_env_lst[idx]; idx++) {
212 if (!strncmp(*ep, safe_env_lst[idx],
213 strlen(safe_env_lst[idx]))) {
214 cleanenv[cidx] = *ep;
221 cleanenv[cidx] = NULL;
226 int main(int argc, char *argv[])
228 int userdir = 0; /* ~userdir flag */
229 uid_t uid; /* user information */
230 gid_t gid; /* target group placeholder */
231 char *target_uname; /* target user name */
232 char *target_gname; /* target group name */
233 char *target_homedir; /* target home directory */
234 char *actual_uname; /* actual user name */
235 char *actual_gname; /* actual group name */
236 char *prog; /* name of this program */
237 char *cmd; /* command to be executed */
238 char cwd[AP_MAXPATH]; /* current working directory */
239 char dwd[AP_MAXPATH]; /* docroot working directory */
240 struct passwd *pw; /* password entry holder */
241 struct group *gr; /* group entry holder */
242 struct stat dir_info; /* directory info holder */
243 struct stat prg_info; /* program info holder */
246 * Start with a "clean" environment
252 * Check existence/validity of the UID of the user
253 * running this program. Error out if invalid.
256 if ((pw = getpwuid(uid)) == NULL) {
257 log_err("crit: invalid uid: (%ld)\n", uid);
261 * See if this is a 'how were you compiled' request, and
265 && (! strcmp(argv[1], "-V"))
268 /* User name comparisons are case insensitive on BS2000/OSD */
269 || (! strcasecmp(AP_HTTPD_USER, pw->pw_name)))
270 #else /* _OSD_POSIX */
271 || (! strcmp(AP_HTTPD_USER, pw->pw_name)))
272 #endif /* _OSD_POSIX */
275 fprintf(stderr, " -D AP_DOC_ROOT=\"%s\"\n", AP_DOC_ROOT);
278 fprintf(stderr, " -D AP_GID_MIN=%d\n", AP_GID_MIN);
281 fprintf(stderr, " -D AP_HTTPD_USER=\"%s\"\n", AP_HTTPD_USER);
284 fprintf(stderr, " -D AP_LOG_EXEC=\"%s\"\n", AP_LOG_EXEC);
287 fprintf(stderr, " -D AP_SAFE_PATH=\"%s\"\n", AP_SAFE_PATH);
289 #ifdef AP_SUEXEC_UMASK
290 fprintf(stderr, " -D AP_SUEXEC_UMASK=%03o\n", AP_SUEXEC_UMASK);
293 fprintf(stderr, " -D AP_UID_MIN=%d\n", AP_UID_MIN);
295 #ifdef AP_USERDIR_SUFFIX
296 fprintf(stderr, " -D AP_USERDIR_SUFFIX=\"%s\"\n", AP_USERDIR_SUFFIX);
301 * If there are a proper number of arguments, set
302 * all of them to variables. Otherwise, error out.
305 log_err("too few arguments\n");
308 target_uname = argv[1];
309 target_gname = argv[2];
313 * Check to see if the user running this program
314 * is the user allowed to do so as defined in
315 * suexec.h. If not the allowed user, error out.
318 /* User name comparisons are case insensitive on BS2000/OSD */
319 if (strcasecmp(AP_HTTPD_USER, pw->pw_name)) {
320 log_err("user mismatch (%s instead of %s)\n", pw->pw_name, AP_HTTPD_USER);
324 if (strcmp(AP_HTTPD_USER, pw->pw_name)) {
325 log_err("user mismatch (%s instead of %s)\n", pw->pw_name, AP_HTTPD_USER);
328 #endif /*_OSD_POSIX*/
331 * Check for a leading '/' (absolute path) in the command to be executed,
332 * or attempts to back up out of the current directory,
333 * to protect against attacks. If any are
334 * found, error out. Naughty naughty crackers.
336 if ((cmd[0] == '/') || (!strncmp(cmd, "../", 3))
337 || (strstr(cmd, "/../") != NULL)) {
338 log_err("invalid command (%s)\n", cmd);
343 * Check to see if this is a ~userdir request. If
344 * so, set the flag, and remove the '~' from the
347 if (!strncmp("~", target_uname, 1)) {
353 * Error out if the target username is invalid.
355 if (strspn(target_uname, "1234567890") != strlen(target_uname)) {
356 if ((pw = getpwnam(target_uname)) == NULL) {
357 log_err("invalid target user name: (%s)\n", target_uname);
362 if ((pw = getpwuid(atoi(target_uname))) == NULL) {
363 log_err("invalid target user id: (%s)\n", target_uname);
369 * Error out if the target group name is invalid.
371 if (strspn(target_gname, "1234567890") != strlen(target_gname)) {
372 if ((gr = getgrnam(target_gname)) == NULL) {
373 log_err("invalid target group name: (%s)\n", target_gname);
378 if ((gr = getgrgid(atoi(target_gname))) == NULL) {
379 log_err("invalid target group id: (%s)\n", target_gname);
384 actual_gname = strdup(gr->gr_name);
388 * Initialize BS2000 user environment
394 switch (pid = ufork(target_uname)) {
396 log_err("failed to setup bs2000 environment for user %s: %s\n",
397 target_uname, strerror(errno));
401 default: /* Father */
402 while (pid != waitpid(pid, &status, 0))
404 /* @@@ FIXME: should we deal with STOP signals as well? */
405 if (WIFSIGNALED(status)) {
406 kill (getpid(), WTERMSIG(status));
408 exit(WEXITSTATUS(status));
411 #endif /*_OSD_POSIX*/
414 * Save these for later since initgroups will hose the struct
417 actual_uname = strdup(pw->pw_name);
418 target_homedir = strdup(pw->pw_dir);
421 * Log the transaction here to be sure we have an open log
422 * before we setuid().
424 log_no_err("uid: (%s/%s) gid: (%s/%s) cmd: %s\n",
425 target_uname, actual_uname,
426 target_gname, actual_gname,
430 * Error out if attempt is made to execute as root or as
431 * a UID less than AP_UID_MIN. Tsk tsk.
433 if ((uid == 0) || (uid < AP_UID_MIN)) {
434 log_err("cannot run as forbidden uid (%d/%s)\n", uid, cmd);
439 * Error out if attempt is made to execute as root group
440 * or as a GID less than AP_GID_MIN. Tsk tsk.
442 if ((gid == 0) || (gid < AP_GID_MIN)) {
443 log_err("cannot run as forbidden gid (%d/%s)\n", gid, cmd);
448 * Change UID/GID here so that the following tests work over NFS.
450 * Initialize the group access list for the target user,
451 * and setgid() to the target group. If unsuccessful, error out.
453 if (((setgid(gid)) != 0) || (initgroups(actual_uname, gid) != 0)) {
454 log_err("failed to setgid (%ld: %s)\n", gid, cmd);
459 * setuid() to the target user. Error out on fail.
461 if ((setuid(uid)) != 0) {
462 log_err("failed to setuid (%ld: %s)\n", uid, cmd);
467 * Get the current working directory, as well as the proper
468 * document root (dependant upon whether or not it is a
469 * ~userdir request). Error out if we cannot get either one,
470 * or if the current working directory is not in the docroot.
471 * Use chdir()s and getcwd()s to avoid problems with symlinked
474 if (getcwd(cwd, AP_MAXPATH) == NULL) {
475 log_err("cannot get current working directory\n");
480 if (((chdir(target_homedir)) != 0) ||
481 ((chdir(AP_USERDIR_SUFFIX)) != 0) ||
482 ((getcwd(dwd, AP_MAXPATH)) == NULL) ||
483 ((chdir(cwd)) != 0)) {
484 log_err("cannot get docroot information (%s)\n", target_homedir);
489 if (((chdir(AP_DOC_ROOT)) != 0) ||
490 ((getcwd(dwd, AP_MAXPATH)) == NULL) ||
491 ((chdir(cwd)) != 0)) {
492 log_err("cannot get docroot information (%s)\n", AP_DOC_ROOT);
497 if ((strncmp(cwd, dwd, strlen(dwd))) != 0) {
498 log_err("command not in docroot (%s/%s)\n", cwd, cmd);
503 * Stat the cwd and verify it is a directory, or error out.
505 if (((lstat(cwd, &dir_info)) != 0) || !(S_ISDIR(dir_info.st_mode))) {
506 log_err("cannot stat directory: (%s)\n", cwd);
511 * Error out if cwd is writable by others.
513 if ((dir_info.st_mode & S_IWOTH) || (dir_info.st_mode & S_IWGRP)) {
514 log_err("directory is writable by others: (%s)\n", cwd);
519 * Error out if we cannot stat the program.
521 if (((lstat(cmd, &prg_info)) != 0) || (S_ISLNK(prg_info.st_mode))) {
522 log_err("cannot stat program: (%s)\n", cmd);
527 * Error out if the program is writable by others.
529 if ((prg_info.st_mode & S_IWOTH) || (prg_info.st_mode & S_IWGRP)) {
530 log_err("file is writable by others: (%s/%s)\n", cwd, cmd);
535 * Error out if the file is setuid or setgid.
537 if ((prg_info.st_mode & S_ISUID) || (prg_info.st_mode & S_ISGID)) {
538 log_err("file is either setuid or setgid: (%s/%s)\n", cwd, cmd);
543 * Error out if the target name/group is different from
544 * the name/group of the cwd or the program.
546 if ((uid != dir_info.st_uid) ||
547 (gid != dir_info.st_gid) ||
548 (uid != prg_info.st_uid) ||
549 (gid != prg_info.st_gid)) {
550 log_err("target uid/gid (%ld/%ld) mismatch "
551 "with directory (%ld/%ld) or program (%ld/%ld)\n",
553 dir_info.st_uid, dir_info.st_gid,
554 prg_info.st_uid, prg_info.st_gid);
558 * Error out if the program is not executable for the user.
559 * Otherwise, she won't find any error in the logs except for
560 * "[error] Premature end of script headers: ..."
562 if (!(prg_info.st_mode & S_IXUSR)) {
563 log_err("file has no execute permission: (%s/%s)\n", cwd, cmd);
567 #ifdef AP_SUEXEC_UMASK
569 * umask() uses inverse logic; bits are CLEAR for allowed access.
571 if ((~AP_SUEXEC_UMASK) & 0022) {
572 log_err("notice: AP_SUEXEC_UMASK of %03o allows "
573 "write permission to group and/or other\n", AP_SUEXEC_UMASK);
575 umask(AP_SUEXEC_UMASK);
576 #endif /* AP_SUEXEC_UMASK */
579 * ask fcntl(2) to set the FD_CLOEXEC flag on the log file,
580 * so it'll be automagically closed if the exec() call succeeds.
585 if ((fcntl(fileno(log), F_SETFD, FD_CLOEXEC) == -1)) {
586 log_err("error: can't set close-on-exec flag");
592 * Execute the command, replacing our image with its own.
594 #ifdef NEED_HASHBANG_EMUL
595 /* We need the #! emulation when we want to execute scripts */
597 extern char **environ;
599 ap_execve(cmd, &argv[3], environ);
601 #else /*NEED_HASHBANG_EMUL*/
602 execv(cmd, &argv[3]);
603 #endif /*NEED_HASHBANG_EMUL*/
606 * (I can't help myself...sorry.)
608 * Uh oh. Still here. Where's the kaboom? There was supposed to be an
609 * EARTH-shattering kaboom!
611 * Oh well, log the failure and error out.
613 log_err("(%d)%s: exec failed (%s)\n", errno, strerror(errno), cmd);