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.

241 lines
9.5 KiB

  1. /*---------------------------------------------------------------------------
  2. File: RightsTranslator.cpp
  3. Comments: Functions to translate user rights
  4. (c) Copyright 1999, Mission Critical Software, Inc., All Rights Reserved
  5. Proprietary and confidential to Mission Critical Software, Inc.
  6. REVISION LOG ENTRY
  7. Revision By: Christy Boles
  8. Revised on 02/25/99 19:57:16
  9. ---------------------------------------------------------------------------
  10. */
  11. #include "StdAfx.h"
  12. #include "Mcs.h"
  13. #include "WorkObj.h"
  14. #include "SecTrans.h"
  15. #include "STArgs.hpp"
  16. #include "SidCache.hpp"
  17. #include "SDStat.hpp"
  18. #include "TxtSid.h"
  19. #include "ErrDct.hpp"
  20. //#import "\bin\McsDctWorkerObjects.tlb"
  21. #import "WorkObj.tlb"
  22. extern TErrorDct err;
  23. DWORD
  24. TranslateUserRights(
  25. WCHAR const * serverName, // in - name of server to translate groups on
  26. SecurityTranslatorArgs * stArgs, // in - translation settings
  27. TSDRidCache * cache, // in - translation table
  28. TSDResolveStats * stat // in - stats on items modified
  29. )
  30. {
  31. // DWORD rc = 0;
  32. HRESULT hr;
  33. SAFEARRAY * pRights = NULL;
  34. SAFEARRAY * pUsers = NULL;
  35. TAcctNode * node = NULL;
  36. _bstr_t server = serverName;
  37. MCSDCTWORKEROBJECTSLib::IUserRightsPtr pLsa(CLSID_UserRights);
  38. WCHAR currPath[500];
  39. DWORD mode = stArgs->TranslationMode();
  40. BOOL bUseMapFile = stArgs->UsingMapFile();
  41. //
  42. // If server name is not null then use specified name for generated messages. If specified
  43. // server name is null that means the user rights translation is being performed on the
  44. // local machine therefore retrieve the local machine name and use it for generated messages.
  45. //
  46. WCHAR szServerName[LEN_Computer];
  47. if (serverName && *serverName)
  48. {
  49. wcsncpy(szServerName, serverName, LEN_Computer);
  50. szServerName[LEN_Computer - 1] = L'\0';
  51. }
  52. else
  53. {
  54. DWORD dwSize = LEN_Computer;
  55. if (!GetComputerName(szServerName, &dwSize))
  56. {
  57. szServerName[0] = L'\0';
  58. }
  59. }
  60. if ( pLsa == NULL )
  61. {
  62. return E_FAIL;
  63. }
  64. pLsa->NoChange = stArgs->NoChange();
  65. if ( stArgs->TranslationMode() == REPLACE_SECURITY || stArgs->TranslationMode() == REMOVE_SECURITY )
  66. {
  67. err.MsgWrite(0,DCT_MSG_USER_RIGHTS_ONLY_ADDS);
  68. stArgs->SetTranslationMode(ADD_SECURITY);
  69. }
  70. // Get a list of all the rights
  71. hr = pLsa->raw_GetRights(server,&pRights);
  72. if ( SUCCEEDED(hr) )
  73. {
  74. LONG nRights = 0;
  75. long ndx[1];
  76. hr = SafeArrayGetUBound(pRights,1,&nRights);
  77. if ( SUCCEEDED(hr) )
  78. {
  79. for ( long i = 0 ; i <= nRights && !stArgs->Cache()->IsCancelled(); i++ )
  80. {
  81. BSTR right;
  82. ndx[0] = i;
  83. hr = SafeArrayGetElement(pRights,ndx,&right);
  84. if ( SUCCEEDED(hr) )
  85. {
  86. swprintf(currPath,L"%s\\%s",szServerName,(WCHAR*)right);
  87. if( stat )
  88. {
  89. stat->DisplayPath(currPath);
  90. }
  91. // Get a list of users who have this right
  92. hr = pLsa->raw_GetUsersWithRight(server,right,&pUsers);
  93. if ( SUCCEEDED(hr))
  94. {
  95. LONG nUsers = 0;
  96. hr = SafeArrayGetUBound(pUsers,1,&nUsers);
  97. if ( SUCCEEDED(hr) )
  98. {
  99. BSTR user;
  100. PSID pSid = NULL;
  101. // PSID pTgt = NULL;
  102. for ( long j = 0 ; j <= nUsers ; j++ )
  103. {
  104. ndx[0] = j;
  105. hr = SafeArrayGetElement(pUsers,ndx,&user);
  106. if ( SUCCEEDED(hr))
  107. {
  108. // Get the user's sid
  109. pSid = SidFromString(user);
  110. if ( pSid )
  111. {
  112. stat->IncrementExamined(userright);
  113. // Lookup the user in the cache
  114. if (!bUseMapFile)
  115. node = cache->Lookup(pSid);
  116. else
  117. node = cache->LookupWODomain(pSid);
  118. if ( node )
  119. {
  120. if ( node == (TAcctNode*)-1 )
  121. {
  122. node = NULL;
  123. }
  124. if ( node && node->IsValidOnTgt() )
  125. {
  126. // Found the account in the cache
  127. // remove the right from the source user
  128. if ( (stArgs->TranslationMode() == REMOVE_SECURITY ||stArgs->TranslationMode() == REPLACE_SECURITY) )
  129. {
  130. hr = pLsa->raw_RemoveUserRight(server,user,right);
  131. if ( FAILED(hr))
  132. {
  133. err.SysMsgWrite(ErrE,hr,DCT_MSG_REMOVE_RIGHT_FAILED_SSSD,
  134. (WCHAR*)right,node->GetAcctName(),szServerName,hr);
  135. stat->IncrementSkipped(userright);
  136. }
  137. else
  138. {
  139. err.MsgWrite(0,DCT_MSG_REMOVED_RIGHT_SSSS,szServerName,right,stArgs->Source(),node->GetAcctName());
  140. }
  141. }
  142. if ( SUCCEEDED(hr) )
  143. {
  144. stat->IncrementChanged(userright);
  145. PSID sid = NULL;
  146. if (!bUseMapFile)
  147. sid = cache->GetTgtSid(node);
  148. else
  149. sid = cache->GetTgtSidWODomain(node);
  150. if ( sid )
  151. {
  152. WCHAR strSid[200];
  153. DWORD lenStrSid = DIM(strSid);
  154. GetTextualSid(sid,strSid,&lenStrSid);
  155. if ( (stArgs->TranslationMode() != REMOVE_SECURITY) )
  156. {
  157. hr = pLsa->raw_AddUserRight(server,SysAllocString(strSid),right);
  158. if ( FAILED(hr) )
  159. {
  160. err.SysMsgWrite(ErrE,hr,DCT_MSG_ADD_RIGHT_FAILED_SSSD,
  161. (WCHAR*)right,node->GetAcctName(),szServerName,hr);
  162. }
  163. else
  164. {
  165. err.MsgWrite(0,DCT_MSG_ADDED_RIGHT_SSSS,szServerName,right,stArgs->Target(),node->GetAcctName());
  166. }
  167. }
  168. free(sid);
  169. }
  170. }
  171. }
  172. }
  173. FreeSid(pSid);
  174. }
  175. else
  176. {
  177. err.MsgWrite(ErrW,DCT_MSG_INVALID_SID_STRING_S,user);
  178. }
  179. SysFreeString(user);
  180. }
  181. }
  182. }
  183. else
  184. {
  185. err.SysMsgWrite(ErrE,hr,DCT_MSG_USERS_WITH_RIGHT_COUNT_FAILED_SSD,(WCHAR*)right,szServerName,hr);
  186. }
  187. SafeArrayDestroy(pUsers);
  188. }
  189. else
  190. {
  191. err.MsgWrite(ErrE,DCT_MSG_GET_USERS_WITH_RIGHT_FAILED_SSD,(WCHAR*)right,szServerName,hr);
  192. }
  193. SysFreeString(right);
  194. }
  195. else
  196. {
  197. err.MsgWrite(ErrE,DCT_MSG_LIST_RIGHTS_FAILED_SD,szServerName,hr);
  198. break;
  199. }
  200. }
  201. }
  202. else
  203. {
  204. err.MsgWrite(ErrE,DCT_MSG_LIST_RIGHTS_FAILED_SD,szServerName,hr);
  205. }
  206. SafeArrayDestroy(pRights);
  207. }
  208. else
  209. {
  210. err.MsgWrite(ErrE,DCT_MSG_LIST_RIGHTS_FAILED_SD,szServerName,hr);
  211. }
  212. if( stat )
  213. {
  214. stat->DisplayPath(L"");
  215. }
  216. // set the translation mode back to its original value
  217. stArgs->SetTranslationMode(mode);
  218. return hr;
  219. }