/* * usrchk - Show logged-on users on 3com Total Control Hubs. * * VERSION 1.06a - Mar 20, 2002 * * History. * pmwho.c - utility to query Livingston Portmasters for port info. * Johan Persson, jp@abc.se * pm3who.c - modified to include Portmaster 3 port and modem info. * chandhok@within.com * userchk.c - modified to work with USR Total Control Hubs, and port resets. * Grant Hopwood, ghopwood@trip.net * usrchk.c - utility to query 3com Total Control Hubs for port info, and * port resets. Works with both Netserver and HyperArc chassises. * Kudos to everyone who started this project. :-) * Anthony J. Biacco, admin@intergrafix.net * * 03-98 to Present * Further mods by Anthony J. Biacco * HiperARC functionality. (session times also pulled out from horrible * START TIME format) * Highest user killed when at capacity (or specified # of ports) * Structs for seperate chassises, increased HTML features, time sorting * Removed some portmaster variables. Sorry, I only have TChubs and the * portmaster stuff was bloating the code for me. * Other things I cant recall right now * Code still needs some major cleanup for old variables not needed * anymore * More in CHANGES file * * 02/25/98 * Added ability to record and reset duplicate ports on multiple TCH's. * * 02/12/98 * Compilation Info: Can be found further down in this file. * * chandhok@within.com: Added these features: * - By default, show the number of lines in use at the end of the report * You can disable this behavior with the "-x" switch. * - Using the "-S" option creates a single line summary report of line usage * suitable for parsing by other programs * */ /* START OF USER DEFINABLE SETTINGS */ /* Below are all the definitions that you will need to modify */ /* that usually dont change. Rest of them are in the config file */ /* Define your domain and sysadmin account for email to be sent to. */ /* FEATURE CURRENTLY DISABLED */ char domain[]="intergrafix.net"; char admin[]="admin"; /* This line is displayed at the end of your email for customers benefit. */ /* FEATURE CURRENTLY DISABLED */ char companyinfo[]="Intergrafix Internet Services"; /* Command to get page break to turn off for HyperARC for current session */ #define NOPAGE_COMMAND "set command local_terminal 255\n" /* What command prompt we check for on netserver and hyperarc chassises */ /* This represents the END of the prompt. The default is the normal */ /* If you change your prompt, change this. ESPECIALLY IF YOU TAKE A */ /* SPACE OUT OF YOUR PROMPT */ /* NS_COM_PROMPT is for Netserver TC Chassises */ /* HYPER_COM_PROMPT is for HyperARC TC Chassises */ #define NS_COM_PROMPT "d> " #define HYPER_COM_PROMPT "> " /* Location of default config file */ #define CONF "/etc/usrchk.cfg" /* END OF USER DEFINABLE SETTINGS */ #define MAXSES 250 /* New defines for NETSERVER modem lines of the form: 1 2 3 4 5 6 0123456789012345678901234567890123456789012345678901234567890123456789012345678 9 S5 gambino 205.219.3.47 Netwrk In ESTABLISHED 11:40 0 */ #define LINELEN 500 #define IDLE_POS 51 #define S0 0 #define LOG_POS 38 #define USER_POS 6 #define START_POS 65 /* New defines for HYPERARC modem connection lines of the form: 1 2 3 4 5 6 0123456789012345678901234567890123456789012345678901234567890123456789012345678 9 slot:12/mod:12 gambino DIALIN PPP 07-AUG-2041 16:30:45 New defines for HYPERARC modem network lines of the form: test-ip-I4841 IP slot:15/mod:1 ENA DYN 226.37.136.10/H New defines for HYPERARC interface stats of the form: INTERFACE slot:1/mod:1 SETTINGS Description: GWC Modem Driver Type: RS232 Speed: 33333 */ #define HYPER_IDLE_POS 51 #define HYPER_LOG_POS 44 #define HYPER_USER_POS 16 #define HYPER_NET_USER_POS_END 31 #define HYPER_START_POS 56 #define HYPER_IP_PORT_POS 37 #define HYPER_IP_PORT_POS_END 50 #define HYPER_IP_ADDY_TYPE 59 #define HYPER_IP_ADDY_TYPE_END 63 #define HYPER_IP_ADDY_POS 64 #define HYPER_INT_SPEED_POS 42 #if defined(__sun__) /* SunOS of some sort */ #if defined(__svr4__) /* Solaris, i.e. SunOS 5.x */ #define SOL_SYS #else /* __sun__ && !__svr4__ (SunOS 4.x presumeably) */ #define SUN4_SYS #endif /* __svr4__ */ #endif /* __sun__ */ #define TELNET_PORT "23" #define TELNET_PROMPT "telnet> " #if defined(SUN4_SYS) || defined(SOL_SYS) # define HAVE_SYS_WAIT #else # if defined(DNIX) || defined(HPUX) # define HAVE_SYS_WAIT # else # if defined(__linux__) # define HAVE_SYS_WAIT # else # if defined(_WIN32) || defined(WIN32) # define HAVE_SYS_WAIT /* #define HAVE_WAIT */ # else /* generic entry */ /* #define HAVE_SYS_WAIT */ /* #define HAVE_WAIT */ # endif /* end windows if */ # endif /* end linux if */ # endif /* end dnix/hpux if */ #endif /* end sun/solaris if */ /* header files */ #include #include #include #include #include #if defined(HPUX) #include #define NSIG _NSIG #endif #include #include /* OS dependent includes */ #if defined(_WIN32) && !defined(__CYGWIN32__) /* this probably isn't right..anyone? */ #include #include #include #include #include #define EINTR WSAEINTR #define EMFILE WSAEMFILE #define EWOULDBLOCK WSAEWOULDBLOCK #define EINPROGRESS WSAEINPROGRESS #define EAFNOSUPPORT WSAEAFNOSUPPORT #define ENETUNREACH WSAENETUNREACH #define ETIMEDOUT WSAETIMEDOUT #define ECONNREFUSED WSAECONNREFUSED #define errno WSAGetLastError() #else /* NOT windows */ #define SOCKET_ERROR -1 #if !defined(__STRICT_ANSI__) #define __STRICT_ANSI__ #include /* for socket(), etc.. */ #undef __STRICT_ANSI__ #else #include /* for socket(), etc.. */ #endif #include /* for sockaddr_in structure */ #include /* for inet_addr() */ #endif /* end of OS dependent includes */ /* #if defined(HAVE_SYS_WAIT) #include #else # if defined(HAVE_WAIT) #include # else extern pid_t wait(int *status); # endif #endif */ #include #include #include #define asizeof(a) (sizeof (a) / sizeof ((a)[0])) /* If not defined, i.e. unix system, define it so we cut */ /* down on ifdefs between win32 and *nix */ #if !defined(INVALID_SOCKET) #define INVALID_SOCKET -1 #endif #if !defined(EPROTO) #define EPROTO EINTR #endif #if !defined(SIGCLD) #define SIGCLD SIGCHLD #endif #if defined(_WIN32) && !defined(__CYGWIN32__) #define CLOSE closesocket #else #define CLOSE close #endif /* Program declarations */ void usage(void); void pmwho(void); void reset_port(); void wlog(); void sigalrm(int signo); void sigalrm1(int signo); void sigalrm2(int signo); void sigalrm3(int signo); void sigfunc(int signo); void reset_alarm(int time, int mode); void err_msg(const char *format, ...); void err_exit(const char *format, ...); void log_times(); void clear_list(int mode); void midcpy(char *strf, char *strt, int fr, int to); void move_down(int pos); void copy_list(); void print_users(); void remove_first(char *inpstr); void read_config(); void clear_structs(void); void clear_structs2(void); int do_hyper_read2(int fd, char *text1, char *text2, int show, int timeout); int convert_time(char *str); int main(int argc, char *argv[]); int do_read(int fd, char *text1, char *text2, int show, int timeout); int do_write(int fd, char *text, int timeout); int count_groups(char *str); unsigned long hyper_session_time(char *inpstr, int mode, char *line2); long DateDay(int month, int day, int year); char *get_time_as_string(void); char *clear_trail_space(char *str); char *clear_leading_space(char *str); char *clear_trail_junk(char *cygstr); char *strip_afterequals(char *cygstr2); char *hyper_session_time_formatted(char *inpstr, char *line); char *strip_slotchunk(char *fullport, int mode); char *strip_html(char *buffer); char *my_strptime(char *buf, char *fmt, struct tm *tmStruct); char x2c(char *what); void unescape_url(char *url); void unescape_url2(char *url); /* program constants and global vars */ char *progname; char host[32]; char config_file[256]=CONF; char glob_hostname[50]; char glob_statusname[50]; char glob_portname[50]; char glob_username[100]; time_t glob_time; long glob_speed; int glob_sort=0; int glob_fd; int CGI=0; pid_t pid; int showall; int summaryOnly; int addtime; int HTML; int DEBUG=0; int showSpeed=0; int num_opts=0; int startportchecked=0; int portfound=0; int MAXCHAS=0; int KILL_TOTAL=0; int HOWTO_LOG=0; int high_total=0; char URL[256]; char LOG_DIR[256]; char KILL_LOG_FILE[256]; int globhold; int killtime=0; int portreset=0; int Basic=0; int hp=0; int xx=0; int dd=0; int nn=0; int dpi=0; int indexer=0; int cindexer=0; int hypercount=0; int listindex=0; char resets[100]; char username_g[50]; char hostname2[50]; char portname[50]; char typename[50]; char statusname[50]; char timename[50]; char timename_hour[3]; char idlename[50]; char resetport[50]; char buf[150]; char mess[500]; int group_pos=0; int group_count=0; int group_at=0; int group_lines=0; char connlist[1000][500]; char templist[1000][500]; char portsortstr[10], timesortstr[10], speedsortstr[10]; /* * * How it actually works * --------------------- * It starts a connect() session to the TChub on TELNET_PORT, * issues the commands to login, shows the information and then logs out. * The information is passed to and from the process through read() * and write() * * Configuration options * --------------------- * Before building the program you should check out (and modify if necessary) * the values of these constants to match your own configuration (good default * values are already provided for SunOS 4.x, Solaris 2.x, DNIX and Linux). * and then modify the usrchk.cfg config file as per the included README * for your OS * * TELNET_PORT - a string, the port to use when starting 'telnet'. * TELNET_PROMPT - a string, the prompt of telnet. * HAVE_SYS_WAIT, HAVE_WAIT - #define only one of these to indicate which * headerfile you have ( _or_ , * in that order), if none of them is #define-d a * generic definition be used. * * Building the utility * -------------------- * - Using Linux, Solaris 2.x, or SunOS 4.x: * $ gcc usrchk.c -o usrchk * * - Using DNIX: * $ cc -DDNIX usrchk.c -o usrchk * * - Using HP-UX 9.0x * $ cc -DHPUX -D_INCLUDE_POSIX_SOURCE usrchk.c -o usrchk -O -Aa * * - PROMPT replaced by "> " since we have to handle both * a prompt of type "Command> " and "$hostname> ", this * will probably mean that we will run into trouble when * someone enters a username with "> " in it... * */ /* Structure for all your TCH's. */ struct { char groupname[32]; /* location group this chassis is in */ char hostname[32]; /* ip and location designation letter, if */ /* 1 chassis is used for several cities */ /* the letter breaks the city into a seperate */ /* table and makes the identifier unique */ char username[32]; /* username to log in to the chassis */ char password[32]; /* password for said username */ char name[32]; /* Table header if output is in HTML */ int linestotal; /* how many total available lines on this box */ int beforekill; /* how many lines should be active before we start killing users past KILL_TOTAL? */ char startport[15]; /* for hiperarc, the slot we're in to search */ char lastport[15]; /* up to what port/slot do we want to check? */ /* for hiperarc this is the slot immediately */ /* after the last slot we want to check i.e. */ /* to check up to including slot 15, set the */ /* lastport at slot:16 */ int sortTime; /* 0 - sort by port, ascending */ /* 1 - sort by session time, ascending */ int type; /* 0 - netserver 1 - hiperarc */ } pwtable[20]; struct { char highport[20]; char host[32]; char name[100]; unsigned int timeon[MAXSES]; int hightime; int numlines; char port[MAXSES][20]; char username[MAXSES][50]; char colontime[MAXSES][20]; } timetable[20]; /* ..and awaaaaaaaaaaayyyyy we go! */ /* main procedure, find out program name and the hostname to use */ int main(int argc, char *argv[]) { char *t, *query, *method, tmpBuf[100], myargs[12][30]; int ch, n, i=0, a=0, found=0, cols=0, j=0; char speed[100]; nn=0; killtime = 0; pid = -1; showall = 0; summaryOnly = 0; addtime = 0; HTML = 0; showSpeed = 0; num_opts = 0; method=(char *)getenv("REQUEST_METHOD"); if (method && !strcmp(method,"GET")) { if (!strlen((char *)getenv("QUERY_STRING"))) goto ARGS1; printf("Content-type: text/html\n\n"); query = (char *)getenv("QUERY_STRING"); strncpy(tmpBuf,query,256); unescape_url2(tmpBuf); CGI=1; a++; for (i=0;i\n"); exit(0); } ARGS1: t = strrchr(argv[0], '/'); progname = (t != NULL) ? t+1 : argv[0]; while ((ch = getopt(argc, argv, "kacdsBSTHhPf")) != -1) { switch (ch) { case 'k': killtime=1; num_opts++; break; case 'a': showall = 1; num_opts++; break; case 'S': summaryOnly = 1; num_opts++; break; case 'c': showSpeed = 1; num_opts++; break; case 'T': addtime = 1; num_opts++; break; case 'H': HTML = 1; num_opts++; break; case 'B': Basic = 1; num_opts++; break; case 'P': if (argc > 2) { sscanf(argv[num_opts+2],"%s",resetport); } else { fprintf(stderr,"-P requires a portname argument\n"); exit(0); } printf("Reset port: %s\n",resetport); num_opts+=2; portreset=1; break; case 's': if (argc > 2) { if (!strcmp(argv[num_opts+2],"port")) glob_sort=1; else if (!strcmp(argv[num_opts+2],"time")) glob_sort=2; else if (!strcmp(argv[num_opts+2],"speed")) glob_sort=3; else { fprintf(stderr,"Sort type \"%s\" not recognized!\n",argv[num_opts+2]); fprintf(stderr,"-s requires a sort argument of time, port, or speed\n"); exit(0); } } else { fprintf(stderr,"-s requires a sort argument of time, port, or speed\n"); exit(0); } printf("Sort type: %d\n",glob_sort-1); num_opts+=2; break; case 'f': if (argc > 2) { sscanf(argv[num_opts+2],"%s",mess); strncpy(config_file,mess,sizeof(config_file)); } else { fprintf(stderr,"-f requires a path argument to the config file\n"); exit(0); } num_opts+=2; break; case 'd': if (argc > 2) { if (!strcmp(argv[num_opts+2],"1")) DEBUG=1; else if (!strcmp(argv[num_opts+2],"2")) DEBUG=2; else { fprintf(stderr,"Debug level \"%s\" not recognized!\n",argv[num_opts+2]); fprintf(stderr,"-d requires an argument of 1 or 2\n"); exit(0); } } else { fprintf(stderr,"-d requires an argument of 1 or 2\n"); exit(0); } printf("Debug type: %d\n",DEBUG); num_opts+=2; break; case 'h': /* fall-through */ default: usage(); } /* end of switch */ } /* end of while */ ARGSDONE: /* Clear timetable structures */ clear_structs(); clear_structs2(); /* setup signals properly */ for(n = 1; n < NSIG; n++) signal(n, sigfunc); signal(SIGCLD, SIG_IGN); reset_alarm(30,0); /* Read configuration file */ read_config(); if (argc <= num_opts+1) { printf("No hosts specified to check!\n\n"); usage(); exit(0); } if (HTML) { fprintf(stdout,"\n"); fprintf(stdout,"\n"); fprintf(stdout,"\n"); fprintf(stdout,"\n"); } for (i=0;i\n",i,a,myargs[i]); } if (portreset) { if (!strcmp(myargs[1],"-P")) printf("Resetting port %s on %s..
\n",resetport,myargs[3]); else printf("Resetting port %s on %s\n",resetport,argv[3]); } for (i=num_opts+1;i\n",i,myargs[i],host); START: if (group_at) a=group_pos+1; else a=0; /* clear_structs(); */ for (a=a;a\n",host,group_at,group_count,group_pos); */ break; } } /* end of for */ if (!found) { printf("Cant find host %s in table!\n",host); if (HTML) printf("\n"); exit(0); } else { found=0; dpi=a; globhold=a; a=0; } strncpy(timetable[dpi].host,pwtable[dpi].hostname,sizeof(timetable[dpi].host)); if (glob_sort) pwtable[dpi].sortTime=glob_sort-1; portsortstr[0]=0; timesortstr[0]=0; speedsortstr[0]=0; if (pwtable[dpi].sortTime==1) strcpy(timesortstr,""); else if (pwtable[dpi].sortTime==2) strcpy(speedsortstr,""); /* printf("DPI: %d host: %s host: %s\n",dpi,timetable[dpi].host,pwtable[5].name); */ if (killtime) { listindex+=timetable[dpi].numlines; timetable[dpi].numlines=0; } else listindex=0; if (addtime) { printf("%s - %s (%s)\n", pwtable[dpi].name, timetable[dpi].host, get_time_as_string()); } /* See if we're checking a card in a multiple pop chassis */ if (isalpha(timetable[dpi].host[strlen(timetable[dpi].host)-1])) timetable[dpi].host[strlen(timetable[dpi].host)-1]=0; if (!summaryOnly) { if (HTML) { fprintf(stdout,"
\n"); if (dpi==0) fprintf(stdout,"%s\n", get_time_as_string()); fprintf(stdout,"

\n"); fprintf(stdout,"\n"); cols=9; if (showSpeed) cols++; fprintf(stdout,"", cols,pwtable[dpi].name,timetable[dpi].host); if (pwtable[dpi].type==0) { if (pwtable[dpi].sortTime) { fprintf(stdout,"\n\n"); } else { fprintf(stdout,"\n\n"); } } else if (pwtable[dpi].type==1) { if (showSpeed) sprintf(speed,"",pwtable[dpi].sortTime==2?"":"",pwtable[dpi].sortTime==2?"":""); else strcpy(speed,""); fprintf(stdout,"\n%s\n", pwtable[dpi].sortTime==0?"":"", pwtable[dpi].sortTime==0?"":"", speed, pwtable[dpi].sortTime==1?"":"", pwtable[dpi].sortTime==1?"":""); /* if (pwtable[dpi].sortTime) { fprintf(stdout,"\n%s\n",speed); } else { fprintf(stdout,"\n%s\n",speed); } */ } /* end of else if type */ } else if (portreset || killtime) { } fflush(stdout); } /* end of not summary only */ hp=0; pmwho(); glob_time=0; glob_speed=0; /* if in group we'll print totals later after loop */ if (summaryOnly) { printf("[ Lines-In-Use: %d out of %d ]\n", timetable[dpi].numlines, pwtable[dpi].linestotal); } if (HTML) { fprintf(stdout,"", cols,timetable[dpi].numlines,pwtable[dpi].linestotal-timetable[dpi].numlines); fprintf(stdout,"
%s (%s)
PortUsernameHostnameConn TypeLink StatusSession TimeIdle Time
PortUsernameHostnameConn TypeLink StatusSession TimeIdle Time
%sSpeed%s
%sPort%sUsernameHostnameConn TypeType of IP%sSession Time%s
PortUsernameHostnameConn TypeType of IPSession Time
PortUsernameHostnameConn TypeType of IPSession Time
Active modems: %d    Free modems: %d
\n
"); fprintf(stdout,"\n\n"); } if (killtime) { if (group_at) { if (DEBUG==2) printf("Group lines at: %d\n",group_lines); group_lines+=timetable[dpi].numlines; if (DEBUG==2) printf("Group lines now at: %d\n",group_lines); } } if (!group_at) { if (killtime) { if (timetable[dpi].numlines < pwtable[dpi].beforekill) continue; } else if (portreset) { } else continue; } else if (group_at==group_count) { if (DEBUG==2) printf("This is the last host in group (%s:%s:%d)\n",pwtable[dpi].name,pwtable[dpi].groupname,group_at); if (killtime) { if (group_lines < pwtable[dpi].beforekill) { group_pos=0; group_count=0; group_at=0; /* we're good, no killing */ if (DEBUG==2) printf("Less than needed: Active lines: %d Beforekill lines: %d\n",group_lines,pwtable[dpi].beforekill); group_lines=0; continue; } /* end of if less than beforekill */ if (DEBUG==2) printf("We're gonna kill: Active lines: %d Beforekill lines: %d\n",group_lines,pwtable[dpi].beforekill); } /* end of if killtime */ else { group_pos=0; group_count=0; group_at=0; group_lines=0; continue; } } else if (group_at) { goto START; } if (killtime) { sprintf(buf,"%s: Lines: %d (kill at %d)\n",get_time_as_string(),group_lines,pwtable[dpi].beforekill); if (DEBUG==2) printf("%s\n",buf); /* wlog(buf); */ /* let's calculate the high port for this host */ dd=0; xx=0; indexer=0; cindexer=0; for (xx=0;xx timetable[cindexer].timeon[indexer]) { indexer=dd; cindexer=xx; } /* end of if */ } /* end of dd sub-for */ dd=0; } /* end of xx for */ if (strlen(timetable[cindexer].port[indexer]) > 1) { sprintf(buf,"%s: At max lines, Got HighPort: %s HighTime: %s Active Lines: %d\n",get_time_as_string(),timetable[cindexer].port[indexer],timetable[cindexer].colontime[indexer],group_lines); if (DEBUG==2) printf("%s\n",buf); /* wlog(buf); */ if (timetable[cindexer].timeon[indexer] >= KILL_TOTAL) { /* sprintf(buf,"CalcHtime: %d LineHtime: %s LineHPort: %s CIndexer: %d Indexer: %d KILL_TOTAL: %d\n", timetable[cindexer].timeon[indexer],timetable[cindexer].colontime[indexer], timetable[cindexer].port[indexer],cindexer,indexer,KILL_TOTAL); */ reset_port(timetable[cindexer].port[indexer],timetable[cindexer].host); wlog(get_time_as_string()); wlog(" USR-killer disconnected user "); wlog(timetable[cindexer].username[indexer]); wlog(" ("); wlog(timetable[cindexer].port[indexer]); wlog(") on "); wlog(pwtable[cindexer].name); wlog(" ("); wlog(timetable[cindexer].host); wlog(") with time "); sprintf(buf,"%s",timetable[cindexer].colontime[indexer]); /* Formatted */ wlog(buf); wlog(". Active lines: "); sprintf(buf,"%d",group_lines); wlog(buf); wlog("\n\n"); } else { wlog(get_time_as_string()); wlog(" USR-killer didn't disconnect user "); wlog(timetable[cindexer].username[indexer]); wlog(" ("); wlog(timetable[cindexer].port[indexer]); wlog(") not high enough time on "); wlog(pwtable[cindexer].name); wlog(" ("); wlog(timetable[cindexer].host); wlog(") with time "); sprintf(buf,"%s",timetable[cindexer].colontime[indexer]); /* Formatted */ wlog(buf); wlog(". Active lines: "); sprintf(buf,"%d",group_lines); wlog(buf); wlog("\n"); } } /* end of strlen highport */ dd=0; xx=0; indexer=0; cindexer=0; group_pos=0; group_count=0; group_at=0; group_lines=0; /* test..are we resetting group lines */ /* correctly on high kill */ a=0; } /* end of if killtime */ else if (portreset) { reset_port(resetport,timetable[dpi].host); dd=0; indexer=0; for (dd=0;ddReset user %s on port %s


\nClose
\n",timetable[dpi].username[indexer],timetable[dpi].port[indexer]); else printf("Reset user %s on port %s\n",timetable[dpi].username[indexer],timetable[dpi].port[indexer]); } /* end of if portreset */ startportchecked=0; username_g[0]=0; hostname2[0]=0; portname[0]=0; typename[0]=0; statusname[0]=0; timename[0]=0; timename_hour[0]=0; idlename[0]=0; resetport[0]=0; buf[0]=0; } /* end of for */ if (HTML) fprintf(stdout,"\n\n"); exit(0); } /* Log string to file */ void wlog(str) char *str; { FILE *fp; char ipjunk[550]; ipjunk[0]=0; if (HOWTO_LOG==0) return; else if (HOWTO_LOG==1) { sprintf(ipjunk,"%s/%s",LOG_DIR,KILL_LOG_FILE); fp=fopen(ipjunk,"a"); } else if (HOWTO_LOG==2) { sprintf(ipjunk,"%s/.%s.log",LOG_DIR,pwtable[dpi].groupname); fp=fopen(ipjunk,"a"); } else if (HOWTO_LOG==3) { if (portreset) sprintf(ipjunk,"%s/.%s-manual.log",LOG_DIR,pwtable[dpi].groupname); else if (killtime) sprintf(ipjunk,"%s/.%s-auto.log",LOG_DIR,pwtable[dpi].groupname); else sprintf(ipjunk,"%s/.%s.log",LOG_DIR,pwtable[dpi].groupname); fp=fopen(ipjunk,"a"); } else return; fputs(str,fp); fclose(fp); } /* show usage info */ void usage(void) { fprintf(stderr, "usage: %s [-achkHST] [-f /path/to/config] [-d 1|2] [-s sort_type] [-P port_name] [hub1 hub2...]\n",progname); fprintf(stderr, "\t-a\tshow all lines instead of just the ones in use (only netserver)\n"); fprintf(stderr, "\t-c\tshow connect speed info in output (hyperarc only)\n"); fprintf(stderr, "\t-d\tturn on debugging. takes an argument of 1 or 2\n"); fprintf(stderr, "\t-f\tspecific config file to use (default is %s)\n",CONF); fprintf(stderr, "\t-h\tshow this message\n"); fprintf(stderr, "\t-k\tkill highest session time user on hubX, if requirements met\n"); fprintf(stderr, "\t-s\toverride config and sort by 'sort_type' which can be \"time\" \"speed\" or \"port\"\n"); fprintf(stderr, "\t-H\tadd html codes to the output (use when part of a web page)\n"); fprintf(stderr, "\t-S\tshow summary information only\n"); fprintf(stderr, "\t-T\tadd time and host info before the outputted list\n"); fprintf(stderr, "\t-P\treset port 'port_name' on host hubX\n"); fprintf(stderr, "\n"); exit(0); } /* relogin to hosts and reset ports */ void reset_port(str,porthost) char *str; char *porthost; { int n; int size=sizeof(struct sockaddr_in); unsigned long f; struct sockaddr_in raddr; char hosttemp[50]; char pw[50]; char username2[50]; pw[0]=0; hosttemp[0]=0; username2[0]=0; /* Zero out memory for address */ memset((char *)&raddr, 0, size); if((f = inet_addr(porthost)) == -1L) { printf("CONNECT: Unknown ip address %s\n", porthost); exit(0); } (void)bcopy((char *)&f,(char *)&raddr.sin_addr,sizeof(f)); raddr.sin_port = htons((unsigned short)atoi(TELNET_PORT)); raddr.sin_family = AF_INET; if ((glob_fd = socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET) { printf("CONNECT: Cannot make socket %d:%s\n",errno, strerror(errno)); exit(0); } if (connect(glob_fd, (struct sockaddr *)&raddr, sizeof(raddr)) == SOCKET_ERROR) { if (errno == ECONNREFUSED) { printf("CONNECT: Connection refused to %s!\n", porthost); } else if (errno == ETIMEDOUT) { printf("CONNECT: Connection timed out to %s!\n", porthost); } else if (errno == ENETUNREACH) { printf("CONNECT: Network unreachable to %s!\n", porthost); } else if (errno != EINPROGRESS) { printf("CONNECT: Unknown problem connecting to %s! %d:%s\n", porthost,errno,strerror(errno)); } exit(0); } if (DEBUG>=1) printf("CONNECT: reset_port: Connected to %s\n",porthost); fflush(stdout); strncpy(pw,pwtable[globhold].password,sizeof(pw)); strncpy(username2,pwtable[globhold].username,sizeof(username2)); for(;;) { n = do_read(glob_fd, "login: ", TELNET_PROMPT, 0, 15); if (n != 1) { n = -1; /* TELNET_PROMPT == connection failed */ break; } /* end of if */ if ((n = do_write(glob_fd, username2, strlen(username2))) < 0) break; if (pwtable[globhold].type==1) { if ((n = do_write(glob_fd, "\r", 1)) < 0) break; } else { if ((n = do_write(glob_fd, "\n", 1)) < 0) break; } if ((n = do_read(glob_fd, "Password: ", NULL, 0, 10)) < 0) break; if ((n = do_write(glob_fd, pw, strlen(pw))) < 0) break; if (pwtable[globhold].type==1) { if ((n = do_write(glob_fd, "\r", 1)) < 0) break; } else { if ((n = do_write(glob_fd, "\n", 1)) < 0) break; } if (DEBUG>=1) printf("LOGIN: reset_port: Logged in to %s\n",porthost); fflush(stdout); if (pwtable[globhold].type==1) { if ((n = do_write(glob_fd, "\r", 1)) < 0) break; } else { if ((n = do_write(glob_fd, "\n", 1)) < 0) break; } if (pwtable[globhold].type==1) { if ((n = do_read(glob_fd, HYPER_COM_PROMPT, NULL, 0, 10)) < 0) break; } else { if ((n = do_read(glob_fd, NS_COM_PROMPT, NULL, 0, 10)) < 0) break; } if (DEBUG>=1) printf("COMMAND: reset_port: Read prompt from %s\n",porthost); fflush(stdout); xx=0; if (pwtable[globhold].type==1) { strcpy(resets, "hangup interface "); strcat(resets, str); strcat(resets, "\n"); } else { strcpy(resets, "reset "); strcat(resets, str); strcat(resets, "\n"); } if (DEBUG>=1) printf("SEND: reset_port: sending command \"%s\"\n",resets); fflush(stdout); /* printf("DEBUG:%s ", resets); */ if ((n = do_write(glob_fd, resets, strlen(resets))) < 0) { printf("do_write of hangup command to %s failed!\n",porthost); fflush(stdout); } break; }/*end for loop*/ CLOSE(glob_fd); if (DEBUG>=1) { printf("CONNECT: reset_port: Closed connection to %s\n",porthost); fflush(stdout); } } /* "show session" and logging out from 'hostname' */ void pmwho() { int i=0,tempcount=0,found=0,timeon=0,timeon2=0,a=0,b=0; char line2[500]; char chunk0[500]; char line[LINELEN+1]; char templine[LINELEN+1]; int n; int size=sizeof(struct sockaddr_in); unsigned long f; struct sockaddr_in raddr; char hosttemp[50]; char pw[50]; char username2[50]; pw[0]=0; hosttemp[0]=0; username2[0]=0; /* Zero out memory for address */ memset((char *)&raddr, 0, size); if((f = inet_addr(timetable[dpi].host)) == -1L) { printf("CONNECT: Unknown ip address %s\n", timetable[dpi].host); exit(0); } (void)bcopy((char *)&f,(char *)&raddr.sin_addr,sizeof(f)); raddr.sin_port = htons((unsigned short)atoi(TELNET_PORT)); raddr.sin_family = AF_INET; if ((glob_fd = socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET) { printf("CONNECT: Cannot make socket %d:%s\n",errno, strerror(errno)); exit(0); } if (connect(glob_fd, (struct sockaddr *)&raddr, sizeof(raddr)) == SOCKET_ERROR) { if (errno == ECONNREFUSED) { printf("CONNECT: Connection refused to %s!\n", timetable[dpi].host); } else if (errno == ETIMEDOUT) { printf("CONNECT: Connection timed out to %s!\n", timetable[dpi].host); } else if (errno == ENETUNREACH) { printf("CONNECT: Network unreachable to %s!\n", timetable[dpi].host); } else if (errno != EINPROGRESS) { printf("CONNECT: Unknown problem connecting to %s! %d:%s\n", timetable[dpi].host,errno,strerror(errno)); } exit(0); } if (DEBUG>=1) printf("CONNECT: Connected to %s\n",timetable[dpi].host); strncpy(pw,pwtable[globhold].password,sizeof(pw)); strncpy(username2,pwtable[globhold].username,sizeof(username2)); for(;;) { n = do_read(glob_fd, "login: ", TELNET_PROMPT, 0, 15); if (n != 1) { n = -1; /* TELNET_PROMPT == connection failed */ break; } /* end of if */ if (DEBUG>=1) printf("LOGIN: Got login prompt from %s\n",timetable[dpi].host); if ((n = do_write(glob_fd, username2, strlen(username2))) < 0) break; if (pwtable[dpi].type==1) { if ((n = do_write(glob_fd, "\r", 1)) < 0) break; } else { if ((n = do_write(glob_fd, "\n", 1)) < 0) break; } if (DEBUG>=1) printf("LOGIN: Wrote username to %s\n",timetable[dpi].host); if ((n = do_read(glob_fd, "Password: ", NULL, 0, 10)) < 0) break; if (DEBUG>=1) printf("LOGIN: Read password prompt from %s\n",timetable[dpi].host); if ((n = do_write(glob_fd, pw, strlen(pw))) < 0) break; if (pwtable[dpi].type==1) { if ((n = do_write(glob_fd, "\r", 1)) < 0) break; } else { if ((n = do_write(glob_fd, "\n", 1)) < 0) break; } if (DEBUG>=1) printf("LOGIN: Wrote password to %s\n",timetable[dpi].host); if (n < 0) break; if (pwtable[dpi].type==1) { if ((n = do_write(glob_fd, "\r", 1)) < 0) break; } else { if ((n = do_write(glob_fd, "\n", 1)) < 0) break; } if (pwtable[dpi].type==1) { if ((n = do_read(glob_fd, HYPER_COM_PROMPT, NULL, 0, 10)) < 0) break; } else { if ((n = do_read(glob_fd, NS_COM_PROMPT, NULL, 0, 10)) < 0) break; } if (DEBUG>=1) printf("LOGIN: Read prompt from %s\n",timetable[dpi].host); if (pwtable[dpi].type==1) { if ((n = do_write(glob_fd, NOPAGE_COMMAND, 5)) < 0) break; if ((n = do_read(glob_fd, NOPAGE_COMMAND, NULL, 0, 10)) < 0) break; if ((n = do_read(glob_fd, HYPER_COM_PROMPT, NULL, 0, 10)) < 0) break; /* get time */ if ((n = do_write(glob_fd, "show time\n", 5)) < 0) break; if ((n = do_read(glob_fd, "show time\n", NULL, 0, 10)) < 0) break; if ((n = do_read(glob_fd, "System Date", NULL, 0, 10)) < 0) break; if ((n = do_read(glob_fd, HYPER_COM_PROMPT, NULL, 0, 10)) < 0) break; /* get time */ } if (pwtable[dpi].type==1) { if ((n = do_write(glob_fd, "list connections\n", 5)) < 0) break; } else { if ((n = do_write(glob_fd, "show session\n", 5)) < 0) break; } for(;;) { if (pwtable[dpi].type==1) { n = do_read(glob_fd, HYPER_COM_PROMPT, HYPER_COM_PROMPT, 1, 10); if ((n==1) || (n==2)) break; } else { n = do_read(glob_fd, "-- Press Return for More -- ", NS_COM_PROMPT, 1, 30); if ((n < 0) || (n == 2)) break; } if ((n = do_write(glob_fd, "\n", 5)) < 0) break; } /* end of sub for */ if (n < 0) break; startportchecked=0; if (summaryOnly) break; /* If hyperarc chassis we do out line breakout here since we have to */ /* get information via 2 commands, what a pain */ if (pwtable[dpi].type==1 && !Basic) { if ((n = do_write(glob_fd, "list ip networks\n", 5)) < 0) break; hypercount=0; clear_list(1); for(;;) { n = do_hyper_read2(glob_fd, HYPER_COM_PROMPT, HYPER_COM_PROMPT, 1, 10); if ((n==1) || (n == 2)) break; } /* end of sub for */ if (n < 0) break; hypercount=0; if (DEBUG>1) { printf("
Connection list
\n"); for (a=0;a%s\n",connlist[a]); } } a=0; while (strlen(connlist[a])) { if (DEBUG > 1) printf("in connlist line %d \"%s\"\n",a,connlist[a]); strncpy(line,connlist[a],sizeof(line)); midcpy(line,portname,S0,15); midcpy(line,username_g,HYPER_USER_POS,HYPER_LOG_POS-1); midcpy(line,typename,HYPER_LOG_POS,HYPER_IDLE_POS-2); strncpy(timename, &line[HYPER_START_POS],20); /* convert stupid start time into real session time */ strncpy(timename,hyper_session_time_formatted(timename,line),sizeof(timename)); if (strstr(timename,":")) { for (i=0;i 1) printf("in templist line %d \"%s\"\n",b,templist[b]); /* Copy username and formatted time to list */ midcpy(templist[b],glob_username,0,HYPER_NET_USER_POS_END); n=0; for (i=strlen(glob_username);i>0;--i) { if (glob_username[i]=='-') { if (n) { glob_username[i]=0; i=-1; break; } else n++; } } /* end for */ n=0; /* should i be doing this?? */ strncpy(glob_username,clear_trail_space(glob_username),sizeof(glob_username)); if (DEBUG > 1) printf("looking for user %s, found network user %s\n",username_g,glob_username); midcpy(templist[b],glob_portname,HYPER_IP_PORT_POS,HYPER_IP_PORT_POS_END); strncpy(glob_portname,clear_trail_space(glob_portname),sizeof(glob_portname)); midcpy(templist[b],glob_hostname,HYPER_IP_ADDY_POS,200); strncpy(glob_hostname,clear_trail_junk(glob_hostname),sizeof(glob_hostname)); midcpy(templist[b],glob_statusname,HYPER_IP_ADDY_TYPE,HYPER_IP_ADDY_TYPE_END); /* compare port from hyper read with port from */ /* current line, if a match, we found our line */ if (!strcmp(clear_trail_space(username_g),glob_username)) { portfound=1; break; } /* if (!strcmp(clear_trail_space(portname),glob_portname)) { portfound=1; break; } */ b++; } /* end of while templist b */ b=0; if (portfound==1) portfound=0; else { strcpy(username_g,"NEW-CALL"); strcpy(glob_hostname,"Pending"); strcpy(glob_statusname,"--- "); } /* end of else portfound */ /* printf("
LAST PORTNAME: \"%s\" \"%s\"

\n",portname,glob_hostname); */ strncpy(username_g,clear_trail_space(username_g),sizeof(username_g)); /* ok we have got all out port and ip info, should we now */ /* get the speed of this connection? */ if (showSpeed) { sprintf(mess,"show interface %s\n",portname); if ((n = do_write(glob_fd, mess, 5)) < 0) break; if ((n = do_read(glob_fd, mess, NULL, 0, 10)) < 0) break; if ((n = do_read(glob_fd, "Speed", NULL, 0, 10)) < 0) break; if ((n = do_read(glob_fd, HYPER_COM_PROMPT, NULL, 0, 10)) < 0) break; } /* end of if showSpeed */ if (HTML) { strncpy(portname,clear_trail_space(portname),sizeof(portname)); strncpy(typename,clear_trail_space(typename),sizeof(typename)); strcpy(line,""); strcat(line,"\n"); sprintf(templine,"%s%s",portsortstr,portname); strcat(line,templine); for (i=0;i%s",mess,username_g); if (i==-1) strcat(templine," (@)"); else strcat(templine,""); i=0; strcat(line,templine); if (showSpeed) { sprintf(templine,"%s%7ld",speedsortstr,glob_speed); strcat(line,templine); } sprintf(templine,"%16s",glob_hostname); strcat(line,templine); sprintf(templine,"%s",typename); strcat(line,templine); sprintf(templine,"%s",glob_statusname); strcat(line,templine); if ((strlen(timename) > 4) || ((strlen(timename) == 4) && (atoi(timename_hour)>=high_total) )) { sprintf(templine,"%s%s",timesortstr,timename); strcat(line,templine); } else { if (!strcmp(username_g,"NEW-CALL")) sprintf(templine,"%s- (%s)",timesortstr,timename); else sprintf(templine,"%s%s",timesortstr,timename); strcat(line,templine); } strcat(line,"\n"); strncpy(connlist[a],line,sizeof(connlist[a])); } /* end of if HTML */ else { if (killtime) { } else if (!portreset) { /* printf("Hostname2: \"%s\"\n",glob_hostname); printf("Statname2: \"%s\"\n",glob_statusname); printf("Username2: \"%s\"\n",username_g); printf("Typename2: \"%s\"\n",typename); printf("Timename2: \"%s\"\n",timename); */ if (strstr(username_g,"@")) { for (i=0;i=1)) { printf("going in for
\n\r"); printf("CONNECTION LIST
\n\r"); for (a=0;a<1000;++a) { if (strlen(connlist[a])) printf("%d: %s
\n\r",a,strip_html(connlist[a])); } a=0; } if (strlen(connlist[0])) { strncpy(templist[tempcount],connlist[i],sizeof(templist[tempcount])); /* printf("copied first line: %s
\n\r",strip_html(templist[tempcount])); */ } i++; while (strlen(connlist[i])) { /* printf("Line 5: %s\n",connlist[i]); */ a=0; b=0; timeon=0; line2[0]=0; chunk0[0]=0; /* check time from stored line */ strncpy(line2,connlist[i],sizeof(line2)); for (a=0;a\n\r",timeon); */ while (!found) { timeon2=0; line2[0]=0; chunk0[0]=0; /* GET TIME FROM LIST ITEM */ /* check time from stored line */ /* printf("strlen of tempcount %d is %d\n",tempcount,strlen(templist[tempcount])); */ if (!strlen(templist[tempcount])) goto COPYEND; strncpy(line2,templist[tempcount],sizeof(line2)); for (a=0;a\n\r",timeon2); */ if (timeon >= timeon2) { /* printf("real \"%d\" greater than or eql to temp \"%d\", moving up to tempcount of %d..
\n\r",timeon,timeon2,tempcount); */ /* if (timeon2!=0) */ move_down(tempcount); COPYEND: /* printf("line were copying over is: %s
\n\r",strip_html(connlist[i])); printf("line were copying over is: %s
\n\r",connlist[i]); */ strncpy(templist[tempcount],connlist[i],sizeof(templist[tempcount])); /* printf("TEMPLIST AFTER CHANGE
\n\r"); for (a=0;a\n\r",a,strip_html(templist[a])); } a=0; */ found=1; } else { /* printf("real LESS than temp, continuing..
\n\r"); */ tempcount++; } } /* end of sub-while */ i++; tempcount=0; found=0; } /* end of while */ i=0; /* printf("CONNECTION LIST
\n\r"); for (a=0;a<1000;++a) { if (strlen(connlist[a])) printf("%d: %s
\n\r",a,strip_html(connlist[a])); } a=0; printf("copying list..
\n\r"); */ copy_list(); /* printf("CONNECTION LIST
\n\r"); for (a=0;a<1000;++a) { if (strlen(connlist[a])) printf("%d: %s
\n\r",a,strip_html(connlist[a])); } a=0; */ } /* end of if sortTime */ if (DEBUG>=1) printf("printing users..
\n\r"); if (!killtime && !portreset) print_users(); if (DEBUG>=1) printf("clearing list..
\n\r"); clear_list(1); clear_list(2); break; } CLOSE(glob_fd); if (DEBUG>=1) printf("CONNECT: Closed connection to %s\n",timetable[dpi].host); } /* read information from 'telnet', stop reading upon errors, on a timeout */ /* and when the beginning of a line equals to 'text1' (and then return 1) */ /* or when the beginning of a line equals to 'text2' (and then return 2), */ /* if 'show' is non zero we display the information to the screen */ /* (using 'stdout'). */ int do_read(int fd, char *text1, char *text2, int show, int timeout) { int num_slot_chunk1=0; int num_slot_chunk2=0; int num_slot_chunk3=0; char line[LINELEN+1]; char templine[LINELEN+1]; int n, uu, len1, len2, m, t; int tt=0; char str1[20]; char str2[20]; int hours; time_t tm; struct tm tmNowBuf; line[0]=0; templine[0]=0; /* username_g[0]=0; hostname2[0]=0; portname[0]=0; typename[0]=0; statusname[0]=0; timename[0]=0; timename_hour[0]=0; idlename[0]=0; printf("Looking for \"%s\" or \"%s\" timeout %d\n
",text1,text2,timeout); fflush(stdout); */ /* setup alarm (so we won't hang forever upon problems) */ reset_alarm(timeout,1); len1 = strlen(text1); len2 = (text2 != NULL) ? strlen(text2) : 0; /* start reading from 'telnet' */ for(;;) { n = 0; for(t=0;t",mess); else printf("%s",mess); sprintf(mess,"%s: Line read: %s\n",get_time_as_string(),strip_html(line)); wlog(mess); if (HTML) printf("%s
",mess); else printf("%s",mess); alarm(0); /* disable alarm */ return(-1); } /* end of if m */ if ((line[n] == '\r')) continue; if (n >= len1-1) { if (strncmp(&line[n-(len1-1)], text1, len1) == 0) { if (!strcmp(text1,"System Date")) { read(fd,line,25); for(t=0;t=1) printf("Line 1: \"%s\"\n",line); tt=37; uu=0; while (tt) { t = read(fd,&line[uu],tt); if (DEBUG>=1) printf("Line 1a: (%d left) \"%s\" %d\n",tt-t,line,strlen(line)); tt-=t; uu+=t; } tt=0; if (DEBUG>=1) printf("Line 1b: \"%s\"\n",line); for(t=0;t=1) printf("Line 3: %s \"%s\"\n",portname,line); strcpy(line,clear_leading_space(line)); glob_speed = atol(line); if (DEBUG>=1) printf("Line 4: \"%ld\"\n",glob_speed); } /* end of if Speed */ /* we found the keyword we were searching for */ TIME_DONE: alarm(0); /* disable alarm */ return(1); } } /* end of if n */ if ((text2 != NULL) && (n >= len2-1)) { if (strncmp(&line[n-(len2-1)], text2, strlen(line)) == 0) { /* we found the keyword we were searching for */ alarm(0); /* disable alarm */ return(2); } } /* end of if text2 */ if (line[n] == '\n') break; n++; } /* end of sub for */ if (show) { username_g[0]=0; hostname2[0]=0; portname[0]=0; typename[0]=0; statusname[0]=0; timename[0]=0; timename_hour[0]=0; idlename[0]=0; line[++n] = '\0'; if (startportchecked==2) { if (DEBUG>=1) printf("in do_read after last port 1!\n"); fflush(stdout); return(1); } /* Netserver chassis card */ if (pwtable[dpi].type==0) { /* First 2 lines of output */ if (((line[0] == '-') && (line[1] == '-')) || (strncmp(&line[0], "Port",4) == 0)){ fflush(stdout); } /* end of if */ else { for(t=0;t=1) printf("Hit last port %s\n",pwtable[dpi].lastport); for(t=0;t 1)) { */ if ((strlen(line) > 1) && (!strncmp(&line[IDLE_POS], "ESTABLISHED",11) || !strncmp(&line[IDLE_POS], "CONNECTING",10))) { if (!strncmp(&line[USER_POS],"system ", 8)) continue; if (killtime || HTML || portreset) { /* if (!strncmp(&line[IDLE_POS], "ESTABLISHED",11) || !strncmp(&line[IDLE_POS], "CONNECTING",10)) { */ for(t=0;t\n",line); printf("username1: \"%s\" %d
\n",username_g,strlen(username_g)); */ if (HTML) { if (!strstr(line,"MPlink")) { strncpy(username_g, &line[USER_POS],8); } else { strncpy(username_g, &line[USER_POS],16); } /* printf("username2: \"%s\"
\n",username_g); */ strncpy(hostname2, &line[22],16); strncpy(typename, &line[39],7); strncpy(statusname, &line[51],12); strncpy(timename, &line[START_POS],6); for (dd=0;dd\n"); sprintf(templine,"%s%s",portsortstr,portname); strcat(line,templine); if (strstr(URL,"userinfo.cgi")) sprintf(mess,URL,pwtable[dpi].hostname,username_g,portname); else sprintf(mess,URL,portname,pwtable[dpi].hostname); sprintf(templine,"%s%s",mess,username_g,dd == -1 ? " (@)" : ""); dd=0; strcat(line,templine); sprintf(templine,"%16s",hostname2); strcat(line,templine); sprintf(templine,"%s",typename); strcat(line,templine); sprintf(templine,"%s",statusname); strcat(line,templine); if ((strlen(timename) > 4) || ((strlen(timename) == 4) && (atoi(timename_hour)>=high_total) )) { sprintf(templine,"%s%s",timesortstr,timename); strcat(line,templine); } else { sprintf(templine,"%s%s",timesortstr,timename); strcat(line,templine); } sprintf(templine,"%s",idlename); strcat(line,templine); strcat(line,"\n"); /* printf("will copy to slot %d
\n\r",listindex); */ strcpy(connlist[listindex],line); connlist[listindex][strlen(connlist[listindex])]=0; time(&tm); /* printf("received input line: %s :%s
\n\r",strip_html(connlist[listindex]),ctime(&tm)); printf("CONNECTION LIST
\n\r"); for (a=0;a<1000;++a) { if (strlen(connlist[a])) printf("%d: %s
\n\r",a,strip_html(connlist[a])); } a=0; */ } /* end of if HTML */ else if (killtime) { } else if (!portreset) { fputs(line, stdout); } } /* end of not summary only */ /* fflush(stdout); */ timetable[dpi].numlines++; listindex++; } /* end of if real, used port */ } /* end of else not header line */ } /* end of Netserver */ else { /* HiperARC chassis */ /* First few lines of output */ if (strncmp(&line[0], "CONNECTIONS",11) == 0){ fflush(stdout); } /* end of if */ else if (strncmp(&line[0], "IfName",6) == 0){ fflush(stdout); } /* end of if */ else { for(t=0;t=1) { printf("Line : \"%s\"\n",line); printf("Line slot : \"%d\"\n",num_slot_chunk1); printf("Start slot: \"%d\"\n",num_slot_chunk2); printf("End slot : \"%d\"\n",num_slot_chunk3); } /* if line slot < config start slot */ if ((num_slot_chunk1 >= num_slot_chunk2) && (num_slot_chunk1 <= num_slot_chunk3)) { if (DEBUG>=1) printf("Start port found!\n"); startportchecked=1; } else { for(t=0;t 1)) { if (!strncmp(&line[HYPER_USER_POS],"system ", 8)) continue; if (!strncmp(&line[HYPER_IDLE_POS], "PPP",3)) { /* get start time string */ strncpy(str1, &line[HYPER_START_POS],20); /* get session time from dumb 3com format */ timetable[dpi].timeon[hp]=hyper_session_time(str1,0,""); strncpy(timetable[dpi].port[hp], &line[S0], 15); /* Copy username and formatted time to list */ midcpy(line,timetable[dpi].username[hp],HYPER_USER_POS,HYPER_LOG_POS); strcpy(timetable[dpi].colontime[hp],hyper_session_time_formatted(str1,line)); dd=0; t=0; for (dd=0;dd",mess); else printf("%s",mess); sprintf(mess,"%s: Line read: %s\n",get_time_as_string(),strip_html(line)); wlog(mess); if (HTML) printf("%s
",mess); else printf("%s",mess); alarm(0); /* disable alarm */ return(-1); } /* end of if m */ if ((line[n] == '\r')) continue; if (n >= len1-1) { if (strncmp(&line[n-(len1-1)], text1, len1) == 0) { /* we found the keyword we were searching for */ alarm(0); /* disable alarm */ return(1); } } /* end of if n */ if ((text2 != NULL) && (n >= len2-1)) { if (strncmp(&line[n-(len2-1)], text2, len2) == 0) { /* we found the keyword we were searching for */ alarm(0); /* disable alarm */ return(2); } } /* end of if text2 */ if (line[n] == '\n') break; n++; } /* end of sub for */ if (show) { line[++n] = '\0'; /* check for ppp line */ if (!strncmp(&line[HYPER_IP_PORT_POS], "slot", 4) && (strlen(line) > 1)) { strcpy(templist[hypercount],line); hypercount++; } /* end of if strncmp real, used port */ } /* end of if show */ } /* end of for */ } /* write a command to the 'telnet' process */ int do_write(int fd, char *text, int timeout) { int err, len, n; /* setup alarm (so we won't hang forever upon problems) */ reset_alarm(timeout,3); len = strlen(text); n = write(fd, text, len); if (n != len) { err = errno; alarm(0); /* disable alarm */ #if 1 if (n < 0) { err_msg("%s: write failed on fd %d: errno=%d (%s)\n", progname, fd, err, strerror(err)); } #endif return(-1); } else { alarm(0); /* disable alarm */ return(0); } } /* our timeout procedure, used to abort malfunctioning connections */ void sigalrm(int signo) { err_msg("%s: timeout on connection to host '%s'\n", progname, timetable[dpi].host); CLOSE(glob_fd); exit(0); } void sigalrm1(int signo) { err_msg("%s: timeout do_read() on connection to host '%s'\n", progname, timetable[dpi].host); CLOSE(glob_fd); exit(0); } void sigalrm2(int signo) { err_msg("%s: timeout do_hyper_read2()ing on connection to host '%s'\n", progname, timetable[dpi].host); CLOSE(glob_fd); exit(0); } void sigalrm3(int signo) { err_msg("%s: timeout do_write() on connection to host '%s'\n", progname, timetable[dpi].host); CLOSE(glob_fd); exit(0); } /* handle the reception of signals */ void sigfunc(int signo) { time_t tm; #if defined(SIGPIPE) if (signo != SIGPIPE) #endif { time(&tm); if (HTML) err_msg("%s: received signal #%d during connection to host '%s' -- aborting: %s
\n", progname, signo, timetable[dpi].host,ctime(&tm)); else err_msg("%s: received signal #%d during connection to host '%s' -- aborting: %s\n", progname, signo, timetable[dpi].host,ctime(&tm)); } CLOSE(glob_fd); exit(1); } /* print error text */ void err_msg(const char *format, ...) { va_list ap; /* show error message */ va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap); fflush(stderr); } /* print error text and exit gracefully */ void err_exit(const char *format, ...) { va_list ap; /* show error message */ va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap); fflush(stderr); /* exit with error */ CLOSE(glob_fd); exit(1); } char *get_time_as_string(void) { time_t now; char *timestring; time(&now); timestring = ctime(&now); timestring[strlen(timestring) - 1] = '\0'; /*trim newline at end*/ return timestring; } void midcpy(char *strf, char *strt, int fr, int to) { int f; for (f=fr;f<=to;++f) { if (!strf[f]) { strt[f-fr]='\0'; return; } strt[f-fr]=strf[f]; } strt[f-fr]='\0'; } void copy_list() { int i=0; int j=0; /* now copy them all back to the original list */ for (i=0;i\n\r"); for (jj=0;jj\n\r",jj,strip_html(templist[jj])); } jj=0; */ for (;aa>=0;--aa) { if (strlen(templist[aa])) { found=1; break; } } if (found) { while (aa >= pos) { strncpy(templist[aa+1],templist[aa],sizeof(templist[aa+1])); aa--; } /* end of while */ } /* end of if */ /* printf("TEMPLIST AFTER MOVE, BEFORE ADDITION
\n\r"); for (jj=0;jj\n\r",jj,strip_html(templist[jj])); } jj=0; */ found=0; aa=0; } char *strip_html(char *buffer) { int i=0,j=0; int pos=0; static char buffer2[300]; buffer2[0]=0; if (!strstr(buffer,"<") || !strstr(buffer,">")) { strncpy(buffer2,buffer,sizeof(buffer2)); goto JUMP; } j=0; pos=0; for (i=0;i')) { pos=0; continue; } else if (pos==1) { continue; } if (buffer[i]=='<') { pos=1; continue; } else { buffer2[j]=buffer[i]; j++; } } /* end of for */ buffer2[j]='\0'; JUMP: return buffer2; } char *clear_trail_space(str) char *str; { int zz=0; static char str4[100]; for (zz=0;zz sizeof(str6)) midcpy(str,str6,zz,sizeof(str6)+zz); else midcpy(str,str6,zz,strlen(str)); return str6; } long DateDay(int month, int day, int year) { if (month > 2) { month += 1; } else { month += 13; year -= 1; } return( ((long)year * 365L) + ((long)year / 4L) - ((long)year / 100L) + ((long)year / 400L) + ((long)month * 306001L / 10000L) /* month * 30.6001 */ + (long)day); } unsigned long hyper_session_time(char *inpstr, int mode, char *line2) { unsigned long on_for_mins; time_t tm_then; struct tm tmBuf; on_for_mins=0; tm_then=0; memset(&tmBuf, 0, sizeof(struct tm)); /* 22-JUN-2000 12:36:06 */ if (my_strptime(inpstr, "%d-%b-%Y %H:%M:%S", &tmBuf) == NULL) { /* printf("CYGTESTErrno2: %d %s\n",errno,strerror(errno)); if (mode==0) printf("my_strptime() failed getting hyperarc time for %s\nCalled from do_read()!\npart was %s\nline was: %s\n",pwtable[dpi].hostname,inpstr,line2); else if (mode==1) printf("my_strptime() failed getting hyperarc time for %s\nCalled from hyper_session_time_formatted()!\npart was %s\nline was: %s\n",pwtable[dpi].hostname,inpstr,line2); */ return 0; } else { tm_then = mktime(&tmBuf); /* printf("Formatted string : \"%s\"
\n",inpstr); printf("Time for formattted string: %ld
\n",tm_then); printf("Formatted string2 : \"%s\"
\n",ctime(&tm_then)); printf("Time for now : %ld
\n",glob_time); printf("Time for now Formatted : \"%s\"
\n",ctime(&glob_time)); printf("Difference (secs) : %ld
\n",glob_time-tm_then); printf("Difference (mins) : %ld
\n",(glob_time-tm_then)/60); */ on_for_mins = (glob_time - tm_then)/60; if ((on_for_mins < 0) || (on_for_mins > 525600)) return 0; } return on_for_mins; } char *hyper_session_time_formatted(char *inpstr, char *line) { unsigned long ftime_hours; unsigned long minutes; char ftime_mins[15]; static char ftime[15]; minutes = hyper_session_time(inpstr,1,line); /* convert minutes to mm or hh:mm */ if (minutes < 60) { sprintf(ftime,"%ld",minutes); } else { ftime_hours=minutes/60; minutes%=60; if (minutes < 10) sprintf(ftime_mins,"0%ld",minutes); else sprintf(ftime_mins,"%ld",minutes); sprintf(ftime,"%ld:%s",ftime_hours,ftime_mins); } /* end of else */ return ftime; } void log_times() { int oo=0,pp=0; FILE *ttt; ttt=fopen("/tmp/timecheck","a"); fprintf(ttt,"\nDPI: %d Numlines: %d\n",dpi,timetable[dpi].numlines); for (pp=0;pp= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0')); digit *= 16; digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0')); return(digit); } char *my_strptime(char *buf, char *fmt, struct tm *tmStruct) { char c; char* ptr; int i; int len; struct dtconv { char *abbrev_month_names[12]; char *month_names[12]; char *abbrev_weekday_names[7]; char *weekday_names[7]; char *time_format; char *sdate_format; char *dtime_format; char *am_string; char *pm_string; char *ldate_format; }; static struct dtconv US_English_fmt = { { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }, { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }, { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }, { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }, "%H:%M:%S", "%m/%d/%y", "%a %b %e %T %Z %Y", "AM", "PM", "%A, %B, %e, %Y" }; ptr = fmt; while (*ptr != 0) { if (*buf == 0) break; c = *ptr++; if (c != '%') { if (isspace((int)c)) while (*buf != 0 && isspace((int)*buf)) buf++; else if (c != *buf++) return 0; continue; } c = *ptr++; switch (c) { case 0: case '%': if (*buf++ != '%') return 0; break; case 'C': buf = my_strptime(buf, US_English_fmt.ldate_format, tmStruct); if (buf == 0) return 0; break; case 'c': buf = my_strptime(buf, "%x %X", tmStruct); if (buf == 0) return 0; /* */ break; case 'D': buf = my_strptime(buf, "%m/%d/%y", tmStruct); if (buf == 0) return 0; break; { i *= 10; i += *buf - '0'; } if (i > 365) return 0; tmStruct->tm_yday = i; break; case 'M': case 'S': if (*buf == 0 || isspace((int)*buf)) break; if (!isdigit((int)*buf)) return 0; for (i = 0; *buf != 0 && isdigit((int)*buf); buf++) { i *= 10; i += *buf - '0'; } if (i > 59) return 0; if (c == 'M') tmStruct->tm_min = i; else tmStruct->tm_sec = i; if (*buf != 0 && isspace((int)*buf)) while (*ptr != 0 && !isspace((int)*ptr)) ptr++; break; case 'H': case 'I': case 'k': case 'l': if (!isdigit((int)*buf)) return 0; for (i = 0; *buf != 0 && isdigit((int)*buf); buf++) { i *= 10; i += *buf - '0'; } if (c == 'H' || c == 'k') { if (i > 23) return 0; } else if (i > 11) return 0; tmStruct->tm_hour = i; if (*buf != 0 && isspace((int)*buf)) while (*ptr != 0 && !isspace((int)*ptr)) ptr++; break; case 'p': len = strlen(US_English_fmt.am_string); if (strncasecmp(buf, US_English_fmt.am_string, len) == 0) { if (tmStruct->tm_hour > 12) return 0; if (tmStruct->tm_hour == 12) tmStruct->tm_hour = 0; buf += len; break; } len = strlen(US_English_fmt.pm_string); if (strncasecmp(buf, US_English_fmt.pm_string, len) == 0) { if (tmStruct->tm_hour > 12) return 0; if (tmStruct->tm_hour != 12) tmStruct->tm_hour += 12; buf += len; break; } return 0; case 'A': case 'a': for (i = 0; i < asizeof(US_English_fmt.weekday_names); i++) { len = strlen(US_English_fmt.weekday_names[i]); if (strncasecmp(buf, US_English_fmt.weekday_names[i], len) == 0) break; len = strlen(US_English_fmt.abbrev_weekday_names[i]); if (strncasecmp(buf, US_English_fmt.abbrev_weekday_names[i], len) == 0) break; } if (i == asizeof(US_English_fmt.weekday_names)) return 0; tmStruct->tm_wday = i; buf += len; break; case 'd': case 'e': if (!isdigit((int)*buf)) return 0; for (i = 0; *buf != 0 && isdigit((int)*buf); buf++) { i *= 10; i += *buf - '0'; } if (i > 31) return 0; tmStruct->tm_mday = i; if (*buf != 0 && isspace((int)*buf)) while (*ptr != 0 && !isspace((int)*ptr)) ptr++; break; case 'B': case 'b': case 'h': for (i = 0; i < asizeof(US_English_fmt.month_names); i++) { len = strlen(US_English_fmt.month_names[i]); if (strncasecmp(buf, US_English_fmt.month_names[i], len) == 0) break; len = strlen(US_English_fmt.abbrev_month_names[i]); if (strncasecmp(buf, US_English_fmt.abbrev_month_names[i], len) == 0) break; } if (i == asizeof(US_English_fmt.month_names)) return 0; tmStruct->tm_mon = i; buf += len; break; case 'm': if (!isdigit((int)*buf)) return 0; for (i = 0; *buf != 0 && isdigit((int)*buf); buf++) { i *= 10; i += *buf - '0'; } if (i < 1 || i > 12) return 0; tmStruct->tm_mon = i - 1; if (*buf != 0 && isspace((int)*buf)) while (*ptr != 0 && !isspace((int)*ptr)) ptr++; break; case 'Y': case 'y': if (*buf == 0 || isspace((int)*buf)) break; if (!isdigit((int)*buf)) return 0; for (i = 0; *buf != 0 && isdigit((int)*buf); buf++) { i *= 10; i += *buf - '0'; } if (c == 'Y') i -= 1900; if (i < 0) return 0; if (i > 138) i -= 30; /* printf("%s produced a year diff of %d\n",buf,i); */ tmStruct->tm_year = i; if (*buf != 0 && isspace((int)*buf)) while (*ptr != 0 && !isspace((int)*ptr)) ptr++; break; } } return buf; } /*** reset alarm - first called from add_user ***/ void reset_alarm(int time, int mode) { if (!mode) signal(SIGALRM, sigalrm); else if (mode==1) signal(SIGALRM, sigalrm1); else if (mode==2) signal(SIGALRM, sigalrm2); else if (mode==3) signal(SIGALRM, sigalrm3); alarm(time); }