View Javadoc

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