home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3343 / setup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-17  |  3.5 KB  |  168 lines

  1. /*
  2.  * Copyright 1989, 1990, 1991, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #include <sys/types.h>
  13. #include <stdio.h>
  14. #include <utmp.h>
  15. #include <syslog.h>
  16.  
  17. #ifdef    BSD
  18. #include <strings.h>
  19. #define    strchr    index
  20. #else
  21. #include <string.h>
  22. #include <memory.h>
  23. #endif
  24.  
  25. #include "config.h"
  26. #include "pwd.h"
  27.  
  28. #ifndef    lint
  29. static    char    sccsid[] = "@(#)setup.c    3.3    07:46:41    2/6/91";
  30. #endif
  31.  
  32. #ifndef    PATH
  33. #define    PATH    "PATH=/bin:/usr/bin"
  34. #endif
  35.  
  36. #ifndef    SUPATH
  37. #define    SUPATH    "PATH=/bin:/usr/bin:/etc"
  38. #endif
  39.  
  40. #ifndef    MAILDIR
  41. #define    MAILDIR    "/usr/spool/mail"
  42. #endif
  43.  
  44. #ifndef    TTYPERM
  45. #define    TTYPERM    0622
  46. #endif
  47.  
  48. #ifndef    SU
  49. extern    struct    utmp    utent;
  50. #endif
  51.  
  52. #ifdef    QUOTAS
  53. long    strtol ();
  54. #ifdef    ULIMIT
  55. long    ulimit ();
  56. #endif
  57. #endif
  58.  
  59. void    addenv ();
  60.  
  61. /*
  62.  * setup - initialize login environment
  63.  *
  64.  *    setup() performs the following steps -
  65.  *
  66.  *    set the login tty to be owned by the new user ID with TTYPERM modes
  67.  *    change to the user's home directory
  68.  *    set the process nice, ulimit, and umask from the password file entry
  69.  *    set the group ID to the value from the password file entry
  70.  *    set the user ID to the value from the password file entry
  71.  *    set the HOME, SHELL, MAIL, PATH, and LOGNAME environmental variables
  72.  */
  73.  
  74. void    setup (info)
  75. struct    passwd    *info;
  76. {
  77.     extern    int    errno;
  78.     char    buf[BUFSIZ];
  79. #ifndef    SU
  80.     char    tty[30];
  81. #endif
  82.     char    *cp;
  83.     int    i;
  84.     long    l;
  85.  
  86. #ifndef    SU
  87.     (void) strcat (strcpy (tty, "/dev/"), utent.ut_line);
  88.     if (chown (tty, info->pw_uid, info->pw_gid) || chmod (tty, TTYPERM)) {
  89.         (void) sprintf (buf, "Unable to change tty %s", tty);
  90.         syslog (LOG_WARN, "unable to change tty `%s' for user `%s'",
  91.             tty, info->pw_name);
  92.         perror (buf);
  93.         exit (errno);
  94.     }
  95. #endif
  96.     if (chdir (info->pw_dir) == -1) {
  97.         (void) sprintf (buf, "Unable to cd to \"%s\"", info->pw_dir);
  98.         syslog (LOG_WARN, "unable to cd to `%s' for user `%s'",
  99.             info->pw_dir, info->pw_name);
  100.         perror (buf);
  101.         exit (errno);
  102.     }
  103. #ifdef    QUOTAS
  104.     for (cp = info->pw_gecos;cp != (char *) 0;cp = strchr (cp, ',')) {
  105.         if (*cp == ',')
  106.             cp++;
  107.  
  108.         if (strncmp (cp, "pri=", 4) == 0) {
  109.             i = atoi (cp + 4);
  110.             if (i >= -20 && i <= 20)
  111.                 (void) nice (i);
  112.  
  113.             continue;
  114.         }
  115. #ifdef    ULIMIT
  116.         if (strncmp (cp, "ulimit=", 7) == 0) {
  117.             l = strtol (cp + 7, (char **) 0, 10);
  118.             (void) ulimit (2, l);
  119.  
  120.             continue;
  121.         }
  122. #endif
  123.         if (strncmp (cp, "umask=", 6) == 0) {
  124.             i = strtol (cp + 6, (char **) 0, 8) & 0777;
  125.             (void) umask (i);
  126.  
  127.             continue;
  128.         }
  129.     }
  130. #endif
  131.     if (setgid (info->pw_gid) == -1) {
  132.         puts ("Bad group id");
  133.         syslog (LOG_WARN, "bad group ID `%d' for user `%s'",
  134.             info->pw_gid, info->pw_name);
  135.         exit (errno);
  136.     }
  137. #ifndef    BSD
  138.     if (setuid (info->pw_uid))
  139. #else
  140.     if (setreuid (info->pw_uid, info->pw_uid))
  141. #endif
  142.     {
  143.         puts ("Bad user id");
  144.         syslog (LOG_WARN, "bad user ID `%d' for user `%s'",
  145.             info->pw_uid, info->pw_name);
  146.         exit (errno);
  147.     }
  148.     (void) strcat (strcpy (buf, "HOME="), info->pw_dir);
  149.     addenv (buf);
  150.  
  151.     if (info->pw_shell == (char *) 0)
  152.         info->pw_shell = "/bin/sh";
  153.  
  154.     (void) strcat (strcpy (buf, "SHELL="), info->pw_shell);
  155.     addenv (buf);
  156.  
  157.     if (info->pw_uid == 0)
  158.         addenv (SUPATH);
  159.     else
  160.         addenv (PATH);
  161.  
  162.     (void) strcat (strcpy (buf, "LOGNAME="), info->pw_name);
  163.     addenv (buf);
  164.  
  165.     (void) strcat (strcat (strcpy (buf, "MAIL="), MAILDIR), info->pw_name);
  166.     addenv (buf);
  167. }
  168.