#include #ifdef NT #include #include #define strdup _strdup #define ltoa _ltoa #endif #define CONFIG_FILE "iksd.cfg" #define bzero(x,y) memset(x,0,y) #define BSDSELECT #define MAXPORTS 32 struct PORT { short id ; int lsocket ; int asocket ; char * k95cmd ; int showcmd; } ports[MAXPORTS] ; int portcount = 0 ; int ParseCmdLine( int argc, char * argv[] ) { int len = 0, i ; char * p = NULL ; static struct servent *service, servrec; int arg = 1; /* Set up some defaults */ ports[portcount].showcmd = SW_SHOWNORMAL; service = getservbyname("kermit", "tcp"); /* Allocate the max needed memory */ for ( i = 1 ; i < argc ; i++ ) len += strlen( argv[i] ) + 1 ; ports[portcount].k95cmd = (char *) malloc( len+1 ) ; if ( !ports[portcount].k95cmd ) { fprintf( stderr, "memory allocation error\n" ) ; exit(1) ; } ports[portcount].k95cmd[0] = '\0' ; /* Process each command line option */ for ( i=1 ; is_port = htons((unsigned short)atoi(p)); } else { /* Otherwise lookup the service name */ service = getservbyname(p, "tcp"); } continue; } } if ( ports[portcount].k95cmd[0] ) strcat( ports[portcount].k95cmd, " "); strcat( ports[portcount].k95cmd, argv[i] ); } if ( service ) ports[portcount].id = service->s_port; else ports[portcount].id = htons( 1649 ); portcount++ ; return portcount ; } HANDLE StartKermit( int socket, char * cmdline, int ShowCmd ) { #ifdef NT PROCESS_INFORMATION StartKermitProcessInfo ; OSVERSIONINFO osverinfo ; STARTUPINFO si ; HANDLE sockdup = INVALID_HANDLE_VALUE ; static char buf[512] ; osverinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO) ; GetVersionEx( &osverinfo ) ; memset( &si, 0, sizeof(STARTUPINFO) ) ; si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = ShowCmd; if (!DuplicateHandle( GetCurrentProcess(), (HANDLE) socket, GetCurrentProcess(), &sockdup, DUPLICATE_SAME_ACCESS, TRUE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS )) { closesocket( (int) socket ) ; fprintf( stderr, "\nINTERNET KERMIT SERVICE DAEMON FATAL ERROR:\n" ); fprintf( stderr, " You are using a WinSOCK which does not allow socket handles\n"); fprintf( stderr, " to be duplicated or shared with child processes.\n\n"); fprintf( stderr, " If you are attempting to start Kermit 95 Host Mode,\n"); fprintf( stderr, " please change SESSIONS to 1 in your configuration.\n"); fprintf( stderr, "\n Press ENTER to exit...\n"); fscanf( stdin, "" ) ; exit( 2 ) ; } #ifdef DEBUG strcpy( buf, "msdev.exe cknker.exe -# 132 -A " ) ; ltoa( (LONG) sockdup, buf+strlen(buf), 10 ) ; strcat( buf, " " ); strcat( buf, cmdline ) ; printf("Executing: %s\n",buf ) ; if (CreateProcess( (LPSTR)NULL, /* start K-95 */ (LPSTR)buf, /* give it the file */ (LPSECURITY_ATTRIBUTES)NULL, (LPSECURITY_ATTRIBUTES)NULL, TRUE, /* fix if necessary */ (DWORD) CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP, (LPVOID)NULL, /* fix if necessary */ (LPSTR)NULL, /* Current directory */ &si, /* Startup info, fix */ &StartKermitProcessInfo /* Process info */ )) { closesocket( (int) sockdup ) ; return (StartKermitProcessInfo.hProcess); } #endif /* DEBUG */ #ifdef DEBUG strcpy( buf, "msdev.exe iksdnt.exe -# 132 -A " ) ; #else strcpy( buf, "iksdnt.exe -# 132 -A " ) ; #endif /* DEBUG */ ltoa( (LONG) sockdup, buf+strlen(buf), 10 ) ; strcat( buf, " " ); strcat( buf, cmdline ) ; printf("Executing: %s\n",buf ) ; if (CreateProcess( (LPSTR)NULL, /* start K-95 */ (LPSTR)buf, /* give it the file */ (LPSECURITY_ATTRIBUTES)NULL, (LPSECURITY_ATTRIBUTES)NULL, TRUE, /* fix if necessary */ (DWORD) CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP, (LPVOID)NULL, /* fix if necessary */ (LPSTR)NULL, /* Current directory */ &si, /* Startup info, fix */ &StartKermitProcessInfo /* Process info */ )) { closesocket( (int) sockdup ) ; return (StartKermitProcessInfo.hProcess); } #ifdef DEBUG strcpy( buf, "msdev.exe k95.exe -# 132 -A " ) ; #else strcpy( buf, "k95.exe -# 132 -A " ) ; #endif /* DEBUG */ ltoa( (LONG) sockdup, buf+strlen(buf), 10 ) ; strcat( buf, " " ); strcat( buf, cmdline ) ; printf("Executing: %s\n",buf ) ; if (CreateProcess( (LPSTR)NULL, /* start K-95 */ (LPSTR)buf, /* give it the file */ (LPSECURITY_ATTRIBUTES)NULL, (LPSECURITY_ATTRIBUTES)NULL, TRUE, /* fix if necessary */ (DWORD) CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP, (LPVOID)NULL, /* fix if necessary */ (LPSTR)NULL, /* Current directory */ &si, /* Startup info, fix */ &StartKermitProcessInfo /* Process info */ )) { closesocket( (int) sockdup ) ; return (StartKermitProcessInfo.hProcess); } printf("CreateProcess() failed gle=%ul\n",GetLastError()); closesocket( (int) sockdup ) ; return(FALSE); #else /* NT */ Not built for OS/2 yet. #endif /* NT */ } int main( int argc, char * argv[] ) { char *p=NULL, * dbdir=NULL, dbfile[256]; int i, x; int on = 1, rc = 0; int ready_to_accept = 0 ; static struct servent *service, servrec; static struct hostent *host; static struct sockaddr_in saddr; static int saddrlen ; #ifdef BSDSELECT fd_set rfds; struct timeval tv; #endif /* BSDSELECT */ int tcpsrv_fd = -1, ttyfd = -1 ; #ifdef NT WSADATA data ; OSVERSIONINFO osverinfo ; HANDLE hProcess; printf("Internet Kermit Service Daemon\n"); rc = WSAStartup( MAKEWORD( 2, 0 ), &data ) ; if ( rc == WSAVERNOTSUPPORTED ) { WSACleanup() ; rc = WSAStartup( MAKEWORD( 1, 1 ), &data ) ; } #else /* NT */ #endif /* NT */ ParseCmdLine( argc, argv ); if ( !portcount ) { #ifdef DEBUG ports[portcount].showcmd = SW_NORMAL; #else ports[portcount].showcmd = SW_SHOWMINNOACTIVE; #endif ports[portcount].k95cmd = (char *) malloc( 1 ) ; ports[portcount].k95cmd[0] = '\0' ; ports[portcount].id = htons( 1649 ) ; portcount++; } #ifdef NT osverinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO) ; GetVersionEx( &osverinfo ) ; if (osverinfo.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) { dbdir = getenv("SystemRoot"); } else { dbdir = getenv("winbootdir"); if (!dbdir) dbdir = getenv("windir"); } if (!dbdir) dbdir = "C:/"; #else /* NT */ dbdir = "C:/"; #endif /* NT */ sprintf(dbfile,"%s\\iksd.lck",dbdir); unlink(dbfile); sprintf(dbfile,"%s\\iksd.db",dbdir); unlink(dbfile); for ( i=0; i 0) { for ( i=0; ih_name,ntohs(ports[i].id)) ; } /* Now start subprocess */ if ( ports[i].asocket,ports[i].k95cmd ) printf("Starting IKSD with socket %d and command %s\n",ports[i].asocket,ports[i].k95cmd); else printf("Starting IKSD with socket %d\n"); hProcess = StartKermit(ports[i].asocket,ports[i].k95cmd, ports[i].showcmd) ; if ( hProcess != INVALID_HANDLE_VALUE ) CloseHandle(hProcess); closesocket(ports[i].asocket); continue; /* Go get the next one */ } else { i = errno; /* save error code */ closesocket(ports[i].lsocket) ; ports[i].lsocket = -1; ports[i].asocket = -1; errno = i; /* and report this error */ fprintf(stderr,"tcpsrv_open accept errno","",errno); exit(7); } } #ifdef NT WSACleanup() ; #else #endif /* NT */ } .