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.

589 lines
19 KiB

  1. <%
  2. '-------------------------------------------------------------------------
  3. ' inc_accountsgroups.asp: Some common functions for accounts and groups
  4. '
  5. ' Copyright (c) Microsoft Corporation. All rights reserved.
  6. '
  7. ' Date Description
  8. ' 04/08/2000 Creation date
  9. '-------------------------------------------------------------------------
  10. 'Error Messages
  11. Dim L_DOMAINFAILED_ERRORMESSAGE
  12. Dim L_FAILEDTOGETUSERACCOUNTS_ERRORMESSAGE
  13. Dim L_FAILEDTOGETGROUPACCOUNTS_ERRORMESSAGE
  14. Dim L_FAILEDTOGETSYSTEMACCOUNTS_ERRORMESSAGE
  15. Dim L_FAILEDTORETRIEVEMEMBERS_ERRORMESSAGE
  16. L_DOMAINFAILED_ERRORMESSAGE = objLocMgr.GetString("sacoremsg.dll","&HC020004D", varReplacementStrings)
  17. L_FAILEDTOGETUSERACCOUNTS_ERRORMESSAGE = objLocMgr.GetString("sacoremsg.dll","&HC020004E", varReplacementStrings)
  18. L_FAILEDTOGETGROUPACCOUNTS_ERRORMESSAGE = objLocMgr.GetString("sacoremsg.dll","&HC020004F", varReplacementStrings)
  19. L_FAILEDTOGETSYSTEMACCOUNTS_ERRORMESSAGE = objLocMgr.GetString("sacoremsg.dll","&HC0200050", varReplacementStrings)
  20. L_FAILEDTORETRIEVEMEMBERS_ERRORMESSAGE= objLocMgr.GetString("sacoremsg.dll","&HC0200051", varReplacementStrings)
  21. 'CONSTANTS
  22. 'CONST G_strGroupsNotReq = ":CREATOR GROUP SERVER:CREATOR OWNER SERVER:LOCAL:PROXY:"
  23. 'CONST G_strDomainNameNotReq = ":EVERYONE:CREATOR GROUP:CREATOR OWNER:"
  24. 'CONST G_strGroupsReq = "Authenticated Users:TERMINAL SERVER USER"
  25. '-------------------------------------------------------------------------
  26. 'Function name: getGroupsNotReq
  27. 'Description: gets the groups not required
  28. 'Input Variables: Connection to the WMI
  29. 'Output Variables: None
  30. 'Returns: ":" separaterd string of the groups not required
  31. '-------------------------------------------------------------------------
  32. function getGroupsNotReq(objService)
  33. ' The groups (in English) and corresponding Well known SIDs are
  34. ' CREATOR GROUP SERVER S-1-3-3
  35. ' CREATOR OWNER SERVER S-1-3-2
  36. ' LOCAL S-1-2-0
  37. ' PROXY S-1-5-8
  38. ' BUILTIN S-1-5-32 (BUILTIN as an win32_SystemAccount object
  39. ' is only on XP, not on W2K server)
  40. Dim arrSid(4)
  41. arrSid(0) = "S-1-3-3"
  42. arrSid(1) = "S-1-3-2"
  43. arrSid(2) = "S-1-2-0"
  44. arrSid(3) = "S-1-5-8"
  45. arrSid(4) = "S-1-5-32"
  46. getGroupsNotReq = constructNameList(arrSid, objService)
  47. End function
  48. '-------------------------------------------------------------------------
  49. 'Function name: getDomainNameNotReq
  50. 'Description: gets the domain names not required
  51. 'Input Variables: Connection to the WMI
  52. 'Output Variables: None
  53. 'Returns: ":" separaterd string of the Domain names not required
  54. '-------------------------------------------------------------------------
  55. function getDomainNameNotReq(objService)
  56. ' The Domain names (in English) and corresponding Well known SIDs are
  57. ' EVERYONE S-1-1-0
  58. ' CREATOR GROUP S-1-3-1
  59. ' CREATOR OWNER S-1-3-0
  60. Dim arrSid(2)
  61. arrSid(0) = "S-1-1-0"
  62. arrSid(1) = "S-1-3-1"
  63. arrSid(2) = "S-1-3-0"
  64. getDomainNameNotReq = constructNameList(arrSid, objService)
  65. End function
  66. '-------------------------------------------------------------------------
  67. 'Function name: getGroupsReq
  68. 'Description: gets the groups required
  69. 'Input Variables: Connection to the WMI
  70. 'Output Variables: None
  71. 'Returns: ":" separaterd string of the groups required
  72. '-------------------------------------------------------------------------
  73. function getGroupsReq(objService)
  74. ' The groups (in English) and corresponding Well known SIDs are
  75. ' Authenticated Users S-1-5-11
  76. ' TERMINAL SERVER USER S-1-5-13
  77. Dim arrSid(1)
  78. Dim sid
  79. arrSid(0) = "S-1-5-11"
  80. arrSid(1) = "S-1-5-13"
  81. getGroupsReq = constructNameList(arrSid, objService)
  82. ' Get rid of the begin and end ":"
  83. getGroupsReq = Mid(getGroupsReq, 2, len(getGroupsReq)-2)
  84. End function
  85. '-------------------------------------------------------------------------
  86. 'Function name: getNTAuthorityDomainName
  87. 'Description: gets the NT Authority Domain Name for Localization
  88. 'Input Variables: Connection to the WMI
  89. 'Output Variables: None
  90. 'Returns: string of NT Authority Domain Name
  91. '-------------------------------------------------------------------------
  92. function getNTAuthorityDomainName(objService)
  93. Dim strWelKnownSid
  94. Dim objSid
  95. ' Get the NT Authority Domain name from a well known SID
  96. strWelKnownSid = "S-1-5-11"
  97. set objSid = objService.Get("Win32_SID.SID=""" & strWelKnownSid & """")
  98. getNTAuthorityDomainName = objSid.ReferencedDomainName
  99. set objSid = nothing
  100. End function
  101. '-------------------------------------------------------------------------
  102. 'Function name: getBuiltinDomainName
  103. 'Description: gets the BUILTIN Domain Name for Localization
  104. 'Input Variables: Connection to the WMI
  105. 'Output Variables: None
  106. 'Returns: string of BUILTIN Domain Name
  107. '-------------------------------------------------------------------------
  108. function getBuiltinDomainName(objService)
  109. Dim strWelKnownSid
  110. Dim objSid
  111. ' Get the NT Authority Domain name from a well known SID
  112. strWelKnownSid = "S-1-5-32"
  113. set objSid = objService.Get("Win32_SID.SID=""" & strWelKnownSid & """")
  114. getBuiltinDomainName = objSid.ReferencedDomainName
  115. set objSid = nothing
  116. End function
  117. '-------------------------------------------------------------------------
  118. 'Function name: constructNameList
  119. 'Description: construct a list of Name based on the SIDs
  120. 'Input Variables: Connection to the WMI
  121. 'Output Variables: None
  122. 'Returns: ":" separaterd string of the names
  123. '-------------------------------------------------------------------------
  124. Function constructNameList(arrSid, objService)
  125. Dim sid
  126. Dim objSid
  127. constructNameList = ":"
  128. ' Get the name for each SID and concatenate it into the list
  129. For Each sid in arrSid
  130. set objSid = objService.Get("Win32_SID.SID=""" & sid & """")
  131. constructNameList = constructNameList & objSid.AccountName & ":"
  132. Next
  133. set objSid = nothing
  134. End Function
  135. '-------------------------------------------------------------------------
  136. 'Function name: getSystemAccounts
  137. 'Description: gets the system accounts of localmachine
  138. 'Input Variables: Connection to the WMI
  139. 'Output Variables: None
  140. 'Returns: Chr(1) separated string groups in the domain.
  141. '-------------------------------------------------------------------------
  142. function getSystemAccounts(objService)
  143. Err.Clear
  144. Dim objCollectionofSystemAccounts
  145. Dim objSysAcccount
  146. Dim strQuery
  147. Dim strSysAcccounts
  148. Dim strCompName
  149. Dim arrTemp,i
  150. Dim strDomainName
  151. Dim strGroupsNotReq
  152. Dim strDomainNameNotReq
  153. Dim strGroupsReq
  154. strGroupsNotReq = getGroupsNotReq(objService)
  155. strDomainNameNotReq = getDomainNameNotReq(objService)
  156. strGroupsReq = getGroupsReq(objService)
  157. strCompName = GetComputerName()
  158. strSysAcccounts =""
  159. 'strDomainName ="NT Authority"
  160. strDomainName =getNTAuthorityDomainName(objService)
  161. strQuery = "SELECT Name From Win32_SystemAccount"
  162. Set objCollectionofSystemAccounts = objService.ExecQuery(strQuery)
  163. If objCollectionofSystemAccounts.Count = 0 then
  164. getSystemAccounts = strSysAcccounts
  165. Exit function
  166. End if
  167. For each objSysAcccount in objCollectionofSystemAccounts
  168. if instr(ucase(strGroupsNotReq),":"& ucase(objSysAcccount.Name) &":") = 0 then
  169. if instr(ucase(strDomainNameNotReq),":"& ucase(objSysAcccount.Name) &":") = 0 then
  170. strSysAcccounts = strSysAcccounts & chr(1)& strDomainName &"\"&objSysAcccount.Name &chr(2)&strCompName&"\"&objSysAcccount.Name
  171. else
  172. strSysAcccounts = strSysAcccounts & chr(1)& ""&objSysAcccount.Name &chr(2)&strCompName&"\"&objSysAcccount.Name
  173. End if
  174. End if
  175. Next
  176. arrTemp = split(strGroupsReq,":")
  177. for i= 0 to ubound(arrTemp)
  178. If instr(ucase(strSysAcccounts), ucase(arrTemp(i))) = 0 Then
  179. strSysAcccounts = strSysAcccounts & chr(1)& strDomainName &"\"&arrTemp(i) &chr(2)&strCompName&"\"&arrTemp(i)
  180. End If
  181. next
  182. Set objCollectionofSystemAccounts=Nothing
  183. set objSysAcccount = Nothing
  184. If Err.number <> 0 Then
  185. SetErrMsg L_FAILEDTOGETSYSTEMACCOUNTS_ERRORMESSAGE & "(" & Hex(Err.Number) & ")"
  186. getSystemAccounts = ""
  187. Exit Function
  188. End If
  189. getSystemAccounts = strSysAcccounts
  190. End Function
  191. '-------------------------------------------------------------------------
  192. 'Function name: getConnectedDomain
  193. 'Description: gets the domain in which the machine is present.
  194. 'Input Variables: Connection to the WMI
  195. 'Output Variables: None
  196. 'Returns: Domain Name
  197. '-------------------------------------------------------------------------
  198. Function getConnectedDomain(objService)
  199. Err.Clear
  200. Dim objColletionofSystem
  201. Dim objSystem
  202. Dim strDomainName
  203. strDomainName =""
  204. Set objColletionofSystem = objService.InstancesOf ("Win32_ComputerSystem")
  205. For each objSystem in objColletionofSystem
  206. If objSystem.DomainRole <> 2 Then
  207. strDomainName = getShortDomainName(objSystem.Domain)
  208. End IF
  209. Next
  210. If Err.number <> 0 then
  211. SetErrMsg L_DOMAINFAILED_ERRORMESSAGE & "(" & Hex(Err.Number) & ")"
  212. getConnectedDomain = strDomainName
  213. Exit Function
  214. End If
  215. getConnectedDomain = strDomainName
  216. End Function
  217. '-------------------------------------------------------------------------
  218. 'Function name: getShortDomainName
  219. 'Description: gets the short domain name (vs. DNS name)
  220. 'Input Variables: domain name that may be a domain DNS name
  221. 'Output Variables: None
  222. 'Returns: Short Domain Name
  223. '-------------------------------------------------------------------------
  224. Function getShortDomainName(strDomainName)
  225. Err.Clear
  226. Dim arrDomainName
  227. '
  228. ' If domain name contains char ".", it's a DNS domain name
  229. ' e.g. redmond.corp.microsoft.com. We need to get the shortname
  230. ' which is "redmond". That is because ADSI GetObject only accepts
  231. ' short domain name.
  232. '
  233. If InStr(strDomainName, ".") Then
  234. arrDomainName = Split(strDomainName, ".")
  235. getShortDomainName = arrDomainName(0)
  236. Else
  237. getShortDomainName = strDomainName
  238. End If
  239. End Function
  240. '-------------------------------------------------------------------------
  241. 'Function name: getUserAccounts
  242. 'Description: gets the users of the given domain.
  243. 'Input Variables: Connection to the WMI
  244. ' Domain name
  245. 'Output Variables: None
  246. 'Returns: Chr(1) separated string users in the domain.
  247. '-------------------------------------------------------------------------
  248. Function getUserAccounts(objService,strDomain)
  249. Err.Clear
  250. Dim objCollectionofUsers
  251. Dim objUser
  252. Dim strQuery
  253. Dim strUsers
  254. strUsers =""
  255. if Trim(strDomain) = "" then
  256. getUserAccounts = strUsers
  257. Exit function
  258. end if
  259. strQuery = "SELECT Name From Win32_UserAccount WHERE Domain=" & "'" & strDomain & "'"
  260. Set objCollectionofUsers = objService.ExecQuery(strQuery,"WQL",48,null)
  261. For each objUser in objCollectionofUsers
  262. strUsers = strUsers & chr(1)& objUser.Name &chr(2) &objUser.Name
  263. Next
  264. Set objCollectionofUsers=Nothing
  265. set objUser = Nothing
  266. If Err.number <> 0 Then
  267. SetErrMsg L_FAILEDTOGETUSERACCOUNTS_ERRORMESSAGE & "(" & Hex(Err.Number) & ")"
  268. getUserAccounts = strUsers
  269. Exit Function
  270. End If
  271. getUserAccounts = strUsers
  272. End Function
  273. '-------------------------------------------------------------------------
  274. 'Function name: getGroups
  275. 'Description: gets the groups of the given domain.
  276. 'Input Variables: Connection to the WMI
  277. ' Domain name
  278. 'Output Variables: None
  279. 'Returns: Chr(1) separated string groups in the domain.
  280. '-------------------------------------------------------------------------
  281. Function getGroups(objService,strDomain)
  282. Err.Clear
  283. Dim objCollectionofGroups
  284. Dim objGroup
  285. Dim strQuery
  286. Dim strGroups
  287. strGroups =""
  288. if Trim(strDomain) = "" then
  289. getGroups = strGroups
  290. Exit function
  291. end if
  292. strQuery = "SELECT Name From Win32_Group WHERE Domain=" & "'" & strDomain & "'"
  293. Set objCollectionofGroups = objService.ExecQuery(strQuery,"WQL",48,null)
  294. if not isnull(objCollectionofGroups) then
  295. For each objGroup in objCollectionofGroups
  296. strGroups = strGroups & chr(1)& strDomain & "\" & objGroup.Name & chr(2)& objGroup.Name
  297. Next
  298. End if
  299. if Err.number <> 0 then
  300. SetErrMsg L_FAILEDTOGETGROUPACCOUNTS_ERRORMESSAGE & "(" & Hex(Err.Number) & ")"
  301. getGroups = ""
  302. Exit Function
  303. End If
  304. getGroups = strGroups
  305. End Function
  306. '-------------------------------------------------------------------------
  307. 'Function name: ServetoListBox
  308. 'Description: gets the groups of the given domain.
  309. 'Input Variables: Chr(1) separated string groups in the domain.
  310. 'Output Variables: None
  311. 'Returns: Output to the listbox
  312. '-------------------------------------------------------------------------
  313. Function ServetoListBox(strInput)
  314. Err.Clear
  315. Dim arrInput
  316. Dim nIndex
  317. Dim arrTemp
  318. arrInput = split(strInput,chr(1))
  319. for nIndex = 1 to ubound(arrInput)
  320. if instr(arrInput(nIndex),chr(2)) = 0 then
  321. Response.write "<OPTION VALUE=" & Chr(34) & arrInput(nIndex) & Chr(34) & "> " _
  322. & arrInput(nIndex) &"</OPTION>"
  323. else
  324. arrTemp = split(arrInput(nIndex),chr(2))
  325. Response.write "<OPTION VALUE=" & Chr(34) & arrTemp(0) & Chr(34) & "> " _
  326. & arrTemp(1) &"</OPTION>"
  327. end if
  328. next
  329. End Function
  330. '-------------------------------------------------------------------------
  331. 'Function name: isValidInstance
  332. 'Description: Checks the instance for valid ness.
  333. 'Input Variables: objService - object to WMI
  334. ' strClassName - WMI class name
  335. ' strPropertyName - Property name of the class
  336. '
  337. 'Output Variables: None
  338. 'Returns: Returns true on Valid Instance ,
  339. ' False on invalid and also on Error
  340. ' Checks whether the given instance is valid in WMI.Returns true on valid
  341. ' false on invalid or Error.
  342. '-------------------------------------------------------------------------
  343. Function isValidInstance(objService,strClassName,strPropertyName)
  344. Err.Clear
  345. Dim strInstancePath
  346. Dim objInstance
  347. On Error Resume Next
  348. strInstancePath = strClassName & "." & strPropertyName
  349. Set objInstance = objservice.Get(strInstancePath)
  350. if NOT isObject(objInstance) or Err.number <> 0 Then
  351. isValidInstance = FALSE
  352. Err.Clear
  353. Else
  354. isValidInstance = TRUE
  355. End If
  356. End Function
  357. '---------------------------------------------------------------------
  358. ' Function name: getLocalUsersList
  359. ' Description: Gets the members of the logon domain
  360. ' Input Variables: WMI Connection
  361. ' Output Variables: None
  362. ' Returns: chr(1) seperated members of the connected domain
  363. ' Global Variables: In: L_(*)
  364. '---------------------------------------------------------------------
  365. Function getLocalUsersList(objService)
  366. Err.Clear
  367. Dim strGroupsNUsers
  368. 'Dim strDomain
  369. Dim strCompName
  370. Dim strTemp
  371. 'Intialization of the variables to get the domain & computer name
  372. strTemp= ""
  373. strGroupsNUsers = ""
  374. 'strDomain = getConnectedDomain(objService)
  375. strCompName= GetComputerName()
  376. 'Get the members of the local system
  377. strTemp = getUserAccounts(objService,strCompName)
  378. strTemp =replace(strTemp,chr(1),(chr(1)&strCompName &"\"))
  379. strGroupsNUsers = getSystemAccounts(objService) & strTemp
  380. strGroupsNUsers = replace(strGroupsNUsers, chr(2)& UCASE(strCompName) &"\",chr(2))
  381. if Err.number <> 0 then
  382. ServeFailurePage L_FAILEDTORETRIEVEMEMBERS_ERRORMESSAGE & "(" & Hex(Err.number) & ")"
  383. End if
  384. getLocalUsersList = strGroupsNUsers
  385. End Function
  386. '---------------------------------------------------------------------
  387. ' Function name: getLocalUsersListEx
  388. ' Description: Gets the members of the logon domain
  389. ' Input Variables: WMI Connection
  390. ' intType
  391. ' Type - 0 for all users
  392. ' Type - 1 for system/builtin users
  393. ' Type - 2 for normal users
  394. ' Output Variables: None
  395. ' Returns: chr(1) seperated members of the connected domain
  396. ' Global Variables: In: L_(*)
  397. '---------------------------------------------------------------------
  398. Function getLocalUsersListEx(objService,intType)
  399. Err.Clear
  400. Dim strGroupsNUsers
  401. 'Dim strDomain
  402. Dim strCompName
  403. Dim strTemp
  404. 'Intialization of the variables to get the domain & computer name
  405. strTemp= ""
  406. strGroupsNUsers = ""
  407. 'strDomain = getConnectedDomain(objService)
  408. strCompName= GetComputerName()
  409. 'Get the members of the local system
  410. if (intType=1 or intType=0) Then
  411. strTemp = getSystemAccounts(objService)
  412. strGroupsNUsers = strGroupsNUsers & strTemp
  413. end if
  414. if (intType=2 or intType=0) Then
  415. strTemp = getUserAccounts(objService,strCompName)
  416. strTemp =replace(strTemp,chr(1),(chr(1)&strCompName &"\"))
  417. strGroupsNUsers = strGroupsNUsers & strTemp
  418. end if
  419. strGroupsNUsers = replace(strGroupsNUsers, chr(2)& UCASE(strCompName) &"\",chr(2))
  420. if Err.number <> 0 then
  421. ServeFailurePage L_FAILEDTORETRIEVEMEMBERS_ERRORMESSAGE & "(" & Hex(Err.number) & ")"
  422. End if
  423. getLocalUsersListEx = strGroupsNUsers
  424. End Function
  425. '-----------------------------------------------------------------------------------
  426. 'Function name : Getbuiltingroups
  427. 'Description : Serves in getting BUILTIN groups and writes into
  428. ' a select box.
  429. 'Input Variables Object G_objService
  430. 'Output Variables
  431. ' Boolean -Returns True/False on Success/Failure
  432. 'Global Variables
  433. ' L_* (in) -Localized strings
  434. '-----------------------------------------------------------------------------------
  435. Function Getbuiltingroups(objService)
  436. Err.Clear
  437. Dim objCollection
  438. Dim objInstance
  439. Dim strQuery
  440. Dim strBuiltinGroups
  441. Dim Domainname
  442. Domainname = getBuiltinDomainName(objService)
  443. strBuiltinGroups =""
  444. strQuery = "SELECT * From Win32_Group WHERE Domain=" & "'" & Domainname & "'"
  445. Set objCollection = objService.ExecQuery(strQuery)
  446. If objCollection.Count = 0 then
  447. Exit function
  448. End if
  449. For each objInstance in objCollection
  450. strBuiltinGroups = strBuiltinGroups & chr(1)& Domainname & "\" & objInstance.Name & chr(2)& objInstance.Name
  451. Next
  452. Set objCollection = Nothing
  453. Getbuiltingroups = strBuiltinGroups
  454. End Function
  455. %>