#include #include #include #include #include #include #include #include #include #include /*--------------------------------------------------------------------------*/ int pty_phyle; char the_login_prompt[]="login: "; char recent_input[8]; struct termios tp1; struct termios tp2; /*--------------------------------------------------------------------------*/ int at_login_prompt(void); int at_main_prompt(void); void log_pty_data(int the_character); void manual(void); int send_command(char *the_string, int add_cr, int expect_prompt ); int send_constant(char *the_string); int wait_for_main_or_login_prompt(int bell); int wait_for_main_prompt(int bell); int wait_for_one_character(void); int wait_for_password_prompt(int bell); int wait_for_status(int bell); int wait_for_string(char *the_string, int bell ); void wait_for_zero_status(int bell); /*--------------------------------------------------------------------------*/ int at_login_prompt(void) { if((recent_input[sizeof(recent_input)-7]=='l') && (recent_input[sizeof(recent_input)-6]=='o') && (recent_input[sizeof(recent_input)-5]=='g') && (recent_input[sizeof(recent_input)-4]=='i') && (recent_input[sizeof(recent_input)-3]=='n') && (recent_input[sizeof(recent_input)-2]==':') && (recent_input[sizeof(recent_input)-1]==' ') ) { return 1; } return 0; } /* at_login_prompt() */ /*--------------------------------------------------------------------------*/ int at_main_prompt(void) { if((recent_input[sizeof(recent_input)-8]=='w') && (recent_input[sizeof(recent_input)-7]=='e') && (recent_input[sizeof(recent_input)-6]=='q') && (recent_input[sizeof(recent_input)-5]=='u') && (recent_input[sizeof(recent_input)-4]=='d') && (recent_input[sizeof(recent_input)-3]=='o') && (recent_input[sizeof(recent_input)-2]=='n') && (recent_input[sizeof(recent_input)-1]=='a') ) { return 1; } return 0; } /* at_main_prompt() */ /*--------------------------------------------------------------------------*/ void log_pty_data(int the_character) { int jndex; char the_buffer[4]; the_buffer[0]=the_character; write(STDOUT_FILENO, the_buffer, 1 ); for(jndex=0; jndex='0') && (the_buffer[0]<='9') ) { the_sum=10*the_sum+the_buffer[0]-'0'; got_any_digits=1; } else if(got_any_digits) { wait_for_main_prompt(bell); return the_sum; } } } } /* wait_for_status() */ /*--------------------------------------------------------------------------*/ int wait_for_string(char *the_string, int bell ) { int found_it; int jndex; int kndex; int max_fd; int select_result; char the_buffer[4]; ssize_t read_result; struct timeval timeout; fd_set read_fds; for(;;) { FD_ZERO( &read_fds ); FD_SET (pty_phyle,&read_fds ); max_fd=pty_phyle; timeout.tv_sec =1; timeout.tv_usec=0; select_result=select(max_fd+1, &read_fds, NULL, NULL, &timeout ); if(select_result<1) { if(0) { the_buffer[0]='.'; write(STDOUT_FILENO, the_buffer, 1 ); } continue; /* <--------- */ } if(FD_ISSET(pty_phyle,&read_fds)) { read_result=read(pty_phyle, the_buffer, 1 ); if(read_result<0) { return -1; /* <--------- */ } if(read_result==0) { fprintf(stderr, "read(pty) returned 0\n" ); return 0; } log_pty_data(the_buffer[0]); found_it=1; jndex=sizeof(recent_input)-1; kndex=strlen(the_string)-1; for(;;) { if((jndex<0) || (kndex<0) ) { break; /* <--------- */ } if(recent_input[jndex]!= the_string [kndex] ) { found_it=0; break; /* <--------- */ } jndex--; kndex--; } if(found_it) { if(bell) { the_buffer[0]=7; write(STDOUT_FILENO, the_buffer, 1 ); } return 1; } } } } /* wait_for_string() */ /*--------------------------------------------------------------------------*/ void wait_for_zero_status(int bell) { if(wait_for_status(bell)!=0) { fprintf(stderr, "\nstatus was not zero\n" ); tcsetattr(0,TCSANOW,&tp1); exit(1); } } /* wait_for_zero_status() */ /*--------------------------------------------------------------------------*/ int main(void) { pid_t the_child; struct winsize window_size; recent_input[sizeof(recent_input)-1]=0; ioctl(STDIN_FILENO, TIOCGWINSZ, &window_size ); the_child=forkpty(&pty_phyle, NULL, NULL, &window_size ); if(the_child==0) { execl("/bin/bash", "bash", NULL ); exit(0); } tcgetattr(0,&tp1); tp2=tp1; tp2.c_iflag&=~ICRNL; tp2.c_lflag&=~ICANON; tp2.c_lflag&=~ECHO; tp2.c_cc[VMIN ]=1; tp2.c_cc[VTIME]=0; tp2.c_cc[VINTR]=0xFF; tp2.c_cc[VSUSP]=0xFF; tp2.c_cc[VQUIT]=0xFF; if(tcsetattr(0,TCSANOW,&tp2)!=0) { fprintf(stderr, "setting attributes failed\n" ); } sleep(1); send_command(". autoprompt",1,1); send_command("su evans",1,0); wait_for_password_prompt(0); send_command("XXXXXXXX",1,0); send_command(". /etc/profile",1,0); send_command("pwd",1,0); send_command("cd",1,0); send_command(". .profile",1,0); send_command(". .bashrc",1,0); send_command(". autoprompt",1,1); send_command("cd",1,1); wait_for_zero_status(0); send_command("cd minicom",1,1); wait_for_zero_status(0); send_command("pwd",1,1); wait_for_zero_status(0); /* * The following line runs pnx, a helper script which basically runs telnet * to my ISP. You'd want to substitute a simple "telnet" command. */ send_constant("pnx\r"); wait_for_one_character(); if(wait_for_main_or_login_prompt(0)<2) { tcsetattr(0,TCSANOW,&tp1); printf("\n"); return 1; } send_constant("wje\r"); wait_for_password_prompt(0); send_constant("YYYYYYYY\r"); sleep(1); send_command(". autoprompt",1,1); wait_for_zero_status(0); send_command("rm incoming 2>/dev/null",1,1); send_command("snooze mail",1,1); wait_for_zero_status(0); send_command("touch incoming",1,1); wait_for_zero_status(0); send_command("cp incoming inbox",1,1); wait_for_zero_status(0); send_command("spamn",1,2); wait_for_zero_status(0); send_command("sleep 5",1,1); wait_for_zero_status(0); send_command("touch remote-filtered",1,1); wait_for_zero_status(0); send_command("exit",1,1); wait_for_zero_status(0); send_command("cd",1,1); wait_for_zero_status(0); send_command("cd mailnews",1,1); wait_for_zero_status(0); send_command("rm remote-filtered 2> /dev/null",1,1); send_command("ftp",1,0); wait_for_string("ftp>",0); send_command("open shell.panix.com",1,0); wait_for_string("Name",0); send_command("wje",1,0); wait_for_string("assword:",0); send_command("YYYYYYYY",1,0); wait_for_string("ftp>",0); send_command("get remote-filtered",1,0); wait_for_string("ftp>",0); send_command("exit",1,1); wait_for_zero_status(0); send_command("spamn",1,2); wait_for_zero_status(0); send_command("sleep 5",1,1); wait_for_zero_status(0); send_command("touch local-filtered",1,1); wait_for_zero_status(0); send_command("cat local-filtered >> inbox",1,1); wait_for_zero_status(0); send_command("cd",1,1); wait_for_zero_status(0); send_command("cd mailnews/stalk.host",1,1); wait_for_zero_status(0); send_command("rm incoming 2>/dev/null",1,1); send_command("snooze host esk",1,1); wait_for_zero_status(0); send_command("snooze mail",1,1); wait_for_zero_status(0); send_command("touch incoming",1,1); wait_for_zero_status(0); send_command("ls -l incoming",1,1); wait_for_zero_status(0); send_command("cat incoming >> ../inbox",1,1); wait_for_zero_status(0); /* manual(); */ printf("\n"); tcsetattr(0,TCSANOW,&tp1); return 0; } /* main() */