-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathLDAP.java
More file actions
214 lines (194 loc) · 6.68 KB
/
Copy pathLDAP.java
File metadata and controls
214 lines (194 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package javaforce;
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
/** LDAP client functions.
*
* @author pquiring
*/
public class LDAP {
private static boolean debug = false;
private DirContext ctx;
public Exception lastException;
/*
LDAP Data Interchange Format
see https://en.wikipedia.org/wiki/LDAP_Data_Interchange_Format
dn = distinguished name (comma list of other attributes)
dc = domain component
ou = organizational unit
cn = common name
givenName = first name
sn = surname
mail = email address
telephoneNumber = full phone number
manager : manager in dn format
*/
/** Logins into a LDAP Server.
*
* @param server = LDAP server (FQN)
* @param domain = domain (ie: example)
* @param username = username
* @param password = password
* @return login successful
*/
public boolean login(String server, String domain, String username, String password) {
try {
if (username.length() == 0) throw new Exception("invalid username");
if (password.length() == 0) throw new Exception("invalid password");
// Set up the environment for creating the initial context
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://" + server + ":389");
// Authenticate as user and password
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, domain + "\\" + username);
env.put(Context.SECURITY_CREDENTIALS, password);
// Create the initial context
ctx = new InitialDirContext(env);
return true;
} catch (Exception e) {
lastException = e;
System.out.println(e);
return false;
}
}
public void close() {
if (ctx != null) {
try {ctx.close();} catch (Exception e) {}
ctx = null;
}
}
/** Returns user object attributes.
* Must login() first
*
* @param domain_dn = domain as dn (distinguished name) (ie: DC=example,DC=com)
* @param key = search key (ie: SAMAccountName=bob)
* @param attrs = list of attributes to read (ie: member)
* @return attributes in order of attrs
*/
public String[] getAttributes(String domain_dn, String key, String[] attrs) {
if (ctx == null) {
JFLog.log("Please login first!");
return null;
}
String[] values = new String[attrs.length];
try {
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); //resursive
constraints.setReturningAttributes(attrs);
NamingEnumeration answer = ctx.search(domain_dn, key, constraints);
if (!answer.hasMore()) {
throw new Exception("no results");
}
Attributes results = ((SearchResult) answer.next()).getAttributes();
if (debug) JFLog.log("results=" + results);
for(int a=0;a<attrs.length;a++) {
Object value = results.get(attrs[a]);
if (value == null) value = "null";
values[a] = value.toString();
}
return values;
} catch (Exception e) {
lastException = e;
JFLog.log(e);
return null;
}
}
/** Queries user to determine if they are a member of a group.
* Must login() first.
*
* @param domain_dn = domain as dn (distinguished name) (ie: DC=example,DC=com)
* @param username = user to check for (for computer use COMPUTERNAME plus $)
* @param group = group name
* @return user is a member
*/
public boolean memberOf(String domain_dn, String username, String group) {
String[] list = getAttributes(domain_dn, "SAMAccountName=" + username, new String[] {"memberOf"});
if (list == null || list.length == 0) return false;
String[] groups = list[0].replaceAll("\\\\,", "_").split("[,] ");
for(String grp : groups) {
int i1 = grp.indexOf('=');
int i2 = grp.indexOf(',');
if (i1 == -1 || i2 == -1) continue;
grp = grp.substring(i1 + 1, i2);
if (grp.equalsIgnoreCase(group)) return true;
}
return false;
}
/** Create distinguished name from dot domain name. */
public static String build_dn(String domain) {
StringBuilder dn = new StringBuilder();
String[] p = domain.split("[.]");
for(int a=0;a<p.length;a++) {
if (a > 0) dn.append(",");
dn.append("dc=");
dn.append(p[a]);
}
return dn.toString();
}
public Exception getLastException() {
return lastException;
}
private static void usage() {
System.out.println("Usage:ldap {command} [args]");
System.out.println(" login server domain username password");
System.out.println(" attrs server domain username password dname key attrs...");
System.out.println(" memberof server domain username password dname {username|computername} group");
System.out.println(" ERRORLEVEL==1 if true");
System.out.println(" Where:");
System.out.println(" server = LDAP server (Windows AD Server)");
System.out.println(" domain = short domain name (example)");
System.out.println(" dname = long domain name in distinguished name format (dc=example,dc=com)");
System.out.println(" computername = should end with $ ( %COMPUTERNAME%$ )");
System.exit(1);
}
public static void main(String[] args) {
if (args.length < 2) {
usage();
}
LDAP ldap = new LDAP();
try {
switch (args[0]) {
case "login": {
boolean ok = ldap.login(args[1], args[2], args[3], args[4]);
System.out.println("login = " + ok);
break;
}
case "attrs": {
boolean ok = ldap.login(args[1], args[2], args[3], args[4]);
if (!ok) {
System.out.println("login failed");
return;
}
int attrCount = args.length - 7;
String[] attrs = new String[attrCount];
for(int a=0;a<attrCount;a++) {
attrs[a] = args[a + 7];
}
String[] list = ldap.getAttributes(args[5], args[6], attrs);
if (list == null || list.length == 0) {
System.out.println("no attrs returned");
} else {
for(String attr : list) {
System.out.println("attr[]=" + attr);
}
}
break;
}
case "memberof": {
boolean ok = ldap.login(args[1], args[2], args[3], args[4]);
if (!ok) {
System.out.println("login failed");
return;
}
boolean isMember = ldap.memberOf(args[5], args[6], args[7]);
System.out.println("memberOf = " + isMember);
System.exit(isMember ? 1 : 0);
break;
}
}
} catch (Exception e) {
JFLog.log(e);
}
}
}