blob: e212da7d5c3512692ec321c24827ef6a0d4d89be [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.security.jgss.wrapper;
27
28import java.util.HashMap;
29import java.security.Provider;
30import java.security.AccessController;
31import java.security.PrivilegedAction;
32import org.ietf.jgss.Oid;
33import sun.security.action.PutAllAction;
34
35/**
36 * Defines the Sun NativeGSS provider for plugging in the
37 * native GSS mechanisms to Java GSS.
38 *
39 * List of supported mechanisms depends on the local
40 * machine configuration.
41 *
42 * @author Yu-Ching Valerie Peng
43 */
44
45public final class SunNativeProvider extends Provider {
46
47 private static final long serialVersionUID = -238911724858694204L;
48
49 private static final String NAME = "SunNativeGSS";
50 private static final String INFO = "Sun Native GSS provider";
51 private static final String MF_CLASS =
52 "sun.security.jgss.wrapper.NativeGSSFactory";
53 private static final String LIB_PROP = "sun.security.jgss.lib";
54 private static final String DEBUG_PROP = "sun.security.nativegss.debug";
55 private static HashMap MECH_MAP;
56 static final Provider INSTANCE = new SunNativeProvider();
57 static boolean DEBUG;
58 static void debug(String message) {
59 if (DEBUG) {
60 if (message == null) {
61 throw new NullPointerException();
62 }
63 System.out.println(NAME + ": " + message);
64 }
65 }
66
67 static {
68 MECH_MAP =
69 AccessController.doPrivileged(new PrivilegedAction<HashMap>() {
70 public HashMap run() {
71 DEBUG = Boolean.parseBoolean
72 (System.getProperty(DEBUG_PROP));
73 try {
74 System.loadLibrary("j2gss");
75 } catch (Error err) {
76 debug("No j2gss library found!");
77 if (DEBUG) err.printStackTrace();
78 return null;
79 }
80 String gssLib = System.getProperty(LIB_PROP);
81 if (gssLib == null || gssLib.trim().equals("")) {
82 String osname = System.getProperty("os.name");
83 if (osname.startsWith("SunOS")) {
84 gssLib = "libgss.so";
85 } else if (osname.startsWith("Linux")) {
86 gssLib = "libgssapi.so";
87 }
88 }
89 if (GSSLibStub.init(gssLib)) {
90 debug("Loaded GSS library: " + gssLib);
91 Oid[] mechs = GSSLibStub.indicateMechs();
92 HashMap<String, String> map =
93 new HashMap<String, String>();
94 for (int i = 0; i < mechs.length; i++) {
95 debug("Native MF for " + mechs[i]);
96 map.put("GssApiMechanism." + mechs[i],
97 MF_CLASS);
98 }
99 return map;
100 }
101 return null;
102 }
103 });
104 }
105
106 public SunNativeProvider() {
107 /* We are the Sun NativeGSS provider */
108 super(NAME, 1.0, INFO);
109
110 if (MECH_MAP != null) {
111 AccessController.doPrivileged(new PutAllAction(this, MECH_MAP));
112 }
113 }
114}