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