[16869] | 1 | /*
|
---|
| 2 | * txt2usersDB.java
|
---|
| 3 | * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
|
---|
| 4 | *
|
---|
| 5 | * This program is free software; you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 8 | * (at your option) any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program 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
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program; if not, write to the Free Software
|
---|
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 18 | */
|
---|
[14334] | 19 | package org.greenstone.gsdl3.util;
|
---|
| 20 |
|
---|
| 21 | import java.io.BufferedReader;
|
---|
| 22 | import java.io.IOException;
|
---|
[25338] | 23 | import java.io.File;
|
---|
[14334] | 24 | import java.io.FileReader;
|
---|
| 25 | import java.sql.SQLException;
|
---|
| 26 |
|
---|
[25338] | 27 | import org.greenstone.gsdl3.service.Authentication;
|
---|
| 28 |
|
---|
[14334] | 29 | public class txt2usersDB {
|
---|
| 30 |
|
---|
| 31 | public static void main(String[] args) throws SQLException{
|
---|
[25338] | 32 | boolean appending = false;
|
---|
| 33 |
|
---|
| 34 | String usage = "Usage: java org.greenstone.gsdl3.txt2usersDB full_path_of_the_text_file full_path_of_the_usersDB [-append]";
|
---|
| 35 | if (args.length < 2){
|
---|
| 36 | System.out.println(usage);
|
---|
[14334] | 37 | System.exit(0);
|
---|
| 38 | }
|
---|
[25338] | 39 | File txtfile = new File(args[0]);
|
---|
| 40 | if(!txtfile.exists()) {
|
---|
| 41 | System.out.println("File " + args[0] + " does not exist.");
|
---|
| 42 | System.out.println(usage);
|
---|
| 43 | System.exit(0);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[14334] | 46 | try {
|
---|
| 47 | BufferedReader in = new BufferedReader(new FileReader(args[0]));
|
---|
| 48 | String str;
|
---|
| 49 | DerbyWrapper dw=new DerbyWrapper();
|
---|
| 50 | dw.connectDatabase(args[1],false);
|
---|
[25338] | 51 |
|
---|
| 52 | if(args.length > 2 && args[2].equals("-append")) {
|
---|
| 53 | appending = true;
|
---|
| 54 | } else {
|
---|
| 55 | // no appending, replace existing database: the text file
|
---|
| 56 | // represents the new database, so delete the existing DB first
|
---|
| 57 | boolean delete_rows = dw.deleteAllUser();
|
---|
| 58 | dw.closeDatabase();
|
---|
| 59 | if (!delete_rows){
|
---|
| 60 | System.out.println("Couldn't delete rows of the users table");
|
---|
| 61 | System.exit(0);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[14334] | 65 | String username=null;
|
---|
| 66 | String password=null;
|
---|
| 67 | String groups=null;
|
---|
| 68 | String accountstatus=null;
|
---|
| 69 | String comment=null;
|
---|
[25308] | 70 | String email=null;
|
---|
[25338] | 71 |
|
---|
[14334] | 72 | while ((str = in.readLine()) != null) {
|
---|
| 73 | //ystem.out.println(str);
|
---|
[25308] | 74 |
|
---|
| 75 | if(str.indexOf(" = ") != -1) { // works with DerbyWrapper.db2txt() and usersDB2txt.java. Fields listed as: USERNAME = admin
|
---|
| 76 | String field=str.substring(0,str.indexOf(" = "));
|
---|
| 77 | if (field.equalsIgnoreCase("email")){
|
---|
| 78 | email=str.substring(str.indexOf(" = ")+3,str.length());
|
---|
| 79 | }
|
---|
| 80 | if (field.equalsIgnoreCase("comment")){
|
---|
| 81 | comment=str.substring(str.indexOf(" = ")+3,str.length());
|
---|
| 82 | }
|
---|
| 83 | if (field.equalsIgnoreCase("status")){
|
---|
| 84 | accountstatus=str.substring(str.indexOf(" = ")+3,str.length());
|
---|
| 85 | }
|
---|
| 86 | if (field.equalsIgnoreCase("groups")){
|
---|
| 87 | groups=str.substring(str.indexOf(" = ")+3,str.length());
|
---|
| 88 | }
|
---|
| 89 | if (field.equalsIgnoreCase("password")){
|
---|
| 90 | //password=dw.rot13(str.substring(str.indexOf(">")+1,str.length()));
|
---|
| 91 | password=str.substring(str.indexOf(" = ")+3,str.length());
|
---|
| 92 | }
|
---|
| 93 | if (field.equalsIgnoreCase("username")){
|
---|
| 94 | username=str.substring(str.indexOf(" = ")+3,str.length());
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | else if (str.startsWith("<")){ // fields listed as: <username>admin
|
---|
[14334] | 98 | String field=str.substring(1,str.indexOf(">"));
|
---|
[25308] | 99 | if (field.equals("email")){
|
---|
| 100 | email=str.substring(str.indexOf(">")+1,str.length());
|
---|
| 101 | }
|
---|
[14334] | 102 | if (field.equals("comment")){
|
---|
| 103 | comment=str.substring(str.indexOf(">")+1,str.length());
|
---|
| 104 | }
|
---|
[25308] | 105 | if (field.equals("enabled") || field.equals("status")){
|
---|
[14334] | 106 | accountstatus=str.substring(str.indexOf(">")+1,str.length());
|
---|
| 107 | }
|
---|
| 108 | if (field.equals("groups")){
|
---|
| 109 | groups=str.substring(str.indexOf(">")+1,str.length());
|
---|
| 110 | }
|
---|
[25338] | 111 | if (field.equals("password")){
|
---|
[25308] | 112 | password=str.substring(str.indexOf(">")+1,str.length());
|
---|
[14334] | 113 | }
|
---|
| 114 | if (field.equals("username")){
|
---|
| 115 | username=str.substring(str.indexOf(">")+1,str.length());
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
[25308] | 118 | else if (str.equals("----------------------------------------------------------------------")
|
---|
[25338] | 119 | || str.equals("-------------------------------------")) {
|
---|
[25308] | 120 |
|
---|
[25338] | 121 | if ((username!=null) && (password!=null) && (groups!=null) && (accountstatus!=null) && (comment!=null)) {
|
---|
[14336] | 122 | dw.connectDatabase(args[1],false);
|
---|
[25338] | 123 |
|
---|
| 124 | // check if it's a new user or already exists in the database
|
---|
| 125 | UserQueryResult findUserResult = dw.findUser(username);
|
---|
| 126 |
|
---|
| 127 | if(findUserResult == null) { // add new user
|
---|
| 128 | if(password.length() >= 3 && password.length() <= 8) { // if not yet encrypted, encrypt first
|
---|
| 129 | password = Authentication.hashPassword(password);
|
---|
| 130 | } // if > 8 chars, password for user being added was already encrypted (hashed-and-hexed)
|
---|
| 131 | dw.addUser(username, password, groups, accountstatus, comment, email);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | else { // modify existing user
|
---|
| 135 | // if any of the other fields are not specified, get them from the database
|
---|
| 136 | UserTermInfo user = findUserResult.getUserTerms().get(0);
|
---|
| 137 |
|
---|
| 138 | if(password.length() < 3 || password.length() > 8) { // includes empty string case
|
---|
| 139 | password = user.password;
|
---|
| 140 | } else { // need to first encrypt (hash-and-hex) the user-entered password
|
---|
| 141 | // Use the same encryption technique used by the Admin Authentication page
|
---|
| 142 | // This ensures that the password generated for a string remains consistent
|
---|
| 143 | password = Authentication.hashPassword(password);
|
---|
| 144 | }
|
---|
| 145 | groups = groups.equals("") ? user.groups : groups;
|
---|
| 146 | accountstatus = accountstatus.equals("") ? user.accountstatus : accountstatus;
|
---|
| 147 | comment = comment.equals("") ? user.comment : comment;
|
---|
| 148 |
|
---|
| 149 | if (email == null) { // special checking for backwards compatibility since old DB did not have email field
|
---|
| 150 | email = "";
|
---|
| 151 | }
|
---|
| 152 | if(user.email == null) {
|
---|
| 153 | user.email = "";
|
---|
| 154 | }
|
---|
| 155 | if(email.equals("")) {
|
---|
| 156 | email = user.email;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | //System.err.println("**** Password: " + password);
|
---|
| 160 | //System.err.println("**** " + username + " " + password + " " + groups + " " + accountstatus + " " + comment + " " + email);
|
---|
| 161 | dw.modifyUserInfo(username, password, groups, accountstatus, comment, email);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[14334] | 164 | username=null;
|
---|
| 165 | password=null;
|
---|
| 166 | groups=null;
|
---|
| 167 | accountstatus=null;
|
---|
| 168 | comment=null;
|
---|
[25308] | 169 | email=null;
|
---|
| 170 | //dw.connectDatabase(args[1],false); // should this be closeDatabase()????
|
---|
| 171 | dw.closeDatabase();
|
---|
[14334] | 172 | }
|
---|
| 173 | }
|
---|
[25338] | 174 |
|
---|
| 175 | // only true back when when hashed passwords weren't being converted to hex
|
---|
| 176 | //else { // encrypted passwords can span multiple lines for some reason
|
---|
[25308] | 177 | // assume that is the case here
|
---|
[25338] | 178 | //if(password != null) {
|
---|
| 179 | // password = password + "\n" + str;
|
---|
| 180 | // }
|
---|
| 181 | //}
|
---|
| 182 |
|
---|
| 183 | }
|
---|
| 184 | //dw.closeDatabase();
|
---|
[14334] | 185 | in.close();
|
---|
| 186 | } catch (IOException e) {
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 | }
|
---|