Debian Codegraph @

ssl-proxies/src/main/java/org/globus/util/ConfigUtil.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15 package org.globus.util;
16
17 import java.io.IOException;
18 import java.io.File;
19 import java.io.BufferedReader;
20 import java.io.InputStreamReader;
21
22 public class ConfigUtil {
23
24 public static final int UNDEFINED_OS = -1;
25 public static final int WINDOWS_OS = 0;
26 public static final int UNIX_OS = 1;
27 public static final int MAC_OS = 2;
28 public static final int OTHER_OS = 3;
29
30 private static int osType = UNDEFINED_OS;
31
32 private static final String PROXY_NAME = "x509up_u";
33
34 private static final String SOLARIS_ID_EXEC =
35 "/usr/xpg4/bin/id";
36
37 public static String globus_dir = null;
38
39 static {
40 globus_dir = System.getProperty("user.home") +
41 File.separator +
42 ".globus" +
43 File.separator;
44 }
45
46 /** Returns default PKCS11 library name */
47 public static String discoverPKCS11LibName() {
48 return "dspkcs"; // use the ibutton library as the default for now
49 }
50
51 /** Returns default location of user cert file */
52 public static String discoverUserCertLocation() {
53 String location = null;
54 location = globus_dir + "usercert.pem";
55 return location;
56 }
57
58 /** Returns default location of user key file */
59 public static String discoverUserKeyLocation() {
60 String location = null;
61 location = globus_dir + "userkey.pem";
62 return location;
63 }
64
65 /**
66 * Tries to discover user proxy location.
67 * If a UID system property is set, and running on a Unix machine it
68 * returns /tmp/x509up_u${UID}. If any other machine then Unix, it returns
69 * ${tempdir}/x509up_u${UID}, where tempdir is a platform-specific
70 * temporary directory as indicated by the java.io.tmpdir system property.
71 * If a UID system property is not set, the username will be used instead
72 * of the UID. That is, it returns ${tempdir}/x509up_u_${username}
73 */
74 public static String discoverProxyLocation() {
75
76 String dir = null;
77
78 if (getOS() == UNIX_OS) {
79 dir = "/tmp/";
80 } else {
81 String tmpDir = System.getProperty("java.io.tmpdir");
82 dir = (tmpDir == null) ? globus_dir : tmpDir;
83 }
84
85 String uid = System.getProperty("UID");
86
87 if (uid != null) {
88 return getLocation(dir, PROXY_NAME + uid);
89 } else if (getOS() == UNIX_OS) {
90 try {
91 return getLocation(dir, PROXY_NAME + getUID());
92 } catch (IOException e) {
93 }
94 }
95
96 /* If all else fails use username */
97 String suffix = System.getProperty("user.name");
98 if (suffix != null) {
99 suffix = suffix.toLowerCase();
100 } else {
101 suffix = "nousername";
102 }
103
104 return getLocation(dir, PROXY_NAME + "_" + suffix);
105 }
106
107 private static String getLocation(String dir, String file) {
108 File f = new File(dir, file);
109 return f.getAbsolutePath();
110 }
111
112 /**
113 * Returns the user id. The user id is obtained by executing 'id -u'
114 * external program.
115 * <BR><BR><B>Note: </B><I>
116 * Under some circumstances, this function executes an external program;
117 * thus, its behavior is influenced by environment variables such as the
118 * caller's PATH and the environment variables that control dynamic
119 * loading. Care should be used if calling this function from a program
120 * that will be run as a Unix setuid program, or in any other manner in
121 * which the owner of the Unix process does not completely control its
122 * runtime environment.
123 * </I>
124 *
125 * @throws IOException if unable to determine the user id.
126 * @return the user id
127 */
128 public static String getUID() throws IOException {
129 String exec = "id";
130 String osname = System.getProperty("os.name");
131 if (osname != null) {
132 osname = osname.toLowerCase();
133 if ((osname.indexOf("solaris") != -1) ||
134 (osname.indexOf("sunos") != -1)) {
135 if ((new File(SOLARIS_ID_EXEC).exists())) {
136 exec = SOLARIS_ID_EXEC;
137 }
138 } else if (osname.indexOf("windows") != -1) {
139 throw new IOException("Unable to determine the user id");
140 }
141 }
142
143 Runtime runTime = Runtime.getRuntime();
144 Process process = null;
145 BufferedReader buffInReader = null;
146 String s = null;
147 StringBuffer output = new StringBuffer();
148 int exitValue = -1;
149
150 try {
151 process = runTime.exec(exec + " -u");
152 buffInReader = new BufferedReader
153 ( new InputStreamReader(process.getInputStream()) );
154 while ((s = buffInReader.readLine()) != null) {
155 output.append(s);
156 }
157 exitValue = process.waitFor();
158 } catch (Exception e) {
159 throw new IOException("Unable to execute 'id -u'");
160 } finally {
161 if (buffInReader != null) {
162 try {
163 buffInReader.close();
164 } catch (IOException e) {}
165 }
166 if (process != null) {
167 try {
168 process.getErrorStream().close();
169 } catch (IOException e) {}
170 try {
171 process.getOutputStream().close();
172 } catch (IOException e) {}
173 }
174 }
175 if (exitValue != 0) {
176 throw new IOException("Unable to perform 'id -u'");
177 }
178 return output.toString().trim();
179 }
180
181 /**
182 * Discovers location of CA certificates directory.
183 * First the ${user.home}/.globus/certificates directory is checked.
184 * If the directory does not exist, and on a Unix machine, the
185 * /etc/grid-security/certificates directory is checked next.
186 * If that directory does not exist and GLOBUS_LOCATION
187 * system property is set then the ${GLOBUS_LOCATION}/share/certificates
188 * directory is checked. Otherwise, null is returned.
189 * This indicates that the certificates directory could
190 * not be found.
191 */
192 public static String discoverCertDirLocation() {
193 String location = null;
194
195 location = getDir(globus_dir + "certificates");
196 if (location != null) return location;
197
198 if (getOS() == UNIX_OS) {
199 location = getDir( "/etc/grid-security/certificates");
200 if (location != null) return location;
201 }
202
203 String suffix = File.separator + "share" + File.separator +
204 "certificates";
205
206 location = getDir(System.getProperty("GLOBUS_LOCATION") +
207 suffix);
208 if (location != null) return location;
209
210 return null;
211 }
212
213
214 public static int getOS() {
215 if (osType != UNDEFINED_OS) {
216 return osType;
217 }
218
219 String osname = System.getProperty("os.name");
220 if (osname != null) {
221 osname = osname.toLowerCase();
222 if (osname.indexOf("windows") != -1) {
223 osType = WINDOWS_OS;
224 } else if ( (osname.indexOf("solaris") != -1) ||
225 (osname.indexOf("sunos") != -1) ||
226 (osname.indexOf("linux") != -1) ||
227 (osname.indexOf("aix") != -1) ||
228 (osname.indexOf("hp-ux") != -1) ||
229 (osname.indexOf("compaq's digital unix") != -1) ||
230 (osname.indexOf("osf1") != -1) ||
231 (osname.indexOf("mac os x") != -1) ||
232 (osname.indexOf("netbsd") != -1) ||
233 (osname.indexOf("freebsd") != -1) ||
234 (osname.indexOf("irix") != -1) ) {
235 osType = UNIX_OS;
236 } else if (osname.indexOf("mac") != -1) {
237 osType = MAC_OS;
238 } else {
239 osType = OTHER_OS;
240 }
241 } else {
242 osType = OTHER_OS;
243 }
244
245 return osType;
246 }
247
248 private static String getDir(String directory) {
249 if (directory == null) return null;
250 File f = new File(directory);
251 if (f.isDirectory() && f.canRead()) {
252 return f.getAbsolutePath();
253 } else {
254 return null;
255 }
256 }
257
258 }