home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Black Box 4
/
BlackBox.cdr
/
lan
/
lanutsrc.arj
/
LOGIN.C
< prev
next >
Wrap
Text File
|
1992-07-14
|
14KB
|
510 lines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <direct.h>
#include <conio.h>
#include <process.h>
#include "lantasti.h"
#define DOS 0x21
#define NETBIOS 0x5C
#define DELIM "+, "
typedef struct FLAGS {
int script,
suppress,
duplicates,
stop;
char drive[3];
} FLAGS;
#define LIST_SIZE 30
#define NAME_SIZE 20
typedef struct LIST {
int num_servers,num_ids,num_pw;
char server[LIST_SIZE][NAME_SIZE],
user[LIST_SIZE][NAME_SIZE],
password[LIST_SIZE][NAME_SIZE];
} LIST;
/* definitions to save typing time */
#define C_SERVER list->server[i % list->num_servers]
#define C_USER list->user[i % list->num_ids]
#define C_PASS list->password[i % list->num_pw]
FLAGS flags = {
TRUE,
FALSE,
TRUE,
FALSE,
"B:"
};
int ret_code = 0;
/* select_drive *************************************************************
Select current disk drive given drive letter. Returns number of drives
on system.
****************************************************************************/
int select_drive(drive)
char drive;
{
union REGS regs;
drive = drive - 65; /* convert to drive number */
regs.h.ah = 14; /* select drive function */
regs.h.dl = drive;
intdos(®s,®s);
return(regs.h.al); /* number of drives on system */
}
/* get_drive ********************************************************************
Returns the letter of the current disk drive
*****************************************************************************/
char get_drive() {
union REGS regs;
regs.h.ah = 0x19;
intdos(®s,®s); /* get drive number */
return(regs.h.al + 65); /* convert to drive letter and return */
}
/* fexists ********************************************************************
*****************************************************************************/
int fexists(filename)
char *filename;
{
FILE *file;
int result;
file = fopen(filename,"rb");
result = (file != NULL);
if (result) fclose(file);
return(result);
}
/* pwgets ********************************************************************
*****************************************************************************/
char *pwgets(buffer,length)
char *buffer;
int length;
{
int i,key;
i = 0;
while (i < length) {
key = getch();
if (key == '\r') {
putch(key);
putch('\n');
break;
}
buffer[i] = key;
if (key == '\b') {
if (i > 0) {
printf("\b \b");
i--;
}
else putch('\a');
}
else {
putch('∙');
i++;
}
}
buffer[i] = 0;
return(buffer);
}
/* getstring ********************************************************************
Get a string from the console -- takes a pointer to a string buffer, the
longest permissible length and two switches. The empty switch determines
whether or not the user is allowed to enter an empty string. If TRUE, he
can, if FALSE, he must enter something. The shown switch determines
whether or not input is echoed to the screen, TRUE if echoed, FALSE if
invisible
*****************************************************************************/
void getstring(prompt,buffer,length,empty,shown)
char *prompt,*buffer;
int length,empty,shown;
{
int i;
if (shown) {
fprintf(stdout,"%s",prompt);
fgets(buffer,length,stdin);
if (buffer[strlen(buffer) - 1] == '\n') buffer[strlen(buffer) - 1] = 0;
while (!(strlen(buffer) || empty)) {
fprintf(stdout,"%s",prompt);
fgets(buffer,length,stdin);
if (buffer[strlen(buffer) - 1] == '\n') buffer[strlen(buffer) - 1] = 0;
}
}
else { /* get string without echoing */
fprintf(stdout,"%s",prompt);
pwgets(buffer,length);
while (!(strlen(buffer) || empty)) {
fprintf(stdout,"%s",prompt);
pwgets(buffer,length);
}
}
}
/* error ********************************************************************
*****************************************************************************/
void error(code,message)
int code;
char *message;
{
char *ptr;
ret_code = (code) ? code : -1;
if (flags.suppress) return;
if (code) {
ptr = get_error_text(code);
puts(ptr);
}
else puts(message);
}
/* get_available_servers ********************************************************************
*****************************************************************************/
void get_available_servers(list)
LIST *list;
{
char buffer[17];
int index,result;
index = 0;
result = get_inactive_server(buffer,index);
while (result) {
strcpy(list->server[list->num_servers++],buffer);
if (list->num_servers >= LIST_SIZE) {
puts("Warning: Too many servers. Only the first 50 can be used.");
break;
}
result = get_inactive_server(buffer,++index);
}
}
/* process_server_list ********************************************************************
*****************************************************************************/
void process_server_list(string,list)
char *string;
LIST *list;
{
char *ptr;
ptr = strtok(string,DELIM);
while (ptr !=NULL) {
if (*ptr == '*') {
get_available_servers(list);
break;
}
else {
strcpy(list->server[list->num_servers++],ptr);
}
if (list->num_servers >= LIST_SIZE ) {
puts("Warning: Too many servers. Only the first 30 will be used.");
break;
}
ptr = strtok(NULL," ,");
}
/* if no servers are given, ask for one */
if (list->num_servers == 0) {
strcpy(list->server[list->num_servers++],"?");
}
}
/* process_user_list *********************************************************
*****************************************************************************/
void process_user_list(string,list)
char *string;
LIST *list;
{
char *ptr;
ptr = strtok(string,DELIM);
while (ptr != NULL) {
strcpy(list->user[list->num_ids++],ptr);
if (list->num_ids >= LIST_SIZE ) {
puts("Warning: Too many user IDs. Only the first 30 can be used.");
break;
}
ptr = strtok(NULL,DELIM);
}
/* if no user ids are given, ask for one */
if (list->num_ids == 0) {
strcpy(list->user[list->num_ids++],"?");
}
}
/* process_password_list *****************************************************
*****************************************************************************/
void process_password_list(string,list)
char *string;
LIST *list;
{
char *ptr;
ptr = strtok(string,DELIM);
while (ptr !=NULL) {
strcpy(list->password[list->num_pw++],ptr);
if (list->num_pw >= LIST_SIZE ) {
puts("Warning: Too many passwords. Only the first 30 can be used.");
break;
}
ptr = strtok(NULL," ,");
}
/* if no passwords given, set up a zero length string */
if (list->num_pw == 0) {
list->password[list->num_pw++][0] = 0;
}
}
/* options */
#define NUM_OPTIONS 4
char *options[NUM_OPTIONS] = {
"NODUPLICATES",
"NOSCRIPT",
"SUPPRESS",
"HELP"
};
/* process_option ********************************************************************
*****************************************************************************/
void process_option(string,flags)
char *string;
FLAGS *flags;
{
char *ptr;
int i;
ptr = strtok(string,"=:");
for (i = 0; i < NUM_OPTIONS; i++) {
if (!strcmpi(ptr,options[i])) break;
}
if (i >= NUM_OPTIONS) {
printf("Whoops! %s isn't a valid command line option.\n",ptr);
return;
}
ptr = strtok(NULL," =");
switch (i) {
case 0: /* report duplicate logins */
flags->duplicates = FALSE;
break;
case 1: /* no script */
flags->script = FALSE;
break;
case 2: /* suppress error messages */
flags->suppress = TRUE;
break;
case 3: /* help */
puts("LOGIN utility for LANtastic -- Copyright 1989 by SoftMagic, Inc.");
puts("All rights reserved. LANtastic is a trademark of Artisoft, Inc.\n");
puts("Usage: LOGIN <server list> <user id list> <password list> [/OPTIONS]");
puts(" The '*' character in the server lists means all available servers.");
puts(" A question mark in any list causes the program to prompt the user");
puts(" for input (passwords will not be displayed).\n");
puts("Available options are:");
puts("/NODUPLICATES - report duplicate login attempts");
puts("/NOSCRIPT - bypass the login script facility");
puts("/SUPPRESS - suppress all error messages");
puts("/HELP - display this documentation");
exit(0);
break;
}
}
/* scan_command_line ********************************************************************
*****************************************************************************/
scan_command_line(argc,argv,flags,list)
int argc;
char *argv[];
FLAGS *flags;
LIST *list;
{
int i,state;
char s_buffer[128],u_buffer[128],p_buffer[128];
s_buffer[0] = u_buffer[0] = p_buffer[0] = 0;
state = list->num_servers = list->num_ids = list->num_pw = 0;
for (i = 1;i < argc; i++) {
strupr(argv[i]);
if (argv[i][0] == '/') {
process_option(&argv[i][1],flags);
continue;
}
switch (state++) {
case 0:
strcpy(s_buffer,argv[i]);
break;
case 1:
strcpy(u_buffer,argv[i]);
break;
case 2:
strcpy(p_buffer,argv[i]);
break;
}
}
/* call the setup functions */
process_server_list(s_buffer,list);
process_user_list(u_buffer,list);
process_password_list(p_buffer,list);
}
/* do_script ********************************************************************
*****************************************************************************/
do_script(server,user,drive)
char *server,*user,*drive;
{
char string[60],node[17],starting_drive;
int result;
/* get our node name */
whoami(node);
/* save the current drivespec */
starting_drive = get_drive();
/* redirect the server's network directory */
sprintf(string,"\\\\%s",server);
result = redirect_device(drive,string,DISK_DEVICE);
if (!result) {
/* execute the system login script */
select_drive(flags.drive[0]);
sprintf(string,"%s$SYSTEM.BAT",drive);
if (fexists(string)) {
sprintf(string,"%s %s",string,node);
system(string);
}
/* execute the user's login script */
sprintf(string,"%s%s.BAT",drive,user);
if (fexists(string)) {
sprintf(string,"%s %s",string,node);
system(string);
}
/* restore the script drive to normal */
if (get_drive() == flags.drive[0]) select_drive(starting_drive);
cancel_redirection(flags.drive);
}
else {
sprintf(string,"Unable to access network directory on server %s\n",server);
error(FALSE,string);
}
}
/* do_login ********************************************************************
*****************************************************************************/
int do_login(flags,list)
FLAGS *flags;
LIST *list;
{
int result,i,interactive;
char login_buffer[60],
logout_buffer[60],
prompt[50],
*ptr;
/* build \\SERVER<0>USERID<0>PASSWORD<0><0> string */
/* first, initialize the whole login buffer to zeros to make sure
that we don't get tripped up because LANtastic needs an extra
terminating 0 if there's no password */
memset(login_buffer,0,60);
for (i = 0; i < list->num_servers; i++) {
/* see if we've got any "?"s in the list */
if (C_SERVER[0] == '?') {
getstring("Server: ",login_buffer,128,FALSE,TRUE);
list->num_servers = i;
process_server_list(login_buffer,list);
}
if (C_USER[0] == '?') {
if (list->num_ids > 1)
sprintf(prompt,"User Name (for server %s): ",C_SERVER);
else sprintf(prompt,"User Name: ");
getstring(prompt,C_USER,128,FALSE,TRUE);
if (C_PASS[0] == 0) strcpy(C_PASS,"?");
}
if (C_PASS[0] == '?') {
if (list->num_pw > 1)
sprintf(prompt,"Password (for server %s): ",C_SERVER);
else sprintf(prompt,"Password: ");
getstring(prompt,C_PASS,128,TRUE,FALSE);
}
sprintf(login_buffer,"\\\\%s\\%s",C_SERVER,C_USER);
ptr = login_buffer + strlen(login_buffer) + 1;
strcpy(ptr,C_PASS);
sprintf(logout_buffer,"\\\\%s",C_SERVER);
logout(logout_buffer);
result = login(login_buffer);
if (result == 0) {
if (flags->script) do_script(C_SERVER,C_USER,flags->drive);
}
else error(result,NULL);
}
}
/* main ********************************************************************
*****************************************************************************/
int main(argc,argv)
int argc;
char *argv[];
{
LIST list; /*server, user and password lists*/
int serverno; /*index into serverlist*/
#ifdef SHAREWARE
puts("LOGIN utility for LANtastic -- Copyright 1989 by SoftMagic, Inc.");
puts("All rights reserved. Thanks for trying this unregistered Shareware edition!\n");
#endif
scan_command_line(argc,argv,&flags,&list);
if (list.num_servers > 0) do_login(&flags,&list);
else error(FALSE,"No available servers found -- Unable to log in.");
return(ret_code);
}