Convert CSV to LDIF for import into ldap? - csv

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.

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

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.

Related

How to create a Function to check the supplied URL against a Whitelist of approved URLs Or Domains before redirecting

I am new in the Classic ASP, and I want to ask if there's anyone who has experienced and knew of how to create a validation Function in Classic ASP to check the supplied URL against a Whitelist of approved URLs or Domains before redirecting. Below is the sample codes
If not GetNameChk then
chk = AssignCustAddr(id)
If UCase(Left(chk,5)) = "ERROR" then
ReportAnWarning chk, RelConn
End if
gostring = "proposer.asp"
Else
gostring = "CustGet.asp?AddrId=" & encrypt1(trim(request.form("AddrId" & id))) & "&CustId=" & encrypt1(trim(request.form("CustId" & id))) & "&id=" & encrypt1(id) & "&SurName" & id & "="
gostring = gostring & encrypt1(Replace(trim(request.form("SurName" & id))," ","+"))
gostring = gostring & "&GivenName" & id & "="
gostring = gostring & encrypt1(Replace(trim(request.form("GivenName" & id))," ","+"))
gostring = gostring & "&NRIC" & id & "="
gostring = gostring & encrypt1(Replace(trim(request.form("nric" & id))," ","+"))
End If
response.redirect gostring
The gostring URL or Domain is need to Validate and Whitelist. Is there anyone knew about this? Thank you in advance!
I have tried a sort of validation but it's only sanitized(removing the untrusted characters) the value of the URL or Domain please see below
Function CRLF(strChar)
If strChar ="" Or IsNull(strChar) Then CRLF ="":Exit Function
Dim strBadChar, arrBadChar, tempChar, I
strBadChar = "--,+,\,\r,*,%,^,#,!,#,$,(,),[,],{,},;,<,>,~,`,\n," & Chr(34) & "," & Chr(0) & ""
arrBadChar = Split(strBadChar, ",")
tempChar = strChar
For I = 0 To UBound(arrBadChar)
tempChar = Replace(tempChar, arrBadChar(I), "")
Next
tempChar = Replace(tempChar, "##", "#")
CRLF = tempChar
End Function
So, what I am expecting is to check the supplied URL/Domain (gostring) and compare against a whitelist of approved URLs or Domains before redirecting.

Data type conflict in criteria expression

