001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.startup;
019:
020: import java.io.BufferedReader;
021: import java.io.FileReader;
022: import java.io.IOException;
023: import java.util.Hashtable;
024: import java.util.Enumeration;
025:
026: /**
027: * Concrete implementation of the <strong>UserDatabase</code> interface
028: * that processes the <code>/etc/passwd</code> file on a Unix system.
029: *
030: * @author Craig R. McClanahan
031: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
032: */
033:
034: public final class PasswdUserDatabase implements UserDatabase {
035:
036: // --------------------------------------------------------- Constructors
037:
038: /**
039: * Initialize a new instance of this user database component.
040: */
041: public PasswdUserDatabase() {
042:
043: super ();
044:
045: }
046:
047: // --------------------------------------------------- Instance Variables
048:
049: /**
050: * The pathname of the Unix password file.
051: */
052: private static final String PASSWORD_FILE = "/etc/passwd";
053:
054: /**
055: * The set of home directories for all defined users, keyed by username.
056: */
057: private Hashtable homes = new Hashtable();
058:
059: /**
060: * The UserConfig listener with which we are associated.
061: */
062: private UserConfig userConfig = null;
063:
064: // ----------------------------------------------------------- Properties
065:
066: /**
067: * Return the UserConfig listener with which we are associated.
068: */
069: public UserConfig getUserConfig() {
070:
071: return (this .userConfig);
072:
073: }
074:
075: /**
076: * Set the UserConfig listener with which we are associated.
077: *
078: * @param userConfig The new UserConfig listener
079: */
080: public void setUserConfig(UserConfig userConfig) {
081:
082: this .userConfig = userConfig;
083: init();
084:
085: }
086:
087: // ------------------------------------------------------- Public Methods
088:
089: /**
090: * Return an absolute pathname to the home directory for the specified user.
091: *
092: * @param user User for which a home directory should be retrieved
093: */
094: public String getHome(String user) {
095:
096: return ((String) homes.get(user));
097:
098: }
099:
100: /**
101: * Return an enumeration of the usernames defined on this server.
102: */
103: public Enumeration getUsers() {
104:
105: return (homes.keys());
106:
107: }
108:
109: // ------------------------------------------------------ Private Methods
110:
111: /**
112: * Initialize our set of users and home directories.
113: */
114: private void init() {
115:
116: BufferedReader reader = null;
117: try {
118:
119: reader = new BufferedReader(new FileReader(PASSWORD_FILE));
120:
121: while (true) {
122:
123: // Accumulate the next line
124: StringBuffer buffer = new StringBuffer();
125: while (true) {
126: int ch = reader.read();
127: if ((ch < 0) || (ch == '\n'))
128: break;
129: buffer.append((char) ch);
130: }
131: String line = buffer.toString();
132: if (line.length() < 1)
133: break;
134:
135: // Parse the line into constituent elements
136: int n = 0;
137: String tokens[] = new String[7];
138: for (int i = 0; i < tokens.length; i++)
139: tokens[i] = null;
140: while (n < tokens.length) {
141: String token = null;
142: int colon = line.indexOf(':');
143: if (colon >= 0) {
144: token = line.substring(0, colon);
145: line = line.substring(colon + 1);
146: } else {
147: token = line;
148: line = "";
149: }
150: tokens[n++] = token;
151: }
152:
153: // Add this user and corresponding directory
154: if ((tokens[0] != null) && (tokens[5] != null))
155: homes.put(tokens[0], tokens[5]);
156:
157: }
158:
159: reader.close();
160: reader = null;
161:
162: } catch (Exception e) {
163: if (reader != null) {
164: try {
165: reader.close();
166: } catch (IOException f) {
167: ;
168: }
169: reader = null;
170: }
171: }
172:
173: }
174:
175: }
|