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.

1610 lines
35 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. isrpc.cxx
  5. Abstract:
  6. Contains ISRPC class implementation.
  7. Author:
  8. Murali R. Krishnan 11-Dec-1995
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. --*/
  13. /************************************************************
  14. * Include Headers
  15. ************************************************************/
  16. #include <tcpdllp.hxx>
  17. #include <isplat.h>
  18. #include "dbgutil.h"
  19. #include "isrpc.hxx"
  20. extern PFN_INETINFO_START_RPC_SERVER pfnInetinfoStartRpcServer;
  21. extern PFN_INETINFO_STOP_RPC_SERVER pfnInetinfoStopRpcServer;
  22. /************************************************************
  23. * Functions
  24. ************************************************************/
  25. ISRPC::ISRPC(IN LPCTSTR pszServiceName)
  26. /*++
  27. This function constructs a new ISRPC object, initializing the
  28. members to proper state.
  29. Always the ISRPC members will use RPC_C_AUTHN_WINNT.
  30. Arguments:
  31. pszServiceName - pointer to string containing the name of the service
  32. dwServiceAuthId - DWORD containing the service Authentication Identifier.
  33. Returns:
  34. A valid initialized ISRPC object on success.
  35. --*/
  36. : m_dwProtocols ( 0),
  37. m_fInterfaceAdded ( FALSE),
  38. m_fEpRegistered ( FALSE),
  39. m_fServerStarted ( FALSE),
  40. m_hRpcInterface ( NULL),
  41. m_pszServiceName ( pszServiceName),
  42. m_pBindingVector ( NULL)
  43. {
  44. DBG_REQUIRE( SetSecurityDescriptor() == NO_ERROR);
  45. IF_DEBUG( DLL_RPC) {
  46. DBGPRINTF(( DBG_CONTEXT,
  47. " Created new ISRPC object for %s at %08p\n",
  48. m_pszServiceName, this));
  49. }
  50. } // ISRPC::ISRPC()
  51. ISRPC::~ISRPC(VOID)
  52. /*++
  53. This function cleans up the ISRPC object and releases any dynamic memory or
  54. state associated with this object.
  55. --*/
  56. {
  57. if( m_hRpcInterface != NULL ) {
  58. // CleanupData() should not be called twice
  59. CleanupData();
  60. }
  61. IF_DEBUG( DLL_RPC) {
  62. DBGPRINTF(( DBG_CONTEXT,
  63. " Destroyed ISRPC object for %s at %p\n",
  64. m_pszServiceName, this));
  65. }
  66. } // ISRPC::~ISRPC()
  67. DWORD
  68. ISRPC::CleanupData(VOID)
  69. /*++
  70. Routine Description:
  71. This member function cleans up the ISRPC object.
  72. Arguments:
  73. None.
  74. Return Value:
  75. None.
  76. --*/
  77. {
  78. DWORD rpcStatus = RPC_S_OK;
  79. IF_DEBUG( DLL_RPC) {
  80. DBGPRINTF(( DBG_CONTEXT,
  81. " ISRPC(%p)::Cleaning up for %s\n",
  82. this, m_pszServiceName));
  83. }
  84. if ( m_fServerStarted) {
  85. rpcStatus = StopServer( );
  86. }
  87. DBG_ASSERT( rpcStatus == RPC_S_OK);
  88. rpcStatus = UnRegisterInterface();
  89. m_dwProtocols = 0;
  90. m_hRpcInterface = NULL;
  91. return (rpcStatus);
  92. } // ISRPC::CleanupData()
  93. DWORD
  94. ISRPC::RegisterInterface( IN RPC_IF_HANDLE hRpcInterface)
  95. /*++
  96. This function registers the RPC inteface in the object.
  97. If there is already a valid instance present in the object,
  98. this function fails and returns error.
  99. If this is the new interface specified, the function registers the
  100. interface both for dynamic and static bindings.
  101. Should be called after calling AddProtocol() and before StartServer()
  102. Arguments:
  103. hRpcInteface - RPC inteface handle.
  104. Returns:
  105. Win32 Error Code - NO_ERROR on success.
  106. --*/
  107. {
  108. DWORD dwError = NO_ERROR;
  109. if ( m_dwProtocols == 0) {
  110. // No protocol added. Return failure.
  111. return ( ERROR_INVALID_PARAMETER);
  112. }
  113. if ( m_hRpcInterface != NULL) {
  114. dwError = ( RPC_S_DUPLICATE_ENDPOINT);
  115. } else {
  116. //
  117. // since there is no duplicate, just set the new value and return.
  118. //
  119. if ( hRpcInterface == NULL) {
  120. dwError = ERROR_INVALID_PARAMETER;
  121. } else {
  122. m_hRpcInterface = hRpcInterface;
  123. }
  124. }
  125. if ( dwError == RPC_S_OK) {
  126. dwError = RpcServerRegisterIf(m_hRpcInterface,
  127. 0, // MgrUuid
  128. 0 // MgrEpv (Entry Point Vector)
  129. );
  130. if ( dwError == RPC_S_OK ) {
  131. m_fInterfaceAdded = TRUE;
  132. //
  133. // Establish the dynamic bindings if any.
  134. //
  135. if ( (m_dwProtocols & (ISRPC_OVER_TCPIP | ISRPC_OVER_SPX)) != 0) {
  136. dwError = RpcServerInqBindings( &m_pBindingVector);
  137. if ( dwError == RPC_S_OK) {
  138. DBG_ASSERT( m_pBindingVector != NULL);
  139. dwError = RpcEpRegister(m_hRpcInterface,
  140. m_pBindingVector,
  141. NULL,
  142. (unsigned char *) "" );
  143. if ( dwError == RPC_S_OK) {
  144. m_fEpRegistered = TRUE;
  145. }
  146. } // Ep registering
  147. } // dynamic bindings
  148. } // registration successful
  149. }
  150. IF_DEBUG(DLL_RPC) {
  151. DBGPRINTF(( DBG_CONTEXT, "ISRPC(%p)::RegisterInterface(%08x)"
  152. " returns %ld\n",
  153. this, hRpcInterface, dwError));
  154. }
  155. return ( dwError);
  156. } // ISRPC::RegisterInterface()
  157. DWORD
  158. ISRPC::UnRegisterInterface( VOID)
  159. /*++
  160. This function unregisters the RPC inteface in the object.
  161. Should be called after after StopServer() and before cleanup.
  162. Arguments:
  163. None
  164. Returns:
  165. Win32 Error Code - NO_ERROR on success.
  166. --*/
  167. {
  168. DWORD rpcStatus = RPC_S_OK;
  169. if ( m_fEpRegistered) {
  170. DBG_ASSERT( m_hRpcInterface != NULL && m_pBindingVector != NULL);
  171. rpcStatus = RpcEpUnregister(m_hRpcInterface,
  172. m_pBindingVector,
  173. NULL // pUuidVector
  174. );
  175. IF_DEBUG( DLL_RPC) {
  176. DBGPRINTF(( DBG_CONTEXT,
  177. "%p::RpcEpUnregister(%s) returns %d\n",
  178. this, m_pszServiceName, rpcStatus));
  179. }
  180. if( rpcStatus == EPT_S_CANT_PERFORM_OP )
  181. {
  182. // This error can be returned in cases such as system
  183. // shutdown that are not severe errors. So we don't
  184. // want to assert.
  185. DBGWARN(( DBG_CONTEXT,
  186. "%p::RpcEpUnregister(%s) failed with EPT_S_CANT_PERFORM_OP\n",
  187. this, m_pszServiceName
  188. ));
  189. }
  190. else
  191. {
  192. DBG_ASSERT( rpcStatus == RPC_S_OK );
  193. m_fEpRegistered = FALSE;
  194. }
  195. }
  196. if ( m_pBindingVector != NULL) {
  197. rpcStatus = RpcBindingVectorFree( &m_pBindingVector);
  198. IF_DEBUG( DLL_RPC) {
  199. DBGPRINTF(( DBG_CONTEXT,
  200. "%p::RpcBindingVectorFree(%s, %p) returns %d\n",
  201. this, m_pszServiceName,
  202. m_pBindingVector, rpcStatus));
  203. }
  204. DBG_ASSERT( rpcStatus == RPC_S_OK);
  205. m_pBindingVector = NULL;
  206. }
  207. if ( m_fInterfaceAdded != NULL) {
  208. rpcStatus = RpcServerUnregisterIf(m_hRpcInterface,
  209. NULL, // MgrUuid
  210. TRUE // wait for calls to complete
  211. );
  212. IF_DEBUG( DLL_RPC) {
  213. DBGPRINTF(( DBG_CONTEXT,
  214. "%p::RpcServerUnregisterIf(%s, %08x) returns %d\n",
  215. this, m_pszServiceName, m_hRpcInterface, rpcStatus));
  216. }
  217. }
  218. IF_DEBUG(DLL_RPC) {
  219. DBGPRINTF(( DBG_CONTEXT, "ISRPC(%p)::UnRegisterInterface(%08x)"
  220. " returns %ld\n",
  221. this, m_hRpcInterface, rpcStatus));
  222. }
  223. return ( rpcStatus);
  224. } // ISRPC::UnRegisterInterface()
  225. DWORD
  226. ISRPC::AddProtocol( IN DWORD Protocol)
  227. /*++
  228. Routine Description:
  229. This member function adds another protocol to the binding list.
  230. Arguments:
  231. protocol - protocol binding opcode.
  232. fDynamic - Boolean indicating if the call should do dynamic or static
  233. RPC binding for the protocol specified.
  234. Return Value:
  235. RPC error code.
  236. --*/
  237. {
  238. DWORD rpcStatus = RPC_S_OK;
  239. if ( Protocol & ISRPC_OVER_LPC ) {
  240. // Currently we only support static binding
  241. rpcStatus = BindOverLpc( FALSE);
  242. }
  243. #ifndef CHICAGO
  244. //
  245. // Enable all remote bindings
  246. //
  247. if ( rpcStatus == RPC_S_OK ) {
  248. if ( Protocol & ISRPC_OVER_TCPIP ) {
  249. // Currently we only support dynamic binding
  250. rpcStatus = BindOverTcp( TRUE);
  251. }
  252. if ( rpcStatus == RPC_S_OK && Protocol & ISRPC_OVER_NP ) {
  253. // Currently we only support static binding
  254. rpcStatus = BindOverNamedPipe( FALSE);
  255. }
  256. if ( rpcStatus == RPC_S_OK && Protocol & ISRPC_OVER_SPX ) {
  257. // Currently we only support dynamic binding
  258. rpcStatus = BindOverSpx( TRUE);
  259. }
  260. }
  261. #else // CHICAGO
  262. rpcStatus = RPC_S_OK;
  263. if ( Protocol & ISRPC_OVER_TCPIP ) {
  264. // Currently we only support dynamic binding
  265. rpcStatus = BindOverTcp( TRUE);
  266. }
  267. if ( Protocol & ISRPC_OVER_NB ) {
  268. // Currently we only support dynamic binding
  269. // Ignore status for NB for now
  270. (VOID)BindOverNetBios(TRUE);
  271. }
  272. #endif // CHICAGO
  273. IF_DEBUG( DLL_RPC) {
  274. DBGPRINTF(( DBG_CONTEXT,
  275. "ISRPC(%p)::AddProtocol(%08x) returns %ld.\n",
  276. this, Protocol, rpcStatus ));
  277. }
  278. return( rpcStatus );
  279. } // ISRPC::AddProtocol()
  280. DWORD
  281. ISRPC::RemoveProtocol(IN DWORD Protocol)
  282. /*++
  283. Routine Description:
  284. This member function removes a protocol from the binding list.
  285. Arguments:
  286. protocol - protocol binding opcode.
  287. Return Value:
  288. RPC error code.
  289. Note:
  290. As a side effect, this function removes the dynamic endpoing on
  291. TCPIP when SPX binding is removed and vice-versa.
  292. --*/
  293. {
  294. DBGPRINTF(( DBG_CONTEXT,
  295. " ISRPC(%p)::RemoveProtocol(%s) is not implemented\n",
  296. this, m_pszServiceName));
  297. DBG_ASSERT( FALSE);
  298. return ( ERROR_CALL_NOT_IMPLEMENTED);
  299. } // ISRPC::RemoveProtocol()
  300. DWORD
  301. ISRPC::StartServer(
  302. VOID
  303. )
  304. /*++
  305. Routine Description:
  306. This member function start RPC server.
  307. Arguments:
  308. None.
  309. Return Value:
  310. RPC error code.
  311. --*/
  312. {
  313. DWORD rpcStatus;
  314. //
  315. // add the interface.
  316. //
  317. if ( m_hRpcInterface == NULL) {
  318. return (ERROR_INVALID_PARAMETER);
  319. }
  320. //
  321. // start rpc server.
  322. //
  323. #ifndef SERVICE_AS_EXE
  324. rpcStatus = pfnInetinfoStartRpcServer();
  325. #else
  326. rpcStatus = RpcServerListen(
  327. 1, // minimum num threads.
  328. 1, // max concurrent calls.
  329. TRUE ); // don't wait
  330. #endif // SERVICE_AS_EXE
  331. if ( rpcStatus == RPC_S_OK ) {
  332. m_fServerStarted = TRUE;
  333. }
  334. IF_DEBUG( DLL_RPC) {
  335. DBGPRINTF(( DBG_CONTEXT, "ISRPC(%p)::StartServer(%s) returns %ld\n",
  336. this, m_pszServiceName, rpcStatus));
  337. }
  338. return( rpcStatus );
  339. } // ISRPC::StartServer()
  340. DWORD
  341. ISRPC::StopServer(
  342. VOID
  343. )
  344. {
  345. DWORD rpcStatus = RPC_S_OK;
  346. if( m_fServerStarted ) {
  347. #ifndef SERVICE_AS_EXE
  348. rpcStatus = pfnInetinfoStopRpcServer();
  349. #else
  350. //
  351. // stop server listen.
  352. //
  353. rpcStatus = RpcMgmtStopServerListening(0);
  354. //
  355. // wait for all RPC threads to go away.
  356. //
  357. if( rpcStatus == RPC_S_OK) {
  358. rpcStatus = RpcMgmtWaitServerListen();
  359. }
  360. #endif // SERVICE_AS_EXE
  361. m_fServerStarted = FALSE;
  362. }
  363. IF_DEBUG( DLL_RPC) {
  364. DBGPRINTF(( DBG_CONTEXT,
  365. "ISRPC(%p)::StopServer( %s) returns %d\n",
  366. this, m_pszServiceName, rpcStatus));
  367. }
  368. return ( rpcStatus);
  369. } // ISRPC::StopServer()
  370. DWORD
  371. ISRPC::EnumBindingStrings(
  372. IN OUT LPINET_BINDINGS pBindings
  373. )
  374. /*++
  375. Routine Description:
  376. This member function enumurates the binding strings of the protocols
  377. bound to the server.
  378. Arguments:
  379. pBindings : pointer to a binding strings structure. The caller
  380. should call FreeBindingStrings member function to free the string
  381. after use.
  382. Return Value:
  383. Windows Error Code;
  384. --*/
  385. {
  386. DWORD dwError;
  387. RPC_BINDING_VECTOR * pBindingVector = NULL;
  388. LPINET_BIND_INFO pBindingsInfo;
  389. DWORD dwCount = 0;
  390. DWORD i;
  391. //
  392. // query RPC for RPC_BINDING_VECTORS.
  393. //
  394. dwError = RpcServerInqBindings( &pBindingVector );
  395. if( dwError != NO_ERROR ) {
  396. goto Cleanup;
  397. }
  398. DBG_ASSERT( pBindingVector->Count > 0 );
  399. //
  400. // alloc memory for INET_RPC_BINDING_STRINGS.
  401. //
  402. pBindingsInfo = (LPINET_BIND_INFO)
  403. LocalAlloc( GPTR, sizeof(INET_BIND_INFO) * pBindingVector->Count );
  404. if( pBindingsInfo == NULL ) {
  405. dwError = ERROR_NOT_ENOUGH_MEMORY;
  406. goto Cleanup;
  407. }
  408. //
  409. // convert binding handle to binding vectors.
  410. //
  411. pBindings->NumBindings = 0;
  412. pBindings->BindingsInfo = pBindingsInfo;
  413. for( i = 0; i < pBindingVector->Count; i++ ) {
  414. LPSTR BindingString;
  415. BindingString = NULL;
  416. dwError = RpcBindingToStringBindingA(pBindingVector->BindingH[i],
  417. (LPBYTE *)&BindingString );
  418. if( dwError != NO_ERROR ) {
  419. goto Cleanup;
  420. }
  421. IF_DEBUG( DLL_RPC) {
  422. DBGPRINTF(( DBG_CONTEXT, "Binding Handle[%d] = %08x. String = %s\n",
  423. i, pBindingVector->BindingH[i], BindingString));
  424. }
  425. //
  426. // check to we get only our named-pipe endpoint.
  427. //
  428. if ( ( strstr( BindingString, "ncacn_np" ) == NULL ) ||
  429. ( strstr(BindingString, m_pszServiceName ) == NULL ) ) {
  430. RpcStringFreeA( (LPBYTE *)&BindingString );
  431. } else {
  432. //
  433. // found a named-pipe binding string with service name.
  434. //
  435. IF_DEBUG( DLL_RPC) {
  436. DBGPRINTF(( DBG_CONTEXT, "Binding String Chosen = %s\n",
  437. BindingString));
  438. }
  439. pBindings->BindingsInfo[dwCount].Length =
  440. (strlen(BindingString) + 1) * sizeof(CHAR);
  441. pBindings->BindingsInfo[dwCount].BindData = BindingString;
  442. dwCount++;
  443. }
  444. } // for
  445. dwError = NO_ERROR;
  446. pBindings->NumBindings = dwCount;
  447. IF_DEBUG( DLL_RPC) {
  448. DBGPRINTF(( DBG_CONTEXT, "Binding Vectors chosen"
  449. " Service = %s, NumBindings = %d of Total = %d\n",
  450. m_pszServiceName, dwCount, pBindingVector->Count));
  451. }
  452. Cleanup:
  453. if( pBindingVector != NULL ) {
  454. DWORD LocalError;
  455. LocalError = RpcBindingVectorFree( &pBindingVector );
  456. DBG_ASSERT( LocalError == NO_ERROR );
  457. }
  458. if( dwError != NO_ERROR ) {
  459. FreeBindingStrings( pBindings );
  460. pBindings->NumBindings = 0;
  461. IF_DEBUG( DLL_RPC) {
  462. DBGPRINTF(( DBG_CONTEXT,
  463. "ISRPC(%p)::EnumBindingStrings(%s) failed, %ld.",
  464. this, m_pszServiceName, dwError ));
  465. }
  466. }
  467. return( dwError );
  468. } // ISRPC::EnumBindingStrings()
  469. VOID
  470. ISRPC::FreeBindingStrings(
  471. IN OUT LPINET_BINDINGS pInetBindings
  472. )
  473. /*++
  474. Routine Description:
  475. This member function deletes a binding vector that was returned by the
  476. EnumBindingStrings member function.
  477. Arguments:
  478. pBindings : pointer to a binding vector.
  479. Return Value:
  480. Windows Error Code;
  481. --*/
  482. {
  483. DWORD dwError;
  484. DWORD i;
  485. //
  486. // free binding strings.
  487. //
  488. for( i = 0; i < pInetBindings->NumBindings; i++) {
  489. dwError = RpcStringFreeA( ((LPBYTE *)&pInetBindings
  490. ->BindingsInfo[i].BindData ));
  491. DBG_ASSERT( dwError == NO_ERROR );
  492. }
  493. pInetBindings->NumBindings = 0;
  494. //
  495. // free bindings info array.
  496. //
  497. if( pInetBindings->BindingsInfo != NULL ) {
  498. LocalFree( (LPWSTR)pInetBindings->BindingsInfo );
  499. pInetBindings->BindingsInfo = NULL;
  500. }
  501. return;
  502. } // ISRPC::FreeBindingStrings()
  503. DWORD
  504. ISRPC::BindOverTcp(IN BOOL fDynamic)
  505. {
  506. DWORD rpcStatus = RPC_S_OK;
  507. DBG_ASSERT( (m_dwProtocols & ISRPC_OVER_TCPIP) == 0);
  508. if ( !fDynamic) {
  509. rpcStatus = ( ERROR_CALL_NOT_IMPLEMENTED);
  510. } else {
  511. rpcStatus = ( ISRPC::DynamicBindOverTcp());
  512. }
  513. if ( rpcStatus == RPC_S_OK) {
  514. m_dwProtocols |= ISRPC_OVER_TCPIP;
  515. }
  516. if (rpcStatus == RPC_S_PROTSEQ_NOT_SUPPORTED) {
  517. //
  518. // This error gets written to the event log by the service controller,
  519. // so give it something the user is more likely to understand.
  520. //
  521. rpcStatus = DNS_ERROR_NO_TCPIP;
  522. IF_DEBUG( DLL_RPC) {
  523. DBGPRINTF(( DBG_CONTEXT,
  524. "(%p)::BindOverTcp(%s) mapping error %d to error %d\n",
  525. this,
  526. m_pszServiceName,
  527. RPC_S_PROTSEQ_NOT_SUPPORTED,
  528. DNS_ERROR_NO_TCPIP));
  529. }
  530. }
  531. IF_DEBUG( DLL_RPC) {
  532. DBGPRINTF(( DBG_CONTEXT, "(%p)::BindOverTcp(%s) returns %d\n",
  533. this, m_pszServiceName, rpcStatus));
  534. }
  535. return ( rpcStatus);
  536. } // ISRPC::BindOverTcpIp()
  537. #ifdef CHICAGO
  538. DWORD
  539. ISRPC::BindOverNetBios(IN BOOL fDynamic)
  540. {
  541. DWORD rpcStatus = RPC_S_OK;
  542. DBG_ASSERT( (m_dwProtocols & ISRPC_OVER_NB) == 0);
  543. if ( !fDynamic) {
  544. return ( ERROR_CALL_NOT_IMPLEMENTED);
  545. }
  546. // We will use Dynamic endpoint for the NetBios binding.
  547. rpcStatus =
  548. RpcServerUseProtseqW(
  549. L"ncacn_nb_ipx", // protocol string.
  550. ISRPC_PROTSEQ_MAX_REQS, //max concurrent calls
  551. &sm_sid ); // security
  552. rpcStatus =
  553. RpcServerUseProtseqW(
  554. L"ncacn_nb_tcp", // protocol string.
  555. ISRPC_PROTSEQ_MAX_REQS, //max concurrent calls
  556. &sm_sid ); // security
  557. switch (rpcStatus) {
  558. case RPC_S_OK:
  559. //
  560. // set the protocol bit.
  561. //
  562. m_dwProtocols |= ISRPC_OVER_NB;
  563. break;
  564. case RPC_S_DUPLICATE_ENDPOINT:
  565. DBGPRINTF(( DBG_CONTEXT,
  566. "(%p) ncacn_nb is already added for %s\n",
  567. this,
  568. m_pszServiceName));
  569. rpcStatus = RPC_S_OK;
  570. break;
  571. case RPC_S_PROTSEQ_NOT_SUPPORTED:
  572. case RPC_S_CANT_CREATE_ENDPOINT:
  573. DBGPRINTF(( DBG_CONTEXT,
  574. "(%p) ncacn_nb is not supported for %s (%ld).\n",
  575. this, m_pszServiceName, rpcStatus ));
  576. rpcStatus = RPC_S_OK;
  577. break;
  578. default:
  579. break;
  580. } // switch()
  581. //
  582. // if the security support provider is not enabled, do so.
  583. //
  584. if( rpcStatus == RPC_S_OK && !IsSecurityEnabled() ) {
  585. rpcStatus = AddSecurity();
  586. }
  587. IF_DEBUG( DLL_RPC) {
  588. DBGPRINTF(( DBG_CONTEXT, "(%p)::BindOverNetBios(%s) returns %d\n",
  589. this, m_pszServiceName, rpcStatus));
  590. }
  591. return ( rpcStatus);
  592. } // ISRPC::BindOverNetBios()
  593. #endif // CHICAGO
  594. DWORD
  595. ISRPC::BindOverNamedPipe(IN BOOL fDynamic)
  596. {
  597. DWORD rpcStatus = RPC_S_OK;
  598. DBG_ASSERT( (m_dwProtocols & ISRPC_OVER_NP) == 0);
  599. //
  600. // On Named Pipe, we support only static bindings. No dynamic Binding.
  601. //
  602. if ( fDynamic) {
  603. return ( ERROR_CALL_NOT_IMPLEMENTED);
  604. }
  605. if( (m_dwProtocols & ISRPC_OVER_NP) == 0 ) {
  606. WCHAR rgchNp[1024];
  607. wsprintfW( rgchNp,
  608. #ifdef UNICODE
  609. L"%ws%s"
  610. #else
  611. L"%ws%S"
  612. #endif // UNICODE
  613. ,
  614. ISRPC_NAMED_PIPE_PREFIX_W,
  615. m_pszServiceName);
  616. //
  617. // Establish a static Named pipe binding.
  618. //
  619. rpcStatus =
  620. RpcServerUseProtseqEpW(
  621. L"ncacn_np", // protocol string.
  622. ISRPC_PROTSEQ_MAX_REQS, //max concurrent calls
  623. rgchNp, // end point!
  624. &sm_sid ); // security
  625. IF_DEBUG( DLL_RPC) {
  626. CHAR pszBuff[100];
  627. wsprintfA( pszBuff, "%S", rgchNp);
  628. DBGPRINTF(( DBG_CONTEXT,
  629. " RpcServerUseProtseqEpW( %s, %d, %s, %p) returns"
  630. " %d\n",
  631. "ncacn_np", ISRPC_PROTSEQ_MAX_REQS,
  632. pszBuff, &sm_sid, rpcStatus));
  633. }
  634. switch (rpcStatus) {
  635. case RPC_S_OK:
  636. //
  637. // set the protocol bit.
  638. //
  639. m_dwProtocols |= ISRPC_OVER_NP;
  640. break;
  641. case RPC_S_DUPLICATE_ENDPOINT:
  642. //
  643. // Ignore the duplicate end point error
  644. //
  645. DBGPRINTF(( DBG_CONTEXT,
  646. "(%p) ncacn_np is already added for %s\n",
  647. this,
  648. m_pszServiceName));
  649. m_dwProtocols |= ISRPC_OVER_NP;
  650. rpcStatus = RPC_S_OK;
  651. break;
  652. case RPC_S_PROTSEQ_NOT_SUPPORTED:
  653. case RPC_S_CANT_CREATE_ENDPOINT:
  654. DBGPRINTF(( DBG_CONTEXT,
  655. "(%p) ncacn_np is not supported for %s (%ld).\n",
  656. this, m_pszServiceName, rpcStatus ));
  657. rpcStatus = RPC_S_OK;
  658. break;
  659. default:
  660. break;
  661. } // switch()
  662. }
  663. return ( rpcStatus);
  664. } // ISRPC::BindOverNamedPipe()
  665. DWORD
  666. ISRPC::BindOverLpc(IN BOOL fDynamic)
  667. {
  668. DWORD rpcStatus = RPC_S_OK;
  669. DBG_ASSERT( (m_dwProtocols & ISRPC_OVER_LPC) == 0);
  670. //
  671. // On LPC, we support only static bindings. No dynamic Binding.
  672. //
  673. if ( fDynamic) {
  674. return ( ERROR_CALL_NOT_IMPLEMENTED);
  675. }
  676. if( (m_dwProtocols & ISRPC_OVER_LPC) == 0 ) {
  677. WCHAR rgchLpc[1024];
  678. // LPC Endpoint string is: <InterfaceName>_LPC
  679. wsprintfW( rgchLpc,
  680. #ifdef UNICODE
  681. L"%s_%ws"
  682. #else
  683. L"%S_%ws"
  684. #endif // UNICODE
  685. ,
  686. m_pszServiceName,
  687. ISRPC_LPC_NAME_SUFFIX_W);
  688. //
  689. // Establish a static Lpc binding.
  690. //
  691. rpcStatus =
  692. RpcServerUseProtseqEpW(
  693. L"ncalrpc", // protocol string.
  694. ISRPC_PROTSEQ_MAX_REQS, //max concurrent calls
  695. rgchLpc, // end point!
  696. &sm_sid ); // security
  697. IF_DEBUG( DLL_RPC) {
  698. CHAR pszBuff[100];
  699. wsprintfA( pszBuff, "%S", rgchLpc);
  700. DBGPRINTF(( DBG_CONTEXT,
  701. " RpcServerUseProtseqEpW( %s, %d, %s, %p) returns"
  702. " %d\n",
  703. "ncalrpc", ISRPC_PROTSEQ_MAX_REQS,
  704. pszBuff, &sm_sid, rpcStatus));
  705. }
  706. switch (rpcStatus) {
  707. case RPC_S_OK:
  708. //
  709. // set the protocol bit.
  710. //
  711. m_dwProtocols |= ISRPC_OVER_LPC;
  712. break;
  713. case RPC_S_DUPLICATE_ENDPOINT:
  714. //
  715. // Ignore the duplicate end point error
  716. //
  717. DBGPRINTF(( DBG_CONTEXT,
  718. "(%p) ncalrpc is already added for %s\n",
  719. this,
  720. m_pszServiceName));
  721. m_dwProtocols |= ISRPC_OVER_LPC;
  722. rpcStatus = RPC_S_OK;
  723. break;
  724. case RPC_S_PROTSEQ_NOT_SUPPORTED:
  725. case RPC_S_CANT_CREATE_ENDPOINT:
  726. DBGPRINTF(( DBG_CONTEXT,
  727. "(%p) ncalrpc is not supported for %s (%ld).\n",
  728. this, m_pszServiceName, rpcStatus ));
  729. rpcStatus = RPC_S_OK;
  730. break;
  731. default:
  732. break;
  733. } // switch()
  734. }
  735. return ( rpcStatus);
  736. } // ISRPC::BindOverLpc()
  737. DWORD
  738. ISRPC::BindOverSpx(IN BOOL fDynamic)
  739. {
  740. DWORD rpcStatus = RPC_S_OK;
  741. DBG_ASSERT( (m_dwProtocols & ISRPC_OVER_SPX) == 0);
  742. if ( !fDynamic) {
  743. rpcStatus = ( ERROR_CALL_NOT_IMPLEMENTED);
  744. } else {
  745. rpcStatus = ISRPC::DynamicBindOverSpx();
  746. }
  747. if ( rpcStatus == RPC_S_OK) {
  748. m_dwProtocols |= ISRPC_OVER_SPX;
  749. }
  750. IF_DEBUG( DLL_RPC) {
  751. DBGPRINTF(( DBG_CONTEXT, "(%p)::BindOverSpx(%s) returns %d\n",
  752. this, m_pszServiceName, rpcStatus));
  753. }
  754. return ( rpcStatus);
  755. } // ISRPC::BindOverSpx()
  756. # if DBG
  757. VOID
  758. ISRPC::Print(VOID) const
  759. {
  760. DBGPRINTF(( DBG_CONTEXT,
  761. " ISRPC(%p). SvcName=%s\n"
  762. " Protocols = %d.\n"
  763. " RPC Interface = %08x. Binding Vector = %p\n"
  764. " InterfaceAdded = %d.\n"
  765. " EpRegistered = %d. ServerStarted = %d.\n"
  766. ,
  767. this, m_pszServiceName,
  768. m_dwProtocols,
  769. m_hRpcInterface, m_pBindingVector,
  770. m_fInterfaceAdded,
  771. m_fEpRegistered, m_fServerStarted
  772. ));
  773. } // ISRPC::Print()
  774. # endif // DBG
  775. /******************************
  776. * STATIC Member Definitions
  777. ******************************/
  778. DWORD ISRPC::sm_dwProtocols = 0;
  779. SECURITY_DESCRIPTOR ISRPC::sm_sid;
  780. PACL ISRPC::sm_pACL;
  781. BOOL ISRPC::sm_fSecurityEnabled = FALSE;
  782. DWORD
  783. ISRPC::Initialize(VOID)
  784. {
  785. sm_dwProtocols = 0;
  786. return SetSecurityDescriptor();
  787. } // ISRPC::Initialize()
  788. DWORD
  789. ISRPC::Cleanup(VOID)
  790. {
  791. //
  792. // Free up the memory holding the ACL for the security descriptor
  793. //
  794. delete [] ((BYTE *) sm_pACL);
  795. sm_pACL = NULL;
  796. //
  797. // Free up the security descriptor
  798. //
  799. ZeroMemory( (PVOID) &sm_sid, sizeof(sm_sid));
  800. //
  801. // For now nothing to do. Just a place holder.
  802. //
  803. return ( NO_ERROR);
  804. } // ISRPC::Cleanup()
  805. DWORD
  806. ISRPC::DynamicBindOverTcp(VOID)
  807. /*++
  808. This static function (ISRPC member) establishes a dynamic endpoing
  809. RPC binding over TCP/IP, using a run-time library call to RPC.
  810. RPC run-time library allows one to create as many dynamic end points
  811. as one wishes. So we maintain external state and control the number
  812. of end points created to 1.
  813. Arguments:
  814. None
  815. Returns:
  816. RPC status - RPC_S_OK for success.
  817. --*/
  818. {
  819. DWORD rpcStatus = RPC_S_OK;
  820. if( (sm_dwProtocols & ISRPC_OVER_TCPIP) == 0 ) {
  821. //
  822. // Not already present. Add dynamic endpoint over TCP/IP
  823. //
  824. rpcStatus =
  825. RpcServerUseProtseqW(
  826. L"ncacn_ip_tcp", // protocol string.
  827. ISRPC_PROTSEQ_MAX_REQS, //max concurrent calls
  828. &sm_sid ); // security
  829. switch (rpcStatus) {
  830. case RPC_S_OK:
  831. //
  832. // set the protocol bit.
  833. //
  834. sm_dwProtocols |= ISRPC_OVER_TCPIP;
  835. break;
  836. case RPC_S_DUPLICATE_ENDPOINT:
  837. DBGPRINTF(( DBG_CONTEXT,
  838. "ncacn_ip_tcp is already added.\n"));
  839. sm_dwProtocols |= ISRPC_OVER_TCPIP;
  840. rpcStatus = RPC_S_OK;
  841. break;
  842. case RPC_S_PROTSEQ_NOT_SUPPORTED:
  843. case RPC_S_CANT_CREATE_ENDPOINT:
  844. DBGPRINTF(( DBG_CONTEXT,
  845. "ncacn_ip_tcp is not supported. Error = %ld\n",
  846. rpcStatus));
  847. break;
  848. default:
  849. break;
  850. } // switch()
  851. //
  852. // if the security support provider is not enabled, do so.
  853. //
  854. if( rpcStatus == RPC_S_OK && !IsSecurityEnabled() ) {
  855. rpcStatus = AddSecurity();
  856. }
  857. }
  858. IF_DEBUG( DLL_RPC) {
  859. DBGPRINTF(( DBG_CONTEXT, "DynamicBindOverTcp() returns %d\n",
  860. rpcStatus));
  861. }
  862. return ( rpcStatus);
  863. } // ISRPC::DynamicBindOverTcp()
  864. DWORD
  865. ISRPC::DynamicBindOverSpx(VOID)
  866. /*++
  867. This static function (ISRPC member) establishes a dynamic endpoing
  868. RPC binding over SPX, using a run-time library call to RPC.
  869. RPC run-time library allows one to create as many dynamic end points
  870. as one wishes. So we maintain external state and control the number
  871. of end points created to 1.
  872. Arguments:
  873. None
  874. Returns:
  875. RPC status - RPC_S_OK for success.
  876. --*/
  877. {
  878. DWORD rpcStatus = RPC_S_OK;
  879. if( (sm_dwProtocols & ISRPC_OVER_SPX) == 0 ) {
  880. // Use dynamic end point for the server.
  881. rpcStatus =
  882. RpcServerUseProtseqW(
  883. L"ncacn_spx", // protocol string.
  884. ISRPC_PROTSEQ_MAX_REQS, //max concurrent calls
  885. &sm_sid ); // security
  886. switch (rpcStatus) {
  887. case RPC_S_OK:
  888. //
  889. // set the protocol bit.
  890. //
  891. sm_dwProtocols |= ISRPC_OVER_SPX;
  892. break;
  893. case RPC_S_DUPLICATE_ENDPOINT:
  894. DBGPRINTF(( DBG_CONTEXT,
  895. "ncacn_spx is already added.\n"
  896. ));
  897. sm_dwProtocols |= ISRPC_OVER_SPX;
  898. rpcStatus = RPC_S_OK;
  899. break;
  900. case RPC_S_PROTSEQ_NOT_SUPPORTED:
  901. case RPC_S_CANT_CREATE_ENDPOINT:
  902. DBGPRINTF(( DBG_CONTEXT,
  903. "ncacn_spx is not supported. Error (%ld).\n",
  904. rpcStatus ));
  905. break;
  906. default:
  907. break;
  908. } // switch()
  909. //
  910. // if the security support provider is not enabled, do so.
  911. //
  912. if( rpcStatus == RPC_S_OK && !IsSecurityEnabled()) {
  913. rpcStatus = AddSecurity();
  914. }
  915. }
  916. IF_DEBUG( DLL_RPC) {
  917. DBGPRINTF(( DBG_CONTEXT, "DynamicBindOverSpx() returns %d\n",
  918. rpcStatus));
  919. }
  920. return ( rpcStatus);
  921. } // ISRPC::DynamicBindOverSpx()
  922. DWORD
  923. ISRPC::SetSecurityDescriptor( VOID)
  924. /*++
  925. Routine Description:
  926. This member function builds the security descriptor used by RPC module.
  927. The security descriptor denies everybody the ability to change/see anything
  928. connected to the DACL and allows everybody to read from/write to the pipe.
  929. Arguments:
  930. None.
  931. Return Value:
  932. Windows error code.
  933. --*/
  934. {
  935. DWORD dwError = NO_ERROR;
  936. BOOL fSuccess = FALSE;
  937. SID_IDENTIFIER_AUTHORITY siaWorld = SECURITY_WORLD_SID_AUTHORITY;
  938. PSID psidWorld = NULL;
  939. PSID psidLocalSystem = NULL;
  940. BYTE *pbBuffer = NULL;
  941. DWORD cbAcl = 0;
  942. InitializeSecurityDescriptor(&sm_sid,
  943. SECURITY_DESCRIPTOR_REVISION );
  944. //
  945. // Create the "WORLD" sid
  946. //
  947. if ( !AllocateAndInitializeSid( &siaWorld,
  948. 1,
  949. SECURITY_WORLD_RID,
  950. 0,0,0,0,0,0,0,
  951. &psidWorld ) )
  952. {
  953. DBGPRINTF((DBG_CONTEXT,
  954. "AllocateAndInitializeSid failed : 0x%x\n",
  955. GetLastError()));
  956. goto cleanup;
  957. }
  958. //
  959. // Calculate the size of the ACL that will hold the the ACESS_DENIED and ACCESS_ALLOW ace
  960. // [ripped off from MSDN docs]
  961. //
  962. cbAcl = sizeof(ACL) +
  963. sizeof( ACCESS_ALLOWED_ACE ) +
  964. sizeof( ACCESS_DENIED_ACE ) +
  965. 2*GetLengthSid(psidWorld) -
  966. 2*sizeof(DWORD) ;
  967. if ( ! ( pbBuffer = new BYTE[cbAcl] ) )
  968. {
  969. goto cleanup;
  970. }
  971. sm_pACL = (PACL) pbBuffer;
  972. //
  973. // Initialize the ACL
  974. //
  975. if ( !InitializeAcl( sm_pACL,
  976. cbAcl,
  977. ACL_REVISION ) )
  978. {
  979. DBGPRINTF((DBG_CONTEXT,
  980. "InitializeAcl failed : 0x%x\n",
  981. GetLastError()));
  982. goto cleanup;
  983. }
  984. //
  985. // Add the Access Denied ACE; this has to be first in the list to make sure
  986. // that any attempt to muck with the DACL will be disallowed
  987. //
  988. if ( !AddAccessDeniedAce( sm_pACL,
  989. ACL_REVISION,
  990. WRITE_DAC | DELETE | WRITE_OWNER,
  991. psidWorld ) )
  992. {
  993. DBGPRINTF((DBG_CONTEXT,
  994. "AddAccessDeniedAce failed : 0x%x\n",
  995. GetLastError()));
  996. goto cleanup;
  997. }
  998. //
  999. // Add the Access Allowed ACE
  1000. //
  1001. if ( !AddAccessAllowedAce( sm_pACL,
  1002. ACL_REVISION,
  1003. FILE_ALL_ACCESS,
  1004. psidWorld ) )
  1005. {
  1006. DBGPRINTF((DBG_CONTEXT,
  1007. "AddAccessAllowedAce failed : 0x%x\n",
  1008. GetLastError()));
  1009. goto cleanup;
  1010. }
  1011. //
  1012. // Set (no) group & owner for the security descriptor
  1013. //
  1014. if ( !SetSecurityDescriptorOwner( &sm_sid,
  1015. NULL,
  1016. FALSE ) )
  1017. {
  1018. DBGPRINTF((DBG_CONTEXT,
  1019. "SetsecurityDescriptorOwner failed : 0x%x\n",
  1020. GetLastError()));
  1021. goto cleanup;
  1022. }
  1023. if ( !SetSecurityDescriptorGroup( &sm_sid,
  1024. NULL,
  1025. FALSE ) )
  1026. {
  1027. DBGPRINTF((DBG_CONTEXT,
  1028. "SetsecurityDescriptorGroup failed : 0x%x\n",
  1029. GetLastError()));
  1030. goto cleanup;
  1031. }
  1032. if ( !( fSuccess = SetSecurityDescriptorDacl ( &sm_sid,
  1033. TRUE, // Dacl present
  1034. #if 1
  1035. sm_pACL,
  1036. #else
  1037. NULL, // NULL Dacl
  1038. #endif
  1039. FALSE ) ) ) // Not defaulted
  1040. {
  1041. DBGPRINTF((DBG_CONTEXT,
  1042. "SetSecurityDescriptorDacl failed : 0x%x\n",
  1043. GetLastError()));
  1044. }
  1045. cleanup:
  1046. if ( psidWorld )
  1047. {
  1048. FreeSid( psidWorld );
  1049. }
  1050. if (!fSuccess)
  1051. {
  1052. dwError = GetLastError();
  1053. if ( pbBuffer )
  1054. {
  1055. delete [] pbBuffer;
  1056. sm_pACL = NULL;
  1057. }
  1058. //
  1059. // free up security discriptor memory and set it to NULL.
  1060. //
  1061. memset( (PVOID ) &sm_sid, 0, sizeof(sm_sid));
  1062. }
  1063. return( dwError );
  1064. } // ISRPC::SetSecurityDescriptor()
  1065. DWORD
  1066. ISRPC::AddSecurity(
  1067. VOID
  1068. )
  1069. /*++
  1070. Routine Description:
  1071. This member function adds security support provider over RPC.
  1072. Arguments:
  1073. None.
  1074. Return Value:
  1075. Windows error code.
  1076. --*/
  1077. {
  1078. DWORD rpcStatus;
  1079. //
  1080. // Register for authentication using WinNT.
  1081. //
  1082. rpcStatus = RpcServerRegisterAuthInfo(
  1083. (unsigned char * ) NULL, // app name to security provider
  1084. RPC_C_AUTHN_WINNT, // Auth package ID.
  1085. NULL, // RPC_C_AUTHN_WINNT ==> NULL
  1086. NULL // args ptr for authn function.
  1087. );
  1088. if ( rpcStatus == RPC_S_OK) {
  1089. sm_fSecurityEnabled = TRUE;
  1090. }
  1091. IF_DEBUG( DLL_RPC) {
  1092. DBGPRINTF(( DBG_CONTEXT, "AddSecurity() returns Error %u\n",
  1093. rpcStatus));
  1094. }
  1095. //
  1096. // Hide the failure that occurs when the server is locked
  1097. // down and does not have the network client installed. This will
  1098. // cause performance counters to fail.
  1099. //
  1100. if( rpcStatus == RPC_S_UNKNOWN_AUTHN_SERVICE )
  1101. {
  1102. DBGWARN(( DBG_CONTEXT,
  1103. "RpcServerRegisterAuthInfo failed with "
  1104. "RPC_S_UNKNOWN_AUTHN_SERVICE. Some features, such as "
  1105. "performance counters, may not function.\n"
  1106. ));
  1107. rpcStatus = RPC_S_OK;
  1108. }
  1109. return (rpcStatus);
  1110. } // ISRPC::AddSecurity()