1 | /*
|
---|
2 | * ServletRealmCheck.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.File;
|
---|
23 | import java.io.FileInputStream;
|
---|
24 | import java.io.InputStream;
|
---|
25 | import java.io.InputStreamReader;
|
---|
26 | import java.io.IOException;
|
---|
27 | import java.net.HttpURLConnection;
|
---|
28 | import java.net.URL;
|
---|
29 | import java.net.URLConnection;
|
---|
30 | import java.util.Properties;
|
---|
31 |
|
---|
32 | import org.greenstone.util.ProtocolPortProperties;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Commandline script that is used by gliserver.pl to authenticate a username and password and
|
---|
36 | * return the user's groups, while the derby server is running. Because 2 JVM instances can't
|
---|
37 | * access the same embedded derby server at the same time, gliserver can't call usersDB2txt.java.
|
---|
38 | * If a collection parameter is additionally provided, this script will check the user's groups
|
---|
39 | * to see if any of these allow the user to edit that collection.
|
---|
40 | *
|
---|
41 | * Run as java org.greenstone.gsdl3.util.ServletRealmCheck <GSDL3HOME> <un> <pwd> [colname]
|
---|
42 | *
|
---|
43 | * >java -classpath "greenstone3\web\WEB-INF\lib\gsdl3.jar;greenstone3\web\WEB-INF\lib\gutil.jar"
|
---|
44 | * org.greenstone.gsdl3.util.ServletRealmCheck "greenstone3\web" <un> <pw> [colname] 2>&1
|
---|
45 | *
|
---|
46 | * Tries URL: http://hostname:port/context/library?a=s&sa=authenticated-ping&excerptid=gs_content&un=<un>&pw=<pw>[&col=colname]
|
---|
47 | * The &excerptid=gs_content in the URL will return just the <div id="gs_content" /> part of the
|
---|
48 | * page that we're interested in.
|
---|
49 | *
|
---|
50 | * Result: either prints out an error message ("Authentication failed...") or a positive result,
|
---|
51 | * which is the user's groups. For the admin user example: administrator,all-collections-editor.
|
---|
52 | * If no collection is specified, will print the user groups.
|
---|
53 | * If a collection is specified, will only print user groups if the user has access to the collection.
|
---|
54 | *
|
---|
55 | */
|
---|
56 | public class ServletRealmCheck
|
---|
57 | {
|
---|
58 | public static void main(String[] args) {
|
---|
59 |
|
---|
60 | if (args.length < 3 || args.length > 4){
|
---|
61 | System.out.println("Run with: <GSDL3HOME> <un> <pwd> [collection-name]");
|
---|
62 | System.exit(0);
|
---|
63 | }
|
---|
64 |
|
---|
65 | String gsdl3home = args[0];
|
---|
66 | String username = args[1];
|
---|
67 | String password = args[2];
|
---|
68 | String collection = (args.length > 3) ? args[3] : null;
|
---|
69 |
|
---|
70 | //System.err.println("gsdl3srchome: " + gsdl3srchome);
|
---|
71 | //System.err.println("username: " + username);
|
---|
72 | //System.err.println("password: " + password);
|
---|
73 | //System.err.println("collection: " + collection);
|
---|
74 |
|
---|
75 |
|
---|
76 | // Load the global.properties file, get the GS3 server URL and send authenticated-ping and print the return result
|
---|
77 |
|
---|
78 | //http://www.mkyong.com/java/java-properties-file-examples/
|
---|
79 | Properties globalProps = new Properties();
|
---|
80 | InputStream input = null;
|
---|
81 |
|
---|
82 | try {
|
---|
83 | File globalPropsFile = new File(gsdl3home, "WEB-INF" +File.separatorChar+ "classes"+ File.separatorChar+"global.properties");
|
---|
84 | input = new FileInputStream(globalPropsFile);
|
---|
85 |
|
---|
86 | // load a properties file
|
---|
87 | globalProps.load(input);
|
---|
88 |
|
---|
89 | // get the property value and print it out
|
---|
90 | String servername = globalProps.getProperty("tomcat.server");
|
---|
91 | ProtocolPortProperties protocolPortProps = new ProtocolPortProperties(globalProps); // can throw Exception
|
---|
92 | String protocol = protocolPortProps.getProtocol();
|
---|
93 | String port = protocolPortProps.getPort();
|
---|
94 | int portNum = Integer.parseInt(port);
|
---|
95 |
|
---|
96 | String context = globalProps.getProperty("tomcat.context");
|
---|
97 | // Appending &excerptid=gs_content will get just the <div ... id="gs_content"/> from the final web page:
|
---|
98 | // TODO dynamically get library name
|
---|
99 | String urlSuffix = "/"+context+"/library?a=s&sa=authenticated-ping&excerptid=gs_content&un="+username+"&pw="+password;
|
---|
100 | if(collection != null) {
|
---|
101 | urlSuffix = urlSuffix + "&col="+collection;
|
---|
102 | }
|
---|
103 | URL authenticationUrl = new URL(protocol, servername, portNum, urlSuffix);
|
---|
104 |
|
---|
105 | HttpURLConnection conn = (HttpURLConnection)authenticationUrl.openConnection();
|
---|
106 | BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
---|
107 | String result = "";
|
---|
108 | String line = null;
|
---|
109 |
|
---|
110 | while((line = reader.readLine()) != null) {
|
---|
111 | result += line;
|
---|
112 | }
|
---|
113 |
|
---|
114 | //System.err.println("** Sent: " + authenticationUrl);
|
---|
115 | //System.err.println("** Got result:\n" + result);
|
---|
116 |
|
---|
117 | // Parse out the content nested inside <div ... id="gs_content"> </div>
|
---|
118 | int start = result.indexOf("id=\"gs_content\"");
|
---|
119 | if(start != -1) {
|
---|
120 | start = result.indexOf(">", start);
|
---|
121 | int end = result.indexOf("<", start);
|
---|
122 | result = result.substring(start+1, end);
|
---|
123 | result = result.trim();
|
---|
124 | }
|
---|
125 |
|
---|
126 | // Now we finally have what we actually want to print out for the caller to use
|
---|
127 | System.out.print(result + ","); // don't add newline to end
|
---|
128 |
|
---|
129 | } catch (Exception ex) {
|
---|
130 | System.err.println("Authentication failed: Java error: " + ex.getMessage());
|
---|
131 | ex.printStackTrace();
|
---|
132 | } finally {
|
---|
133 | if (input != null) {
|
---|
134 | try {
|
---|
135 | input.close();
|
---|
136 | } catch (IOException e) {
|
---|
137 | e.printStackTrace();
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | }
|
---|
143 |
|
---|
144 | }
|
---|