1 18 package net.sf.drftpd.master.usermanager.glftpd; 19 20 import java.io.BufferedReader ; 21 import java.io.File ; 22 import java.io.FileNotFoundException ; 23 import java.io.FileReader ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Properties ; 30 import java.util.StringTokenizer ; 31 32 import net.sf.drftpd.DuplicateElementException; 33 import net.sf.drftpd.master.ConnectionManager; 34 import net.sf.drftpd.master.usermanager.NoSuchUserException; 35 import net.sf.drftpd.master.usermanager.User; 36 import net.sf.drftpd.master.usermanager.UserFileException; 37 import net.sf.drftpd.master.usermanager.UserManager; 38 39 import org.apache.log4j.Logger; 40 41 46 public class GlftpdUserManager implements UserManager { 47 private static final Logger logger = 48 Logger.getLogger(GlftpdUserManager.class.getName()); 49 50 private File userpathFile; 51 52 private String userdirpath; 53 private File passwdfile; 54 64 public GlftpdUserManager() { 65 this(System.getProperties()); 66 } 67 68 public GlftpdUserManager(Properties cfg) { 69 String glftpdRoot = cfg.getProperty("glftpd.root", "."); 72 userdirpath = 73 cfg.getProperty("glftpd.users", glftpdRoot + "/ftp-data/users/"); 74 userpathFile = new File (userdirpath); 75 passwdfile = 76 new File ( 77 cfg.getProperty("glftpd.passwd", glftpdRoot + "/etc/passwd")); 78 } 79 80 83 void save(User user) throws IOException { 84 throw new UnsupportedOperationException (); 85 } 86 87 90 public User create(String username) { 91 throw new UnsupportedOperationException (); 92 } 93 94 void load(User user) throws NoSuchUserException, IOException { 95 GlftpdUser gluser = null; 96 if (user instanceof GlftpdUser) 97 gluser = (GlftpdUser) user; 98 99 { 100 BufferedReader in; 101 try { 102 in = 103 new BufferedReader ( 104 new FileReader (getUserfilepath(user.getUsername()))); 105 } catch (FileNotFoundException ex) { 106 throw new NoSuchUserException(ex.toString()); 107 } 108 109 111 String param[], line, arg; 112 int temp = 0; 113 try { 114 while (true) { 115 try { 116 if ((line = in.readLine()) == null) 117 break; 118 param = line.split(" "); 119 arg = line.substring(param[0].length() + 1); 120 } catch (IOException ex) { 121 ex.printStackTrace(); 122 break; 123 } 124 if ("USER".equals(param[0])) { 125 user.setComment(arg); 126 } else if ("GENERAL".equals(param[0])) { 129 gluser.setWeeklyAllotment( 131 Integer.parseInt(param[1].substring(2))); 132 int idleTime = Integer.parseInt(param[2]) * 60; 133 if (idleTime < 0) 134 idleTime = 0; 135 user.setIdleTime(idleTime); 136 } else if ("LOGINS".equals(param[0])) { 139 141 user.setMaxLogins(Integer.parseInt(param[1])); 143 user.setMaxLoginsPerIP(Integer.parseInt(param[2])); 144 user.setMaxSimDownloads(Integer.parseInt(param[3])); 145 user.setMaxSimUploads(Integer.parseInt(param[4])); 146 } else if ("FLAGS".equals(param[0])) { 147 if (arg.indexOf('1') != -1) 148 user.addGroup("siteop"); 149 if (arg.indexOf('2') != -1) 150 user.addGroup("gadmin"); 151 if (arg.indexOf('3') != -1) 152 user.addGroup("glock"); 153 if (arg.indexOf('4') != -1) 154 user.addGroup("exempt"); 155 if (arg.indexOf('5') != -1) 156 user.addGroup("color"); 157 if (arg.indexOf('6') != -1) 158 user.addGroup("deleted"); 159 if (arg.indexOf('7') != -1) 160 user.addGroup("useredit"); 161 if (arg.indexOf('8') != -1) 162 user.addGroup("anonymous"); 163 if (arg.indexOf('A') != -1) 164 user.addGroup("nuke"); 165 if (arg.indexOf('B') != -1) 166 user.addGroup("unnuke"); 167 if (arg.indexOf('C') != -1) 168 user.addGroup("undupe"); 169 if (arg.indexOf('D') != -1) 170 user.addGroup("kick"); 171 if (arg.indexOf('E') != -1) 172 user.addGroup("kill"); 173 if (arg.indexOf('F') != -1) 174 user.addGroup("take"); 175 if (arg.indexOf('G') != -1) 176 user.addGroup("give"); 177 if (arg.indexOf('H') != -1) 178 user.addGroup("users"); 179 if (arg.indexOf('J') != -1) 180 user.addGroup("cust1"); 181 if (arg.indexOf('K') != -1) 182 user.addGroup("cust2"); 183 if (arg.indexOf('L') != -1) 184 user.addGroup("cust3"); 185 if (arg.indexOf('M') != -1) 186 user.addGroup("cust4"); 187 if (arg.indexOf('N') != -1) 188 user.addGroup("cust5"); 189 } else if ("TAGLINE".equals(param[0])) { 190 user.setTagline(arg); 191 } else if ("CREDITS".equals(param[0])) { 195 user.setCredits(Long.parseLong(param[1]) * 1000); 196 } else if ("RATIO".equals(param[0])) { 197 user.setRatio(Float.parseFloat(param[1])); 198 199 } else if ("ALLUP".equals(param[0])) { 201 user.setUploadedFiles(Integer.parseInt(param[1])); 202 user.setUploadedBytes(Long.parseLong(param[2]) * 1000); 203 user.setUploadedSeconds(Integer.parseInt(param[3])); 204 } else if ("MONTHUP".equals(param[0])) { 205 user.setUploadedFilesMonth(Integer.parseInt(param[1])); 206 user.setUploadedBytesMonth( 207 Long.parseLong(param[2]) * 1000); 208 user.setUploadedSecondsMonth( 209 Integer.parseInt(param[3])); 210 } else if ("WKUP".equals(param[0])) { 211 user.setUploadedFilesWeek(Integer.parseInt(param[1])); 212 user.setUploadedBytesWeek( 213 Long.parseLong(param[2]) * 1000); 214 user.setUploadedSecondsWeek(Integer.parseInt(param[3])); 215 } else if ("DAYUP".equals(param[0])) { 216 user.setUploadedFilesDay(Integer.parseInt(param[1])); 217 user.setUploadedBytesDay( 218 Long.parseLong(param[2]) * 1000); 219 user.setUploadedSecondsDay(Integer.parseInt(param[3])); 220 221 } else if ("ALLDN".equals(param[0])) { 222 user.setDownloadedFiles(Integer.parseInt(param[1])); 223 user.setDownloadedBytes( 224 Long.parseLong(param[2]) * 1000); 225 user.setDownloadedSeconds(Integer.parseInt(param[3])); 226 } else if ("MONTHDN".equals(param[0])) { 227 user.setDownloadedFilesMonth( 228 Integer.parseInt(param[1])); 229 user.setDownloadedBytesMonth( 230 Long.parseLong(param[2]) * 1000); 231 user.setDownloadedSecondsMonth( 232 Integer.parseInt(param[3])); 233 } else if ("WKDN".equals(param[0])) { 234 user.setDownloadedFilesMonth( 235 Integer.parseInt(param[1])); 236 user.setDownloadedBytesMonth( 237 Long.parseLong(param[2]) * 1000); 238 user.setDownloadedSecondsMonth( 239 Integer.parseInt(param[3])); 240 } else if ("DAYDN".equals(param[0])) { 241 user.setDownloadedFilesDay(Integer.parseInt(param[1])); 242 user.setDownloadedBytesDay( 243 Long.parseLong(param[2]) * 1000); 244 user.setDownloadedSecondsDay( 245 Integer.parseInt(param[3])); 246 } else if ("TIME".equals(param[0])) { 247 user.setLogins(Integer.parseInt(param[1])); 249 user.setLastAccessTime(Integer.parseInt(param[2])); 250 } else if ("NUKE".equals(param[0])) { 253 user.setLastNuked(Integer.parseInt(param[1])); 255 user.setTimesNuked(Integer.parseInt(param[2])); 256 user.setNukedBytes(Long.parseLong(param[3]) * 1000000); 257 258 } else if ("SLOTS".equals(param[0])) { 259 gluser.setGroupSlots(Short.parseShort(param[1])); 261 gluser.setGroupLeechSlots(Short.parseShort(param[2])); 262 } else if ("TIMEFRAME".equals(param[0])) { 263 } else if ("GROUP".equals(param[0])) { 264 if (temp == 0) { 265 user.setGroup(arg); 266 temp = 1; 267 } 268 user.addGroup(arg); 269 } else if ("PRIVATE".equals(param[0])) { 270 gluser.addPrivateGroup(arg); 271 } else if ("IP".equals(param[0])) { 272 user.addIPMask(arg); 273 } else if (param[0].startsWith("#")) { 274 } else { 276 logger.info("Unrecognized userfile entry: " + line); 277 } 278 } 279 } catch (DuplicateElementException e) { 280 logger.warn("", e); 281 } finally { 282 in.close(); 283 } 284 } 285 { 286 BufferedReader in; 287 try { 288 in = new BufferedReader (new FileReader (passwdfile)); 289 } catch (FileNotFoundException ex) { 290 throw new RuntimeException (ex.toString()); 291 } 292 String line; 293 boolean foundpassword = false; 294 try { 295 while ((line = in.readLine()) != null) { 296 StringTokenizer st = new StringTokenizer (line, ":"); 297 String username2 = st.nextToken(); 298 if (user.getUsername().equals(username2)) { 299 gluser.setUnixPassword(st.nextToken()); 300 foundpassword = true; 301 break; 302 } 303 } 304 } finally { 305 in.close(); 306 } 307 if (!foundpassword) { 308 logger.warn("couldn't find a password in " + passwdfile); 309 return; 310 } 311 } 312 } 314 315 public User getUserByNameUnchecked(String username) 316 throws UserFileException, NoSuchUserException { 317 if (!new File (getUserfilepath(username)).exists()) { 318 throw new NoSuchUserException("No userfile for user " + username); 319 } 320 GlftpdUser user = new GlftpdUser(username); 321 try { 322 load(user); 323 } catch (IOException e) { 325 throw new UserFileException(e); 326 } 327 return user; 328 } 329 330 public boolean exists(String name) { 331 throw new UnsupportedOperationException (); 332 } 333 334 private String getUserfilepath(String username) { 335 return userdirpath + "/" + username; 336 } 337 338 public void saveAll() throws UserFileException { 339 throw new UnsupportedOperationException (); 340 } 341 public Collection getAllUsersByGroup(String group) 342 throws UserFileException { 343 Collection users = getAllUsers(); 344 for (Iterator iter = users.iterator(); iter.hasNext();) { 345 GlftpdUser user = (GlftpdUser) iter.next(); 346 if (!user.getGroupName().equals(group)) 347 iter.remove(); 348 } 349 return users; 350 } 351 public List getAllUsers() throws UserFileException { 352 if (!userpathFile.exists()) 353 throw new UserFileException(userpathFile + " not found"); 354 String userpaths[] = userpathFile.list(); 355 ArrayList users = new ArrayList (); 356 357 for (int i = 0; i < userpaths.length; i++) { 358 String userpath = userpaths[i]; 359 logger.debug(userpath); 360 if (userpath.endsWith(".xml")) 361 continue; 362 if (!new File (getUserfilepath(userpath)).isFile()) 363 continue; 364 if (userpath.endsWith(".user")) continue; 366 logger.debug("add " + userpath); 367 try { 368 users.add(getUserByName(userpath)); 369 } catch (NoSuchUserException e) { 371 throw new UserFileException("", e); 372 } 373 } 374 return users; 375 } 376 public Collection getAllGroups() throws UserFileException { 377 throw new UnsupportedOperationException (); 378 379 396 } 397 398 public void init(ConnectionManager mgr) { 399 } 400 401 public User getUserByName(String username) 402 throws NoSuchUserException, UserFileException { 403 User user = getUserByNameUnchecked(username); 404 if (user.isDeleted()) 405 throw new NoSuchUserException(user.getUsername() + " is deleted"); 406 return user; 407 } 408 409 } 410 | Popular Tags |