home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2288 / setup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.5 KB  |  143 lines

  1. /*
  2.  * Copyright 1989, 1990, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Use, duplication, and disclosure prohibited without
  6.  * the express written permission of the author.
  7.  */
  8.  
  9. #include <sys/types.h>
  10. #include <pwd.h>
  11. #include <utmp.h>
  12. #ifndef    BSD
  13. #include <string.h>
  14. #include <memory.h>
  15. #else
  16. #include <strings.h>
  17. #define    strchr    index
  18. #define    strrchr    rindex
  19. #endif
  20. #include "config.h"
  21.  
  22. #ifndef    lint
  23. static    char    _sccsid[] = "@(#)setup.c    2.2    19:24:10    7/29/90";
  24. #endif
  25.  
  26. extern    char    home[];
  27. extern    char    prog[];
  28. extern    char    name[];
  29. extern    char    mail[];
  30.  
  31. #ifndef    PATH
  32. #define    PATH    ":/bin:/usr/bin"
  33. #endif
  34.  
  35. #ifndef    SUPATH
  36. #define    SUPATH    ":/bin:/usr/bin:/etc"
  37. #endif
  38.  
  39. #ifndef    MAILDIR
  40. #define    MAILDIR    "/usr/spool/mail"
  41. #endif
  42.  
  43. #ifndef    TTYPERM
  44. #define    TTYPERM    0622
  45. #endif
  46.  
  47. #ifndef    SU
  48. extern    struct    utmp    utent;
  49. #endif
  50.  
  51. #ifdef    QUOTAS
  52. long    strtol ();
  53. #ifdef    ULIMIT
  54. long    ulimit ();
  55. #endif
  56. #endif
  57.  
  58. void    addenv ();
  59.  
  60. void    setup (info)
  61. struct    passwd    *info;
  62. {
  63.     extern    int    errno;
  64.     char    logname[30];
  65. #ifndef    SU
  66.     char    tty[30];
  67. #endif
  68.     char    *cp;
  69.     int    i;
  70.     long    l;
  71.  
  72. #ifndef    SU
  73.     (void) strcat (strcpy (tty, "/dev/"), utent.ut_line);
  74.     if (chown (tty, info->pw_uid, info->pw_gid) ||
  75.                     chmod (tty, TTYPERM))
  76.         perror (tty);
  77. #endif
  78.     if (chdir (info->pw_dir) == -1) {
  79.         (void) printf ("Unable to change directory to \"%s\"\n", info->pw_dir);
  80.         exit (errno);
  81.     }
  82. #ifdef    QUOTAS
  83.     for (cp = info->pw_gecos;cp != (char *) 0;cp = strchr (cp, ',')) {
  84.         if (*cp == ',')
  85.             cp++;
  86.  
  87.         if (strncmp (cp, "pri=", 4) == 0) {
  88.             i = atoi (cp + 4);
  89.             if (i >= -20 && i <= 20)
  90.                 (void) nice (i);
  91.  
  92.             continue;
  93.         }
  94. #ifdef    ULIMIT
  95.         if (strncmp (cp, "ulimit=", 7) == 0) {
  96.             l = strtol (cp + 7, (char **) 0, 10);
  97.             (void) ulimit (2, l);
  98.  
  99.             continue;
  100.         }
  101. #endif
  102.         if (strncmp (cp, "umask=", 6) == 0) {
  103.             i = strtol (cp + 6, (char **) 0, 8) & 0777;
  104.             (void) umask (i);
  105.  
  106.             continue;
  107.         }
  108.     }
  109. #endif
  110.     if (setgid (info->pw_gid) == -1) {
  111.         puts ("Bad group id");
  112.         exit (errno);
  113.     }
  114. #ifndef    BSD
  115.     if (setuid (info->pw_uid))
  116. #else
  117.     if (setreuid (info->pw_uid, info->pw_uid))
  118. #endif
  119.     {
  120.         puts ("Bad user id");
  121.         exit (errno);
  122.     }
  123.     (void) strcat (strcpy (home, "HOME="), info->pw_dir);
  124.     addenv (home);
  125.  
  126.     if (info->pw_shell == (char *) 0)
  127.         info->pw_shell = "/bin/sh";
  128.  
  129.     (void) strcat (strcpy (prog, "SHELL="), info->pw_shell);
  130.     addenv (prog);
  131.  
  132.     if (info->pw_uid == 0)
  133.         addenv (SUPATH);
  134.     else
  135.         addenv (PATH);
  136.  
  137.     (void) strcat (strcpy (logname, "LOGNAME="), name);
  138.     addenv (logname);
  139.  
  140.     (void) strcat (strcat (strcpy (mail, "MAIL="), MAILDIR), name);
  141.     addenv (mail);
  142. }
  143.