dgaudet     99/06/20 15:59:53

  Modified:    mpm/src  CHANGES
               mpm/src/include http_config.h
               mpm/src/main mpm_prefork.c
               mpm/src/os/unix Makefile.tmpl
  Added:       mpm/src/os/unix unixd.c unixd.h
  Log:
  detach, set_group_privs, and such... these will be common amongst the
  unix MPMs, so split them off into os/unix/unixd.[ch].
  
  Revision  Changes    Path
  1.8       +3 -0      apache-2.0/mpm/src/CHANGES
  
  Index: CHANGES
  ===================================================================
  RCS file: /home/cvs/apache-2.0/mpm/src/CHANGES,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- CHANGES   1999/06/20 21:46:11     1.7
  +++ CHANGES   1999/06/20 22:59:49     1.8
  @@ -1,5 +1,8 @@
   Changes with MPM
   
  +    * os/unix/unixd.[ch]: detach, setuid, setgid, stuff which will be common
  +      amongst the unix MPMs
  +
       * mpm_prefork: throw away all the alarm/timeout crud; and clean up the
         signal handling for the new world order.  [Dean Gaudet]
   
  
  
  
  1.2       +13 -13    apache-2.0/mpm/src/include/http_config.h
  
  Index: http_config.h
  ===================================================================
  RCS file: /home/cvs/apache-2.0/mpm/src/include/http_config.h,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- http_config.h     1999/06/18 18:39:28     1.1
  +++ http_config.h     1999/06/20 22:59:50     1.2
  @@ -311,6 +311,19 @@
   /* for implementing subconfigs and customized config files */
   API_EXPORT(const char *) ap_srm_command_loop(cmd_parms *parms, void *config);
   
  +/* ap_check_cmd_context() definitions: */
  +API_EXPORT(const char *) ap_check_cmd_context(cmd_parms *cmd, unsigned 
forbidden);
  +
  +/* ap_check_cmd_context():              Forbidden in: */
  +#define  NOT_IN_VIRTUALHOST     0x01 /* <Virtualhost> */
  +#define  NOT_IN_LIMIT           0x02 /* <Limit> */
  +#define  NOT_IN_DIRECTORY       0x04 /* <Directory> */
  +#define  NOT_IN_LOCATION        0x08 /* <Location> */
  +#define  NOT_IN_FILES           0x10 /* <Files> */
  +#define  NOT_IN_DIR_LOC_FILE    
(NOT_IN_DIRECTORY|NOT_IN_LOCATION|NOT_IN_FILES) /* 
<Directory>/<Location>/<Files>*/
  +#define  GLOBAL_ONLY            
(NOT_IN_VIRTUALHOST|NOT_IN_LIMIT|NOT_IN_DIR_LOC_FILE)
  +
  +
   #ifdef CORE_PRIVATE
   
   extern API_VAR_EXPORT module *top_module;
  @@ -349,19 +362,6 @@
   CORE_EXPORT(const char *) ap_init_virtual_host(pool *p, const char *hostname,
                                server_rec *main_server, server_rec **);
   void ap_process_resource_config(server_rec *s, const char *fname, pool *p, 
pool *ptemp);
  -
  -/* ap_check_cmd_context() definitions: */
  -API_EXPORT(const char *) ap_check_cmd_context(cmd_parms *cmd, unsigned 
forbidden);
  -
  -/* ap_check_cmd_context():              Forbidden in: */
  -#define  NOT_IN_VIRTUALHOST     0x01 /* <Virtualhost> */
  -#define  NOT_IN_LIMIT           0x02 /* <Limit> */
  -#define  NOT_IN_DIRECTORY       0x04 /* <Directory> */
  -#define  NOT_IN_LOCATION        0x08 /* <Location> */
  -#define  NOT_IN_FILES           0x10 /* <Files> */
  -#define  NOT_IN_DIR_LOC_FILE    
