jdk/src/share/classes/sun/security/jgss/wrapper/SunNativeProvider.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 5300 a291a0bff79f
child 8388 bc5ae489cd71
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5300
diff changeset
     2
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5300
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5300
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5300
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5300
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5300
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.jgss.wrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import org.ietf.jgss.Oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.action.PutAllAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Defines the Sun NativeGSS provider for plugging in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * native GSS mechanisms to Java GSS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * List of supported mechanisms depends on the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * machine configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Yu-Ching Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public final class SunNativeProvider extends Provider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final long serialVersionUID = -238911724858694204L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final String NAME = "SunNativeGSS";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final String INFO = "Sun Native GSS provider";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final String MF_CLASS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        "sun.security.jgss.wrapper.NativeGSSFactory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final String LIB_PROP = "sun.security.jgss.lib";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final String DEBUG_PROP = "sun.security.nativegss.debug";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static HashMap MECH_MAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static final Provider INSTANCE = new SunNativeProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static boolean DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static void debug(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (message == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            System.out.println(NAME + ": " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        MECH_MAP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            AccessController.doPrivileged(new PrivilegedAction<HashMap>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    public HashMap run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                        DEBUG = Boolean.parseBoolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                            (System.getProperty(DEBUG_PROP));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                            System.loadLibrary("j2gss");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                        } catch (Error err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                            debug("No j2gss library found!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                            if (DEBUG) err.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                        }
5300
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
    80
                        String gssLibs[] = new String[0];
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
    81
                        String defaultLib = System.getProperty(LIB_PROP);
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
    82
                        if (defaultLib == null || defaultLib.trim().equals("")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                            String osname = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                            if (osname.startsWith("SunOS")) {
5300
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
    85
                                gssLibs = new String[]{ "libgss.so" };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                            } else if (osname.startsWith("Linux")) {
5300
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
    87
                                gssLibs = new String[]{
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
    88
                                    "libgssapi.so",
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
    89
                                    "libgssapi_krb5.so",
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
    90
                                };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                            }
5300
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
    92
                        } else {
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
    93
                            gssLibs = new String[]{ defaultLib };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        }
5300
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
    95
                        for (String libName: gssLibs) {
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
    96
                            if (GSSLibStub.init(libName)) {
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
    97
                                debug("Loaded GSS library: " + libName);
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
    98
                                Oid[] mechs = GSSLibStub.indicateMechs();
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
    99
                                HashMap<String, String> map =
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
   100
                                            new HashMap<String, String>();
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
   101
                                for (int i = 0; i < mechs.length; i++) {
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
   102
                                    debug("Native MF for " + mechs[i]);
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
   103
                                    map.put("GssApiMechanism." + mechs[i],
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
   104
                                            MF_CLASS);
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
   105
                                }
a291a0bff79f 6944847: native gss lib names on linux
weijun
parents: 2
diff changeset
   106
                                return map;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public SunNativeProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        /* We are the Sun NativeGSS provider */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        super(NAME, 1.0, INFO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (MECH_MAP != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            AccessController.doPrivileged(new PutAllAction(this, MECH_MAP));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}