Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

436 lines
9.2 KiB

  1. //---------------------------------------------------------------
  2. //
  3. // File: SASetupCA.cpp
  4. //
  5. // Synopsis: This is the implementation of the custom actions in Server Appliance
  6. // setup.
  7. //
  8. //
  9. // History: 03/29/2001 AlpOn Created
  10. //
  11. // Copyright (C) 2000-2001 Microsoft Corporation
  12. // All rights reserved.
  13. //
  14. // SASetupCA.cpp : Defines the entry point for the DLL application.
  15. //
  16. #include "stdafx.h"
  17. #include <winsvc.h>
  18. const char REG_APPEND[]="append";
  19. //
  20. //private methods declared here
  21. //
  22. HRESULT
  23. StartNTService (
  24. /*[in]*/ PSTR pwszServiceName
  25. );
  26. HRESULT
  27. StopNTService (
  28. /*[in]*/ PSTR pwszServiceName
  29. );
  30. //++---------------------------------------------------------------------------
  31. //
  32. // Function: ChangeRegistry
  33. //
  34. // Synopsis: Function that gets called by SAkit setup to do custom operations
  35. // on registry
  36. // Arguments: szRegKeyName, - name of the regkey to be opened
  37. // szRegValueName - name of the value to be operated on
  38. // szRegAction - desired action on the registry (e.g. append)
  39. // szKeyValue - new value to be used for the reg key value during the operation
  40. // Returns: HRESULT - success/failure
  41. //
  42. // History: AlpOn Created 03/29/01
  43. //
  44. // Called By; Server Appliance Kit setup
  45. //
  46. //-----------------------------------------------------------------------------
  47. STDAPI ChangeRegistry(char *szRegKeyName,
  48. char *szRegValueName,
  49. char *szRegAction,
  50. char *szKeyValue)
  51. {
  52. HRESULT hReturn=S_OK;
  53. DWORD dwSize=0;
  54. CRegKey hKey;
  55. LONG lRes=0;
  56. char *szKeyValueRead=NULL;
  57. char *szRegKeyNewValue=NULL;
  58. SATracePrintf("ChangeRegistry called with: szRegKeyName:%s,szRegValueName:%s,szRegAction:%s,szKeyValue:%s",
  59. szRegKeyName,szRegValueName,szRegAction,szKeyValue);
  60. do{
  61. lRes=hKey.Open(HKEY_LOCAL_MACHINE,
  62. szRegKeyName,
  63. KEY_ALL_ACCESS );
  64. if(lRes!=ERROR_SUCCESS)
  65. {
  66. SATracePrintf("Regkey open - hKey.Open failed , lRes= %x Key=%s", lRes, szRegKeyName);
  67. hReturn=E_FAIL;
  68. break;
  69. }
  70. //open registry key, get size and read the value
  71. lRes=hKey.QueryValue(NULL,szRegValueName,&dwSize);
  72. szKeyValueRead=new char[dwSize];
  73. lRes=hKey.QueryValue(szKeyValueRead,szRegValueName,&dwSize);
  74. if(lRes!=ERROR_SUCCESS)
  75. {
  76. SATracePrintf("Unable to query regkey hKey.QueryValue failed lRes= %x valuename= %s",lRes, szKeyValueRead);
  77. hReturn=E_FAIL;
  78. break;
  79. }
  80. if(0==strcmpi(szRegAction,REG_APPEND))
  81. {
  82. SATracePrintf("ChangeRegistry called with append param");
  83. int size=(strlen(szKeyValueRead) + strlen(szKeyValue))+2;
  84. szRegKeyNewValue=new char[size];
  85. szRegKeyNewValue[0]='\0';
  86. strcat(szRegKeyNewValue,szKeyValueRead);
  87. strcat(szRegKeyNewValue," ");
  88. strcat(szRegKeyNewValue,szKeyValue);
  89. lRes=hKey.SetValue(szRegKeyNewValue, szRegValueName);
  90. if(lRes!=ERROR_SUCCESS)
  91. {
  92. SATracePrintf("Unable set regkey hKey.SetValue failed lRes= %x",lRes);
  93. hReturn=E_FAIL;
  94. break;
  95. }
  96. }
  97. }while(false);
  98. if(hKey.m_hKey)
  99. {
  100. hKey.Close();
  101. }
  102. delete []szKeyValueRead;
  103. delete []szRegKeyNewValue;
  104. return hReturn;
  105. }
  106. BOOL APIENTRY DllMain( HANDLE hModule,
  107. DWORD ul_reason_for_call,
  108. LPVOID lpReserved
  109. )
  110. {
  111. return TRUE;
  112. }
  113. //++---------------------------------------------------------------------------
  114. //
  115. // Function: ConfigureService
  116. //
  117. // Synopsis: API that gets called by SAkit setup to configure an
  118. // NT Service
  119. // Arguments:
  120. // [in] PSTR - Service Name (preferably short name)
  121. // [in] DWORD - Config Type (start or stop supported)
  122. //
  123. // Returns: HRESULT - success/failure
  124. //
  125. // History: MKarki Created 04/05/2001
  126. //
  127. // Called By; Server Appliance Kit setup
  128. //
  129. //-----------------------------------------------------------------------------
  130. STDAPI ConfigureService (
  131. /*[in]*/ PSTR pszServiceName,
  132. /*[in]*/ DWORD dwControlCode
  133. )
  134. {
  135. HRESULT hr = S_OK;
  136. CSATraceFunc objTraceFunc ("ConfigureService");
  137. try
  138. {
  139. if (NULL == pszServiceName)
  140. {
  141. SATraceString ("SASetup-ConfigureService passed in invalid argument");
  142. hr = E_INVALIDARG;
  143. return (hr);
  144. }
  145. //
  146. // carry out the required action
  147. //
  148. switch (dwControlCode)
  149. {
  150. case 0:
  151. //
  152. // stop NT Service
  153. //
  154. hr = StopNTService (pszServiceName);
  155. break;
  156. case 1:
  157. //
  158. // start NT Service
  159. //
  160. hr = StartNTService (pszServiceName);
  161. break;
  162. default:
  163. //
  164. // unknown control code passed in
  165. //
  166. SATracePrintf (
  167. "SASetup-ConfigureService passed in incorrect control code:%d",
  168. dwControlCode
  169. );
  170. hr = E_FAIL;
  171. break;
  172. }
  173. }
  174. catch (...)
  175. {
  176. SATraceString ("SASetup-ConfigureService caught unhandled exception");
  177. hr = E_FAIL;
  178. }
  179. return (hr);
  180. } // end of ConfigureService API
  181. //++---------------------------------------------------------------------------
  182. //
  183. // Function: StartNTService
  184. //
  185. // Synopsis: Method that Starts an NT Service
  186. // Arguments:
  187. // [in] PSTR - Service Name (preferably short name)
  188. //
  189. // Returns: HRESULT - success/failure
  190. //
  191. // History: MKarki Created 04/05/2001
  192. //
  193. // Called By; ConfigureService () API
  194. //
  195. //-----------------------------------------------------------------------------
  196. HRESULT
  197. StartNTService (
  198. /*[in]*/ PSTR pszServiceName
  199. )
  200. {
  201. CSATraceFunc objTraceFunc ("StartNTService");
  202. HRESULT hr = S_OK;
  203. SC_HANDLE hServiceManager = NULL;
  204. SC_HANDLE hService = NULL;
  205. do
  206. {
  207. if (NULL == pszServiceName)
  208. {
  209. SATraceString ("SASetup-StartNTService passed in invalid argument");
  210. hr = E_INVALIDARG;
  211. break;
  212. }
  213. //
  214. // open the Service Control Manager
  215. //
  216. hServiceManager = OpenSCManager (
  217. NULL,
  218. NULL,
  219. SC_MANAGER_ALL_ACCESS
  220. );
  221. if (NULL == hServiceManager)
  222. {
  223. SATraceFailure ("SASetup-StartNTService::OpenSCManager", GetLastError ());
  224. hr = E_FAIL;
  225. break;
  226. }
  227. //
  228. // open the NT Service
  229. //
  230. hService = OpenService (
  231. hServiceManager,
  232. pszServiceName,
  233. SERVICE_ALL_ACCESS
  234. );
  235. if (NULL == hService)
  236. {
  237. SATraceFailure ("SASetup-StartNTService::OpenService", GetLastError ());
  238. hr = E_FAIL;
  239. break;
  240. }
  241. //
  242. // start the service now
  243. //
  244. BOOL bRetVal = StartService (
  245. hService,
  246. 0,
  247. NULL
  248. );
  249. if (FALSE == bRetVal)
  250. {
  251. DWORD dwError = GetLastError ();
  252. //
  253. // its OK if the service is already running
  254. //
  255. if (ERROR_SERVICE_ALREADY_RUNNING != dwError)
  256. {
  257. SATraceFailure ("SASetup-StartNTService::StartService", dwError);
  258. hr = E_FAIL;
  259. break;
  260. }
  261. }
  262. }
  263. while (false);
  264. //
  265. // cleanup now
  266. //
  267. if (hService)
  268. {
  269. CloseServiceHandle(hService);
  270. }
  271. if (hServiceManager)
  272. {
  273. CloseServiceHandle(hServiceManager);
  274. }
  275. return (hr);
  276. } // end of StartNTService method
  277. //++---------------------------------------------------------------------------
  278. //
  279. // Function: StopNTService
  280. //
  281. // Synopsis: Method that stops an NT Service
  282. // Arguments:
  283. // [in] PWCHAR - Service Name (preferably short name)
  284. //
  285. // Returns: HRESULT - success/failure
  286. //
  287. // History: MKarki Created 04/05/2001
  288. //
  289. // Called By; ConfigureService () API
  290. //
  291. //-----------------------------------------------------------------------------
  292. HRESULT
  293. StopNTService (
  294. /*[in]*/ PSTR pszServiceName
  295. )
  296. {
  297. CSATraceFunc objTraceFunc ("StopNTService");
  298. HRESULT hr = S_OK;
  299. SC_HANDLE hServiceManager = NULL;
  300. SC_HANDLE hService = NULL;
  301. do
  302. {
  303. if (NULL == pszServiceName)
  304. {
  305. SATraceString ("SASetup-StopNTService passed in invalid argument");
  306. hr = E_INVALIDARG;
  307. break;
  308. }
  309. //
  310. // open the Service Control Manager
  311. //
  312. hServiceManager = OpenSCManager (
  313. NULL,
  314. NULL,
  315. SC_MANAGER_ALL_ACCESS
  316. );
  317. if (NULL == hServiceManager)
  318. {
  319. SATraceFailure ("SASetup-StopNTService::OpenSCManager", GetLastError ());
  320. hr = E_FAIL;
  321. break;
  322. }
  323. //
  324. // open the NT Service
  325. //
  326. hService = OpenService (
  327. hServiceManager,
  328. pszServiceName,
  329. SERVICE_ALL_ACCESS
  330. );
  331. if (NULL == hService)
  332. {
  333. SATraceFailure ("SASetup-StopNTService::OpenService", GetLastError ());
  334. hr = E_FAIL;
  335. break;
  336. }
  337. SERVICE_STATUS ServiceStatus;
  338. //
  339. // stop the service now
  340. //
  341. BOOL bRetVal = ControlService (
  342. hService,
  343. SERVICE_CONTROL_STOP,
  344. &ServiceStatus
  345. );
  346. if (FALSE == bRetVal)
  347. {
  348. DWORD dwError = GetLastError ();
  349. //
  350. // its OK if the service is already stopped
  351. //
  352. if (ERROR_SERVICE_NOT_ACTIVE != dwError)
  353. {
  354. SATraceFailure ("SASetup-StopNTService::ControlService", dwError);
  355. hr = E_FAIL;
  356. break;
  357. }
  358. }
  359. }
  360. while (false);
  361. //
  362. // cleanup now
  363. //
  364. if (hService)
  365. {
  366. CloseServiceHandle(hService);
  367. }
  368. if (hServiceManager)
  369. {
  370. CloseServiceHandle(hServiceManager);
  371. }
  372. return (hr);
  373. } // end of StopNTService method