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