(NOT_IN_DIRECTORY|NOT_IN_LOCATION|NOT_IN_FILES) /* 
<Directory>/<Location>/<Files>*/
  -#define  GLOBAL_ONLY            
(NOT_IN_VIRTUALHOST|NOT_IN_LIMIT|NOT_IN_DIR_LOC_FILE)
  -
   
   /* Module-method dispatchers, also for http_request.c */
   
  
  
  
  1.6       +13 -218   apache-2.0/mpm/src/main/mpm_prefork.c
  
  Index: mpm_prefork.c
  ===================================================================
  RCS file: /home/cvs/apache-2.0/mpm/src/main/mpm_prefork.c,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- mpm_prefork.c     1999/06/20 21:46:13     1.5
  +++ mpm_prefork.c     1999/06/20 22:59:51     1.6
  @@ -96,6 +96,7 @@
   #include "http_connection.h"
   #include "scoreboard_prefork.h"
   #include "ap_mpm.h"
  +#include "unixd.h"
   #ifdef USE_SHMGET_SCOREBOARD
   #include <sys/types.h>
   #include <sys/ipc.h>
  @@ -108,12 +109,6 @@
   
   /* config globals */
   
  -static uid_t ap_user_id=0;
  -static char *ap_user_name=NULL;
  -static gid_t ap_group_id=0;
  -#ifdef MULTIPLE_GROUPS
  -static gid_t group_id_list[NGROUPS_MAX];
  -#endif
   static int ap_max_requests_per_child=0;
   static char *ap_pid_fname=NULL;
   static char *ap_scoreboard_fname=NULL;
  @@ -163,7 +158,6 @@
   static int sd;
   static fd_set listenfds;
   static int listenmaxfd;
  -static pid_t pgrp;
   
   /* one_process --- debugging mode variable; can be set from the command line
    * with the -X flag.  If set, this gets you the child_main loop running
  @@ -502,8 +496,8 @@
        /* restrict it to use only by the appropriate user_id ... not that this
         * stops CGIs from acquiring it and dinking around with it.
         */
  -     buf.sem_perm.uid = ap_user_id;
  -     buf.sem_perm.gid = ap_group_id;
  +     buf.sem_perm.uid = unixd_config.user_id;
  +     buf.sem_perm.gid = unixd_config.group_id;
        buf.sem_perm.mode = 0600;
        ick.buf = &buf;
        if (semctl(sem_id, 0, IPC_SET, ick) < 0) {
  @@ -1211,8 +1205,8 @@
                "shmctl() could not stat segment #%d", shmid);
        }
        else {
  -         shmbuf.shm_perm.uid = ap_user_id;
  -         shmbuf.shm_perm.gid = ap_group_id;
  +         shmbuf.shm_perm.uid = unixd_config.user_id;
  +         shmbuf.shm_perm.gid = unixd_config.group_id;
            if (shmctl(shmid, IPC_SET, &shmbuf) != 0) {
                ap_log_error(APLOG_MARK, APLOG_ERR, server_conf,
                    "shmctl() could not set segment #%d", shmid);
  @@ -2027,139 +2021,7 @@
   #endif
   }
   
  -
   /*****************************************************************
  - * Here follows a long bunch of generic server bookkeeping stuff...
  - */
  -
  -static void detach(void)
  -{
  -    int x;
  -
  -    chdir("/");
  -#if !defined(MPE) && !defined(OS2) && !defined(TPF)
  -/* Don't detach for MPE because child processes can't survive the death of
  -   the parent. */
  -    if ((x = fork()) > 0)
  -     exit(0);
  -    else if (x == -1) {
  -     perror("fork");
  -     fprintf(stderr, "%s: unable to fork new process\n", ap_server_argv0);
  -     exit(1);
  -    }
  -    RAISE_SIGSTOP(DETACH);
  -#endif
  -#ifndef NO_SETSID
  -    if ((pgrp = setsid()) == -1) {
  -     perror("setsid");
  -     fprintf(stderr, "%s: setsid failed\n", ap_server_argv0);
  -     exit(1);
  -    }
  -#elif defined(NEXT) || defined(NEWSOS)
  -    if (setpgrp(0, getpid()) == -1 || (pgrp = getpgrp(0)) == -1) {
  -     perror("setpgrp");
  -     fprintf(stderr, "%s: setpgrp or getpgrp failed\n", ap_server_argv0);
  -     exit(1);
  -    }
  -#elif defined(OS2) || defined(TPF)
  -    /* OS/2 and TPF don't support process group IDs */
  -    pgrp = getpid();
  -#elif defined(MPE)
  -    /* MPE uses negative pid for process group */
  -    pgrp = -getpid();
  -#else
  -    if ((pgrp = setpgrp(getpid(), 0)) == -1) {
  -     perror("setpgrp");
  -     fprintf(stderr, "%s: setpgrp failed\n", ap_server_argv0);
  -     exit(1);
  -    }
  -#endif
  -
  -    /* close out the standard file descriptors */
  -    if (freopen("/dev/null", "r", stdin) == NULL) {
  -     fprintf(stderr, "%s: unable to replace stdin with /dev/null: %s\n",
  -             ap_server_argv0, strerror(errno));
  -     /* continue anyhow -- note we can't close out descriptor 0 because we
  -      * have nothing to replace it with, and if we didn't have a descriptor
  -      * 0 the next file would be created with that value ... leading to
  -      * havoc.
  -      */
  -    }
  -    if (freopen("/dev/null", "w", stdout) == NULL) {
  -     fprintf(stderr, "%s: unable to replace stdout with /dev/null: %s\n",
  -             ap_server_argv0, strerror(errno));
  -    }
  -    /* stderr is a tricky one, we really want it to be the error_log,
  -     * but we haven't opened that yet.  So leave it alone for now and it'll
  -     * be reopened moments later.
  -     */
  -}
  -
  -/* 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 void set_group_privs(void)
  -{
  -    if (!geteuid()) {
  -     char *name;
  -
  -     /* Get username if passed as a uid */
  -
  -     if (ap_user_name[0] == '#') {
  -         struct passwd *ent;
  -         uid_t uid = atoi(&ap_user_name[1]);
  -
  -         if ((ent = getpwuid(uid)) == NULL) {
  -             ap_log_error(APLOG_MARK, APLOG_ALERT, server_conf,
  -                      "getpwuid: couldn't determine user name from uid %u, "
  -                      "you probably need to modify the User directive",
  -                      (unsigned)uid);
  -             clean_child_exit(APEXIT_CHILDFATAL);
  -         }
  -
  -         name = ent->pw_name;
  -     }
  -     else
  -         name = ap_user_name;
  -
  -#if !defined(OS2) && !defined(TPF)
  -     /* OS/2 and TPF don't support groups. */
  -
  -     /*
  -      * Set the GID before initgroups(), since on some platforms
  -      * setgid() is known to zap the group list.
  -      */
  -     if (setgid(ap_group_id) == -1) {
  -         ap_log_error(APLOG_MARK, APLOG_ALERT, server_conf,
  -                     "setgid: unable to set group id to Group %u",
  -                     (unsigned)ap_group_id);
  -         clean_child_exit(APEXIT_CHILDFATAL);
  -     }
  -
  -     /* Reset `groups' attributes. */
  -
  -     if (initgroups(name, ap_group_id) == -1) {
  -         ap_log_error(APLOG_MARK, APLOG_ALERT, server_conf,
  -                     "initgroups: unable to set groups for User %s "
  -                     "and Group %u", name, (unsigned)ap_group_id);
  -         clean_child_exit(APEXIT_CHILDFATAL);
  -     }
  -#ifdef MULTIPLE_GROUPS
  -     if (getgroups(NGROUPS_MAX, group_id_list) == -1) {
  -         ap_log_error(APLOG_MARK, APLOG_ALERT, server_conf,
  -                     "getgroups: unable to get group list");
  -         clean_child_exit(APEXIT_CHILDFATAL);
  -     }
  -#endif /* MULTIPLE_GROUPS */
  -#endif /* !defined(OS2) && !defined(TPF) */
  -    }
  -}
  -
  -/*****************************************************************
    * Connection structures and accounting...
    */
   
  @@ -2550,31 +2412,9 @@
       reopen_scoreboard(pchild);
       SAFE_ACCEPT(accept_mutex_child_init(pchild));
   
  -    set_group_privs();
  -#ifdef MPE
  -    /* Only try to switch if we're running as MANAGER.SYS */
  -    if (geteuid() == 1 && ap_user_id > 1) {
  -     GETPRIVMODE();
  -     if (setuid(ap_user_id) == -1) {
  -         GETUSERMODE();
  -         ap_log_error(APLOG_MARK, APLOG_ALERT, server_conf,
  -                     "setuid: unable to change uid");
  -         exit(1);
  -     }
  -     GETUSERMODE();
  -    }
  -#else
  -    /* Only try to switch if we're running as root */
  -    if (!geteuid() && (
  -#ifdef _OSD_POSIX
  -     os_init_job_environment(server_conf, ap_user_name, one_process) != 0 || 
  -#endif
  -     setuid(ap_user_id) == -1)) {
  -     ap_log_error(APLOG_MARK, APLOG_ALERT, server_conf,
  -                 "setuid: unable to change uid");
  +    if (unixd_setup_child()) {
        clean_child_exit(APEXIT_CHILDFATAL);
       }
  -#endif
   
       ap_child_init_hook(pchild, server_conf);
   
  @@ -2869,7 +2709,7 @@
   
   #ifdef _OSD_POSIX
       /* BS2000 requires a "special" version of fork() before a setuid() call 
*/
  -    if ((pid = os_fork(ap_user_name)) == -1) {
  +    if ((pid = os_fork(unixd_config.user_name)) == -1) {
   #elif defined(TPF)
       if ((pid = os_fork(s, slot)) == -1) {
   #else
  @@ -3258,7 +3098,7 @@
        /* Time to gracefully shut down:
         * Kill child processes, tell them to call child_exit, etc...
         */
  -     if (ap_killpg(pgrp, SIGTERM) < 0) {
  +     if (ap_killpg(getpgrp(), SIGTERM) < 0) {
            ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "killpg 
SIGTERM");
        }
        reclaim_child_processes(1);             /* Start with SIGTERM */
  @@ -3304,7 +3144,7 @@
                    "SIGUSR1 received.  Doing graceful restart");
   
        /* kill off the idle ones */
  -     if (ap_killpg(pgrp, SIGUSR1) < 0) {
  +     if (ap_killpg(getpgrp(), SIGUSR1) < 0) {
            ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "killpg 
SIGUSR1");
        }
   #ifndef SCOREBOARD_FILE
  @@ -3323,7 +3163,7 @@
       }
       else {
        /* Kill 'em off */
  -     if (ap_killpg(pgrp, SIGHUP) < 0) {
  +     if (ap_killpg(getpgrp(), SIGHUP) < 0) {
            ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "killpg 
SIGHUP");
        }
        reclaim_child_processes(0);             /* Not when just starting up */
  @@ -3360,15 +3200,13 @@
        is_graceful = 0;
   
        if (!one_process) {
  -         detach();
  +         unixd_detach();
        }
   
        my_pid = getpid();
       }
   
  -    ap_user_name = DEFAULT_USER;
  -    ap_user_id = ap_uname2id(DEFAULT_USER);
  -    ap_group_id = ap_gname2id(DEFAULT_GROUP);
  +    unixd_pre_config();
       ap_daemons_to_start = DEFAULT_START_DAEMON;
       ap_daemons_min_free = DEFAULT_MIN_FREE_DAEMON;
       ap_daemons_max_free = DEFAULT_MAX_FREE_DAEMON;
  @@ -3402,46 +3240,6 @@
       }
   }
   
  -static const char *set_user(cmd_parms *cmd, void *dummy, char *arg)
  -{
  -    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
  -    if (err != NULL) {
  -        return err;
  -    }
  -
  -    ap_user_name = arg;
  -    ap_user_id = ap_uname2id(arg);
  -#if !defined (BIG_SECURITY_HOLE) && !defined (OS2)
  -    if (ap_user_id == 0) {
  -     fprintf(stderr,
  -             "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 EXTRA_CFLAGS line in your\n"
  -             "\tsrc/Configuration file and rebuild the server.  It is\n"
  -             "\tstrongly suggested that you instead modify the User\n"
  -             "\tdirective in your httpd.conf file to list a non-root\n"
  -             "\tuser.\n");
  -     exit (1);
  -    }
  -#endif
  -
  -    return NULL;
  -}
  -
  -static const char *set_group(cmd_parms *cmd, void *dummy, char *arg)
  -{
  -    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
  -    if (err != NULL) {
  -        return err;
  -    }
  -
  -    ap_group_id = ap_gname2id(arg);
  -
  -    return NULL;
  -}
  -
   static const char *set_pidfile(cmd_parms *cmd, void *dummy, char *arg) 
   {
       const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
  @@ -3665,10 +3463,7 @@
   
   
   static const command_rec prefork_cmds[] = {
  -{ "User", set_user, NULL, RSRC_CONF, TAKE1,
  -  "Effective user id for this server"},
  -{ "Group", set_group, NULL, RSRC_CONF, TAKE1,
  -  "Effective group id for this server"},
  +UNIX_DAEMON_COMMANDS
   { "PidFile", set_pidfile, NULL, RSRC_CONF, TAKE1,
       "A file for logging the server process ID"},
   { "ScoreBoardFile", set_scoreboard, NULL, RSRC_CONF, TAKE1,
  
  
  
  1.3       +1 -1      apache-2.0/mpm/src/os/unix/Makefile.tmpl
  
  Index: Makefile.tmpl
  ===================================================================
  RCS file: /home/cvs/apache-2.0/mpm/src/os/unix/Makefile.tmpl,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Makefile.tmpl     1999/06/18 19:08:07     1.2
  +++ Makefile.tmpl     1999/06/20 22:59:52     1.3
  @@ -3,7 +3,7 @@
   INCLUDES=$(INCLUDES1) $(INCLUDES0) $(EXTRA_INCLUDES)
   LDFLAGS=$(LDFLAGS1) $(EXTRA_LDFLAGS)
   
  -OBJS=        os.o os-inline.o
  +OBJS=        os.o os-inline.o unixd.o
   
   LIB= libos.a
   
  
  
  
  1.1                  apache-2.0/mpm/src/os/unix/unixd.c
  
  Index: unixd.c
  ===================================================================
  /* ====================================================================
   * Copyright (c) 1998-1999 The Apache Group.  All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. All advertising materials mentioning features or use of this
   *    software must display the following acknowledgment:
   *    "This product includes software developed by the Apache Group
   *    for use in the Apache HTTP server project (http://www.apache.org/)."
   *
   * 4. The names "Apache Server" and "Apache Group" must not be used to
   *    endorse or promote products derived from this software without
   *    prior written permission. For written permission, please contact
   *    [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 6. Redistributions of any form whatsoever must retain the following
   *    acknowledgment:
   *    "This product includes software developed by the Apache Group
   *    for use in the Apache HTTP server project (http://www.apache.org/)."
   *
   * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
   * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Group and was originally based
   * on public domain software written at the National Center for
   * Supercomputing Applications, University of Illinois, Urbana-Champaign.
   * For more information on the Apache Group and the Apache HTTP server
   * project, please see <http://www.apache.org/>.
   *
   */
  
  #include "httpd.h"
  #include "http_config.h"
  #include "http_main.h"
  #include "http_log.h"
  #include "unixd.h"
  
  unixd_config_rec unixd_config;
  
  void unixd_detach(void)
  {
      int x;
      pid_t pgrp;
  
      chdir("/");
  #if !defined(MPE) && !defined(OS2) && !defined(TPF)
  /* Don't detach for MPE because child processes can't survive the death of
     the parent. */
      if ((x = fork()) > 0)
        exit(0);
      else if (x == -1) {
        perror("fork");
        fprintf(stderr, "%s: unable to fork new process\n", ap_server_argv0);
        exit(1);
      }
      RAISE_SIGSTOP(DETACH);
  #endif
  #ifndef NO_SETSID
      if ((pgrp = setsid()) == -1) {
        perror("setsid");
        fprintf(stderr, "%s: setsid failed\n", ap_server_argv0);
        exit(1);
      }
  #elif defined(NEXT) || defined(NEWSOS)
      if (setpgrp(0, getpid()) == -1 || (pgrp = getpgrp(0)) == -1) {
        perror("setpgrp");
        fprintf(stderr, "%s: setpgrp or getpgrp failed\n", ap_server_argv0);
        exit(1);
      }
  #elif defined(OS2) || defined(TPF)
      /* OS/2 and TPF don't support process group IDs */
      pgrp = getpid();
  #elif defined(MPE)
      /* MPE uses negative pid for process group */
      pgrp = -getpid();
  #else
      if ((pgrp = setpgrp(getpid(), 0)) == -1) {
        perror("setpgrp");
        fprintf(stderr, "%s: setpgrp failed\n", ap_server_argv0);
        exit(1);
      }
  #endif
  
      /* close out the standard file descriptors */
      if (freopen("/dev/null", "r", stdin) == NULL) {
        fprintf(stderr, "%s: unable to replace stdin with /dev/null: %s\n",
                ap_server_argv0, strerror(errno));
        /* continue anyhow -- note we can't close out descriptor 0 because we
         * have nothing to replace it with, and if we didn't have a descriptor
         * 0 the next file would be created with that value ... leading to
         * havoc.
         */
      }
      if (freopen("/dev/null", "w", stdout) == NULL) {
        fprintf(stderr, "%s: unable to replace stdout with /dev/null: %s\n",
                ap_server_argv0, strerror(errno));
      }
      /* stderr is a tricky one, we really want it to be the error_log,
       * but we haven't opened that yet.  So leave it alone for now and it'll
       * be reopened moments later.
       */
  }
  
  /* 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()) {
        char *name;
  
        /* Get username if passed as a uid */
  
        if (unixd_config.user_name[0] == '#') {
            struct passwd *ent;
            uid_t uid = atoi(&unixd_config.user_name[1]);
  
            if ((ent = getpwuid(uid)) == NULL) {
                ap_log_error(APLOG_MARK, APLOG_ALERT, NULL,
                         "getpwuid: couldn't determine user name from uid %u, "
                         "you probably need to modify the User directive",
                         (unsigned)uid);
                return -1;
            }
  
            name = ent->pw_name;
        }
        else
            name = unixd_config.user_name;
  
  #if !defined(OS2) && !defined(TPF)
        /* OS/2 and TPF don't support groups. */
  
        /*
         * Set the GID before initgroups(), since on some platforms
         * setgid() is known to zap the group list.
         */
        if (setgid(unixd_config.group_id) == -1) {
            ap_log_error(APLOG_MARK, APLOG_ALERT, NULL,
                        "setgid: unable to set group id to Group %u",
                        (unsigned)unixd_config.group_id);
            return -1;
        }
  
        /* Reset `groups' attributes. */
  
        if (initgroups(name, unixd_config.group_id) == -1) {
            ap_log_error(APLOG_MARK, APLOG_ALERT, NULL,
                        "initgroups: unable to set groups for User %s "
                        "and Group %u", name, (unsigned)unixd_config.group_id);
            return -1;
        }
  #endif /* !defined(OS2) && !defined(TPF) */
      }
      return 0;
  }
  
  
  int unixd_setup_child(void)
  {
      if (set_group_privs()) {
        return -1;
      }
  #ifdef MPE
      /* Only try to switch if we're running as MANAGER.SYS */
      if (geteuid() == 1 && unixd_config.user_id > 1) {
        GETPRIVMODE();
        if (setuid(unixd_config.user_id) == -1) {
            GETUSERMODE();
            ap_log_error(APLOG_MARK, APLOG_ALERT, NULL,
                        "setuid: unable to change uid");
            exit(1);
        }
        GETUSERMODE();
      }
  #else
      /* Only try to switch if we're running as root */
      if (!geteuid() && (
  #ifdef _OSD_POSIX
        os_init_job_environment(server_conf, unixd_config.user_name, 
one_process) != 0 || 
  #endif
        setuid(unixd_config.user_id) == -1)) {
        ap_log_error(APLOG_MARK, APLOG_ALERT, NULL,
                    "setuid: unable to change uid");
        return -1;
      }
  #endif
      return 0;
  }
  
  
  const char *unixd_set_user(cmd_parms *cmd, void *dummy, char *arg)
  {
      const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
      if (err != NULL) {
          return err;
      }
  
      unixd_config.user_name = arg;
      unixd_config.user_id = ap_uname2id(arg);
  #if !defined (BIG_SECURITY_HOLE) && !defined (OS2)
      if (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 EXTRA_CFLAGS line in your\n"
                "\tsrc/Configuration file and rebuild the server.  It is\n"
                "\tstrongly 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;
  }
  
  const char *unixd_set_group(cmd_parms *cmd, void *dummy, char *arg)
  {
      const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
      if (err != NULL) {
          return err;
      }
  
      unixd_config.group_id = ap_gname2id(arg);
  
      return NULL;
  }
  
  void unixd_pre_config(void)
  {
      unixd_config.user_name = DEFAULT_USER;
      unixd_config.user_id = ap_uname2id(DEFAULT_USER);
      unixd_config.group_id = ap_gname2id(DEFAULT_GROUP);
  }
  
  
  
  1.1                  apache-2.0/mpm/src/os/unix/unixd.h
  
  Index: unixd.h
  ===================================================================
  /* ====================================================================
   * Copyright (c) 1998-1999 The Apache Group.  All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. All advertising materials mentioning features or use of this
   *    software must display the following acknowledgment:
   *    "This product includes software developed by the Apache Group
   *    for use in the Apache HTTP server project (http://www.apache.org/)."
   *
   * 4. The names "Apache Server" and "Apache Group" must not be used to
   *    endorse or promote products derived from this software without
   *    prior written permission. For written permission, please contact
   *    [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 6. Redistributions of any form whatsoever must retain the following
   *    acknowledgment:
   *    "This product includes software developed by the Apache Group
   *    for use in the Apache HTTP server project (http://www.apache.org/)."
   *
   * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
   * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Group and was originally based
   * on public domain software written at the National Center for
   * Supercomputing Applications, University of Illinois, Urbana-Champaign.
   * For more information on the Apache Group and the Apache HTTP server
   * project, please see <http://www.apache.org/>.
   *
   */
  
  #ifndef UNIXD_H
  #define UNIXD_H
  
  /* common stuff that unix MPMs will want */
  
  typedef struct {
      char *user_name;
      uid_t user_id;
      gid_t group_id;
  } unixd_config_rec;
  extern unixd_config_rec unixd_config;
  
  void unixd_detach(void);
  int unixd_setup_child(void);
  void unixd_pre_config(void);
  const char *unixd_set_user(cmd_parms *cmd, void *dummy, char *arg);
  const char *unixd_set_group(cmd_parms *cmd, void *dummy, char *arg);
  
  #define UNIX_DAEMON_COMMANDS  \
  { "User", unixd_set_user, NULL, RSRC_CONF, TAKE1, \
    "Effective user id for this server"}, \
  { "Group", unixd_set_group, NULL, RSRC_CONF, TAKE1, \
    "Effective group id for this server"}, \
  
  #endif
  
  
  

Reply via email to