#define Version "0.50" /* simple server ver.0.50 last modified 3-May-1997 by Y.Takeuchi 07-Dec-2000 Is spec. of recv(2) changed? 05-Mar-2000 remedy for reference of non-listed hostname usage: -p PortNumber -f LogFile : if you omit this option , log to stdout. -a HostsFile : if you omit this , no host is denyed. Maximum number of hosts is defined as HostsMAX below. */ #include #include #include #include #include #include #include #include #include #include #include typedef unsigned char uchar; char* timestr(void); int secure(char*); char* adrstr(struct sockaddr*); int catch(void); typedef void (*sighandler_t)(int); int auth(int , char** , char* ); #define USAGE "usage: -p PortNumber [-f LogFile] [-a HostsFile] execfile\n" jmp_buf env; FILE* logf; #define HostsMAX 30 /* ---------------------------------------------------------------- MAIN ------------------------------------------------------------------ */ int main( int argc , char** argv ) { int fd ,so; int salen; struct sockaddr_in sa; int count = 0 , len , i , sum; char buf[256], cmd[256]; FILE* pipe; int shut_by_client = 0; int PortNumber = -1; char* LogFile = NULL , * ExecFile = NULL , * HostsFile = NULL; int HostsInx = 0; char* Hosts[HostsMAX] , foreign[256]; FILE* hf; int c; extern char *optarg; extern int optind; char msg1[] = "Sorry, Your host is NOT authorized.\n.\n"; /* --- option analysis --- */ while((c=getopt(argc,argv,"p:f:a:")) !=EOF) switch(c) { case'p': PortNumber = atoi( optarg ); break; case'f': LogFile = optarg; break; case'a': HostsFile = optarg; break; case'?': default: fprintf(stderr,USAGE); exit(2); } ExecFile = argv[optind]; /* -- port option -- */ if ( PortNumber <= 0 || ExecFile==NULL ) { fprintf(stderr,USAGE); exit(2); } /* -- logfile option -- */ if (LogFile == NULL ) { LogFile = "stdout"; logf = stdout; } else { logf = fopen(LogFile , "w" ); if (logf == NULL ) { fprintf( stdout , "Can't open %s\n" , LogFile); exit(-1); } } /* -- hostsfile option -- */ if (HostsFile != NULL) { hf = fopen(HostsFile , "r" ); if (hf == NULL ) { fprintf( stdout , "Can't open %s\n" , HostsFile); exit(-1); } HostsInx = 0; while(1) { if (fgets( buf , 256 , hf) == NULL ) break; if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0; Hosts[HostsInx++] = strdup(buf); } } /* -- server startup log --*/ fprintf(logf , "Simple server Ver.%s\n" , Version); fprintf(logf , "Port: %d\n", PortNumber ); fprintf(logf , "Log file: %s\n", LogFile ); fprintf(logf , "Exec file: %s\n", ExecFile ); if ( HostsFile != NULL ) { fprintf(logf , "permitted Hosts:\n" ); for( i = 0 ; i' : case '&' : case ';' : case '!' : case 0xff : case '|' : case '`' : fprintf( logf, "\n" , buf[i] ); fflush( logf); buf[i] = ' '; break; default : } i++; } } /* ---------------------------------------------------------------- timestr ------------------------------------------------------------------ */ char* timestr(void) { static char tbuf[80]; time_t clock; (void) time(&clock); strftime(tbuf , 80 , "%h %d %T", localtime(&clock)); return tbuf; } /* ---------------------------------------------------------------- adrstr ------------------------------------------------------------------ */ char* adrstr(struct sockaddr* sa) { static char buf[80]; unsigned long uaddr; struct hostent * hp; sprintf(buf , "%d.%d.%d.%d" , (uchar)sa->sa_data[2],(uchar)sa->sa_data[3], (uchar)sa->sa_data[4],(uchar)sa->sa_data[5]); uaddr = inet_addr( buf ); if ( uaddr != -1 ) { hp = gethostbyaddr(( char *) &uaddr ,4, AF_INET); if (hp != NULL) strcpy(buf , hp->h_name); } return buf; } /* ---------------------------------------------------------------- catch ------------------------------------------------------------------ */ int catch(void) { fprintf( logf , "%s Broken pipe... Server restart " , timestr()); fflush(logf); signal( SIGPIPE , (sighandler_t)catch ); longjmp(env,3); return( 0 ); }