login.c
wsh/src/tcp-s/login.c · 9.3 KB
From the archive wsh-147.tar.gz
Download the archive « Back to the archive contents
/* login.c Shareware Copyright by Sam Lantinga 5/6/93 */
#include "tcp.h"
#include <utmp.h>
#include <setjmp.h>
#include "login.h"
#ifdef NEEDSHADOW
#include <shadow.h>
#endif
#ifdef DEBUG
#undef UTMP_FILE
#define UTMP_FILE "/tmp/utmp"
#else
#ifndef UTMP_FILE
#define UTMP_FILE "/etc/utmp"
#endif /* UTMP_FILE */
#endif /* DEBUG */
static char *get_line();
static jmp_buf timejmp;
void time_jump()
{
(void) longjmp(timejmp, 1);
}
/* This is the login/password authentication routine */
struct passwd *login_auth(fd)
int fd; /* The socket r/w file descriptor */
{
int noentry=1; /* No authorization yet */
int tries=RETRIES; /* The number of retries */
char login[BUFSIZ]; /* The login name */
char pass[BUFSIZ]; /* The password */
char *passptr; /* The encrypted passwd */
char *ptr; /* Miscellaneous pointer */
struct passwd *pw; /* Password structure */
#ifdef NEEDSHADOW
struct spwd *spw; /* The shadow structure */
#endif
struct stat sb; /* That generic stat structure */
#ifdef WELCOME
/* Print out the welcome message */
write(fd, WELCOME, strlen(WELCOME));
#endif
if ( setjmp(timejmp) ) { /* We are returming from a timeout alarm */
sprintf(login, "Login timed out after %d seconds\r\n", TIMEOUT);
write(fd, login, strlen(login));
return(NULL);
}
signal(SIGALRM, time_jump);
alarm(TIMEOUT);
while ( noentry && tries )
{
write(fd, LOGIN, strlen(LOGIN));
if ( get_line(fd, login, BUFSIZ, 1) == NULL )
break;
if ( strlen(login) == 0 )
continue;
--tries;
/*
* You can replace getpwnam() with any routine that takes
* a login name and returns a struct passwd for that login,
* or NULL for a nonexistent login name.
*/
if ( (pw=(struct passwd *)getpwnam(login)) == NULL )
{
write(fd, "Password: ", 10);
get_line(fd, pass, BUFSIZ, 0);
ptr="Login incorrect\r\n";
write(fd, ptr, strlen(ptr));
continue;
}
#ifdef NEEDSHADOW
if ( (spw=getspnam(login)) == NULL )
{
write(fd, "Password: ", 10);
get_line(fd, pass, BUFSIZ, 0);
ptr="Login incorrect\r\n";
write(fd, ptr, strlen(ptr));
continue;
}
else
passptr=spw->sp_pwdp;
#else
passptr=pw->pw_passwd;
#endif
if ( ! *passptr ) /* If no passwd, allow entry */
goto logit;
write(fd, "Password: ", 10);
get_line(fd, pass, BUFSIZ, 0);
if ( strcmp(passptr, (char *)crypt(pass, passptr)) == 0 )
goto logit;
else
{
ptr="Login incorrect\r\n";
write(fd, ptr, strlen(ptr));
}
}
alarm(0); /* Reset timeout alarm */
return(NULL);
logit:
alarm(0); /* Reset timeout alarm */
/* Check for the home directory and the .hushlogin file */
if ( stat(pw->pw_dir, &sb) < 0 )
{
ptr="No home directory!\r\nLogging in with home=/\r\n";
write(fd, ptr, strlen(ptr));
}
/* If there is no shell field, shell is /bin/sh */
if ( ! *pw->pw_shell )
pw->pw_shell="/bin/sh";
/* Make sure the shell exists. */
if ( stat(pw->pw_shell, &sb) < 0 )
{
ptr="No login shell.\r\n";
write(fd, ptr, strlen(ptr));
return(NULL);
}
return(pw);
}
/* This is a function to get a line of input, emulating line driver */
/* \010 is the octal representation of Ctrl-H. */
#define BS "\010 \010"
#define CTRL_H 8 /* Decimal value of ^H (backspace key) */
#define CTRL_U 21 /* Decimal value of ^U */
#define CTRL_D 4 /* Decimal value of ^D */
#define DEL 127 /* Decimal value of ^? (delete key) */
static int at_cr=0; /* At a carriage return? */
static char *get_line(fd, buf, len, echo)
int fd;
char buf[];
int len;
int echo; /* Do we echo the input? */
{
int i=0, newline=0;
extern int telnet; /* From tcpserv.c */
extern int istelnet; /* From telnet.c */
struct Buf_Len buflen, *newbuflen; /* From tcp.h */
while ( !newline && ((buflen.len=read(fd, buflen.buffer, 1)) == 1) )
{
if ( telnet )
newbuflen=negotiate(fd, &buflen);
else
newbuflen=(&buflen);
/* Has the telnet negotiation used the input? */
if ( newbuflen->len == 0 )
continue;
switch (newbuflen->buffer[0])
{
case CTRL_H: /* Backspace */
case DEL: if ( echo && i )
write(fd, BS, 3);
i -= (i ? 1 : 0);
break;
case CTRL_U: if ( echo )
{
while ( i-- > 0 )
write(fd, BS, 3);
}
i=0;
break;
case '\r': at_cr=1;
write(fd, "\r\n", 2);
newline=1;
break;
case '\0': if ( at_cr )
at_cr=0;
break;
case '\n': if ( ! at_cr )
{
write(fd, "\r\n", 2);
newline=1;
}
else
at_cr=0;
break;
case CTRL_D: if ( i == 0 )
return(NULL);
default: if ( echo )
write(fd, newbuflen->buffer, 1);
if ( i < len )
buf[i++]=newbuflen->buffer[0];
}
}
if ( istelnet ) { /* Read NUL or NL for CR-NUL or CR-NL sequence */
read(fd, buflen.buffer, 1);
if ( (*buflen.buffer != '\0') && (*buflen.buffer != '\n') )
buf[i++]=buflen.buffer[0];
}
if ( buflen.len <= 0 )
return(NULL);
buf[i]='\0';
return(buf);
}
/* This function executes a shell, utilizing some login(1) standards.
The home directory and shell are assumed to have been checked to
make sure they exist. */
int login_exec(args, pw)
char *args[];
struct passwd *pw;
{
extern char **environ;
extern int errno;
extern char remotehost[]; /* From tcpserv.c */
char path[BUFSIZ], argv0[BUFSIZ], *ptr;
time_t now;
FILE *fp;
struct stat sb;
/* The environment variables */
char *termptr;
static char logname[BUFSIZ], home[BUFSIZ];
static char shell[BUFSIZ], cpath[BUFSIZ];
static char user[BUFSIZ], mail[BUFSIZ];
static char term[BUFSIZ], nethost[BUFSIZ];
/* Set the nethost, no matter what happens */
sprintf(nethost, "NETHOST=%s", remotehost);
/* If there is no login entry, don't perform login */
if ( ! pw ) {
#ifdef SET_NETHOST
(void) putenv(nethost);
#endif
return(execvp(args[0], args));
}
/* Set the first argument of the program */
if ( args[0] )
strcpy(argv0, args[0]);
else {
args[1]=NULL;
if ( *pw->pw_shell )
strcpy(argv0, pw->pw_shell);
else
strcpy(argv0, "/bin/sh");
}
if ( (ptr=(char *)strrchr(argv0, '/')) == NULL ) {
fprintf(stderr, "Login shell is not a full pathname.\n");
return(-1);
}
++ptr;
if ( (args[0]=(char *)malloc(strlen(ptr)+2)) == NULL)
return(-1);
sprintf(args[0], "-%s", ptr);
/* Check for .hushlogin */
sprintf(path, "%s/.hushlogin", pw->pw_dir);
if ( stat(path, &sb) != 0 ) {
/* Cat /etc/motd */
if ( (fp=fopen("/etc/motd", "r")) != NULL ) {
while ( fgets(path, BUFSIZ-1, fp) )
fputs(path, stdout);
}
/* Check for mail */
sprintf(path, "%s/%s", MAILDIR, pw->pw_name);
if ( (stat(path, &sb) == 0) && (sb.st_size != 0) ) {
if ( sb.st_mtime > sb.st_atime )
printf("You have NEW mail.\n");
else
printf("You have mail.\n");
}
}
/* Set the environment and user/group id */
termptr=(char *)getenv("TERM"); /* Get any env ptrs here */
sprintf(logname, "LOGNAME=%s", pw->pw_name);
*environ=logname;
sprintf(user, "USER=%s", pw->pw_name);
*(environ+1)=user;
sprintf(home, "HOME=%s", pw->pw_dir);
*(environ+2)=home;
sprintf(shell, "SHELL=%s", pw->pw_shell);
*(environ+3)=shell;
sprintf(cpath, "PATH=/bin:/usr/bin:/etc:");
*(environ+4)=cpath;
sprintf(mail, "MAIL=%s/%s", MAILDIR, pw->pw_name);
*(environ+5)=mail;
if ( termptr ) /* Initialized previously */
sprintf(term, "TERM=%s", ptr);
else
sprintf(term, "TERM=network");
*(environ+6)=term;
#ifdef SET_NETHOST
*(environ+7)=nethost;
*(environ+8)=NULL;
#else
*(environ+7)=NULL;
#endif /* SET_NETHOST */
(void) setgid(pw->pw_gid);
(void) setuid(pw->pw_uid);
if ( chdir(pw->pw_dir) < 0 ) /* We already warned in login_auth() */
(void) chdir("/");
return(execvp(argv0, args)); /* Just DO IT */
}
/* This utmp function was adapted from pty's logs.c */
int utmp(line, name, host, date, cleanup)
char *line;
char name[];
char *host;
time_t date;
int cleanup;
{
struct utmp ut;
struct utmp xt;
char *lineptr;
int fd, found=0;
if ( (fd=open(UTMP_FILE, O_RDWR)) < 0 )
return(-1);
/* Initialize the utmp structure */
d_zero((char *)&ut, sizeof(struct utmp));
/* Get the ttyxy form of the tty pathname if possible. */
if ( *line == '/' )
{
for ( lineptr=(line+1); *lineptr; ++lineptr )
{
if ( *lineptr == '/' )
break;
}
if ( *lineptr == '/' )
++lineptr;
}
else
lineptr=line;
(void) strncpy(ut.ut_line, lineptr, sizeof(ut.ut_line));
if ( cleanup )
{
ut.ut_name[0]='\0';
#ifdef USER_PROCESS
ut.ut_type = DEAD_PROCESS;
#endif
}
else
{
#ifdef USER_PROCESS
ut.ut_type = USER_PROCESS;
ut.ut_pid = getpid();
#endif
strncpy(ut.ut_name, name, sizeof(ut.ut_name));
}
ut.ut_time=date;
#ifdef HAVE_UTHOST
if ( ! cleanup && host )
strncpy(ut.ut_host, host, sizeof(ut.ut_host));
else
ut.ut_host[0]='\0';
#endif
while (read(fd,(char *) &xt, sizeof(xt)) == sizeof(xt))
{
#ifdef USER_PROCESS
if ( cleanup && xt.ut_type != USER_PROCESS )
continue; /* Only grab the active lines */
#endif
if (strncmp(xt.ut_line, ut.ut_line, sizeof(xt.ut_line)) == 0)
{
found=1;
lseek(fd, -(long)sizeof(struct utmp), 1);
break;
}
}
/* Add a new entry to the utmp file if we can't find our entry */
if ( (!found) && (!cleanup) )
{ /* Reopen to avoid a race with other end-of-utmp entries. */
(void) close(fd);
if ( (fd=open(UTMP_FILE, (O_RDWR|O_APPEND))) < 0 )
return -1;
}
if (write(fd, (char *) &ut, sizeof(ut)) < sizeof(ut))
{
(void) close(fd);
return -1;
}
return(close(fd));
}