/* * NLOGIN - Automated login to Novell file server * * Copyright (c) 1991 Stephen Houser, * University of Southern Maine/Academic Computing Services * * * December 16, 1991/Stephen Houser */ #include #include #include #include #include #include #include #define MAX_LOGINS 10 // Maximum number of alternates #define ADDRESS_FILE "NLOGIN.DAT" // Default address file #define LOGIN_COMMAND "LOGIN" // Default LOGIN.EXE command #define LOGIN_DIRECTORY "/SYS:LOGIN" // SYS:LOGIN directory name /* * GETOPT is distributed with the Borland C compilers. * If one of the Borland compilers is not used, another * UNIX style getopt() will need to be provided. */ extern char *optarg; /* --- GETOPT.C --- */ extern int optind; /* --- GETOPT.C --- */ char p_address[20]; // P_STATION Hardware Address char p_station[6]; // P_STATION as returnd from Novell char s_list[MAX_LOGINS][48]; // List of alternate servers char u_list[MAX_LOGINS][48]; // List of alternate usernames char login_cmd[MAXFILE]; // The LOGIN.EXE command char data_file[MAXPATH]; // The NLOGIN.DAT file char debug = 0; // Debugging display /* ------------------------------------------------------------------ */ /*~ CLIPPER ~*/ /* ------------------------------------------------------------------ */ /* * char *clipper( void ); * * Clips the next token from the character string pointed to by 'mark' * and places it in the character string 'token'. Uses 'whitespace' * as a list of token delimiters. * * Returns: NULL if no token could be formed. * Pointer to the 'token' variable if a token was found. * */ char *mark = NULL; char token[256]; char *whitespace = " \t\r\n,.:;\\/"; char *clipper( void ) { char *p = token; if ( !mark || !*mark ) return 0; while ( *mark && strchr( whitespace, *mark ) ) mark++; if ( !mark || !*mark ) return 0; while ( *mark && !strchr( whitespace, *mark ) ) *p++ = *mark++; *p = 0; return token; } /* ------------------------------------------------------------------ */ /*~ MAP_LOGIN_DRIVE ~*/ /* ------------------------------------------------------------------ */ /* * int map_login_drive( char *server_name ); * * Maps the SYS:LOGIN drive to the first available network drive letter, * then makes the LOGIN drive the current drive and directory. * */ int map_login_drive( char *s_name ) { char drive[2]; char path[256]; int last_drive; strcpy( path, s_name ); strcat( path, LOGIN_DIRECTORY ); last_drive = GetNumberOfLocalDrives(); drive[0] = last_drive + 'A'; drive[1] = 0; MapDriveUsingString( "ADD", drive, path ); setdisk( last_drive ); return 1; } /* ------------------------------------------------------------------ */ /*~ ATTACH ~*/ /* ------------------------------------------------------------------ */ /* * int attach( char *server_name ); * * Attempts to attach to the given server, DETACHES FROM THE DEFAULT SERVER. * * Returns : 0 if the attachment failed, remains attached to default server. * 1 if the attachment succedes, detaches from the default server, * and maps the first network drive to the LOGIN directory. * */ int attach( char *s_name ) { int connection_id; int old_id; if ( debug ) printf( "%-10s", s_name ); old_id = GetDefaultConnectionID(); switch ( AttachToFileServer( s_name, &connection_id ) ) { case ALREADY_ATTACHED_TO_SERVER: if ( debug ) printf( " Already attached!\n" ); SetPreferredConnectionID( connection_id ); SetPrimaryConnectionID( connection_id ); map_login_drive( s_name ); return 1; case SUCCESSFUL: if ( debug ) printf( " Attached.\n" ); SetPreferredConnectionID( connection_id ); SetPrimaryConnectionID( connection_id ); if ( connection_id != old_id ) DetachFromFileServer( old_id ); map_login_drive( s_name ); return 1; default: if ( debug ) printf( " Could not attach!\n" ); return 0; } } /* ------------------------------------------------------------------ */ /*~ LOGIN_USER ~*/ /* ------------------------------------------------------------------ */ /* * int login_user( char *server_name, char *user_name ); * * Attempts to login the 'username' into the server 'server_name'. * * Returns : 0 if the login cannot be completed. * 1 if the user is logged in, LOGIN drive is mapped. * */ int login_user( char *s_name, char *u_name ) { int rc; if ( !attach( s_name ) ) return 0; rc = LoginToFileServer( u_name, OT_USER, "" ); if ( (rc == 0x00) || (rc == 0xff) ) { map_login_drive( s_name ); return 1; } return 0; } /* ------------------------------------------------------------------ */ /*~ LOOKUP_ADDRESS ~*/ /* ------------------------------------------------------------------ */ int lookup_address( void ) { FILE *fp; char xxx[256]; int server; int rc = 0; if ( debug ) printf( " Looking up [%s] in [%s]...", p_address, data_file ); if ( !(fp = fopen( data_file, "rt" )) ) { if ( debug ) printf( "Cannot open file!\n" ); return 0; } while ( fgets( xxx, 256, fp ) ) { mark = xxx; if ( !clipper() ) continue; if ( !strncmpi( p_address, token, strlen( token ) ) ) { if ( debug ) printf( "\n %s", xxx ); for ( server = 0; server < MAX_LOGINS; server++ ) { if ( !clipper() ) break; strcpy( s_list[server], strupr( token ) ); if ( !clipper() ) break; strcpy( u_list[server], strupr( token ) ); } rc = server; break; } } if ( debug && !rc ) printf( "No entries for this station found!\n" ); fclose( fp ); return rc; } /* ------------------------------------------------------------------ */ /*~ M A I N ~*/ /* ------------------------------------------------------------------ */ void main( int argc, char **argv ) { char s_name[48]; int servers; int i; int opt; debug = 0; strcpy( login_cmd, LOGIN_COMMAND ); strcpy( data_file, ADDRESS_FILE ); while ( (opt = getopt( argc, argv, "df:l:" )) != EOF ) { switch ( opt ) { case 'd': /* --- DEBUG --- */ debug = 1; break; case 'l': /* --- LOGIN COMMAND --- */ strcpy( login_cmd, optarg ); break; case 'f': /* --- DATA FILE --- */ strcpy( data_file, optarg ); break; default: printf( "Unknown option %c\n", opt ); break; } } argc -= optind; argv += optind - 1; if ( !GetConnectionNumber() ) { printf( "Not attached to a Novell file server.\n" ); exit( 1 ); } Logout(); GetStationAddress( p_station ); sprintf( p_address, "%02X%02X%02X%02X%02X%02X", (p_station[0] & 0xff), (p_station[1] & 0xff), (p_station[2] & 0xff), (p_station[3] & 0xff), (p_station[4] & 0xff), (p_station[5] & 0xff) ); GetFileServerName( GetDefaultConnectionID(), s_name ); map_login_drive( s_name ); printf( "\nStation [%s] on server [%s]\n", p_address, s_name ); /* * If no servers are specified on the command line, look for the * address file on the default server. */ if ( !argc ) goto Lookup_first; while ( *(++argv) ) { /* if ( !stricmp( strupr( *argv ), strupr( s_name ) ) ) continue; */ if ( !attach( strupr( *argv ) ) ) continue; Lookup_first: if ( (servers = lookup_address()) ) { for ( i = 0; i < servers; i++ ) { if ( login_user( s_list[i], u_list[i] ) ) { printf( "\n%s %s/%s\n", login_cmd, s_list[i], u_list[i] ); execlp( login_cmd, s_list[i], u_list[i], NULL ); printf( "Login failed!\n" ); exit( 3 ); } if ( debug ) printf( "\t\tCannot login as [%s/%s]\n\n", s_list[i], u_list[i] ); } } } printf( "\nERROR: Could not login to any servers!\n" ); exit( 2 ); }