<!--#include virtual="/NRConfig/SecurityConfig/CNCASPPageUI_SecurityConfig.inc"-->
<% 
'*****************************************************************************
' Copyright (C) 2001 Microsoft Corporation. All rights reserved.
'
' File:  NRCONFIG/CNCASPPageManager_SecurityConfig.inc
' Author: Microsoft Corporation
'
' Purpose:
'   Define Constants, global variables and functions that are shared by 
'   the whole application
'
'Public Methods and Properties Exposed:
'	- Sub InitManager()
'	Property SysAccount [ReadOnly, String]
'	Property SysUserName [ReadOnly, String]
'	Property SysPassword [ReadOnly, String]
'	Property GuestAccount [ReadOnly, String] 
'	Property GuestUserName [ReadOnly, String]
'	Property CookieLifeTime [ReadOnly, Number]
'	Property IsCheckMachineIP [ReadOnly, Boolean]
'	Property IsAllowGuestsOnSite [ReadOnly, Boolean]
'	Property ResServerIndex [ReadOnly, Number]   	
'	Property ValidateNTUserStatus [ReadOnly, Number]
'	Property Error [ReadOnly, String]
'	Property ErrorDetail [ReadOnly, String]
'	Property IsSupportedBrowser [ReadOnly, Boolean]
'	Property IsSSLRunning [Readonly, Boolean] 
'	Property IsLocalHost [Readonly, Boolean] 
'	Property SecurityAlertURL [Readonly, String]
'
'*****************************************************************************

'--Define constants

