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
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17/*
18 * This file will include OS specific functions which are not inlineable.
19 * Any inlineable functions should be defined in os-inline.c instead.
20 */
21
22#ifdef _OSD_POSIX
23
24#include "os.h"
25
26#include "httpd.h"
27#include "http_config.h"
28#include "http_log.h"
29#include "apr_lib.h"
30
31#define USER_LEN 8
32
33APLOG_USE_MODULE(core);
34
35typedef enum
36{
37    bs2_unknown,     /* not initialized yet. */
38    bs2_noFORK,      /* no fork() because -X flag was specified */
39    bs2_FORK,        /* only fork() because uid != 0 */
40    bs2_UFORK        /* Normally, ufork() is used to switch identities. */
41} bs2_ForkType;
42
43static bs2_ForkType forktype = bs2_unknown;
44
45/* Determine the method for forking off a child in such a way as to
46 * set both the POSIX and BS2000 user id's to the unprivileged user.
47 */
48static bs2_ForkType os_forktype(int one_process)
49{
50    /* have we checked the OS version before? If yes return the previous
51     * result - the OS release isn't going to change suddenly!
52     */
53    if (forktype == bs2_unknown) {
54        /* not initialized yet */
55
56        /* No fork if the one_process option was set */
57        if (one_process) {
58            forktype = bs2_noFORK;
59        }
60        /* If the user is unprivileged, use the normal fork() only. */
61        else if (getuid() != 0) {
62            forktype = bs2_FORK;
63        }
64        else
65            forktype = bs2_UFORK;
66    }
67    return forktype;
68}
69
70
71
72/* This routine complements the setuid() call: it causes the BS2000 job
73 * environment to be switched to the target user's user id.
74 * That is important if CGI scripts try to execute native BS2000 commands.
75 */
76int os_init_job_environment(server_rec *server, const char *user_name, int one_process)
77{
78    bs2_ForkType            type = os_forktype(one_process);
79
80    /* We can be sure that no change to uid==0 is possible because of
81     * the checks in http_core.c:set_user()
82     */
83
84    if (one_process) {
85
86        type = forktype = bs2_noFORK;
87
88        ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, APLOGNO(02170)
89                     "The debug mode of Apache should only "
90                     "be started by an unprivileged user!");
91        return 0;
92    }
93
94    return 0;
95}
96
97/* BS2000 requires a "special" version of fork() before a setuid() call */
98pid_t os_fork(const char *user)
99{
100    pid_t pid;
101    char  username[USER_LEN+1];
102
103    switch (os_forktype(0)) {
104
105      case bs2_FORK:
106        pid = fork();
107        break;
108
109      case bs2_UFORK:
110        apr_cpystrn(username, user, sizeof username);
111
112        /* Make user name all upper case - for some versions of ufork() */
113        ap_str_toupper(username);
114
115        pid = ufork(username);
116        if (pid == -1 && errno == EPERM) {
117            ap_log_error(APLOG_MARK, APLOG_EMERG, errno,
118                         ap_server_conf, APLOGNO(02171) "ufork: Possible mis-configuration "
119                         "for user %s - Aborting.", user);
120            exit(1);
121        }
122        break;
123
124      default:
125        pid = 0;
126        break;
127    }
128
129    return pid;
130}
131
132#else /* _OSD_POSIX */
133void bs2000_os_is_not_here()
134{
135}
136#endif /* _OSD_POSIX */
137