Source code of Windows XP (NT5)
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.

237 lines
5.6 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. DnsClnt.c
  5. Abstract:
  6. Setup program for installing/removing the "EchoExample" service.
  7. Author:
  8. David Treadwell (davidtr) 30-June-1994
  9. Revision History:
  10. Chuck Y. Chan (chuckc) 17-July-1994
  11. Misc cleanup. Pointer based blobs.
  12. Charles K. Moore (keithmo) 27-July-1994
  13. Added RrnSvc service setup option.
  14. --*/
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <windows.h>
  18. #define DNS_SERVICE_NAME "DNS"
  19. #define DNS_DISPLAY_NAME "Domain Name Server"
  20. #define SERVICES_KEY_NAME "System\\CurrentControlSet\\Services"
  21. #define EVENTLOG_KEY_NAME SERVICES_KEY_NAME "\\EventLog\\System\\"
  22. #define DNS_EVENT_KEY_NAME EVENTLOG_KEY_NAME DNS_SERVICE_NAME
  23. #define DNS_EXE_PATH "%SystemRoot%\\system32\\dns.exe"
  24. #define START_AUTOMATICALLY 0x2
  25. #define START_MANUALLY 0x3
  26. #define SERVICE_DEPENDENCY_LIST ("TcpIp\0Afd\0NetBT\0RpcSs\0NtLmSsp\0\0")
  27. void
  28. DnsServiceSetup(
  29. VOID
  30. )
  31. {
  32. SC_HANDLE ServiceManagerHandle;
  33. SC_HANDLE ServiceHandle;
  34. HKEY DnsKey;
  35. LONG err;
  36. DWORD Disposition;
  37. DWORD Startval;
  38. //
  39. // open service manager
  40. //
  41. ServiceManagerHandle = OpenSCManager(
  42. NULL,
  43. NULL,
  44. SC_MANAGER_ALL_ACCESS );
  45. if( ServiceManagerHandle == NULL )
  46. {
  47. printf( "OpenSCManager failed: %ld\n", GetLastError() );
  48. exit(1);
  49. }
  50. //
  51. // create the service
  52. //
  53. ServiceHandle = CreateService(
  54. ServiceManagerHandle,
  55. DNS_SERVICE_NAME,
  56. DNS_DISPLAY_NAME,
  57. GENERIC_READ | GENERIC_WRITE,
  58. SERVICE_WIN32_SHARE_PROCESS,
  59. SERVICE_DEMAND_START,
  60. SERVICE_ERROR_NORMAL,
  61. DNS_EXE_PATH,
  62. NULL,
  63. NULL,
  64. SERVICE_DEPENDENCY_LIST,
  65. NULL,
  66. NULL );
  67. if( ServiceHandle == NULL )
  68. {
  69. //
  70. // Service already exists?
  71. //
  72. if ( ( GetLastError() & 0xFFFF ) == ERROR_SERVICE_EXISTS )
  73. {
  74. printf( "Updating previously installed DNS service.\n" );
  75. ServiceHandle = OpenService(
  76. ServiceManagerHandle,
  77. DNS_SERVICE_NAME,
  78. GENERIC_READ | GENERIC_WRITE );
  79. if ( ServiceHandle == NULL )
  80. {
  81. printf( "OpenService failed: %ld\n", GetLastError() );
  82. CloseServiceHandle( ServiceManagerHandle );
  83. exit(1);
  84. }
  85. if ( !ChangeServiceConfig(
  86. ServiceHandle,
  87. SERVICE_WIN32_SHARE_PROCESS,
  88. SERVICE_DEMAND_START,
  89. SERVICE_ERROR_NORMAL,
  90. DNS_EXE_PATH,
  91. NULL,
  92. NULL,
  93. SERVICE_DEPENDENCY_LIST,
  94. NULL,
  95. NULL,
  96. DNS_DISPLAY_NAME ) )
  97. {
  98. printf(
  99. "ERROR: Could not change services configuration.\n"
  100. "\tError = %ld\n"
  101. "The DNS services may not be installed properly.\n",
  102. GetLastError() );
  103. }
  104. }
  105. else
  106. {
  107. printf( "CreateService failed: %ld\n", GetLastError() );
  108. CloseServiceHandle( ServiceManagerHandle );
  109. exit(1);
  110. }
  111. }
  112. else
  113. {
  114. //
  115. // successful creation of service
  116. //
  117. printf(
  118. "%s created with path %s\n",
  119. DNS_SERVICE_NAME,
  120. DNS_EXE_PATH );
  121. }
  122. CloseServiceHandle( ServiceHandle );
  123. CloseServiceHandle( ServiceManagerHandle );
  124. //
  125. // Add the data to the EventLog's registry key so that the
  126. // log insertion strings may be found by the Event Viewer.
  127. //
  128. err = RegCreateKeyEx(
  129. HKEY_LOCAL_MACHINE,
  130. DNS_EVENT_KEY_NAME,
  131. 0,
  132. NULL,
  133. REG_OPTION_NON_VOLATILE,
  134. KEY_WRITE,
  135. NULL,
  136. &DnsKey,
  137. &Disposition );
  138. if( err != ERROR_SUCCESS )
  139. {
  140. printf( "RegCreateKeyEx failed: %ld\n", err );
  141. exit(1);
  142. }
  143. err = RegSetValueEx(
  144. DnsKey,
  145. "EventMessageFile",
  146. 0,
  147. REG_EXPAND_SZ,
  148. DNS_EXE_PATH,
  149. strlen( DNS_EXE_PATH ) + 1 );
  150. if( err == ERROR_SUCCESS )
  151. {
  152. DWORD Value;
  153. Value = EVENTLOG_ERROR_TYPE
  154. | EVENTLOG_WARNING_TYPE
  155. | EVENTLOG_INFORMATION_TYPE;
  156. err = RegSetValueEx(
  157. DnsKey,
  158. "TypesSupported",
  159. 0,
  160. REG_DWORD,
  161. (CONST BYTE *)&Value,
  162. sizeof(Value) );
  163. }
  164. RegCloseKey( DnsKey );
  165. if( err != 0 )
  166. {
  167. printf( "RegSetValueEx failed: %ld\n", err );
  168. exit(1);
  169. }
  170. //
  171. // Add the data to the EventLog's registry key so that the
  172. // log insertion strings may be found by the Event Viewer.
  173. //
  174. RegCloseKey( DnsKey );
  175. if( err != 0 )
  176. {
  177. printf( "RegSetValueEx (%s) failed: %ld\n", "Start", err );
  178. exit(1);
  179. }
  180. exit(0);
  181. }
  182. void __cdecl
  183. main (
  184. int argc,
  185. CHAR *argv[]
  186. )
  187. {
  188. DnsServiceSetup();
  189. exit(0);
  190. } // main