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 /* controlset.c */ 154 155 BOOL ScmGetControlSetValues(VOID); 156 157 158 /* database.c */ 159 160 DWORD ScmCreateServiceDatabase(VOID); 161 VOID ScmShutdownServiceDatabase(VOID); 162 VOID ScmGetBootAndSystemDriverState(VOID); 163 VOID ScmAutoStartServices(VOID); 164 VOID ScmAutoShutdownServices(VOID); 165 DWORD ScmStartService(PSERVICE Service, 166 DWORD argc, 167 LPWSTR *argv); 168 169 VOID ScmRemoveServiceImage(PSERVICE_IMAGE pServiceImage); 170 PSERVICE ScmGetServiceEntryByName(LPCWSTR lpServiceName); 171 PSERVICE ScmGetServiceEntryByDisplayName(LPCWSTR lpDisplayName); 172 PSERVICE ScmGetServiceEntryByResumeCount(DWORD dwResumeCount); 173 DWORD ScmCreateNewServiceRecord(LPCWSTR lpServiceName, 174 PSERVICE *lpServiceRecord, 175 DWORD dwServiceType, 176 DWORD dwStartType); 177 VOID ScmDeleteServiceRecord(PSERVICE lpService); 178 DWORD ScmMarkServiceForDelete(PSERVICE pService); 179 180 DWORD ScmControlService(HANDLE hControlPipe, 181 PWSTR pServiceName, 182 SERVICE_STATUS_HANDLE hServiceStatus, 183 DWORD dwControl); 184 185 BOOL ScmLockDatabaseExclusive(VOID); 186 BOOL ScmLockDatabaseShared(VOID); 187 VOID ScmUnlockDatabase(VOID); 188 189 VOID ScmInitNamedPipeCriticalSection(VOID); 190 VOID ScmDeleteNamedPipeCriticalSection(VOID); 191 192 193 /* driver.c */ 194 195 DWORD ScmStartDriver(PSERVICE lpService); 196 DWORD ScmControlDriver(PSERVICE lpService, 197 DWORD dwControl, 198 LPSERVICE_STATUS lpServiceStatus); 199 200 201 /* groupdb.c */ 202 203 PSERVICE_GROUP 204 ScmGetServiceGroupByName( 205 _In_ LPCWSTR lpGroupName); 206 207 DWORD ScmCreateGroupList(VOID); 208 DWORD ScmSetServiceGroup(PSERVICE lpService, 209 LPCWSTR lpGroupName); 210 211 212 /* lock.c */ 213 214 DWORD ScmAcquireServiceStartLock(IN BOOL IsServiceController, 215 OUT LPSC_RPC_LOCK lpLock); 216 DWORD ScmReleaseServiceStartLock(IN OUT LPSC_RPC_LOCK lpLock); 217 VOID ScmQueryServiceLockStatusW(OUT LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus); 218 VOID ScmQueryServiceLockStatusA(OUT LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus); 219 220 221 /* rpcserver.c */ 222 223 VOID ScmStartRpcServer(VOID); 224 225 226 /* security.c */ 227 228 DWORD ScmInitializeSecurity(VOID); 229 VOID ScmShutdownSecurity(VOID); 230 231 DWORD 232 ScmCreateDefaultServiceSD( 233 PSECURITY_DESCRIPTOR *ppSecurityDescriptor); 234 235 236 /* services.c */ 237 238 VOID PrintString(LPCSTR fmt, ...); 239 VOID ScmLogEvent(DWORD dwEventId, 240 WORD wType, 241 WORD wStrings, 242 LPCWSTR *lpStrings); 243 VOID ScmWaitForLsa(VOID); 244 245 #endif /* _SERVICES_H */ 246