1 17 18 19 package org.apache.catalina.startup; 20 21 22 import java.io.BufferedReader ; 23 import java.io.FileReader ; 24 import java.io.IOException ; 25 import java.util.Hashtable ; 26 import java.util.Enumeration ; 27 28 29 36 37 public final class PasswdUserDatabase 38 implements UserDatabase { 39 40 41 43 44 47 public PasswdUserDatabase() { 48 49 super(); 50 51 } 52 53 54 56 57 60 private static final String PASSWORD_FILE = "/etc/passwd"; 61 62 63 66 private Hashtable homes = new Hashtable (); 67 68 69 72 private UserConfig userConfig = null; 73 74 75 77 78 81 public UserConfig getUserConfig() { 82 83 return (this.userConfig); 84 85 } 86 87 88 93 public void setUserConfig(UserConfig userConfig) { 94 95 this.userConfig = userConfig; 96 init(); 97 98 } 99 100 101 103 104 109 public String getHome(String user) { 110 111 return ((String ) homes.get(user)); 112 113 } 114 115 116 119 public Enumeration getUsers() { 120 121 return (homes.keys()); 122 123 } 124 125 126 128 129 132 private void init() { 133 134 BufferedReader reader = null; 135 try { 136 137 reader = new BufferedReader (new FileReader (PASSWORD_FILE)); 138 139 while (true) { 140 141 StringBuffer buffer = new StringBuffer (); 143 while (true) { 144 int ch = reader.read(); 145 if ((ch < 0) || (ch == '\n')) 146 break; 147 buffer.append((char) ch); 148 } 149 String line = buffer.toString(); 150 if (line.length() < 1) 151 break; 152 153 int n = 0; 155 String tokens[] = new String [7]; 156 for (int i = 0; i < tokens.length; i++) 157 tokens[i] = null; 158 while (n < tokens.length) { 159 String token = null; 160 int colon = line.indexOf(':'); 161 if (colon >= 0) { 162 token = line.substring(0, colon); 163 line = line.substring(colon + 1); 164 } else { 165 token = line; 166 line = ""; 167 } 168 tokens[n++] = token; 169 } 170 171 if ((tokens[0] != null) && (tokens[5] != null)) 173 homes.put(tokens[0], tokens[5]); 174 175 } 176 177 reader.close(); 178 reader = null; 179 180 } catch (Exception e) { 181 if (reader != null) { 182 try { 183 reader.close(); 184 } catch (IOException f) { 185 ; 186 } 187 reader = null; 188 } 189 } 190 191 } 192 193 194 } 195 | Popular Tags |