Source code of Windows XP (NT5)
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.

235 lines
5.2 KiB

  1. '
  2. 'Description:
  3. '--------------
  4. 'This example shows you how to use IIS admin objects to set certain
  5. 'directory security settings
  6. '
  7. 'Usage: cscript SetIPRestriction.vbs <adspath>
  8. ' [--IPRestriction|-r grantbydefault|denybydefault]
  9. ' [--DomaintoExclude|-d] domain1,domain2,..
  10. ' [--IPtoExclude|-ip] IP1:mask1,IP2:mask2, ..
  11. ' [--ClearRestrictionList|-c]
  12. '
  13. 'examples:
  14. ' 1) cscript SetIPRestriction.vbs IIS://localhost/w3svc/1/root -r grantbydefault
  15. ' 2) cscript SetIPRestriction.vbs IIS://localhost/w3svc/1/root -d test1.com,test2.com,test3.com
  16. ' 3) cscript SetIPRestriction.vbs IIS://localhost/w3svc/1/root -ip 123.232.121.1:255.255.0.0,123.123.123.123
  17. ' 4) cscript SetIPRestriction.vbs IIS://localhost/w3svc/1/root -c
  18. option explicit
  19. Dim iArg 'index of Args
  20. Dim oArgs 'Wscript.Arguments
  21. Dim aAuthen, aSecureComm, aDomain, aIP
  22. Dim fSetDefaultIPRestriction, fClearAllRestriction, fGrantByDefault
  23. Dim ADspath, oNode
  24. set oArgs=Wscript.Arguments
  25. if oArgs.count<2 then
  26. UsageMsg
  27. end if
  28. iArg=0
  29. fSetDefaultIPRestriction=false
  30. fClearAllRestriction=false
  31. While(iArg<aArgs.count)
  32. Select CASE UCASE(oArgs(iArg))
  33. CASE "--IPRESTRICTION","-R":
  34. iArg=iArg+1
  35. fSetDefaultIPRestriction=True
  36. if UCASE(oArgs(iArg)) ="GRANTBYDEFAULT" then
  37. fGrantByDefault=true
  38. elseif UCASE(oArgs(iArg)) ="DENYBYDEFAULT" then
  39. fGrantByDefault=false
  40. else
  41. end if
  42. CASE "--DOMAINTOEXCLUDE", "-D":
  43. iArg=iArg+1
  44. aDomain=Split(oArgs(iArg), ",", -1)
  45. CASE "-IPTOEXCLUDE", "-IP":
  46. iArg=iArg+1
  47. aIP=Split(oArgs(iArg), ",", -1)
  48. CASE "--CLEARRESTRICTIONLIST","-C":
  49. fClearAllRestriction=true
  50. CASE else:
  51. ADspath=oArgs(iArg)
  52. End Select
  53. iArg=iArg+1
  54. Wend
  55. if len(adspath)=0 then
  56. ErrMsg "Missing adspath"
  57. end if
  58. set oNode=GetObject(UCASE(adspath))
  59. if fSetDefaultIPRestriction then
  60. call setDefaultAccess(oNode,fGrantByDefault)
  61. end if
  62. if fClearAllRestriction then
  63. call ClearIPRestriction(oNode)
  64. else
  65. call SetIPRestriction(oNode,aIP,aDomain)
  66. end if
  67. '
  68. '
  69. 'Description: The function set default IP access on the virtual directory
  70. 'input:
  71. ' oNode -> virtual directory's ADSI object
  72. ' fGrantbyDefault -> boolean variable indicate default access
  73. '
  74. sub SetDefaultAccess(oNode, fGrantbyDefault)
  75. Dim oIPSec
  76. set oIPSec=oNode.IPSecurity
  77. if fGrantbyDefault then
  78. oIPSec.GrantbyDefault=true
  79. else
  80. oIPSec.GrantbyDefault=false
  81. end if
  82. oNode.IPSecurity=oIPSec
  83. oNode.SetInfo
  84. end sub
  85. ' Description: remove the restriction on given virtual directory
  86. ' input:
  87. ' adsi object for the virtual directory
  88. Sub ClearIPRestriction(oNode)
  89. Dim oIPSec, dummyList
  90. dummyList=Array()
  91. set oIPSec= oNode.IPSecurity
  92. if oIPSec.GrantbyDefault then
  93. oIPSec.IPDeny=dummyList
  94. oIPSec.DomainDeny=dummyList
  95. else
  96. oIPSec.IPGrant=dummyList
  97. oIPSec.DomainGrant=dummyList
  98. end if
  99. oNode.IPSecurity=oIPSec
  100. oNode.SetInfo
  101. end Sub
  102. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  103. ' Description : set the restriction on given virtual directory
  104. ' input :
  105. ' oNode -> adsi object for the given directory
  106. ' aIP -> array of IP to be set
  107. ' aDomain -> array of Domain to be set
  108. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  109. Sub SetIPRestriction(oNode, aIP, aDomain)
  110. dim cBound, i, oIPSec
  111. dim aNew
  112. 'build IP array into righ format
  113. if isarray(aIP) then
  114. arraybound= ubound(aIP)
  115. if arraybound>=0 then
  116. for i=0 to arraybound
  117. aIP(i)= replace(aIP(i), ":", ",")
  118. next
  119. end if
  120. end if
  121. set oIPSec=oNode.IPSecurity
  122. if oIPSec.GrantbyDefault then
  123. aNew=MergList(aIP, oIPSec.IPDeny)
  124. oIPSec.IPDeny=aNew
  125. aNew=MergList(aDomain, oIPSec.DomainDeny)
  126. oIPSec.DomainDeny=aNew
  127. else
  128. aNew=MergList(aIP, oIPSec.IPGrant)
  129. oIPSec.IPGrant=aNew
  130. aList=MergList(aDomain, oIPSec.DomainGrant)
  131. oIPSec.DomainGrant=aList
  132. end if
  133. oNode.IPSecurity=oIPSec
  134. oNode.SetInfo
  135. end Sub
  136. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  137. 'Description : concatinate two list into a new list
  138. 'input :
  139. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  140. function MergList( array1, array2)
  141. dim ub1, ub2, i, aMerg()
  142. if IsNonemptyArray(array1)= false then
  143. MergList=array2
  144. exit function
  145. end if
  146. if IsNonemptyArray(array2)= false then
  147. mergList=Array1
  148. exit function
  149. end if
  150. ub1=ubound(array1)
  151. ub2=ubound(array2)
  152. redim aMerg(ub1+ub2+2)
  153. for i=0 to ub2
  154. aMerg(i)=array2(i)
  155. next
  156. for i=0 to ub1
  157. aMerg(ub2+1+i)=array1(i)
  158. next
  159. MergList=aMerg
  160. end function
  161. ''''''''''''''''''''''''''''''''''''''''''''''''
  162. 'Function: check if variable is non empty array
  163. ''''''''''''''''''''''''''''''''''''''''''''''''
  164. function IsNonemptyArray(aInput)
  165. if isArray(aInput) =false then
  166. IsNonemptyArray=false
  167. exit function
  168. end if
  169. if ubound(aInput)<0 then
  170. IsNonemptyArray=false
  171. else
  172. IsNonEmptyArray=true
  173. end if
  174. end function
  175. 'Display Error Message then quits
  176. Sub ErrMsg( msg)
  177. WScript.echo "Error:" & msg
  178. WScript.quit
  179. End Sub
  180. ' Displays usage message, then quits
  181. Sub UsageMsg
  182. Wscript.Echo "Usage: cscript SetIPRestriction.vbs <adspath>"
  183. Wscript.Echo space(20)+"[--IPRestriction|-r grantbydefault|denybydefault]"
  184. Wscript.Echo space(20)+"[--DomaintoExclude|-d] domain1,domain2,.."
  185. Wscript.Echo space(20)+"[--IPtoExclude|-ip] IP1:mask1,IP2:mask2, .."
  186. Wscript.Echo space(20)+"[--ClearRestrictionList|-c]"
  187. Wscript.Quit
  188. End Sub