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.

673 lines
17 KiB

  1. //---------------------------------------------------------------------------DSI
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995
  5. //
  6. // File: csess.cxx
  7. //
  8. // Contents: Contains methods for the following objects
  9. // CWinNTSession, CWinNTSessionGeneralInfo
  10. //
  11. //
  12. // History: 02/08/96 ramv (Ram Viswanathan) Created.
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "winnt.hxx"
  16. #pragma hdrstop
  17. #define INITGUID
  18. DECLARE_INFOLEVEL( Session );
  19. DECLARE_DEBUG( Session );
  20. #define SessionDebugOut(x) SessionInlineDebugOut x
  21. DEFINE_IDispatch_ExtMgr_Implementation(CWinNTSession);
  22. DEFINE_IADsExtension_ExtMgr_Implementation(CWinNTSession);
  23. DEFINE_IADs_TempImplementation(CWinNTSession);
  24. DEFINE_IADs_PutGetImplementation(CWinNTSession, SessionClass,gdwSessionTableSize);
  25. DEFINE_IADsPropertyList_Implementation(CWinNTSession, SessionClass,gdwSessionTableSize)
  26. CWinNTSession::CWinNTSession()
  27. {
  28. _pDispMgr = NULL;
  29. _pExtMgr = NULL;
  30. _pszServerName = NULL;
  31. _pszComputerName = NULL;
  32. _pszUserName = NULL;
  33. _pPropertyCache = NULL;
  34. ENLIST_TRACKING(CWinNTSession);
  35. return;
  36. }
  37. CWinNTSession::~CWinNTSession()
  38. {
  39. delete _pExtMgr; // created last, destroyed first
  40. delete _pDispMgr;
  41. if(_pszServerName){
  42. FreeADsStr(_pszServerName);
  43. }
  44. if(_pszServerADsPath){
  45. FreeADsStr(_pszServerADsPath);
  46. }
  47. if(_pszComputerName){
  48. FreeADsStr(_pszComputerName);
  49. }
  50. if(_pszUserName){
  51. FreeADsStr(_pszUserName);
  52. }
  53. delete _pPropertyCache;
  54. return;
  55. }
  56. //+---------------------------------------------------------------------------
  57. //
  58. // Function: CWinNTSession::Create
  59. //
  60. // Synopsis: Static function used to create a Session object. This
  61. // will be called by EnumSessions::Next
  62. //
  63. // Arguments: [ppWinNTSession] -- Ptr to a ptr to a new Session object.
  64. //
  65. // Returns: HRESULT.
  66. //
  67. // Modifies:
  68. //
  69. // History: 12-11-95 RamV Created.
  70. //
  71. //----------------------------------------------------------------------------
  72. HRESULT
  73. CWinNTSession::Create(LPTSTR pszServerADsPath,
  74. LPTSTR pszClientName,
  75. LPTSTR pszUserName,
  76. DWORD dwObject,
  77. REFIID riid,
  78. CWinNTCredentials& Credentials,
  79. LPVOID * ppvoid
  80. )
  81. {
  82. CWinNTSession FAR * pCWinNTSession = NULL;
  83. HRESULT hr;
  84. TCHAR szSessionName[MAX_PATH];
  85. //
  86. // Create the Session Object
  87. //
  88. hr = AllocateSessionObject(pszServerADsPath,
  89. pszClientName,
  90. pszUserName,
  91. &pCWinNTSession);
  92. BAIL_IF_ERROR(hr);
  93. ADsAssert(pCWinNTSession->_pDispMgr);
  94. wcscpy(szSessionName, pszUserName);
  95. wcscat(szSessionName, TEXT("\\"));
  96. wcscat(szSessionName, pszClientName);
  97. hr = pCWinNTSession->InitializeCoreObject(pszServerADsPath,
  98. szSessionName,
  99. SESSION_CLASS_NAME,
  100. SESSION_SCHEMA_NAME,
  101. CLSID_WinNTSession,
  102. dwObject);
  103. BAIL_IF_ERROR(hr);
  104. pCWinNTSession->_Credentials = Credentials;
  105. hr = pCWinNTSession->_Credentials.RefServer(
  106. pCWinNTSession->_pszServerName);
  107. BAIL_IF_ERROR(hr);
  108. if( pszUserName && *pszUserName ) {
  109. hr = SetLPTSTRPropertyInCache(pCWinNTSession->_pPropertyCache,
  110. TEXT("User"),
  111. pszUserName,
  112. TRUE
  113. );
  114. BAIL_IF_ERROR(hr);
  115. }
  116. hr = SetLPTSTRPropertyInCache(pCWinNTSession->_pPropertyCache,
  117. TEXT("Computer"),
  118. pCWinNTSession->_pszComputerName,
  119. TRUE
  120. );
  121. BAIL_IF_ERROR(hr);
  122. //
  123. // Load ext mgr and extensions
  124. //
  125. hr = ADSILoadExtensionManager(
  126. SESSION_CLASS_NAME,
  127. (IADs *) pCWinNTSession,
  128. pCWinNTSession->_pDispMgr,
  129. Credentials,
  130. &pCWinNTSession->_pExtMgr
  131. );
  132. BAIL_IF_ERROR(hr);
  133. ADsAssert(pCWinNTSession->_pExtMgr);
  134. // check if the call is from UMI
  135. if(Credentials.GetFlags() & ADS_AUTH_RESERVED) {
  136. //
  137. // we do not pass riid to InitUmiObject below. This is because UMI object
  138. // does not support IDispatch. There are several places in ADSI code where
  139. // riid passed into this function is defaulted to IID_IDispatch -
  140. // IADsContainer::Create for example. To handle these cases, we always
  141. // request IID_IUnknown from the UMI object. Subsequent code within UMI
  142. // will QI for the appropriate interface.
  143. //
  144. // Session objects have "" as their ADsPath. Just set the class for
  145. // identification purposes.
  146. pCWinNTSession->_CompClasses[0] = L"Session";
  147. hr = pCWinNTSession->InitUmiObject(
  148. pCWinNTSession->_Credentials,
  149. SessionClass,
  150. gdwSessionTableSize,
  151. pCWinNTSession->_pPropertyCache,
  152. (IUnknown *)(INonDelegatingUnknown *) pCWinNTSession,
  153. pCWinNTSession->_pExtMgr,
  154. IID_IUnknown,
  155. ppvoid
  156. );
  157. BAIL_IF_ERROR(hr);
  158. //
  159. // UMI object was created and the interface was obtained successfully.
  160. // UMI object now has a reference to the inner unknown of IADs, since
  161. // the call to Release() below is not going to be made in this case.
  162. //
  163. RRETURN(hr);
  164. }
  165. hr = pCWinNTSession->QueryInterface(riid,
  166. (void **)ppvoid);
  167. BAIL_IF_ERROR(hr);
  168. pCWinNTSession->Release();
  169. cleanup:
  170. if(SUCCEEDED(hr)){
  171. RRETURN(hr);
  172. }
  173. delete pCWinNTSession;
  174. RRETURN_EXP_IF_ERR(hr);
  175. }
  176. HRESULT
  177. CWinNTSession::AllocateSessionObject(LPTSTR pszServerADsPath,
  178. LPTSTR pszClientName,
  179. LPTSTR pszUserName,
  180. CWinNTSession ** ppSession
  181. )
  182. {
  183. CWinNTSession FAR * pCWinNTSession = NULL;
  184. HRESULT hr = S_OK;
  185. POBJECTINFO pServerObjectInfo = NULL;
  186. pCWinNTSession = new CWinNTSession();
  187. if (pCWinNTSession == NULL) {
  188. hr = E_OUTOFMEMORY;
  189. goto cleanup;
  190. }
  191. pCWinNTSession->_pDispMgr = new CAggregatorDispMgr;
  192. if (pCWinNTSession->_pDispMgr == NULL) {
  193. hr = E_OUTOFMEMORY;
  194. goto cleanup;
  195. }
  196. hr = LoadTypeInfoEntry(pCWinNTSession->_pDispMgr,
  197. LIBID_ADs,
  198. IID_IADsSession,
  199. (IADsSession *)pCWinNTSession,
  200. DISPID_REGULAR);
  201. BAIL_IF_ERROR(hr);
  202. pCWinNTSession->_pszServerADsPath
  203. = AllocADsStr(pszServerADsPath);
  204. if(!pCWinNTSession->_pszServerADsPath){
  205. hr = E_OUTOFMEMORY;
  206. goto cleanup;
  207. }
  208. hr = BuildObjectInfo(pszServerADsPath,
  209. &pServerObjectInfo);
  210. BAIL_IF_ERROR(hr);
  211. pCWinNTSession->_pszServerName =
  212. AllocADsStr(pServerObjectInfo->ComponentArray[1]);
  213. if(!pCWinNTSession->_pszServerADsPath){
  214. hr = E_OUTOFMEMORY;
  215. goto cleanup;
  216. }
  217. if(pszClientName){
  218. pCWinNTSession->_pszComputerName = AllocADsStr(pszClientName);
  219. if(!pCWinNTSession->_pszComputerName){
  220. hr = E_OUTOFMEMORY;
  221. goto cleanup;
  222. }
  223. }
  224. if(pszUserName){
  225. pCWinNTSession->_pszUserName = AllocADsStr(pszUserName);
  226. if(!pCWinNTSession->_pszUserName){
  227. hr = E_OUTOFMEMORY;
  228. goto cleanup;
  229. }
  230. }
  231. hr = CPropertyCache::createpropertycache(
  232. SessionClass,
  233. gdwSessionTableSize,
  234. (CCoreADsObject *)pCWinNTSession,
  235. &(pCWinNTSession->_pPropertyCache)
  236. );
  237. BAIL_IF_ERROR(hr);
  238. (pCWinNTSession->_pDispMgr)->RegisterPropertyCache(
  239. pCWinNTSession->_pPropertyCache
  240. );
  241. *ppSession = pCWinNTSession;
  242. cleanup:
  243. if(pServerObjectInfo){
  244. FreeObjectInfo(pServerObjectInfo);
  245. }
  246. if (!SUCCEEDED(hr)) {
  247. //
  248. // direct memeber assignement assignement at pt of creation, so
  249. // do NOT delete _pPropertyCache or _pDisMgr here to avoid attempt
  250. // of deletion again in pPrintJob destructor and AV
  251. //
  252. delete pCWinNTSession;
  253. }
  254. RRETURN(hr);
  255. }
  256. /* IUnknown methods for session object */
  257. //----------------------------------------------------------------------------
  258. // Function: QueryInterface
  259. //
  260. // Synopsis: If this object is aggregated within another object, then
  261. // all calls will delegate to the outer object. Otherwise, the
  262. // non-delegating QI is called
  263. //
  264. // Arguments:
  265. //
  266. // iid interface requested
  267. // ppInterface Returns pointer to interface requested. NULL if interface
  268. // is not supported.
  269. //
  270. // Returns: S_OK on success. Error code otherwise.
  271. //
  272. // Modifies: *ppInterface to return interface pointer
  273. //
  274. //----------------------------------------------------------------------------
  275. STDMETHODIMP CWinNTSession::QueryInterface(
  276. REFIID iid,
  277. LPVOID *ppInterface
  278. )
  279. {
  280. if(_pUnkOuter != NULL)
  281. RRETURN(_pUnkOuter->QueryInterface(
  282. iid,
  283. ppInterface
  284. ));
  285. RRETURN(NonDelegatingQueryInterface(
  286. iid,
  287. ppInterface
  288. ));
  289. }
  290. //----------------------------------------------------------------------------
  291. // Function: AddRef
  292. //
  293. // Synopsis: IUnknown::AddRef. If this object is aggregated within
  294. // another, all calls will delegate to the outer object.
  295. // Otherwise, the non-delegating AddRef is called
  296. //
  297. // Arguments:
  298. //
  299. // None
  300. //
  301. // Returns: New reference count
  302. //
  303. // Modifies: Nothing
  304. //
  305. //----------------------------------------------------------------------------
  306. STDMETHODIMP_(ULONG) CWinNTSession::AddRef(void)
  307. {
  308. if(_pUnkOuter != NULL)
  309. RRETURN(_pUnkOuter->AddRef());
  310. RRETURN(NonDelegatingAddRef());
  311. }
  312. //----------------------------------------------------------------------------
  313. // Function: Release
  314. //
  315. // Synopsis: IUnknown::Release. If this object is aggregated within
  316. // another, all calls will delegate to the outer object.
  317. // Otherwise, the non-delegating Release is called
  318. //
  319. // Arguments:
  320. //
  321. // None
  322. //
  323. // Returns: New reference count
  324. //
  325. // Modifies: Nothing
  326. //
  327. //----------------------------------------------------------------------------
  328. STDMETHODIMP_(ULONG) CWinNTSession::Release(void)
  329. {
  330. if(_pUnkOuter != NULL)
  331. RRETURN(_pUnkOuter->Release());
  332. RRETURN(NonDelegatingRelease());
  333. }
  334. //----------------------------------------------------------------------------
  335. STDMETHODIMP
  336. CWinNTSession::NonDelegatingQueryInterface(REFIID riid, LPVOID FAR* ppvObj)
  337. {
  338. HRESULT hr = S_OK;
  339. if(!ppvObj){
  340. RRETURN(E_POINTER);
  341. }
  342. if (IsEqualIID(riid, IID_IUnknown))
  343. {
  344. *ppvObj = (IADs *) this;
  345. }
  346. else if (IsEqualIID(riid, IID_IDispatch))
  347. {
  348. *ppvObj = (IADs *)this;
  349. }
  350. else if (IsEqualIID(riid, IID_ISupportErrorInfo))
  351. {
  352. *ppvObj = (ISupportErrorInfo FAR *)this;
  353. }
  354. else if (IsEqualIID(riid, IID_IADs))
  355. {
  356. *ppvObj = (IADs FAR *) this;
  357. }
  358. else if (IsEqualIID(riid, IID_IADsPropertyList))
  359. {
  360. *ppvObj = (IADsPropertyList FAR *) this;
  361. }
  362. else if (IsEqualIID(riid, IID_IADsSession))
  363. {
  364. *ppvObj = (IADsSession FAR *) this;
  365. }
  366. else if( (_pDispatch != NULL) &&
  367. IsEqualIID(riid, IID_IADsExtension) )
  368. {
  369. *ppvObj = (IADsExtension *) this;
  370. }
  371. else if (_pExtMgr)
  372. {
  373. RRETURN( _pExtMgr->QueryInterface(riid, ppvObj));
  374. }
  375. else
  376. {
  377. *ppvObj = NULL;
  378. RRETURN(E_NOINTERFACE);
  379. }
  380. ((LPUNKNOWN)*ppvObj)->AddRef();
  381. RRETURN(S_OK);
  382. }
  383. /* ISupportErrorInfo method */
  384. STDMETHODIMP
  385. CWinNTSession::InterfaceSupportsErrorInfo(
  386. THIS_ REFIID riid
  387. )
  388. {
  389. if (IsEqualIID(riid, IID_IADs) ||
  390. IsEqualIID(riid, IID_IADsSession) ||
  391. IsEqualIID(riid, IID_IADsPropertyList)) {
  392. RRETURN(S_OK);
  393. } else {
  394. RRETURN(S_FALSE);
  395. }
  396. }
  397. /* IADs methods */
  398. //+-----------------------------------------------------------------
  399. //
  400. // Function: SetInfo
  401. //
  402. // Synopsis: SetInfo on actual session
  403. //
  404. // Arguments: void
  405. //
  406. // Returns: HRESULT.
  407. //
  408. // Modifies:
  409. //
  410. // History: 02/08/96 RamV Created
  411. //----------------------------------------------------------------------------
  412. STDMETHODIMP
  413. CWinNTSession::SetInfo(THIS)
  414. {
  415. RRETURN_EXP_IF_ERR(E_NOTIMPL);
  416. }
  417. STDMETHODIMP
  418. CWinNTSession::GetInfo(THIS_ DWORD dwApiLevel, BOOL fExplicit)
  419. {
  420. HRESULT hr;
  421. hr = GetLevel_1_Info(fExplicit);
  422. RRETURN_EXP_IF_ERR(hr);
  423. }
  424. STDMETHODIMP
  425. CWinNTSession::GetInfo(THIS)
  426. {
  427. _pPropertyCache->flushpropcache();
  428. RRETURN(GetInfo(1, TRUE));
  429. }
  430. STDMETHODIMP
  431. CWinNTSession::ImplicitGetInfo(THIS)
  432. {
  433. RRETURN(GetInfo(1, FALSE));
  434. }
  435. //
  436. // helper functions for GetInfo
  437. //
  438. STDMETHODIMP
  439. CWinNTSession::GetLevel_1_Info(THIS_ BOOL fExplicit)
  440. {
  441. NET_API_STATUS nasStatus;
  442. LPSESSION_INFO_1 lpSessionInfo =NULL;
  443. HRESULT hr;
  444. TCHAR szUncServerName[MAX_PATH];
  445. TCHAR szUncClientName[MAX_PATH];
  446. //
  447. // Level 1 info
  448. //
  449. hr = MakeUncName(_pszServerName, szUncServerName);
  450. BAIL_IF_ERROR(hr);
  451. hr = MakeUncName(_pszComputerName, szUncClientName);
  452. BAIL_IF_ERROR(hr);
  453. nasStatus = NetSessionGetInfo(szUncServerName,
  454. szUncClientName,
  455. _pszUserName,
  456. 1,
  457. (LPBYTE *)&lpSessionInfo);
  458. if (nasStatus != NERR_Success || !lpSessionInfo){
  459. hr = HRESULT_FROM_WIN32(nasStatus);
  460. goto cleanup;
  461. }
  462. //
  463. // unmarshall the info
  464. //
  465. ADsAssert(lpSessionInfo);
  466. if( _pszUserName && *_pszUserName ) {
  467. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  468. TEXT("User"),
  469. _pszUserName,
  470. fExplicit
  471. );
  472. }
  473. hr = SetLPTSTRPropertyInCache(_pPropertyCache,
  474. TEXT("Computer"),
  475. _pszComputerName,
  476. fExplicit
  477. );
  478. hr = SetDWORDPropertyInCache(_pPropertyCache,
  479. TEXT("ConnectTime"),
  480. lpSessionInfo->sesi1_time,
  481. fExplicit
  482. );
  483. hr = SetDWORDPropertyInCache(_pPropertyCache,
  484. TEXT("IdleTime"),
  485. lpSessionInfo->sesi1_idle_time,
  486. fExplicit
  487. );
  488. hr = SetLPTSTRPropertyInCache(
  489. _pPropertyCache,
  490. TEXT("Name"),
  491. _Name,
  492. fExplicit
  493. );
  494. hr = S_OK;
  495. cleanup:
  496. if(lpSessionInfo)
  497. NetApiBufferFree(lpSessionInfo);
  498. RRETURN(hr);
  499. }
  500. STDMETHODIMP
  501. CWinNTSession::get_User(THIS_ BSTR FAR* retval)
  502. {
  503. HRESULT hr;
  504. //
  505. // UserName is set once and never modified,
  506. //
  507. if(!retval){
  508. RRETURN_EXP_IF_ERR(E_ADS_BAD_PARAMETER);
  509. }
  510. hr = ADsAllocString(_pszUserName, retval);
  511. RRETURN_EXP_IF_ERR(hr);
  512. }
  513. STDMETHODIMP
  514. CWinNTSession::get_UserPath(THIS_ BSTR FAR* retval)
  515. {
  516. RRETURN_EXP_IF_ERR(E_ADS_PROPERTY_NOT_SUPPORTED);
  517. }
  518. STDMETHODIMP
  519. CWinNTSession::get_Computer(THIS_ BSTR FAR* retval)
  520. {
  521. HRESULT hr;
  522. //
  523. // Computer name is set once and never modified,
  524. //
  525. if(!retval){
  526. RRETURN_EXP_IF_ERR(E_ADS_BAD_PARAMETER);
  527. }
  528. hr = ADsAllocString(_pszComputerName, retval);
  529. RRETURN_EXP_IF_ERR(hr);
  530. }
  531. STDMETHODIMP
  532. CWinNTSession::get_ComputerPath(THIS_ BSTR FAR* retval)
  533. {
  534. RRETURN_EXP_IF_ERR(E_ADS_PROPERTY_NOT_SUPPORTED);
  535. }
  536. STDMETHODIMP
  537. CWinNTSession::get_ConnectTime(THIS_ LONG FAR* retval)
  538. {
  539. GET_PROPERTY_LONG((IADsSession *)this, ConnectTime);
  540. }
  541. STDMETHODIMP
  542. CWinNTSession::get_IdleTime(THIS_ LONG FAR* retval)
  543. {
  544. GET_PROPERTY_LONG((IADsSession *)this, IdleTime);
  545. }