<%@ LANGUAGE=VBScript %>
<% Option Explicit %>
<!--#include virtual="/NR/Shared/ASPUtils.inc"-->
<!--#include virtual="/NR/System/WBC/Internals/ServerControls/StateControl/CNCStateControl.inc"-->
<!--#include virtual="/NR/System/Access/Constants/ProgrammingConstants.inc" -->
<!--#include file="CNCStateRenderer_SecurityAlert.inc" -->

<%
'ManualLoginSubmit.asp: Processes the form submitted by ManualLogin.asp.  Attempts
'to verify the username and password submitted.

Response.Buffer = True
Response.Expires = 0


Dim ERRORTXT
Dim strToken, strMachineName, strDomain, strUser, strPassword, chkShowWarning

ERRORTXT = "Username and/or password invalid."

On Error Resume Next


Dim AutoSession
set AutoSession = Server.CreateObject("ResolutionObjectModel.AutoSession3")

'Retrieve the machine name, domain, username and password from the form.
strMachineName = Request.ServerVariables("REMOTE_ADDR")
strDomain = Request.Form("NR_DOMAIN")
strUser = Request.Form("NR_USER")
strPassword = Request.Form("NR_PASSWORD")
chkShowWarning = Request.Form(IDS_CHKBOX_SHOW_WARNING_IN_FUTURE)


' Only Perform SSL checking if there is no SSL
If CStr(Request.ServerVariables(IDS_ACCESS_HTTPS))= "off" Then

	Dim CheckBoxStateControl

	Set CheckBoxStateControl = New CNCStateControl

	Dim pStateControlShowWarning
	Set pStateControlShowWarning = New CNCStateRenderer_SecurityAlert

	' A chk is detected
	If chkShowWarning <> "" Then
		Call CheckBoxStateControl.InitStateControl( IDS_STATE_ID_SHOW_SSL_WARNING, pStateControlShowWarning, IDS_STATE_SKIP_SSL_WARNING, True )
		Call CheckBoxStateControl.AddState( IDS_STATE_SHOW_SSL_WARNING )
		Call CheckBoxStateControl.AddState( IDS_STATE_SKIP_SSL_WARNING )
		CheckBoxStateControl.CurrentState = IDS_STATE_SKIP_SSL_WARNING
	End If

End If



' massage the domain name/user name combination into a format recognized by
' Content Management Server. For Windows NT domains, the login name is WinNT://<domain>/<user>
' and for LDAP it's the full LDAP path as stored in the DB with the CN=<user>
' inserted into the path just after the "LDAP://"
' (e.g. LDAP://cn=user1,ou=orgunit1,dc=mydomain,dc=com)

If UCase( Left( strDomain, 5) ) = "WINNT" and InStr( 1, strUser, "@" ) = 0 Then
	strUser = FormWinNTLoginName( strDomain, strUser )
Else
	strUser = FormLDAPLoginName( strDomain, strUser )
End If

call AutoSession.Session.Login(strUser, strPassword, strMachineName, 0)

'If unsuccessful, return to the manual login screen.
If (Err.Number <> 0) Then
    ServerSideRedirect("ManualLogin.asp?ReturnUrl=" + Server.URLEncode(Request.Form("ReturnUrl")) + "&REASONTXT=" + Server.URLEncode(ERRORTXT))
	Err.Clear

'If successful, set authentication cookie and redirect to ReturnUrl.
Else
	strToken = AutoSession.Session.LoginCookie

	Response.Cookies("CMSAUTHTOKEN") = strToken
	Response.Cookies("CMSAUTHTOKEN").Path = "/"
    ServerSideRedirect(Request.Form("ReturnUrl"))
End If


' The following function composes an LDAP user name
' strDomain can be specified in the following formats:
'	Distinguished name - LDAP://cn=users,dc=microsoft,dc=com
'	Canonical name - microsoft.com (domain), microsoft.com/users (container)
'strUser can have the following format:
'	Distinguished name - LDAP://cn=jsmith,cn=users,dc=microsoft,dc=com
'	Login name - jsmith
'	UPN name - jsmith@microsoft.com

