home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1989, 1990, 1991, John F. Haugh II
- * All rights reserved.
- *
- * Permission is granted to copy and create derivative works for any
- * non-commercial purpose, provided this copyright notice is preserved
- * in all copies of source code, or included in human readable form
- * and conspicuously displayed on all copies of object code or
- * distribution media.
- */
-
- #include <sys/types.h>
- #include <stdio.h>
- #include <utmp.h>
- #include <syslog.h>
-
- #ifdef BSD
- #include <strings.h>
- #define strchr index
- #else
- #include <string.h>
- #include <memory.h>
- #endif
-
- #include "config.h"
- #include "pwd.h"
-
- #ifndef lint
- static char sccsid[] = "@(#)setup.c 3.3 07:46:41 2/6/91";
- #endif
-
- #ifndef PATH
- #define PATH "PATH=/bin:/usr/bin"
- #endif
-
- #ifndef SUPATH
- #define SUPATH "PATH=/bin:/usr/bin:/etc"
- #endif
-
- #ifndef MAILDIR
- #define MAILDIR "/usr/spool/mail"
- #endif
-
- #ifndef TTYPERM
- #define TTYPERM 0622
- #endif
-
- #ifndef SU
- extern struct utmp utent;
- #endif
-
- #ifdef QUOTAS
- long strtol ();
- #ifdef ULIMIT
- long ulimit ();
- #endif
- #endif
-
- void addenv ();
-
- /*
- * setup - initialize login environment
- *
- * setup() performs the following steps -
- *
- * set the login tty to be owned by the new user ID with TTYPERM modes
- * change to the user's home directory
- * set the process nice, ulimit, and umask from the password file entry
- * set the group ID to the value from the password file entry
- * set the user ID to the value from the password file entry
- * set the HOME, SHELL, MAIL, PATH, and LOGNAME environmental variables
- */
-
- void setup (info)
- struct passwd *info;
- {
- extern int errno;
- char buf[BUFSIZ];
- #ifndef SU
- char tty[30];
- #endif
- char *cp;
- int i;
- long l;
-
- #ifndef SU
- (void) strcat (strcpy (tty, "/dev/"), utent.ut_line);
- if (chown (tty, info->pw_uid, info->pw_gid) || chmod (tty, TTYPERM)) {
- (void) sprintf (buf, "Unable to change tty %s", tty);
- syslog (LOG_WARN, "unable to change tty `%s' for user `%s'",
- tty, info->pw_name);
- perror (buf);
- exit (errno);
- }
- #endif
- if (chdir (info->pw_dir) == -1) {
- (void) sprintf (buf, "Unable to cd to \"%s\"", info->pw_dir);
- syslog (LOG_WARN, "unable to cd to `%s' for user `%s'",
- info->pw_dir, info->pw_name);
- perror (buf);
- exit (errno);
- }
- #ifdef QUOTAS
- for (cp = info->pw_gecos;cp != (char *) 0;cp = strchr (cp, ',')) {
- if (*cp == ',')
- cp++;
-
- if (strncmp (cp, "pri=", 4) == 0) {
- i = atoi (cp + 4);
- if (i >= -20 && i <= 20)
- (void) nice (i);
-
- continue;
- }
- #ifdef ULIMIT
- if (strncmp (cp, "ulimit=", 7) == 0) {
- l = strtol (cp + 7, (char **) 0, 10);
- (void) ulimit (2, l);
-
- continue;
- }
- #endif
- if (strncmp (cp, "umask=", 6) == 0) {
- i = strtol (cp + 6, (char **) 0, 8) & 0777;
- (void) umask (i);
-
- continue;
- }
- }
- #endif
- if (setgid (info->pw_gid) == -1) {
- puts ("Bad group id");
- syslog (LOG_WARN, "bad group ID `%d' for user `%s'",
- info->pw_gid, info->pw_name);
- exit (errno);
- }
- #ifndef BSD
- if (setuid (info->pw_uid))
- #else
- if (setreuid (info->pw_uid, info->pw_uid))
- #endif
- {
- puts ("Bad user id");
- syslog (LOG_WARN, "bad user ID `%d' for user `%s'",
- info->pw_uid, info->pw_name);
- exit (errno);
- }
- (void) strcat (strcpy (buf, "HOME="), info->pw_dir);
- addenv (buf);
-
- if (info->pw_shell == (char *) 0)
- info->pw_shell = "/bin/sh";
-
- (void) strcat (strcpy (buf, "SHELL="), info->pw_shell);
- addenv (buf);
-
- if (info->pw_uid == 0)
- addenv (SUPATH);
- else
- addenv (PATH);
-
- (void) strcat (strcpy (buf, "LOGNAME="), info->pw_name);
- addenv (buf);
-
- (void) strcat (strcat (strcpy (buf, "MAIL="), MAILDIR), info->pw_name);
- addenv (buf);
- }
-