1
2
3
4
5
6
7 package gov.bnl.gums.account;
8
9 import gov.bnl.gums.GUMS;
10 import gov.bnl.gums.configuration.Configuration;
11
12 import java.util.Properties;
13
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 GecosNisAccountMapper extends GecosAccountMapper {
30 static private Log log = LogFactory.getLog(GecosNisAccountMapper.class);
31 static Log adminLog = LogFactory.getLog(GUMS.resourceAdminLog);
32
33 static public String getTypeStatic() {
34 return "gecosNIS";
35 }
36
37 private String jndiNisUrl = "";
38 private String gecosField = "gecos";
39 private String accountField = "cn";
40
41 public GecosNisAccountMapper() {
42 super();
43 adminLog.warn("The use of gov.bnl.gums.GecosNisAccountMapper is unsupported.");
44 }
45
46 public GecosNisAccountMapper(Configuration configuration) {
47 super(configuration);
48 }
49
50 public GecosNisAccountMapper(Configuration configuration, String name) {
51 super(configuration, name);
52 }
53
54 public AccountMapper clone(Configuration configuration) {
55 GecosNisAccountMapper accountMapper = new GecosNisAccountMapper(configuration, new String(getName()));
56 accountMapper.setDescription(new String(getDescription()));
57 accountMapper.setJndiNisUrl(new String(jndiNisUrl));
58 accountMapper.setGecosField(new String(gecosField));
59 accountMapper.setAccountField(new String(accountField));
60 return accountMapper;
61 }
62
63 public GecosMap createMap() {
64 Properties jndiProperties = retrieveJndiProperties();
65 int nTries = 5;
66 Exception lastException = null;
67 int i = 0;
68 for (; i < nTries; i++) {
69 GecosMap map = new GecosMap();
70 log.debug("Attempt " + i + " to retrieve map for '" + jndiNisUrl + "'");
71 try {
72 DirContext jndiCtx = new InitialDirContext(jndiProperties);
73 NamingEnumeration nisMap = jndiCtx.search("system/passwd.byname", "(cn=*)", null);
74 log.trace("Server responded");
75 while (nisMap.hasMore()) {
76 SearchResult res = (SearchResult) nisMap.next();
77 Attributes atts = res.getAttributes();
78 String account = (String) atts.get(accountField).get();
79 Attribute gecosAtt = atts.get(gecosField);
80 if (gecosAtt != null) {
81 String gecos = gecosAtt.get().toString();
82 map.addEntry(account, gecos);
83 } else {
84 log.trace("Found user '" + account + "' with no GECOS field");
85 }
86 }
87 jndiCtx.close();
88 return map;
89 } catch (javax.naming.NamingException ne) {
90 log.warn("Error filling the maps for NIS "+jndiNisUrl, ne);
91 lastException = ne;
92 try {
93 Thread.sleep(100);
94 } catch (InterruptedException e) {
95 log.warn("Interrupted", e);
96 }
97 } catch (Exception e) {
98 log.warn("Error filling the maps for NIS "+jndiNisUrl, e);
99 lastException = e;
100 try {
101 Thread.sleep(100);
102 } catch (InterruptedException ie) {
103 log.warn("Interrupted", e);
104 }
105 }
106 }
107 if (i == nTries) {
108 throw new RuntimeException("Couldn't retrieve NIS maps from " + jndiNisUrl, lastException);
109 }
110 return null;
111 }
112
113 public String getAccountField() {
114 return accountField;
115 }
116
117 public String getGecosField() {
118 return gecosField;
119 }
120
121 public String getJndiNisUrl() {
122 return jndiNisUrl;
123 }
124
125 public void setAccountField(String accountField) {
126 this.accountField = accountField;
127 }
128
129 public void setGecosField(String gecosField) {
130 this.gecosField = gecosField;
131 }
132
133 public String getType() {
134 return "gecosNIS";
135 }
136
137 public void setJndiNisUrl(String jndiNisUrl) {
138 this.jndiNisUrl = jndiNisUrl;
139 }
140
141 public String toString(String bgColor) {
142 return "<td bgcolor=\""+bgColor+"\"><a href=\"accountMappers.jsp?command=edit&name=" + getName() + "\">" + getName() + "</a></td><td bgcolor=\""+bgColor+"\">" + getType() + "</td><td bgcolor=\""+bgColor+"\"></td>";
143 }
144
145 public String toXML() {
146 return "\t\t<gecosNisAccountMapper\n"+
147 "\t\t\tname='"+getName()+"'\n"+
148 "\t\t\tdescription='"+getDescription()+"'\n"+
149 "\t\t\tjndiNisUrl='"+jndiNisUrl+"'\n"+
150 "\t\t\tgecosField='"+gecosField+"'\n"+
151 "\t\t\taccountField='"+accountField+"'/>\n\n";
152 }
153
154 private Properties retrieveJndiProperties() {
155 Properties jndiProperties = new java.util.Properties();
156 jndiProperties.put("java.naming.provider.url", jndiNisUrl);
157 jndiProperties.put("java.naming.factory.initial","com.sun.jndi.nis.NISCtxFactory");
158 return jndiProperties;
159 }
160
161 protected String getMapName() {
162 return jndiNisUrl;
163 }
164 }