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 018 package org.apache.catalina.startup; 019 020 021 import java.io.BufferedReader; 022 import java.io.FileReader; 023 import java.io.IOException; 024 import java.util.Hashtable; 025 import java.util.Enumeration; 026 027 028 /** 029 * Concrete implementation of the <strong>UserDatabase</code> interface 030 * that processes the <code>/etc/passwd</code> file on a Unix system. 031 * 032 * @author Craig R. McClanahan 033 * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:49 $ 034 */ 035 036 public final class PasswdUserDatabase 037 implements UserDatabase { 038 039 040 // --------------------------------------------------------- Constructors 041 042 043 /** 044 * Initialize a new instance of this user database component. 045 */ 046 public PasswdUserDatabase() { 047 048 super(); 049 050 } 051 052 053 // --------------------------------------------------- Instance Variables 054 055 056 /** 057 * The pathname of the Unix password file. 058 */ 059 private static final String PASSWORD_FILE = "/etc/passwd"; 060 061 062 /** 063 * The set of home directories for all defined users, keyed by username. 064 */ 065 private Hashtable homes = new Hashtable(); 066 067 068 /** 069 * The UserConfig listener with which we are associated. 070 */ 071 private UserConfig userConfig = null; 072 073 074 // ----------------------------------------------------------- Properties 075 076 077 /** 078 * Return the UserConfig listener with which we are associated. 079 */ 080 public UserConfig getUserConfig() { 081 082 return (this.userConfig); 083 084 } 085 086 087 /** 088 * Set the UserConfig listener with which we are associated. 089 * 090 * @param userConfig The new UserConfig listener 091 */ 092 public void setUserConfig(UserConfig userConfig) { 093 094 this.userConfig = userConfig; 095 init(); 096 097 } 098 099 100 // ------------------------------------------------------- Public Methods 101 102 103 /** 104 * Return an absolute pathname to the home directory for the specified user. 105 * 106 * @param user User for which a home directory should be retrieved 107 */ 108 public String getHome(String user) { 109 110 return ((String) homes.get(user)); 111 112 } 113 114 115 /** 116 * Return an enumeration of the usernames defined on this server. 117 */ 118 public Enumeration getUsers() { 119 120 return (homes.keys()); 121 122 } 123 124 125 // ------------------------------------------------------ Private Methods 126 127 128 /** 129 * Initialize our set of users and home directories. 130 */ 131 private void init() { 132 133 BufferedReader reader = null; 134 try { 135 136 reader = new BufferedReader(new FileReader(PASSWORD_FILE)); 137 138 while (true) { 139 140 // Accumulate the next line 141 StringBuffer buffer = new StringBuffer(); 142 while (true) { 143 int ch = reader.read(); 144 if ((ch < 0) || (ch == '\n')) 145 break; 146 buffer.append((char) ch); 147 } 148 String line = buffer.toString(); 149 if (line.length() < 1) 150 break; 151 152 // Parse the line into constituent elements 153 int n = 0; 154 String tokens[] = new String[7]; 155 for (int i = 0; i < tokens.length; i++) 156 tokens[i] = null; 157 while (n < tokens.length) { 158 String token = null; 159 int colon = line.indexOf(':'); 160 if (colon >= 0) { 161 token = line.substring(0, colon); 162 line = line.substring(colon + 1); 163 } else { 164 token = line; 165 line = ""; 166 } 167 tokens[n++] = token; 168 } 169 170 // Add this user and corresponding directory 171 if ((tokens[0] != null) && (tokens[5] != null)) 172 homes.put(tokens[0], tokens[5]); 173 174 } 175 Rate176 reader.close(); 177 reader = null; 178 179 } catch (Exception e) { 180 if (reader != null) { 181 try { Rate182 reader.close(); 183 } catch (IOException f) { 184 ; 185 } 186 reader = null; 187 } 188 } 189 190 } 191 192 193 }