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.

365 lines
12 KiB

  1. <%
  2. '-------------------------------------------------------------------------
  3. ' inc_usersngroups.asp: Some common functions in groups & users pages
  4. '
  5. ' Copyright (c) Microsoft Corporation. All rights reserved.
  6. '
  7. ' Date Description
  8. ' 04/08/2000 Creation date
  9. '-------------------------------------------------------------------------
  10. Const CONST_FULLCONROL = &H1F01FF
  11. ' Default home directory
  12. Dim CONST_DEFAULTHOMEDIR
  13. ' Key name for Users and Groups
  14. Const CONST_USERGROUP_KEYNAME = "SoftWare\Microsoft\ServerAppliance\UserGroup"
  15. ' Default home directory optional defined by OEM
  16. Const CONST_USERDIR_VALUENAME = "UserDir"
  17. ' Return value of CreateHomeDirectory
  18. Const CONST_CREATDIRECTORY_ERROR = 0
  19. Const CONST_CREATDIRECTORY_EXIST = 1
  20. Const CONST_CREATDIRECTORY_SUCCESS = 2
  21. CONST_DEFAULTHOMEDIR = GetSystemDrive() & "\Users\"
  22. '---------------------------------------------------------------------
  23. ' Function name: isValidMember
  24. ' Description: checks the validity of the user in WiNT name space
  25. ' Input Variables: User name
  26. ' Output Variables: None
  27. ' Returns: True if User path exists in Winnt space else false
  28. '---------------------------------------------------------------------
  29. Function isValidMember(strMember, ByVal sAdminID, ByVal sAdminPSW, ByRef errorCode)
  30. on error resume next
  31. Err.Clear
  32. Dim objTemp
  33. Dim objPath
  34. objPath = "WinNT://" + strMember + ",user"
  35. set objTemp = GetObject(objPath)
  36. errorCode = Err.Number
  37. 'If it's not a valid user, check whether it's a valid group
  38. if errorCode <> 0 then
  39. Err.Clear
  40. objPath = "WinNT://" + strMember
  41. set objTemp = GetObject(objPath)
  42. errorCode = Err.Number
  43. If errorCode <> 0 Then
  44. ' If it's neither a valid user nor a group, check if it's a valid domain member
  45. If IsValidDomainMember( strMember, sAdminID, sAdminPSW, errorCode) Then
  46. isValidMember = true
  47. Exit Function
  48. End If
  49. isValidMember = false
  50. SA_TraceOut "Inc_UsersNGroups", "IsValidMember(" + objPath + ") failed: " + CStr(Hex(errorCode))
  51. Else
  52. isValidMember = true
  53. End If
  54. else
  55. isValidMember = true
  56. end if
  57. End FUnction
  58. Function IsValidDomainMember(ByVal sDomainUser, ByVal sAdminID, ByVal sAdminPSW, ByRef errorCode)
  59. Dim objComputer
  60. Dim objUser
  61. Dim sDomainUserPath
  62. On Error Resume Next
  63. Err.Clear
  64. 'SA_TraceOut "IsValidDomainMember", "Domain User: " + sDomainUser + " AdminID: " + sAdminID
  65. Set objComputer = GetObject("WinNT:")
  66. sDomainUserPath = "WinNT://" + sDomainUser + ",user"
  67. Set objUser = objComputer.OpenDSObject(sDomainUserPath, sAdminID, sAdminPSW, 1 )
  68. 'If it's not a valid domain user, check whether it's a valid domain group
  69. If ( Err.Number <> 0 ) Then
  70. errorCode = Err.Number
  71. Err.Clear
  72. sDomainUserPath = "WinNT://" + sDomainUser
  73. Set objUser = objComputer.OpenDSObject(sDomainUserPath, sAdminID, sAdminPSW, 1 )
  74. ' If it's neither a valid domain user nor a domain group, it's invalid input
  75. If ( Err.Number <> 0 ) Then
  76. errorCode = Err.Number
  77. SA_TraceOut "IsValidDomainMember", "objComputer.OpenDSObject failed: " + CStr(Hex(errorCode)) + " : " + Err.Description
  78. IsValidDomainMember = FALSE
  79. Else
  80. errorCode = 0
  81. IsValidDomainMember = TRUE
  82. End If
  83. Else
  84. errorCode = 0
  85. IsValidDomainMember = TRUE
  86. End If
  87. Set objUser = nothing
  88. Set objComputer = nothing
  89. End Function
  90. Function AddUserToGroup(ByVal sGroup, ByVal sUser, ByVal sAdminID, ByVal sAdminPSW)
  91. Dim objComputer
  92. Dim objGroup
  93. Dim objUser
  94. On Error Resume Next
  95. Err.Clear
  96. 'SA_TraceOut "AddUserToGroup", "Group: " + sGroup + " User: " + sUser + " AdminID: " + sAdminID
  97. Set objComputer = GetObject("WinNT:")
  98. Set objGroup = objComputer.OpenDSObject("WinNT://" + GetComputerName() + "/" + sGroup,_
  99. sAdminID, sAdminPSW, 1 )
  100. If ( Err.Number <> 0 ) Then
  101. SA_TraceOut "AddUserToGroup", "objComputer.OpenDSObject failed: " + CStr(Hex(Err.Number)) + " : " + Err.Description
  102. SA_TraceOut "AddUserToGrop", "Attempted to open: " +"WinNT://" + GetComputerName() + "/" + sGroup
  103. AddUserToGroup = FALSE
  104. Exit Function
  105. End If
  106. objGroup.Add( "WinNT://" + sUser )
  107. If ( Err.Number <> 0 ) Then
  108. SA_TraceOut "AddUserToGroup", "objGroup.Add failed: " + CStr(Hex(Err.Number)) + " : " + Err.Description
  109. AddUserToGroup = FALSE
  110. Set objComputer = nothing
  111. Set objGroup = nothing
  112. Exit Function
  113. End If
  114. objGroup.SetInfo
  115. If ( Err.Number <> 0 ) Then
  116. SA_TraceOut "AddUserToGroup", "objGroup.SetInfo failed: " + CStr(Hex(Err.Number)) + " : " + Err.Description
  117. AddUserToGroup = FALSE
  118. Set objComputer = nothing
  119. Set objGroup = nothing
  120. Exit Function
  121. End If
  122. Set objComputer = nothing
  123. Set objGroup = nothing
  124. AddUserToGroup = TRUE
  125. End Function
  126. Function CreateHomeDirectory( ByVal strHomeDirectory, ByRef ObjFolder)
  127. Err.Clear
  128. On Error Resume Next
  129. CreateHomeDirectory= CONST_CREATDIRECTORY_ERROR
  130. If ( ObjFolder.FolderExists( strHomeDirectory ) ) Then
  131. Call SA_TraceOut("inc_userngroups", "Folder exist")
  132. CreateHomeDirectory = CONST_CREATDIRECTORY_EXIST
  133. Exit Function
  134. End If
  135. Dim strParentDirectory
  136. Dim endPosition
  137. endPosition = InStrRev( strHomeDirectory, "\" )
  138. If( endPosition = 0 )Then
  139. Call SA_TraceOut("inc_userngroups", "Error home directory" )
  140. Exit Function
  141. End If
  142. strParentDirectory = Left( strHomeDirectory, ( endPosition - 1 ) )
  143. If( CreateHomeDirectory( strParentDirectory, ObjFolder) = CONST_CREATDIRECTORY_ERROR ) Then
  144. Call SA_TraceOut("inc_userngroups", "create parent folder error" )
  145. Exit Function
  146. End If
  147. ObjFolder.CreateFolder( strHomeDirectory )
  148. If Err.Number <> 0 Then
  149. Call SA_TraceOut("inc_userngroups", "Failed to create folder" & "(" & Hex(Err.Number) & ")" )
  150. Exit Function
  151. End If
  152. CreateHomeDirectory = CONST_CREATDIRECTORY_SUCCESS
  153. End Function
  154. Function SetHomeDirectoryPermission( ByVal strComputerName, ByVal strUserName, ByVal strHomeDirectory )
  155. Err.Clear
  156. On Error Resume Next
  157. SetHomeDirectoryPermission = FALSE
  158. Dim objService 'to hold WMI connection object
  159. Dim strTemp 'to hold temp value
  160. Dim objSecSetting 'to hold security setting value
  161. Dim objSecDescriptor 'to hold security descriptor value
  162. Dim strPath 'to hold Path
  163. Dim objDACL 'to hold DACL value
  164. Dim objUserAce
  165. Dim objAdminAce
  166. Dim objSystemAce
  167. Dim retval 'holds return value
  168. Call SA_TraceOut(SA_GetScriptFileName(), "SetHomeDirectoryPermission( " + strComputerName + ", " + strUserName + ", " + strHomeDirectory )
  169. Set objService = getWMIConnection(CONST_WMI_WIN32_NAMESPACE)
  170. objService.security_.impersonationlevel = 3
  171. 'get the sec seting for file
  172. strPath = "Win32_LogicalFileSecuritySetting.Path='" & strHomeDirectory & "'"
  173. set objSecSetting = objService.Get(strPath)
  174. if Err.number <> 0 then
  175. Call SA_TraceOut ("inc_userngroups", "Failed to get Sec object for dir " & "(" & Hex(Err.Number) & ")" )
  176. exit function
  177. end if
  178. 'get the ace's for users
  179. if NOT GetUserAce(objService, strUserName , strComputerName, CONST_FULLCONROL, objUserAce ) then
  180. Call SA_TraceOut ("inc_userngroups", "Failed to get ACE object for user, error:" & "(" & Hex(Err.Number) & ")" )
  181. exit function
  182. end if
  183. 'get the ace's for System account
  184. if NOT GetSystemAce(objService, SA_GetAccount_System() , strComputerName, CONST_FULLCONROL, objSystemAce ) then
  185. Call SA_TraceOut ("inc_userngroups", "Failed to get ACE object for SYSTEM, error:" & "(" & Hex(Err.Number) & ")" )
  186. exit function
  187. end if
  188. 'get the ace's for Administrators
  189. if NOT GetGroupAce(objService, SA_GetAccount_Administrators() , strComputerName, CONST_FULLCONROL, objAdminAce ) then
  190. Call SA_TraceOut ("inc_userngroups", "Failed to get ACE object for Administrators, error:" & "(" & Hex(Err.Number) & ")" )
  191. exit function
  192. end if
  193. Set objSecDescriptor = objService.Get("Win32_SecurityDescriptor").SpawnInstance_()
  194. if Err.Number <> 0 then
  195. Call SA_TraceOut ("inc_userngroups", "Failed to get create the Win32_SecurityDescriptor object " & "(" & Hex(Err.Number) & ")" )
  196. exit function
  197. end if
  198. objSecDescriptor.Properties_.Item("DACL") = Array()
  199. Set objDACL = objSecDescriptor.Properties_.Item("DACL")
  200. objDACL.Value(0) = objUserAce
  201. objDACL.Value(1) = objAdminAce
  202. objDACL.Value(2) = objSystemAce
  203. objSecDescriptor.Properties_.Item("ControlFlags") = 32772
  204. Set objSecDescriptor.Properties_.Item("Owner") = objUserAce.Trustee
  205. Err.Clear
  206. retval = objSecSetting.SetSecurityDescriptor( objSecDescriptor )
  207. if Err.number <> 0 then
  208. Call SA_TraceOut ( "site_new", "Failed to set the Security Descriptor for Root dir " & "(" & Hex(Err.Number) & ")" )
  209. exit function
  210. end if
  211. SetHomeDirectoryPermission = TRUE
  212. 'Release the objects
  213. set objService = nothing
  214. set objSecSetting = nothing
  215. set objSecDescriptor = nothing
  216. End Function
  217. Function GetUserAce(objService, strName, strDomain, nAccessMask, ByRef objACE)
  218. Dim strObjPath 'holds query string
  219. strObjPath = "Win32_UserAccount.Domain=" & chr(34) & strDomain & chr(34) & ",Name=" & chr(34) & strName & chr(34)
  220. Call SA_TraceOut( "inc_userngroups", "GetUserAce : " +strObjPath )
  221. GetUserAce = GetAce(strObjPath, objService, strName, strDomain, nAccessMask, objACE)
  222. End Function
  223. Function GetSystemAce(objService, strName, strDomain, nAccessMask, ByRef objACE)
  224. Dim strObjPath 'holds query string
  225. strObjPath = "Win32_SystemAccount.Domain=" & chr(34) & strDomain & chr(34) & ",Name=" & chr(34) & strName & chr(34)
  226. Call SA_TraceOut( "inc_userngroups", "GetSystemAce : " +strObjPath )
  227. GetSystemAce = GetAce(strObjPath, objService, strName, strDomain, nAccessMask, objACE)
  228. End Function
  229. Function GetGroupAce(objService, strName, strDomain, nAccessMask, ByRef objACE)
  230. Dim strObjPath 'holds query string
  231. strObjPath = "Win32_Group.Domain=" & chr(34) & strDomain & chr(34) & ",Name=" & chr(34) & strName & chr(34)
  232. Call SA_TraceOut( "inc_userngroups", "GetGroupAce : " + strObjPath )
  233. GetGroupAce = GetAce(strObjPath, objService, strName, strDomain, nAccessMask, objACE)
  234. End Function
  235. Function GetAce(strObjPath, objService, strName, strDomain, nAccessMask, ByRef objACE)
  236. Dim objAcct 'holds query result
  237. Dim objSID 'holds security identifier
  238. Dim objTrustee 'holds trustee value
  239. GetAce = FALSE
  240. set objAcct = objService.Get(strObjPath)
  241. if Err.number <> 0 then
  242. Call SA_TraceOut( "inc_userngroups", "Failed to get object " & "(" & Hex(Err.Number) & ")" )
  243. exit function
  244. end if
  245. set objSID = objService.Get("Win32_SID.SID='" & objAcct.SID & "'")
  246. if Err.number <> 0 then
  247. Call SA_TraceOut( "inc_userngroups", "Failed to get Win32_SID Object " & "(" & Hex(Err.Number) & ")" )
  248. exit function
  249. end if
  250. set objTrustee = objService.Get("Win32_Trustee").SpawnInstance_
  251. if Err.number <> 0 then
  252. Call SA_TraceOut( "inc_userngroups", "Failed to get new Instance of Win32_Trustee " & "(" & Hex(Err.Number) & ")" )
  253. exit function
  254. end if
  255. objTrustee.Name = strName
  256. objTrustee.Domain = strDomain
  257. objTrustee.SID = objSID.BinaryRepresentation
  258. objTrustee.SIDString = objSID.SID
  259. objTrustee.SidLength = objSID.SidLength
  260. set objACE = objService.Get("Win32_ACE").SpawnInstance_
  261. if Err.number <> 0 then
  262. Call SA_TraceOut( "inc_userngroups", "Failed to Create Win32_Ace Object " & "(" & Hex(Err.Number) & ")" )
  263. exit function
  264. end if
  265. objACE.AccessMask = nAccessMask
  266. objACE.Aceflags = 3
  267. objACE.AceType = 0
  268. objACE.Trustee = objTrustee
  269. GetAce = TRUE
  270. 'Release objects
  271. set objAcct = nothing
  272. set objSID = nothing
  273. set objTrustee = nothing
  274. End Function
  275. %>