001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.server.tools;
020:
021: import java.sql.Connection;
022: import java.sql.PreparedStatement;
023: import java.sql.ResultSet;
024: import java.util.logging.Level;
025:
026: import org.lucane.common.Logging;
027: import org.lucane.common.crypto.MD5;
028: import org.lucane.server.ServerConfig;
029: import org.lucane.server.database.DatabaseAbstractionLayer;
030:
031: public class ResetPasswords {
032: private static final String CHARS = "azertyuiopqsdfghjklmwxcvbn1234567890";
033: private static final int PASSWORD_LENGTH = 8;
034:
035: private static String createNewPassword() {
036: String passwd = "";
037:
038: while (passwd.length() < PASSWORD_LENGTH) {
039: char newChar = CHARS
040: .charAt((int) (Math.random() * Integer.MAX_VALUE)
041: % CHARS.length());
042: passwd += newChar;
043: }
044:
045: return passwd;
046: }
047:
048: private static void updateUser(String login, String passwd,
049: PreparedStatement update) throws Exception {
050: update.setString(1, MD5.encode(passwd));
051: update.setString(2, login);
052: update.execute();
053:
054: System.out.println(login + ":" + passwd);
055: }
056:
057: public static void main(String[] args) throws Exception {
058: Logging.getLogger().setLevel(Level.OFF);
059:
060: ServerConfig config = new ServerConfig("etc/server-config.xml");
061: if (!config.getStoreBackend().equals("database")) {
062: System.err.println("not using database backend, sorry.");
063: System.exit(1);
064: }
065:
066: if (args.length > 2) {
067: System.out.println("usage: ResetPasswords [user [passwd]]");
068: System.exit(1);
069: }
070:
071: String login = null;
072: String passwd = null;
073: if (args.length > 0) {
074: login = args[0];
075: if (args.length == 2)
076: passwd = args[1];
077: else
078: passwd = createNewPassword();
079: }
080:
081: DatabaseAbstractionLayer dbLayer = DatabaseAbstractionLayer
082: .createLayer(config);
083:
084: Connection c = dbLayer.getConnection();
085: PreparedStatement update = c
086: .prepareStatement("UPDATE users SET passwd=? WHERE login=?");
087:
088: //-- update all users
089: if (login == null) {
090: PreparedStatement select = c
091: .prepareStatement("SELECT login FROM users");
092: ResultSet rs = select.executeQuery();
093:
094: while (rs.next()) {
095: login = rs.getString(1);
096: passwd = createNewPassword();
097: updateUser(login, passwd, update);
098: }
099: rs.close();
100: select.close();
101: }
102:
103: //-- update one user
104: else
105: updateUser(login, passwd, update);
106:
107: update.close();
108: c.close();
109: }
110: }
|