-4

I have a MSSQL database with 13k users in a table with headers like username, password, fname, lname, etc. I would love to be able to turn this data into a proper ldap server.

Currently I've done the following: Create a job on the mssql server to export that table hourly in a csv. The query pulls username, password, fname, lastname,and memberid. ftp this data hourly to my linux ldap server.

Can someone recommend a method on how to turn this csv into ldif so I can run ldapmodify and put in all these entries into my ldap server? I'm assuming I can write a parser, but would love to know if there's already a product that can do csv to ldif conversion in this manner and if this plan makes any sense or if there's a better way to do this? Thanks.

DrZaiusApeLord
  • 155
  • 1
  • 1
  • 9

2 Answers2

1

Just in case someone is still looking for something like that, I made an autoit script to convert a given CSV file into LDIF:

;###########################################################################################
;##### Load requirements
#include <File.au3>

;###########################################################################################
;####       Start the app

LDIF_Converter()


;###########################################################################################
;####       App function
Func LDIF_Converter()
    TrayTip("CSV2LDIF Converter","Konvertierung gestartet...",2000,1)

    Local $file = "import_ADS.ldif", $contacts = 0, $sFilePath = "3CXImport.csv"
    ; Remote old file
    FileDelete($file);
    ; Write Header
    FileWrite($file,"#Base structure for each person");
    Filewrite($file,@crlf)
    FileWrite($file,"dn: ou=Telefonbuch,dc=example,dc=com");
    Filewrite($file,@crlf)
    FileWrite($file,"ou: Telefonbuch");
    Filewrite($file,@crlf)
    FileWrite($file,"objectClass: organizationalUnit");
    Filewrite($file,@crlf)
    FileWrite($file,"objectClass: top");
    Filewrite($file,@crlf)
    Filewrite($file,@crlf)

    ; Read out CSV File
    _FileReadToArray($sFilePath, $contacts, $FRTA_NOCOUNT, ";")

    ; Write to File for each contact
    For $i = 0 To UBound ($contacts) - 1
        ; Clear contact cache
        $cache = ""
        ; Write object ID & comman attributes
        $cache = $cache & "dn: uid="&$i+1&",ou=Telefonbuch,dc=example,dc=com" & @CRLF
        $cache = $cache & "objectClass: inetOrgPerson" & @CRLF
        $cache = $cache & "objectClass: organizationalPerson" & @CRLF
        $cache = $cache & "objectClass: person" & @CRLF
        $cache = $cache & "objectClass: top" & @CRLF

        ; Check if company or person and set the corresponding CN
        if $contacts[$i][1] == "" Then
            $cn = $contacts[$i][0]
        Else
            $cn = $contacts[$i][1] & " " & $contacts[$i][2]
        EndIf

        ; Write common name
        $cache = $cache & "cn: "&$cn & @CRLF
        if $contacts[$i][1] == "" Then
            $sn = $contacts[$i][0]
        Else
            $sn = $contacts[$i][1] & " " & $contacts[$i][2]
        EndIf

        ; Write lastname/company
        $cache = $cache & "sn: "&$sn & @CRLF

        ; Write mobile number
        $mobile = StringRegExpReplace($contacts[$i][5],'[a-zA-Z;\\/:.,*?\"<>|& ]',"")
        if $mobile <> "" And $mobile <> " " Then
            $cache = $cache & "mobile: "& $mobile & @CRLF
        EndIf

        ; Write organisation
        if $contacts[$i][0] <> "" Then
            $cache = $cache & "o: "&$contacts[$i][0] & @CRLF
        EndIf

        ; Write telephone number
        $phone = StringRegExpReplace($contacts[$i][3],'[a-zA-Z;\\/:.,*?\"<>|& ]',"")
        if $phone <> "" And $phone <> " " Then
            $cache = $cache & "telephoneNumber: "& $phone & @CRLF
        EndIf

        ; Write fax number
        $fax = StringRegExpReplace($contacts[$i][4],'[a-zA-Z;\\/:.,*?\"<>|& ]',"")
        if $fax <> "" And $fax <> " " Then
            $cache = $cache & "facsimileTelephoneNumber: "& $fax & @CRLF
        EndIf

        ; Write email
        if $contacts[$i][6] <> "" Then
            $cache = $cache & "mail: "&$contacts[$i][6] & @CRLF
        EndIf

        ; Seperator for contacts
        $cache = $cache & @CRLF
        ; Write all in one file access
        FileWrite($file,$cache)
    Next
    TrayTip("CSV2LDIF Converter","Konvertiertierung abgeschlossen ",2000)
    sleep(2000)
EndFunc
da.eXecutoR
  • 313
  • 5
  • 16
-1

A simple Google search revealed several CSV to LDIF converters.

For example scv2ldif2.pl. It is in Perl, but you can find at least several Windows implementation of Perl (for example Strawberry Perl.

ShaMan-H_Fel
  • 2,139
  • 17
  • 24