author | jlaskey |
Thu, 14 Nov 2019 12:33:47 -0400 | |
branch | jlaskey-prng |
changeset 59084 | b8fb85ee91e9 |
parent 51120 | dccdf51b10dd |
permissions | -rw-r--r-- |
2 | 1 |
/* |
51120 | 2 |
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
#include <jni.h> |
|
33653
c1ee09fe3274
8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents:
25859
diff
changeset
|
27 |
#include "jni_util.h" |
2 | 28 |
#include "com_sun_security_auth_module_UnixSystem.h" |
29 |
#include <stdio.h> |
|
30 |
#include <sys/types.h> |
|
31 |
#include <unistd.h> |
|
32 |
#include <stdlib.h> |
|
33 |
#include <string.h> |
|
34 |
||
40182
1e16a8fd76ba
8160997: Solaris: deprecated <pwd.h> and <gid.h> interfaces should be replaced
dcubed
parents:
33653
diff
changeset
|
35 |
/* For POSIX-compliant getpwuid_r on Solaris */ |
1e16a8fd76ba
8160997: Solaris: deprecated <pwd.h> and <gid.h> interfaces should be replaced
dcubed
parents:
33653
diff
changeset
|
36 |
#if defined(__solaris__) |
1e16a8fd76ba
8160997: Solaris: deprecated <pwd.h> and <gid.h> interfaces should be replaced
dcubed
parents:
33653
diff
changeset
|
37 |
#define _POSIX_PTHREAD_SEMANTICS |
1e16a8fd76ba
8160997: Solaris: deprecated <pwd.h> and <gid.h> interfaces should be replaced
dcubed
parents:
33653
diff
changeset
|
38 |
#endif |
1e16a8fd76ba
8160997: Solaris: deprecated <pwd.h> and <gid.h> interfaces should be replaced
dcubed
parents:
33653
diff
changeset
|
39 |
#include <pwd.h> |
1e16a8fd76ba
8160997: Solaris: deprecated <pwd.h> and <gid.h> interfaces should be replaced
dcubed
parents:
33653
diff
changeset
|
40 |
|
33653
c1ee09fe3274
8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents:
25859
diff
changeset
|
41 |
/* |
c1ee09fe3274
8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents:
25859
diff
changeset
|
42 |
* 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
|
43 |
*/ |
c1ee09fe3274
8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents:
25859
diff
changeset
|
44 |
DEF_STATIC_JNI_OnLoad |
c1ee09fe3274
8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents:
25859
diff
changeset
|
45 |
|
2 | 46 |
JNIEXPORT void JNICALL |
47 |
Java_com_sun_security_auth_module_UnixSystem_getUnixInfo |
|
48 |
(JNIEnv *env, jobject obj) { |
|
49 |
||
50 |
int i; |
|
51 |
char pwd_buf[1024]; |
|
52 |
struct passwd *pwd; |
|
53 |
struct passwd resbuf; |
|
14203
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
54 |
jfieldID userNameID; |
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
55 |
jfieldID userID; |
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
56 |
jfieldID groupID; |
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
57 |
jfieldID supplementaryGroupID; |
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
58 |
|
2 | 59 |
jstring jstr; |
60 |
jlongArray jgroups; |
|
61 |
jlong *jgroupsAsArray; |
|
16488
816b9df68a9f
8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents:
14342
diff
changeset
|
62 |
jsize numSuppGroups; |
816b9df68a9f
8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents:
14342
diff
changeset
|
63 |
gid_t *groups; |
816b9df68a9f
8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents:
14342
diff
changeset
|
64 |
jclass cls; |
816b9df68a9f
8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents:
14342
diff
changeset
|
65 |
|
816b9df68a9f
8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents:
14342
diff
changeset
|
66 |
numSuppGroups = getgroups(0, NULL); |
51120 | 67 |
if (numSuppGroups == -1) { |
68 |
return; |
|
69 |
} |
|
16488
816b9df68a9f
8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents:
14342
diff
changeset
|
70 |
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
|
71 |
if (groups == NULL) { |
816b9df68a9f
8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents:
14342
diff
changeset
|
72 |
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
|
73 |
if (cls != NULL) |
16488
816b9df68a9f
8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents:
14342
diff
changeset
|
74 |
(*env)->ThrowNew(env, cls, NULL); |
816b9df68a9f
8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents:
14342
diff
changeset
|
75 |
return; |
816b9df68a9f
8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents:
14342
diff
changeset
|
76 |
} |
816b9df68a9f
8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents:
14342
diff
changeset
|
77 |
|
816b9df68a9f
8007607: security native code doesn't always use malloc, realloc, and calloc correctly
jzavgren
parents:
14342
diff
changeset
|
78 |
cls = (*env)->GetObjectClass(env, obj); |
2 | 79 |
|
80 |
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
|
81 |
|
2 | 82 |
if (getpwuid_r(getuid(), &resbuf, pwd_buf, sizeof(pwd_buf), &pwd) == 0 && |
83 |
pwd != NULL && |
|
84 |
getgroups(numSuppGroups, groups) != -1) { |
|
85 |
||
14203
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
86 |
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
|
87 |
if (userNameID == 0) |
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
88 |
goto cleanUpAndReturn; |
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
89 |
|
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
90 |
userID = (*env)->GetFieldID(env, cls, "uid", "J"); |
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
91 |
if (userID == 0) |
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
92 |
goto cleanUpAndReturn; |
2 | 93 |
|
14203
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
94 |
groupID = (*env)->GetFieldID(env, cls, "gid", "J"); |
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
95 |
if (groupID == 0) |
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
96 |
goto cleanUpAndReturn; |
2 | 97 |
|
14203
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
98 |
supplementaryGroupID = (*env)->GetFieldID(env, cls, "groups", "[J"); |
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
99 |
if (supplementaryGroupID == 0) |
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
100 |
goto cleanUpAndReturn; |
2 | 101 |
|
14203
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
102 |
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
|
103 |
if (jstr == NULL) |
7bcdb55f70ff
8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents:
16488
diff
changeset
|
104 |
goto cleanUpAndReturn; |
14203
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
105 |
(*env)->SetObjectField(env, obj, userNameID, jstr); |
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
106 |
|
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
107 |
(*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
|
108 |
|
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
109 |
(*env)->SetLongField(env, obj, groupID, pwd->pw_gid); |
2 | 110 |
|
111 |
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
|
112 |
if (jgroups == NULL) |
7bcdb55f70ff
8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents:
16488
diff
changeset
|
113 |
goto cleanUpAndReturn; |
2 | 114 |
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
|
115 |
if (jgroupsAsArray == NULL) |
7bcdb55f70ff
8031586: Check jdk/src/*/native/com/sun/security/auth/module for pending JNI exceptions
alanb
parents:
16488
diff
changeset
|
116 |
goto cleanUpAndReturn; |
2 | 117 |
for (i = 0; i < numSuppGroups; i++) |
118 |
jgroupsAsArray[i] = groups[i]; |
|
119 |
(*env)->ReleaseLongArrayElements(env, jgroups, jgroupsAsArray, 0); |
|
14203
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
120 |
(*env)->SetObjectField(env, obj, supplementaryGroupID, jgroups); |
2 | 121 |
} |
14203
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
122 |
cleanUpAndReturn: |
d74a31d40678
8000204: Memory leak in com/sun/security/auth/module/Unix.c
chegar
parents:
5506
diff
changeset
|
123 |
free(groups); |
2 | 124 |
return; |
125 |
} |