char* getpass(const char *prompt) { static char pwd_buf[128]; size_t i; DWORD br; HANDLE hInput; DWORD dwMode; if(isWinNT()) { return("admin"); // Default password } hInput=GetStdHandle(STD_INPUT_HANDLE); fputs(prompt, stderr); fflush(stderr); fflush(stdout); FlushConsoleInputBuffer(hInput); GetConsoleMode(hInput,&dwMode); SetConsoleMode(hInput, ENABLE_PROCESSED_INPUT); for(i = 0; i < sizeof (pwd_buf) - 1; ++i) { ReadFile(GetStdHandle(STD_INPUT_HANDLE),pwd_buf+i,1,&br,NULL); if (pwd_buf[i] == '\r') break; fputc('*',stdout); fflush (stderr); fflush (stdout); } SetConsoleMode(hInput,dwMode); pwd_buf[i] = '\0'; fputs ("\n", stderr); return pwd_buf; }
VOID AddToMessageLog(LPTSTR lpszMsg) { HANDLE hEventSource; TCHAR szMsg[4096]; #ifdef UNICODE LPCWSTR lpszStrings[1]; #else LPCSTR lpszStrings[1]; #endif if(!isWinNT()) { char *msg = (char*)lpszMsg; printf("%s", msg); if(msg[strlen(msg)-1] != '\n') printf("\n"); return; } if (!bConsole) { hEventSource = RegisterEventSource(NULL, TEXT(SZSERVICENAME)); _stprintf(szMsg, TEXT("%s: %s"), SZSERVICENAME, lpszMsg); lpszStrings[0] = szMsg; if (hEventSource != NULL) { ReportEvent(hEventSource, EVENTLOG_INFORMATION_TYPE, 0, EVENT_GENERIC_INFORMATION, NULL, 1, 0, lpszStrings, NULL); DeregisterEventSource(hEventSource); } } else { _tprintf(TEXT("%s\n"), lpszMsg); } }
void main(int argc, char **argv) { // The StartServiceCtrlDispatcher requires this table to specify // the ServiceMain function to run in the calling process. The first // member in this example is actually ignored, since we will install // our service as a SERVICE_WIN32_OWN_PROCESS service type. The NULL // members of the last entry are necessary to indicate the end of // the table; SERVICE_TABLE_ENTRY serviceTable[] = { { TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)serviceMain }, { NULL, NULL } }; TCHAR szAppParameters[8192]; if(!isWinNT()) { convertArgListToArgString((LPTSTR) szAppParameters,0, argc, argv); if(NULL == szAppParameters){ _tprintf(TEXT("Could not create AppParameters string.\n")); } invokeNtop(szAppParameters); return; } isNtopAservice = 0; // This app may be started with one of three arguments, /i, /r, and // /c, or /?, followed by actual program arguments. These arguments // indicate if the program is to be installed, removed, run as a // console application, or to display a usage message. if(argc > 1){ if(!stricmp(argv[1],"/i")){ installService(argc,argv); printf("NOTE: the default password for the 'admin' user has been set to 'admin'."); } else if(!stricmp(argv[1],"/r")){ removeService(); } else if(!stricmp(argv[1],"/c")){ bConsole = TRUE; runService(argc, argv); } else { printf("\nUnrecognized option: %s\n", argv[1]); printf("Available options:\n"); printf("/i [ntopng options] - Install ntopng as service\n"); printf("/c - Run ntopng on a console\n"); printf("/r - Deinstall the service\n"); printf("/h - Prints this help\n\n"); usage(); } exit(0); } // If main is called without any arguments, it will probably be by the // service control manager, in which case StartServiceCtrlDispatcher // must be called here. A message will be printed just in case this // happens from the console. printf("\nNOTE:\nUnder your version of Windows, ntopng is started as a service.\n"); printf("Please open the services control panel to start/stop ntop,\n"); printf("or type ntop /h to see all the available options.\n"); isNtopAservice = 1; if(!StartServiceCtrlDispatcher(serviceTable)) { printf("\n%s\n", SZFAILURE); AddToMessageLog(TEXT(SZFAILURE)); } }
char* printAvailableInterfaces(char *name_or_index) { char ebuf[PCAP_ERRBUF_SIZE]; char *tmpDev = pcap_lookupdev(ebuf), *ifName; int ifIdx=0, defaultIdx = -1, numInterfaces = 0; uint i, list_devices; char intNames[32][256], intDescr[32][256]; int index; if(tmpDev == NULL) { traceEvent(TRACE_INFO, "Unable to locate default interface (%s)", ebuf); exit(-1); } ifName = tmpDev; if((name_or_index != NULL) && (atoi(name_or_index) == -1)) list_devices = 1; else list_devices = 0; if(list_devices) printf("\n\nAvailable interfaces:\n"); if(!isWinNT()) { for(i=0;; i++) { if(tmpDev[i] == 0) { if(ifName[0] == '\0') break; else { if(list_devices) { numInterfaces++; printf("\t[index=%d] '%s'\n", ifIdx, ifName); } if(ifIdx < 32) { strcpy(intNames[ifIdx], ifName); strcpy(intDescr[ifIdx], ifName); if(list_devices) { if(strncmp(intNames[ifIdx], "PPP", 3) /* Avoid to use the PPP interface */ && strncmp(intNames[ifIdx], "ICSHARE", 6) && (!strcasestr(intNames[ifIdx], "dialup")) ) { /* Avoid to use the internet sharing interface */ defaultIdx = ifIdx; } } } ifIdx++; ifName = &tmpDev[i+1]; } } } tmpDev = intNames[defaultIdx]; } else { /* WinNT/2K */ static char tmpString[128]; int j,ifDescrPos = 0; uint i; unsigned short *ifName; /* UNICODE */ char *ifDescr; ifName = (unsigned short *)tmpDev; while(*(ifName+ifDescrPos) || *(ifName+ifDescrPos-1)) ifDescrPos++; ifDescrPos++; /* Step over the extra '\0' */ ifDescr = (char*)(ifName + ifDescrPos); /* cast *after* addition */ while(tmpDev[0] != '\0') { u_char skipInterface; for(j=0, i=0; !((tmpDev[i] == 0) && (tmpDev[i+1] == 0)); i++) { if(tmpDev[i] != 0) tmpString[j++] = tmpDev[i]; } tmpString[j++] = 0; if(strstr(ifDescr, "NdisWan") || strstr(ifDescr, "dialup")) skipInterface = 1; else skipInterface = 0; if(list_devices) { if(!skipInterface) { printf("\t[index=%d] '%s'\n", ifIdx, ifDescr); numInterfaces++; } } tmpDev = &tmpDev[i+3]; if(!skipInterface) { strcpy(intNames[ifIdx], tmpString); strcpy(intDescr[ifIdx], ifDescr); if(defaultIdx == -1) defaultIdx = ifIdx; ifIdx++; } ifDescr += strlen(ifDescr)+1; } if(!list_devices) tmpDev = intNames[defaultIdx]; /* Default */ } if(list_devices) { if(numInterfaces == 0) { traceEvent(TRACE_WARNING, "no interfaces available! This application cannot"); traceEvent(TRACE_WARNING, "work make sure that winpcap is installed properly"); traceEvent(TRACE_WARNING, "and that you have network interfaces installed."); } return(NULL); } /* Return the first available device */ if(name_or_index == NULL) return(strdup(intNames[defaultIdx])); /* Search the interface by name */ for(i=0; i<ifIdx; i++) { if(strcasestr(intDescr[i], name_or_index) != NULL) { return(strdup(intNames[i])); } } index = atoi(name_or_index); if((index < 0) || (index >= ifIdx)) { traceEvent(TRACE_ERROR, "Interface index %d out of range\n", index); exit(-1); } else return(strdup(intNames[index])); }
void main(int argc, char **argv) { // The StartServiceCtrlDispatcher requires this table to specify // the ServiceMain function to run in the calling process. The first // member in this example is actually ignored, since we will install // our service as a SERVICE_WIN32_OWN_PROCESS service type. The NULL // members of the last entry are necessary to indicate the end of // the table; SERVICE_TABLE_ENTRY serviceTable[] = { { TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)serviceMain }, { NULL, NULL } }; TCHAR szAppParameters[8192]; // pthread_win32_process_attach_np(); if(!isWinNT()) { convertArgListToArgString((LPTSTR) szAppParameters,0, argc, argv); if(NULL == szAppParameters){ _tprintf(TEXT("Could not create AppParameters string.\n")); } invokelprobe(szAppParameters); return; } thisIsAservice = 0; // This app may be started with one of three arguments, /i, /r, and // /c, or /?, followed by actual program arguments. These arguments // indicate if the program is to be installed, removed, run as a // console application, or to display a usage message. if(argc > 1){ char *service_name = "lprobe for Win32"; if(!stricmp(argv[1],"/i")){ if(argc >2) installService(argv[2], argc, argv); else _tprintf(TEXT("/i requires the service name as parameter\n")); } else if(!stricmp(argv[1],"/r")){ if(argc >1) removeService(argv[2]); else _tprintf(TEXT("/r requires the service name as parameter\n")); } else if(!stricmp(argv[1],"/c")){ bConsole = TRUE; runService(argc,argv); } else{ if(stricmp(argv[1],"/h")) printf("\nUnrecognized option: %s\n", argv[1]); printf("Available options:\n"); printf("/i <service name> [lprobe options] - Install lprobe as service\n"); printf("/c [lprobe options] - Run lprobe on a console\n"); printf("/r <service name> - Deinstall the service\n\n"); printf("Example:\n" "Install lprobe as a service: 'lprobe /i my_lprobe -i 0 -n 192.168.0.1:2055'\n" "Remove the lprobe service: 'lprobe /r my_lprobe'\n\n"); printf("Notes:\n" "1. Type 'lprobe /c -h' to see all options\n" "1. In order to reinstall a service with new options\n" " it is necessary to first remove the service, then add it\n" " again with the new options.\n" "2. Services are started/stopped using the Services\n" " control panel item.\n" "3. You can install the lprobe service multiple times\n" " as long as you use different service names.\n\n"); } exit(0); } thisIsAservice = 1; // If main is called without any arguments, it will probably be by the // service control manager, in which case StartServiceCtrlDispatcher // must be called here. A message will be printed just in case this // happens from the console. printf("\nNOTE:\nUnder your version of Windows, lprobe is started as a service.\n"); printf("Please open the services control panel to start/stop lprobe,\n"); printf("or type lprobe /h to see all the available options.\n"); if(!StartServiceCtrlDispatcher(serviceTable)) { printf("\n%s\n", SZFAILURE); AddToMessageLog(TEXT(SZFAILURE)); } }
static KWBoolean client( const time_t exitTime, const char *hotUser, const BPS hotBPS, const int hotHandle, const KWBoolean runUUXQT ) { CONN_STATE s_state = CONN_INITIALIZE; CONN_STATE old_state = CONN_EXIT; KWBoolean contacted = KWFalse; KWBoolean needUUXQT = KWFalse; char sendGrade = ALL_GRADES; /*--------------------------------------------------------------------*/ /* Trap missing modem entry */ /*--------------------------------------------------------------------*/ if ( E_inmodem == NULL ) { printmsg(0,"No modem specified in configuration file or command line"); panic(); } if (!getmodem(E_inmodem)) /* Initialize modem configuration */ panic(); /* Avoid loop if bad modem name */ if ( ! IsNetwork() && ( hotUser == NULL ) && (hotHandle == -1 ) && ! suspend_init(M_device)) { #ifdef WIN32 if (!isWinNT()) { printmsg(0,"Unable to set up pipe for suspending; " "may be unsupported in this environment (use NT for modem sharing)."); } else #endif /* else under WIN32, otherwise unconditional */ { printmsg(0,"Unable to set up pipe for suspending; " "is another UUCICO running?" ); panic(); } } while (s_state != CONN_EXIT ) { printmsg(s_state == old_state ? 10 : 4 , "S state = %c", s_state); old_state = s_state; switch (s_state) { case CONN_INITIALIZE: if (( hotUser == NULL ) && (hotHandle == -1 )) s_state = CONN_ANSWER; else s_state = CONN_HOTMODEM; break; case CONN_WAIT: #if !defined(__TURBOC__) || defined(BIT32ENV) setTitle("Port %s suspended", M_device); s_state = suspend_wait(); #else panic(); /* Why are we here?! */ #endif break; case CONN_ANSWER: setTitle("Monitoring port %s", M_device); s_state = callin( exitTime ); break; case CONN_HOTMODEM: s_state = callhot( hotBPS, hotHandle ); break; case CONN_HOTLOGIN: if ( hotUser == NULL ) /* User specified to login? */ s_state = CONN_LOGIN; /* No --> Process normally */ else if ( loginbypass( hotUser ) ) s_state = CONN_INITSTAT; else s_state = CONN_DROPLINE; break; case CONN_LOGIN: setTitle("Processing login on %s", M_device ); if ( login( ) ) s_state = CONN_INITSTAT; else s_state = CONN_DROPLINE; break; case CONN_INITSTAT: HostStatus(); s_state = CONN_PROTOCOL; break; case CONN_PROTOCOL: setTitle("Establishing connection on %s", M_device); s_state = startup_client(&sendGrade); break; case CONN_CLIENT: contacted = KWTrue; if (bflag[F_MULTITASK]) dcupdate(); setTitle("%s connected to %s", securep->myname, hostp->via, M_device); s_state = process( POLL_PASSIVE, sendGrade ); break; case CONN_TERMINATE: s_state = sysend(); if ( hostp != NULL ) { dcstats(); needUUXQT = KWTrue; } break; case CONN_DROPLINE: shutDown(); if ( locked ) /* Cause could get here w/o locking */ UnlockSystem(); s_state = CONN_CLEANUP; break; case CONN_CLEANUP: if ( runUUXQT && needUUXQT ) s_state = CONN_UUXQT; else s_state = CONN_EXIT; break; case CONN_UUXQT: { char buf[100]; sprintf( buf, "-s %s -x %d", rmtname, debuglevel ); copylog(); execute( "uuxqt", buf, NULL, NULL, KWFalse, KWFalse ); openlog(NULL); } needUUXQT = KWFalse; s_state = CONN_EXIT; break; case CONN_EXIT: break; default: printmsg(0,"dcpmain: Unknown slave state = %c",s_state ); panic(); break; } /* switch */ if ( terminate_processing ) s_state = CONN_EXIT; } /* while */ return contacted; } /* client */
char *cross_platform_cwd(char **dir) { #define MAX_CWD 4096 #ifdef _WIN32 #define BUFF_INC 128 unsigned int buff_size=128; #else #define BUFF_INC 128*UTF_8_SIZE unsigned int buff_size=128*UTF_8_SIZE; #endif #ifndef _WIN32 *dir=safe_malloc(buff_size); while (!(getcwd(*dir,buff_size))) { buff_size+=BUFF_INC; free(*dir); if (buff_size>MAX_CWD) { *dir=NULL; return NULL; } *dir=safe_malloc(buff_size); } #else if (!(isWinNT())) #ifndef UNICOWS { *dir=safe_malloc(buff_size+1); while (!(_getcwd(*dir,buff_size))) { buff_size+=BUFF_INC; free(*dir); if (buff_size>MAX_CWD) { *dir=NULL; return NULL; } *dir=safe_malloc(buff_size+1); } _strlwr(*dir); } #else ; #endif #ifndef UNICOWS else #endif {