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.

310 lines
8.6 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. RnrClnt.c
  5. Abstract:
  6. Setup program for installing/removing the "EchoExample" service.
  7. --*/
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <winsock2.h>
  11. #include <nspapi.h>
  12. WSADATA WsaData;
  13. #ifndef NS_NTDS
  14. #define NS_NTDS 9999
  15. #endif
  16. //
  17. // GUID for Echo-Example created with uuidgen:
  18. // "47da8500-96a1-11cd-901d-204c4f4f5020"
  19. //
  20. GUID ServiceGuid = { 0x47da8500, 0x96a1, 0x11cd, 0x90, 0x1d,
  21. 0x20, 0x4c, 0x4f, 0x4f, 0x50, 0x20 };
  22. #define ECHO_SERVICE_TYPE_NAME "EchoExample"
  23. #define ECHO_SERVICE_SAPID 999
  24. #define ECHO_SERVICE_TCPPORT 999
  25. #define RNR_SERVICE_NAME "RnrSvc"
  26. #define RNR_DISPLAY_NAME "RnrSampleService"
  27. void
  28. DoServiceSetup(
  29. char * Path
  30. )
  31. {
  32. SC_HANDLE ServiceManagerHandle;
  33. SC_HANDLE ServiceHandle;
  34. LPSTR KeyName = "System\\CurrentControlSet\\Services\\EventLog\\System\\RnrSvc";
  35. HKEY RnrKey;
  36. LONG err;
  37. DWORD Disposition;
  38. //
  39. // Create the service.
  40. //
  41. ServiceManagerHandle = OpenSCManager( NULL,
  42. NULL,
  43. STANDARD_RIGHTS_REQUIRED
  44. | SC_MANAGER_CREATE_SERVICE );
  45. if( ServiceManagerHandle == NULL ) {
  46. printf( "OpenSCManager failed: %ld\n", GetLastError() );
  47. exit(1);
  48. }
  49. ServiceHandle = CreateService( ServiceManagerHandle,
  50. RNR_SERVICE_NAME,
  51. RNR_DISPLAY_NAME,
  52. GENERIC_READ | GENERIC_WRITE,
  53. SERVICE_WIN32_OWN_PROCESS,
  54. SERVICE_DEMAND_START,
  55. SERVICE_ERROR_NORMAL,
  56. Path,
  57. NULL,
  58. NULL,
  59. NULL,
  60. NULL,
  61. NULL );
  62. if( ServiceHandle == NULL ) {
  63. printf( "CreateService failed: %ld\n", GetLastError() );
  64. CloseServiceHandle( ServiceManagerHandle );
  65. exit(1);
  66. }
  67. CloseServiceHandle( ServiceHandle );
  68. CloseServiceHandle( ServiceManagerHandle );
  69. printf( "%s created with path %s\n",
  70. RNR_SERVICE_NAME,
  71. Path );
  72. //
  73. // Add the data to the EventLog's registry key so that the
  74. // log insertion strings may be found by the Event Viewer.
  75. //
  76. err = RegCreateKeyEx( HKEY_LOCAL_MACHINE,
  77. KeyName,
  78. 0,
  79. NULL,
  80. REG_OPTION_NON_VOLATILE,
  81. KEY_WRITE,
  82. NULL,
  83. &RnrKey,
  84. &Disposition );
  85. if( err != 0 ) {
  86. printf( "RegCreateKeyEx failed: %ld\n", err );
  87. exit(1);
  88. }
  89. err = RegSetValueEx( RnrKey,
  90. "EventMessageFile",
  91. 0,
  92. REG_EXPAND_SZ,
  93. Path,
  94. strlen( Path ) + 1 );
  95. if( err == 0 ) {
  96. DWORD Value;
  97. Value = EVENTLOG_ERROR_TYPE
  98. | EVENTLOG_WARNING_TYPE
  99. | EVENTLOG_INFORMATION_TYPE;
  100. err = RegSetValueEx( RnrKey,
  101. "TypesSupported",
  102. 0,
  103. REG_DWORD,
  104. (CONST BYTE *)&Value,
  105. sizeof(Value) );
  106. }
  107. RegCloseKey( RnrKey );
  108. if( err != 0 ) {
  109. printf( "RegSetValueEx failed: %ld\n", err );
  110. exit(1);
  111. }
  112. exit(0);
  113. }
  114. void __cdecl
  115. main (
  116. int argc,
  117. char *argv[]
  118. )
  119. {
  120. INT err;
  121. WSASERVICECLASSINFO ServiceClassInfo;
  122. WSANSCLASSINFO lpNSClassInfo[6];
  123. DWORD Value1 = 1 ;
  124. DWORD SapValue = ECHO_SERVICE_SAPID ;
  125. DWORD TcpPortValue = ECHO_SERVICE_TCPPORT ;
  126. DWORD operation = SERVICE_ADD_TYPE;
  127. //
  128. // Initilize the Windows Sockets DLL.
  129. //
  130. err = WSAStartup( 0x0202, &WsaData );
  131. if ( err == SOCKET_ERROR ) {
  132. printf( "WSAStartup() failed: %ld\n", GetLastError( ) );
  133. exit(1);
  134. }
  135. //
  136. // Parse command-line arguments.
  137. //
  138. if (argc > 2) {
  139. printf( "usage: rnrsetup [/ADD | /DEL | /SVC:path]\n") ;
  140. exit(1);
  141. }
  142. if (argc == 2)
  143. {
  144. if ( _strnicmp( argv[1], "/add", 4 ) == 0 )
  145. {
  146. printf( "\nAdding service types to Rnr name spaces.\n" );
  147. }
  148. else if ( _strnicmp( argv[1], "/delete", 4 ) == 0 )
  149. {
  150. err = WSARemoveServiceClass( &ServiceGuid );
  151. WSACleanup();
  152. if ( err != NO_ERROR )
  153. {
  154. printf( "\nWSARemoveServiceClass failed: %ld\n",
  155. GetLastError( ) );
  156. exit(1);
  157. }
  158. printf( "\nWSARemoveServiceClass succeeded\n" );
  159. exit(0);
  160. }
  161. else if ( _strnicmp( argv[1], "/svc:", 5 ) == 0 )
  162. {
  163. printf( "\nAdding service entry to service control manager.\n" );
  164. DoServiceSetup( strchr( argv[1], ':' ) + 1 );
  165. printf( "Adding service types to Rnr name spaces.\n" );
  166. }
  167. else
  168. {
  169. printf( "usage: rnrsetup [/ADD | /DEL | /SVC:path]\n") ;
  170. exit(1);
  171. }
  172. }
  173. //
  174. // Set up information to pass to NSPInstallServiceClass() or
  175. // NSPRemoveServiceClass() to add or delete this
  176. // service type.
  177. //
  178. ServiceClassInfo.lpServiceClassId = &ServiceGuid;
  179. ServiceClassInfo.lpszServiceClassName = ECHO_SERVICE_TYPE_NAME;
  180. ServiceClassInfo.dwCount = 6;
  181. ServiceClassInfo.lpClassInfos = lpNSClassInfo;
  182. //
  183. // - - - SAP provider setup - - -
  184. //
  185. // The first value tells SAP that this is a connection-oriented
  186. // service.
  187. //
  188. lpNSClassInfo[0].lpszName = SERVICE_TYPE_VALUE_CONN ;
  189. lpNSClassInfo[0].dwNameSpace = NS_SAP ;
  190. lpNSClassInfo[0].dwValueType = REG_DWORD ;
  191. lpNSClassInfo[0].dwValueSize = 4 ;
  192. lpNSClassInfo[0].lpValue = &Value1 ;
  193. //
  194. // Next, give SAP the object type to use when broadcasting the
  195. // service name.
  196. //
  197. lpNSClassInfo[1].lpszName = SERVICE_TYPE_VALUE_SAPID ;
  198. lpNSClassInfo[1].dwNameSpace = NS_SAP ;
  199. lpNSClassInfo[1].dwValueType = REG_DWORD ;
  200. lpNSClassInfo[1].dwValueSize = sizeof(DWORD) ;
  201. lpNSClassInfo[1].lpValue = &SapValue ;
  202. //
  203. // - - - TCPIP provider setup - - -
  204. //
  205. // Tell the TCPIP name space provider that we will be using TCP
  206. // port 0x999.
  207. //
  208. lpNSClassInfo[2].lpszName = SERVICE_TYPE_VALUE_TCPPORT ;
  209. lpNSClassInfo[2].dwNameSpace = NS_DNS ;
  210. lpNSClassInfo[2].dwValueType = REG_DWORD ;
  211. lpNSClassInfo[2].dwValueSize = sizeof(DWORD) ;
  212. lpNSClassInfo[2].lpValue = &TcpPortValue ;
  213. //
  214. // - - - NTDS provider setup - - -
  215. //
  216. // The first value tells SAP that this is a connection-oriented
  217. // service.
  218. //
  219. lpNSClassInfo[3].lpszName = SERVICE_TYPE_VALUE_CONN ;
  220. lpNSClassInfo[3].dwNameSpace = NS_NTDS ;
  221. lpNSClassInfo[3].dwValueType = REG_DWORD ;
  222. lpNSClassInfo[3].dwValueSize = 4 ;
  223. lpNSClassInfo[3].lpValue = &Value1 ;
  224. //
  225. // Next, give SAP the object type to use when broadcasting the
  226. // service name.
  227. //
  228. lpNSClassInfo[4].lpszName = SERVICE_TYPE_VALUE_SAPID ;
  229. lpNSClassInfo[4].dwNameSpace = NS_NTDS ;
  230. lpNSClassInfo[4].dwValueType = REG_DWORD ;
  231. lpNSClassInfo[4].dwValueSize = sizeof(DWORD) ;
  232. lpNSClassInfo[4].lpValue = &SapValue ;
  233. //
  234. // Tell the NTDS name space provider that we will be using TCP
  235. // port 0x999.
  236. //
  237. lpNSClassInfo[5].lpszName = SERVICE_TYPE_VALUE_TCPPORT ;
  238. lpNSClassInfo[5].dwNameSpace = NS_NTDS ;
  239. lpNSClassInfo[5].dwValueType = REG_DWORD ;
  240. lpNSClassInfo[5].dwValueSize = sizeof(DWORD) ;
  241. lpNSClassInfo[5].lpValue = &TcpPortValue ;
  242. //
  243. // Finally, call WSAInstallServiceClass to actually perform the operation.
  244. //
  245. err = WSAInstallServiceClass( &ServiceClassInfo );
  246. WSACleanup();
  247. if ( err != NO_ERROR )
  248. {
  249. printf( "WSAInstallServiceClass failed: %ld\n", GetLastError( ) );
  250. exit(1);
  251. }
  252. printf( "WSAInstallServiceClass succeeded\n" );
  253. exit(0);
  254. } // main