~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to os/bs2000/os.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

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