'Define a class to create CInitManager class
Class CNCASPPageManager_SecurityConfig
	
	'Public property
	
	Public SysAccount
	Public SysUserName
	public SysPassword
	Public GuestAccount 
	Public GuestUserName
	Public CookieLifeTime
	Public IsCheckMachineIP
	Public IsAllowGuestsOnSite
	Public ResServerIndex    	
	Public ValidateNTUserStatus
	Public CheckMachineIP
	Public Error
	Public ErrorDetail
	Public IsSupportedBrowser 	
	Public bHiddenSysPassword
	Public IsSSLRunning
	Public IsLocalHost
	Public SecurityAlertURL
	
	'Define private variable	
	
	Private m_pSCAPI
	Private m_strGuestAccount
    Private m_strSysAccount
    
    
	'---- [Private] Standard Class_Initialize
	Private Sub Class_Initialize()
		' -- init all member with default values
		ValidateNTUserStatus = IDS_VALIDATE_SYSTEMANDGUESTACCOUNT_OK
		SysAccount = ""
		SysUserName = ""
		SysPassword = ""
		GuestAccount = "" 
		GuestUserName = ""
		
		bHiddenSysPassword = ""
		
		m_strGuestAccount = ""
		m_strSysAccount = ""  
    	
	
		CookieLifeTime = -1
		IsCheckMachineIP = False
		IsAllowGuestsOnSite = False
		ResServerIndex = -1    	
		
		CheckMachineIP = False
		Error = IDS_OK_NO_ERROR 
		ErrorDetail = ""

		IsLocalHost = False
		IsSSLRunning = False

		Call ChkIsLocalHostConnection()
		Call SetupSSLRunningFlag()
		
		SecurityAlertURL = "\NRConfig\SecurityAlert\SecurityAlert.asp?" & "ResServerIndex=" & Request.QueryString("ResServerIndex")

		'-- Create an instance of ServerConfiguration API
	   	Set m_pSCAPI = Server.CreateObject("ServerConfigurationAPI.NRSCAPI")
		
		'-- Inititalize this API
		m_pSCAPI.Initialize()
	End Sub
	
	'*************************************************
	'Purpose: Response for all init asp process
	'         
	'Input:	  None
	'
	'Output:  display a tab control
    	'************************************************* 
    
    Public Sub InitManager()
		
		' -- Check browser version and java script enable
		IsSupportedBrowser = VerifyIsSupportedBrowser
		
		'--Get MCMS index
		ResServerIndex = Request.QueryString("ResServerIndex")

		' -- Check ResServerIndex exist or not  
		ResServerIndexExist(ResServerIndex)

		If ResServerIndex >ID_NO_RESOLUTION_ENTEY_POINT  Then
		
		'--Check submit form is not empty
			If GetFormElements Then
				
				'--Validate password by API for system account and guest account 
				If  ValidateNTUser = IDS_VALIDATE_SYSTEMANDGUESTACCOUNT_OK Then
					Call UpdateDataToAPI
					If Err.Number = 0 Then
						Call CloseWindowAndRedirectToMainASP
					End If
				Else 								
					Exit Sub 
				End If				
			Else
			
				'--Get API data for both tab control renderer and General config UI
				Call GetAPIDataForUI 
			End If
		End If
   	
   	End Sub
   	
   	'-- [Private] Close popup window and redirect to main asp page after update data to API
   	Private Sub CloseWindowAndRedirectToMainASP()
   		
   		
   		'-- Render a java script to close popup window and redirect to main asp
   		call Response.Write ("<script language='JavaScript'  type='text/javascript'>" & vbCrLf)
		Call Response.Write ("window.opener.location = '../" & IDS_MAIN_CONFIGASPPAGE & "?" & _
							IDS_QUERYSTRING_RESOLUTION_SERVER_INDEX & "=" & Server.URLEncode(ResServerIndex) & _
							"&" & IDS_QUERYSTRING_CURRENT_TAB_INDEX  & _
							"=" & IDS_CONFIG_SECURITY_QUERYSTRING & _
							"&" & IDS_QUERYSTRING_SKIPSSL_WARNING & "=true" & _
							"&" & IDS_TIME_STAMP & "=" & CDbl(CDate(now)) & "';"  & vbCrLf)
		Call Response.Write ("IsWindowOpenBefore = false;" & vbCrLf)
		Call Response.Write ("window.close();" & vbCrLf)
		call Response.Write ("</script>" & vbCrLf)
   	End Sub
   	
   	
   	
   	'-- [Private] Update Security data to API 
   	Private Sub UpdateDataToAPI
   	
		On Error Resume Next
		Dim pGlobalSecurity
		Dim pGlobalSetting
		Dim pLocalSetting 
		
		
		' -- Set new script time out 
		Dim ScriptTimeOut
		Server.ScriptTimeOut = 100000
   		
		'--Bug 6000
		'--Always set the MCMS System Account first.  This is because
		'--if they are using integrated security to connect to the database,
		'--changing this account could break the server.  If this happens,
		'--the remaining calls will fail (i.e. GlobalSettings below).  However,
		'--if we do this first, then at least they will be able change it back
		'--to "unbreak the server"
		Set pLocalSetting = m_pSCAPI.LocalSettings(ResServerIndex)
		
		'User ,on the client side, get new password or user name			
		If  Cbool(bHiddenSysPassword) Then 
			'--Set new domain, user name and password for system account
			Call pLocalSetting.ResSysAccount.SetAccount(SysAccount, _
									SysUserName, _
									SysPassword) 
		End If
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & " " & IDS_UI_ERROR_SET_SYSTEM_ACCOUNT
			ErrorDetail = Err.Description
			Exit Sub
		End If
		
		'--Now try to set the global values. If this fails the dialog will
		'--not be closed. The only thing the user will really be able to do
		'--is change the MCMS System Account back to a valid account.
		Set pGlobalSetting = m_pSCAPI.GlobalSettings(ResServerIndex)

		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & " " & IDS_UI_ERROR_GET_GLOBAL_SETTING
			ErrorDetail = Err.Description 
			Exit Sub
		End If
		
		Set pGlobalSecurity = m_pSCAPI.GlobalSettings(ResServerIndex).SecurityService
					
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & " " & IDS_UI_ERROR_GET_SECURITY_SERVICE 
			ErrorDetail = Err.Description 
			Exit Sub
		End If
		
			
		'--Set Cook life time
		pGlobalSecurity.CookieLifetimeInMinutes = clng(CookieLifeTime)
		
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & " " & IDS_UI_ERROR_SET_COOKIE_LIFETIME 
			ErrorDetail = Err.Description 
			Exit Sub
		End If
		
		'-- Set Machine IP
					
		pGlobalSecurity.CheckMachineIP = IsCheckMachineIP
			
		
		' -- Error Check
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & IDS_UI_ERROR_GET_CHECK_MACHINE_IP
			ErrorDetail = Err.Description
			Exit Sub
		End If 
		
		IsAllowGuestsOnSite = Cbool(IsAllowGuestsOnSite) 
   		
   		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & " " & IDS_UI_ERROR_SET_GUEST_ACCESS_ACCOUNT
			ErrorDetail = Err.Description
			Exit Sub
		End If
		
   		'-- Set Guset account
		Call pGlobalSetting.GuestAccess.SetGuestAccess(IsAllowGuestsOnSite , _
									GuestAccount, _
									GuestUserName)
   		
   		
   		' -- Error Check
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & " " & IDS_UI_ERROR_SET_GUEST_ACCESS_ACCOUNT & GuestAccount
			ErrorDetail = Err.Description
			Exit Sub
		End If
		
		'--Back to default for script time and bufer
		Server.ScriptTimeOut = ScriptTimeOut
		
		
   		'-- clear object 
   		Set pGlobalSecurity = Nothing
		Set pGlobalSetting = Nothing
		Set pLocalSetting = Nothing 
   	
   	End Sub
   	
   	'-- [Private] Get NT domain name and password from one string
   	Private Sub GetDomainNameAndPassword()
   		
   		Dim nCharPos
		Dim strAccount
		   		
		strAccount = SysAccount
		   		
		nCharPos = InStr(1, StrAccount, "\")
				
		If nCharPos = 0 Then 
			nCharPos = InStr(1, StrAccount, "/")		
		End If
			
		SysAccount = Left(StrAccount, Cint(nCharPos)-1)
		SysUserName = Right(StrAccount, Len(strAccount) - nCharPos)
   	
   	End Sub
   	
   	'-- [Private] Get NT domain and User name for guest account
   	Private Sub GetDomainNameAndUserNameForGuest()
   		Dim nCharPos
		Dim strAccount
   		If IsAllowGuestsOnSite Then
				strAccount = GuestAccount	
						
				nCharPos = InStr(1, StrAccount, "\")
							
				If nCharPos = 0 Then 
					nCharPos = InStr(1, StrAccount, "/")		
				End If
						
				GuestAccount = Left(StrAccount,  Cint(nCharPos)-1)
				GuestUserName = Right(StrAccount, Len(strAccount) - nCharPos)
			
			Else
				GuestAccount = ""
		End If
   	End Sub
   	'-- [Private] Validate password for system account and guest account
   	Private Function ValidateNTUser()
   	
   		On Error Resume next
   		Dim bIsRightSysPassword

   		'-- Check SysAccount or SysPassword are changed or not
   		If  Not CBool(bHiddenSysPassword) Then
   				bIsRightSysPassword = True
   		Else
				'-- Get NT domain name and password 
				Call GetDomainNameAndPassword

				'-- Validate password
				Dim PNT 
				Set PNT = m_pSCAPI.GlobalSettings(ResServerIndex).UserAuthenticationSettings.NT
				
		   		' -- Error Check
				If Err.Number <> 0 Then
					Error = IDS_UNEXPEXTED_ERROR
					ErrorDetail = Err.Description 
					Exit Function
				End If
				
				bIsRightSysPassword = PNT.ValidateNTUserWithPassword(SysAccount, _
										SysUserName, _
										SysPassword) 
   		End If
   		' -- Error Check
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR
			ErrorDetail = Err.Description 
			Exit Function
		End If
		
   		Dim bIsRightNTUserForGuest
   		Call GetDomainNameAndUserNameForGuest()
   		
   		'-- Return false if password is false 
   		If Not bIsRightSysPassword Then 			
   			ValidateNTUserStatus = IDS_VALIDATE_SYSTEM_FAIL
   			ValidateNTUser = IDS_VALIDATE_SYSTEM_FAIL
   		Else
   			
			If IsAllowGuestsOnSite Then
				
				bIsRightNTUserForGuest = PNT.ValidateNTUser(GuestAccount,GuestUserName)

				If Err.number <>0 Then
					Error = IDS_UNEXPEXTED_ERROR 
					ErrorDetail = Err.Description
					Exit Function
				End If
		
				
				If  bIsRightNTUserForGuest Then
					
					ValidateNTUserStatus = IDS_VALIDATE_SYSTEMANDGUESTACCOUNT_OK
					ValidateNTUser = IDS_VALIDATE_SYSTEMANDGUESTACCOUNT_OK
				Else 

					ValidateNTUserStatus = IDS_VALIDATE_GUEST_FAIL
					ValidateNTUser = IDS_VALIDATE_GUEST_FAIL
				End If 			
			End If
   			'-- clear object
   			Set PNT = Nothing 
   		End If
   	
   		' -- Error Check
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR
			ErrorDetail = Err.Description
			Exit Function
		End If
		
   	End Function 
   	
   	'-- [Private] Validate form submit or not to decide validate or not
   	Private Function GetFormElements()
   	
   		' -- Get Form Elements
   		
   		SysAccount = Request.Form(DIS_FORM_ELEMENTS_SECURITY_TEXTBOX_NTACCOUNT)
   		SysPassword = Request.Form(DIS_FORM_ELEMENTS_SECURITY_TEXTBOX_SYSPASSWORD)
		
	    bHiddenSysPassword = Request.Form(DIS_FORM_ELEMENTS_SECURITY_HIDDEN_SYSPASSWORD)
	 
		IsAllowGuestsOnSite = Request.Form(DIS_FORM_ELEMENTS_SECURITY_SELECTBOX_GUESTONSITE)
		GuestAccount = Request.Form(DIS_FORM_ELEMENTS_SECURITY_TEXTBOX_GUESTACCOUNT)
		CookieLifeTime = Request.Form(DIS_FORM_ELEMENTS_SECURITY_SELECTBOX_COOKIELIFETIME )
		IsCheckMachineIP = Request.Form(DIS_FORM_ELEMENTS_SECURITY_TEXTBOX_CHECKMACHINEIP )
		
		' -- Return value
		GetFormElements = CBool(Request.Form(DIS_FORM_ELEMENTS_GENERAL_FORMSUBMIT))
   	End Function
   	
	
	'--[Private] Get API data for UI
	Private Sub GetAPIDataForUI()
		
		On Error Resume next
		
		'-- Init check password value
	
		bHiddenSysPassword = "false"
			
		'-- Get data from API
		Dim pLocalSetting
		Set pLocalSetting = m_pSCAPI.LocalSettings(ResServerIndex )
		    	
	    ' -- Error Check
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & " " & IDS_UI_ERROR_GET_LOCAT_SETTING
			ErrorDetail = Err.Description
			Exit Sub
		End If
		
	    Dim pSysAccount
		Set pSysAccount = pLocalSetting.ResSysAccount
		
		' -- Error Check
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & " " &  IDS_UI_ERROR_GET_SYSTEM_ACCOUNT
			ErrorDetail = Err.Description
			Exit Sub
		End If
		
		'--Get data for sys account
		SysAccount = pSysAccount.domain
		
		' -- Error Check
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & " " & IDS_UI_ERROR_GET_SYSTEM_ACCOUNT_DOMAIN 
			ErrorDetail = Err.Description
			Exit Sub
		End If

		SysUserName = pSysAccount.username
		' -- Error Check
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & " " & IDS_UI_ERROR_GET_SYSTEM_ACCOUNT_USERNAME
			ErrorDetail = Err.Description
			Exit Sub
		End If
			
		' -- Error Check
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & " " & IDS_UI_ERROR_GET_SYSTEM_ACCOUNT_PASSWORD
			ErrorDetail = Err.Description
			Exit Sub
		End If
		
		Dim pGlobalSetting
		Set pGlobalSetting = m_pSCAPI.GlobalSettings(ResServerIndex )
		
		' -- Error Check
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & " " & IDS_UI_ERROR_GET_GLOBAL_SETTING
			ErrorDetail = Err.Description
			Exit Sub
		End If
		
		Dim pSService
		Set pSService = pGlobalSetting.SecurityService
		' -- Error Check
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & " " & IDS_UI_ERROR_GET_SECURITY_SERVICE
			ErrorDetail = Err.Description
			Exit Sub
		End If
		    	
		'-- Get cookie life time and CheckMachine IP
		CookieLifeTime = pSService.CookieLifetimeInMinutes	
		
		' -- Error Check
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & " " & IDS_UI_ERROR_GET_COOKIE_LIFETIME
			ErrorDetail = Err.Description
			Exit Sub
		End If
		
		IsCheckMachineIP = pSService.CheckMachineIP
		
		' -- Error Check
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR  & " " & IDS_UI_ERROR_GET_CHECK_MACHINE_IP
			ErrorDetail = Err.Description
			Exit Sub
		End If

		
		Dim pGuestAccess 
		Set pGuestAccess = pGlobalSetting.GuestAccess
		
		' -- Error Check
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & " " & IDS_UI_ERROR_GET_GUEST_ACCESS
			ErrorDetail = Err.Description
			Exit Sub
		End If
		
		'-- Get Allow Guest On Site  
		IsAllowGuestsOnSite = pGuestAccess.IsEnabled
		
		' -- Error Check
		If Err.number <>0 Then
			Error = IDS_UNEXPEXTED_ERROR & " " & IDS_UI_ERROR_GET_GUEST_ACCESS
			ErrorDetail = Err.Description
			Exit Sub
		End If
		
		If IsAllowGuestsOnsite Then
			'--Get Guest account
			GuestAccount = pGuestAccess.Domain
			If Err.number <>0 Then
				Error = IDS_UNEXPEXTED_ERROR  & " " & IDS_UI_ERROR_GET_GUEST_ACCESS_DOMAIN
				ErrorDetail = Err.Description
				Exit Sub
			End If

			GuestUserName = pGuestAccess.UserName

			If Err.number <>0 Then
				Error = IDS_UNEXPEXTED_ERROR  & " " & IDS_UI_ERROR_GET_GUEST_ACCESS_USERNAME
				ErrorDetail = Err.Description
				Exit Sub
			End If
		End If    		
	       
		 '-- clear objects	
		 Set pSysAccount = Nothing
		 Set pSService = Nothing
		 Set pGuestAccess = Nothing
		 Set pGlobalSetting = Nothing
		 Set pLocalSetting = Nothing
												
	End Sub
	
	' ---- [Private] Chk if we are using LocalHost
	Private Sub ChkIsLocalHostConnection()

		Dim strRemoteHost
		Dim strLocalHost

		strRemoteHost = CStr(Request.ServerVariables(IDS_ACCESS_REMOTE_ADDR))
		strLocalHost = CStr(Request.ServerVariables(IDS_ACCESS_LOCAL_ADDR))

		If (strLocalHost = IDS_LOCAL_HOST_ID) OR (strRemoteHost = IDS_LOCAL_HOST_ID) OR _
		   (strLocalHost = strRemoteHost) Then
			IsLocalHost = True
		End If
	End Sub



	' ---- [Private] Chk is SSL is running
	Private Sub SetupSSLRunningFlag()

		Dim strSSLStatus 

		strSSLStatus = CStr(Request.ServerVariables(IDS_ACCESS_HTTPS))

		If strSSLStatus = "on" Then
			IsSSLRunning = True
		End If

	End Sub
	
	
End Class
%>