home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume6
/
shadow-2.pt3
/
smain.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-02-03
|
4KB
|
159 lines
#include <sys/types.h>
#include <stdio.h>
#include <pwd.h>
#include <string.h>
#include <signal.h>
#include "config.h"
#include "lastlog.h"
#ifndef MAXENV
#define MAXENV 64
#endif
char name[BUFSIZ];
char pass[BUFSIZ];
char home[BUFSIZ];
char prog[BUFSIZ];
char mail[BUFSIZ];
char oldname[BUFSIZ];
char *newenvp[MAXENV];
int newenvc = 0;
int maxenv = MAXENV;
struct passwd pwent;
void addenv ();
void entry ();
void sulog ();
void subsystem ();
void setup ();
void motd ();
void mailcheck ();
void shell ();
extern char **environ;
int main (argc, argv, envp)
int argc;
char **argv;
char **envp;
{
char *getenv ();
int doshell;
int fakelogin = 0;
int amroot;
struct passwd *pw;
struct passwd *getpwuid ();
while (*envp) /* add inherited environment, */
addenv (*envp++); /* some variables change later */
#ifdef TZ
addenv (TZ); /* set the default $TZ, if one */
#endif
#ifdef HZ
addenv (HZ); /* set the default $HZ, if one */
#endif
argc--; argv++; /* shift out command name */
if (argc > 0 && argv[0][0] == '-' && argv[0][1] == '\0') {
fakelogin = 1;
argc--; argv++; /* shift ... */
}
if (argc > 0 && argv[0][0] != '-') {
(void) strcpy (name, argv[0]); /* use this login id */
argc--; argv++; /* shift ... */
}
doshell = argc == 0; /* any arguments remaining? */
if (pw = getpwuid (getuid ())) /* need old user name */
(void) strcpy (oldname, pw->pw_name);
else /* user ID MUST exist */
goto failure;
amroot = getuid () == 0; /* currently am super user */
if (! name[0]) /* use default user ID */
(void) strcpy (name, "root");
entry (name, &pwent); /* get password file entry */
if (pwent.pw_name == (char *) 0) { /* unknown user */
(void) fprintf (stderr, "Unknown id: %s\n", pwent.pw_name);
exit (1);
}
/*
* Here we have a sticky situation. Some accounts may have no
* password entry in the password file. So, we don't ask for a
* password. Others, have a blank password entered - you be the
* judge. The conditional compilation NOBLANK requires even
* blank passwords to be prompted for. This may well break
* quite a few systems. Use with discretion.
*/
#ifdef NOBLANK
if (! amroot && ! password ("Password:", pass))
goto failure;
#else
if (! amroot && (pwent.pw_name == (char *) 0 || pwent.pw_passwd)
&& ! password ("Password:", pass))
goto failure;
#endif
/* check encrypted passwords ... */
if (! amroot && ! valid (pass, &pwent)) {
failure: sulog (0); /* log failed attempt */
puts ("Sorry.");
exit (1);
}
#ifdef SULOG
sulog (1); /* save SU information */
#endif
if (pwent.pw_uid == 0)
addenv (SUPATH);
else
addenv (PATH);
environ = newenvp; /* make new environment active */
if (getenv ("IFS")) /* don't export user IFS ... */
addenv ("IFS= \t\n"); /* ... instead, set a safe IFS */
if (doshell && pwent.pw_shell[0] == '*') /* subsystem root required */
subsystem (); /* figure out what to execute */
if (fakelogin)
setup (&pwent); /* set UID, GID, HOME, etc ... */
else {
if (setgid (pwent.pw_gid) || setuid (pwent.pw_uid)) {
perror ("Can't set ID");
exit (1);
}
}
if (! doshell) { /* execute arguments as command */
if (! pwent.pw_shell)
pwent.pw_shell = "/bin/sh";
argv[-1] = pwent.pw_shell;
(void) execv (pwent.pw_shell, &argv[-1]);
(void) fprintf (stderr, "No shell\n");
exit (1);
}
if (fakelogin) {
#ifdef MOTD
motd (); /* print the message of the day */
#endif
#ifdef MAILCHECK
mailcheck (); /* report on the status of mail */
#endif
shell (pwent.pw_shell); /* exec the shell finally. */
} else {
if (pwent.pw_shell == (char *) 0)
pwent.pw_shell = "/bin/sh";
execl (pwent.pw_shell, "su", (char *) 0);
perror (pwent.pw_shell);
exit (1);
}
/*NOTREACHED*/
}