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