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.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24
25
26
27
28
29 public class GecosLdapAccountMapper extends GecosAccountMapper {
30 static private Log log = LogFactory.getLog(GecosLdapAccountMapper.class);
31
32 static public String getTypeStatic() {
33 return "gecosLdap";
34 }
35
36 private String jndiLdapUrl = "";
37 private String gecosField = "gecos";
38 private String accountField = "uid";
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 return accountMapper;
59 }
60
61 public String getAccountField() {
62 return accountField;
63 }
64
65 public String getGecosField() {
66 return gecosField;
67 }
68
69 public String getJndiLdapUrl() {
70 return jndiLdapUrl;
71 }
72
73 public String getType() {
74 return "gecosLdap";
75 }
76
77 public void setAccountField(String accountField) {
78 this.accountField = accountField;
79 }
80
81 public void setGecosField(String gecosField) {
82 this.gecosField = gecosField;
83 }
84
85 public void setJndiLdapUrl(String jndiLdapUrl) {
86 this.jndiLdapUrl = jndiLdapUrl;
87 }
88
89 public String toString(String bgColor) {
90 return "<td bgcolor=\""+bgColor+"\"><a href=\"accountMappers.jsp?command=edit&name=" + getName() + "\">" + getName() + "</a></td><td bgcolor=\""+bgColor+"\">" + getType() + "</td><td bgcolor=\""+bgColor+"\"> </td>";
91 }
92
93 public String toXML() {
94 return "\t\t<gecosLdapAccountMapper\n"+
95 "\t\t\tname='"+getName()+"'\n"+
96 "\t\t\tdescription='"+getDescription()+"'\n"+
97 "\t\t\tjndiLdapUrl='"+jndiLdapUrl+"'\n"+
98 "\t\t\tgecosField='"+gecosField+"'\n"+
99 "\t\t\taccountField='"+accountField+"'/>\n\n";
100
101 }
102
103 private Properties retrieveJndiProperties() {
104 Properties jndiProperties = new java.util.Properties();
105 jndiProperties.put("java.naming.provider.url", jndiLdapUrl);
106 jndiProperties.put("java.naming.factory.initial","com.sun.jndi.ldap.LdapCtxFactory");
107 jndiProperties.put(Context.SECURITY_PROTOCOL, "none");
108 return jndiProperties;
109 }
110
111 protected GecosMap createMap() {
112 Properties jndiProperties = retrieveJndiProperties();
113 int nTries = 5;
114 Exception lastException = null;
115 int i = 0;
116 for (; i < nTries; i++) {
117 GecosMap map = new GecosMap();
118 log.debug("Attempt " + i + " to retrieve map for '" + jndiLdapUrl + "'");
119 try {
120 DirContext jndiCtx = new InitialDirContext(jndiProperties);
121 NamingEnumeration nisMap = jndiCtx.search("ou=People", "(cn=*)", null);
122 log.trace("Server responded");
123 while (nisMap.hasMore()) {
124 SearchResult res = (SearchResult) nisMap.next();
125 Attributes atts = res.getAttributes();
126 String account = (String) atts.get(accountField).get();
127 Attribute gecosAtt = atts.get(gecosField);
128 if (gecosAtt != null) {
129 String gecos = gecosAtt.get().toString();
130 map.addEntry(account, gecos);
131 } else {
132 log.trace("Found user '" + account + "' with no GECOS field");
133 }
134 }
135 jndiCtx.close();
136 return map;
137 } catch (javax.naming.NamingException ne) {
138 log.warn("Error filling the maps for NIS "+jndiLdapUrl, ne);
139 lastException = ne;
140 try {
141 Thread.sleep(100);
142 } catch (InterruptedException e) {
143 log.warn("Interrupted", e);
144 }
145 } catch (Exception e) {
146 log.warn("Error filling the maps for NIS "+jndiLdapUrl, e);
147 lastException = e;
148 try {
149 Thread.sleep(100);
150 } catch (InterruptedException ie) {
151 log.warn("Interrupted", e);
152 }
153 }
154 }
155 if (i == nTries) {
156 throw new RuntimeException("Couldn't retrieve NIS maps from " + jndiLdapUrl, lastException);
157 }
158 return null;
159 }
160
161 protected String getMapName() {
162 return jndiLdapUrl;
163 }
164 }