Changeset 25338

Show
Ignore:
Timestamp:
03.04.2012 20:56:04 (8 years ago)
Author:
ak19
Message:

1. Replacing ChangePwdUsersDB.java with new file ModifyUsersDB.java, since the latter allows you to modify any and all fields for a username. 2. Now build.xml's config-admin target has been updated to call ModifyUsersDB with a new password for the admin user. A new target, config-user, takes user input to set any or all fields of any username. This can then be called by the release-kit if we wish to add a demo user during installation as we did in the GS2 releasekit. 3. Updated txt2usersDB.java to take the append flag: with this flag on, it will no longer delete the entire DB and read a new DB in from the input text file, but will append the additional entries in the input text file to the existing entries in the usersDB.

Location:
main/trunk/greenstone3
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/build.xml

    r25321 r25338  
    11171117  </target> 
    11181118 
    1119   <target name="check-tomcat-running"> 
     1119  <target name="check-tomcat-running"><!--if you update this target, may want to change similar elements in config-admin--> 
    11201120    <!--can also try the "socket" condition in place of the "http" condition--> 
    11211121    <condition property="tomcat.isrunning"><!--<waitfor maxwait="5" maxwaitunit="second" timeoutproperty="tomcat.isstopped">--> 
     
    11791179    </if> 
    11801180  </target> 
     1181 
     1182  <!-- ======================= Admin Targets ============================ -->   
     1183 
     1184  <!-- This target won't work with Eclipse because the SecureInputHandler used below conflicts with it.  
     1185    See http://www.dcepler.net/post.cfm/hiding-password-input-in-ant  
     1186    But you can do: echo mypassword | ant config-admin --> 
     1187  <target name="config-admin" description="Reset admin password"> 
     1188    <input addproperty="admin.password" defaultvalue="admin" message="New admin password (3-8 characters):&gt;"> 
     1189      <handler classname="org.apache.tools.ant.input.SecureInputHandler" /> <!-- password won't be visible on screen --> 
     1190    </input> 
     1191    <echo>PWD: ${admin.password}</echo> 
     1192    <antcall target="update-userdb"> 
     1193      <param name="user.username" value="admin"/> 
     1194      <param name="user.password" value="${admin.password}"/> 
     1195      <param name="user.groups" value=""/> 
     1196      <param name="user.status" value=""/> 
     1197      <param name="user.comment" value="Password updated."/> 
     1198      <param name="user.email" value=""/> 
     1199    </antcall> 
     1200  </target> 
     1201 
     1202  <target name="config-user" description="Add or modify users" depends="get-user-data,update-userdb"/> 
     1203 
     1204  <target name="get-user-data" description="Get user details"> 
     1205    <input addproperty="user.username" message="Username:&gt;"/> 
     1206    <input addproperty="user.password" defaultvalue="" message="Password (3-8 characters):&gt;"> 
     1207      <handler classname="org.apache.tools.ant.input.SecureInputHandler" /> <!-- password won't be visible on screen --> 
     1208    </input> 
     1209    <input addproperty="user.groups" defaultvalue="" message="Groups (comma-separated list):&gt;"/> 
     1210    <input addproperty="user.status" defaultvalue="true" message="Enabled (true/false):&gt;"/> 
     1211    <input addproperty="user.comment" defaultvalue="" message="Comment:&gt;"/> 
     1212    <input addproperty="user.email" defaultvalue="" message="Email:&gt;"/> 
     1213  </target> 
     1214 
     1215<!-- This target won't work with Eclipse because the SecureInputHandler used below conflicts with it.  
     1216    See http://www.dcepler.net/post.cfm/hiding-password-input-in-ant  
     1217    But you can do: echo mypassword | ant config-admin --> 
     1218  <target name="update-userdb" description="Add or modify users"> 
     1219    <echo>username: ${user.username}</echo> 
     1220    <echo>groups: ${user.groups}</echo> 
     1221 
     1222    <!-- stop tomcat if running, since derby db is embedded and only allows connections from one jvm instance at a time  
     1223      See http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html --> 
     1224    <!--<antcall target="check-tomcat-running"/>--><!--won't set the tomcat.isrunning property for use below--> 
     1225    <condition property="tomcat.isrunning"> 
     1226      <http url="http://${tomcat.server}:${tomcat.port}${app.path}"/><!--can also try the "socket" condition in place of the "http" condition--> 
     1227    </condition> 
     1228    <if> 
     1229      <bool> 
     1230    <istrue value="${tomcat.isrunning}"/> 
     1231      </bool> 
     1232      <antcall target="force-stop-tomcat"/> 
     1233    </if> 
     1234 
     1235    <!--<echo>${admin.password}</echo>--> <!-- for testing --> 
     1236    <java classname="org.greenstone.gsdl3.util.ModifyUsersDB"> 
     1237      <classpath refid="compile.classpath"/> <!--for ${web.lib}/gsdl3.jar and supporting files--> 
     1238      <arg file="${web.home}/sites/localsite/etc/usersDB"/> 
     1239      <arg value="${user.username}"/> 
     1240      <arg value="password=${user.password}"/> 
     1241      <arg value="groups=${user.groups}"/> 
     1242      <arg value="status=${user.status}"/> 
     1243      <arg value="comment=${user.comment}"/> 
     1244      <arg value="email=${user.email}"/> 
     1245    </java> 
     1246     
     1247    <!-- run tomcat again if it used to be running --> 
     1248    <if> 
     1249      <bool> 
     1250    <istrue value="${tomcat.isrunning}"/> 
     1251    </bool> 
     1252      <antcall target="start-tomcat"/> 
     1253    </if> 
     1254  </target> 
     1255 
    11811256 
    11821257  <!-- ======================= Axis Targets ============================ -->   
     
    12251300      </else> 
    12261301 
    1227     </if> 
    1228   </target> 
    1229  
    1230   <!-- This target won't work with Eclipse because the SecureInputHandler used below conflicts with it.  
    1231     See http://www.dcepler.net/post.cfm/hiding-password-input-in-ant  
    1232     But you can do: echo mypassword | ant config-admin --> 
    1233   <target name="config-admin" description="Reset admin password"> 
    1234     <input addproperty="admin.password" defaultvalue="admin" message="New admin password (3-8 characters):&gt;"> 
    1235       <handler classname="org.apache.tools.ant.input.SecureInputHandler" /> <!-- password won't be visible on screen --> 
    1236     </input> 
    1237  
    1238     <!-- stop tomcat if running, since derby db is embedded and only allows connections from one jvm instance at a time  
    1239       See http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html --> 
    1240  
    1241     <!--<antcall target="check-tomcat-running"/>--><!--won't set the tomcat.isrunning property for use below--> 
    1242     <condition property="tomcat.isrunning"> 
    1243       <http url="http://${tomcat.server}:${tomcat.port}${app.path}"/><!--can also try the "socket" condition in place of the "http" condition--> 
    1244     </condition> 
    1245     <if> 
    1246       <bool> 
    1247     <istrue value="${tomcat.isrunning}"/> 
    1248       </bool> 
    1249       <antcall target="force-stop-tomcat"/> 
    1250     </if> 
    1251  
    1252     <!--<echo>${admin.password}</echo>--> <!-- for testing --> 
    1253     <java classname="org.greenstone.gsdl3.util.ChangePwdUsersDB"> 
    1254       <classpath refid="compile.classpath"/> <!--for ${web.lib}/gsdl3.jar and supporting files--> 
    1255       <arg file="${web.home}/sites/localsite/etc/usersDB"/> 
    1256       <arg value="admin"/> 
    1257       <arg value="${admin.password}"/> 
    1258     </java> 
    1259  
    1260     <!-- run tomcat again if it used to be running --> 
    1261     <if> 
    1262       <bool> 
    1263     <istrue value="${tomcat.isrunning}"/> 
    1264     </bool> 
    1265       <antcall target="start-tomcat"/> 
    12661302    </if> 
    12671303  </target> 
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/txt2usersDB.java

    r25308 r25338  
    2121import java.io.BufferedReader; 
    2222import java.io.IOException; 
     23import java.io.File; 
    2324import java.io.FileReader; 
    2425import java.sql.SQLException; 
     26 
     27import org.greenstone.gsdl3.service.Authentication; 
    2528 
    2629public class txt2usersDB { 
    2730     
    2831    public static void main(String[] args) throws SQLException{ 
    29      
    30     if (args.length!=2){ 
    31         System.out.println("Usage: java org.greenstone.gsdl3.txt2usersDB full_path_of_the_text_file full_path_of_the_usersDB"); 
     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); 
    3237        System.exit(0); 
    3338    } 
     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 
    3446    try { 
    3547        BufferedReader in = new BufferedReader(new FileReader(args[0])); 
     
    3749        DerbyWrapper dw=new DerbyWrapper(); 
    3850        dw.connectDatabase(args[1],false); 
    39         boolean delete_rows = dw.deleteAllUser(); 
    40         dw.closeDatabase(); 
    41         if (!delete_rows){ 
    42         System.out.println("Couldn't delete rows of the users table"); 
    43         System.exit(0); 
    44         } 
     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 
    4565        String username=null; 
    4666        String password=null; 
     
    4969        String comment=null; 
    5070        String email=null; 
     71 
    5172        while ((str = in.readLine()) != null) { 
    5273        //ystem.out.println(str); 
     
    88109            groups=str.substring(str.indexOf(">")+1,str.length()); 
    89110            } 
    90             if (field.equals("password")){ 
    91             //password=dw.rot13(str.substring(str.indexOf(">")+1,str.length())); 
     111            if (field.equals("password")){           
    92112            password=str.substring(str.indexOf(">")+1,str.length()); 
    93113            } 
     
    97117        } 
    98118        else if (str.equals("----------------------------------------------------------------------") 
    99              || str.equals("-------------------------------------")) {           
     119             || str.equals("-------------------------------------")) { 
    100120             
    101             if ((username!=null) && (password!=null) && (groups!=null) && (accountstatus!=null) && (comment!=null) && (email!=null)) { 
     121            if ((username!=null) && (password!=null) && (groups!=null) && (accountstatus!=null) && (comment!=null)) { 
    102122            dw.connectDatabase(args[1],false); 
    103             dw.addUser(username, password, groups, accountstatus, comment, email); 
     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             
    104164            username=null; 
    105165            password=null; 
     
    112172            } 
    113173        } 
    114         else { // encrypted passwords can span multiple lines for some reason 
     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 
    115177               // assume that is the case here 
    116             if(password != null) {  
    117             password = password + "\n" + str; 
    118             } 
    119         } 
    120         }    
     178        //if(password != null) {  
     179        //  password = password + "\n" + str; 
     180        //  } 
     181        //} 
     182 
     183        } 
     184        //dw.closeDatabase(); 
    121185        in.close(); 
    122186    } catch (IOException e) {