KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > tools > ResetPasswords


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.server.tools;
20
21 import java.sql.Connection JavaDoc;
22 import java.sql.PreparedStatement JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.util.logging.Level JavaDoc;
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 JavaDoc CHARS = "azertyuiopqsdfghjklmwxcvbn1234567890";
34     private static final int PASSWORD_LENGTH = 8;
35     
36     private static String JavaDoc createNewPassword()
37     {
38         String JavaDoc 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 JavaDoc login, String JavaDoc passwd, PreparedStatement JavaDoc update)
51     throws Exception JavaDoc
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 JavaDoc [] args)
61     throws Exception JavaDoc
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 JavaDoc login = null;
79         String JavaDoc 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 JavaDoc c = dbLayer.getConnection();
94         PreparedStatement JavaDoc update = c.prepareStatement(
95                 "UPDATE users SET passwd=? WHERE login=?");
96
97         //-- update all users
98
if(login == null)
99         {
100             PreparedStatement JavaDoc select = c.prepareStatement("SELECT login FROM users");
101             ResultSet JavaDoc 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         //-- update one user
114
else
115             updateUser(login, passwd, update);
116         
117         update.close();
118         c.close();
119     }
120 }
Popular Tags