home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / os / bs2000 / bs2login.c next >
Encoding:
C/C++ Source or Header  |  1999-01-01  |  7.9 KB  |  243 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1995-1999 The Apache Group.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer. 
  10.  *
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in
  13.  *    the documentation and/or other materials provided with the
  14.  *    distribution.
  15.  *
  16.  * 3. All advertising materials mentioning features or use of this
  17.  *    software must display the following acknowledgment:
  18.  *    "This product includes software developed by the Apache Group
  19.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  20.  *
  21.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  22.  *    endorse or promote products derived from this software without
  23.  *    prior written permission. For written permission, please contact
  24.  *    apache@apache.org.
  25.  *
  26.  * 5. Products derived from this software may not be called "Apache"
  27.  *    nor may "Apache" appear in their names without prior written
  28.  *    permission of the Apache Group.
  29.  *
  30.  * 6. Redistributions of any form whatsoever must retain the following
  31.  *    acknowledgment:
  32.  *    "This product includes software developed by the Apache Group
  33.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  36.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Group and was originally based
  51.  * on public domain software written at the National Center for
  52.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  53.  * For more information on the Apache Group and the Apache HTTP server
  54.  * project, please see <http://www.apache.org/>.
  55.  *
  56.  */
  57.  
  58. #ifdef _OSD_POSIX
  59. #include "httpd.h"
  60. #include "http_config.h"
  61. #include "http_log.h"
  62. #include <ctype.h>
  63. #include <sys/utsname.h>
  64.  
  65. #define ACCT_LEN 8
  66. #define USER_LEN 8
  67.  
  68. static const char *bs2000_account = NULL;
  69.  
  70.  
  71. static void ap_pad(char *dest, size_t size, char ch)
  72. {
  73.     int i = strlen(dest); /* Leave space for trailing '\0' */
  74.     
  75.     while (i < size-1)
  76.     dest[i++] = ch;
  77.  
  78.     dest[size-1] = '\0';    /* Guarantee for trailing '\0' */
  79. }
  80.  
  81. static void ap_str_toupper(char *str)
  82. {
  83.     while (*str) {
  84.     *str = ap_toupper(*str);
  85.     ++str;
  86.     }
  87. }
  88.  
  89. /* This routine is called by http_core for the BS2000Account directive */
  90. /* It stores the account name for later use */
  91. const char *os_set_account(pool *p, const char *account)
  92. {
  93.     char account_temp[ACCT_LEN+1];
  94.  
  95.     ap_cpystrn(account_temp, account, sizeof account_temp);
  96.  
  97.     /* Make account all upper case */
  98.     ap_str_toupper(account_temp);
  99.  
  100.     /* Pad to length 8 */
  101.     ap_pad(account_temp, sizeof account_temp, ' ');
  102.  
  103.     bs2000_account = ap_pstrdup(p, account_temp);
  104.     return NULL;
  105. }
  106.  
  107. /* This routine complements the setuid() call: it causes the BS2000 job
  108.  * environment to be switched to the target user's user id.
  109.  * That is important if CGI scripts try to execute native BS2000 commands.
  110.  */
  111. int os_init_job_environment(server_rec *server, const char *user_name, int one_process)
  112. {
  113.     _rini_struct            inittask; 
  114.     char                    username[USER_LEN+1];
  115.     int                     save_errno;
  116.  
  117.     /* We can be sure that no change to uid==0 is possible because of
  118.      * the checks in http_core.c:set_user()
  119.      */
  120.  
  121.     /* The _rini() function works only after a prior _rfork().
  122.      * In the case of one_process, it would fail.
  123.      */
  124.     /* An Account is required for _rini() */
  125.     if (bs2000_account == NULL)
  126.     {
  127.     ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO, server,
  128.              "No BS2000Account configured - cannot switch to User %s",
  129.              user_name);
  130.     exit(APEXIT_CHILDFATAL);
  131.     }
  132.  
  133.     /* The one_process test is placed _behind_ the BS2000Account test
  134.      * because we never want the user to forget configuring an account.
  135.      */
  136.     if (one_process) {
  137.     ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, server,
  138.              "The debug mode of Apache should only "
  139.              "be started by an unprivileged user!");
  140.     return 0;
  141.     }
  142.  
  143.     ap_cpystrn(username, user_name, sizeof username);
  144.  
  145.     /* Make user name all upper case */
  146.     ap_str_toupper(username);
  147.  
  148.     /* Pad to length 8 */
  149.     ap_pad(username, sizeof username, ' ');
  150.  
  151.     inittask.username       = username;
  152.     inittask.account        = bs2000_account;
  153.     inittask.processor_name = "        ";
  154.  
  155.     /* Switch to the new logon user (setuid() and setgid() are done later) */
  156.     /* Only the super use can switch identities. */
  157.     if (_rini(&inittask) != 0) {
  158.     save_errno = errno;
  159.  
  160.     ap_log_error(APLOG_MARK, APLOG_ALERT, server,
  161.              "_rini: BS2000 auth failed for user \"%s\" acct \"%s\"",
  162.              inittask.username, inittask.account);
  163.  
  164.     if (save_errno == EAGAIN) {
  165.         /* This funny error code does NOT mean that the operation should
  166.          * be retried. Instead it means that authentication failed
  167.          * because of possibly incompatible `JOBCLASS'es between
  168.          * the calling (SYSROOT) and the target non-privileged user id.
  169.          * Help the administrator by logging a hint.
  170.          */
  171.         char *curr_user, curr_uid[L_cuserid];
  172.  
  173.         if ((curr_user = cuserid(curr_uid)) == NULL) {
  174.         /* This *SHOULD* not occur. But if it does, deal with it. */
  175.         ap_snprintf(curr_uid, sizeof curr_uid, "#%u", getuid());
  176.         curr_user = curr_uid;
  177.         }
  178.  
  179.         ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO, server,
  180.              "_rini: Hint: Possible reason: JOBCLASS of user %s "
  181.              "not compatible with that of user %s ?",
  182.              curr_user, inittask.username);
  183.     }
  184.     exit(APEXIT_CHILDFATAL);
  185.     }
  186.  
  187.     return 0;
  188. }
  189.  
  190. /* BS2000 requires a "special" version of fork() before a setuid()/_rini() call */
  191. /* Additionally, there's an OS release dependency here :-((( */
  192. /* I'm sorry, but there was no other way to make it work.  -Martin */
  193. pid_t os_fork(void)
  194. {
  195.     struct utsname os_version;
  196.  
  197.     /*
  198.      * When we run as a normal user (and bypass setuid() and _rini()),
  199.      * we use the regular fork().
  200.      */
  201.     if (getuid() != 0) {
  202.     return fork();
  203.     }
  204.  
  205.     if (uname(&os_version) >= 0)
  206.     {
  207.     /*
  208.      * Old versions (before XPG4 SPEC1170) don't work with Apache
  209.      * and they require a fork(), not a _rfork()
  210.      */
  211.     if (strcmp(os_version.release, "01.0A") == 0 ||
  212.         strcmp(os_version.release, "02.0A") == 0 ||
  213.         strcmp(os_version.release, "02.1A") == 0)
  214.     {
  215.         return fork();
  216.     }
  217.  
  218.     /* The following versions are special:
  219.      * OS versions before A17 work with regular fork() only,
  220.      * later versions with _rfork() only.
  221.      */
  222.     if (strcmp(os_version.release, "01.1A") == 0 ||
  223.         strcmp(os_version.release, "03.0A") == 0 ||
  224.         strcmp(os_version.release, "03.1A") == 0 ||
  225.         strcmp(os_version.release, "04.0A") == 0)
  226.     {
  227.         return (strcmp (os_version.version, "A17") < 0)
  228.             ? fork() : _rfork();
  229.     }
  230.     }
  231.  
  232.     /* All later OS versions will require _rfork()
  233.      * to prepare for authorization with _rini()
  234.      */
  235.     return _rfork();
  236. }
  237.  
  238. #else /* _OSD_POSIX */
  239. void bs2login_is_not_here()
  240. {
  241. }
  242. #endif /* _OSD_POSIX */
  243.