I'm currently trying to use a db (.accdb-file) in my vbscript.
my function from a .vbs-file that's executed by a hta-file:
function dbCall(sAction, sPayload, sTable, sConCol, sConVal)
updateLocalDB()
Dim sConnectionString, objConnection, objRecordset, dbQuery, lTemp
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & localDB
'Query für die DB zusammenbauen
Select Case sAction
Case "get"
If sConCol = False Then
dbQuery = "SELECT " & sPayload & " FROM " & sTable
Else
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE " & sConCol & " = '" & sConVal & "'"
End If
End Select
'do DB-Stuff
objConnection.open sConnectionString
objRecordset.Open dbQuery, objConnection
IF objRecordset.fields.Count = 1 Then
lTemp = objRecordset.fields(0)
End If
objRecordset.close
objConnection.close
dbCall = lTemp
End function
the results are used to decide some things for the design of my hta-file.
I have multiple uses for it.
calling it like this:
getSlotAmount = dbCall("get", "value", "config", "name", "MiPaCount")
returns a number according to
name (short String)
value (Integer)
MiPaCount
5
but if I call it with
iStart = dbCall("get", "startzeit", "slots", "ID", tmp)
I get the error from the top.
The table currently looks like
ID (Integer, Byte)
Startzeit (short String)
1
11:30
previously the "startzeit"-column was a time-type but since I'm only storing and not calculating Data in the DB it's not that important
I tried to use an integer and a string as tmp but in all these cases it gives me an error on the line where I try to objRecordset.open (german: "Datentypenkonflikt in Kriterienausdruck", translating it by google resulted in the title).
While creating this question SO offered me multiple similar posts that lead me to two more experiments.
When I tried to change the string "dbQuery = ..." like this:
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE " & sConCol & " = '" & sConVal
it said that a required value is missing.
When I tried this:
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE " & sConCol & " = """ & sConVal & """"
I again got the error from the title so I returned to my original string (since it works fine with my first example of using dbCall).
What am I missing?
EDIT:
Found a solution based on the accepted answer. with this function it works:
function dbCall(sAction, sPayload, sTable, conCol, conVal, conType)
updateLocalDB()
Dim sConnectionString, objConnection, objRecordset, dbQuery, lTemp
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & localDB
'Query für die DB zusammenbauen
Select Case sAction
Case "get"
dbQuery = "SELECT " & sPayload & " FROM " & sTable
End Select
Select Case conType
Case "str"
dbQuery = dbQuery & " WHERE " & ConCol & " = '" & ConVal & "'"
Case "int"
dbQuery = dbQuery & " WHERE " & ConCol & " = " & ConVal
End Select
'do DB-Stuff
objConnection.open sConnectionString
objRecordset.Open dbQuery, objConnection
IF objRecordset.fields.Count = 1 Then
lTemp = objRecordset.fields(0)
End If
objRecordset.close
objConnection.close
dbCall = lTemp
End function
You are missing that you wish to handle several data types, and to take care of reserved words for field names. So, for example, you would need:
' For text:
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE [" & sConCol & "] = '" & sConVal & "'"
' For numbers:
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE [" & sConCol & "] = " & Str(sConVal) & ""
' For dates:
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE [" & sConCol & "] = #" & Format(DateValue(sConVal), "yyyy\/mm\/dd") & "#"
You may be able to simplify this by using my function CSql, but I haven't tested it in a scenario like this.
Try replacing this line:
iStart = dbCall("get", "startzeit", "slots", "ID", tmp)
with this:
iStart = dbCall("get", "startzeit", "slots", "ID", "tmp")
You're looking for a value in that parameter and tmp (without quotations) is expected to be a numerical one.

How do I resolve run-time error in this code?

I'm getting a run-time error 3075
I have checked all the parentheses and quotes, everything seems okay but still not running.
'Now Check the database to see if there are existing records for the Month and year in question
txtSQL = "SELECT Count([Rec_ID]) AS CountID FROM [dbo_NBD_EMEA_NBD_Source_Download] Where [Ledger_Year] = " & CurYear & " AND [Ledger_Month] = " & CurMonth & " AND ([Region_Cd] = 'EMEA' OR [Region_Cd] = 'APAC' OR [Region_Cd] = 'INDA');"
Set dbs = CurrentDb
Set Rs = dbs.OpenRecordset(txtSQL, dbOpenSnapshot)
NumRecs = Rs("CountID")
Rs.Close
If NumRecs > 0 Then
Prompt1 = "WARNING... There are " & NumRecs & " Records already in the database for Year " & CurYear & " Month " & CurMonth & Chr(10) & " Do you want to ERASE the existing records and REPLACE them with the NEW RECORDS in your IMPORT FILE " & Chr(10) & Selected_File & "?"
Response1 = MsgBox(Prompt1, Style2, "DELETE EXISTING RECORDS IN DATABASE?")
If Response1 = vbOK Then 'Continue with Delete of existing Records and Import of new
Prompt2 = "Confirm... Existing Records will be deleted and replaced with your new file"
Response2 = MsgBox(Prompt2, Style2, "Confirm Deletions")
If Response2 = vbOK Then
'Run Stored Procedure to delete the records
Me.ProcessStatus.Caption = "Deleting existing records"
Set db = DBEngine.Workspaces(0).OpenDatabase("", False, False, Connect_String)
db.Execute "XPROC1_NBD_EMEA_Source_Download_Delete " & CurYear & " , " & CurMonth, dbSQLPassThrough
Set db = Nothing
Else
If Response2 = vbCancel Then 'If no confirmation of delete then cancel
Me.ProcessStatus.Caption = "Import Canceled"
Exit Sub
End If
End If
Else
If Response1 = vbCancel Then ' Cancel import
Me.ProcessStatus.Caption = "Import Canceled"
Exit Sub
End If
End If
End If
This line does not look to be valid SQL:
db.Execute "XPROC1_NBD_EMEA_Source_Download_Delete " & CurYear & " , " & CurMonth, dbSQLPassThrough
The Execute method will run an action query or execute a supplied SQL statement, it does not evaluate VBA code within another database.