Function FormLDAPLoginName( strDomain, strUser )
	'check if username is UPN name or distinguished name
	if InStr( 1, strUser, "@" ) > 0 or IsDistinguishedName(strUser) then
		'just let it go
		FormLDAPLoginName = strUser

	elseif IsDistinguishedName(strDomain) then
		FormLDAPLoginName = FormDistinguishedName(strDomain, strUser)

	elseif	InStr( 1, strDomain, "/" ) > 0 then	'canonical name, container
		'just add user name to the end
		if Right(strDomain, 1) = "/" then
			FormLDAPLoginName = strDomain + strUser
		else
			FormLDAPLoginName = strDomain + "/" + strUser
		end if

	else	'canonical name, domain
		'form UPN name
		FormLDAPLoginName = strUser + "@" + strDomain
	end if

end function

' The following function takes in an LDAP path and a user string and
' inserts the user in the form cn=<user> after the last slash in the
' LDAP path.
Function FormDistinguishedName( strDomain, strUser )
	' find the position in the domain string that should preceed the
	' user name (ie. "LDAP://" or "LDAP://<site server name>:<port>/".
	' This ends up being the position after the last slash in the string.
	Dim nPos ' string position "pointer"
    nPos = 0
    While InStr( nPos + 1, strDomain, "/" ) > 0
        nPos = InStr( nPos + 1, strDomain, "/" )
    WEnd

    ' strip out the portion of the pathname that should come before the
    ' user name
	Dim strPrefix
    strPrefix = Left( strDomain, nPos )

    ' strip out remaining domain info to append to the end.
	Dim strTrimmedDomain
    strTrimmedDomain = Mid( strDomain, nPos + 1 )

    ' put the whole string together. Note that if the user name is a plain
    ' name without the "cn=" at the beginning, we will automatically
    ' prepend "cn=" to the supplied string
	Dim strLoginName
    If Left( UCase( strUser ), 3 ) = "CN=" Then
        strLoginName = strPrefix & strUser
    Else
        strLoginName = strPrefix & "cn=" & strUser
    End If

	' for robustness, if the user appended a comma to the end of the
	' user name, we won't add one. If there is no comma, we must add
	' one to flow into the rest of the distinguished name
    If Right( strUser, 1 ) = "," Then
        strLoginName = strLoginName & strTrimmedDomain
    Else
        strLoginName = strLoginName & "," & strTrimmedDomain
    End If

    FormDistinguishedName = strLoginName

End Function  ' FormDistinguishedName

Function IsDistinguishedName(strObjectName)

	if InStr( 1, strObjectName, "LDAP://" ) > 0  then
		IsDistinguishedName = true
		exit function
	end if

	dim strTemp, strDCPortion
	dim nPos, nPos2

	strTemp  = Ucase(strObjectName)
    nPos = 0

    While InStr( nPos + 1, strTemp, "DC" ) > 0
        nPos = InStr( nPos + 1, strTemp, "DC" )
        strDCPortion = Right(strTemp, Len(strTemp) - nPos - 1)

		nPos2 = 1
        while nPos2 < Len(strDCPortion)

        	if Mid(strDCPortion, nPos2, 1) = "=" then
        		IsDistinguishedName = true
        		exit function

			elseif Mid(strDCPortion, nPos2, 1) = "=" then
				strTemp = strDCPortion
				break
			end if

			nPos2 = nPos2 + 1

        wend
    WEnd

	IsDistinguishedName = false

end function



' The following function takes in an NT domain and a user string and
' creates a login name of the form WinNT://<user>
Function FormWinNTLoginName( strDomain, strUser )

	FormWinNTLoginName = strDomain & "/" & strUser

End Function  ' FormWinNTLoginName

%>

