jdk/src/jdk.security.auth/unix/native/libjaas/Unix.c
author bobv
Mon, 19 Oct 2015 13:41:09 -0400
changeset 33653 c1ee09fe3274
parent 25859 3317bb8137f4
child 40182 1e16a8fd76ba
permissions -rw-r--r--
8136556: Add the ability to perform static builds of MacOSX x64 binaries Reviewed-by: ihse, bdelsart, gadams, lfoltan, rriggs, hseigel, twisti
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
     2
 * Copyright (c) 2000, 2015, 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
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#define _POSIX_C_SOURCE 199506L
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <jni.h>
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
    31
#include "jni_util.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "com_sun_security_auth_module_UnixSystem.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <pwd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
    40
/*
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
    41
 * Declare library specific JNI_Onload entry if static build
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
    42
 */
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
    43
DEF_STATIC_JNI_OnLoad
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
    44
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
Java_com_sun_security_auth_module_UnixSystem_getUnixInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                                                (JNIEnv *env, jobject obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    char pwd_buf[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    struct passwd *pwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    struct passwd resbuf;
14203
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    53
    jfieldID userNameID;
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    54
    jfieldID userID;
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    55
    jfieldID groupID;
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    56
    jfieldID supplementaryGroupID;
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    57
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    jstring jstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    jlongArray jgroups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    jlong *jgroupsAsArray;
16488
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 14342
diff changeset
    61
    jsize numSuppGroups;
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 14342
diff changeset
    62
    gid_t *groups;
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 14342
diff changeset
    63
    jclass cls;
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 14342
diff changeset
    64
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 14342
diff changeset
    65
    numSuppGroups = getgroups(0, NULL);
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 14342
diff changeset
    66
    groups = (gid_t *)calloc(numSuppGroups, sizeof(gid_t));
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 14342
diff changeset
    67
    if (groups == NULL) {
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 14342
diff changeset
    68
        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
    69
        if (cls != NULL)
16488
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 14342
diff changeset
    70
            (*env)->ThrowNew(env, cls, NULL);
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 14342
diff changeset
    71
        return;
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 14342
diff changeset
    72
    }
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 14342
diff changeset
    73
816b9df68a9f 8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents: 14342
diff changeset
    74
    cls = (*env)->GetObjectClass(env, obj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    memset(pwd_buf, 0, sizeof(pwd_buf));
14203
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    77
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    if (getpwuid_r(getuid(), &resbuf, pwd_buf, sizeof(pwd_buf), &pwd) == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        pwd != NULL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        getgroups(numSuppGroups, groups) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
14203
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    82
        userNameID = (*env)->GetFieldID(env, cls, "username", "Ljava/lang/String;");
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    83
        if (userNameID == 0)
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    84
            goto cleanUpAndReturn;
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    85
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    86
        userID = (*env)->GetFieldID(env, cls, "uid", "J");
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    87
        if (userID == 0)
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    88
            goto cleanUpAndReturn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
14203
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    90
        groupID = (*env)->GetFieldID(env, cls, "gid", "J");
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    91
        if (groupID == 0)
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    92
            goto cleanUpAndReturn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
14203
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    94
        supplementaryGroupID = (*env)->GetFieldID(env, cls, "groups", "[J");
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    95
        if (supplementaryGroupID == 0)
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    96
            goto cleanUpAndReturn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
14203
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
    98
        jstr = (*env)->NewStringUTF(env, pwd->pw_name);
22648
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
    99
        if (jstr == NULL)
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
   100
            goto cleanUpAndReturn;
14203
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
   101
        (*env)->SetObjectField(env, obj, userNameID, jstr);
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
   102
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
   103
        (*env)->SetLongField(env, obj, userID, pwd->pw_uid);
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
   104
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
   105
        (*env)->SetLongField(env, obj, groupID, pwd->pw_gid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        jgroups = (*env)->NewLongArray(env, numSuppGroups);
22648
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
   108
        if (jgroups == NULL)
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
   109
            goto cleanUpAndReturn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        jgroupsAsArray = (*env)->GetLongArrayElements(env, jgroups, 0);
22648
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
   111
        if (jgroupsAsArray == NULL)
7bcdb55f70ff 8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents: 16488
diff changeset
   112
            goto cleanUpAndReturn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        for (i = 0; i < numSuppGroups; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            jgroupsAsArray[i] = groups[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        (*env)->ReleaseLongArrayElements(env, jgroups, jgroupsAsArray, 0);
14203
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
   116
        (*env)->SetObjectField(env, obj, supplementaryGroupID, jgroups);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
14203
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
   118
cleanUpAndReturn:
d74a31d40678 8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents: 5506
diff changeset
   119
    free(groups);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
}