1 /*
2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle 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 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.
24  */
25 
26 #include <jni.h>
27 #include "com_sun_security_auth_module_SolarisSystem.h"
28 #include <stdio.h>
29 #include <pwd.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <pwd.h>
34 
throwIllegalArgumentException(JNIEnv * env,const char * msg)35 static void throwIllegalArgumentException(JNIEnv *env, const char *msg) {
36     jclass clazz = (*env)->FindClass(env, "java/lang/IllegalArgumentException");
37     if (clazz != NULL)
38         (*env)->ThrowNew(env, clazz, msg);
39 }
40 
41 JNIEXPORT void JNICALL
Java_com_sun_security_auth_module_SolarisSystem_getSolarisInfo(JNIEnv * env,jobject obj)42 Java_com_sun_security_auth_module_SolarisSystem_getSolarisInfo
43                                                 (JNIEnv *env, jobject obj) {
44 
45     int i;
46     char pwd_buf[1024];
47     struct passwd pwd;
48     jsize numSuppGroups = getgroups(0, NULL);
49     jfieldID fid;
50     jstring jstr;
51     jlongArray jgroups;
52     jlong *jgroupsAsArray;
53     gid_t *groups;
54     jclass cls;
55 
56     groups = (gid_t *)calloc(numSuppGroups, sizeof(gid_t));
57 
58     if (groups == NULL) {
59         jclass cls = (*env)->FindClass(env,"java/lang/OutOfMemoryError");
60         if (cls != NULL)
61             (*env)->ThrowNew(env, cls, NULL);
62         return;
63     }
64 
65     cls = (*env)->GetObjectClass(env, obj);
66 
67     memset(pwd_buf, 0, sizeof(pwd_buf));
68     if (getpwuid_r(getuid(), &pwd, pwd_buf, sizeof(pwd_buf)) != NULL &&
69         getgroups(numSuppGroups, groups) != -1) {
70 
71         /*
72          * set username
73          */
74         fid = (*env)->GetFieldID(env, cls, "username", "Ljava/lang/String;");
75         if (fid == 0) {
76             (*env)->ExceptionClear(env);
77             throwIllegalArgumentException(env, "invalid field: username");
78             return;
79         }
80         jstr = (*env)->NewStringUTF(env, pwd.pw_name);
81         if (jstr == NULL)
82             return;
83         (*env)->SetObjectField(env, obj, fid, jstr);
84 
85         /*
86          * set uid
87          */
88         fid = (*env)->GetFieldID(env, cls, "uid", "J");
89         if (fid == 0) {
90             (*env)->ExceptionClear(env);
91             throwIllegalArgumentException(env, "invalid field: uid");
92             return;
93         }
94         (*env)->SetLongField(env, obj, fid, pwd.pw_uid);
95 
96         /*
97          * set gid
98          */
99         fid = (*env)->GetFieldID(env, cls, "gid", "J");
100         if (fid == 0) {
101             (*env)->ExceptionClear(env);
102             throwIllegalArgumentException(env, "invalid field: gid");
103             return;
104         }
105         (*env)->SetLongField(env, obj, fid, pwd.pw_gid);
106 
107         /*
108          * set supplementary groups
109          */
110         fid = (*env)->GetFieldID(env, cls, "groups", "[J");
111         if (fid == 0) {
112             (*env)->ExceptionClear(env);
113             throwIllegalArgumentException(env, "invalid field: groups");
114             return;
115         }
116 
117         jgroups = (*env)->NewLongArray(env, numSuppGroups);
118         if (jgroups == NULL)
119             return;
120         jgroupsAsArray = (*env)->GetLongArrayElements(env, jgroups, 0);
121         if (jgroupsAsArray == NULL)
122             return;
123         for (i = 0; i < numSuppGroups; i++)
124             jgroupsAsArray[i] = groups[i];
125         (*env)->ReleaseLongArrayElements(env, jgroups, jgroupsAsArray, 0);
126         (*env)->SetObjectField(env, obj, fid, jgroups);
127     }
128     return;
129 }
130