1 19 package org.lucane.server.tools; 20 21 import java.sql.Connection ; 22 import java.sql.PreparedStatement ; 23 import java.sql.ResultSet ; 24 import java.util.logging.Level ; 25 26 import org.lucane.common.Logging; 27 import org.lucane.common.crypto.MD5; 28 import org.lucane.server.ServerConfig; 29 import org.lucane.server.database.DatabaseAbstractionLayer; 30 31 public class ResetPasswords 32 { 33 private static final String CHARS = "azertyuiopqsdfghjklmwxcvbn1234567890"; 34 private static final int PASSWORD_LENGTH = 8; 35 36 private static String createNewPassword() 37 { 38 String passwd = ""; 39 40 while(passwd.length() < PASSWORD_LENGTH) 41 { 42 char newChar = CHARS.charAt((int)(Math.random() * Integer.MAX_VALUE) 43 % CHARS.length()); 44 passwd += newChar; 45 } 46 47 return passwd; 48 } 49 50 private static void updateUser(String login, String passwd, PreparedStatement update) 51 throws Exception 52 { 53 update.setString(1, MD5.encode(passwd)); 54 update.setString(2, login); 55 update.execute(); 56 57 System.out.println(login + ":" + passwd); 58 } 59 60 public static void main(String [] args) 61 throws Exception 62 { 63 Logging.getLogger().setLevel(Level.OFF); 64 65 ServerConfig config = new ServerConfig("etc/server-config.xml"); 66 if(!config.getStoreBackend().equals("database")) 67 { 68 System.err.println("not using database backend, sorry."); 69 System.exit(1); 70 } 71 72 if(args.length > 2) 73 { 74 System.out.println("usage: ResetPasswords [user [passwd]]"); 75 System.exit(1); 76 } 77 78 String login = null; 79 String passwd = null; 80 if(args.length > 0) 81 { 82 login = args[0]; 83 if(args.length == 2) 84 passwd = args[1]; 85 else 86 passwd = createNewPassword(); 87 } 88 89 90 91 DatabaseAbstractionLayer dbLayer = DatabaseAbstractionLayer.createLayer(config); 92 93 Connection c = dbLayer.getConnection(); 94 PreparedStatement update = c.prepareStatement( 95 "UPDATE users SET passwd=? WHERE login=?"); 96 97 if(login == null) 99 { 100 PreparedStatement select = c.prepareStatement("SELECT login FROM users"); 101 ResultSet rs = select.executeQuery(); 102 103 while(rs.next()) 104 { 105 login = rs.getString(1); 106 passwd = createNewPassword(); 107 updateUser(login, passwd, update); 108 } 109 rs.close(); 110 select.close(); 111 } 112 113 else 115 updateUser(login, passwd, update); 116 117 update.close(); 118 c.close(); 119 } 120 } | Popular Tags |