1
2
3
4
5
6
7 package gov.bnl.gums.account;
8
9 import gov.bnl.gums.configuration.Configuration;
10
11 import java.util.Properties;
12
13 import javax.naming.Context;
14 import javax.naming.NamingEnumeration;
15 import javax.naming.directory.Attribute;
16 import javax.naming.directory.Attributes;
17 import javax.naming.directory.DirContext;
18 import javax.naming.directory.InitialDirContext;
19 import javax.naming.directory.SearchResult;
20
21 import org.apache.log4j.Logger;
22
23
24
25
26
27
28 public class GecosLdapAccountMapper extends GecosAccountMapper {
29 static private Logger log = Logger.getLogger(GecosLdapAccountMapper.class);
30
31 static public String getTypeStatic() {
32 return "gecosLdap";
33 }
34
35 private String jndiLdapUrl = "";
36 private String gecosField = "gecos";
37 private String accountField = "uid";
38 private String peopleObject = "ou=People";
39
40 public GecosLdapAccountMapper() {
41 super();
42 }
43
44 public GecosLdapAccountMapper(Configuration configuration) {
45 super(configuration);
46 }
47
48 public GecosLdapAccountMapper(Configuration configuration, String name) {
49 super(configuration, name);
50 }
51
52 public AccountMapper clone(Configuration configuration) {
53 GecosLdapAccountMapper accountMapper = new GecosLdapAccountMapper(configuration, new String(getName()));
54 accountMapper.setDescription(new String(getDescription()));
55 accountMapper.setJndiLdapUrl(new String(jndiLdapUrl));
56 accountMapper.setGecosField(new String(gecosField));
57 accountMapper.setAccountField(new String(accountField));
58 accountMapper.setPeopleObject(new String(peopleObject));
59 return accountMapper;
60 }
61
62 public String getAccountField() {
63 return accountField;
64 }
65
66 public String getGecosField() {
67 return gecosField;
68 }
69
70 public String getJndiLdapUrl() {
71 return jndiLdapUrl;
72 }
73
74 public String getPeopleObject() {
75 return peopleObject;
76 }
77
78 public String getType() {
79 return "gecosLdap";
80 }
81
82 public void setAccountField(String accountField) {
83 this.accountField = accountField;
84 }
85
86 public void setGecosField(String gecosField) {
87 this.gecosField = gecosField;
88 }
89
90 public void setJndiLdapUrl(String jndiLdapUrl) {
91 this.jndiLdapUrl = jndiLdapUrl;
92 }
93
94 public void setPeopleObject(String peopleObject) {
95 this.peopleObject = peopleObject;
96 }
97
98 public String toString(String bgColor) {
99 return "<td bgcolor=\""+bgColor+"\"><a href=\"accountMappers.jsp?command=edit&name=" + getName() + "\">" + getName() + "</a></td><td bgcolor=\""+bgColor+"\">" + getType() + "</td><td bgcolor=\""+bgColor+"\"> </td>";
100 }
101
102 public String toXML() {
103 return "\t\t<gecosLdapAccountMapper\n"+
104 "\t\t\tname='"+getName()+"'\n"+
105 "\t\t\tdescription='"+getDescription()+"'\n"+
106 "\t\t\tjndiLdapUrl='"+jndiLdapUrl+"'\n"+
107 "\t\t\tgecosField='"+gecosField+"'\n"+
108 "\t\t\taccountField='"+accountField+"'\n"+
109 "\t\t\tpeopleObject='"+peopleObject+"'/>\n\n";
110 }
111
112 private Properties retrieveJndiProperties() {
113 Properties jndiProperties = new java.util.Properties();
114 jndiProperties.put("java.naming.provider.url", jndiLdapUrl);
115 jndiProperties.put("java.naming.factory.initial","com.sun.jndi.ldap.LdapCtxFactory");
116 jndiProperties.put(Context.SECURITY_PROTOCOL, "none");
117 return jndiProperties;
118 }
119
120 protected GecosMap createMap() {
121 Properties jndiProperties = retrieveJndiProperties();
122 int nTries = 5;
123 Exception lastException = null;
124 int i = 0;
125 for (; i < nTries; i++) {
126 GecosMap map = new GecosMap();
127 log.debug("Attempt " + i + " to retrieve map for '" + jndiLdapUrl + "'");
128 try {
129 DirContext jndiCtx = new InitialDirContext(jndiProperties);
130 NamingEnumeration nisMap = jndiCtx.search(peopleObject, "("+accountField+"=*)", null);
131 log.trace("Server responded");
132 while (nisMap.hasMore()) {
133 SearchResult res = (SearchResult) nisMap.next();
134 Attributes atts = res.getAttributes();
135 String account = (String) atts.get(accountField).get();
136 Attribute gecosAtt = atts.get(gecosField);
137 if (gecosAtt != null) {
138 String gecos = gecosAtt.get().toString();
139 map.addEntry(account, gecos);
140 } else {
141 log.trace("Found user '" + account + "' with no GECOS field");
142 }
143 }
144 jndiCtx.close();
145 return map;
146 } catch (javax.naming.NamingException ne) {
147 log.warn("Error filling the maps for NIS "+jndiLdapUrl, ne);
148 lastException = ne;
149 try {
150 Thread.sleep(100);
151 } catch (InterruptedException e) {
152 log.warn("Interrupted", e);
153 }
154 } catch (Exception e) {
155 log.warn("Error filling the maps for NIS "+jndiLdapUrl, e);
156 lastException = e;
157 try {
158 Thread.sleep(100);
159 } catch (InterruptedException ie) {
160 log.warn("Interrupted", e);
161 }
162 }
163 }
164 if (i == nTries) {
165 throw new RuntimeException("Couldn't retrieve NIS maps from " + jndiLdapUrl, lastException);
166 }
167 return null;
168 }
169
170 protected String getMapName() {
171 return jndiLdapUrl;
172 }
173 }