source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/txt2usersDB.java@ 30196

Last change on this file since 30196 was 30196, checked in by ak19, 9 years ago

Fixes to do with networked Derby: port and host number changes propagated from build.props to global.props are now at last picked up by DerbyWrapper.java by build.xml setting a system property (-Dgsdl3.writablehome), which is then used to force load Global.Properties if it's not loaded yet. It won't be loaded when launching the ant update-userdb cmd which runs ModifyUsersDB.java. Or if running ModifyUsersDB.java, txt2usersDB.java or usersDB2txt from the cmdline.

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