home *** CD-ROM | disk | FTP | other *** search
- <%
- // ldif-import.fts v1.0
- // PURPOSE
- // Takes an ldif file and imports them as local mailboxes
- //
- //
- // WARNING:
- // This script is supplied for reference only, it should be considered untested
- // with no warranty either written or implied. Use is strictly at you own risk.
- //
- #include <ftgate.fts>
- %>
- <HTML>
- <HEAD>
- <STYLE>
- STRONG
- {
- color: #660066;
- font-weight: bold;
- }
- BODY
- {
- font-family: "Arial";
- color: #330033;
- }
-
- </STYLE>
- </HEAD>
- <BODY>
- <TABLE WIDTH="100%">
- <TR><TD BGCOLOR="gold"><STRONG>FTGate LDIF Import v1.0</STRONG></TD></TR>
- </TABLE>
- <%
- if (form.config=="1")
- {
- var defaultLang = "lang-en"
-
- const STATE_VERSION = 0
- const STATE_DN = 1
- const STATE_ATTRVAL_CHANGE = 2
- const STATE_ATTRVAL = 3
- const STATE_CHANGE = 5
- const STATE_CHANGE_CONTROL = 7
-
- var error=false
-
- var changeAction=""
- var changeAttribute=""
- var changeType=""
- var changeDomain=""
- var changeMbx=""
- var replaceFirst=true
-
- var first=false
-
- var newDomain = new array[1]
- var domainCount=0
-
- var person=false
- var alias=false
-
- var aliasMail=""
- var aliasUid=""
-
- // defined in rfc2798
- var domain=""
- var mail=""
- var uid=""
- var displayName=""
- var cn=""
- var sn=""
- var gn=""
- var title=""
- var ou=""
- var description=""
- var phone=""
- var fax=""
- var password=""
-
- function error(text, line)
- {
- error=true
-
- if (line!=-1)
- text=text+" at line "+line
-
- output.writeln(text+"<BR>")
- }
-
- function write(text)
- {
- output.writeln(text+"<BR>")
- }
-
- function debug(text)
- {
- // Comment out to remove debug messages
- write(text)
- }
-
- function base64(content)
- {
- content=string(content)
- if (content.slice(0,1)==":")
- {
- content = content.slice(1,content.length)
- content = content.trim()
- content = content.base64decode()
- }
-
- return content
- }
-
- function oldDomain(name)
- {
- var old=true
- var count = 0
-
- for (count=0; count<domainCount; count++)
- {
- if (newDomain[count]==name)
- {
- old=false
- break
- }
- }
-
- return old
- }
-
- function newDomain(name, linenumber)
- {
- var ok = true
-
- var d = new server.domain
- d.newlocaldomain(domain)
- if (!d.domainname)
- {
- error("Create domain '"+domain+"' failed", lineNumber)
- ok=false
- }
- else
- {
- write("Created local domain "+domain)
- newDomain[domainCount]=domain
- domainCount++
- }
-
- return ok
- }
-
- function processDN(content, lineNumber)
- {
- var o
- content=base64(content)
- content=string(content)
-
- var pos=content.indexof("=",0)
- while (pos!=-1)
- {
- var attrib=content.slice(0, pos)
- content=content.slice(pos+1,content.length)
-
- attrib=attrib.trim()
- content=content.trim()
- if (content.left(1)=="\"")
- {
- pos=0
- content=content.slice(1,content.length)
- while ((pos<content.length)&&(content.slice(pos, pos+1)!="\""))
- {
- pos++
- if (content.slice(pos, pos+1)=="\\")
- pos=pos+2
- }
-
- if (content.slice(pos, pos+1)=="\"")
- content = content.slice(0, pos)+content.slice(pos+1, content.length)
-
- pos=content.indexof(",", number(pos))
- }
- else
- pos=content.indexof(",", 0)
-
- if (pos==-1)
- pos=content.length
-
- if (attrib=="dc")
- {
- if (domain!="")
- domain=domain+"."
-
- domain=domain+content.slice(0, pos)
- }
- else if (attrib=="o")
- {
- o=content.slice(0, pos)
- }
- else if (attrib=="ou")
- {
- ou=content.slice(0, pos)
- }
- else if (attrib=="uid")
- {
- uid=content.slice(0, pos)
- }
- else if (attrib=="cn")
- {
- cn=content.slice(0, pos)
- }
-
- content=content.slice(pos+1, content.length)
- pos=content.indexof("=",0)
- }
-
- if (domain=="")
- domain=o
-
- return true
- }
-
- function extractDomain(mail)
- {
- mail=string(mail)
- var index=mail.indexof("@", 0)
- return mail.slice(index+1, 999)
- }
-
- function processRecord(lineNumber)
- {
- output.writeln("<FONT COLOR=\"red\">")
-
- if (person|alias)
- {
- while (true)
- {
- // See if can work out the domain
- //
- if ((domain=="")||(domain==0))
- domain=extractDomain(mail)
-
- if ((domain=="")||(domain==0))
- {
- error("no domain for record", linenumber)
- break
- }
-
- domain=domain.tolowercase()
-
- var d = new server.domain
- d.domainname=domain
- if (d.domainname==domain)
- {
- // the domain already exists
- // Make sure it's a local domain
- //
- d.domainname=d.domainname // dereference if it's an alias
- if (d.description!="Local")
- {
- error("domain '"+d.domainname+"' is not Local", lineNumber)
- break
- }
-
- // if we haven't already purged this domain then do so
- //
- if (oldDomain(domain))
- {
- d.deletedomain(domain)
- if (!newDomain(domain, linenumber))
- break
- }
- }
- else if (!newDomain(domain, linenumber))
- break
-
- d.domainname=domain
- mail=mail.tolowercase()
- uid=uid.tolowercase()
-
- if ((mail!="")||(uid!=""))
- {
- // get the mailbox
- //
- if (mail!="")
- mail = mail.slice(0,mail.indexof("@",0))
- else
- mail=uid
-
- var m = new d.mailbox
-
- if (cn=="")
- cn=displayName
-
- m.name=mail
- if (m.name!=mail)
- {
- write("Created mailbox "+mail+"@"+domain+" for "+cn)
- d.newusermbx(mail)
- m.name=mail
- }
-
- if (m.name==mail)
- {
- m.ldap.commonname=cn
- m.ldap.givenname=gn
- m.ldap.lastname=sn
- m.ldap.department=ou
- m.description=description
- if (title!="")
- m.ldap.jobtitle=title
- m.ldap.phone=phone
- m.ldap.fax=fax
-
- if (password!="")
- m.password(password)
-
- if (alias)
- {
- if (aliasMail!="")
- aliasMail= aliasMail.left(aliasMail.indexof("@",0))
- else
- aliasMail=aliasUid
-
- if (aliasMail!="")
- {
- m.newalias(aliasMail, m.name)
- write("Created alias "+aliasMail+"@"+domain+" for "+mail+"@"+domain)
- }
- }
- }
- else
- error("Cannot create "+mail+"@"+domain, lineNumber)
- }
-
- break
- }
- }
-
- person=false
- alias=false
-
- aliasMail=""
- aliasUid=""
-
- domain=""
- mail=""
- uid=""
-
- cn=""
- displayName=""
- sn=""
- gn=""
- description=""
-
- output.writeln("</FONT>")
- }
-
- function parseAttribute(record, content, lineNumber)
- {
- var isDefaultLang = false
-
- record=string(record)
- var pos = record.indexof(";",0)
-
- if (pos!=-1)
- {
- var options = record.slice(pos+1, record.length)
- record = record.slice(0, pos)
-
- if (options.indexof(defaultLang)!=-1)
- isDefaultLang=true
- }
-
- output.writeln("<FONT COLOR=\"green\">")
- debug("record="+record)
- output.writeln("</FONT>")
-
- if (record=="cn")
- {
- content=base64(content)
- if ((cn=="")||(isDefaultLang))
- cn=content
- }
- else if ((record=="gn")||(record=="givenname"))
- {
- debug("gn")
- content=base64(content)
- if ((gn=="")||(isDefaultLang))
- gn=content
- }
- else if (record=="sn")
- {
- content=base64(content)
- if ((sn=="")||(isDefaultLang))
- sn=content
- }
- else if (record=="title")
- {
- debug("in title="+content)
- content=base64(content)
- if ((title=="")||(isDefaultLang))
- {
- debug("set title="+content)
- title=content
- }
- }
- else if (record=="ou")
- {
- content=base64(content)
- if ((ou=="")||(isDefaultLang))
- ou=content
- }
- else if ((record=="telephonenumber")||(record=="phone"))
- {
- content=base64(content)
- if (phone=="")
- phone=content
- }
- else if ((record=="facsimiletelephonenumber")||(record=="fax"))
- {
- content=base64(content)
- if (fax=="")
- fax=content
- }
- else if (record=="userpassword")
- {
- content=base64(content)
- if (password=="")
- password=content
- }
- else if (record=="description")
- {
- debug("description")
- content=base64(content)
- if ((description=="")||(isDefaultLang))
- description=content
- }
- else if (record=="displayname")
- {
- content=base64(content)
- if ((displayname=="")||(isDefaultLang))
- displayname=content
- }
- else if ((record=="mail")||(record=="rfc822mailbox"))
- {
- content=base64(content)
- if (alias)
- aliasMail=content
- else
- mail=content
- }
- else if ((record=="uid")||(record=="userid"))
- {
- content=base64(content)
- if (alias)
- aliasUid=content
- else
- uid=content
- }
- else if (record=="aliasedobjectname")
- {
- alias=true
- aliasUid=uid
- aliasMail=mail
- domain=""
- processDN(content, lineNumber)
- }
- else if (record=="objectclass")
- {
- content=base64(content)
- content = string(content)
- content = content.tolowercase()
- if (content=="person")
- person=true
- }
- }
-
- function locateChange(linenumber)
- {
- changeDomain=domain
- changeMbx=""
-
- while (true)
- {
- if (changeDomain=="")
- changeDomain=extractDomain(mail)
-
- if (changeDomain=="")
- {
- error("no domain for record", linenumber)
- break
- }
- else
- {
- changeDomain=changeDomain.tolowercase()
-
- var d = new server.domain
- d.domainname=changeDomain
- if (d.domainname!=changeDomain)
- {
- error("Cannot locate domain '"+changeDomain+"'", linenumber)
- break
- }
-
- // the domain exists
- d.domainname=d.domainname // dereference if it's an alias
- if (d.description!="Local")
- {
- error("domain '"+d.domainname+"' is not Local", lineNumber)
- break
- }
-
- if (cn=="")
- cn=displayName
-
- mail=mail.tolowercase()
- uid=uid.tolowercase()
-
- if ((mail!="")||(uid!=""))
- {
- // get the mailbox
- //
- if (mail!="")
- mail = mail.slice(0,mail.indexof("@",0))
- else
- mail=uid
-
- var m = new d.mailbox
-
- m.name=mail
-
- if (m.name!=mail)
- {
- error("Cannot locate mailbox "+m.name+"@"+domain, linenumber)
- break
- }
- else
- changeMbx = mail
- }
- else if (cn!=""||ou!="")
- {
- var m = new d.mailbox
-
- var ok = m.findfirst("*")
- while (ok)
- {
- if ((cn!="")&&(m.ldap.commonname==cn))
- {
- if ((ou!="")&&(m.ldap.department==ou))
- break
- else if (ou=="")
- break
- }
- else if ((cn=="")&&(ou!="")&&(m.ldap.department==ou))
- break
-
- ok = m.findnext()
- }
-
- if (!ok)
- {
- error("Cannot locate mailbox for "+cn, linenumber)
- break
- }
- else
- changeMbx=m.name
- }
- }
-
- if (changeMbx)
- {
- var d = new server.domain
- d.domainname=changeDomain
- var m = new d.mailbox
- m.name=changeMbx
- cn = m.ldap.commonname
- gn = m.ldap.givenname
- sn = m.ldap.lastname
- ou = m.ldap.department
- description = m.description
- title = m.ldap.jobtitle
- phone = m.ldap.phone
- fax = m.ldap.fax
- }
-
- break
- }
- }
-
- function parseChange(action, content, lineNumber)
- {
- if (changetype=="add")
- {
- parseAttribute(action, content, lineNumber)
- }
- else if (changetype=="modify")
- {
- debug("modify changeAction="+changeAction)
- if (changeAction=="")
- {
- debug("action="+action+" content="+content)
- changeAction=action
- changeAttribute=content
-
- if ((action=="replace")||(action=="delete"))
- {
- debug("replace or delete")
- replaceFirst=true
-
- if (content=="cn")
- cn=""
- else if ((content=="gn")||(content=="givenname"))
- gn=""
- else if (content=="sn")
- sn=""
- else if (content=="title")
- title=""
- else if (content=="ou")
- ou=""
- else if ((content=="telephonenumber")||(content=="phone"))
- {
- phone=""
- debug("yphone="+phone)
- }
- else if ((content=="facsimiletelephonenumber")||(content=="fax"))
- fax=""
- else if (content=="displayname")
- displayname=""
- else if ((content=="mail")||(content=="rfc822mailbox"))
- mail=""
- else if ((content=="uid")||(content=="userid"))
- uid=""
- }
- else
- debug("modify")
- }
- else
- {
- debug("changeAction="+changeAction+" replaceFirst="+replaceFirst)
- if ((changeAction=="replace")&&(replaceFirst))
- replaceFirst=false
- else
- {
- parseAttribute(action, content, lineNumber)
- changeAction=""
- }
- }
- }
- else if (changetype=="delete")
- {
- }
- }
-
- function processChange(lineNumber)
- {
- output.writeln("<FONT COLOR=\"red\">")
-
- if (changetype=="add")
- {
- processRecord(lineNumber)
- }
- else if (changetype=="modify")
- {
- while (true)
- {
- var d = new server.domain
- d.domainname=changeDomain
- if (!d.domainname)
- break
-
- var m=new d.mailbox
- m.name=changeMbx
- if (!m.name)
- break
-
- m.ldap.commonname=cn
- m.ldap.givenname=gn
- m.ldap.lastname=sn
- m.ldap.department=ou
- m.description=description
- m.ldap.jobtitle=title
- m.ldap.phone=phone
- m.ldap.fax=fax
-
- if (password!="")
- m.password(password)
-
- mail=mail.tolowercase()
- uid=uid.tolowercase()
-
- if ((mail!="")||(uid!=""))
- {
- // get the mailbox
- //
- if (mail!="")
- mail = mail.slice(0,mail.indexof("@",0))
- else
- mail=uid
-
- if (mail!=m.name)
- m.rename(mail)
- }
-
- break
- }
- }
- else if ((changetype=="delete")&&(changeDomain!="")&&(changeMbx!=""))
- {
- debug("deleting")
- while (true)
- {
- var d = new server.domain
- d.domainname=changeDomain
- if (!d.domainname)
- break
-
- debug("deleting "+changeMbx+"@"+d.domainname)
- d.deletemailbox(changeMbx)
- break
- }
- }
-
- changeType=""
- changeAction=""
- changeDomain=""
- changeMbx=""
-
- aliasMail=""
- aliasUid=""
-
- domain=""
- mail=""
- uid=""
-
- cn=""
- displayName=""
- sn=""
- gn=""
- description=""
-
- output.writeln("</FONT>")
- }
-
- function importLDIF(filename)
- {
- var file = new server.file
-
- if (file.open(filename))
- {
- var state = STATE_VERSION
- var ok = file.first()
- var lineNumber=1
- var recordLine=1
- var debug=false
-
- while (ok)
- {
- var line = string(file.text)
- line=line.trim()
- while ((ok)&&((line.slice(0,1)=="#")||(line=="")))
- {
- ok=file.next()
- line = string(file.text)
- line=line.trim()
- }
-
- var next
- var newLine
-
- ok = file.next()
- if (ok)
- {
- next = string(file.text)
-
- while ((ok)&&(next.left(1)==" "))
- {
- next=next.trim()
- line = line+next
- ok = file.next()
- next = string(file.text)
- newLine++
- }
- }
- next=next.trim()
-
- var sep = line.indexof(":",0)
- if (sep==-1)
- sep = line.indexof("=",0)
-
- if (sep!=-1)
- {
- var attribute = line.left(sep)
- attribute = attribute.tolowercase()
-
- var content = line.slice(sep+1, line.length)
- content=content.trim()
-
- if (content.slice(0,1)==":")
- {
- content=content.trim()
-
- while ((ok)&&(next.indexof(":",0)==-1))
- {
- next=next.trim()
- content = content+next
- ok = file.next()
- next = string(file.text)
- newLine++
- }
- }
-
- debug("state="+state+" content="+content)
- if (state==STATE_VERSION)
- {
- if ((attribute=="version")&&(content!=1))
- {
- error("Incompatible file version", lineNumber)
- break
- }
- else if (attribute=="dn")
- {
- recordLine=lineNumber
- ok=processDN(content, lineNumber)
- state=STATE_ATTRVAL_CHANGE
- }
- }
- else if (state==STATE_DN)
- {
- recordLine=lineNumber
- ok=processDN(content, lineNumber)
- state=STATE_ATTRVAL_CHANGE
- }
- else if (state==STATE_ATTRVAL_CHANGE)
- {
- if (attribute=="control")
- {
- }
- else if (attribute=="changetype")
- {
- state=STATE_CHANGE
- changetype=content
- if (ok==false)
- {
- locateChange(recordLine)
- processChange(recordLine)
- }
- }
- else
- {
- state=STATE_ATTRVAL
- if (attribute=="dn")
- {
- processRecord(content, recordLine)
- if (!processDN(content, lineNumber))
- ok=false
-
- recordLine=lineNumber
- }
- else if (!parseAttribute(attribute, content, lineNumber))
- ok=false
- }
- }
- else if (state==STATE_ATTRVAL)
- {
- if (attribute=="dn")
- {
- processRecord(recordLine)
- if (!processDN(content, lineNumber))
- ok=false
-
- recordLine=lineNumber
- }
- else
- {
- parseAttribute(attribute, content, lineNumber)
- if (ok==false)
- processRecord(recordLine)
- }
- }
- else if (state==STATE_CHANGE)
- {
- debug("attrib="+attribute)
- if (attribute=="dn")
- {
- processChange(content, recordLine)
- if (!processDN(content, lineNumber))
- ok=false
-
- state=STATE_CHANGE_CONTROL
- recordLine=lineNumber
- }
- else if (attribute=="-")
- {
- changeAction=""
- if (ok==false)
- processChange(recordLine)
- }
- else
- {
- parseChange(attribute, content, lineNumber)
- if (ok==false)
- processChange(recordLine)
- }
- }
- else if (state==STATE_CHANGE_CONTROL)
- {
- if (attribute=="control")
- {
- }
- else if (attribute=="changetype")
- {
- state=STATE_CHANGE
- changetype=content
-
- if (content!="add")
- {
- var ok2=ok
- locateChange(recordLine)
- ok=ok2
- debug("Ok4="+ok)
- }
- }
- }
- else
- {
- output.write("How did I get here?")
- break
- }
-
- line = next
- lineNumber = newLine
- }
- else if ((state==STATE_CHANGE)&&(line.slice(0,1)=="-"))
- {
- }
- else
- ok=false
-
- if (line=="-")
- debug=true
- }
-
- file.close()
- }
- else
- error("Failed to open file", 0-1)
- }
-
- importLDIF(form.file)
- }
- else
- {
- %>
- <FORM NAME="ftgate" METHOD="POST">
- <INPUT TYPE="HIDDEN" NAME="config" VALUE="1">
- <TABLE BORDER="0">
- <TR>
- <TD VALIGN="TOP"> Enter the name of the ldif file you wish to import then
- click the Import button</TD>
- </TR>
- <TR>
- <TD VALIGN="TOP"> File to import: <INPUT TYPE="FILE" SIZE="40" NAME="file"></TD>
- </TR>
- <TR>
- <TD ALIGN="RIGHT"><INPUT TYPE="SUBMIT" VALUE=" Import "></TD>
- </TR>
- </TABLE>
- <%
- }
- %>