J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2002 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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 | JNIEXPORT void JNICALL |
| 35 | Java_com_sun_security_auth_module_SolarisSystem_getSolarisInfo |
| 36 | (JNIEnv *env, jobject obj) { |
| 37 | |
| 38 | int i; |
| 39 | char pwd_buf[1024]; |
| 40 | struct passwd pwd; |
| 41 | jsize numSuppGroups = getgroups(0, NULL); |
| 42 | gid_t *groups = (gid_t *)calloc(numSuppGroups, sizeof(gid_t)); |
| 43 | |
| 44 | jfieldID fid; |
| 45 | jstring jstr; |
| 46 | jlongArray jgroups; |
| 47 | jlong *jgroupsAsArray; |
| 48 | jclass cls = (*env)->GetObjectClass(env, obj); |
| 49 | |
| 50 | memset(pwd_buf, 0, sizeof(pwd_buf)); |
| 51 | if (getpwuid_r(getuid(), &pwd, pwd_buf, sizeof(pwd_buf)) != NULL && |
| 52 | getgroups(numSuppGroups, groups) != -1) { |
| 53 | |
| 54 | /* |
| 55 | * set username |
| 56 | */ |
| 57 | fid = (*env)->GetFieldID(env, cls, "username", "Ljava/lang/String;"); |
| 58 | if (fid == 0) { |
| 59 | jclass newExcCls = |
| 60 | (*env)->FindClass(env, "java/lang/IllegalArgumentException"); |
| 61 | if (newExcCls == 0) { |
| 62 | /* Unable to find the new exception class, give up. */ |
| 63 | return; |
| 64 | } |
| 65 | (*env)->ThrowNew(env, newExcCls, "invalid field: username"); |
| 66 | } |
| 67 | jstr = (*env)->NewStringUTF(env, pwd.pw_name); |
| 68 | (*env)->SetObjectField(env, obj, fid, jstr); |
| 69 | |
| 70 | /* |
| 71 | * set uid |
| 72 | */ |
| 73 | fid = (*env)->GetFieldID(env, cls, "uid", "J"); |
| 74 | if (fid == 0) { |
| 75 | jclass newExcCls = |
| 76 | (*env)->FindClass(env, "java/lang/IllegalArgumentException"); |
| 77 | if (newExcCls == 0) { |
| 78 | /* Unable to find the new exception class, give up. */ |
| 79 | return; |
| 80 | } |
| 81 | (*env)->ThrowNew(env, newExcCls, "invalid field: username"); |
| 82 | } |
| 83 | (*env)->SetLongField(env, obj, fid, pwd.pw_uid); |
| 84 | |
| 85 | /* |
| 86 | * set gid |
| 87 | */ |
| 88 | fid = (*env)->GetFieldID(env, cls, "gid", "J"); |
| 89 | if (fid == 0) { |
| 90 | jclass newExcCls = |
| 91 | (*env)->FindClass(env, "java/lang/IllegalArgumentException"); |
| 92 | if (newExcCls == 0) { |
| 93 | /* Unable to find the new exception class, give up. */ |
| 94 | return; |
| 95 | } |
| 96 | (*env)->ThrowNew(env, newExcCls, "invalid field: username"); |
| 97 | } |
| 98 | (*env)->SetLongField(env, obj, fid, pwd.pw_gid); |
| 99 | |
| 100 | /* |
| 101 | * set supplementary groups |
| 102 | */ |
| 103 | fid = (*env)->GetFieldID(env, cls, "groups", "[J"); |
| 104 | if (fid == 0) { |
| 105 | jclass newExcCls = |
| 106 | (*env)->FindClass(env, "java/lang/IllegalArgumentException"); |
| 107 | if (newExcCls == 0) { |
| 108 | /* Unable to find the new exception class, give up. */ |
| 109 | return; |
| 110 | } |
| 111 | (*env)->ThrowNew(env, newExcCls, "invalid field: username"); |
| 112 | } |
| 113 | |
| 114 | jgroups = (*env)->NewLongArray(env, numSuppGroups); |
| 115 | jgroupsAsArray = (*env)->GetLongArrayElements(env, jgroups, 0); |
| 116 | for (i = 0; i < numSuppGroups; i++) |
| 117 | jgroupsAsArray[i] = groups[i]; |
| 118 | (*env)->ReleaseLongArrayElements(env, jgroups, jgroupsAsArray, 0); |
| 119 | (*env)->SetObjectField(env, obj, fid, jgroups); |
| 120 | } |
| 121 | return; |
| 122 | } |