1
2
3
4
5
6
7 package gov.bnl.gums;
8
9 import java.util.Properties;
10 import javax.naming.NamingEnumeration;
11 import javax.naming.directory.Attribute;
12 import javax.naming.directory.Attributes;
13 import javax.naming.directory.DirContext;
14 import javax.naming.directory.InitialDirContext;
15 import javax.naming.directory.SearchResult;
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19 /*** Matches the DN with the account information retrieved from a NIS server.
20 *
21 * @author Gabriele Carcassi
22 */
23 public class GecosNisAccountMapper extends GecosAccountMapper {
24 static private Log log = LogFactory.getLog(GecosNisAccountMapper.class);
25
26 private String jndiNisUrl;
27
28 /***
29 * Returns the URL used to describe the NIS server.
30 * @return NIS url according to JNDI NIS driver.
31 */
32 public String getJndiNisUrl() {
33 return this.jndiNisUrl;
34 }
35
36 /***
37 * Changes the NIS server to use.
38 * @param jndiNisUrl NIS url according to JNDI NIS driver.
39 */
40 public void setJndiNisUrl(String jndiNisUrl) {
41 this.jndiNisUrl = jndiNisUrl;
42 }
43
44 /*** Prepares the parameters for the JNDI connection.
45 */
46 private Properties retrieveJndiProperties() {
47 Properties jndiProperties = new java.util.Properties();
48 jndiProperties.put("java.naming.provider.url", jndiNisUrl);
49 jndiProperties.put("java.naming.factory.initial","com.sun.jndi.nis.NISCtxFactory");
50 return jndiProperties;
51 }
52
53 protected GecosMap createMap() {
54 Properties jndiProperties = retrieveJndiProperties();
55 int nTries = 5;
56 Exception lastException = null;
57 int i = 0;
58 for (; i < nTries; i++) {
59 GecosMap map = new GecosMap();
60 log.debug("Attemp " + i + " to retrieve map for '" + jndiNisUrl + "'");
61 try {
62 DirContext jndiCtx = new InitialDirContext(jndiProperties);
63 NamingEnumeration nisMap = jndiCtx.search("system/passwd.byname", "(cn=*)", null);
64 log.trace("Server responded");
65 while (nisMap.hasMore()) {
66 SearchResult res = (SearchResult) nisMap.next();
67 Attributes atts = res.getAttributes();
68 String username = (String) atts.get("cn").get();
69 Attribute gecosAtt = atts.get("gecos");
70 if (gecosAtt != null) {
71 String gecos = gecosAtt.get().toString();
72 map.addEntry(username, gecos);
73 } else {
74 log.trace("Found user '" + username + "' with no GECOS field");
75 }
76 }
77 jndiCtx.close();
78 return map;
79 } catch (javax.naming.NamingException ne) {
80 log.warn("Error filling the maps for NIS "+jndiNisUrl, ne);
81 lastException = ne;
82 try {
83 Thread.sleep(100);
84 } catch (InterruptedException e) {
85 log.warn("Interrupted", e);
86 }
87 } catch (Exception e) {
88 log.warn("Error filling the maps for NIS "+jndiNisUrl, e);
89 lastException = e;
90 try {
91 Thread.sleep(100);
92 } catch (InterruptedException ie) {
93 log.warn("Interrupted", e);
94 }
95 }
96 }
97 if (i == nTries) {
98 throw new RuntimeException("Couldn't retrieve NIS maps from " + jndiNisUrl, lastException);
99 }
100 return null;
101 }
102
103 protected String mapName() {
104 return jndiNisUrl;
105 }
106 }