home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume26
/
modempool
/
part01
/
login.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-05
|
6KB
|
273 lines
/*******************************************************************
*
* Module: @(#)login.c 4.4 92/04/21
*
* Description:
* Handle login during user callup.
*
* Revision:
* Date By Reason
* ---- -- ------
* 920309 Lars Berntzon Created
*
*******************************************************************/
static char SccsId[] = "@(#)login.c 4.4 92/04/21";
#include <stdio.h>
#ifndef NOSTDLIB
#include <stdlib.h>
#endif
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "modempool.h"
#ifdef RLOGIN
#include <pwd.h>
#endif
/*******************************************************************
* H D L _C O N N E C T
* --------------------
*
* Description:
* Handles initial connection to callback server.
*
* Arguments:
* mod_rc - Return code from modem specifying connection.
*
*******************************************************************/
int
hdl_connect(int mod_rc)
{
slot_t msg_slot; /* A message carrier between do_login and callback */
int rc;
switch(mod_rc)
{
case MOD_CONNECT:
case MOD_CONNECT_300:
slot.baud = 300;
break;
case MOD_CONNECT_1200:
slot.baud = 1200;
break;
case MOD_CONNECT_2400:
slot.baud = 2400;
break;
case MOD_CONNECT_4800:
slot.baud = 4800;
break;
case MOD_CONNECT_9600:
slot.baud = 9600;
break;
case MOD_RING:
case MOD_OK:
return E_RETRY;
default:
logerr("connect: unknown modem code: %d", mod_rc);
return E_FAIL;
}
/*
* Don't accept dialup orders during login.
*/
slot.status = SLOT_LOGIN;
slot_write(&slot);
log("connected at baud %d", slot.baud);
/* Also set the message data to correct baud */
msg_slot.baud = slot.baud;
/*
* Setup port in sane mode.
*/
tty_baud(slot.baud);
tty_local(0);
tty_sane();
tty_flush();
/*
* Request everything from calling user.
*/
rc = do_login(&msg_slot);
tty_raw();
/* Turn back off DCD checking */
tty_local(1);
/* If login successful, do a callback.
* If I'm the only free server left, the
* call to callback will never return.
*/
if (rc != E_OK) {
return E_FAIL;
}
mod_put(MSG_HANGUP);
if (callback(&msg_slot) != E_OK) {
logerr("callback failed");
return E_FAIL;
}
return E_OK;
}
/*******************************************************************
*
* DO_LOGIN
* --------
*
* Description:
* Reads username and password, does a check and return phone number
*
*******************************************************************/
int
do_login(slot_t *sp)
{
char passwd[NAME_SIZE]; /* Temporary password */
char str[200]; /* Temporary string */
char *field[N_FIELDS]; /* Fields in the USERS file */
int rc; /* Return code */
int retry; /* Retry counter */
FILE *fp; /* Temporary file pointer */
char *p; /* Temporary char pointer */
/*
* Give greetings message.
*/
if ((fp = fopen(GREETINGSFILE, "r")) != NULL) {
while(fgets(str, sizeof str, fp) != NULL) {
mod_put(str);
mod_put("\r");
}
fclose(fp);
}
else {
logerr("failed to open greetings file");
}
for(retry = 0; retry < MAX_RETRY; retry++)
{
/* Get login name */
do {
rc = prompt(MSG_NAME, sp->name, sizeof sp->name, LOGIN_TMOUT);
}
while(rc == E_OK && strlen(sp->name) == 0);
if (rc != E_OK) {
if (rc == E_TMOUT) {
debug("do_login: timeout");
mod_put(MSG_LOGINFAIL);
return E_FAIL;
}
debug("do_login: login: rc = %d, errno = %d", rc, errno);
mod_put(MSG_LOGINFAIL);
continue;
}
/* Get passwd */
tty_noecho();
rc = prompt(MSG_PASSWD, passwd, sizeof passwd, LOGIN_TMOUT);
tty_echo();
mod_put("\r\n");
if (rc != E_OK) {
debug("do_login: passwd: rc = %d, errno = %d", rc, errno);
mod_put(MSG_LOGINFAIL);
continue;
}
/* Get user data */
if (db_find(USERS, field, sizeof field / sizeof field[0], 0, sp->name) != E_OK)
{
debug("do_login: db_find: rc = %d, errno = %d", rc, errno);
mod_put(MSG_LOGINFAIL);
continue;
}
/* Check validity */
if (field[0] == NULL || field[1] == NULL || field[2] == NULL
#ifdef RLOGIN
|| field[3] == NULL
#endif
)
{
logerr("do_login: illegal data for user '%s'", sp->name);
mod_put(MSG_LOGINFAIL);
return E_FAIL;
}
/* Check if password is ok */
if (strcmp(field[1], passwd) != 0) {
debug("do_login: wrong password");
mod_put(MSG_LOGINFAIL);
continue;
}
/* Login succeded */
strncpy(sp->name, field[0], sizeof sp->name);
#ifdef RLOGIN
strncpy(sp->host, field[3], sizeof sp->host);
#endif
/*
* Check if wildcard phonenumber or phonenumber list.
*/
if ((strchr(field[2], '*') != NULL) || (strchr(field[2], '/') != NULL)) {
/*
* Request phonenumber from user.
*/
do {
rc = prompt(MSG_PHONE, sp->phone, sizeof sp->phone, LOGIN_TMOUT);
}
while(rc == E_OK && strlen(sp->phone) == 0);
if (rc != E_OK) {
log("failed to read phone number");
mod_put(MSG_LOGINFAIL);
continue;
}
/*
* Check entered number against list.
*/
for(p = strtok(field[2], "/"); p != NULL; p = strtok(NULL, "/"))
{
debug("trying '%s' vs. '%s'\n", p, sp->phone);
if ((strcmp(p, "*") == 0) || (strcmp(p, sp->phone) == 0)) {
break;
}
}
if (p == NULL) {
log("wrong phonenumber");
mod_put(MSG_LOGINFAIL);
continue;
}
if (strcmp(p, "*") == 0) {
log("privileged user '%s' succeded to login (phone: %s)", sp->name, sp->phone);
}
else {
log("user '%s' succeded to login (phone: %s)", sp->name, sp->phone);
}
}
else {
strncpy(sp->phone, field[2], sizeof sp->phone);
log("user '%s' succeded to login", sp->name);
}
return E_OK;
}
log("user '%s' tried to login but failed", sp->name);
return E_FAIL;
}