autoit modi.document error Variable must be of type "Object"

#include <Constants.au3>
#include <ScreenCapture.au3>
#Include <Misc.au3>
#include <Array.au3>
#include <GDIPlus.au3>
$image = #MyDocumentsDir & "\aaa_teste.jpg"
ConsoleWrite(OCR($image))
Func OCR($Image)
Local $miDoc, $Doc
Local $str
Local $oWord
Local $sArray[500]
Local $as_Text = ""
$miDoc = ObjCreate("MODI.Document")
ConsoleWrite("ObjCreate - The error value is: " & #error & #CRLF)
$miDoc.Create(#TempDir & '\OCR_ImageResize.jpg')
ConsoleWrite("Create - The error value is: " & #error & #CRLF)
$miDoc.Ocr(9, False, False)
ConsoleWrite("Ocr - The error value is: " & #error & #CRLF)
If $Error = False Then
$i = 0
For $oWord in $miDoc.Images(0).Layout.Words
$str = $str & $oWord.text & #CrLf
ConsoleWrite($oWord.text & #CRLF)
$sArray [$i] = $oWord.text
$i += 1
Next
$as_Text = _ArrayToString($sArray," ",0)
$as_Text = StringReplace($as_Text, " ", "", 0)
ElseIf $Error = True Then
$Error = False
EndIf
$miDoc.Close(False)
Return $as_Text
EndFunc
I`m trying read a text from a screencapture The error is:
"C:\Users\jose.barbosaneto\Desktop\TESTE.au3" (23) : ==> Variable must be of type "Object".:
$miDoc.Create(#TempDir & '\OCR_ImageResize.jpg')
$miDoc^ ERROR
Please help me
The only downside of MODI library is that it relies 100 % on MS Office.
So if you don't have the proper version installed it will not find the needed COM object.
Check the Office version you have installed and see if MODI is in there.
Go in Uninstall Programs and click on Change/Repair on the MS Office version you have. From there you can check if MODI documents are installed.
Edit: Install MDI to TIFF Files Converter and SharePoint Designer from this link: https://support.microsoft.com/en-us/kb/982760
After installing them go to > Uninstall a Program>MS Office SharePoint Designer 2007> Right click on it and select Change>select Add or Remove Features.
From there in the menu you will find the option Office Tools. Expand it.
Then click on the small arrows on MODI(Microsoft Office Document Imaging) and select "Run all from my Computer".
And you are ready to go!
Edit2: Here is a tested and working example.
#include <GUIConstants.au3>
#include <Array.au3>
Dim $miDoc, $Doc
Dim $str
Dim $oWord
Dim $sArray[500]
Const $miLANG_CZECH = 5
Const $miLANG_DANISH = 6
Const $miLANG_DUTCH = 19
Const $miLANG_ENGLISH = 9
Const $miLANG_FINNISH = 11
Const $miLANG_FRENCH = 12
Const $miLANG_GERMAN = 7
Const $miLANG_GREEK = 8
Const $miLANG_HUNGARIAN = 14
Const $miLANG_ITALIAN = 16
Const $miLANG_JAPANESE = 17
Const $miLANG_KOREAN = 18
Const $miLANG_NORWEGIAN = 20
Const $miLANG_POLISH = 21
Const $miLANG_PORTUGUESE = 22
Const $miLANG_RUSSIAN = 25
Const $miLANG_SPANISH = 10
Const $miLANG_SWEDISH = 29
Const $miLANG_TURKISH = 31
Const $miLANG_SYSDEFAULT = 2048
Const $miLANG_CHINESE_SIMPLIFIED = 2052
Const $miLANG_CHINESE_TRADITIONAL = 1028
Local $ImagePath = #DesktopDir & "\Untitled.png" ; you can choose .tif format if you also want
; Initialize error handler
$oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$miDoc = ObjCreate("MODI.Document")
$miDocView = ObjCreate("MiDocViewer.MiDocView")
$Viewer = GUICreate("Embedded MODI Viewer", 640, 580, (#DesktopWidth - 640) / 2, (#DesktopHeight - 580) / 2, _
$WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)
;Creates an ActiveX Control in the GUI.
$GUIActiveX = GUICtrlCreateObj($miDocView, -1, -1, 640, 580)
GUICtrlSetResizing($Viewer, $GUI_DOCKAUTO)
$miDoc.Create($ImagePath)
$miDoc.Ocr($miLANG_ENGLISH, True, False)
; Show GUI
GUISetState()
$miDocView.Document = $miDoc
$miDocView.SetScale(0.75, 0.75)
While 1
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
$i = 0
For $oWord In $miDoc.Images(0).Layout.Words
$str = $str & $oWord.text & #CRLF
ConsoleWrite($oWord.text & #CRLF)
$sArray[$i] = $oWord.text
$i += 1
Next
_ArrayDisplay($sArray, "OCR Result")
;MsgBox(0,"",$miDocView.FileName)
;------------------------------ This is a COM Error handler --------------------------------
Func MyErrFunc()
$HexNumber = Hex($oMyError.number, 8)
MsgBox(0, "COM Error Test", "We intercepted a COM Error !" & #CRLF & #CRLF & _
"err.description is: " & #TAB & $oMyError.description & #CRLF & _
"err.windescription:" & #TAB & $oMyError.windescription & #CRLF & _
"err.number is: " & #TAB & $HexNumber & #CRLF & _
"err.lastdllerror is: " & #TAB & $oMyError.lastdllerror & #CRLF & _
"err.scriptline is: " & #TAB & $oMyError.scriptline & #CRLF & _
"err.source is: " & #TAB & $oMyError.source & #CRLF & _
"err.helpfile is: " & #TAB & $oMyError.helpfile & #CRLF & _
"err.helpcontext is: " & #TAB & $oMyError.helpcontext _
)
SetError(1) ; to check for after this function returns
EndFunc ;==>MyErrFunc

Parameterized query in ADO - add data into database

Bit of a newbie to vbscript, not particularly fond of it but my client uses it so I can't do much about it! I'm having trouble using parameterized queries to add data into the database! Please save me guys!!
So far, the existing code is
function ecaddupdateDEV (thistable, idvar, vararray)
for each varname in vararray
valstring = valstring & ", session(""" & varname & """)"
thesefields = thesefields & ", " & varname
next
thesefields = idvar & thesefields
valstring = " array( " & ecremovel(valstring, 2) & ")"
vals = eval(valstring)
set temprs = Server.CreateObject("ADODB.RecordSet")
if session(idvar) = 0 then
'response.Write("chid is 0 add new record")
' ADD THIS REC
temprs.open thistable, db,1,3,2
temprs.AddNew vararray, vals
ecaddupdateDEV = temprs(idvar)
temprs.close
set temprs = ecquery("select " & idvar & " from " & thistable & " order by " & idvar & " desc ")
ecaddupdateDEV = temprs(idvar)
else
' UPDATE THIS REC
selectclause = "select " & thesefields & " from " & thistable & " where " & idvar & " = " & session(idvar)
temprs.open selectclause, db ,1,3
temprs.update vararray, vals
ecaddupdateDEV = temprs(idvar)
temprs.close
end if
set temprs = nothing
end function
The temprs variable I want to make parameterized for security purposes, obviously lol! I just have no clue how to do it! What I've read I just found confusing, please help!! Many thanks in advance!
Instead of a Recordset, use a Command. Here's the example, that will get you going, but you may have to tweak it, as from experience, named parameters don't really work, so you assign their value in the order in which they appear in the SQL statement.
How To Call a Parameterized Query to an Access Database with ADO