]> granicus.if.org Git - apache/blob - os/beos/beosd.c
Update to Apache Software License version 1.1
[apache] / os / beos / beosd.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  *    if any, must include the following acknowledgment:
21  *       "This product includes software developed by the
22  *        Apache Software Foundation (http://www.apache.org/)."
23  *    Alternately, this acknowledgment may appear in the software itself,
24  *    if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" must
27  *    not be used to endorse or promote products derived from this
28  *    software without prior written permission. For written
29  *    permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  *    nor may "Apache" appear in their name, without prior written
33  *    permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation.  For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  * Portions of this software are based upon public domain software
55  * originally written at the National Center for Supercomputing Applications,
56  * University of Illinois, Urbana-Champaign.
57  */
58
59 #include "httpd.h"
60 #include "http_config.h"
61 #include "http_main.h"
62 #include "http_log.h"
63 #include "beosd.h"
64
65 beosd_config_rec beosd_config;
66
67 void beosd_detach(void)
68 {
69     pid_t pgrp;
70
71     chdir("/");
72
73     RAISE_SIGSTOP(DETACH);
74
75     if ((pgrp = setsid()) == -1) {
76         perror("setsid");
77         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, 
78                      "%s: setsid failed", ap_server_argv0);
79         exit(1);
80     }
81
82     /* close out the standard file descriptors */
83     if (freopen("/dev/null", "r", stdin) == NULL) {
84         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, 
85                      "%s: unable to replace stdin with /dev/null: %s",
86                 ap_server_argv0, strerror(errno));
87         /* continue anyhow -- note we can't close out descriptor 0 because we
88          * have nothing to replace it with, and if we didn't have a descriptor
89          * 0 the next file would be created with that value ... leading to
90          * havoc.
91          */
92     }
93     if (freopen("/dev/null", "w", stdout) == NULL) {
94         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, 
95                      "%s: unable to replace stdout with /dev/null: %s",
96                 ap_server_argv0, strerror(errno));
97     }
98     /* stderr is a tricky one, we really want it to be the error_log,
99      * but we haven't opened that yet.  So leave it alone for now and it'll
100      * be reopened moments later.
101      */
102 }
103
104 /* Set group privileges.
105  *
106  * Note that we use the username as set in the config files, rather than
107  * the lookup of to uid --- the same uid may have multiple passwd entries,
108  * with different sets of groups for each.
109  */
110
111 static int set_group_privs(void)
112 {
113 #if B_BEOS_VERSION < 0x0460
114
115     if (!geteuid()) {
116         char *name;
117
118         /* Get username if passed as a uid */
119
120         if (beosd_config.user_name[0] == '#') {
121             struct passwd *ent;
122             uid_t uid = atoi(&beosd_config.user_name[1]);
123
124             if ((ent = getpwuid(uid)) == NULL) {
125                 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
126                          "getpwuid: couldn't determine user name from uid %u, "
127                          "you probably need to modify the User directive",
128                          (unsigned)uid);
129                 return -1;
130             }
131
132             name = ent->pw_name;
133         }
134         else
135             name = beosd_config.user_name;
136
137         if (setgid(beosd_config.group_id) == -1) {
138             ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
139                         "setgid: unable to set group id to Group %u",
140                         (unsigned)beosd_config.group_id);
141             return -1;
142         }
143
144         /* Reset `groups' attributes. */
145
146         if (initgroups(name, beosd_config.group_id) == -1) {
147             ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
148                         "initgroups: unable to set groups for User %s "
149                         "and Group %u", name, (unsigned)beosd_config.group_id);
150             return -1;
151         }
152     }
153 #endif
154     return 0;
155 }
156
157
158 int beosd_setup_child(void)
159 {
160     /* TODO: revisit the whole issue of users/groups for BeOS as
161      * R4.5.2 and below doesn't really have much concept of them.
162      */
163
164     return 0;
165 }
166
167
168 const char *beosd_set_user(cmd_parms *cmd, void *dummy, char *arg)
169 {
170     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
171     if (err != NULL) {
172         return err;
173     }
174
175     beosd_config.user_name = arg;
176     beosd_config.user_id = ap_uname2id(arg);
177 #if !defined (BIG_SECURITY_HOLE) && !defined (OS2)
178     if (beosd_config.user_id == 0) {
179         return "Error:\tApache has not been designed to serve pages while\n"
180                 "\trunning as root.  There are known race conditions that\n"
181                 "\twill allow any local user to read any file on the system.\n"
182                 "\tIf you still desire to serve pages as root then\n"
183                 "\tadd -DBIG_SECURITY_HOLE to the EXTRA_CFLAGS line in your\n"
184                 "\tsrc/Configuration file and rebuild the server.  It is\n"
185                 "\tstrongly suggested that you instead modify the User\n"
186                 "\tdirective in your httpd.conf file to list a non-root\n"
187                 "\tuser.\n";
188     }
189 #endif
190
191     return NULL;
192 }
193
194 const char *beosd_set_group(cmd_parms *cmd, void *dummy, char *arg)
195 {
196     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
197     if (err != NULL) {
198         return err;
199     }
200
201     beosd_config.group_id = ap_gname2id(arg);
202
203     return NULL;
204 }
205
206 void beosd_pre_config(void)
207 {
208     beosd_config.user_name = DEFAULT_USER;
209     beosd_config.user_id = ap_uname2id(DEFAULT_USER);
210     beosd_config.group_id = ap_gname2id(DEFAULT_GROUP);
211 }