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.

815 lines
18 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 IsValid() const;
  314. inline void Normalize();
  315. #if DBG
  316. void AssertValid();
  317. #else
  318. inline void AssertValid() {};
  319. #endif
  320. friend class CDebugString;
  321. private:
  322. #endif // #if !defined(__midl) && defined(__cplusplus)
  323. char _szMachine[16];
  324. } CMachineId;
  325. #if !defined(__midl) && defined(__cplusplus)
  326. inline
  327. CMachineId::CMachineId()
  328. {
  329. memset(this, 0, sizeof(*this));
  330. }
  331. inline
  332. CMachineId::CMachineId( const CMachineId &mcid )
  333. {
  334. *this = mcid;
  335. }
  336. inline
  337. CMachineId::CMachineId( GUID g )
  338. {
  339. memcpy( _szMachine, &g, sizeof(g) );
  340. Normalize();
  341. }
  342. inline void
  343. CMachineId::Normalize()
  344. {
  345. NTSTATUS status;
  346. WCHAR UnicodeStringBuffer[ sizeof(_szMachine) ] = { L'\0' };
  347. WCHAR DowncaseUnicodeStringBuffer[ sizeof(_szMachine) ] = { L'\0' };
  348. UNICODE_STRING UnicodeString;
  349. UNICODE_STRING DowncaseUnicodeString;
  350. OEM_STRING OemString;
  351. // Make sure the string is terminated.
  352. _szMachine[sizeof(_szMachine)-1] = '\0';
  353. // Convert the OEM string to Unicode
  354. RtlInitString( &OemString, _szMachine );
  355. RtlInitUnicodeString( &UnicodeString, UnicodeStringBuffer );
  356. UnicodeString.MaximumLength = sizeof(UnicodeStringBuffer);
  357. status = RtlOemStringToUnicodeString( &UnicodeString,
  358. &OemString,
  359. FALSE );
  360. if( NT_SUCCESS(status) )
  361. {
  362. // Lower-case the Unicode string.
  363. RtlInitUnicodeString( &DowncaseUnicodeString, DowncaseUnicodeStringBuffer );
  364. DowncaseUnicodeString.MaximumLength = sizeof(DowncaseUnicodeStringBuffer);
  365. status = RtlDowncaseUnicodeString( &DowncaseUnicodeString,
  366. &UnicodeString,
  367. FALSE );
  368. if( NT_SUCCESS(status) )
  369. {
  370. // Convert back to OEM.
  371. status = RtlUnicodeStringToOemString( &OemString,
  372. &DowncaseUnicodeString,
  373. FALSE );
  374. if( NT_SUCCESS(status) )
  375. {
  376. memcpy( _szMachine, OemString.Buffer, OemString.Length );
  377. }
  378. }
  379. }
  380. // For backwards compatibility, if the above fails for
  381. // some reason, make sure we make the best attempt.
  382. if( !NT_SUCCESS(status) )
  383. {
  384. for( int i = 0; i < sizeof(_szMachine)-1; i++ )
  385. {
  386. if( _szMachine[i] >= 'A' && _szMachine[i] <= 'Z' )
  387. _szMachine[i] = 'a' + (_szMachine[i] - 'A');
  388. }
  389. }
  390. }
  391. inline CMachineId::operator == (const CMachineId &other) const
  392. {
  393. return( 0 == memcmp( &_szMachine, &other._szMachine, sizeof(_szMachine) ) );
  394. }
  395. inline CMachineId::operator > (const CMachineId &other) const
  396. {
  397. if(strncmp(_szMachine, (LPCSTR)&other, sizeof(*this)) > 0)
  398. {
  399. return TRUE;
  400. }
  401. return FALSE;
  402. }
  403. inline CMachineId::operator != (const CMachineId &other) const
  404. {
  405. return !(other == *this);
  406. }
  407. HRESULT
  408. GetServerComputerName(
  409. const WCHAR *pwszFile,
  410. HANDLE hFile,
  411. WCHAR *pwszComputer );
  412. inline HRESULT
  413. CMachineId::InitFromPath(const TCHAR * ptszPath, HANDLE hFile)
  414. {
  415. HRESULT hr = S_OK;
  416. TCHAR tszMachine[ MAX_COMPUTERNAME_LENGTH + 1 ];
  417. int nReturn;
  418. // Get the server name for this path
  419. hr = GetServerComputerName( ptszPath, hFile, tszMachine );
  420. if( FAILED(hr) )
  421. return( hr );
  422. // Convert the machine name from Unicode into Ansi, and put it into
  423. // the caller-provided machineid object.
  424. #ifdef UNICODE
  425. //wcstombs(_szMachine, tszMachine, sizeof(_szMachine));
  426. nReturn = WideCharToMultiByte( CP_OEMCP, 0,
  427. tszMachine, -1,
  428. _szMachine, sizeof(_szMachine),
  429. NULL, NULL );
  430. if( 0 == nReturn )
  431. return HRESULT_FROM_WIN32( GetLastError() );
  432. #else
  433. strcpy(_szMachine, tszMachine);
  434. #endif
  435. Normalize();
  436. return( S_OK );
  437. }
  438. #endif // #if !defined(__midl) && defined(__cplusplus)
  439. //+-------------------------------------------------------------------------
  440. //
  441. // CDomainRelativeObjId - class definition
  442. //
  443. //--------------------------------------------------------------------------
  444. enum TCL_ENUM
  445. {
  446. TCL_SET_BIRTHID,
  447. TCL_READ_BIRTHID
  448. };
  449. typedef struct CDomainRelativeObjId // droid
  450. {
  451. #if !defined(__midl) && defined(__cplusplus)
  452. inline CDomainRelativeObjId();
  453. inline CDomainRelativeObjId( const CDomainRelativeObjId &droid );
  454. inline CDomainRelativeObjId(const CVolumeId &volume, const CObjId &object);
  455. inline CDomainRelativeObjId( const FILE_OBJECTID_BUFFER & fobBirthId );
  456. inline CDomainRelativeObjId( const FILE_OBJECTID_INFORMATION & foiBirthId );
  457. inline CDomainRelativeObjId(const TCHAR * ptszTest);
  458. inline void DebugPrint(const TCHAR *ptszName);
  459. inline void Init();
  460. inline NTSTATUS InitFromFile( HANDLE hFile, const FILE_OBJECTID_BUFFER & fobOID ); // Current DROID
  461. inline void InitFromFOB( const FILE_OBJECTID_BUFFER & fobOID ); // Birth DROID
  462. inline void InitFromFOI( const FILE_OBJECTID_INFORMATION & foiOID ); // Birth DROID
  463. TCHAR* Stringize(TCHAR *ptszBuf, DWORD dwBufSize ) const;
  464. inline DWORD Size() const;
  465. inline DWORD SerializeRaw( BYTE *pb ) const;
  466. inline operator == (const CDomainRelativeObjId & Other) const;
  467. inline operator != (const CDomainRelativeObjId & Other) const;
  468. inline CDomainRelativeObjId& operator = (const CDomainRelativeObjId & Other);
  469. void InitFromLdapBuffer(char * pVolumeId, int cbVolumeId, char * pObjId, int cbObjectId);
  470. static NTSTATUS Load( CDomainRelativeObjId *pdroid, HANDLE hFile, FILE_OBJECTID_BUFFER fobOID, LINK_TYPE_ENUM LinkType );
  471. void FillLdapIdtKeyBuffer(TCHAR * pchCN, DWORD dwBufSize) const;
  472. void ReadLdapIdtKeyBuffer(const TCHAR *pchCN );
  473. inline CVolumeId & GetVolumeId() { return _volume; }
  474. inline const CVolumeId & GetVolumeId() const { return _volume; }
  475. inline const CObjId & GetObjId() const { return _object; }
  476. private:
  477. #endif // #if !defined(__midl) && defined(__cplusplus)
  478. CVolumeId _volume;
  479. CObjId _object;
  480. } CDomainRelativeObjId;
  481. #if !defined(__midl) && defined(__cplusplus)
  482. inline
  483. CDomainRelativeObjId&
  484. CDomainRelativeObjId::operator = (const CDomainRelativeObjId & Other)
  485. {
  486. _volume = Other._volume;
  487. _object = Other._object;
  488. return( *this );
  489. }
  490. inline
  491. CDomainRelativeObjId::CDomainRelativeObjId()
  492. {
  493. }
  494. inline
  495. CDomainRelativeObjId::CDomainRelativeObjId( const CDomainRelativeObjId &droid )
  496. {
  497. *this = droid;
  498. }
  499. inline
  500. CDomainRelativeObjId::CDomainRelativeObjId(const CVolumeId &volume, const CObjId &object) :
  501. _volume(volume),
  502. _object(object)
  503. {
  504. }
  505. inline
  506. CDomainRelativeObjId::CDomainRelativeObjId( const FILE_OBJECTID_BUFFER & fobBirthId ) :
  507. _volume( fobBirthId ),
  508. _object( FOB_BIRTHID, fobBirthId )
  509. {
  510. }
  511. inline
  512. CDomainRelativeObjId::CDomainRelativeObjId( const FILE_OBJECTID_INFORMATION & foiBirthId ) :
  513. _volume( foiBirthId ),
  514. _object( FOI_BIRTHID, foiBirthId )
  515. {
  516. }
  517. inline DWORD
  518. CDomainRelativeObjId::Size() const
  519. {
  520. return( _object.Size() + _volume.Size() );
  521. }
  522. inline DWORD
  523. CDomainRelativeObjId::SerializeRaw( BYTE *pb ) const
  524. {
  525. // Serialize the volid, then the objid
  526. _object.SerializeRaw( &pb[ _volume.SerializeRaw(pb) ] );
  527. return( Size() );
  528. }
  529. NTSTATUS
  530. CDomainRelativeObjId::InitFromFile( HANDLE hFile,
  531. const FILE_OBJECTID_BUFFER & fobOID )
  532. {
  533. NTSTATUS status = STATUS_SUCCESS;
  534. IO_STATUS_BLOCK Iosb;
  535. FILE_FS_OBJECTID_INFORMATION fsobOID;
  536. status = NtQueryVolumeInformationFile( hFile, &Iosb, &fsobOID, sizeof(fsobOID),
  537. FileFsObjectIdInformation );
  538. if( STATUS_OBJECT_NAME_NOT_FOUND == status )
  539. {
  540. memset( &fsobOID, 0, sizeof(fsobOID) );
  541. status = STATUS_SUCCESS;
  542. }
  543. else if( !NT_SUCCESS(status) ) goto Exit;
  544. _volume = CVolumeId( fsobOID );
  545. _object = CObjId( FOB_OBJECTID, fobOID );
  546. Exit:
  547. return( status );
  548. }
  549. inline
  550. void
  551. CDomainRelativeObjId::InitFromFOB( const FILE_OBJECTID_BUFFER & fobOID )
  552. {
  553. _volume = CVolumeId( fobOID );
  554. _object = CObjId( FOB_BIRTHID, fobOID );
  555. }
  556. inline
  557. void
  558. CDomainRelativeObjId::InitFromFOI( const FILE_OBJECTID_INFORMATION & foiOID )
  559. {
  560. _volume = CVolumeId( foiOID );
  561. _object = CObjId( FOI_BIRTHID, foiOID );
  562. }
  563. #endif // #if !defined(__midl) && defined(__cplusplus)
  564. #if !defined(__midl) && defined(__cplusplus)
  565. struct SAllIDs
  566. {
  567. inline SAllIDs() {};
  568. inline SAllIDs( const CDomainRelativeObjId &droidBirthInit,
  569. const CDomainRelativeObjId &droidCurrentInit,
  570. const CMachineId &mcidInit );
  571. CDomainRelativeObjId droidBirth;
  572. CDomainRelativeObjId droidRevised;
  573. CMachineId mcid;
  574. };
  575. inline
  576. SAllIDs::SAllIDs( const CDomainRelativeObjId &droidBirthInit,
  577. const CDomainRelativeObjId &droidRevisedInit,
  578. const CMachineId &mcidInit )
  579. {
  580. droidBirth = droidBirthInit;
  581. droidRevised = droidRevisedInit;
  582. mcid = mcidInit;
  583. }
  584. #endif // #if !defined(__midl) && defined(__cplusplus)
  585. //+-------------------------------------------------------------------------
  586. //
  587. // VolumeMapEntry
  588. //
  589. //--------------------------------------------------------------------------
  590. typedef struct
  591. {
  592. CVolumeId volume;
  593. CMachineId machine;
  594. } VolumeMapEntry;
  595. #endif // #ifndef LINKDATA_H