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.

1363 lines
39 KiB

  1. <%
  2. '-------------------------------------------------------------------------
  3. ' inc_global.asp: Page level Functions
  4. ' Copyright (c) Microsoft Corporation. All rights reserved.
  5. '-------------------------------------------------------------------------
  6. '-------------------------------------------------------------------------
  7. 'Note :When ever you are using these functions in your asp files the
  8. ' localized error messages should be declared in your files
  9. '-------------------------------------------------------------------------
  10. 'Const L_WMICONNECTIONFAILED_ERRORMESSAGE = "Connection to WMI Failed"
  11. 'Const L_LOCALIZATIONOBJECTFAILED_ERRORMESSAGE= "Unable to Create Localization Object"
  12. 'Const L_COMPUTERNAME_ERRORMESSAGE = "Error occurred while getting Computer Name"
  13. 'Namespace constants
  14. Const CONST_WMI_WIN32_NAMESPACE = "root\cimv2" ' wmi namespace
  15. Const CONST_WMI_IIS_NAMESPACE = "root\MicrosoftIISv1" ' wmi namespace
  16. Const CONST_WMI_IIS60_NAMESPACE = "root\MicrosoftIISv2" ' wmi namespace
  17. Const CONST_OSNAME_XPE = "Microsoft Windows XP Professional"
  18. Const CONST_OSNAME_XPSERVER = "Microsoft Windows XP Server"
  19. Const CONST_OSNAME_W2KSERVER = "Microsoft Windows 2000 Server"
  20. Const CONST_SITENAME_ADMINISTRATION = "Administration"
  21. Const CONST_SITENAME_SHARES = "Shares"
  22. Const CONST_WEBFRAMEWORK_REGKEY = "Software\Microsoft\ServerAppliance\WebFramework"
  23. Const CONST_ADMINSITEID_REGVAL = "AdministrationSiteID"
  24. Const CONST_SHARESSITEID_REGVAL = "SharesSiteID"
  25. %>
  26. <script runat="server" language="javascript">
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. // UTF8toUnicode
  29. //
  30. // @jfunc This function converts a string from UTF-8 to Unicode encoding.
  31. //
  32. // @rdesc Newly formatted string
  33. //
  34. // @ex Usage: strShow = UTF8toUnicode("\xC2\xBD\xC2\xA6\xE8\xAB\x8B");
  35. ///////////////////////////////////////////////////////////////////////////////////////////////////
  36. function UTF8ToUnicode(
  37. strInUTF8 // @parm The string in UTF-8 encoding
  38. )
  39. {
  40. // Validate input.
  41. if (null == strInUTF8)
  42. return null;
  43. // The following line fixes a problem when the input is not a valid java script string object.
  44. // This can happen, for example, if the caller passes the output of QueryString() to this
  45. // function; InterDev pops up the following error message if this happen: the error code is
  46. // object doesn't support this property or method. This line of code makes sure we use a valid
  47. // java script string object.
  48. var strUTF8 = ""+strInUTF8;
  49. // Map string.
  50. var strUni = ""; // Unicode encoded string.
  51. for(var i=0; i<strUTF8.length; )
  52. {
  53. // Get three values from current position.
  54. var chr1 = strUTF8.charCodeAt(i);
  55. var chr2 = strUTF8.charCodeAt(i+1);
  56. var chr3 = strUTF8.charCodeAt(i+2);
  57. if (chr1 < 0x80)
  58. {
  59. // A char in range 0-0x7f don't need any work. just copy the char.
  60. strUni += strUTF8.charAt(i);
  61. i++;
  62. }
  63. else if (0xC0 == (chr1 & 0xE0))
  64. {
  65. // A char in range 0x80-0x7ff is converted to 2 bytes as follows:
  66. // 0000 0yyy xxxx xxxx -> 110y yyxx 10xx xxxx
  67. // The following logic rebuilds the original character.
  68. // Validate next char.
  69. if (0x80 != (chr2 & 0xC0))
  70. return null;
  71. // Convert 2 utf-8 chars to 1 unicode char.
  72. strUni += String.fromCharCode(((chr1 & 0x1F) << 6) | (chr2 & 0x3F));
  73. i += 2;
  74. }
  75. else if ( 0xE0 == (chr1 & 0xF0))
  76. {
  77. // A char in range 0x800-0xffff is converted to 3 bytes as follows:
  78. // yyyy yyyy xxxx xxxx -> 1110 yyyy 10yy yyxx 10xx xxxx
  79. // The following logic rebuilds the original character.
  80. // Validate next 2 chars.
  81. if (0x80 != (chr2 & 0xC0) || 0x80 != (chr3 & 0xC0))
  82. return null;
  83. // Convert 3 utf-8 chars to 1 unicode char.
  84. strUni += String.fromCharCode(((chr1 & 0x0F) << 12) | ((chr2 & 0x3F) << 6) | (chr3 & 0x3F));
  85. i += 3;
  86. }
  87. else
  88. {
  89. // Invalid.
  90. return null;
  91. }
  92. }
  93. return strUni;
  94. }
  95. </script>
  96. <%
  97. '-------------------------------------------------------------------------
  98. 'Function name: GetWMIConnection
  99. 'Description: Serves in getting connected to the server
  100. 'Input Variables: strNamespace
  101. 'Output Variables: None
  102. 'Returns: Object -connection to the server object
  103. 'Global Variables: In -L_WMICONNECTIONFAILED_ERRORMESSAGE -Localized strings
  104. 'This will try to create an object and connect to wmi if fails shows failure
  105. 'page
  106. '-------------------------------------------------------------------------
  107. Public Function SA_GetWMIConnectionAttributes()
  108. SA_GetWMIConnectionAttributes = "{impersonationLevel=impersonate,authenticationLevel=pktPrivacy}"
  109. End Function
  110. Function GetWMIConnection(strNamespace)
  111. On Error Resume Next
  112. Err.Clear
  113. Dim objLocator
  114. Dim objService
  115. 'Call SA_TraceOut("INC_GLOBAL.ASP", "Entering GetWMIConnection( " & strNamespace & " )")
  116. ' If IIS6.0 WMI provider is installed, connect to root\MicrosoftIISv2 instead of root\MicrosoftIISv1
  117. ' such that we won't need to change a lot of legacy code
  118. If IsIIS60Installed() And strNamespace = CONST_WMI_IIS_NAMESPACE Then
  119. strNamespace = CONST_WMI_IIS60_NAMESPACE
  120. End IF
  121. '
  122. ' Connect to WMI
  123. Set objLocator = Server.CreateObject("WbemScripting.SWbemLocator")
  124. If strNamespace = "" OR strNamespace="default" OR strNamespace="DEFAULT" OR strNamespace="Default" Then
  125. Set objService = objLocator.ConnectServer
  126. Else
  127. Set objService = objLocator.ConnectServer(".",strNamespace )
  128. End if
  129. If Err.number <> 0 Then
  130. Call SA_TraceOut("INC_GLOBAL.ASP", "WMI Connection error: " & Hex(Err.Number) & " " & Err.Description)
  131. ServeFailurePage L_WMICONNECTIONFAILED_ERRORMESSAGE
  132. Set objLocator=Nothing
  133. Set objService=Nothing
  134. Exit Function
  135. End If
  136. '
  137. ' Set WMI Security properties
  138. objService.Security_.impersonationlevel = 3 ' wbemImpersonationLevelImpersonate
  139. objService.Security_.AuthenticationLevel = 6 ' wbemAuthenticationLevelPktPrivacy
  140. If Err.number <> 0 Then
  141. Call SA_TraceOut("INC_GLOBAL.ASP", "WMI Security property error: " & Hex(Err.Number) & " " & Err.Description)
  142. ServeFailurePage L_WMICONNECTIONFAILED_ERRORMESSAGE
  143. Set objLocator=Nothing
  144. Set objService=Nothing
  145. Exit Function
  146. End If
  147. '
  148. ' Success
  149. Set GetWMIConnection = objService
  150. 'Set to nothing
  151. Set objLocator=Nothing
  152. Set objService=Nothing
  153. End Function
  154. '-------------------------------------------------------------------------
  155. 'Function name: SA_Sleep
  156. 'Description: Sleep for the given period of time (ms)
  157. 'Input Variables: Time to sleep in ms
  158. 'Output Variables:
  159. 'Returns: None
  160. 'Global Variables:
  161. '-------------------------------------------------------------------------
  162. Public Function SA_Sleep(lngTimeToSleep)
  163. On Error Resume Next
  164. Dim objSystem
  165. Set objSystem = CreateObject("comhelper.SystemSetting")
  166. If Err.Number <> 0 Then
  167. Call SA_TraceOut(SA_GetScriptFileName(), "SA_Sleep failed to create COMHelper object: " + CStr(Hex(Err.Number)))
  168. Set objSystem = Nothing
  169. Exit Function
  170. End If
  171. call objSystem.Sleep(lngTimeToSleep)
  172. If Err.Number <> 0 Then
  173. Call SA_TraceOut(SA_GetScriptFileName(), "SA_Sleep failed: " + CStr(Hex(Err.Number)))
  174. Set objSystem = Nothing
  175. Exit Function
  176. End If
  177. Set objSystem = Nothing
  178. End Function
  179. '-------------------------------------------------------------------------
  180. 'Function name: GetComputerNameEx
  181. 'Description: Get's the long ComputerName
  182. 'Input Variables: None
  183. 'Output Variables:
  184. 'Returns: String -Returns ComputerName
  185. 'Global Variables: In -L_COMPUTERNAME_ERRORMESSAGE -Localized strings
  186. 'This returns the computer name if object fails dislays the error message
  187. '-------------------------------------------------------------------------
  188. Private Function GetComputerNameEx()
  189. On Error Resume Next
  190. Dim objSystem
  191. Dim objComputer
  192. Set objSystem = CreateObject("comhelper.SystemSetting")
  193. If Err.Number <> 0 Then
  194. GetComputerNameEx = GetComputerName()
  195. Exit Function
  196. End If
  197. Set objComputer = objSystem.Computer
  198. If Err.Number <> 0 Then
  199. Set objSystem = Nothing
  200. GetComputerNameEx = GetComputerName()
  201. Exit Function
  202. End If
  203. GetComputerNameEx = objComputer.ComputerName
  204. Set objSystem = Nothing
  205. Set objComputer = Nothing
  206. End Function
  207. '-------------------------------------------------------------------------
  208. 'Function name: GetComputerName
  209. 'Description: Get's the ComputerName
  210. 'Input Variables: None
  211. 'Output Variables:
  212. 'Returns: String -Returns ComputerName
  213. 'Global Variables: In -L_COMPUTERNAME_ERRORMESSAGE -Localized strings
  214. 'This returns the computer name if object fails dislays the error message
  215. '-------------------------------------------------------------------------
  216. Function GetComputerName
  217. Err.Clear
  218. Dim NetWork
  219. set NetWork = Server.CreateObject("WScript.Network")
  220. GetComputerName = NetWork.ComputerName
  221. If Err.number <> 0 Then
  222. ServeFailurePage L_COMPUTERNAME_ERRORMESSAGE & "(" & Err.Number & ")"
  223. End if
  224. End Function
  225. '-------------------------------------------------------------------------
  226. 'Function name: getLocalizationObject
  227. 'Description: Returns an Instance of ServerAppliance.LocalizationManager
  228. 'Input Variables: None
  229. 'Output Variables:
  230. 'Returns: Object -Returns an object
  231. 'Global Variables: In - L_LOCALIZATIONOBJECTFAILED_ERRORMESSAGE
  232. 'If object fails dislays the error message
  233. '-------------------------------------------------------------------------
  234. Function getLocalizationObject()
  235. Err.Clear
  236. Set getLocalizationObject = Server.CreateObject("ServerAppliance.LocalizationManager")
  237. If Err.Number <> 0 Then
  238. ServeFailurePage L_UNABLETOCREATELOCALIZATIONOBJECT & "(" & Hex(Err.Number) & ")"
  239. End If
  240. End function
  241. Public Function SA_EncodeQuotes(ByVal strIn)
  242. SA_EncodeQuotes = FormatJScriptString(strIn)
  243. End Function
  244. Public Function SA_UnEncodeQuotes(ByVal sValue)
  245. sValue = Replace(sValue,"\u0022", """")
  246. sValue = Replace(sValue,"\'","'")
  247. SA_UnEncodeQuotes = sValue
  248. End Function
  249. Function FormatJScriptString (ByVal strIn)
  250. strIn = ReplaceSubString(strIn, "'", "\'")
  251. strIn = ReplaceSubString(strIn, """", "\u0022")
  252. FormatJScriptString = strIn
  253. End Function
  254. Function ReplaceSubString (ByRef strIn, ByVal strDelim, ByVal strRep)
  255. Dim strArray
  256. Dim elementCt
  257. Dim strOut
  258. strArray = Split(strIn, strDelim)
  259. If IsArray(strArray) Then
  260. If UBound(strArray) > 0 Then
  261. For elementCt = 0 to UBound(strArray) - 1
  262. strOut = strOut + strArray(elementCt) + strRep
  263. Next
  264. strOut = strOut + strArray(elementCt)
  265. strIn = strOut
  266. Else
  267. ' Empty string
  268. End If
  269. End If
  270. ReplaceSubString = strIn
  271. End Function
  272. '----------------------------------------------------------------------------
  273. ' Function: UnescapeChars
  274. ' Description: removes escape characters
  275. ' Input Variables: String-FolderName
  276. ' Output Variables: None
  277. ' Return Values: String-FolderName( with out escape chars)
  278. ' Global Variables: None
  279. '----------------------------------------------------------------------------
  280. Function UnescapeChars(strFolderName)
  281. Dim strTemp
  282. strTemp=Replace(strFolderName,"\'","'")
  283. UnescapeChars=strTemp
  284. End Function
  285. '----------------------------------------------------------------------------
  286. '
  287. ' Function : SAQuickSort
  288. '
  289. ' Synopsis : sorts elements in alphabetical order
  290. '
  291. '
  292. ' Returns : an array of sorted elements.
  293. '
  294. '----------------------------------------------------------------------------
  295. Sub SAQuickSort(arrData, iLow, iHigh, numCols, iSortCol )
  296. 'Call SA_TraceOut("INC_GLOBAL", "Entering SAQuickSort")
  297. Dim iTmpLow, iTmpHigh, iTmpMid, vTempVal(), vTmpHold()
  298. Dim i
  299. ReDim vTempVal(numCols+1)
  300. ReDim vTmpHold(numCols+1)
  301. iTmpLow = iLow
  302. iTmpHigh = iHigh
  303. If iHigh <= iLow Then Exit Sub
  304. iTmpMid = INT((iLow + iHigh) \ 2)
  305. For i = 0 to numCols-1
  306. vTempVal(i) = arrData(iTmpMid, i)
  307. Next
  308. Do While (iTmpLow <= iTmpHigh)
  309. Do While ( StrComp( arrData(iTmpLow, iSortCol ), vTempVal( iSortCol ), 1 ) = -1 And iTmpLow < iHigh)
  310. iTmpLow = iTmpLow + 1
  311. Loop
  312. Do While ( StrComp( vTempVal( iSortCol ) , arrData(iTmpHigh, iSortCol ), 1 ) = -1 And iTmpHigh > iLow)
  313. iTmpHigh = iTmpHigh - 1
  314. Loop
  315. If (iTmpLow <= iTmpHigh) Then
  316. 'Store it in a temporary array
  317. For i = 0 to numCols-1
  318. vTmpHold( i ) = arrData( iTmpLow, i )
  319. Next
  320. ' Swap temporary row with the row in arrData
  321. For i = 0 to numCols-1
  322. arrData(iTmpLow, i ) = arrData(iTmpHigh, i)
  323. Next
  324. ' Swap temporary row with the row in arrData
  325. For i = 0 to numCols-1
  326. arrData(iTmpHigh, i) = vTmpHold(i)
  327. Next
  328. iTmpLow = iTmpLow + 1
  329. iTmpHigh = iTmpHigh - 1
  330. End If
  331. Loop
  332. If (iLow < iTmpHigh) Then
  333. SAQuickSort arrData, iLow, iTmpHigh, numCols, iSortCol
  334. End If
  335. If (iTmpLow < iHigh) Then
  336. SAQuickSort arrData, iTmpLow, iHigh, numCols, iSortCol
  337. End If
  338. End Sub
  339. Sub SAQuickSortEx(arrData, iLow, iHigh, numCols, iSortCol, sortSeq, bUseCompareCallback )
  340. Dim iTmpLow, iTmpHigh, iTmpMid, vTempVal(), vTmpHold()
  341. Dim i
  342. Dim iCompare
  343. If ( UCase(sortSeq) = "D" ) Then
  344. iCompare = 1
  345. Else
  346. iCompare = -1
  347. End If
  348. ReDim vTempVal(numCols+1)
  349. ReDim vTmpHold(numCols+1)
  350. iTmpLow = iLow
  351. iTmpHigh = iHigh
  352. If iHigh <= iLow Then Exit Sub
  353. iTmpMid = INT((iLow + iHigh) \ 2)
  354. For i = 0 to numCols-1
  355. vTempVal(i) = arrData(iTmpMid)(i)
  356. Next
  357. Do While (iTmpLow <= iTmpHigh)
  358. Do While ( StrComp( arrData(iTmpLow)(iSortCol ), vTempVal( iSortCol ), 1 ) = iCompare And iTmpLow < iHigh)
  359. iTmpLow = iTmpLow + 1
  360. Loop
  361. Do While ( StrComp( vTempVal( iSortCol ) , arrData(iTmpHigh)(iSortCol ), 1 ) = iCompare And iTmpHigh > iLow)
  362. iTmpHigh = iTmpHigh - 1
  363. Loop
  364. If (iTmpLow <= iTmpHigh) Then
  365. 'Store it in a temporary array
  366. For i = 0 to numCols-1
  367. vTmpHold( i ) = arrData( iTmpLow)( i )
  368. Next
  369. ' Swap temporary row with the row in arrData
  370. For i = 0 to numCols-1
  371. arrData(iTmpLow)( i ) = arrData(iTmpHigh)(i)
  372. Next
  373. ' Swap temporary row with the row in arrData
  374. For i = 0 to numCols-1
  375. arrData(iTmpHigh)(i) = vTmpHold(i)
  376. Next
  377. iTmpLow = iTmpLow + 1
  378. iTmpHigh = iTmpHigh - 1
  379. End If
  380. Loop
  381. If (iLow < iTmpHigh) Then
  382. SAQuickSortEx arrData, iLow, iTmpHigh, numCols, iSortCol, sortSeq, bUseCompareCallback
  383. End If
  384. If (iTmpLow < iHigh) Then
  385. SAQuickSortEx arrData, iTmpLow, iHigh, numCols, iSortCol, sortSeq, bUseCompareCallback
  386. End If
  387. End Sub
  388. Public Function SA_IsServiceInstalled(strServiceName)
  389. Err.Clear
  390. on error resume next
  391. Dim objWMIConnection
  392. Dim objService
  393. Dim objInstance
  394. Dim strQuery
  395. SA_IsServiceInstalled = FALSE
  396. Set objWMIConnection = getWMIConnection(CONST_WMI_WIN32_NAMESPACE)
  397. If (Err.number <> 0) Then
  398. Call SA_TraceOut(SA_GetScriptFileName(), _
  399. "SA_IsServiceInstalled: getWMIConnection(CONST_WMI_WIN32_NAMESPACE) failed: "_
  400. + CStr(Hex(Err.Number)) + " " + Err.Description)
  401. Exit Function
  402. End If
  403. strQuery="Select * FROM Win32_Service WHERE Name='" + strServiceName + "'"
  404. Set objService = objWMIConnection.ExecQuery(strQuery)
  405. If (Err.number <> 0) Then
  406. Call SA_TraceOut(SA_GetScriptFileName(), _
  407. "SA_IsServiceInstalled: objWMIConnection.ExecQuery(strQuery) failed: "_
  408. + CStr(Hex(Err.Number)) + " " + Err.Description)
  409. Call SA_TraceOut(SA_GetScriptFileName(), "Query was: " + CStr(strQuery))
  410. Exit Function
  411. End If
  412. For Each objInstance in objService
  413. SA_IsServiceInstalled = True
  414. Next
  415. Set objService = Nothing
  416. Set objInstance = Nothing
  417. Set objWMIConnection = Nothing
  418. End Function
  419. Public Function SA_GetAccount_Everyone()
  420. On Error Resume Next
  421. Err.Clear
  422. Dim oAccountNames
  423. SA_GetAccount_Everyone = "Everyone"
  424. Set oAccountNames = CreateObject("ComHelper.AccountNames")
  425. if ( Err.Number <> 0 ) Then
  426. Call SA_TraceOut("inc_global", "SA_GetAccount_Everyone Error creating Microsoft.AccountNameHelper object, error: " + CStr(Hex(Err.Number)))
  427. Exit Function
  428. End If
  429. SA_GetAccount_Everyone = oAccountNames.Everyone()
  430. if ( Err.Number <> 0 ) Then
  431. Call SA_TraceOut("inc_global", "SA_GetAccount_Everyone oAccountNames.Everyone, error: " + CStr(Hex(Err.Number)) + " " + Err.Description)
  432. End If
  433. Set oAccountNames = Nothing
  434. End Function
  435. Public Function SA_GetAccount_Administrator()
  436. On Error Resume Next
  437. Err.Clear
  438. Dim oAccountNames
  439. SA_GetAccount_Administrator = "Administrator"
  440. Set oAccountNames = CreateObject("ComHelper.AccountNames")
  441. if ( Err.Number <> 0 ) Then
  442. Call SA_TraceOut("inc_global", "SA_GetAccount_Administrator Error creating Microsoft.AccountNameHelper object, error: " + CStr(Hex(Err.Number)))
  443. Exit Function
  444. End If
  445. SA_GetAccount_Administrator = oAccountNames.Administrator()
  446. if ( Err.Number <> 0 ) Then
  447. Call SA_TraceOut("inc_global", "SA_GetAccount_Administrator oAccountNames.Administrator, error: " + CStr(Hex(Err.Number)) + " " + Err.Description)
  448. End If
  449. Set oAccountNames = Nothing
  450. End Function
  451. Public Function SA_GetAccount_Administrators()
  452. On Error Resume Next
  453. Err.Clear
  454. Dim oAccountNames
  455. SA_GetAccount_Administrators = "Administrators"
  456. Set oAccountNames = CreateObject("ComHelper.AccountNames")
  457. if ( Err.Number <> 0 ) Then
  458. Call SA_TraceOut("inc_global", "SA_GetAccount_Administrators Error creating Microsoft.AccountNameHelper object, error: " + CStr(Hex(Err.Number)))
  459. Exit Function
  460. End If
  461. SA_GetAccount_Administrators = oAccountNames.Administrators()
  462. if ( Err.Number <> 0 ) Then
  463. Call SA_TraceOut("inc_global", "SA_GetAccount_Administrators oAccountNames.Administrators, error: " + CStr(Hex(Err.Number)) + " " + Err.Description)
  464. End If
  465. Set oAccountNames = Nothing
  466. End Function
  467. Public Function SA_GetAccount_Guest()
  468. On Error Resume Next
  469. Err.Clear
  470. Dim oAccountNames
  471. SA_GetAccount_Guest = "Guest"
  472. Set oAccountNames = CreateObject("ComHelper.AccountNames")
  473. if ( Err.Number <> 0 ) Then
  474. Call SA_TraceOut("inc_global", "SA_GetAccount_Guest Error creating Microsoft.AccountNameHelper object, error: " + CStr(Hex(Err.Number)))
  475. Exit Function
  476. End If
  477. SA_GetAccount_Guest = oAccountNames.Guest()
  478. if ( Err.Number <> 0 ) Then
  479. Call SA_TraceOut("inc_global", "SA_GetAccount_Guest oAccountNames.Guest, error: " + CStr(Hex(Err.Number)) + " " + Err.Description)
  480. End If
  481. Set oAccountNames = Nothing
  482. End Function
  483. Public Function SA_GetAccount_Guests()
  484. On Error Resume Next
  485. Err.Clear
  486. Dim oAccountNames
  487. SA_GetAccount_Guests = "Guests"
  488. Set oAccountNames = CreateObject("ComHelper.AccountNames")
  489. if ( Err.Number <> 0 ) Then
  490. Call SA_TraceOut("inc_global", "SA_GetAccount_Guests Error creating Microsoft.AccountNameHelper object, error: " + CStr(Hex(Err.Number)))
  491. Exit Function
  492. End If
  493. SA_GetAccount_Guests = oAccountNames.Guests()
  494. if (Err.Number <> 0) Then
  495. Call SA_TraceOut("inc_global", "SA_GetAccount_Guests oAccountNames.Guests, error: " + CStr(Hex(Err.Number)) + " " + Err.Description)
  496. End If
  497. Set oAccountNames = Nothing
  498. End Function
  499. Public Function SA_GetAccount_System()
  500. On Error Resume Next
  501. Err.Clear
  502. Dim oAccountNames
  503. SA_GetAccount_System = "System"
  504. Set oAccountNames = CreateObject("ComHelper.AccountNames")
  505. if ( Err.Number <> 0 ) Then
  506. Call SA_TraceOut("inc_global", "SA_GetAccount_System Error creating Microsoft.AccountNameHelper object, error: " + CStr(Hex(Err.Number)))
  507. Exit Function
  508. End If
  509. SA_GetAccount_System = oAccountNames.System()
  510. if (Err.Number <> 0) Then
  511. Call SA_TraceOut("inc_global", "SA_GetAccount_System oAccountNames.System, error: " + CStr(Hex(Err.Number)) + " " + Err.Description)
  512. End If
  513. Set oAccountNames = Nothing
  514. End Function
  515. '-------------------------------------------------------------------------
  516. 'Function name: IsIIS60Installed
  517. 'Description: Check to see if IIS 6.0 WMI provider is installed
  518. 'Input Variables: None
  519. 'Output Variables: None
  520. 'Returns: Boolean, true if IIS 6.0 installed
  521. 'Global Variables:
  522. '-------------------------------------------------------------------------
  523. Public Function IsIIS60Installed()
  524. On Error Resume Next
  525. Err.Clear
  526. Dim nsList
  527. Dim ns
  528. set nsList = GetObject("winmgmts:/root").InstancesOf("__NAMESPACE")
  529. if (Err.Number <> 0) Then
  530. Call SA_TraceOut("inc_global", "IsIIS60Installed(): fail to get __NAMESPACE instances " + CStr(Hex(Err.Number)) + " " + Err.Description)
  531. IsIIS60Installed = false
  532. exit function
  533. End If
  534. for each ns in nsList
  535. if UCASE(ns.Name) = "MICROSOFTIISV2" THEN
  536. IsIIS60Installed = true
  537. set nsList = nothing
  538. Exit Function
  539. end if
  540. next
  541. ' Return false if could not find the IIS 6.0 WMI provider namespace
  542. IsIIS60Installed = false
  543. set nsList = nothing
  544. if (Err.Number <> 0) Then
  545. Call SA_TraceOut("inc_global", "IsIIS60Installed() error: " + CStr(Hex(Err.Number)) + " " + Err.Description)
  546. End If
  547. End Function
  548. '-------------------------------------------------------------------------
  549. 'Function name: IsIISWMIProviderName
  550. 'Description: Check to see if the name is iis WMI provider name
  551. 'Input Variables: None
  552. 'Output Variables: None
  553. 'Returns: Boolean, true if the name is IIS WMI provider name
  554. 'Global Variables:
  555. '-------------------------------------------------------------------------
  556. Public Function IsIISWMIProviderName(strProviderName)
  557. On Error Resume Next
  558. Err.Clear
  559. IsIISWMIProviderName = false
  560. If InStr(ucase(strProviderName), "MICROSOFTIISV") Then
  561. IsIISWMIProviderName = true
  562. End If
  563. if (Err.Number <> 0) Then
  564. Call SA_TraceOut("inc_global", "IsIISWMIProviderName() error: " + CStr(Hex(Err.Number)))
  565. End If
  566. End Function
  567. '-------------------------------------------------------------------------
  568. 'Function name: GetIISWMIProviderClassName
  569. 'Description: Get the WMI provider class name for IIS
  570. 'Input Variables: strClassName (in old WMI provider)
  571. 'Output Variables: None
  572. 'Returns: The class name for the installed IIS WMI provider
  573. 'Global Variables:
  574. '-------------------------------------------------------------------------
  575. Function GetIISWMIProviderClassName(strClassName)
  576. On Error Resume Next
  577. Err.Clear
  578. ' IIS 6.0 WMI provider use "IIS" instead of "IIS_" to prefix class names
  579. ' For example, "IIS_WebServer" will be "IISWebServer" in 6.0
  580. If IsIIS60Installed Then
  581. GetIISWMIProviderClassName = replace(ucase(strClassName), "IIS_", "IIs")
  582. Else
  583. GetIISWMIProviderClassName = strClassName
  584. End If
  585. if (Err.Number <> 0) Then
  586. SA_TraceOut "inc_global", "GetIISWMIProviderClassName() error: " + CStr(Hex(Err.Number)) + " " + Err.Description
  587. End If
  588. 'SA_TraceOut "inc_global" , "GetIISWMIProviderClassName() :" + strClassName + " : " + GetIISWMIProviderClassName
  589. End Function
  590. '-------------------------------------------------------------------------
  591. 'Function name: GetIISWMIQuery
  592. 'Description: Get the WMI query for IIS provider installed
  593. 'Input Variables: strWMIQuery
  594. 'Output Variables: None
  595. 'Returns: the valid WMI query for IIS provider installed
  596. 'Global Variables:
  597. '-------------------------------------------------------------------------
  598. Function GetIISWMIQuery(strWMIQuery)
  599. On Error Resume Next
  600. Err.Clear
  601. GetIISWMIQuery = ""
  602. ' IIS 6.0 WMI provider use "IIS" instead of "IIS_" to prefix class names
  603. ' For example, "IIS_WebServer" will be "IISWebServer" in 6.0
  604. If IsIIS60Installed Then
  605. GetIISWMIQuery = replace(ucase(strWMIQuery), "IIS_", "IIs")
  606. Else
  607. GetIISWMIQuery = strWMIQuery
  608. End If
  609. if (Err.Number <> 0) Then
  610. SA_TraceOut "inc_global", "GetIISWMIQuery() error: " + CStr(Hex(Err.Number)) + " " + Err.Description
  611. End If
  612. End Function
  613. '-------------------------------------------------------------------------
  614. 'Function name: GetCurrentWebsiteName
  615. 'Description: Get the name of the web site running the current document
  616. 'Input Variables: None
  617. 'Output Variables: None
  618. 'Returns: The current web site name
  619. 'Global Variables:
  620. '-------------------------------------------------------------------------
  621. Function GetCurrentWebsiteName()
  622. On Error Resume Next
  623. Err.Clear
  624. dim objRegConn
  625. Set objRegConn = RegConnection()
  626. GetCurrentWebsiteName = "W3SVC/" & GetRegKeyValue(objRegConn,CONST_WEBFRAMEWORK_REGKEY,CONST_ADMINSITEID_REGVAL,CONST_DWORD)
  627. 'If we cannot get the adminsite id, it's a serious problem and we should stop right away.
  628. If Err.number <> 0 Then
  629. SA_TraceOut "inc_global", "GetCurrentWebsiteName() error: " + CStr(Hex(Err.Number)) + " " + Err.Description
  630. GetCurrentWebsiteName = 0
  631. exit Function
  632. End if
  633. End Function
  634. '-------------------------------------------------------------------------
  635. 'Function name: GetSharesFolder
  636. 'Description: Get the shares site folder
  637. 'Input Variables: None
  638. 'Output Variables: None
  639. 'Returns: The shares site folder
  640. 'Global Variables:
  641. '-------------------------------------------------------------------------
  642. Function GetSharesFolder()
  643. On Error Resume Next
  644. dim oSharesSite
  645. Set oSharesSite = GetObject("IIS://localhost/w3svc/" & GetSharesSiteID() & "/Root")
  646. GetSharesFolder = oSharesSite.Path
  647. If Err.number <> 0 Then
  648. SA_TraceOut "inc_global", "GetSharesFolder() error: " + CStr(Hex(Err.Number)) + " " + Err.Description
  649. GetSharesFolder = ""
  650. exit Function
  651. End if
  652. End Function
  653. '-------------------------------------------------------------------------
  654. 'Function name: GetSharesSiteID
  655. 'Description: Get the shares site ID. After create the shares site, setup will
  656. ' store the shares site ID in the registry.
  657. 'Input Variables: None
  658. 'Output Variables: None
  659. 'Returns: The shares site ID
  660. 'Global Variables:
  661. '-------------------------------------------------------------------------
  662. Function GetSharesSiteID
  663. dim objRegConn
  664. Set objRegConn = RegConnection()
  665. GetSharesSiteID = GetRegKeyValue(objRegConn,CONST_WEBFRAMEWORK_REGKEY,CONST_SHARESSITEID_REGVAL,CONST_DWORD)
  666. If Err.number <> 0 Then
  667. SA_TraceOut "inc_global", "GetSharesSiteID() error: " + CStr(Hex(Err.Number)) + " " + Err.Description
  668. GetSharesSiteID = 0
  669. exit Function
  670. End if
  671. End Function
  672. '-------------------------------------------------------------------------
  673. 'Function name: GetServerOSName
  674. 'Description: Get the name of the server OS (XP_SERVER, XPE, W2K_SERVER, etc)
  675. 'Input Variables: None
  676. 'Output Variables: None
  677. 'Returns: The current web site name
  678. 'Global Variables:
  679. '-------------------------------------------------------------------------
  680. Function GetServerOSName()
  681. On Error Resume Next
  682. Err.Clear
  683. Dim objOS
  684. Dim objOSs
  685. Dim strOSName
  686. set objOSs = GetObject("WINMGMTS:{impersonationLevel=impersonate}!\\" & GetServerName & "\root\cimv2:Win32_OperatingSystem").Instances_
  687. If ( Err.Number <> 0 ) Then
  688. Call SA_TraceOut(SA_GetScriptFileName(), "Get Win32_OperatingSystem failed: " + CStr(Hex(Err.Number)) + " " + Err.Description)
  689. Exit Function
  690. End If
  691. 'Should be only one OS
  692. for each objOS in objOSs
  693. strOSName = objOS.Caption
  694. exit for
  695. next
  696. GetServerOSName = strOSName
  697. if (Err.Number <> 0) Then
  698. SA_TraceOut "inc_global", "GetServerOSName() error: " + CStr(Hex(Err.Number)) + " " + Err.Description
  699. End If
  700. End Function
  701. '-------------------------------------------------------------------------
  702. 'Function name: GetIISAnonUsername
  703. 'Description: Get the anonymous username created from IIS
  704. ' Notice we cannot use IUSR_ + computername because user
  705. ' may change the computername later, but the anonymous name
  706. ' remains the same.
  707. 'Input Variables: None
  708. 'Output Variables: None
  709. 'Returns: The anonymous username created from IIS
  710. 'Global Variables:
  711. '-------------------------------------------------------------------------
  712. Function GetIISAnonUsername()
  713. On Error Resume Next
  714. Err.Clear
  715. Dim objWMIConnection
  716. Dim objWebService
  717. GetIISAnonUsername = ""
  718. Set objWMIConnection = getWMIConnection(CONST_WMI_IIS_NAMESPACE)
  719. Set objWebService = objWMIConnection.Get(GetIISWMIProviderClassName("IIS_WebServiceSetting") & ".Name='W3SVC'")
  720. if Err.number <> 0 then
  721. Call SA_TraceOut("inc_wsa", "GetIISAnonUsername() encountered error: "+ CStr(Hex(Err.Number)))
  722. Exit Function
  723. end if
  724. GetIISAnonUsername = objWebService.AnonymousUserName
  725. End Function
  726. '-------------------------------------------------------------------------
  727. 'Function name: GetIISWAMUsername
  728. 'Description: Get the WAM username created from IIS
  729. 'Input Variables: None
  730. 'Output Variables: None
  731. 'Returns: The WAM username created from IIS
  732. 'Global Variables:
  733. '-------------------------------------------------------------------------
  734. Function GetIISWAMUsername()
  735. On Error Resume Next
  736. Err.Clear
  737. Dim objWMIConnection
  738. Dim objWebService
  739. GetIISWAMUsername = ""
  740. Set objWMIConnection = getWMIConnection(CONST_WMI_IIS_NAMESPACE)
  741. Set objWebService = objWMIConnection.Get(GetIISWMIProviderClassName("IIS_WebServiceSetting") & ".Name='W3SVC'")
  742. if Err.number <> 0 then
  743. Call SA_TraceOut("inc_wsa", "GetIISWAMUsername() encountered error: "+ CStr(Hex(Err.Number)))
  744. Exit Function
  745. end if
  746. GetIISWAMUsername = objWebService.WAMUserName
  747. End Function
  748. '-------------------------------------------------------------------------
  749. 'Function: GetSystemDrive()
  750. 'Description: To get the Operating System Drive
  751. 'Input Variables: None
  752. 'Output Variables: None
  753. 'Returns: Operating system Drive
  754. 'Global Variables: None
  755. '-------------------------------------------------------------------------
  756. Function GetSystemDrive
  757. Err.Clear
  758. On Error Resume Next
  759. Dim objFso
  760. GetSystemDrive = "C:"
  761. Set objFso = Server.CreateObject("Scripting.FileSystemObject")
  762. If Err.Number <> 0 Then
  763. SA_TraceOut "GetSystemDrive", "failed to call CreateObject"
  764. Exit Function
  765. End If
  766. GetSystemDrive = objFso.GetSpecialFolder(0).Drive
  767. If Err.Number <> 0 Then
  768. SA_TraceOut "GetSystemDrive", "failed to call GetSpecialFolder"
  769. Exit Function
  770. End If
  771. End Function
  772. '-------------------------------------------------------------------------
  773. ' Function: IsWebsiteNotStopped()
  774. ' Description: Called to verify whether the website is
  775. ' stopped or not
  776. ' Input Variables: strWebsiteName
  777. ' Output Variables: None
  778. ' Return Values: Boolean
  779. ' Global Variables: None
  780. '-------------------------------------------------------------------------
  781. Function IsWebsiteNotStopped(strWebsiteName)
  782. On Error Resume Next
  783. Err.Clear
  784. Dim objWMIConnection
  785. Dim objWebServer
  786. Const CONST_STOPPED_STATE = 4 'Stopped state of website
  787. 'Setup the default return value
  788. IsWebsiteNotStopped = true
  789. Set objWMIConnection = getWMIConnection(CONST_WMI_IIS_NAMESPACE)
  790. Set objWebServer = objWMIConnection.Get(GetIISWMIProviderClassName("IIs_WebServer") & ".Name='" & strWebsiteName & "'")
  791. if Err.number <> 0 then
  792. Call SA_TraceOut("inc_wsa", "IsWebsiteNotStopped() encountered error: "+ CStr(Hex(Err.Number)))
  793. Exit Function
  794. end if
  795. if objWebServer.ServerState <> CONST_STOPPED_STATE Then
  796. IsWebsiteNotStopped = true
  797. Else
  798. IsWebsiteNotStopped = false
  799. End If
  800. End Function
  801. '-------------------------------------------------------------------------
  802. 'Function name: getShortDomainName
  803. 'Description: gets the short domain name (vs. DNS name)
  804. 'Input Variables: domain name that may be a domain DNS name
  805. 'Output Variables: None
  806. 'Returns: Short Domain Name
  807. '-------------------------------------------------------------------------
  808. Function getShortDomainName(strDomainName)
  809. Err.Clear
  810. Dim arrDomainName
  811. '
  812. ' If domain name contains char ".", it's a DNS domain name
  813. ' e.g. redmond.corp.microsoft.com. We need to get the shortname
  814. ' which is "redmond". That is because ADSI GetObject only accepts
  815. ' short domain name.
  816. '
  817. If InStr(strDomainName, ".") Then
  818. arrDomainName = Split(strDomainName, ".")
  819. getShortDomainName = arrDomainName(0)
  820. Else
  821. getShortDomainName = strDomainName
  822. End If
  823. End Function
  824. '----------------------------------------------------------------------------
  825. '
  826. ' Class: CSAValidator
  827. '
  828. ' Synopsis: Server side utility class to perform input validation.
  829. '
  830. '----------------------------------------------------------------------------
  831. '
  832. Class CSAValidator
  833. Private oRegExp ' Private reference to Regular Expression object
  834. Private bInitialized ' Private initialization state reference variable
  835. '--------------------------------------------------------------------
  836. ' Method: Init
  837. '
  838. ' Synopsis: Initialize the object. This method is called internally to create an instance
  839. ' of the Regular Expression object.
  840. '--------------------------------------------------------------------
  841. '
  842. Private Function Init()
  843. If ( bInitialized <> TRUE ) Then
  844. Err.Clear
  845. Set oRegExp = new RegExp
  846. bInitialized = TRUE
  847. End If
  848. End Function
  849. Public Function IsValidShareName(ByVal sInput)
  850. On Error Resume Next
  851. Init()
  852. IsValidShareName = IsValidFileName(sInput)
  853. If ( NOT IsValidShareName ) Then
  854. Exit Function
  855. End If
  856. '
  857. ' Check for valid length
  858. If ( ( Len(sInput) < 1 ) OR ( Len(sInput) > 80 ) ) Then
  859. Call SA_TraceOut("CSAValidator", "Invalid input to function IsValidShareName")
  860. Exit Function
  861. End If
  862. Dim bMatch
  863. '
  864. ' Match any of the following characters
  865. oRegExp.Pattern = "[\[\]\;\=\,\+]"
  866. ' If we matched the pattern (any non-word character)
  867. ' then the Input is NOT a valid identifier.
  868. bMatch = oRegExp.Test(sInput)
  869. If ( bMatch ) Then
  870. ' Input is bad
  871. Else
  872. ' Input is good
  873. IsValidShareName = TRUE
  874. End If
  875. End Function
  876. Public Function IsValidFileName(ByVal sInput)
  877. On Error Resume Next
  878. Init()
  879. IsValidFileName = FALSE
  880. '
  881. ' Check for valid type
  882. If ( TypeName(sInput) <> "String" ) Then
  883. Call SA_TraceOut("CSAValidator", "Invalid input to function IsValidFileName")
  884. Exit Function
  885. End If
  886. '
  887. ' Check for valid length
  888. If ( ( Len(sInput) < 1 ) OR ( Len(sInput) > 256 ) ) Then
  889. Call SA_TraceOut("CSAValidator", "Invalid input to function IsValidFileName")
  890. Exit Function
  891. End If
  892. Dim bMatch
  893. '
  894. ' Match any of the following characters
  895. oRegExp.Pattern = "[|\/<>"":*]"
  896. ' If we matched the pattern (any non-word character)
  897. ' then the Input is NOT a valid identifier.
  898. bMatch = oRegExp.Test(sInput)
  899. If ( bMatch ) Then
  900. ' Input is bad
  901. Else
  902. ' Input is good
  903. IsValidFileName = TRUE
  904. End If
  905. End Function
  906. '--------------------------------------------------------------------
  907. ' Method: IsValidIdentifier
  908. '
  909. ' Synopsis: Check the input to verify that it is a valid identifier. A string is considered
  910. ' a valid identifier if it:
  911. '
  912. ' 1) Begins with an alpha character
  913. ' 2) Contains alpha numeric data (A-Z, a-z, 0-9), or an underscore
  914. ' 3) Length >= 1 AND <= 512 characters
  915. '
  916. '--------------------------------------------------------------------
  917. '
  918. Public Function IsValidIdentifier(ByVal sInput)
  919. On Error Resume Next
  920. Init()
  921. IsValidIdentifier = FALSE
  922. '
  923. ' Check for valid type
  924. If ( TypeName(sInput) <> "String" ) Then
  925. Call SA_TraceOut("CSAValidator", "Invalid input to function IsValidIdentifier")
  926. Exit Function
  927. End If
  928. '
  929. ' Check for valid length
  930. If ( ( Len(sInput) < 1 ) OR ( Len(sInput) > 512 ) ) Then
  931. Call SA_TraceOut("CSAValidator", "Invalid input to function IsValidIdentifier")
  932. Exit Function
  933. End If
  934. Dim bMatch
  935. '
  936. ' Match any non-word character
  937. oRegExp.Pattern = "\W"
  938. ' If we matched the pattern (any non-word character)
  939. ' then the Input is NOT a valid identifier.
  940. bMatch = oRegExp.Test(sInput)
  941. If ( bMatch ) Then
  942. ' Input is bad
  943. Else
  944. ' Input is good
  945. IsValidIdentifier = TRUE
  946. End If
  947. End Function
  948. End Class
  949. '----------------------------------------------------------------------------
  950. '
  951. ' Class: CSAEncoder
  952. '
  953. ' Synopsis: Server side utility class to perform encoding.
  954. '
  955. '----------------------------------------------------------------------------
  956. '
  957. Class CSAEncoder
  958. '--------------------------------------------------------------------
  959. ' Method: EncodeAttribute
  960. '
  961. ' Synopsis:
  962. ' Convert input to properly quoted and encoded HTML attribute.
  963. ' Input is limited to being 512 characters. Null input is converted to empty string.
  964. ' Non-string input is converted to a string.
  965. '
  966. ' Input:
  967. ' sInput: URL input string which is to be encoded
  968. '
  969. ' Example:
  970. ' Response.Write("<table bgcolor=" & oEncoder.EncodeAttribute(backgroundColor) & " >")
  971. '
  972. '--------------------------------------------------------------------
  973. '
  974. Public Function EncodeAttribute(ByVal sInput)
  975. On Error Resume Next
  976. EncodeAttribute = """" & """"
  977. '
  978. ' Ensure we have non-null input
  979. If ( IsNull(sInput) ) Then
  980. sInput = ""
  981. End If
  982. '
  983. ' Cast to String if necessary
  984. If ( TypeName(sInput) <> "String" ) Then
  985. sInput = CStr(sInput)
  986. End If
  987. '
  988. ' Input must be string length between 0 and 512 characters
  989. If ( ( Len(sInput) < 0 ) OR (Len(sInput) > 512) ) Then
  990. Call SA_TraceOut("CSAEncoder.EncodeAttribute", "Invalid input to function EncodeAttribute")
  991. Exit Function
  992. End If
  993. EncodeAttribute = """" & Server.HTMLEncode(SA_EscapeQuotes(sInput)) & """"
  994. End Function
  995. '--------------------------------------------------------------------
  996. ' Method: EncodeElement
  997. '
  998. ' Synopsis: Convert the input into a valid encoded HTML
  999. '
  1000. ' Input:
  1001. ' sInput: URL input string which is to be encoded
  1002. '
  1003. '--------------------------------------------------------------------
  1004. '
  1005. Public Function EncodeElement(ByVal sInput)
  1006. On Error Resume Next
  1007. '
  1008. ' Ensure we have non-null input
  1009. If ( IsNull(sInput) ) Then
  1010. sInput = ""
  1011. End If
  1012. EncodeElement = Server.HTMLEncode(sInput)
  1013. End Function
  1014. '--------------------------------------------------------------------
  1015. ' Method: EncodeJScriptArg
  1016. '
  1017. ' Synopsis: Convert the input into a valid encoded argument for a clientside call to
  1018. ' a Javascript function.
  1019. '
  1020. ' Input:
  1021. ' sInput: URL input string which is to be encoded
  1022. '
  1023. '--------------------------------------------------------------------
  1024. '
  1025. Public Function EncodeJScript(ByVal sInput)
  1026. On Error Resume Next
  1027. EncodeJScript = ""
  1028. '
  1029. ' Ensure we have non-null input
  1030. If ( IsNull(sInput) ) Then
  1031. sInput = ""
  1032. End If
  1033. '
  1034. ' Cast to string if necessary
  1035. If ( TypeName(sInput) <> "String" ) Then
  1036. sInput = CStr(sInput)
  1037. End If
  1038. '
  1039. ' Input must be string length between 0 and 512 characters
  1040. If ( ( Len(sInput) < 0 ) OR (Len(sInput) > 512) ) Then
  1041. Call SA_TraceOut("CSAEncoder.EncodeJScript", "Invalid input to function EncodeJScript")
  1042. Exit Function
  1043. End If
  1044. EncodeJScript= SA_EscapeQuotes(sInput)
  1045. End Function
  1046. End Class
  1047. %>