How to access Windows IIS User Info with Perl

From: BigNin (page.nix_at_gmail.com)
Date: 03/29/05


Date: 29 Mar 2005 11:56:46 -0800

I have a Perl script which runs on a Unix server with Apache as the
http server. With the use of an .htaccess file, only users that login
with basic authentication are able to run the script. This script is
only available on the intranet so it's internal company use only. The
perl script compares the authenticated username with a text file which
contains groups to which certain employees belong and serves different
HTML depending on the user's group memberships.

In our IT department's infinite wisdom, they have moved us to a Windows
2003 server running IIS. I've made the modifications to my script so
that it runs, but the IIS server is configured for Windows
Authentication. The IT department states that this allows users to
login into the network when they first turn on their PC and then the
users don't have to enter any additional usernames or passwords to
authenticate with IIS and my script. My group text file has now been
converted to Windows security groups.

I have searched HotScripts.com, ActiveState.com, and Google and I can
not find any example of how to get perl to read Windows security groups
and tell me which ones the user belongs to. I'm guessing that someone
somewhere has done this before.

My administrators gave me some ASP code that supposedly can do what I
want, but that doesn't really help me do it in perl. For reference, I
have included it below:

Public Function Groups(ByVal SearchResult As
System.DirectoryServices.SearchResult) As String
    Dim i As Integer
    Dim tmp As String
    Dim groupSid As Object
    Dim sid() As Byte
    Try
        Dim de As DirectoryEntry = SearchResult.GetDirectoryEntry
        'pull username and password from web.config file.
        de.Username =
Configuration.ConfigurationSettings.AppSettings("User")
        de.Password =
Configuration.ConfigurationSettings.AppSettings("Pass")
        de.RefreshCache(New String() {"tokenGroups"})
        'this line is sometimes necessary to get tokenGroups in the
property cache...
        'loop through each sid in the tokenGroups
        For Each groupSid In de.Properties("tokenGroups")
            'just another way of doing a ctype.
            sid = DirectCast(groupSid, Byte())
            'set up the groupentry for query
            'ConvertToOctetString is the important part here. This is
where the real work is.
            Dim groupEntry As New
DirectoryEntry(String.Format("LDAP://", ConvertToOctetString(sid)))
            Dim propcoll As PropertyCollection = groupEntry.Properties
            Dim key As String
            Dim values As Object

            'loop through all of the properties for this record
            For Each key In propcoll.PropertyNames
                'loop through all the values associated with our key
                For Each values In propcoll(key)
                    If LCase(key) = "distinguishedname" Then
                      Dim temp As String = values.ToString
                      If Not InStr(temp, "ImportedExchange") Then
                        Dim atemp() As String = temp.Split(",")
                        tmp &= Replace(atemp(0).ToString, "CN=", ",")
                        If Left(tmp, 1) = "," Then
                            tmp = Mid(tmp, 2)
                        End If
                      End If
                    End If
                Next
            Next
        Next

    Catch ex As Exception
        'process exception
    End Try

    Return tmp
End Property

'overload for lazy programming
Public Overloads Shared Function ConvertToOctetString(ByVal values As
Byte()) As String
    Return ConvertToOctetString(values, False, False)
End Function

'overload for lazy programming
Public Overloads Shared Function ConvertToOctetString(ByVal values As
Byte(), _
     ByVal isAddBackslash As Boolean) As String
    Return ConvertToOctetString(values, isAddBackslash, False)
End Function

'This is where the work really comes in. This method allows us to
convert the sid
'into a usable string that LDAP can use to search for the groups this
user belongs to.
Public Overloads Shared Function ConvertToOctetString(ByVal values As
Byte(), _
     ByVal isAddBackslash As Boolean, ByVal isUpperCase As Boolean) As
String
    Dim iterator As Integer
    Dim builder As System.Text.StringBuilder

    Dim slash As String
    If isAddBackslash Then
        slash = "\"
    Else
        slash = String.Empty
    End If
    Dim formatCode As String
    If isUpperCase Then
        formatCode = "X2"
    Else
        formatCode = "x2"
    End If
    builder = New System.Text.StringBuilder(values.Length * 2)
    For iterator = 0 To values.Length - 1
        builder.Append(slash)
        builder.Append(values(iterator).ToString(formatCode))
    Next

    Return builder.ToString()

End Function
 
 
Thanks in advance to any suggestions.



Relevant Pages

  • Re: Password Expire
    ... We have one fron end Edge server in our DMZ which passes email onto two ... I used to schedule a script to run every 24 hours on my Exchange 2003 ... Dim fso, txtarray, BodyText ... Call ProcessFolder (objContainer, numDays) ...
    (microsoft.public.exchange.admin)
  • Re: check disk space and email if above xx%?
    ... ' DriveSpace to HTM and email results VBS script ... 'This script will pull a listing of servers from (in this example, ... 'Additionally, in the summary and warning htm, each server has been ... Dim strComputer, Silent, strGBFree, strDiskFreeSpace, strDrvString ...
    (microsoft.public.scripting.vbscript)
  • Re: VBscript in 2008 Server Task Scheduler will not run
    ... That means that yes, pointing directly to the script should work ok; it will probably default to the 64-bit version of the host IIRC, but that should be ok. ... In 2003 server and 2008 32 bit you can just point the task scheduler command ... > Dim mToday ... > For each inputData in input ...
    (microsoft.public.scripting.vbscript)
  • Re: Script taking longer than expected to complete: Consequences?
    ... If the user closes the browser window, the script will continue to run on ... Once the request is made to the server, ... > Dim sql ...
    (microsoft.public.inetserver.asp.general)
  • Using VBScript to accept POST data
    ... I would like to have the web server either host a VBS file, ... Dim objShell, objScriptExec, a, strIpConfig, myvar ... first script into this script as an agrument, ... Dim strDirectory, strFile, strText ...
    (microsoft.public.scripting.vbscript)