]> granicus.if.org Git - apache/blob - os/unix/unixd.c
fix a compile failure with recent z/OS and picky settings for compiler/
[apache] / os / unix / unixd.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2003 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 "ap_config.h"
60 #define CORE_PRIVATE
61 #include "httpd.h"
62 #include "http_config.h"
63 #include "http_main.h"
64 #include "http_log.h"
65 #include "unixd.h"
66 #include "mpm_common.h"
67 #include "os.h"
68 #include "ap_mpm.h"
69 #include "apr_thread_proc.h"
70 #include "apr_strings.h"
71 #include "apr_portable.h"
72 #ifdef HAVE_PWD_H
73 #include <pwd.h>
74 #endif
75 #ifdef HAVE_SYS_RESOURCE_H
76 #include <sys/resource.h>
77 #endif
78 /* XXX */
79 #include <sys/stat.h>
80 #ifdef HAVE_UNISTD_H
81 #include <unistd.h>
82 #endif
83 #ifdef HAVE_GRP_H
84 #include <grp.h>
85 #endif
86 #ifdef HAVE_STRINGS_H
87 #include <strings.h>
88 #endif
89 #ifdef HAVE_SYS_SEM_H
90 #include <sys/sem.h>
91 #endif
92 #ifdef HAVE_SYS_PRCTL_H
93 #include <sys/prctl.h>
94 #endif
95
96 unixd_config_rec unixd_config;
97
98 /* Set group privileges.
99  *
100  * Note that we use the username as set in the config files, rather than
101  * the lookup of to uid --- the same uid may have multiple passwd entries,
102  * with different sets of groups for each.
103  */
104
105 static int set_group_privs(void)
106 {
107     if (!geteuid()) {
108         const char *name;
109
110         /* Get username if passed as a uid */
111
112         if (unixd_config.user_name[0] == '#') {
113             struct passwd *ent;
114             uid_t uid = atoi(&unixd_config.user_name[1]);
115
116             if ((ent = getpwuid(uid)) == NULL) {
117                 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
118                          "getpwuid: couldn't determine user name from uid %u, "
119                          "you probably need to modify the User directive",
120                          (unsigned)uid);
121                 return -1;
122             }
123
124             name = ent->pw_name;
125         }
126         else
127             name = unixd_config.user_name;
128
129 #if !defined(OS2) && !defined(TPF)
130         /* OS/2 and TPF don't support groups. */
131
132         /*
133          * Set the GID before initgroups(), since on some platforms
134          * setgid() is known to zap the group list.
135          */
136         if (setgid(unixd_config.group_id) == -1) {
137             ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
138                         "setgid: unable to set group id to Group %u",
139                         (unsigned)unixd_config.group_id);
140             return -1;
141         }
142
143         /* Reset `groups' attributes. */
144
145         if (initgroups(name, unixd_config.group_id) == -1) {
146             ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
147                         "initgroups: unable to set groups for User %s "
148                         "and Group %u", name, (unsigned)unixd_config.group_id);
149             return -1;
150         }
151 #endif /* !defined(OS2) && !defined(TPF) */
152     }
153     return 0;
154 }
155
156
157 AP_DECLARE(int) unixd_setup_child(void)
158 {
159     if (set_group_privs()) {
160         return -1;
161     }
162 #ifdef MPE
163     /* Only try to switch if we're running as MANAGER.SYS */
164     if (geteuid() == 1 && unixd_config.user_id > 1) {
165         GETPRIVMODE();
166         if (setuid(unixd_config.user_id) == -1) {
167             GETUSERMODE();
168             ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
169                         "setuid: unable to change to uid: %ld",
170                         (long) unixd_config.user_id);
171             exit(1);
172         }
173         GETUSERMODE();
174     }
175 #else
176     /* Only try to switch if we're running as root */
177     if (!geteuid() && (
178 #ifdef _OSD_POSIX
179         os_init_job_environment(NULL, unixd_config.user_name, ap_exists_config_define("DEBUG")) != 0 ||
180 #endif
181         setuid(unixd_config.user_id) == -1)) {
182         ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
183                     "setuid: unable to change to uid: %ld",
184                     (long) unixd_config.user_id);
185         return -1;
186     }
187 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE) 
188     /* this applies to Linux 2.4+ */
189 #ifdef AP_MPM_WANT_SET_COREDUMPDIR
190     if (ap_coredumpdir_configured) {
191         if (prctl(PR_SET_DUMPABLE, 1)) {
192             ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
193                          "set dumpable failed - this child will not coredump"
194                          " after software errors");
195         }
196     }
197 #endif
198 #endif
199 #endif
200     return 0;
201 }
202
203
204 AP_DECLARE(const char *) unixd_set_user(cmd_parms *cmd, void *dummy, 
205                                         const char *arg)
206 {
207     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
208     if (err != NULL) {
209         return err;
210     }
211
212     unixd_config.user_name = arg;
213     unixd_config.user_id = ap_uname2id(arg);
214 #if !defined (BIG_SECURITY_HOLE) && !defined (OS2)
215     if (unixd_config.user_id == 0) {
216         return "Error:\tApache has not been designed to serve pages while\n"
217                 "\trunning as root.  There are known race conditions that\n"
218                 "\twill allow any local user to read any file on the system.\n"
219                 "\tIf you still desire to serve pages as root then\n"
220                 "\tadd -DBIG_SECURITY_HOLE to the CFLAGS env variable\n"
221                 "\tand then rebuild the server.\n"
222                 "\tIt is strongly suggested that you instead modify the User\n"
223                 "\tdirective in your httpd.conf file to list a non-root\n"
224                 "\tuser.\n";
225     }
226 #endif
227
228     return NULL;
229 }
230
231 AP_DECLARE(const char *) unixd_set_group(cmd_parms *cmd, void *dummy, 
232                                          const char *arg)
233 {
234     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
235     if (err != NULL) {
236         return err;
237     }
238
239     unixd_config.group_id = ap_gname2id(arg);
240
241     return NULL;
242 }
243
244 AP_DECLARE(void) unixd_pre_config(apr_pool_t *ptemp)
245 {
246     apr_finfo_t wrapper;
247
248     unixd_config.user_name = DEFAULT_USER;
249     unixd_config.user_id = ap_uname2id(DEFAULT_USER);
250     unixd_config.group_id = ap_gname2id(DEFAULT_GROUP);
251
252     /* Check for suexec */
253     unixd_config.suexec_enabled = 0;
254     if ((apr_stat(&wrapper, SUEXEC_BIN, 
255                   APR_FINFO_NORM, ptemp)) != APR_SUCCESS) {
256         return;
257     }
258
259     /* XXX - apr_stat is incapable of checking suid bits (grumble) */
260     /* if ((wrapper.filetype & S_ISUID) && wrapper.user == 0) { */
261         unixd_config.suexec_enabled = 1;
262     /* } */
263 }
264
265
266 AP_DECLARE(void) unixd_set_rlimit(cmd_parms *cmd, struct rlimit **plimit, 
267                            const char *arg, const char * arg2, int type)
268 {
269 #if (defined(RLIMIT_CPU) || defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_NPROC) || defined(RLIMIT_AS)) && APR_HAVE_STRUCT_RLIMIT && APR_HAVE_GETRLIMIT
270     char *str;
271     struct rlimit *limit;
272     /* If your platform doesn't define rlim_t then typedef it in ap_config.h */
273     rlim_t cur = 0;
274     rlim_t max = 0;
275
276     *plimit = (struct rlimit *)apr_pcalloc(cmd->pool, sizeof(**plimit));
277     limit = *plimit;
278     if ((getrlimit(type, limit)) != 0)  {
279         *plimit = NULL;
280         ap_log_error(APLOG_MARK, APLOG_ERR, errno, cmd->server,
281                      "%s: getrlimit failed", cmd->cmd->name);
282         return;
283     }
284
285     if ((str = ap_getword_conf(cmd->pool, &arg))) {
286         if (!strcasecmp(str, "max")) {
287             cur = limit->rlim_max;
288         }
289         else {
290             cur = atol(str);
291         }
292     }
293     else {
294         ap_log_error(APLOG_MARK, APLOG_ERR, 0, cmd->server,
295                      "Invalid parameters for %s", cmd->cmd->name);
296         return;
297     }
298
299     if (arg2 && (str = ap_getword_conf(cmd->pool, &arg2))) {
300         max = atol(str);
301     }
302
303     /* if we aren't running as root, cannot increase max */
304     if (geteuid()) {
305         limit->rlim_cur = cur;
306         if (max) {
307             ap_log_error(APLOG_MARK, APLOG_ERR, 0, cmd->server,
308                          "Must be uid 0 to raise maximum %s", cmd->cmd->name);
309         }
310     }
311     else {
312         if (cur) {
313             limit->rlim_cur = cur;
314         }
315         if (max) {
316             limit->rlim_max = max;
317         }
318     }
319 #else
320
321     ap_log_error(APLOG_MARK, APLOG_ERR, 0, cmd->server,
322                  "Platform does not support rlimit for %s", cmd->cmd->name);
323 #endif
324 }
325
326 APR_HOOK_STRUCT(
327                APR_HOOK_LINK(get_suexec_identity)
328 )
329
330 AP_IMPLEMENT_HOOK_RUN_FIRST(ap_unix_identity_t *, get_suexec_identity,
331                          (const request_rec *r), (r), NULL)
332
333 static apr_status_t ap_unix_create_privileged_process(
334                               apr_proc_t *newproc, const char *progname,
335                               const char * const *args,
336                               const char * const *env,
337                               apr_procattr_t *attr, ap_unix_identity_t *ugid,
338                               apr_pool_t *p)
339 {
340     int i = 0;
341     const char **newargs;
342     char *newprogname;
343     char *execuser, *execgroup;
344     const char *argv0;
345
346     if (!unixd_config.suexec_enabled) {
347         return apr_proc_create(newproc, progname, args, env, attr, p);
348     }
349
350     argv0 = ap_strrchr_c(progname, '/');
351     /* Allow suexec's "/" check to succeed */
352     if (argv0 != NULL) {
353         argv0++;
354     }
355     else {
356         argv0 = progname;
357     }
358
359
360     if (ugid->userdir) {
361         execuser = apr_psprintf(p, "~%ld", (long) ugid->uid);
362     }
363     else {
364         execuser = apr_psprintf(p, "%ld", (long) ugid->uid);
365     }
366     execgroup = apr_psprintf(p, "%ld", (long) ugid->gid);
367
368     if (!execuser || !execgroup) {
369         return APR_ENOMEM;
370     }
371
372     i = 0;
373     if (args) {
374         while (args[i]) {
375             i++;
376             }
377     }
378     /* allocate space for 4 new args, the input args, and a null terminator */
379     newargs = apr_palloc(p, sizeof(char *) * (i + 4));
380     newprogname = SUEXEC_BIN;
381     newargs[0] = SUEXEC_BIN;
382     newargs[1] = execuser;
383     newargs[2] = execgroup;
384     newargs[3] = apr_pstrdup(p, argv0);
385
386     /*
387     ** using a shell to execute suexec makes no sense thus
388     ** we force everything to be APR_PROGRAM, and never
389     ** APR_SHELLCMD
390     */
391     if(apr_procattr_cmdtype_set(attr, APR_PROGRAM) != APR_SUCCESS) {
392         return APR_EGENERAL;
393     }
394
395     i = 1;
396     do {
397         newargs[i + 3] = args[i];
398     } while (args[i++]);
399
400     return apr_proc_create(newproc, newprogname, newargs, env, attr, p);
401 }
402
403 AP_DECLARE(apr_status_t) ap_os_create_privileged_process(
404     const request_rec *r,
405     apr_proc_t *newproc, const char *progname,
406     const char * const *args,
407     const char * const *env,
408     apr_procattr_t *attr, apr_pool_t *p)
409 {
410     ap_unix_identity_t *ugid = ap_run_get_suexec_identity(r);
411
412     if (ugid == NULL) {
413         return apr_proc_create(newproc, progname, args, env, attr, p);
414     }
415
416     return ap_unix_create_privileged_process(newproc, progname, args, env,
417                                               attr, ugid, p);
418 }
419
420 /* XXX move to APR and externalize (but implement differently :) ) */
421 static apr_lockmech_e proc_mutex_mech(apr_proc_mutex_t *pmutex)
422 {
423     const char *mechname = apr_proc_mutex_name(pmutex);
424
425     if (!strcmp(mechname, "sysvsem")) {
426         return APR_LOCK_SYSVSEM;
427     }
428     else if (!strcmp(mechname, "flock")) {
429         return APR_LOCK_FLOCK;
430     }
431     return APR_LOCK_DEFAULT;
432 }
433
434 AP_DECLARE(apr_status_t) unixd_set_proc_mutex_perms(apr_proc_mutex_t *pmutex)
435 {
436     if (!geteuid()) {
437         apr_lockmech_e mech = proc_mutex_mech(pmutex);
438
439         switch(mech) {
440 #if APR_HAS_SYSVSEM_SERIALIZE
441         case APR_LOCK_SYSVSEM:
442         {
443             apr_os_proc_mutex_t ospmutex;
444 #if !APR_HAVE_UNION_SEMUN
445             union semun {
446                 long val;
447                 struct semid_ds *buf;
448                 unsigned short *array;
449             };
450 #endif
451             union semun ick;
452             struct semid_ds buf;
453         
454             apr_os_proc_mutex_get(&ospmutex, pmutex);
455             buf.sem_perm.uid = unixd_config.user_id;
456             buf.sem_perm.gid = unixd_config.group_id;
457             buf.sem_perm.mode = 0600;
458             ick.buf = &buf;
459             if (semctl(ospmutex.crossproc, 0, IPC_SET, ick) < 0) {
460                 return errno;
461             }
462         }
463         break;
464 #endif
465 #if APR_HAS_FLOCK_SERIALIZE
466         case APR_LOCK_FLOCK:
467         {
468             const char *lockfile = apr_proc_mutex_lockfile(pmutex);
469
470             if (lockfile) {
471                 if (chown(lockfile, unixd_config.user_id,
472                           -1 /* no gid change */) < 0) {
473                     return errno;
474                 }
475             }
476         }
477         break;
478 #endif
479         default:
480             /* do nothing */
481             break;
482         }
483     }
484     return APR_SUCCESS;
485 }
486
487 AP_DECLARE(apr_status_t) unixd_set_global_mutex_perms(apr_global_mutex_t *gmutex)
488 {
489 #if !APR_PROC_MUTEX_IS_GLOBAL
490     apr_os_global_mutex_t osgmutex;
491     apr_os_global_mutex_get(&osgmutex, gmutex);
492     return unixd_set_proc_mutex_perms(osgmutex.proc_mutex);
493 #else  /* APR_PROC_MUTEX_IS_GLOBAL */
494     /* In this case, apr_proc_mutex_t and apr_global_mutex_t are the same. */
495     return unixd_set_proc_mutex_perms(gmutex);
496 #endif /* APR_PROC_MUTEX_IS_GLOBAL */
497 }
498
499 AP_DECLARE(apr_status_t) unixd_accept(void **accepted, ap_listen_rec *lr,
500                                         apr_pool_t *ptrans)
501 {
502     apr_socket_t *csd;
503     apr_status_t status;
504     int sockdes;
505
506     *accepted = NULL;
507     status = apr_socket_accept(&csd, lr->sd, ptrans);
508     if (status == APR_SUCCESS) { 
509         *accepted = csd;
510         apr_os_sock_get(&sockdes, csd);
511         if (sockdes >= FD_SETSIZE) {
512             ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL,
513                          "new file descriptor %d is too large; you probably need "
514                          "to rebuild Apache with a larger FD_SETSIZE "
515                          "(currently %d)",
516                          sockdes, FD_SETSIZE);
517             apr_socket_close(csd);
518             return APR_EINTR;
519         } 
520 #ifdef TPF
521         if (sockdes == 0) {                  /* 0 is invalid socket for TPF */
522             return APR_EINTR;
523         }
524 #endif
525         return status;
526     }
527
528     if (APR_STATUS_IS_EINTR(status)) {
529         return status;
530     }
531     /* Our old behaviour here was to continue after accept()
532      * errors.  But this leads us into lots of troubles
533      * because most of the errors are quite fatal.  For
534      * example, EMFILE can be caused by slow descriptor
535      * leaks (say in a 3rd party module, or libc).  It's
536      * foolish for us to continue after an EMFILE.  We also
537      * seem to tickle kernel bugs on some platforms which
538      * lead to never-ending loops here.  So it seems best
539      * to just exit in most cases.
540      */
541     switch (status) {
542 #if defined(HPUX11) && defined(ENOBUFS)
543         /* On HPUX 11.x, the 'ENOBUFS, No buffer space available'
544          * error occurs because the accept() cannot complete.
545          * You will not see ENOBUFS with 10.20 because the kernel
546          * hides any occurrence from being returned to user space.
547          * ENOBUFS with 11.x's TCP/IP stack is possible, and could
548          * occur intermittently. As a work-around, we are going to
549          * ignore ENOBUFS.
550          */
551         case ENOBUFS:
552 #endif
553
554 #ifdef EPROTO
555         /* EPROTO on certain older kernels really means
556          * ECONNABORTED, so we need to ignore it for them.
557          * See discussion in new-httpd archives nh.9701
558          * search for EPROTO.
559          *
560          * Also see nh.9603, search for EPROTO:
561          * There is potentially a bug in Solaris 2.x x<6,
562          * and other boxes that implement tcp sockets in
563          * userland (i.e. on top of STREAMS).  On these
564          * systems, EPROTO can actually result in a fatal
565          * loop.  See PR#981 for example.  It's hard to
566          * handle both uses of EPROTO.
567          */
568         case EPROTO:
569 #endif
570 #ifdef ECONNABORTED
571         case ECONNABORTED:
572 #endif
573         /* Linux generates the rest of these, other tcp
574          * stacks (i.e. bsd) tend to hide them behind
575          * getsockopt() interfaces.  They occur when
576          * the net goes sour or the client disconnects
577          * after the three-way handshake has been done
578          * in the kernel but before userland has picked
579          * up the socket.
580          */
581 #ifdef ECONNRESET
582         case ECONNRESET:
583 #endif
584 #ifdef ETIMEDOUT
585         case ETIMEDOUT:
586 #endif
587 #ifdef EHOSTUNREACH
588         case EHOSTUNREACH:
589 #endif
590 #ifdef ENETUNREACH
591         case ENETUNREACH:
592 #endif
593             break;
594 #ifdef ENETDOWN
595         case ENETDOWN:
596             /*
597              * When the network layer has been shut down, there
598              * is not much use in simply exiting: the parent
599              * would simply re-create us (and we'd fail again).
600              * Use the CHILDFATAL code to tear the server down.
601              * @@@ Martin's idea for possible improvement:
602              * A different approach would be to define
603              * a new APEXIT_NETDOWN exit code, the reception
604              * of which would make the parent shutdown all
605              * children, then idle-loop until it detected that
606              * the network is up again, and restart the children.
607              * Ben Hyde noted that temporary ENETDOWN situations
608              * occur in mobile IP.
609              */
610             ap_log_error(APLOG_MARK, APLOG_EMERG, status, ap_server_conf,
611                          "apr_socket_accept: giving up.");
612             return APR_EGENERAL;
613 #endif /*ENETDOWN*/
614
615 #ifdef TPF
616         case EINACT:
617             ap_log_error(APLOG_MARK, APLOG_EMERG, status, ap_server_conf,
618                          "offload device inactive");
619             return APR_EGENERAL;
620             break;
621         default:
622             ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf,
623                          "select/accept error (%d)", status);
624             return APR_EGENERAL;
625 #else
626         default:
627             ap_log_error(APLOG_MARK, APLOG_ERR, status, ap_server_conf,
628                          "apr_socket_accept: (client socket)");
629             return APR_EGENERAL;
630 #endif
631     }
632     return status;
633 }
634