void ReportError(BOOL bError, LPCTSTR szError, ...) { static BOOL bEventSourceAdded = FALSE; char buf[512]; const char *bufp = buf; va_list va; va_start(va,szError); vsprintf(buf,szError,va); va_end(va); if(g_bTestMode) { printf("%s%s\n",bError?"Error: ":"",buf); } else { if(!bEventSourceAdded) { char szModule[MAX_PATH]; GetModuleFileName(NULL,szModule,MAX_PATH); AddEventSource(SERVICE_NAME,szModule); bEventSourceAdded=TRUE; } HANDLE hEvent = RegisterEventSource(NULL, SERVICE_NAME); ReportEvent(hEvent,bError?EVENTLOG_ERROR_TYPE:EVENTLOG_INFORMATION_TYPE,0,MSG_STRING,NULL,1,0,&bufp,NULL); DeregisterEventSource(hEvent); } }
void install_service(const char *config) { SC_HANDLE schService; SC_HANDLE schSCManager; CHAR szPath[MAX_PATH]; AddEventSource("tdifw"); if (GetModuleFileName(NULL, szPath, sizeof(szPath)) == 0) { winerr("install_service: GetModuleFileName"); return; } schSCManager = OpenSCManager( NULL, // machine (NULL == local) NULL, // database (NULL == default) SC_MANAGER_ALL_ACCESS); // access required if (schSCManager != NULL) { schService = CreateService( schSCManager, // SCManager database "tdifw", // name of service "TDI-based open source personal firewall", // name to display SERVICE_ALL_ACCESS, // desired access SERVICE_WIN32_OWN_PROCESS, // service type SERVICE_AUTO_START, // start type SERVICE_ERROR_NORMAL, // error control type szPath, // service's binary NULL, // no load ordering group NULL, // no tag identifier NULL, // dependencies NULL, // LocalSystem account NULL); // no password if (schService != NULL) { printf("tdifw service has been installed\n"); if (!add_config_info(schService, config)) fprintf(stderr, "Can't store config info! Service will use defaults.\n"); CloseServiceHandle(schService); } else winerr("install_service: CreateService"); CloseServiceHandle(schSCManager); } else winerr("install_service: OpenSCManager"); }
// // FUNCTION: CmdInstallService() // // PURPOSE: Installs the service // // PARAMETERS: // none // // RETURN VALUE: // none // // COMMENTS: // void CmdInstallService(char *Account,char *Password) { SC_HANDLE schService; SC_HANDLE schSCManager; TCHAR szPath[512]; if ( GetModuleFileName( NULL, szPath, 512 ) == 0 ) { _tprintf(TEXT("Unable to install %s - %s\n"), TEXT(SZSERVICEDISPLAYNAME), GetLastErrorText(szErr, 256)); return; } schSCManager = OpenSCManager( NULL, // machine (NULL == local) NULL, // database (NULL == default) SC_MANAGER_ALL_ACCESS // access required ); if ( schSCManager ) { schService = CreateService( schSCManager, // SCManager database TEXT(SZSERVICENAME), // name of service TEXT(SZSERVICEDISPLAYNAME), // name to display SERVICE_ALL_ACCESS, // desired access SERVICE_WIN32_OWN_PROCESS, // service type SERVICE_DEMAND_START, // start type SERVICE_ERROR_NORMAL, // error control type szPath, // service's binary NULL, // no load ordering group NULL, // no tag identifier TEXT(SZDEPENDENCIES), // dependencies Account, // Administrator account Password); // password if ( schService ) { _tprintf(TEXT("%s installed.\n"), TEXT(SZSERVICEDISPLAYNAME) ); CloseServiceHandle(schService); AddEventSource(); AddParametersRegistry(); } else { _tprintf(TEXT("CreateService failed - %s\n"), GetLastErrorText(szErr, 256)); } CloseServiceHandle(schSCManager); } else _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256)); }
void openlog(char *ident, int logstat, int logfac){ if(ident != NULL){ LogTag = ident; LogStat = logstat; if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) LogFacility = logfac; /* Add the registry key each time openlog is called. */ AddEventSource(ident); } opened = 1; }
void ReportError(BOOL bError, LPCTSTR szError) { static BOOL bEventSourceAdded = FALSE; if(!bEventSourceAdded) { TCHAR szModule[MAX_PATH]; GetModuleFileName(g_hInstance,szModule,MAX_PATH); AddEventSource(L"CVSNT",szModule); bEventSourceAdded=TRUE; } HANDLE hEvent = RegisterEventSource(NULL, L"CVSNT"); ReportEvent(hEvent,bError?EVENTLOG_ERROR_TYPE:EVENTLOG_INFORMATION_TYPE,0,MSG_STRING,NULL,1,0,&szError,NULL); DeregisterEventSource(hEvent); }
DWORD Install(const TCHAR * full_path, const TCHAR * pName) { TCHAR pTemp[1024]; DWORD err = ERROR_SUCCESS; SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE); if (schSCManager == 0) { err = GetLastError(); _stprintf_s(pTemp, _T("OpenSCManager failed, error code = %d\n"), err); WriteLog(pTemp); return err; } SC_HANDLE schService = CreateService( schSCManager, /* SCManager database */ ServiceName, /* name of service */ ServiceName, /* service name to display */ SERVICE_ALL_ACCESS, /* desired access */ SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, /* service type */ SERVICE_AUTO_START, /* start type */ SERVICE_ERROR_NORMAL, /* error control type */ full_path, /* service's binary */ NULL, /* no load ordering group */ NULL, /* no tag identifier */ NULL, /* no dependencies */ NULL, /* LocalSystem account */ NULL ); if (schService == 0) { err = GetLastError(); _stprintf_s(pTemp, _T("Failed to create service %s, error code = %d\n"), ServiceName, err); WriteLog(pTemp); } else { CloseServiceHandle(schService); AddEventSource(L"Application", ServiceName); } CloseServiceHandle(schSCManager); return err; }
/* * openlog - open remote syslog server or event logging */ void openlog(char *ident, int logstat, int logfac){ /* * Open remote syslog server */ if(strlen(ctl.syslog_server) > 0){ /* Connect to Target server. */ if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == SOCKET_ERROR){ warnx("*** Could not create the socket to send the syslog alert. Error Number: %d.\n", WSAGetLastError()); } else { sin.sin_port = htons((u_short)ctl.syslog_port); sin.sin_family = AF_INET; if (!(sin.sin_addr.s_addr = resolve_host(ctl.syslog_server))){ warnx("*** Could not resolve syslog server's hostname. Error Number: %d.\n", WSAGetLastError()); closesocket(sockfd); } else { outp.syslog_open = TRUE; return; } } warnx("*** Remote syslog not working, will now fail over to local event log"); ctl.syslog_server[0] = '\0'; /* Null string to show are logging locally */ } /* * Open local event logging */ if (!util_check_version_winNT()) { warnx("*** Local event logging requires WinNT or better"); return; } if(ident != NULL){ strlcpy(LogTag, ident, sizeof(LogTag)); LogStat = logstat; if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) LogFacility = logfac; /* Add the registry key each time openlog is called. */ AddEventSource(LogTag); } outp.syslog_open = TRUE; }
int ServiceInstall( IN HWND hWnd, IN LPTSTR Service ) { DWORD Err = NO_ERROR; SC_HANDLE hSvc = NULL; SC_HANDLE hMgr = NULL; SERVICE_DESCRIPTION desc; SC_ACTION restartAction[3]; SERVICE_FAILURE_ACTIONS actions; SERVICE_FAILURE_ACTIONS_FLAG flag; OSVERSIONINFOEX info; ULONG WindowsVersion; info.dwOSVersionInfoSize = sizeof(info); WindowsVersion = 0; if (GetVersionEx((OSVERSIONINFO*)&info)) { if (((info.dwMajorVersion & ~0xff) == 0) && ((info.dwMinorVersion & ~0xff) == 0)) { WindowsVersion = (info.dwMajorVersion << 8) | info.dwMinorVersion; } } AddEventSource(Service); hMgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hMgr == NULL) { Err = GetLastError(); goto exit; } // // First check if xenservice is already installed // hSvc = OpenService(hMgr, SVC_NAME, SERVICE_ALL_ACCESS); if (hSvc == NULL) { // // Service does not exist, so create it. // hSvc = CreateService(hMgr, // SCManager database SVC_NAME, // name of service SVC_DISPLAYNAME, // name to display SERVICE_ALL_ACCESS, // desired access SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS, // service type SERVICE_AUTO_START, // start type SERVICE_ERROR_NORMAL, // error control type Service, // service's binary NULL, // no load ordering group NULL, // no tag identifier (((WindowsVersion >= 0x0600) || (info.wSuiteMask == 0x0300)) ?//dependencies //despite what MSDN doc says, XP Home does _not_ evaluate to VER_SUITE_PERSONAL (0x0200) "WinMgmt\0" : "Wmi\0WinMgmt\0"), NULL, // LocalSystem account NULL); // no password if (hSvc == NULL) { Err = GetLastError(); MessageBox(hWnd, "Failed to install the service.", "Install Error", MB_OK); goto exit; } StartService(hSvc, 0, NULL); } else { // // Service already exists, so just update its values. // if (!ChangeServiceConfig(hSvc, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, Service, NULL, NULL, (((WindowsVersion >= 0x0600) || (info.wSuiteMask == 0x0300)) ? "WinMgmt\0" : "Wmi\0WinMgmt\0"), NULL, NULL, SVC_DISPLAYNAME)) { Err = GetLastError(); MessageBox(hWnd, "Failed to update the service.", "Install Error", MB_OK); goto exit; } } // // In all cases change the service description. // desc.lpDescription = SVC_DESC; if (!ChangeServiceConfig2(hSvc, SERVICE_CONFIG_DESCRIPTION, &desc)) DisplayError(hWnd, TEXT("ChangeServiceConfig2(...SERVICE_CONFIG_DESCRIPTION...)")); restartAction[0].Type = SC_ACTION_RESTART; restartAction[0].Delay = 4 // minutes * 60 // s * 1000; // ms restartAction[1].Type = SC_ACTION_RESTART; restartAction[1].Delay = 8 // minutes * 60 // s * 1000; // ms restartAction[2].Type = SC_ACTION_RESTART; restartAction[2].Delay = 12 // minutes * 60 // s * 1000; // ms actions.dwResetPeriod = 3600; actions.lpRebootMsg = NULL; actions.lpCommand = NULL; actions.cActions = sizeof (restartAction) / sizeof (restartAction[0]); actions.lpsaActions = restartAction; if (!ChangeServiceConfig2(hSvc, SERVICE_CONFIG_FAILURE_ACTIONS, &actions)) DisplayError(hWnd, TEXT("ChangeServiceConfig2(...SERVICE_CONFIG_FAILURE_ACTIONS...)")); flag.fFailureActionsOnNonCrashFailures = TRUE; /* This is expected to fail on anything other than Windows 7; just ignore the error. */ ChangeServiceConfig2(hSvc, SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, &flag); exit: if (hSvc != NULL) { CloseServiceHandle(hSvc); } if (hMgr != NULL) { CloseServiceHandle(hMgr); } if (Err == NO_ERROR) { return 0; } else { return 1; } }
VOID LogEvent(WORD wEventType, DWORD dwEventID, ...) { va_list listArgs; HANDLE hEventSource; HANDLE hMutex = NULL; LPTSTR lpArgs[MAXARGS]; CHAR lpStrings[MAXARGS][STRLEN]; static CHAR lpLastStrings[MAXARGS][STRLEN]; WORD wNumArgs = 0; static WORD wLastNumArgs = MAXARGS; static time_t lastMessageTime = 0; static WORD wLastEventType = 0; static DWORD dwLastEventID = 0; time_t now; DWORD code; BOOL bLogMessage = TRUE; WORD i = 0, j; // Ensure that our event source is properly initialized. if (!AddEventSource()) return; // Get a handle to the event log. hEventSource = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME); if (hEventSource == NULL) return; // Construct the array of substitution strings. va_start(listArgs, dwEventID); switch ( dwEventID ) { case MSG_FLUSH_NO_SHARE_NAME: case MSG_FLUSH_NO_MEMORY: case MSG_FLUSH_IMPERSONATE_ERROR: case MSG_FLUSH_UNEXPECTED_EVENT: case MSG_UNHANDLED_EXCEPTION: case MSG_SMB_ZERO_TRANSACTION_COUNT: case MSG_SERVICE_INCORRECT_VERSIONS: case MSG_SERVICE_STOPPING: case MSG_SERVICE_STOPPED: case MSG_SERVICE_ERROR_STOP: case MSG_CRYPT_OFF: case MSG_CRYPT_ON: break; case MSG_SERVICE_START_PENDING: wNumArgs = 1; lpArgs[0] = AFSVersion; break; case MSG_SERVICE_RUNNING: wNumArgs = 1; if (smb_Enabled && RDR_Initialized) lpArgs[0] = "SMB and RDR interfaces"; else if (smb_Enabled) lpArgs[0] = "SMB interface"; else if (RDR_Initialized) lpArgs[0] = "RDR interface"; else lpArgs[0] = "No active interface"; break; case MSG_FLUSH_BAD_SHARE_NAME: case MSG_FLUSH_OPEN_ENUM_ERROR: case MSG_FLUSH_ENUM_ERROR: case MSG_FLUSH_FAILED: case MSG_RX_HARD_DEAD_TIME_EXCEEDED: case MSG_SERVICE_ERROR_STOP_WITH_MSG: case MSG_SMB_SEND_PACKET_FAILURE: case MSG_UNEXPECTED_SMB_SESSION_CLOSE: case MSG_RX_MSGSIZE_EXCEEDED: case MSG_RX_BUSY_CALL_CHANNEL: wNumArgs = 1; lpArgs[0] = va_arg(listArgs, LPTSTR); break; case MSG_TIME_FLUSH_PER_VOLUME: case MSG_TIME_FLUSH_TOTAL: case MSG_SMB_MAX_MPX_COUNT: case MSG_SMB_MAX_BUFFER_SIZE: wNumArgs = 2; lpArgs[0] = va_arg(listArgs, LPTSTR); lpArgs[1] = va_arg(listArgs, LPTSTR); break; case MSG_SERVER_REPORTS_VNOVOL: case MSG_SERVER_REPORTS_VMOVED: case MSG_SERVER_REPORTS_VOFFLINE: case MSG_SERVER_REPORTS_VSALVAGE: case MSG_SERVER_REPORTS_VNOSERVICE: case MSG_SERVER_REPORTS_VIO: case MSG_SERVER_REPORTS_VBUSY: case MSG_SERVER_REPORTS_VRESTARTING: wNumArgs = 3; lpArgs[0] = va_arg(listArgs, LPTSTR); StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,afs_int32)); lpArgs[1] = lpStrings[1]; lpArgs[2] = va_arg(listArgs, LPTSTR); break; case MSG_ALL_SERVERS_BUSY: case MSG_ALL_SERVERS_OFFLINE: case MSG_ALL_SERVERS_DOWN: wNumArgs = 2; lpArgs[0] = va_arg(listArgs, LPTSTR); StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,afs_int32)); lpArgs[1] = lpStrings[1]; break; case MSG_BAD_SMB_PARAM: wNumArgs = 5; lpArgs[0] = va_arg(listArgs, LPTSTR); StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,int)); StringCbPrintf(lpStrings[2],STRLEN,"%d",va_arg(listArgs,int)); StringCbPrintf(lpStrings[3],STRLEN,"%d",va_arg(listArgs,int)); StringCbPrintf(lpStrings[4],STRLEN,"%d",va_arg(listArgs,WORD)); lpArgs[1] = lpStrings[1]; lpArgs[2] = lpStrings[2]; lpArgs[3] = lpStrings[3]; lpArgs[4] = lpStrings[4]; break; case MSG_BAD_SMB_PARAM_WITH_OFFSET: wNumArgs = 6; lpArgs[0] = va_arg(listArgs, LPTSTR); StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,int)); StringCbPrintf(lpStrings[2],STRLEN,"%d",va_arg(listArgs,int)); StringCbPrintf(lpStrings[3],STRLEN,"%d",va_arg(listArgs,int)); StringCbPrintf(lpStrings[4],STRLEN,"%d",va_arg(listArgs,int)); StringCbPrintf(lpStrings[5],STRLEN,"%d",va_arg(listArgs,WORD)); lpArgs[1] = lpStrings[1]; lpArgs[2] = lpStrings[2]; lpArgs[3] = lpStrings[3]; lpArgs[4] = lpStrings[4]; lpArgs[5] = lpStrings[5]; break; case MSG_BAD_SMB_TOO_SHORT: case MSG_BAD_SMB_INVALID: case MSG_BAD_SMB_INCOMPLETE: wNumArgs = 1; StringCbPrintf(lpStrings[0],STRLEN,"%d",va_arg(listArgs,WORD)); lpArgs[0] = lpStrings[0]; break; case MSG_SMB_SESSION_START: wNumArgs = 1; StringCbPrintf(lpStrings[0],STRLEN,"%d",va_arg(listArgs,long)); lpArgs[0] = lpStrings[0]; break; case MSG_BAD_SMB_WRONG_SESSION: wNumArgs = 2; StringCbPrintf(lpStrings[0],STRLEN,"%d",va_arg(listArgs,DWORD)); StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,WORD)); lpArgs[0] = lpStrings[0]; lpArgs[1] = lpStrings[1]; break; case MSG_BAD_VCP: wNumArgs = 4; StringCbPrintf(lpStrings[0],STRLEN,"%d",va_arg(listArgs,UCHAR)); StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,UCHAR)); StringCbPrintf(lpStrings[2],STRLEN,"%d",va_arg(listArgs,UCHAR)); StringCbPrintf(lpStrings[3],STRLEN,"%d",va_arg(listArgs,UCHAR)); lpArgs[0] = lpStrings[0]; lpArgs[1] = lpStrings[1]; lpArgs[2] = lpStrings[2]; lpArgs[3] = lpStrings[3]; break; case MSG_SERVICE_ERROR_STOP_WITH_MSG_AND_LOCATION: wNumArgs = 3; lpArgs[0] = va_arg(listArgs, LPTSTR); StringCbPrintf(lpStrings[1],STRLEN,"%d",va_arg(listArgs,int)); lpArgs[1] = lpStrings[1]; lpArgs[2] = va_arg(listArgs,LPTSTR); break; case MSG_DIRTY_BUFFER_AT_SHUTDOWN: wNumArgs = 6; lpArgs[0] = va_arg(listArgs, LPTSTR); lpArgs[1] = va_arg(listArgs, LPTSTR); StringCbPrintf(lpStrings[2],STRLEN,"%u",va_arg(listArgs,int)); StringCbPrintf(lpStrings[3],STRLEN,"%u",va_arg(listArgs,int)); StringCbPrintf(lpStrings[4],STRLEN,"%I64u",va_arg(listArgs,afs_int64)); StringCbPrintf(lpStrings[5],STRLEN,"%I64u",va_arg(listArgs,afs_int64)); lpArgs[2] = lpStrings[2]; lpArgs[3] = lpStrings[3]; lpArgs[4] = lpStrings[4]; lpArgs[5] = lpStrings[5]; break; } va_end(listArgs); // Make sure we were not given too many args. if (wNumArgs >= MAXARGS) goto done; hMutex = CreateMutex( NULL, TRUE, "AFSD Event Log Mutex"); if (hMutex == NULL) goto done; if (GetLastError() == ERROR_ALREADY_EXISTS) { code = WaitForSingleObject( hMutex, 500); if (code != WAIT_OBJECT_0) goto done; } /* * We rate limit consecutive duplicate messages to one every * five seconds. */ now = time(NULL); if (now < lastMessageTime + 5 && wEventType == wLastEventType && dwEventID == dwLastEventID && wNumArgs == wLastNumArgs) { for (i=0; i<wNumArgs; i++) { if ( strncmp(lpArgs[i], lpLastStrings[i], STRLEN)) break; } if (i == wNumArgs) bLogMessage = FALSE; } if ( bLogMessage) { wLastNumArgs = wNumArgs; wLastEventType = wEventType; dwLastEventID = dwEventID; lastMessageTime = now; for ( j = (i == wNumArgs ? 0 : i) ; i < wNumArgs; i++) { StringCbCopyEx( lpLastStrings[i], STRLEN, lpArgs[i], NULL, NULL, STRSAFE_NULL_ON_FAILURE); } } ReleaseMutex(hMutex); // Log the event. if ( bLogMessage) code = ReportEvent(hEventSource, // handle of event source wEventType, // event type 0, // event category dwEventID, // event ID NULL, // current user's SID wNumArgs, // strings in lpszArgs 0, // no bytes of raw data wNumArgs ? lpArgs : NULL,// array of error strings NULL); // no raw data done: if (hMutex) CloseHandle(hMutex); DeregisterEventSource(hEventSource); }