jdk/src/jdk.security.auth/solaris/native/libjaas/Solaris.c
author erikj
Mon, 26 Jan 2015 10:24:25 +0100
changeset 28658 344426303820
parent 25859 jdk/src/jdk.security.auth/unix/native/libjaas/Solaris.c@3317bb8137f4
child 40182 1e16a8fd76ba
permissions -rw-r--r--
8055190: Cleanup include and exclude of core-libs native libraries after source code restructure Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 22658
diff changeset
     2
 * Copyright (c) 2000, 2013, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
#include <jni.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "com_sun_security_auth_module_SolarisSystem.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <pwd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <pwd.h>
16488
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 5506
diff changeset
    34
22648
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
    35
static void throwIllegalArgumentException(JNIEnv *env, const char *msg) {
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
    36
    jclass clazz = (*env)->FindClass(env, "java/lang/IllegalArgumentException");
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
    37
    if (clazz != NULL)
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
    38
        (*env)->ThrowNew(env, clazz, msg);
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
    39
}
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
Java_com_sun_security_auth_module_SolarisSystem_getSolarisInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                                                (JNIEnv *env, jobject obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    char pwd_buf[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    struct passwd pwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    jsize numSuppGroups = getgroups(0, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    jstring jstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    jlongArray jgroups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    jlong *jgroupsAsArray;
16488
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 5506
diff changeset
    53
    gid_t *groups;
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 5506
diff changeset
    54
    jclass cls;
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 5506
diff changeset
    55
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 5506
diff changeset
    56
    groups = (gid_t *)calloc(numSuppGroups, sizeof(gid_t));
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 5506
diff changeset
    57
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 5506
diff changeset
    58
    if (groups == NULL) {
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 5506
diff changeset
    59
        jclass cls = (*env)->FindClass(env,"java/lang/OutOfMemoryError");
22648
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
    60
        if (cls != NULL)
16488
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 5506
diff changeset
    61
            (*env)->ThrowNew(env, cls, NULL);
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 5506
diff changeset
    62
        return;
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 5506
diff changeset
    63
    }
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 5506
diff changeset
    64
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 5506
diff changeset
    65
    cls = (*env)->GetObjectClass(env, obj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    memset(pwd_buf, 0, sizeof(pwd_buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    if (getpwuid_r(getuid(), &pwd, pwd_buf, sizeof(pwd_buf)) != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        getgroups(numSuppGroups, groups) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         * set username
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        fid = (*env)->GetFieldID(env, cls, "username", "Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (fid == 0) {
22648
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
    76
            (*env)->ExceptionClear(env);
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
    77
            throwIllegalArgumentException(env, "invalid field: username");
22658
30164afa0211 8025669: [parfait] warning from b62 for jdk.src.solaris.native.com.sun.security.auth.module: memory leak
vinnie
parents: 22648
diff changeset
    78
            goto cleanupAndReturn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        jstr = (*env)->NewStringUTF(env, pwd.pw_name);
22658
30164afa0211 8025669: [parfait] warning from b62 for jdk.src.solaris.native.com.sun.security.auth.module: memory leak
vinnie
parents: 22648
diff changeset
    81
        if (jstr == NULL) {
30164afa0211 8025669: [parfait] warning from b62 for jdk.src.solaris.native.com.sun.security.auth.module: memory leak
vinnie
parents: 22648
diff changeset
    82
            goto cleanupAndReturn;
30164afa0211 8025669: [parfait] warning from b62 for jdk.src.solaris.native.com.sun.security.auth.module: memory leak
vinnie
parents: 22648
diff changeset
    83
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        (*env)->SetObjectField(env, obj, fid, jstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         * set uid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        fid = (*env)->GetFieldID(env, cls, "uid", "J");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (fid == 0) {
22648
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
    91
            (*env)->ExceptionClear(env);
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
    92
            throwIllegalArgumentException(env, "invalid field: uid");
22658
30164afa0211 8025669: [parfait] warning from b62 for jdk.src.solaris.native.com.sun.security.auth.module: memory leak
vinnie
parents: 22648
diff changeset
    93
            goto cleanupAndReturn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        (*env)->SetLongField(env, obj, fid, pwd.pw_uid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         * set gid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        fid = (*env)->GetFieldID(env, cls, "gid", "J");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (fid == 0) {
22648
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
   102
            (*env)->ExceptionClear(env);
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
   103
            throwIllegalArgumentException(env, "invalid field: gid");
22658
30164afa0211 8025669: [parfait] warning from b62 for jdk.src.solaris.native.com.sun.security.auth.module: memory leak
vinnie
parents: 22648
diff changeset
   104
            goto cleanupAndReturn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        (*env)->SetLongField(env, obj, fid, pwd.pw_gid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         * set supplementary groups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        fid = (*env)->GetFieldID(env, cls, "groups", "[J");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (fid == 0) {
22648
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
   113
            (*env)->ExceptionClear(env);
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
   114
            throwIllegalArgumentException(env, "invalid field: groups");
22658
30164afa0211 8025669: [parfait] warning from b62 for jdk.src.solaris.native.com.sun.security.auth.module: memory leak
vinnie
parents: 22648
diff changeset
   115
            goto cleanupAndReturn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        jgroups = (*env)->NewLongArray(env, numSuppGroups);
22658
30164afa0211 8025669: [parfait] warning from b62 for jdk.src.solaris.native.com.sun.security.auth.module: memory leak
vinnie
parents: 22648
diff changeset
   119
        if (jgroups == NULL) {
30164afa0211 8025669: [parfait] warning from b62 for jdk.src.solaris.native.com.sun.security.auth.module: memory leak
vinnie
parents: 22648
diff changeset
   120
            goto cleanupAndReturn;
30164afa0211 8025669: [parfait] warning from b62 for jdk.src.solaris.native.com.sun.security.auth.module: memory leak
vinnie
parents: 22648
diff changeset
   121
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        jgroupsAsArray = (*env)->GetLongArrayElements(env, jgroups, 0);
22658
30164afa0211 8025669: [parfait] warning from b62 for jdk.src.solaris.native.com.sun.security.auth.module: memory leak
vinnie
parents: 22648
diff changeset
   123
        if (jgroupsAsArray == NULL) {
30164afa0211 8025669: [parfait] warning from b62 for jdk.src.solaris.native.com.sun.security.auth.module: memory leak
vinnie
parents: 22648
diff changeset
   124
            goto cleanupAndReturn;
30164afa0211 8025669: [parfait] warning from b62 for jdk.src.solaris.native.com.sun.security.auth.module: memory leak
vinnie
parents: 22648
diff changeset
   125
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        for (i = 0; i < numSuppGroups; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            jgroupsAsArray[i] = groups[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        (*env)->ReleaseLongArrayElements(env, jgroups, jgroupsAsArray, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        (*env)->SetObjectField(env, obj, fid, jgroups);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
22658
30164afa0211 8025669: [parfait] warning from b62 for jdk.src.solaris.native.com.sun.security.auth.module: memory leak
vinnie
parents: 22648
diff changeset
   131
cleanupAndReturn:
30164afa0211 8025669: [parfait] warning from b62 for jdk.src.solaris.native.com.sun.security.auth.module: memory leak
vinnie
parents: 22648
diff changeset
   132
    free(groups);
30164afa0211 8025669: [parfait] warning from b62 for jdk.src.solaris.native.com.sun.security.auth.module: memory leak
vinnie
parents: 22648
diff changeset
   133
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}