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.

1846 lines
40 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. security.c
  5. Abstract:
  6. This module provides security for the service.
  7. Author:
  8. Wesley Witt (wesw) 2-Dec-1996
  9. Revision History:
  10. --*/
  11. #include "faxsvc.h"
  12. #pragma hdrstop
  13. //
  14. // do this to avoid dragging in ntrtl.h since we already include some stuff
  15. // from ntrtl.h
  16. //
  17. NTSYSAPI
  18. BOOLEAN
  19. NTAPI
  20. RtlValidRelativeSecurityDescriptor (
  21. IN PSECURITY_DESCRIPTOR SecurityDescriptorInput,
  22. IN ULONG SecurityDescriptorLength,
  23. IN SECURITY_INFORMATION RequiredInformation
  24. );
  25. //
  26. // Number of ACEs currently defined for fax.
  27. //
  28. #define FAX_ACE_COUNT 26
  29. typedef struct _FAX_SECURITY {
  30. LPCTSTR RegKey;
  31. PSECURITY_DESCRIPTOR SecurityDescriptor;
  32. PGENERIC_MAPPING GenericMapping;
  33. DWORD AceCount;
  34. DWORD AceIdx[FAX_ACE_COUNT];
  35. DWORD StringResource; // Resource Id of friendly name
  36. } FAX_SECURITY, *PFAX_SECURITY;
  37. typedef struct _ACE_DATA {
  38. ACCESS_MASK AccessMask;
  39. PSID *Sid;
  40. UCHAR AceType;
  41. UCHAR AceFlags;
  42. } ACE_DATA, *PACE_DATA;
  43. typedef struct _ACE {
  44. ACE_HEADER Header;
  45. ACCESS_MASK Mask;
  46. //
  47. // The SID follows in the buffer
  48. //
  49. } ACE, *PACE;
  50. //
  51. // Universal well known SIDs
  52. //
  53. PSID NullSid;
  54. PSID WorldSid;
  55. PSID LocalSid;
  56. PSID CreatorOwnerSid;
  57. PSID CreatorGroupSid;
  58. //
  59. // SIDs defined by NT
  60. //
  61. PSID DialupSid;
  62. PSID NetworkSid;
  63. PSID BatchSid;
  64. PSID InteractiveSid;
  65. PSID ServiceSid;
  66. PSID LocalSystemSid;
  67. PSID AliasAdminsSid;
  68. PSID AliasUsersSid;
  69. PSID AliasGuestsSid;
  70. PSID AliasPowerUsersSid;
  71. PSID AliasAccountOpsSid;
  72. PSID AliasSystemOpsSid;
  73. PSID AliasPrintOpsSid;
  74. PSID AliasBackupOpsSid;
  75. PSID AliasReplicatorSid;
  76. // Note - Georgeje
  77. //
  78. // The number of security descriptors has been reduced from six
  79. // to one. The tables in security.c have been left in place in
  80. // case we need to add more security descriptos later.
  81. GENERIC_MAPPING FaxGenericMapping[] =
  82. {
  83. { STANDARD_RIGHTS_READ,
  84. STANDARD_RIGHTS_WRITE,
  85. STANDARD_RIGHTS_EXECUTE,
  86. STANDARD_RIGHTS_REQUIRED
  87. }
  88. };
  89. //
  90. // Indexes for the ACE Data Array:
  91. //
  92. // ACE 0 - unused
  93. // ACE 1 - ADMIN Full
  94. // ACE 2 - ADMIN Read
  95. // ACE 3 - ADMIN Read Write
  96. // ACE 4 - ADMIN Read Write Delete
  97. // ACE 5 - Creator Full
  98. // ACE 6 - Creator Read Write
  99. // ACE 7 - World Full
  100. // ACE 8 - World Read
  101. // ACE 9 - World Read Write
  102. // ACE 10 - World Read Write Delete
  103. // ACE 11 - PowerUser Full
  104. // ACE 12 - PowerUser Read Write
  105. // ACE 13 - PowerUser Read Write Delete
  106. // ACE 14 - System Ops Full
  107. // ACE 15 - System Ops Read Write
  108. // ACE 16 - System Ops Read Write Delete
  109. // ACE 17 - System Full
  110. // ACE 18 - System Read Write
  111. // ACE 19 - System Read
  112. // ACE 20 - ADMIN Read Write Execute
  113. // ACE 21 - Interactive User Full
  114. // ACE 22 - Interactive User Read
  115. // ACE 23 - Interactive User Read Write
  116. // ACE 24 - Interactive User Read Write Delete
  117. // ACE 25 - Normal Users Read / Write
  118. //
  119. FAX_SECURITY FaxSecurity[] =
  120. {
  121. { REGVAL_CONFIG_SET, NULL, &FaxGenericMapping[0], 5, {1,5,9,11,17}, IDS_SET_CONFIG }
  122. };
  123. #define FaxSecurityCount (sizeof(FaxSecurity)/sizeof(FAX_SECURITY))
  124. //
  125. // Array of ACEs to be applied to fax. They will be
  126. // initialized during program startup based on the data in the
  127. // FaxAceDataTable. The index of each element corresponds to the
  128. // ordinals used in the [ACL] section of perms.inf.
  129. //
  130. PACE AcesForFax[FAX_ACE_COUNT];
  131. //
  132. // Array that contains the size of each ACE in the
  133. // array AceSizesForFax. These sizes are needed
  134. // in order to allocate a buffer of the right size
  135. // when we build an ACL.
  136. //
  137. ULONG AceSizesForFax[FAX_ACE_COUNT];
  138. //
  139. // Table describing the data to put into each ACE for fax.
  140. //
  141. // This table will be read during initialization and used to construct a
  142. // series of ACEs. The index of each ACE in the FaxAces array defined below
  143. // corresponds to fax ordinals used in the ACL section of perms.inf
  144. //
  145. ACE_DATA AceDataTableForFax[FAX_ACE_COUNT] = {
  146. //
  147. // Index 0 is unused
  148. //
  149. { 0,NULL,0,0 },
  150. //
  151. //
  152. // ACE 1 - ADMIN Full
  153. // (for fax)
  154. //
  155. {
  156. FAX_ALL_ACCESS,
  157. &AliasAdminsSid,
  158. ACCESS_ALLOWED_ACE_TYPE,
  159. CONTAINER_INHERIT_ACE
  160. },
  161. //
  162. // ACE 2 - ADMIN Read
  163. // (for fax)
  164. //
  165. {
  166. FAX_READ,
  167. &AliasAdminsSid,
  168. ACCESS_ALLOWED_ACE_TYPE,
  169. CONTAINER_INHERIT_ACE
  170. },
  171. //
  172. // ACE 3 - ADMIN Read Write
  173. // (for fax)
  174. //
  175. {
  176. FAX_READ | FAX_WRITE,
  177. &AliasAdminsSid,
  178. ACCESS_ALLOWED_ACE_TYPE,
  179. CONTAINER_INHERIT_ACE
  180. },
  181. //
  182. // ACE 4 - ADMIN Read Write Delete
  183. // (for fax)
  184. //
  185. {
  186. FAX_READ | FAX_WRITE | FAX_JOB_MANAGE,
  187. &AliasAdminsSid,
  188. ACCESS_ALLOWED_ACE_TYPE,
  189. CONTAINER_INHERIT_ACE
  190. },
  191. //
  192. // ACE 5 - Creator Full
  193. // (for fax)
  194. //
  195. {
  196. FAX_ALL_ACCESS,
  197. &CreatorOwnerSid,
  198. ACCESS_ALLOWED_ACE_TYPE,
  199. CONTAINER_INHERIT_ACE
  200. },
  201. //
  202. // ACE 6 - Creator Read Write
  203. // (for fax)
  204. //
  205. {
  206. FAX_READ | FAX_WRITE,
  207. &CreatorOwnerSid,
  208. ACCESS_ALLOWED_ACE_TYPE,
  209. CONTAINER_INHERIT_ACE
  210. },
  211. //
  212. // ACE 7 - World Full
  213. // (for fax)
  214. //
  215. {
  216. FAX_ALL_ACCESS,
  217. &WorldSid,
  218. ACCESS_ALLOWED_ACE_TYPE,
  219. CONTAINER_INHERIT_ACE
  220. },
  221. //
  222. // ACE 8 - World Read
  223. // (for fax)
  224. //
  225. {
  226. FAX_READ,
  227. &WorldSid,
  228. ACCESS_ALLOWED_ACE_TYPE,
  229. CONTAINER_INHERIT_ACE
  230. },
  231. //
  232. // ACE 9 - World Read Write
  233. // (for fax)
  234. //
  235. {
  236. FAX_READ | FAX_WRITE,
  237. &WorldSid,
  238. ACCESS_ALLOWED_ACE_TYPE,
  239. CONTAINER_INHERIT_ACE
  240. },
  241. //
  242. // ACE 10 - World Read Write Delete
  243. // (for fax)
  244. //
  245. {
  246. FAX_READ | FAX_WRITE | FAX_JOB_MANAGE,
  247. &WorldSid,
  248. ACCESS_ALLOWED_ACE_TYPE,
  249. CONTAINER_INHERIT_ACE
  250. },
  251. //
  252. // ACE 11 - PowerUser Full
  253. // (for fax)
  254. //
  255. {
  256. FAX_ALL_ACCESS,
  257. &AliasPowerUsersSid,
  258. ACCESS_ALLOWED_ACE_TYPE,
  259. CONTAINER_INHERIT_ACE
  260. },
  261. //
  262. // ACE 12 - PowerUser Read Write
  263. // (for fax)
  264. //
  265. {
  266. FAX_READ | FAX_WRITE,
  267. &AliasPowerUsersSid,
  268. ACCESS_ALLOWED_ACE_TYPE,
  269. CONTAINER_INHERIT_ACE
  270. },
  271. //
  272. // ACE 13 - PowerUser Read Write Delete
  273. // (for fax)
  274. //
  275. {
  276. FAX_READ | FAX_WRITE | FAX_JOB_MANAGE,
  277. &AliasPowerUsersSid,
  278. ACCESS_ALLOWED_ACE_TYPE,
  279. CONTAINER_INHERIT_ACE
  280. },
  281. //
  282. // ACE 14 - System Ops Full
  283. // (for fax)
  284. //
  285. {
  286. FAX_ALL_ACCESS,
  287. &AliasSystemOpsSid,
  288. ACCESS_ALLOWED_ACE_TYPE,
  289. CONTAINER_INHERIT_ACE
  290. },
  291. //
  292. // ACE 15 - System Ops Read Write
  293. // (for fax)
  294. //
  295. {
  296. FAX_READ | FAX_WRITE,
  297. &AliasSystemOpsSid,
  298. ACCESS_ALLOWED_ACE_TYPE,
  299. CONTAINER_INHERIT_ACE
  300. },
  301. //
  302. // ACE 16 - System Ops Read Write Delete
  303. // (for fax)
  304. //
  305. {
  306. FAX_READ | FAX_WRITE | FAX_JOB_MANAGE,
  307. &AliasSystemOpsSid,
  308. ACCESS_ALLOWED_ACE_TYPE,
  309. CONTAINER_INHERIT_ACE
  310. },
  311. //
  312. // ACE 17 - System Full
  313. // (for fax)
  314. //
  315. {
  316. FAX_ALL_ACCESS,
  317. &LocalSystemSid,
  318. ACCESS_ALLOWED_ACE_TYPE,
  319. CONTAINER_INHERIT_ACE
  320. },
  321. //
  322. // ACE 18 - System Read Write
  323. // (for fax)
  324. //
  325. {
  326. FAX_READ | FAX_WRITE| FAX_JOB_MANAGE,
  327. &LocalSystemSid,
  328. ACCESS_ALLOWED_ACE_TYPE,
  329. CONTAINER_INHERIT_ACE
  330. },
  331. //
  332. // ACE 19 - System Read
  333. // (for fax)
  334. //
  335. {
  336. FAX_READ,
  337. &LocalSystemSid,
  338. ACCESS_ALLOWED_ACE_TYPE,
  339. CONTAINER_INHERIT_ACE
  340. },
  341. //
  342. // ACE 20 - ADMIN Read Write Execute
  343. // (for fax)
  344. //
  345. {
  346. FAX_READ | FAX_WRITE,
  347. &AliasAdminsSid,
  348. ACCESS_ALLOWED_ACE_TYPE,
  349. CONTAINER_INHERIT_ACE
  350. },
  351. //
  352. // ACE 21 - Interactive User Full
  353. // (for fax)
  354. //
  355. {
  356. FAX_ALL_ACCESS,
  357. &InteractiveSid,
  358. ACCESS_ALLOWED_ACE_TYPE,
  359. CONTAINER_INHERIT_ACE
  360. },
  361. //
  362. // ACE 22 - Interactive User Read
  363. // (for fax)
  364. //
  365. {
  366. FAX_READ,
  367. &InteractiveSid,
  368. ACCESS_ALLOWED_ACE_TYPE,
  369. CONTAINER_INHERIT_ACE
  370. },
  371. //
  372. // ACE 23 - Interactive User Read Write
  373. // (for fax)
  374. //
  375. {
  376. FAX_READ | FAX_WRITE,
  377. &InteractiveSid,
  378. ACCESS_ALLOWED_ACE_TYPE,
  379. CONTAINER_INHERIT_ACE
  380. },
  381. //
  382. // ACE 24 - Interactive User Read Write Delete
  383. // (for fax)
  384. //
  385. {
  386. FAX_READ | FAX_WRITE| FAX_JOB_MANAGE,
  387. &InteractiveSid,
  388. ACCESS_ALLOWED_ACE_TYPE,
  389. CONTAINER_INHERIT_ACE
  390. },
  391. //
  392. // ACE 25 - Normal Users Read / Write
  393. // (for fax)
  394. //
  395. {
  396. FAX_READ | FAX_WRITE,
  397. &AliasUsersSid,
  398. ACCESS_ALLOWED_ACE_TYPE,
  399. CONTAINER_INHERIT_ACE
  400. },
  401. };
  402. static CRITICAL_SECTION CsSecurity;
  403. static BOOL CsInit = FALSE;
  404. DWORD
  405. InitializeSids(
  406. VOID
  407. );
  408. VOID
  409. TearDownSids(
  410. VOID
  411. );
  412. DWORD
  413. InitializeAces(
  414. IN OUT PACE_DATA DataTable,
  415. IN OUT PACE* AcesArray,
  416. IN OUT PULONG AceSizesArray,
  417. IN ULONG ArrayCount
  418. );
  419. VOID
  420. TearDownAces(
  421. IN OUT PACE* AcesArray,
  422. IN ULONG ArrayCount
  423. );
  424. DWORD
  425. InitializeSids(
  426. VOID
  427. )
  428. /*++
  429. Routine Description:
  430. This function initializes the global variables used by and exposed
  431. by security.
  432. Arguments:
  433. None.
  434. Return Value:
  435. Win32 error indicating outcome.
  436. --*/
  437. {
  438. SID_IDENTIFIER_AUTHORITY NullSidAuthority = SECURITY_NULL_SID_AUTHORITY;
  439. SID_IDENTIFIER_AUTHORITY WorldSidAuthority = SECURITY_WORLD_SID_AUTHORITY;
  440. SID_IDENTIFIER_AUTHORITY LocalSidAuthority = SECURITY_LOCAL_SID_AUTHORITY;
  441. SID_IDENTIFIER_AUTHORITY CreatorSidAuthority = SECURITY_CREATOR_SID_AUTHORITY;
  442. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  443. BOOL b = TRUE;
  444. //
  445. // Ensure the SIDs are in a well-known state
  446. //
  447. NullSid = NULL;
  448. WorldSid = NULL;
  449. LocalSid = NULL;
  450. CreatorOwnerSid = NULL;
  451. CreatorGroupSid = NULL;
  452. DialupSid = NULL;
  453. NetworkSid = NULL;
  454. BatchSid = NULL;
  455. InteractiveSid = NULL;
  456. ServiceSid = NULL;
  457. LocalSystemSid = NULL;
  458. AliasAdminsSid = NULL;
  459. AliasUsersSid = NULL;
  460. AliasGuestsSid = NULL;
  461. AliasPowerUsersSid = NULL;
  462. AliasAccountOpsSid = NULL;
  463. AliasSystemOpsSid = NULL;
  464. AliasPrintOpsSid = NULL;
  465. AliasBackupOpsSid = NULL;
  466. AliasReplicatorSid = NULL;
  467. //
  468. // Allocate and initialize the universal SIDs
  469. //
  470. b = b && AllocateAndInitializeSid(
  471. &NullSidAuthority,
  472. 1,
  473. SECURITY_NULL_RID,
  474. 0,0,0,0,0,0,0,
  475. &NullSid
  476. );
  477. b = b && AllocateAndInitializeSid(
  478. &WorldSidAuthority,
  479. 1,
  480. SECURITY_WORLD_RID,
  481. 0,0,0,0,0,0,0,
  482. &WorldSid
  483. );
  484. b = b && AllocateAndInitializeSid(
  485. &LocalSidAuthority,
  486. 1,
  487. SECURITY_LOCAL_RID,
  488. 0,0,0,0,0,0,0,
  489. &LocalSid
  490. );
  491. b = b && AllocateAndInitializeSid(
  492. &CreatorSidAuthority,
  493. 1,
  494. SECURITY_CREATOR_OWNER_RID,
  495. 0,0,0,0,0,0,0,
  496. &CreatorOwnerSid
  497. );
  498. b = b && AllocateAndInitializeSid(
  499. &CreatorSidAuthority,
  500. 1,
  501. SECURITY_CREATOR_GROUP_RID,
  502. 0,0,0,0,0,0,0,
  503. &CreatorGroupSid
  504. );
  505. //
  506. // Allocate and initialize the NT defined SIDs
  507. //
  508. b = b && AllocateAndInitializeSid(
  509. &NtAuthority,
  510. 1,
  511. SECURITY_DIALUP_RID,
  512. 0,0,0,0,0,0,0,
  513. &DialupSid
  514. );
  515. b = b && AllocateAndInitializeSid(
  516. &NtAuthority,
  517. 1,
  518. SECURITY_NETWORK_RID,
  519. 0,0,0,0,0,0,0,
  520. &NetworkSid
  521. );
  522. b = b && AllocateAndInitializeSid(
  523. &NtAuthority,
  524. 1,
  525. SECURITY_BATCH_RID,
  526. 0,0,0,0,0,0,0,
  527. &BatchSid
  528. );
  529. b = b && AllocateAndInitializeSid(
  530. &NtAuthority,
  531. 1,
  532. SECURITY_INTERACTIVE_RID,
  533. 0,0,0,0,0,0,0,
  534. &InteractiveSid
  535. );
  536. b = b && AllocateAndInitializeSid(
  537. &NtAuthority,
  538. 1,
  539. SECURITY_SERVICE_RID,
  540. 0,0,0,0,0,0,0,
  541. &ServiceSid
  542. );
  543. b = b && AllocateAndInitializeSid(
  544. &NtAuthority,
  545. 1,
  546. SECURITY_LOCAL_SYSTEM_RID,
  547. 0,0,0,0,0,0,0,
  548. &LocalSystemSid
  549. );
  550. b = b && AllocateAndInitializeSid(
  551. &NtAuthority,
  552. 2,
  553. SECURITY_BUILTIN_DOMAIN_RID,
  554. DOMAIN_ALIAS_RID_ADMINS,
  555. 0,0,0,0,0,0,
  556. &AliasAdminsSid
  557. );
  558. b = b && AllocateAndInitializeSid(
  559. &NtAuthority,
  560. 2,
  561. SECURITY_BUILTIN_DOMAIN_RID,
  562. DOMAIN_ALIAS_RID_USERS,
  563. 0,0,0,0,0,0,
  564. &AliasUsersSid
  565. );
  566. b = b && AllocateAndInitializeSid(
  567. &NtAuthority,
  568. 2,
  569. SECURITY_BUILTIN_DOMAIN_RID,
  570. DOMAIN_ALIAS_RID_GUESTS,
  571. 0,0,0,0,0,0,
  572. &AliasGuestsSid
  573. );
  574. b = b && AllocateAndInitializeSid(
  575. &NtAuthority,
  576. 2,
  577. SECURITY_BUILTIN_DOMAIN_RID,
  578. DOMAIN_ALIAS_RID_POWER_USERS,
  579. 0,0,0,0,0,0,
  580. &AliasPowerUsersSid
  581. );
  582. b = b && AllocateAndInitializeSid(
  583. &NtAuthority,
  584. 2,
  585. SECURITY_BUILTIN_DOMAIN_RID,
  586. DOMAIN_ALIAS_RID_ACCOUNT_OPS,
  587. 0,0,0,0,0,0,
  588. &AliasAccountOpsSid
  589. );
  590. b = b && AllocateAndInitializeSid(
  591. &NtAuthority,
  592. 2,
  593. SECURITY_BUILTIN_DOMAIN_RID,
  594. DOMAIN_ALIAS_RID_SYSTEM_OPS,
  595. 0,0,0,0,0,0,
  596. &AliasSystemOpsSid
  597. );
  598. b = b && AllocateAndInitializeSid(
  599. &NtAuthority,
  600. 2,
  601. SECURITY_BUILTIN_DOMAIN_RID,
  602. DOMAIN_ALIAS_RID_PRINT_OPS,
  603. 0,0,0,0,0,0,
  604. &AliasPrintOpsSid
  605. );
  606. b = b && AllocateAndInitializeSid(
  607. &NtAuthority,
  608. 2,
  609. SECURITY_BUILTIN_DOMAIN_RID,
  610. DOMAIN_ALIAS_RID_BACKUP_OPS,
  611. 0,0,0,0,0,0,
  612. &AliasBackupOpsSid
  613. );
  614. b = b && AllocateAndInitializeSid(
  615. &NtAuthority,
  616. 2,
  617. SECURITY_BUILTIN_DOMAIN_RID,
  618. DOMAIN_ALIAS_RID_REPLICATOR,
  619. 0,0,0,0,0,0,
  620. &AliasReplicatorSid
  621. );
  622. if(!b) {
  623. TearDownSids();
  624. }
  625. return(b ? NO_ERROR : GetLastError() ? GetLastError() : 1 );
  626. }
  627. VOID
  628. TearDownSids(
  629. VOID
  630. )
  631. {
  632. if(NullSid) {
  633. FreeSid(NullSid);
  634. }
  635. if(WorldSid) {
  636. FreeSid(WorldSid);
  637. }
  638. if(LocalSid) {
  639. FreeSid(LocalSid);
  640. }
  641. if(CreatorOwnerSid) {
  642. FreeSid(CreatorOwnerSid);
  643. }
  644. if(CreatorGroupSid) {
  645. FreeSid(CreatorGroupSid);
  646. }
  647. if(DialupSid) {
  648. FreeSid(DialupSid);
  649. }
  650. if(NetworkSid) {
  651. FreeSid(NetworkSid);
  652. }
  653. if(BatchSid) {
  654. FreeSid(BatchSid);
  655. }
  656. if(InteractiveSid) {
  657. FreeSid(InteractiveSid);
  658. }
  659. if(ServiceSid) {
  660. FreeSid(ServiceSid);
  661. }
  662. if(LocalSystemSid) {
  663. FreeSid(LocalSystemSid);
  664. }
  665. if(AliasAdminsSid) {
  666. FreeSid(AliasAdminsSid);
  667. }
  668. if(AliasUsersSid) {
  669. FreeSid(AliasUsersSid);
  670. }
  671. if(AliasGuestsSid) {
  672. FreeSid(AliasGuestsSid);
  673. }
  674. if(AliasPowerUsersSid) {
  675. FreeSid(AliasPowerUsersSid);
  676. }
  677. if(AliasAccountOpsSid) {
  678. FreeSid(AliasAccountOpsSid);
  679. }
  680. if(AliasSystemOpsSid) {
  681. FreeSid(AliasSystemOpsSid);
  682. }
  683. if(AliasPrintOpsSid) {
  684. FreeSid(AliasPrintOpsSid);
  685. }
  686. if(AliasBackupOpsSid) {
  687. FreeSid(AliasBackupOpsSid);
  688. }
  689. if(AliasReplicatorSid) {
  690. FreeSid(AliasReplicatorSid);
  691. }
  692. }
  693. DWORD
  694. InitializeAces(
  695. IN OUT PACE_DATA DataTable,
  696. IN OUT PACE* AcesArray,
  697. IN OUT PULONG AceSizesArray,
  698. IN ULONG ArrayCount
  699. )
  700. /*++
  701. Routine Description:
  702. Initializes the array of ACEs as described in the DataTable
  703. Arguments:
  704. DataTable - Pointer to the array that contains the data
  705. describing each ACE.
  706. AcesArray - Array that will contain the ACEs.
  707. AceSizesArray - Array that contains the sizes for each ACE.
  708. ArrayCount - Number of elements in each array.
  709. Return Value:
  710. Win32 error code indicating outcome.
  711. --*/
  712. {
  713. unsigned u;
  714. DWORD Length;
  715. DWORD rc;
  716. BOOL b;
  717. DWORD SidLength;
  718. //
  719. // Initialize to a known state.
  720. //
  721. ZeroMemory(AcesArray,ArrayCount*sizeof(PACE));
  722. //
  723. // Create ACEs for each item in the data table.
  724. // This involves merging the ace data with the SID data, which
  725. // are initialized in an earlier step.
  726. //
  727. for(u=1; u<ArrayCount; u++) {
  728. SidLength = GetLengthSid(*(DataTable[u].Sid));
  729. Length = SidLength + sizeof(ACE) + sizeof(ACCESS_MASK)- sizeof(ULONG);
  730. AceSizesArray[u] = Length;
  731. AcesArray[u] = MemAlloc(Length);
  732. if(!AcesArray[u]) {
  733. TearDownAces(AcesArray, ArrayCount);
  734. return(ERROR_NOT_ENOUGH_MEMORY);
  735. }
  736. AcesArray[u]->Header.AceType = DataTable[u].AceType;
  737. AcesArray[u]->Header.AceFlags = DataTable[u].AceFlags;
  738. AcesArray[u]->Header.AceSize = (WORD)Length;
  739. AcesArray[u]->Mask = DataTable[u].AccessMask;
  740. b = CopySid(
  741. SidLength, // Length - sizeof(ACE) + sizeof(ULONG),
  742. (PUCHAR)AcesArray[u] + sizeof(ACE),
  743. *(DataTable[u].Sid)
  744. );
  745. if(!b) {
  746. rc = GetLastError();
  747. TearDownAces(AcesArray, ArrayCount);
  748. return(rc);
  749. }
  750. }
  751. return(NO_ERROR);
  752. }
  753. VOID
  754. TearDownAces(
  755. IN OUT PACE* AcesArray,
  756. IN ULONG ArrayCount
  757. )
  758. /*++
  759. Routine Description:
  760. Destroys the array of ACEs as described in the DataTable
  761. Arguments:
  762. None
  763. Return Value:
  764. None
  765. --*/
  766. {
  767. unsigned u;
  768. for(u=1; u<ArrayCount; u++) {
  769. if(AcesArray[u]) {
  770. MemFree(AcesArray[u]);
  771. }
  772. }
  773. }
  774. BOOL
  775. FaxSvcAccessCheck(
  776. DWORD SecurityType,
  777. ACCESS_MASK DesiredAccess
  778. )
  779. {
  780. DWORD rc;
  781. DWORD GrantedAccess;
  782. BOOL AccessStatus;
  783. HANDLE ClientToken = NULL;
  784. BYTE PrivilegeSet[512];
  785. DWORD PrivilegeSetSize;
  786. //
  787. // sanity check
  788. //
  789. if (SecurityType >= FaxSecurityCount) {
  790. return FALSE;
  791. }
  792. //
  793. // Impersonate the client.
  794. //
  795. if ((rc = RpcImpersonateClient(NULL)) != RPC_S_OK) {
  796. return FALSE;
  797. }
  798. EnterCriticalSection( &CsSecurity );
  799. //
  800. // Open the impersonated token.
  801. //
  802. if (!OpenThreadToken( GetCurrentThread(), TOKEN_QUERY, TRUE, &ClientToken )) {
  803. rc = GetLastError();
  804. goto exit;
  805. }
  806. //
  807. // purify the access mask
  808. //
  809. MapGenericMask( &DesiredAccess, FaxSecurity[SecurityType].GenericMapping );
  810. //
  811. // Check if the client has the required access.
  812. //
  813. PrivilegeSetSize = sizeof(PrivilegeSet);
  814. if (!AccessCheck( FaxSecurity[SecurityType].SecurityDescriptor,
  815. ClientToken,
  816. DesiredAccess,
  817. FaxSecurity[SecurityType].GenericMapping,
  818. (PPRIVILEGE_SET) PrivilegeSet,
  819. &PrivilegeSetSize,
  820. &GrantedAccess,
  821. &AccessStatus ) )
  822. {
  823. rc = GetLastError();
  824. goto exit;
  825. }
  826. if (!AccessStatus) {
  827. rc = GetLastError();
  828. goto exit;
  829. }
  830. rc = 0;
  831. exit:
  832. RpcRevertToSelf();
  833. if (ClientToken) {
  834. CloseHandle( ClientToken );
  835. }
  836. if (rc != 0) {
  837. DebugPrint(( TEXT("FaxSvcAccessCheck() failed to authenticate, 0x%08x, 0x%08x\n"), SecurityType, DesiredAccess ));
  838. }
  839. LeaveCriticalSection( &CsSecurity );
  840. return rc == 0;
  841. }
  842. PVOID
  843. MyGetTokenInformation(
  844. HANDLE hToken,
  845. TOKEN_INFORMATION_CLASS TokenInformationClass
  846. )
  847. {
  848. PVOID TokenInformation = NULL;
  849. DWORD Size = 0;
  850. if (!GetTokenInformation( hToken, TokenInformationClass, NULL, 0, &Size ) &&
  851. GetLastError() == ERROR_INSUFFICIENT_BUFFER &&
  852. (TokenInformation = MemAlloc( Size )) &&
  853. GetTokenInformation( hToken, TokenInformationClass, TokenInformation, Size, &Size ))
  854. {
  855. return TokenInformation;
  856. }
  857. MemFree( TokenInformation );
  858. return NULL;
  859. }
  860. DWORD
  861. InitializeFaxSecurityDescriptors(
  862. VOID
  863. )
  864. {
  865. #define BUFFER_SIZE 4096
  866. DWORD i,j;
  867. DWORD rc = ERROR_SUCCESS;
  868. DWORD Ace;
  869. DWORD Size;
  870. HKEY hKey = NULL;
  871. DWORD Disposition;
  872. LPBYTE Buffer = NULL;
  873. BYTE AclBuffer[512];
  874. SECURITY_DESCRIPTOR SecurityDescriptor;
  875. DWORD Type;
  876. PACL Acl;
  877. HANDLE ServiceToken;
  878. PTOKEN_OWNER Owner;
  879. PTOKEN_GROUPS Groups;
  880. PSECURITY_DESCRIPTOR AbsSD;
  881. DWORD AbsSdSize = 0;
  882. DWORD DaclSize = 0;
  883. DWORD SaclSize = 0;
  884. DWORD OwnerSize = 0;
  885. DWORD GroupSize = 0;
  886. PACL pAbsDacl = NULL;
  887. PACL pAbsSacl = NULL;
  888. PSID pAbsOwner = NULL;
  889. PSID pAbsGroup = NULL;
  890. if (!CsInit) {
  891. InitializeCriticalSection( &CsSecurity );
  892. CsInit = TRUE;
  893. }
  894. //
  895. // Initialize SIDs
  896. //
  897. rc = InitializeSids();
  898. if (rc != NO_ERROR) {
  899. goto exit;
  900. }
  901. //
  902. // Initialize Fax ACEs
  903. //
  904. rc = InitializeAces( AceDataTableForFax, AcesForFax, AceSizesForFax, FAX_ACE_COUNT );
  905. if (rc != NO_ERROR) {
  906. goto exit;
  907. }
  908. if (!OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &ServiceToken )) {
  909. rc = GetLastError();
  910. goto exit;
  911. }
  912. Owner = MyGetTokenInformation( ServiceToken, TokenOwner );
  913. if (!Owner) {
  914. rc = ERROR_NOT_ENOUGH_MEMORY;
  915. goto exit;
  916. }
  917. Groups = MyGetTokenInformation( ServiceToken, TokenGroups );
  918. if (!Groups) {
  919. rc = ERROR_NOT_ENOUGH_MEMORY;
  920. goto exit;
  921. }
  922. CloseHandle( ServiceToken );
  923. rc = RegCreateKeyEx(
  924. HKEY_LOCAL_MACHINE,
  925. REGKEY_FAX_SECURITY,
  926. 0,
  927. TEXT(""),
  928. 0,
  929. KEY_ALL_ACCESS,
  930. NULL,
  931. &hKey,
  932. &Disposition
  933. );
  934. if (rc != ERROR_SUCCESS) {
  935. goto exit;
  936. }
  937. Buffer = (LPBYTE) MemAlloc( BUFFER_SIZE );
  938. if (!Buffer) {
  939. rc = ERROR_NOT_ENOUGH_MEMORY;
  940. goto exit;
  941. }
  942. for (i=0; i<FaxSecurityCount; i++) {
  943. Size = BUFFER_SIZE;
  944. ZeroMemory( Buffer, Size );
  945. rc = RegQueryValueEx(
  946. hKey,
  947. FaxSecurity[i].RegKey,
  948. 0,
  949. &Type,
  950. Buffer,
  951. &Size
  952. );
  953. if (rc == ERROR_SUCCESS) {
  954. if (!IsValidSecurityDescriptor( (PSECURITY_DESCRIPTOR) Buffer )) {
  955. rc = GetLastError();
  956. DebugPrint(( TEXT("IsValidSecurityDescriptor() failed, ec=%d"), rc ));
  957. goto exit;
  958. }
  959. //
  960. // the security descriptor needs to be converted to absolute format.
  961. //
  962. AbsSdSize = 0;
  963. DaclSize = 0;
  964. SaclSize = 0;
  965. OwnerSize = 0;
  966. GroupSize = 0;
  967. rc = MakeAbsoluteSD(
  968. (PSECURITY_DESCRIPTOR) Buffer,
  969. NULL,
  970. &AbsSdSize,
  971. NULL,
  972. &DaclSize,
  973. NULL,
  974. &SaclSize,
  975. NULL,
  976. &OwnerSize,
  977. NULL,
  978. &GroupSize
  979. );
  980. if (rc || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
  981. rc = GetLastError();
  982. goto exit;
  983. }
  984. AbsSD = MemAlloc(AbsSdSize);
  985. pAbsDacl = MemAlloc(DaclSize);
  986. pAbsSacl = MemAlloc(SaclSize);
  987. pAbsOwner = MemAlloc(OwnerSize);
  988. pAbsGroup = MemAlloc(GroupSize);
  989. if (NULL == AbsSD || NULL == pAbsDacl || NULL == pAbsSacl || NULL == pAbsOwner || NULL == pAbsGroup) {
  990. rc = ERROR_NOT_ENOUGH_MEMORY;
  991. goto exit;
  992. }
  993. MakeAbsoluteSD(
  994. (PSECURITY_DESCRIPTOR) Buffer,
  995. AbsSD,
  996. &AbsSdSize,
  997. pAbsDacl,
  998. &DaclSize,
  999. pAbsSacl,
  1000. &SaclSize,
  1001. pAbsOwner,
  1002. &OwnerSize,
  1003. pAbsGroup,
  1004. &GroupSize
  1005. );
  1006. FaxSecurity[i].SecurityDescriptor = AbsSD;
  1007. if (!IsValidSecurityDescriptor( FaxSecurity[i].SecurityDescriptor )) {
  1008. rc = GetLastError();
  1009. DebugPrint(( TEXT("IsValidSecurityDescriptor() failed, ec=%d"), rc ));
  1010. goto exit;
  1011. }
  1012. continue;
  1013. }
  1014. //
  1015. // Initialize a security descriptor and an ACL.
  1016. // We use a large static buffer to contain the ACL.
  1017. //
  1018. ZeroMemory( AclBuffer, sizeof(AclBuffer) );
  1019. Acl = (PACL)AclBuffer;
  1020. if(!InitializeAcl( Acl, sizeof(AclBuffer), ACL_REVISION2) ||
  1021. !InitializeSecurityDescriptor( &SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION ))
  1022. {
  1023. rc = GetLastError();
  1024. goto exit;
  1025. }
  1026. //
  1027. // Build up the DACL from the indices
  1028. //
  1029. for (Ace=0; Ace<FaxSecurity[i].AceCount; Ace++) {
  1030. if (!AddAce(
  1031. Acl,
  1032. ACL_REVISION2,
  1033. MAXDWORD,
  1034. AcesForFax[FaxSecurity[i].AceIdx[Ace]],
  1035. AcesForFax[FaxSecurity[i].AceIdx[Ace]]->Header.AceSize
  1036. ))
  1037. {
  1038. rc = GetLastError();
  1039. goto exit;
  1040. }
  1041. }
  1042. //
  1043. // Add the ACL to the security descriptor as the DACL
  1044. //
  1045. if (!SetSecurityDescriptorDacl( &SecurityDescriptor, TRUE, Acl, FALSE )) {
  1046. rc = GetLastError();
  1047. goto exit;
  1048. }
  1049. //
  1050. // set the owner
  1051. //
  1052. if (!SetSecurityDescriptorOwner( &SecurityDescriptor, Owner->Owner, FALSE )) {
  1053. rc = GetLastError();
  1054. goto exit;
  1055. }
  1056. //
  1057. // set the groups
  1058. //
  1059. for (j=0; j<Groups->GroupCount; j++) {
  1060. if (!SetSecurityDescriptorGroup( &SecurityDescriptor, Groups->Groups[j].Sid, FALSE )) {
  1061. rc = GetLastError();
  1062. goto exit;
  1063. }
  1064. }
  1065. //
  1066. // make the security descriptor self relative
  1067. //
  1068. Size = BUFFER_SIZE;
  1069. if (!MakeSelfRelativeSD( &SecurityDescriptor, (PSECURITY_DESCRIPTOR) Buffer, &Size )) {
  1070. rc = GetLastError();
  1071. goto exit;
  1072. }
  1073. //
  1074. // store the security descriptor in the registry
  1075. //
  1076. Size = GetSecurityDescriptorLength( (PSECURITY_DESCRIPTOR) Buffer );
  1077. FaxSecurity[i].SecurityDescriptor = (PSECURITY_DESCRIPTOR) MemAlloc( Size );
  1078. if (!FaxSecurity[i].SecurityDescriptor) {
  1079. rc = ERROR_NOT_ENOUGH_MEMORY;
  1080. goto exit;
  1081. }
  1082. CopyMemory( FaxSecurity[i].SecurityDescriptor, Buffer, Size );
  1083. rc = RegSetValueEx(
  1084. hKey,
  1085. FaxSecurity[i].RegKey,
  1086. 0,
  1087. REG_BINARY,
  1088. (LPBYTE) FaxSecurity[i].SecurityDescriptor,
  1089. Size
  1090. );
  1091. if (rc) {
  1092. goto exit;
  1093. }
  1094. }
  1095. exit:
  1096. if (hKey) {
  1097. RegCloseKey( hKey );
  1098. }
  1099. if (Buffer) {
  1100. MemFree( Buffer );
  1101. }
  1102. if (Owner) {
  1103. MemFree( Owner );
  1104. }
  1105. if (Groups) {
  1106. MemFree( Groups );
  1107. }
  1108. return rc;
  1109. }
  1110. LPWSTR
  1111. GetClientUserName(
  1112. VOID
  1113. )
  1114. {
  1115. WCHAR UserName[128];
  1116. DWORD Size;
  1117. if (RpcImpersonateClient(NULL) != RPC_S_OK) {
  1118. return NULL;
  1119. }
  1120. Size = sizeof(UserName) / sizeof(WCHAR);
  1121. if (!GetUserName( UserName, &Size )) {
  1122. RpcRevertToSelf();
  1123. return NULL;
  1124. }
  1125. RpcRevertToSelf();
  1126. return StringDup( UserName );
  1127. }
  1128. error_status_t
  1129. FAX_GetSecurityDescriptorCount(
  1130. IN handle_t FaxHandle,
  1131. OUT LPDWORD Count
  1132. )
  1133. /*++
  1134. Routine Description:
  1135. Arguments:
  1136. Return Value:
  1137. --*/
  1138. {
  1139. *Count = FaxSecurityCount;
  1140. return ERROR_SUCCESS;
  1141. }
  1142. error_status_t
  1143. FAX_SetSecurityDescriptor(
  1144. IN handle_t FaxHandle,
  1145. IN const LPBYTE Buffer,
  1146. IN DWORD BufferSize
  1147. )
  1148. {
  1149. DWORD rVal = ERROR_SUCCESS;
  1150. PFAX_SECURITY_DESCRIPTOR FaxSecDesc = (PFAX_SECURITY_DESCRIPTOR) Buffer;
  1151. HKEY hKey;
  1152. DWORD Disposition;
  1153. DWORD SecDescBufferSize;
  1154. if (!FaxSvcAccessCheck( SEC_CONFIG_SET, FAX_CONFIG_SET )) {
  1155. return ERROR_ACCESS_DENIED;
  1156. }
  1157. // Check that the buffer is large enough to hold a FAX_SECURITY_DESCRIPTOR
  1158. if (BufferSize < sizeof (FAX_SECURITY_DESCRIPTOR)) {
  1159. return ERROR_INVALID_PARAMETER;
  1160. }
  1161. if (FaxSecDesc->Id > FaxSecurityCount) {
  1162. return ERROR_INVALID_CATEGORY;
  1163. }
  1164. // Check that the offset is within the buffer.
  1165. if (PtrToUlong(FaxSecDesc->SecurityDescriptor) >= BufferSize) {
  1166. return ERROR_INVALID_PARAMETER;
  1167. }
  1168. // Calculate the size of the security descriptor buffer for validation
  1169. SecDescBufferSize = BufferSize - PtrToUlong(FaxSecDesc->SecurityDescriptor);
  1170. FaxSecDesc->SecurityDescriptor = (PSECURITY_DESCRIPTOR) FixupString(Buffer,
  1171. FaxSecDesc->SecurityDescriptor);
  1172. // Validate the passed security descriptor.
  1173. if (!RtlValidRelativeSecurityDescriptor(FaxSecDesc->SecurityDescriptor,
  1174. SecDescBufferSize,
  1175. 0)) {
  1176. return ERROR_INVALID_DATA;
  1177. }
  1178. rVal = RegCreateKeyEx(
  1179. HKEY_LOCAL_MACHINE,
  1180. REGKEY_FAX_SECURITY,
  1181. 0,
  1182. TEXT(""),
  1183. 0,
  1184. KEY_ALL_ACCESS,
  1185. NULL,
  1186. &hKey,
  1187. &Disposition
  1188. );
  1189. if (rVal != ERROR_SUCCESS) {
  1190. return rVal;
  1191. }
  1192. rVal = RegSetValueEx(
  1193. hKey,
  1194. FaxSecurity[FaxSecDesc->Id].RegKey,
  1195. 0,
  1196. REG_BINARY,
  1197. (LPBYTE) FaxSecDesc->SecurityDescriptor,
  1198. GetSecurityDescriptorLength( FaxSecDesc->SecurityDescriptor )
  1199. );
  1200. if (FaxSecurity[FaxSecDesc->Id].SecurityDescriptor) {
  1201. MemFree( FaxSecurity[FaxSecDesc->Id].SecurityDescriptor );
  1202. }
  1203. rVal = InitializeFaxSecurityDescriptors();
  1204. RegCloseKey( hKey );
  1205. return rVal;
  1206. }
  1207. error_status_t
  1208. FAX_GetSecurityDescriptor(
  1209. IN handle_t FaxHandle,
  1210. IN DWORD Id,
  1211. OUT LPBYTE *Buffer,
  1212. OUT LPDWORD BufferSize
  1213. )
  1214. /*++
  1215. Routine Description:
  1216. Retrieves the FAX configuration from the FAX server.
  1217. The SizeOfStruct in the FaxConfig argument MUST be
  1218. set to a value == sizeof(FAX_CONFIGURATION). If the BufferSize
  1219. is not big enough, return an error and set BytesNeeded to the
  1220. required size.
  1221. Arguments:
  1222. FaxHandle - FAX handle obtained from FaxConnectFaxServer.
  1223. Buffer - Pointer to a FAX_CONFIGURATION structure.
  1224. BufferSize - Size of Buffer
  1225. BytesNeeded - Number of bytes needed
  1226. Return Value:
  1227. TRUE - Success
  1228. FALSE - Failure, call GetLastError() for more error information.
  1229. --*/
  1230. {
  1231. error_status_t rVal = ERROR_SUCCESS;
  1232. PFAX_SECURITY_DESCRIPTOR FaxSecDesc;
  1233. ULONG_PTR Offset;
  1234. LPWSTR FriendlyName;
  1235. DWORD DescLength;
  1236. HKEY hKey;
  1237. DWORD Type;
  1238. DWORD Size;
  1239. DWORD Disposition;
  1240. if (!FaxSvcAccessCheck( SEC_CONFIG_QUERY, FAX_CONFIG_QUERY )) {
  1241. return ERROR_ACCESS_DENIED;
  1242. }
  1243. if (Id > FaxSecurityCount) {
  1244. return ERROR_INVALID_CATEGORY;
  1245. }
  1246. rVal = RegCreateKeyEx(
  1247. HKEY_LOCAL_MACHINE,
  1248. REGKEY_FAX_SECURITY,
  1249. 0,
  1250. TEXT(""),
  1251. 0,
  1252. KEY_ALL_ACCESS,
  1253. NULL,
  1254. &hKey,
  1255. &Disposition
  1256. );
  1257. if (rVal != ERROR_SUCCESS) {
  1258. return rVal;
  1259. }
  1260. FriendlyName = GetString( FaxSecurity[Id].StringResource );
  1261. //
  1262. // count up the number of bytes needed
  1263. //
  1264. rVal = RegQueryValueEx(
  1265. hKey,
  1266. FaxSecurity[Id].RegKey,
  1267. 0,
  1268. &Type,
  1269. NULL,
  1270. &DescLength
  1271. );
  1272. if (rVal != ERROR_SUCCESS) {
  1273. goto exit;
  1274. }
  1275. Offset = sizeof(FAX_SECURITY_DESCRIPTOR);
  1276. *BufferSize = (DWORD)(Offset +
  1277. DescLength +
  1278. (wcslen(FriendlyName) + 1) * sizeof(WCHAR));
  1279. *Buffer = MemAlloc( *BufferSize );
  1280. if (*Buffer == NULL) {
  1281. rVal = ERROR_NOT_ENOUGH_MEMORY;
  1282. goto exit;
  1283. }
  1284. FaxSecDesc = (PFAX_SECURITY_DESCRIPTOR) *Buffer;
  1285. FaxSecDesc->Id = Id;
  1286. StoreString(
  1287. FriendlyName,
  1288. (PULONG_PTR)&FaxSecDesc->FriendlyName,
  1289. *Buffer,
  1290. &Offset
  1291. );
  1292. rVal = RegQueryValueEx(
  1293. hKey,
  1294. FaxSecurity[Id].RegKey,
  1295. 0,
  1296. &Type,
  1297. *Buffer + Offset,
  1298. &Size
  1299. );
  1300. FaxSecDesc->SecurityDescriptor = (LPBYTE) Offset;
  1301. exit:
  1302. RegCloseKey( hKey );
  1303. return rVal;
  1304. }
  1305. BOOL
  1306. PostClientMessage(
  1307. PFAX_CLIENT_DATA ClientData,
  1308. PFAX_EVENT FaxEvent
  1309. )
  1310. /*++
  1311. Routine Description:
  1312. attempts to post a message to client on another windowstation and desktop
  1313. Arguments:
  1314. ClientData - pointer to a FAX_CLIENT_DATA structure.
  1315. FaxEvent - pointer to a FAX_EVENT structure.
  1316. Return Value:
  1317. TRUE - Success
  1318. FALSE - Failure, call GetLastError() for more error information.
  1319. --*/
  1320. {
  1321. HWINSTA hWindowStation, hOldWindowStation=NULL;
  1322. HDESK hDesktop, hOldDesktop=NULL;
  1323. BOOL bStatus = FALSE;
  1324. //
  1325. // need to restore windowstation and desktop
  1326. //
  1327. if (! (hOldWindowStation = GetProcessWindowStation()) ) {
  1328. DebugPrint(( TEXT("GetProcessWindowStation failed, ec = %d\n"), GetLastError() ));
  1329. return FALSE;
  1330. }
  1331. if (! (hOldDesktop = GetThreadDesktop( GetCurrentThreadId() )) ) {
  1332. DebugPrint(( TEXT("GetThreadDesktop failed, ec = %d\n"), GetLastError() ));
  1333. return FALSE;
  1334. }
  1335. //
  1336. // impersonate the client
  1337. //
  1338. if (! SetThreadToken( NULL, ClientData->hClientToken ) ) {
  1339. DebugPrint(( TEXT("SetThreadToken failed, ec = %d\n"), GetLastError() ));
  1340. return FALSE;
  1341. }
  1342. //
  1343. // get a handle to the windowstation, switch to new windowstation
  1344. //
  1345. if (! (hWindowStation = OpenWindowStation(ClientData->WindowStation,
  1346. FALSE, //bInherit,
  1347. WINSTA_READATTRIBUTES | WINSTA_WRITEATTRIBUTES)) ) {
  1348. DebugPrint(( TEXT("OpenWindowStation failed, ec = %d\n"), GetLastError() ));
  1349. goto exit;
  1350. }
  1351. if (! SetProcessWindowStation( hWindowStation ) ) {
  1352. DebugPrint(( TEXT("SetProcessWindowStation failed, ec = %d\n"), GetLastError() ));
  1353. goto exit;
  1354. }
  1355. //
  1356. // get a handle to the desktop, switch to new desktop
  1357. //
  1358. if (! (hDesktop = OpenDesktop(ClientData->Desktop,
  1359. 0,
  1360. FALSE,
  1361. DESKTOP_READOBJECTS | DESKTOP_WRITEOBJECTS)) ) {
  1362. DebugPrint(( TEXT("OpenDesktop failed, ec = %d\n"), GetLastError() ));
  1363. goto exit;
  1364. }
  1365. if (! SetThreadDesktop( hDesktop ) ) {
  1366. DebugPrint(( TEXT("SetThreadDesktop failed, ec = %d\n"), GetLastError() ));
  1367. goto exit;
  1368. }
  1369. //
  1370. // post the message to the proper window if it exits
  1371. //
  1372. if (! IsWindow( ClientData->hWnd )) {
  1373. DebugPrint(( TEXT("Hwnd %x doesn't exist on current desktop\n"), ClientData->hWnd ));
  1374. goto exit;
  1375. }
  1376. if (! PostMessage( ClientData->hWnd,
  1377. ClientData->MessageStart + FaxEvent->EventId,
  1378. (WPARAM)FaxEvent->DeviceId,
  1379. (LPARAM)FaxEvent->JobId )) {
  1380. DebugPrint(( TEXT("PostMessage failed, ec = %d\n"), GetLastError() ));
  1381. goto exit;
  1382. }
  1383. bStatus = TRUE;
  1384. exit:
  1385. //
  1386. // revert to old thread context (NULL means stop impersonating)
  1387. //
  1388. SetThreadToken( NULL, NULL );
  1389. if (hOldWindowStation) {
  1390. SetProcessWindowStation( hOldWindowStation );
  1391. CloseWindowStation( hWindowStation );
  1392. }
  1393. if (hOldDesktop) {
  1394. SetThreadDesktop( hOldDesktop );
  1395. CloseDesktop( hDesktop );
  1396. }
  1397. return bStatus;
  1398. }
  1399. BOOL
  1400. BuildSecureSD(
  1401. OUT PSECURITY_DESCRIPTOR *SDIn
  1402. )
  1403. /*++
  1404. Routine Description:
  1405. builds a secure security descriptor to be used in securing a globally
  1406. named object. Our "secure" SD's DACL consists of the following permissions:
  1407. Authenticated users get "generic read" access.
  1408. Administrators get "generic all" access.
  1409. Arguments:
  1410. SDIn - pointer to the PSECURITY_DESCRIPTOR to be created.
  1411. Return Value:
  1412. TRUE - Success, the SECURITY_DESCRIPTOR was created successfully.
  1413. The caller is responsible for freeing the SECURITY_DESCRIPTOR
  1414. --*/
  1415. {
  1416. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  1417. PSID AuthenticatedUsers;
  1418. PSID BuiltInAdministrators;
  1419. PSECURITY_DESCRIPTOR Sd = NULL;
  1420. ACL *Acl;
  1421. ULONG AclSize;
  1422. BOOL RetVal = TRUE;
  1423. *SDIn = NULL;
  1424. //
  1425. // Allocate and initialize the required SIDs
  1426. //
  1427. if (!AllocateAndInitializeSid(
  1428. &NtAuthority,
  1429. 2,
  1430. SECURITY_BUILTIN_DOMAIN_RID,
  1431. DOMAIN_ALIAS_RID_ADMINS,
  1432. 0,0,0,0,0,0,
  1433. &BuiltInAdministrators)) {
  1434. return(FALSE);
  1435. }
  1436. if (!AllocateAndInitializeSid(
  1437. &NtAuthority,
  1438. 1,
  1439. SECURITY_AUTHENTICATED_USER_RID,
  1440. 0,0,0,0,0,0,0,
  1441. &AuthenticatedUsers)) {
  1442. RetVal = FALSE;
  1443. goto e0;
  1444. }
  1445. //
  1446. // "- sizeof (ULONG)" represents the SidStart field of the
  1447. // ACCESS_ALLOWED_ACE. Since we're adding the entire length of the
  1448. // SID, this field is counted twice.
  1449. //
  1450. AclSize = sizeof (ACL) +
  1451. (2 * (sizeof (ACCESS_ALLOWED_ACE) - sizeof (ULONG))) +
  1452. GetLengthSid(AuthenticatedUsers) +
  1453. GetLengthSid(BuiltInAdministrators);
  1454. Sd = MemAlloc(SECURITY_DESCRIPTOR_MIN_LENGTH + AclSize);
  1455. if (!Sd) {
  1456. RetVal = FALSE;
  1457. goto e1;
  1458. }
  1459. Acl = (ACL *)((BYTE *)Sd + SECURITY_DESCRIPTOR_MIN_LENGTH);
  1460. if (!InitializeAcl(Acl,
  1461. AclSize,
  1462. ACL_REVISION)) {
  1463. RetVal = FALSE;
  1464. goto e2;
  1465. } else if (!AddAccessAllowedAce(Acl,
  1466. ACL_REVISION,
  1467. SYNCHRONIZE | GENERIC_READ,
  1468. AuthenticatedUsers)) {
  1469. // Failed to build the ACE granting "Authenticated users"
  1470. // (SYNCHRONIZE | GENERIC_READ) access.
  1471. RetVal = FALSE;
  1472. goto e2;
  1473. } else if (!AddAccessAllowedAce(Acl,
  1474. ACL_REVISION,
  1475. GENERIC_ALL,
  1476. BuiltInAdministrators)) {
  1477. // Failed to build the ACE granting "Built-in Administrators"
  1478. // GENERIC_ALL access.
  1479. RetVal = FALSE;
  1480. goto e2;
  1481. } else if (!InitializeSecurityDescriptor(Sd,
  1482. SECURITY_DESCRIPTOR_REVISION)) {
  1483. RetVal = FALSE;
  1484. goto e2;
  1485. } else if (!SetSecurityDescriptorDacl(Sd,
  1486. TRUE,
  1487. Acl,
  1488. FALSE)) {
  1489. // error
  1490. RetVal = FALSE;
  1491. goto e2;
  1492. }
  1493. if (!IsValidSecurityDescriptor(Sd)) {
  1494. DebugPrint(( TEXT("invalid security descriptor, ec = %d\n"), GetLastError() ));
  1495. RetVal = FALSE;
  1496. goto e2;
  1497. }
  1498. //
  1499. // success
  1500. //
  1501. *SDIn = Sd;
  1502. goto e1;
  1503. e2:
  1504. MemFree(Sd);
  1505. e1:
  1506. FreeSid(AuthenticatedUsers);
  1507. e0:
  1508. FreeSid(BuiltInAdministrators);
  1509. return(RetVal);
  1510. }