/*---------------- 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 */ /* Pretty standard command prompt, unless you've changed it */ #define NS_COM_PROMPT "d> " /* Path to the sendmail program to send out notifications */ #define PATHTO "/usr/sbin/sendmail -t" /* Who do we set the from address to in an email or page? */ #define FROM "nobody@yourdomain.com" /* Who to page. Normally an email address that pages an admin via modem */ /* this goes along with P_SUBJECT which I set to blank for paging */ /* blank to disable */ #define PAGETO "admin-pager@yourdomain.com" /* First person to email notification with a subject of M_SUBJECT */ /* blank to disable */ #define MAILTO1 "admin@yourdomain.com" /* who to email1 */ /* Second person to email notification with a subject of M_SUBJECT */ /* blank to disable */ #define MAILTO2 "admin2@yourdomain.com" /* Subject of page, if you want one */ #define P_SUBJECT "" /* Subject of email, if you want one */ #define M_SUBJECT "NETSERVER PORT STATUS" /* Location of config file */ #define CONF "/etc/usrchk.cfg" /* If you want to do some debugging */ /* valid values are 1 or 2. 2 being more info */ #define DEBUG 0 /*---------------- 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 #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__ */ #if defined(SUN4_SYS) || defined(SOL_SYS) #define TELNET_EXEC "/usr/ucb/telnet" #define TELNET_CLOSE "Connection closed" #define TELNET_PORT "23" #define TELNET_PROMPT "telnet> " #define HAVE_SYS_WAIT #elif defined(DNIX) || defined(HPUX) #define TELNET_EXEC "/usr/bin/telnet" #define TELNET_CLOSE "Trying" #define TELNET_PORT "23" #define TELNET_PROMPT "telnet> " #define HAVE_SYS_WAIT #elif defined(__linux__) #define TELNET_EXEC "/usr/bin/telnet" #define TELNET_CLOSE "Connection closed" #define TELNET_PORT "23" #define TELNET_PROMPT "telnet> " #define HAVE_SYS_WAIT #else /* generic entry */ #define TELNET_EXEC "/usr/bin/telnet" #define TELNET_CLOSE "Connection closed" #define TELNET_PORT "23" #define TELNET_PROMPT "telnet> " /* #define HAVE_SYS_WAIT */ /* #define HAVE_WAIT */ #endif /* header files */ #include #include #include #include #include #if defined(HPUX) #include #define NSIG _NSIG #endif #include #include #if defined(HAVE_SYS_WAIT) #include #elif defined(HAVE_WAIT) #include #else extern pid_t wait(int *status); #endif #include #include #include /* Program declarations */ void pmwho(void); void sigalrm(int signo); void sigfunc(int signo); void err_msg(const char *format, ...); void err_exit(const char *format, ...); void stop_telnet(void); void midcpy(char *strf, char *strt, int fr, int to); void remove_first(char *inpstr); void read_config(); 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); char *get_time_as_string(void); void notify_admin(char *text); int total_hosts=0; int globhold=0; int dpi=0; int MAXCHAS=0; char *progname; char badline[256]; char globstatus[10]; pid_t pid; int got_alarm; int startportchecked=0; char mess[500]; struct { char groupname[32]; /* location group this chassis is in */ char host[32]; /* ip */ 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 kill */ 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]; int main(int argc, char *argv[]) { int i=0,n; progname = argv[0]; /* setup signals properly */ for(n = 1; n < NSIG; n++) signal(n, sigfunc); #if defined(SIGCHLD) signal(SIGCHLD, SIG_IGN); #endif #if defined(SIGCLD) signal(SIGCLD, SIG_IGN); #endif read_config(); for (i=0;i= 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, 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) { 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;t0;--t) { if (isalnum((int)port[t])) { port[t+1]=0; break; } } for(t=0;t= 0) { kill(pid, SIGTERM); kill(pid, SIGKILL); if (kill(pid, 0) >= 0) { for(p = wait(0); (p != -1) && (p != pid); p = wait(0)) ; /* zombies */ } } pid = -1; } } 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'; } /*** removes first word at front of string and moves rest down ***/ void remove_first(char *inpstr) { int newpos,oldpos; newpos=0; oldpos=0; /* find first word */ while(inpstr[oldpos]==' ') { if (!inpstr[oldpos]) { inpstr[0]=0; return; } oldpos++; } /* find end of first word */ while(inpstr[oldpos]!=' ') { if (!inpstr[oldpos]) { inpstr[0]=0; return; } oldpos++; } /* find second word */ while(inpstr[oldpos]==' ') { if (!inpstr[oldpos]) { inpstr[0]=0; return; } oldpos++; } while(inpstr[oldpos]!=0) inpstr[newpos++]=inpstr[oldpos++]; inpstr[newpos]='\0'; } /* 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) */ signal(SIGALRM, sigalrm); alarm(timeout); len = strlen(text); n = write(fd, text, len); if (n != len) { err = errno; alarm(0); /* disable alarm */ #if 0 if (n < 0) { err_msg("%s: write failed: errno=%d (%s)\n", progname, 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) { got_alarm = 1; err_msg("%s: timeout on connection to host '%s'\n", progname, pwtable[globhold].host); stop_telnet(); exit(1); } /* handle the reception of signals */ void sigfunc(int signo) { time_t tm; #if defined(SIGPIPE) if (signo != SIGPIPE) #endif { time(&tm); err_msg("%s: received signal #%d during connection to host %s --aborting: %s\n", progname, signo, pwtable[globhold].host,ctime(&tm)); } stop_telnet(); 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 notify_admin(char *text) { FILE *pp; char tem[256]; sprintf(tem,"%s",PATHTO); if (strlen(PAGETO)) { if (!(pp=popen(tem,"w"))) { exit(0); } fprintf(pp,"From: \"%s\"\n",FROM); fprintf(pp,"To: %s\n",PAGETO); fprintf(pp,"Subject: %s\n\n",P_SUBJECT); fprintf(pp,"%s",text); fputs("\n\n",pp); fputs(".\n",pp); pclose(pp); } if (strlen(MAILTO1)) { if (!(pp=popen(tem,"w"))) { exit(0); } fprintf(pp,"From: \"%s\"\n",FROM); fprintf(pp,"To: %s\n",MAILTO1); fprintf(pp,"Subject: %s\n\n",M_SUBJECT); fprintf(pp,"%s",text); fputs("\n\n",pp); fputs(".\n",pp); pclose(pp); } if (strlen(MAILTO2)) { if (!(pp=popen(tem,"w"))) { exit(0); } fprintf(pp,"From: \"%s\"\n",FROM); fprintf(pp,"To: %s\n",MAILTO2); fprintf(pp,"Subject: %s\n\n",M_SUBJECT); fprintf(pp,"%s",text); fputs("\n\n",pp); fputs(".\n",pp); pclose(pp); } }