September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,153 +1,38 @@
|
|||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.directory.api.ldap.model.cursor.CursorException;
|
||||
import org.apache.directory.api.ldap.model.cursor.EntryCursor;
|
||||
import org.apache.directory.api.ldap.model.entry.Entry;
|
||||
import org.apache.directory.api.ldap.model.exception.LdapException;
|
||||
import org.apache.directory.api.ldap.model.message.SearchScope;
|
||||
import org.apache.directory.ldap.client.api.LdapConnection;
|
||||
import org.apache.directory.ldap.client.api.LdapNetworkConnection;
|
||||
import org.apache.directory.shared.ldap.model.cursor.EntryCursor;
|
||||
import org.apache.directory.shared.ldap.model.entry.Entry;
|
||||
import org.apache.directory.shared.ldap.model.exception.LdapException;
|
||||
import org.apache.directory.shared.ldap.model.message.SearchScope;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class LdapSearchDemo {
|
||||
|
||||
public class RDirectorySearchLDAP {
|
||||
|
||||
protected static final Logger log_;
|
||||
private static LdapConnection connection;
|
||||
private static final String ldapHostName;
|
||||
private static final int ldapPort;
|
||||
private static final String ldapDnStr;
|
||||
private static final String ldapCreds;
|
||||
|
||||
static {
|
||||
|
||||
log_ = LoggerFactory.getLogger(RDirectorySearchLDAP.class);
|
||||
|
||||
ldapHostName = "localhost";
|
||||
ldapPort = 11389;
|
||||
ldapDnStr="uid=admin,ou=system";
|
||||
ldapCreds="********";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
boolean connected = false;
|
||||
|
||||
try {
|
||||
connected = setUp();
|
||||
if (connected) {
|
||||
search("*mil*");
|
||||
}
|
||||
}
|
||||
finally {
|
||||
if (connected) {
|
||||
tearDown();
|
||||
}
|
||||
public static void main(String[] args) throws IOException, LdapException, CursorException {
|
||||
new LdapSearchDemo().demonstrateSearch();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
private static boolean search(String uid) {
|
||||
|
||||
boolean state;
|
||||
EntryCursor cursor;
|
||||
Entry ev;
|
||||
String baseDn;
|
||||
String filter;
|
||||
SearchScope scope;
|
||||
String attributes[];
|
||||
int ksearch = 0;
|
||||
|
||||
state = true;
|
||||
|
||||
baseDn = "ou=users,o=mojo";
|
||||
filter = "(&(objectClass=person)(&(uid=" + uid + ")))";
|
||||
scope = SearchScope.SUBTREE;
|
||||
attributes = new java.lang.String[] { "dn", "cn", "sn", "uid" };
|
||||
|
||||
try {
|
||||
if (log_.isTraceEnabled()) { log_.trace("LDAP search"); }
|
||||
if (log_.isInfoEnabled()) {
|
||||
log_.info("Begin search");
|
||||
log_.info(" search base distinguished name: " + baseDn);
|
||||
log_.info(" search filter: " + filter);
|
||||
log_.info(" search attributes: " + (Arrays.asList(attributes).toString()));
|
||||
}
|
||||
cursor = connection.search(baseDn, filter, scope, attributes);
|
||||
while (cursor.next()) {
|
||||
ksearch++;
|
||||
ev = cursor.get();
|
||||
if (log_.isInfoEnabled()) { log_.info("Search cursor entry count: " + ksearch); }
|
||||
if (log_.isInfoEnabled()) { log_.info(ev.toString()); }
|
||||
}
|
||||
}
|
||||
catch (LdapException lex) {
|
||||
state = false;
|
||||
log_.error("LDAP Error in cursor loop: Iteration " + ksearch, lex);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
state = false;
|
||||
log_.error("I/O Error in cursor loop: Iteration " + ksearch, ex);
|
||||
private void demonstrateSearch() throws IOException, LdapException, CursorException {
|
||||
try (LdapConnection conn = new LdapNetworkConnection("localhost", 11389)) {
|
||||
conn.bind("uid=admin,ou=system", "********");
|
||||
search(conn, "*mil*");
|
||||
conn.unBind();
|
||||
}
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
private void search(LdapConnection connection, String uid) throws LdapException, CursorException {
|
||||
String baseDn = "ou=users,o=mojo";
|
||||
String filter = "(&(objectClass=person)(&(uid=" + uid + ")))";
|
||||
SearchScope scope = SearchScope.SUBTREE;
|
||||
String[] attributes = {"dn", "cn", "sn", "uid"};
|
||||
int ksearch = 0;
|
||||
|
||||
private static boolean search() {
|
||||
|
||||
return search("*");
|
||||
}
|
||||
|
||||
private static boolean setUp() {
|
||||
|
||||
boolean state = false;
|
||||
|
||||
try {
|
||||
if (log_.isInfoEnabled()) { log_.info("LDAP Connection to " + ldapHostName + " on port " + ldapPort); }
|
||||
connection = new LdapNetworkConnection(ldapHostName, ldapPort);
|
||||
|
||||
if (log_.isTraceEnabled()) { log_.trace("LDAP bind"); }
|
||||
connection.bind(ldapDnStr, ldapCreds);
|
||||
|
||||
state = true;
|
||||
EntryCursor cursor = connection.search(baseDn, filter, scope, attributes);
|
||||
while (cursor.next()) {
|
||||
ksearch++;
|
||||
Entry entry = cursor.get();
|
||||
System.out.printf("Search entry %d = %s%n", ksearch, entry);
|
||||
}
|
||||
}
|
||||
catch (LdapException lex) {
|
||||
state = false;
|
||||
log_.error("LDAP Error", lex);
|
||||
}
|
||||
catch (IOException iox) {
|
||||
state = false;
|
||||
log_.error("I/O Error", iox);
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
private static boolean tearDown() {
|
||||
|
||||
boolean state = false;
|
||||
|
||||
try {
|
||||
if (log_.isTraceEnabled()) { log_.trace("LDAP unbind"); }
|
||||
connection.unBind();
|
||||
state = true;
|
||||
}
|
||||
catch (LdapException lex) {
|
||||
state = false;
|
||||
log_.error("LDAP Error", lex);
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
connection.close();
|
||||
}
|
||||
catch (IOException iox) {
|
||||
state = false;
|
||||
log_.error("I/O Error on connection.close()", iox);
|
||||
}
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,60 @@
|
|||
/* Rexx */
|
||||
do
|
||||
LDAP_URL = 'ldap://localhost:11389'
|
||||
LDAP_DN_STR = 'uid=admin,ou=system'
|
||||
LDAP_CREDS = '********'
|
||||
LDAP_BASE_DN = 'ou=users,o=mojo'
|
||||
LDAP_SCOPE = 'sub'
|
||||
LDAP_FILTER = '"(&(objectClass=person)(&(uid=*mil*)))"'
|
||||
LDAP_ATTRIBUTES = '"dn" "cn" "sn" "uid"'
|
||||
|
||||
ldapCommand = ,
|
||||
'ldapsearch' ,
|
||||
'-s base' ,
|
||||
'-H' LDAP_URL ,
|
||||
'-LLL' ,
|
||||
'-x' ,
|
||||
'-v' ,
|
||||
'-s' LDAP_SCOPE ,
|
||||
'-D' LDAP_DN_STR ,
|
||||
'-w' LDAP_CREDS ,
|
||||
'-b' LDAP_BASE_DN ,
|
||||
LDAP_FILTER ,
|
||||
LDAP_ATTRIBUTES ,
|
||||
'2>&1' ,
|
||||
'|' ,
|
||||
'rxqueue' ,
|
||||
''
|
||||
|
||||
address command,
|
||||
ldapCommand
|
||||
|
||||
ldapResult. = ''
|
||||
loop ln = 1 to queued()
|
||||
parse pull line
|
||||
ldapResult.0 = ln
|
||||
ldapResult.ln = line
|
||||
end ln
|
||||
|
||||
loop ln = 1 to ldapResult.0
|
||||
parse var ldapResult.ln 'dn:' dn_ ,
|
||||
0 'uid:' uid_ ,
|
||||
0 'sn:' sn_ ,
|
||||
0 'cn:' cn_
|
||||
select
|
||||
when length(strip(dn_, 'b')) > 0 then dn = dn_
|
||||
when length(strip(uid_, 'b')) > 0 then uid = uid_
|
||||
when length(strip(sn_, 'b')) > 0 then sn = sn_
|
||||
when length(strip(cn_, 'b')) > 0 then cn = cn_
|
||||
otherwise nop
|
||||
end
|
||||
end ln
|
||||
|
||||
say 'Distiguished Name:' dn
|
||||
say ' Common Name:' cn
|
||||
say ' Surname:' sn
|
||||
say ' userID:' uid
|
||||
|
||||
return
|
||||
end
|
||||
exit
|
||||
Loading…
Add table
Add a link
Reference in a new issue