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.

816 lines
19 KiB

  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. //+-------------------------------------------------------------------------
  3. //
  4. // File: linkdata.hxx
  5. //
  6. // Contents: Link tracking identity types.
  7. //
  8. // Classes:
  9. //
  10. // Functions:
  11. //
  12. //
  13. //
  14. // History: 18-Nov-96 BillMo Created.
  15. //
  16. // Notes: Implementations are in trklib.hxx
  17. //
  18. // Codework:
  19. //
  20. //--------------------------------------------------------------------------
  21. #ifndef LINKDATA_H
  22. #define LINKDATA_H
  23. #if !defined(__midl)
  24. #include <tchar.h>
  25. #endif
  26. #if !defined(__midl) && defined(__cplusplus)
  27. #ifdef UNICODE
  28. typedef WCHAR RPC_TCHAR;
  29. #define RPC_TEXT(a) TEXT(a)
  30. #else
  31. typedef unsigned char RPC_TCHAR;
  32. #define RPC_TEXT(a) ((RPC_TCHAR*)a)
  33. #endif
  34. #endif
  35. // The endpoint name used to RPC to the local trkwks service
  36. // (used by shell32!CTracker::Search)
  37. #define TRKWKS_LRPC_ENDPOINT_NAME TEXT("trkwks")
  38. #ifdef __cplusplus
  39. struct CDomainRelativeObjId;
  40. class CObjIdEnumerator;
  41. #endif
  42. //+-------------------------------------------------------------------------
  43. //
  44. // CVolumeSecret
  45. //
  46. //--------------------------------------------------------------------------
  47. typedef struct CVolumeSecret
  48. {
  49. #if !defined(__midl) && defined(__cplusplus)
  50. inline CVolumeSecret();
  51. inline operator == (const CVolumeSecret & other) const;
  52. inline operator != (const CVolumeSecret & other) const;
  53. inline void Stringize(TCHAR * &rptsz) const;
  54. friend class CEntropyRecorder;
  55. private:
  56. #endif
  57. BYTE _abSecret[8];
  58. } CVolumeSecret;
  59. //+-------------------------------------------------------------------------
  60. //
  61. // CObjId - class definition
  62. //
  63. //--------------------------------------------------------------------------
  64. #if !defined(__midl) && defined(__cplusplus)
  65. typedef enum
  66. {
  67. FOB_OBJECTID,
  68. FOB_BIRTHID
  69. } FOB_ENUM;
  70. typedef enum
  71. {
  72. FOI_OBJECTID,
  73. FOI_BIRTHID
  74. } FOI_ENUM;
  75. #endif
  76. enum LINK_TYPE_ENUM
  77. {
  78. LINK_TYPE_FILE = 1,
  79. LINK_TYPE_BIRTH
  80. };
  81. typedef struct CObjId
  82. {
  83. #if !defined(__midl) && defined(__cplusplus)
  84. public:
  85. inline CObjId();
  86. inline CObjId( const CObjId &objid );
  87. inline void Init();
  88. inline CObjId( GUID g );
  89. inline CObjId( FOB_ENUM fobe, const FILE_OBJECTID_BUFFER & fob );
  90. inline CObjId( FOI_ENUM foie, const FILE_OBJECTID_INFORMATION & foi );
  91. inline operator == (const CObjId & other) const;
  92. inline operator != (const CObjId & other) const;
  93. inline CObjId& operator = (const CObjId &objid );
  94. inline void Stringize(TCHAR * &rptsz) const;
  95. inline BOOL Unstringize(const TCHAR * &rptsz);
  96. inline DWORD SerializeRaw( BYTE *pb ) const;
  97. inline DWORD Size() const;
  98. inline RPC_STATUS UuidCreate();
  99. inline operator GUID() const;
  100. private: // debugging
  101. void DebugPrint(const TCHAR *ptszName);
  102. #endif // #if !defined(__midl) && defined(__cplusplus)
  103. GUID _object;
  104. } CObjId;
  105. #if !defined(__midl) && defined(__cplusplus)
  106. inline
  107. CObjId::CObjId()
  108. {
  109. Init();
  110. }
  111. inline
  112. CObjId::CObjId( const CObjId &objid )
  113. {
  114. *this = objid;
  115. }
  116. inline void
  117. CObjId::Init()
  118. {
  119. memset( this, 0, sizeof(*this) );
  120. }
  121. inline
  122. CObjId::CObjId( GUID g )
  123. {
  124. _object = g;
  125. }
  126. inline
  127. CObjId::operator GUID() const
  128. {
  129. return( _object );
  130. }
  131. inline
  132. CObjId::CObjId( FOB_ENUM fobe, const FILE_OBJECTID_BUFFER & fob )
  133. {
  134. memcpy( &_object,
  135. (fobe == FOB_OBJECTID ? (void*)fob.ObjectId : (void*)(fob.ExtendedInfo + sizeof(GUID)) ),
  136. sizeof(_object) );
  137. }
  138. inline
  139. CObjId::CObjId( FOI_ENUM foie, const FILE_OBJECTID_INFORMATION & foi )
  140. {
  141. memcpy( &_object,
  142. (foie == FOI_OBJECTID ? (void*)foi.ObjectId : (void*)(foi.ExtendedInfo + sizeof(GUID)) ),
  143. sizeof(_object) );
  144. }
  145. inline
  146. CObjId::operator == (const CObjId & other) const
  147. {
  148. return(memcmp(&_object, &other._object, sizeof(_object)) == 0);
  149. }
  150. inline
  151. CObjId::operator != (const CObjId & other) const
  152. {
  153. return(memcmp(&_object, &other._object, sizeof(_object)) != 0);
  154. }
  155. inline
  156. CObjId&
  157. CObjId::operator = (const CObjId &objid )
  158. {
  159. memcpy( &_object, &objid._object, sizeof(_object) );
  160. return( *this );
  161. }
  162. inline DWORD
  163. CObjId::Size() const
  164. {
  165. return( sizeof(_object) );
  166. }
  167. inline DWORD
  168. CObjId::SerializeRaw( BYTE *pb ) const
  169. {
  170. memcpy( pb, &_object, sizeof(_object) );
  171. return( Size() );
  172. }
  173. #endif // #if !defined(__midl) && defined(__cplusplus)
  174. //+-------------------------------------------------------------------------
  175. //
  176. // CVolumeId - class definition
  177. //
  178. //--------------------------------------------------------------------------
  179. typedef struct CVolumeId
  180. {
  181. #if !defined(__midl) && defined(__cplusplus)
  182. public:
  183. inline CVolumeId();
  184. inline CVolumeId( const CVolumeId &volid );
  185. inline CVolumeId( const GUID &guid );
  186. inline CVolumeId( const LINK_TRACKING_INFORMATION & StupidStructureNameForVolumeId );
  187. inline CVolumeId( const FILE_OBJECTID_BUFFER & fob );
  188. inline CVolumeId( const FILE_OBJECTID_INFORMATION & foi );
  189. inline CVolumeId( const FILE_FS_OBJECTID_INFORMATION & fsob );
  190. inline CVolumeId( const TCHAR * ptszStringizedGuid, HRESULT hr );
  191. inline void Init();
  192. inline CVolumeId & Normalize();
  193. inline BOOL GetUserBitState() const;
  194. inline void Stringize(TCHAR * & rptsz) const;
  195. inline BOOL Unstringize(const TCHAR * &rptsz);
  196. inline DWORD Size() const;
  197. inline DWORD SerializeRaw( BYTE *pb ) const;
  198. inline operator == (const CVolumeId & Other) const;
  199. inline operator != (const CVolumeId & Other) const;
  200. inline CVolumeId& operator = (const CVolumeId &Other);
  201. inline RPC_STATUS UuidCreate();
  202. inline operator FILE_FS_OBJECTID_INFORMATION () const;
  203. inline operator GUID() const;
  204. private:
  205. #endif // #if !defined(__midl) && defined(__cplusplus)
  206. GUID _volume;
  207. } CVolumeId;
  208. #if !defined(__midl) && defined(__cplusplus)
  209. inline
  210. CVolumeId&
  211. CVolumeId::operator = (const CVolumeId &Other)
  212. {
  213. memcpy( &_volume, &Other._volume, sizeof(_volume) );
  214. return( *this );
  215. }
  216. inline void
  217. CVolumeId::Init()
  218. {
  219. memset(this, 0, sizeof(*this));
  220. }
  221. inline
  222. CVolumeId::CVolumeId()
  223. {
  224. Init();
  225. }
  226. inline
  227. CVolumeId::CVolumeId( const CVolumeId &volid )
  228. {
  229. *this = volid;
  230. }
  231. inline
  232. CVolumeId::CVolumeId( const GUID &guid )
  233. {
  234. *this = *(CVolumeId*)&guid;
  235. }
  236. inline
  237. CVolumeId::operator GUID() const
  238. {
  239. return( _volume );
  240. }
  241. inline
  242. CVolumeId::CVolumeId( const LINK_TRACKING_INFORMATION & StupidStructureNameForVolumeId )
  243. {
  244. memcpy( &_volume, StupidStructureNameForVolumeId.VolumeId, sizeof(_volume) );
  245. }
  246. inline CVolumeId &
  247. CVolumeId::Normalize()
  248. {
  249. _volume.Data1 &= 0xfffffffe;
  250. return(*this);
  251. }
  252. inline
  253. CVolumeId::CVolumeId( const FILE_OBJECTID_BUFFER & fob )
  254. {
  255. memcpy(&_volume, & fob.ExtendedInfo[ 0 ], sizeof(_volume) );
  256. }
  257. inline CVolumeId::CVolumeId( const FILE_OBJECTID_INFORMATION & foi )
  258. {
  259. memcpy(&_volume, & foi.ExtendedInfo[ 0 ], sizeof(_volume) );
  260. }
  261. inline
  262. CVolumeId::CVolumeId( const FILE_FS_OBJECTID_INFORMATION & ffoi )
  263. {
  264. memcpy(&_volume, ffoi.ObjectId, sizeof(_volume));
  265. }
  266. inline DWORD
  267. CVolumeId::Size() const
  268. {
  269. return( sizeof(_volume) );
  270. }
  271. inline DWORD
  272. CVolumeId::SerializeRaw( BYTE *pb ) const
  273. {
  274. memcpy( pb, &_volume, sizeof(_volume) );
  275. return( Size() );
  276. }
  277. #endif // #if !defined(__midl) && defined(__cplusplus)
  278. //+-------------------------------------------------------------------------
  279. //
  280. // CMachineId - class definition
  281. //
  282. //--------------------------------------------------------------------------
  283. enum MCID_CREATE_TYPE
  284. {
  285. MCID_LOCAL,
  286. MCID_INVALID,
  287. MCID_DOMAIN,
  288. MCID_DOMAIN_REDISCOVERY,
  289. MCID_PDC_REQUIRED
  290. };
  291. // The computer's NetBios name is used for the machine ID. NetBios names
  292. // can contain <= 15 Ansi characters between ASCII 33 and 126 inclusive
  293. // except for the MS-DOS reserved characters such as the backward and forward slashes,
  294. // braces ({}), the greater than and less than signs, and so forth.
  295. typedef struct CMachineId
  296. {
  297. #if !defined(__midl) && defined(__cplusplus)
  298. inline CMachineId();
  299. inline CMachineId( const CMachineId &mcid );
  300. inline CMachineId(const char * pb, ULONG cb, HRESULT hr);
  301. inline CMachineId(GUID g);
  302. CMachineId(MCID_CREATE_TYPE type);
  303. CMachineId(handle_t ClientBinding);
  304. CMachineId(const TCHAR * ptszMachine);
  305. inline HRESULT InitFromPath(const TCHAR * ptszPath, HANDLE hFile);
  306. inline operator == (const CMachineId &other) const;
  307. inline operator > (const CMachineId &other) const;
  308. inline operator != (const CMachineId &other) const;
  309. inline void Stringize(TCHAR * &rptsz) const;
  310. inline void StringizeAsGuid(TCHAR * &rptsz) const;
  311. void GetLocalAuthName(RPC_TCHAR *ptszAuthName, DWORD cch) const;
  312. inline void GetName(TCHAR * ptsz, DWORD cch) const;
  313. inline BOOL IsTerminated() const;
  314. inline void RaiseIfInvalid() const;
  315. inline void Normalize();
  316. #if DBG
  317. void AssertValid();
  318. #else
  319. inline void AssertValid() {};
  320. #endif
  321. friend class CDebugString;
  322. private:
  323. #endif // #if !defined(__midl) && defined(__cplusplus)
  324. char _szMachine[16];
  325. } CMachineId;
  326. #if !defined(__midl) && defined(__cplusplus)
  327. inline
  328. CMachineId::CMachineId()
  329. {
  330. memset(this, 0, sizeof(*this));
  331. }
  332. inline
  333. CMachineId::CMachineId( const CMachineId &mcid )
  334. {
  335. *this = mcid;
  336. }
  337. inline
  338. CMachineId::CMachineId( GUID g )
  339. {
  340. memcpy( _szMachine, &g, sizeof(g) );
  341. Normalize();
  342. }
  343. inline void
  344. CMachineId::Normalize()
  345. {
  346. NTSTATUS status;
  347. WCHAR UnicodeStringBuffer[ sizeof(_szMachine) ] = { L'\0' };
  348. WCHAR DowncaseUnicodeStringBuffer[ sizeof(_szMachine) ] = { L'\0' };
  349. UNICODE_STRING UnicodeString;
  350. UNICODE_STRING DowncaseUnicodeString;
  351. OEM_STRING OemString;
  352. // Make sure the string is terminated.
  353. _szMachine[sizeof(_szMachine)-1] = '\0';
  354. // Convert the OEM string to Unicode
  355. RtlInitString( &OemString, _szMachine );
  356. RtlInitUnicodeString( &UnicodeString, UnicodeStringBuffer );
  357. UnicodeString.MaximumLength = sizeof(UnicodeStringBuffer);
  358. status = RtlOemStringToUnicodeString( &UnicodeString,
  359. &OemString,
  360. FALSE );
  361. if( NT_SUCCESS(status) )
  362. {
  363. // Lower-case the Unicode string.
  364. RtlInitUnicodeString( &DowncaseUnicodeString, DowncaseUnicodeStringBuffer );
  365. DowncaseUnicodeString.MaximumLength = sizeof(DowncaseUnicodeStringBuffer);
  366. status = RtlDowncaseUnicodeString( &DowncaseUnicodeString,
  367. &UnicodeString,
  368. FALSE );
  369. if( NT_SUCCESS(status) )
  370. {
  371. // Convert back to OEM.
  372. status = RtlUnicodeStringToOemString( &OemString,
  373. &DowncaseUnicodeString,
  374. FALSE );
  375. if( NT_SUCCESS(status) )
  376. {
  377. memcpy( _szMachine, OemString.Buffer, OemString.Length );
  378. }
  379. }
  380. }
  381. // For backwards compatibility, if the above fails for
  382. // some reason, make sure we make the best attempt.
  383. if( !NT_SUCCESS(status) )
  384. {
  385. for( int i = 0; i < sizeof(_szMachine)-1; i++ )
  386. {
  387. if( _szMachine[i] >= 'A' && _szMachine[i] <= 'Z' )
  388. _szMachine[i] = 'a' + (_szMachine[i] - 'A');
  389. }
  390. }
  391. }
  392. inline CMachineId::operator == (const CMachineId &other) const
  393. {
  394. return( 0 == memcmp( &_szMachine, &other._szMachine, sizeof(_szMachine) ) );
  395. }
  396. inline CMachineId::operator > (const CMachineId &other) const
  397. {
  398. if(strncmp(_szMachine, (LPCSTR)&other, sizeof(*this)) > 0)
  399. {
  400. return TRUE;
  401. }
  402. return FALSE;
  403. }
  404. inline CMachineId::operator != (const CMachineId &other) const
  405. {
  406. return !(other == *this);
  407. }
  408. HRESULT
  409. GetServerComputerName(
  410. const WCHAR *pwszFile,
  411. HANDLE hFile,
  412. WCHAR *pwszComputer );
  413. inline HRESULT
  414. CMachineId::InitFromPath(const TCHAR * ptszPath, HANDLE hFile)
  415. {
  416. HRESULT hr = S_OK;
  417. TCHAR tszMachine[ MAX_COMPUTERNAME_LENGTH + 1 ];
  418. int nReturn;
  419. // Get the server name for this path
  420. hr = GetServerComputerName( ptszPath, hFile, tszMachine );
  421. if( FAILED(hr) )
  422. return( hr );
  423. // Convert the machine name from Unicode into Ansi, and put it into
  424. // the caller-provided machineid object.
  425. #ifdef UNICODE
  426. //wcstombs(_szMachine, tszMachine, sizeof(_szMachine));
  427. nReturn = WideCharToMultiByte( CP_OEMCP, 0,
  428. tszMachine, -1,
  429. _szMachine, sizeof(_szMachine),
  430. NULL, NULL );
  431. if( 0 == nReturn )
  432. return HRESULT_FROM_WIN32( GetLastError() );
  433. #else
  434. strcpy(_szMachine, tszMachine);
  435. #endif
  436. Normalize();
  437. return( S_OK );
  438. }
  439. #endif // #if !defined(__midl) && defined(__cplusplus)
  440. //+-------------------------------------------------------------------------
  441. //
  442. // CDomainRelativeObjId - class definition
  443. //
  444. //--------------------------------------------------------------------------
  445. enum TCL_ENUM
  446. {
  447. TCL_SET_BIRTHID,
  448. TCL_READ_BIRTHID
  449. };
  450. typedef struct CDomainRelativeObjId // droid
  451. {
  452. #if !defined(__midl) && defined(__cplusplus)
  453. inline CDomainRelativeObjId();
  454. inline CDomainRelativeObjId( const CDomainRelativeObjId &droid );
  455. inline CDomainRelativeObjId(const CVolumeId &volume, const CObjId &object);
  456. inline CDomainRelativeObjId( const FILE_OBJECTID_BUFFER & fobBirthId );
  457. inline CDomainRelativeObjId( const FILE_OBJECTID_INFORMATION & foiBirthId );
  458. inline CDomainRelativeObjId(const TCHAR * ptszTest);
  459. inline void DebugPrint(const TCHAR *ptszName);
  460. inline void Init();
  461. inline NTSTATUS InitFromFile( HANDLE hFile, const FILE_OBJECTID_BUFFER & fobOID ); // Current DROID
  462. inline void InitFromFOB( const FILE_OBJECTID_BUFFER & fobOID ); // Birth DROID
  463. inline void InitFromFOI( const FILE_OBJECTID_INFORMATION & foiOID ); // Birth DROID
  464. TCHAR* Stringize(TCHAR *ptszBuf, DWORD dwBufSize ) const;
  465. inline DWORD Size() const;
  466. inline DWORD SerializeRaw( BYTE *pb ) const;
  467. inline operator == (const CDomainRelativeObjId & Other) const;
  468. inline operator != (const CDomainRelativeObjId & Other) const;
  469. inline CDomainRelativeObjId& operator = (const CDomainRelativeObjId & Other);
  470. void InitFromLdapBuffer(char * pVolumeId, int cbVolumeId, char * pObjId, int cbObjectId);
  471. static NTSTATUS Load( CDomainRelativeObjId *pdroid, HANDLE hFile, FILE_OBJECTID_BUFFER fobOID, LINK_TYPE_ENUM LinkType );
  472. void FillLdapIdtKeyBuffer(TCHAR * pchCN, DWORD dwBufSize) const;
  473. void ReadLdapIdtKeyBuffer(const TCHAR *pchCN );
  474. inline CVolumeId & GetVolumeId() { return _volume; }
  475. inline const CVolumeId & GetVolumeId() const { return _volume; }
  476. inline const CObjId & GetObjId() const { return _object; }
  477. private:
  478. #endif // #if !defined(__midl) && defined(__cplusplus)
  479. CVolumeId _volume;
  480. CObjId _object;
  481. } CDomainRelativeObjId;
  482. #if !defined(__midl) && defined(__cplusplus)
  483. inline
  484. CDomainRelativeObjId&
  485. CDomainRelativeObjId::operator = (const CDomainRelativeObjId & Other)
  486. {
  487. _volume = Other._volume;
  488. _object = Other._object;
  489. return( *this );
  490. }
  491. inline
  492. CDomainRelativeObjId::CDomainRelativeObjId()
  493. {
  494. }
  495. inline
  496. CDomainRelativeObjId::CDomainRelativeObjId( const CDomainRelativeObjId &droid )
  497. {
  498. *this = droid;
  499. }
  500. inline
  501. CDomainRelativeObjId::CDomainRelativeObjId(const CVolumeId &volume, const CObjId &object) :
  502. _volume(volume),
  503. _object(object)
  504. {
  505. }
  506. inline
  507. CDomainRelativeObjId::CDomainRelativeObjId( const FILE_OBJECTID_BUFFER & fobBirthId ) :
  508. _volume( fobBirthId ),
  509. _object( FOB_BIRTHID, fobBirthId )
  510. {
  511. }
  512. inline
  513. CDomainRelativeObjId::CDomainRelativeObjId( const FILE_OBJECTID_INFORMATION & foiBirthId ) :
  514. _volume( foiBirthId ),
  515. _object( FOI_BIRTHID, foiBirthId )
  516. {
  517. }
  518. inline DWORD
  519. CDomainRelativeObjId::Size() const
  520. {
  521. return( _object.Size() + _volume.Size() );
  522. }
  523. inline DWORD
  524. CDomainRelativeObjId::SerializeRaw( BYTE *pb ) const
  525. {
  526. // Serialize the volid, then the objid
  527. _object.SerializeRaw( &pb[ _volume.SerializeRaw(pb) ] );
  528. return( Size() );
  529. }
  530. NTSTATUS
  531. CDomainRelativeObjId::InitFromFile( HANDLE hFile,
  532. const FILE_OBJECTID_BUFFER & fobOID )
  533. {
  534. NTSTATUS status = STATUS_SUCCESS;
  535. IO_STATUS_BLOCK Iosb;
  536. FILE_FS_OBJECTID_INFORMATION fsobOID;
  537. status = NtQueryVolumeInformationFile( hFile, &Iosb, &fsobOID, sizeof(fsobOID),
  538. FileFsObjectIdInformation );
  539. if( STATUS_OBJECT_NAME_NOT_FOUND == status )
  540. {
  541. memset( &fsobOID, 0, sizeof(fsobOID) );
  542. status = STATUS_SUCCESS;
  543. }
  544. else if( !NT_SUCCESS(status) ) goto Exit;
  545. _volume = CVolumeId( fsobOID );
  546. _object = CObjId( FOB_OBJECTID, fobOID );
  547. Exit:
  548. return( status );
  549. }
  550. inline
  551. void
  552. CDomainRelativeObjId::InitFromFOB( const FILE_OBJECTID_BUFFER & fobOID )
  553. {
  554. _volume = CVolumeId( fobOID );
  555. _object = CObjId( FOB_BIRTHID, fobOID );
  556. }
  557. inline
  558. void
  559. CDomainRelativeObjId::InitFromFOI( const FILE_OBJECTID_INFORMATION & foiOID )
  560. {
  561. _volume = CVolumeId( foiOID );
  562. _object = CObjId( FOI_BIRTHID, foiOID );
  563. }
  564. #endif // #if !defined(__midl) && defined(__cplusplus)
  565. #if !defined(__midl) && defined(__cplusplus)
  566. struct SAllIDs
  567. {
  568. inline SAllIDs() {};
  569. inline SAllIDs( const CDomainRelativeObjId &droidBirthInit,
  570. const CDomainRelativeObjId &droidCurrentInit,
  571. const CMachineId &mcidInit );
  572. CDomainRelativeObjId droidBirth;
  573. CDomainRelativeObjId droidRevised;
  574. CMachineId mcid;
  575. };
  576. inline
  577. SAllIDs::SAllIDs( const CDomainRelativeObjId &droidBirthInit,
  578. const CDomainRelativeObjId &droidRevisedInit,
  579. const CMachineId &mcidInit )
  580. {
  581. droidBirth = droidBirthInit;
  582. droidRevised = droidRevisedInit;
  583. mcid = mcidInit;
  584. }
  585. #endif // #if !defined(__midl) && defined(__cplusplus)
  586. //+-------------------------------------------------------------------------
  587. //
  588. // VolumeMapEntry
  589. //
  590. //--------------------------------------------------------------------------
  591. typedef struct
  592. {
  593. CVolumeId volume;
  594. CMachineId machine;
  595. } VolumeMapEntry;
  596. #endif // #ifndef LINKDATA_H