1 /* 2 * services.h 3 */ 4 5 #ifndef _SERVICES_H 6 #define _SERVICES_H 7 8 #include <stdio.h> 9 #include <stdlib.h> 10 11 #define WIN32_NO_STATUS 12 #define _INC_WINDOWS 13 #define COM_NO_WINDOWS_H 14 15 #include <windef.h> 16 #include <winbase.h> 17 #include <winsvc.h> 18 #include <winreg.h> 19 #include <winuser.h> 20 #include <netevent.h> 21 #define NTOS_MODE_USER 22 #include <ndk/obfuncs.h> 23 #include <ndk/rtlfuncs.h> 24 #include <services/services.h> 25 #include <svcctl_s.h> 26 27 #include "resource.h" 28 29 typedef struct _SERVICE_GROUP 30 { 31 LIST_ENTRY GroupListEntry; 32 LPWSTR lpGroupName; 33 34 DWORD dwRefCount; 35 BOOLEAN ServicesRunning; 36 ULONG TagCount; 37 PULONG TagArray; 38 39 WCHAR szGroupName[1]; 40 } SERVICE_GROUP, *PSERVICE_GROUP; 41 42 43 typedef struct _SERVICE_IMAGE 44 { 45 LIST_ENTRY ImageListEntry; 46 LPWSTR pszImagePath; 47 LPWSTR pszAccountName; 48 DWORD dwImageRunCount; 49 50 HANDLE hControlPipe; 51 HANDLE hProcess; 52 DWORD dwProcessId; 53 HANDLE hToken; 54 HANDLE hProfile; 55 } SERVICE_IMAGE, *PSERVICE_IMAGE; 56 57 58 typedef struct _SERVICE 59 { 60 LIST_ENTRY ServiceListEntry; 61 LPWSTR lpServiceName; 62 LPWSTR lpDisplayName; 63 PSERVICE_GROUP lpGroup; 64 PSERVICE_IMAGE lpImage; 65 BOOL bDeleted; 66 DWORD dwResumeCount; 67 DWORD dwRefCount; 68 69 SERVICE_STATUS Status; 70 DWORD dwStartType; 71 DWORD dwErrorControl; 72 DWORD dwTag; 73 74 DWORD dwServiceBits; 75 76 ULONG Flags; 77 78 PSECURITY_DESCRIPTOR pSecurityDescriptor; 79 80 BOOLEAN ServiceVisited; 81 82 WCHAR szServiceName[1]; 83 } SERVICE, *PSERVICE; 84 85 86 #define LOCK_TAG 0x4C697041 /* 'ApiL' */ 87 88 typedef struct _START_LOCK 89 { 90 DWORD Tag; /* Must be LOCK_TAG */ 91 DWORD TimeWhenLocked; /* Number of seconds since 1970 */ 92 PSID LockOwnerSid; /* It is NULL if the SCM acquired the lock */ 93 } START_LOCK, *PSTART_LOCK; 94 95 96 /* VARIABLES ***************************************************************/ 97 98 extern LIST_ENTRY ServiceListHead; 99 extern LIST_ENTRY GroupListHead; 100 extern LIST_ENTRY ImageListHead; 101 extern BOOL ScmInitialize; 102 extern BOOL ScmShutdown; 103 104 105 /* FUNCTIONS ***************************************************************/ 106 107 /* config.c */ 108 109 DWORD ScmOpenServiceKey(LPWSTR lpServiceName, 110 REGSAM samDesired, 111 PHKEY phKey); 112 113 DWORD ScmCreateServiceKey(LPCWSTR lpServiceName, 114 REGSAM samDesired, 115 PHKEY phKey); 116 117 DWORD ScmWriteDependencies(HKEY hServiceKey, 118 LPCWSTR lpDependencies, 119 DWORD dwDependenciesLength); 120 121 DWORD ScmMarkServiceForDelete(PSERVICE pService); 122 BOOL ScmIsDeleteFlagSet(HKEY hServiceKey); 123 124 DWORD ScmReadString(HKEY hServiceKey, 125 LPCWSTR lpValueName, 126 LPWSTR *lpValue); 127 128 DWORD 129 ScmReadDependencies(HKEY hServiceKey, 130 LPWSTR *lpDependencies, 131 DWORD *lpdwDependenciesLength); 132 133 DWORD 134 ScmSetServicePassword( 135 IN PCWSTR pszServiceName, 136 IN PCWSTR pszPassword); 137 138 DWORD 139 ScmWriteSecurityDescriptor( 140 _In_ HKEY hServiceKey, 141 _In_ PSECURITY_DESCRIPTOR pSecurityDescriptor); 142 143 DWORD 144 ScmReadSecurityDescriptor( 145 _In_ HKEY hServiceKey, 146 _Out_ PSECURITY_DESCRIPTOR *ppSecurityDescriptor); 147 148 DWORD 149 ScmDeleteRegKey( 150 _In_ HKEY hKey, 151 _In_ PCWSTR pszSubKey); 152 153 DWORD 154 ScmDecryptPassword( 155 _In_ PBYTE pPassword, 156 _In_ DWORD dwPasswordSize, 157 _Out_ PWSTR *pDecryptedPassword); 158 159 160 /* controlset.c */ 161 162 DWORD 163 ScmCreateLastKnownGoodControlSet(VOID); 164 165 166 /* database.c */ 167 168 DWORD ScmCreateServiceDatabase(VOID); 169 VOID ScmShutdownServiceDatabase(VOID); 170 VOID ScmGetBootAndSystemDriverState(VOID); 171 VOID ScmAutoStartServices(VOID); 172 VOID ScmAutoShutdownServices(VOID); 173 DWORD ScmStartService(PSERVICE Service, 174 DWORD argc, 175 LPWSTR *argv); 176 177 VOID ScmRemoveServiceImage(PSERVICE_IMAGE pServiceImage); 178 PSERVICE ScmGetServiceEntryByName(LPCWSTR lpServiceName); 179 PSERVICE ScmGetServiceEntryByDisplayName(LPCWSTR lpDisplayName); 180 PSERVICE ScmGetServiceEntryByResumeCount(DWORD dwResumeCount); 181 DWORD ScmCreateNewServiceRecord(LPCWSTR lpServiceName, 182 PSERVICE *lpServiceRecord, 183 DWORD dwServiceType, 184 DWORD dwStartType); 185 VOID ScmDeleteServiceRecord(PSERVICE lpService); 186 DWORD ScmMarkServiceForDelete(PSERVICE pService); 187 188 DWORD ScmControlService(HANDLE hControlPipe, 189 PWSTR pServiceName, 190 SERVICE_STATUS_HANDLE hServiceStatus, 191 DWORD dwControl); 192 193 BOOL ScmLockDatabaseExclusive(VOID); 194 BOOL ScmLockDatabaseShared(VOID); 195 VOID ScmUnlockDatabase(VOID); 196 197 VOID ScmInitNamedPipeCriticalSection(VOID); 198 VOID ScmDeleteNamedPipeCriticalSection(VOID); 199 200 201 /* driver.c */ 202 203 DWORD ScmStartDriver(PSERVICE lpService); 204 DWORD ScmControlDriver(PSERVICE lpService, 205 DWORD dwControl, 206 LPSERVICE_STATUS lpServiceStatus); 207 208 209 /* groupdb.c */ 210 211 PSERVICE_GROUP 212 ScmGetServiceGroupByName( 213 _In_ LPCWSTR lpGroupName); 214 215 DWORD ScmCreateGroupList(VOID); 216 DWORD ScmSetServiceGroup(PSERVICE lpService, 217 LPCWSTR lpGroupName); 218 219 220 /* lock.c */ 221 222 DWORD ScmAcquireServiceStartLock(IN BOOL IsServiceController, 223 OUT LPSC_RPC_LOCK lpLock); 224 DWORD ScmReleaseServiceStartLock(IN OUT LPSC_RPC_LOCK lpLock); 225 VOID ScmQueryServiceLockStatusW(OUT LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus); 226 VOID ScmQueryServiceLockStatusA(OUT LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus); 227 228 229 /* rpcserver.c */ 230 231 VOID ScmStartRpcServer(VOID); 232 233 234 /* security.c */ 235 236 DWORD ScmInitializeSecurity(VOID); 237 VOID ScmShutdownSecurity(VOID); 238 239 DWORD 240 ScmCreateDefaultServiceSD( 241 PSECURITY_DESCRIPTOR *ppSecurityDescriptor); 242 243 244 /* services.c */ 245 246 VOID PrintString(LPCSTR fmt, ...); 247 VOID ScmLogEvent(DWORD dwEventId, 248 WORD wType, 249 WORD wStrings, 250 LPCWSTR *lpStrings); 251 VOID ScmWaitForLsa(VOID); 252 253 #endif /* _SERVICES_H */ 254