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.

503 lines
13 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation
  3. Module Name:
  4. wstoken.cpp
  5. Abstract:
  6. This file intializes the members and methods of WsAccessToken class.
  7. Authors:
  8. Christophe Robert
  9. Revision History:
  10. 02-July-2001 : Updated by Wipro Technologies.
  11. --*/
  12. //common header files needed for this file
  13. #include "pch.h"
  14. #include "CommonHeaderFiles.h"
  15. WsAccessToken::WsAccessToken()
  16. /*++
  17. Routine Description:
  18. This function intializes the members of WsAccessToken class.
  19. Arguments:
  20. None
  21. Return Value:
  22. None
  23. --*/
  24. {
  25. // intialize the variables
  26. hToken = NULL ;
  27. dwDomainAttributes = NULL;
  28. }
  29. WsAccessToken::~WsAccessToken()
  30. /*++
  31. Routine Description:
  32. This function deallocates the members of WsAccessToken class.
  33. Arguments:
  34. None
  35. Return Value:
  36. None
  37. --*/
  38. {
  39. // release handle
  40. if(hToken){
  41. CloseHandle ( hToken ) ;
  42. }
  43. if (dwDomainAttributes)
  44. {
  45. FreeMemory ((LPVOID *) &dwDomainAttributes) ;
  46. }
  47. }
  48. DWORD
  49. WsAccessToken::InitGroups(
  50. OUT WsSid ***lppGroupsSid,
  51. OUT WsSid **lppLogonId,
  52. OUT DWORD *lpnbGroups
  53. )
  54. /*++
  55. Routine Description:
  56. This function retrieves and build groups SIDs array.
  57. Arguments:
  58. [OUT] lppGroupsSid : Stores group SID
  59. [OUT] lppLogonId : Stores logon ID
  60. [OUT] lpnbGroups : Stores the number of groups
  61. Return Value:
  62. EXIT_SUCCESS : On success
  63. EXIT_FAILURE : On failure
  64. --*/
  65. {
  66. // sub-local variables
  67. TOKEN_GROUPS *lpTokenGroups ;
  68. DWORD dwTokenGroups = 0;
  69. DWORD dwGroup = 0 ;
  70. WORD wloop = 0 ;
  71. BOOL bTokenInfo = FALSE;
  72. // Get groups size
  73. bTokenInfo = GetTokenInformation(hToken, TokenGroups, NULL, 0, &dwTokenGroups);
  74. // if required buffer size is zero...
  75. // So we assume that there are no groups present in the system
  76. if ( 0 == dwTokenGroups )
  77. {
  78. // return WIN32 error code
  79. return GetLastError () ;
  80. }
  81. // Allocate memory for the groups
  82. lpTokenGroups = ( TOKEN_GROUPS * ) AllocateMemory( dwTokenGroups );
  83. if ( NULL == lpTokenGroups )
  84. {
  85. // release memory
  86. FreeMemory ((LPVOID *) &lpTokenGroups) ;
  87. // return WIN32 error code
  88. return ERROR_NOT_ENOUGH_MEMORY ;
  89. }
  90. // Get the group names
  91. bTokenInfo = GetTokenInformation ( hToken, TokenGroups, lpTokenGroups, dwTokenGroups, &dwTokenGroups );
  92. if ( FALSE == bTokenInfo )
  93. {
  94. FreeMemory ((LPVOID *) &lpTokenGroups) ;
  95. return GetLastError () ;
  96. }
  97. //Build objects
  98. *lpnbGroups = (WORD) lpTokenGroups->GroupCount ;
  99. // check for logonid
  100. if(IsLogonId ( lpTokenGroups )){
  101. (*lpnbGroups)-- ;
  102. }
  103. else{
  104. *lppLogonId = NULL ;
  105. }
  106. // Allocate memory with the actual size
  107. *lppGroupsSid = (WsSid **) AllocateMemory ( *lpnbGroups * sizeof(WsSid**) ) ;
  108. if ( NULL == *lppGroupsSid )
  109. {
  110. FreeMemory ((LPVOID *) &lpTokenGroups) ;
  111. return ERROR_NOT_ENOUGH_MEMORY ;
  112. }
  113. // Get SID with respect to the group names
  114. for( wloop = 0 ; wloop < *lpnbGroups ; wloop++ ){
  115. (*lppGroupsSid)[wloop] = new WsSid () ;
  116. if ( NULL == (*lppGroupsSid)[wloop] )
  117. {
  118. // release memory
  119. FreeMemory ((LPVOID *) &lpTokenGroups) ;
  120. // return WIN32 error code
  121. return GetLastError () ;
  122. }
  123. }
  124. //Allocate memory for attributes
  125. dwDomainAttributes = (DWORD*) AllocateMemory ( (lpTokenGroups->GroupCount * sizeof(DWORD)) + 10) ;
  126. if ( 0 == dwDomainAttributes )
  127. {
  128. FreeMemory ((LPVOID *) &lpTokenGroups) ;
  129. return ERROR_NOT_ENOUGH_MEMORY ;
  130. }
  131. //Loop within groups
  132. for(wloop = 0 , dwGroup = 0;
  133. dwGroup < lpTokenGroups->GroupCount ;
  134. dwGroup++) {
  135. // store the domain attributes
  136. dwDomainAttributes[dwGroup] = lpTokenGroups->Groups[dwGroup].Attributes;
  137. //check whether SID is a logon SID
  138. if(lpTokenGroups->Groups[dwGroup].Attributes & SE_GROUP_LOGON_ID) {
  139. *lppLogonId = new WsSid () ;
  140. if ( NULL == *lppLogonId )
  141. {
  142. // release memory
  143. FreeMemory ((LPVOID *) &lpTokenGroups) ;
  144. // return WIN32 error code
  145. return GetLastError () ;
  146. }
  147. (*lppLogonId)->Init ( lpTokenGroups->Groups[dwGroup].Sid ) ;
  148. }
  149. else {
  150. (*lppGroupsSid)[wloop++]->Init(lpTokenGroups->Groups[dwGroup].Sid);
  151. }
  152. }
  153. dwDomainAttributes[dwGroup] = 0;
  154. //release memory
  155. FreeMemory ((LPVOID *) &lpTokenGroups) ;
  156. // return success
  157. return EXIT_SUCCESS ;
  158. }
  159. DWORD
  160. WsAccessToken::InitPrivs (
  161. OUT WsPrivilege ***lppPriv,
  162. OUT DWORD *lpnbPriv
  163. )
  164. /*++
  165. Routine Description:
  166. This function retrieves and build privileges array.
  167. Arguments:
  168. [OUT] lppPriv : Stores the privileges array
  169. [OUT] lpnbPriv : Stores the number of privileges
  170. Return Value:
  171. EXIT_SUCCESS : On success
  172. EXIT_FAILURE : On failure
  173. --*/
  174. {
  175. //sub-local variables
  176. DWORD dwTokenPriv = 0;
  177. TOKEN_PRIVILEGES *lpTokenPriv ;
  178. WORD wloop = 0 ;
  179. BOOL bTokenInfo = FALSE;
  180. // Get the privileges size
  181. bTokenInfo = GetTokenInformation(hToken,TokenPrivileges, NULL, 0, &dwTokenPriv);
  182. // if required buffer size is zero...
  183. // So we assume that there are no privileges present in the system
  184. if( 0 == dwTokenPriv )
  185. {
  186. // return WIN32 error code
  187. return GetLastError () ;
  188. }
  189. // allocate memory with actual size
  190. lpTokenPriv = (TOKEN_PRIVILEGES *) AllocateMemory ( dwTokenPriv ) ;
  191. if ( NULL == lpTokenPriv)
  192. {
  193. // return WIN32 error code
  194. return ERROR_NOT_ENOUGH_MEMORY ;
  195. }
  196. //Allocate memory for the privileges
  197. bTokenInfo = GetTokenInformation ( hToken, TokenPrivileges, lpTokenPriv, dwTokenPriv, &dwTokenPriv);
  198. if( FALSE == bTokenInfo )
  199. {
  200. // release memory
  201. FreeMemory ((LPVOID *) &lpTokenPriv) ;
  202. // return WIN32 error code
  203. return GetLastError () ;
  204. }
  205. //Get privilege count
  206. *lpnbPriv = (DWORD) lpTokenPriv->PrivilegeCount ;
  207. // allocate memory with the privilege count
  208. *lppPriv = (WsPrivilege **) AllocateMemory( *lpnbPriv * sizeof(WsPrivilege**) );
  209. if ( NULL == *lppPriv )
  210. {
  211. // release memory
  212. FreeMemory ((LPVOID *) &lpTokenPriv) ;
  213. // retrun WIN 32 error code
  214. return ERROR_NOT_ENOUGH_MEMORY ;
  215. }
  216. //Loop through the privileges to display their name
  217. for( wloop = 0 ; wloop < (WORD) lpTokenPriv->PrivilegeCount ; wloop++) {
  218. (*lppPriv)[wloop] = new WsPrivilege ( &lpTokenPriv->Privileges[wloop] ) ;
  219. if ( NULL == (*lppPriv)[wloop] )
  220. {
  221. // release memory
  222. FreeMemory ((LPVOID *) &lpTokenPriv) ;
  223. // retrun WIN 32 error code
  224. return GetLastError () ;
  225. }
  226. }
  227. // release memory
  228. FreeMemory ((LPVOID *) &lpTokenPriv) ;
  229. // return success
  230. return EXIT_SUCCESS ;
  231. }
  232. DWORD
  233. WsAccessToken::InitUserSid (
  234. OUT WsSid *lpSid
  235. )
  236. /*++
  237. Routine Description:
  238. This function initializes the user name and SID.
  239. Arguments:
  240. [OUT] lpSid : Stores the SID
  241. Return Value:
  242. EXIT_SUCCESS : On success
  243. EXIT_FAILURE : On failure
  244. --*/
  245. {
  246. // sub-local varibles
  247. DWORD dwUser = 0 ;
  248. TOKEN_USER *lpUser ;
  249. BOOL bTokenInfo = FALSE;
  250. DWORD dwRetVal = 0;
  251. // Get User name size
  252. bTokenInfo = GetTokenInformation ( hToken, TokenUser, NULL, 0, &dwUser);
  253. // if required buffer size is zero...
  254. if( 0 == dwUser )
  255. {
  256. // return WIN32 error code
  257. return GetLastError () ;
  258. }
  259. // allocate memory with the actual size
  260. lpUser = (TOKEN_USER *) AllocateMemory ( dwUser ) ;
  261. if ( NULL == lpUser )
  262. {
  263. // return WIN32 error code
  264. return ERROR_NOT_ENOUGH_MEMORY ;
  265. }
  266. // Get the information of Logged-on user and SID
  267. bTokenInfo = GetTokenInformation ( hToken, TokenUser, lpUser, dwUser, &dwUser );
  268. if( FALSE == bTokenInfo )
  269. {
  270. FreeMemory ((LPVOID *) &lpUser) ;
  271. // return WIN32 error code
  272. return GetLastError () ;
  273. }
  274. dwRetVal = lpSid->Init ( lpUser->User.Sid );
  275. // release memory
  276. FreeMemory ((LPVOID *) &lpUser) ;
  277. // return SID
  278. return dwRetVal ;
  279. }
  280. BOOL
  281. WsAccessToken::IsLogonId (
  282. OUT TOKEN_GROUPS *lpTokenGroups
  283. )
  284. /*++
  285. Routine Description:
  286. This function checks whether logon id is present or not.
  287. Arguments:
  288. [OUT] lpTokenGroups : Stores the token groups
  289. Return Value:
  290. TRUE : On success
  291. FALSE : On failure
  292. --*/
  293. {
  294. //sub-local variables
  295. DWORD dwGroup = 0;
  296. // Loop through and check whether the SID is a logon SID
  297. for(dwGroup = 0; dwGroup < lpTokenGroups->GroupCount ; dwGroup++) {
  298. if(lpTokenGroups->Groups[dwGroup].Attributes & SE_GROUP_LOGON_ID){
  299. // return 0
  300. return TRUE ;
  301. }
  302. }
  303. // return 1
  304. return FALSE ;
  305. }
  306. DWORD
  307. WsAccessToken::Open ( VOID )
  308. /*++
  309. Routine Description:
  310. This function opens the current access token.
  311. Arguments:
  312. None
  313. Return Value:
  314. EXIT_SUCCESS : On success
  315. EXIT_FAILURE : On failure
  316. --*/
  317. {
  318. // Open current process token
  319. if( FALSE == OpenProcessToken ( GetCurrentProcess(),
  320. TOKEN_QUERY | TOKEN_QUERY_SOURCE,
  321. &hToken )){
  322. //return WIN32 error code
  323. return GetLastError () ;
  324. }
  325. // return success
  326. return EXIT_SUCCESS ;
  327. }
  328. VOID
  329. WsAccessToken::GetDomainAttributes(
  330. IN DWORD dwAttributes,
  331. OUT LPWSTR szDmAttrib,
  332. IN DWORD dwSize
  333. )
  334. /*++
  335. Routine Description:
  336. Gets the domain attribute names
  337. Arguments:
  338. [IN] dwAttributes : attibute value
  339. [OUT] szDomainAttrib : attibute value
  340. Return Value:
  341. TRUE : On success
  342. FALSE : On failure
  343. --*/
  344. {
  345. WCHAR szDomainAttrib [2 * MAX_STRING_LENGTH] ;
  346. BOOL bFlag = FALSE;
  347. // initialize the variables
  348. SecureZeroMemory ( szDomainAttrib, SIZE_OF_ARRAY(szDomainAttrib) );
  349. //Mandatory group
  350. if( SE_GROUP_MANDATORY & dwAttributes )
  351. {
  352. StringConcat (szDomainAttrib, GetResString(IDS_ATTRIB_MANDATORY), SIZE_OF_ARRAY(szDomainAttrib));
  353. bFlag = TRUE;
  354. }
  355. // Enabled by default
  356. if( SE_GROUP_ENABLED_BY_DEFAULT & dwAttributes )
  357. {
  358. if ( TRUE == bFlag )
  359. {
  360. StringConcat (szDomainAttrib, L", ", SIZE_OF_ARRAY(szDomainAttrib));
  361. }
  362. StringConcat (szDomainAttrib, GetResString(IDS_ATTRIB_BYDEFAULT), SIZE_OF_ARRAY(szDomainAttrib));
  363. bFlag = TRUE;
  364. }
  365. //Enabled group
  366. if( SE_GROUP_ENABLED & dwAttributes )
  367. {
  368. if ( TRUE == bFlag )
  369. {
  370. StringConcat (szDomainAttrib, L", ", SIZE_OF_ARRAY(szDomainAttrib));
  371. }
  372. StringConcat (szDomainAttrib, GetResString(IDS_ATTRIB_ENABLED), SIZE_OF_ARRAY(szDomainAttrib));
  373. bFlag = TRUE;
  374. }
  375. //Group owner
  376. if( SE_GROUP_OWNER & dwAttributes )
  377. {
  378. if ( TRUE == bFlag )
  379. {
  380. StringConcat (szDomainAttrib, L", ", SIZE_OF_ARRAY(szDomainAttrib));
  381. }
  382. StringConcat (szDomainAttrib, GetResString(IDS_ATTRIB_OWNER), SIZE_OF_ARRAY(szDomainAttrib));
  383. bFlag = TRUE;
  384. }
  385. //Group use for deny only
  386. if( SE_GROUP_USE_FOR_DENY_ONLY & dwAttributes )
  387. {
  388. if ( TRUE == bFlag )
  389. {
  390. StringConcat (szDomainAttrib, L", ", SIZE_OF_ARRAY(szDomainAttrib));
  391. }
  392. StringConcat (szDomainAttrib, GetResString(IDS_ATTRIB_USEFORDENY), SIZE_OF_ARRAY(szDomainAttrib));
  393. bFlag = TRUE;
  394. }
  395. //local group
  396. if( SE_GROUP_RESOURCE & dwAttributes )
  397. {
  398. if ( TRUE == bFlag )
  399. {
  400. StringConcat (szDomainAttrib, L", ", SIZE_OF_ARRAY(szDomainAttrib));
  401. }
  402. StringConcat (szDomainAttrib, GetResString(IDS_ATTRIB_LOCAL), SIZE_OF_ARRAY(szDomainAttrib));
  403. bFlag = TRUE;
  404. }
  405. // Copy domain attrinutes
  406. StringCopy ( szDmAttrib, szDomainAttrib, dwSize );
  407. return;
  408. }