<!--#include virtual="/NRConfig/SecurityConfig/CNCASPPageUI_SystemAccountConfig.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 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_SystemAccountConfig
	
	'Public property
	
	Public SysAccount
	Public SysUserName
	public SysPassword
	Public ResServerIndex    	
	Public ValidateNTUserStatus
	Public Error
	Public ErrorDetail
	Public IsSupportedBrowser 	
	Public bHiddenSysPassword
	Public IsSSLRunning
	Public IsLocalHost
	Public SecurityAlertURL
	
	'Define private variable	
	
	Private m_pSCAPI
	Private m_strSysAccount
    
    
	'---- [Private] Standard Class_Initialize
	Private Sub Class_Initialize()
		' -- init all member with default values
		ValidateNTUserStatus = IDS_VALIDATE_SYSTEMACCOUNT_OK
		SysAccount = ""
		SysUserName = ""
		SysPassword = ""
		
		bHiddenSysPassword = ""
		
		ResServerIndex = -1    	
		
		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_SYSTEMACCOUNT_OK Then
					Call UpdateDataToAPI
					If Err.Number = 0 Then
						Call CloseWindowAndRedirectToMainASP
					End If
				Else 								
					ValidateNTUserStatus = IDS_VALIDATE_SYSTEM_FAIL
					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_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 pLocalSetting 
		
		
		' -- Set new script time out 
		Dim ScriptTimeOut
		Server.ScriptTimeOut = 100000

		' -- Set the System Account   		
		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
		
		'--Back to default for script time and bufer
		Server.ScriptTimeOut = ScriptTimeOut
		
		
   		'-- clear object 
		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] Validate password for system account and guest account
   	Private Function ValidateNTUser()
   	
   		On Error Resume next
   		Dim bIsRightSysPassword
   		
   		ValidateNTUser = IDS_VALIDATE_SYSTEMACCOUNT_OK

   		'-- 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
		
		If Not bIsRightSysPassword Then
			ValidateNTUser = IDS_VALIDATE_SYSTEM_FAIL
		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)
	 
		' -- 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
		
		 '-- clear objects	
		 Set pSysAccount = 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
%>