annotate src/solaris/native/com/sun/security/auth/module/Unix.c @ 6581:cea72c2bf071

7197491: update copyright year to match last edit in jdk8 jdk repository Reviewed-by: chegar, ksrini
author alanb
date Fri, 02 Nov 2012 15:50:11 +0000
parents 29b58cb8e4fc
children 4103eb6b2137
rev   line source
duke@0 1 /*
alanb@6581 2 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
ohair@2362 7 * published by the Free Software Foundation. Oracle designates this
duke@0 8 * particular file as subject to the "Classpath" exception as provided
ohair@2362 9 * by Oracle in the LICENSE file that accompanied this code.
duke@0 10 *
duke@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 15 * accompanied this code).
duke@0 16 *
duke@0 17 * You should have received a copy of the GNU General Public License version
duke@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 20 *
ohair@2362 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@2362 22 * or visit www.oracle.com if you need additional information or have any
ohair@2362 23 * questions.
duke@0 24 */
duke@0 25
duke@0 26 #ifdef __solaris__
duke@0 27 #define _POSIX_C_SOURCE 199506L
duke@0 28 #endif
duke@0 29
duke@0 30 #include <jni.h>
duke@0 31 #include "com_sun_security_auth_module_UnixSystem.h"
duke@0 32 #include <stdio.h>
duke@0 33 #include <pwd.h>
duke@0 34 #include <sys/types.h>
duke@0 35 #include <unistd.h>
duke@0 36 #include <stdlib.h>
duke@0 37 #include <string.h>
duke@0 38
duke@0 39 JNIEXPORT void JNICALL
duke@0 40 Java_com_sun_security_auth_module_UnixSystem_getUnixInfo
duke@0 41 (JNIEnv *env, jobject obj) {
duke@0 42
duke@0 43 int i;
duke@0 44 char pwd_buf[1024];
duke@0 45 struct passwd *pwd;
duke@0 46 struct passwd resbuf;
duke@0 47 jsize numSuppGroups = getgroups(0, NULL);
duke@0 48 gid_t *groups = (gid_t *)calloc(numSuppGroups, sizeof(gid_t));
duke@0 49
chegar@6505 50 jfieldID userNameID;
chegar@6505 51 jfieldID userID;
chegar@6505 52 jfieldID groupID;
chegar@6505 53 jfieldID supplementaryGroupID;
chegar@6505 54
duke@0 55 jstring jstr;
duke@0 56 jlongArray jgroups;
duke@0 57 jlong *jgroupsAsArray;
duke@0 58 jclass cls = (*env)->GetObjectClass(env, obj);
duke@0 59
duke@0 60 memset(pwd_buf, 0, sizeof(pwd_buf));
chegar@6505 61
duke@0 62 if (getpwuid_r(getuid(), &resbuf, pwd_buf, sizeof(pwd_buf), &pwd) == 0 &&
duke@0 63 pwd != NULL &&
duke@0 64 getgroups(numSuppGroups, groups) != -1) {
duke@0 65
chegar@6505 66 userNameID = (*env)->GetFieldID(env, cls, "username", "Ljava/lang/String;");
chegar@6505 67 if (userNameID == 0)
chegar@6505 68 goto cleanUpAndReturn;
chegar@6505 69
chegar@6505 70 userID = (*env)->GetFieldID(env, cls, "uid", "J");
chegar@6505 71 if (userID == 0)
chegar@6505 72 goto cleanUpAndReturn;
chegar@6505 73
chegar@6505 74 groupID = (*env)->GetFieldID(env, cls, "gid", "J");
chegar@6505 75 if (groupID == 0)
chegar@6505 76 goto cleanUpAndReturn;
chegar@6505 77
chegar@6505 78 supplementaryGroupID = (*env)->GetFieldID(env, cls, "groups", "[J");
chegar@6505 79 if (supplementaryGroupID == 0)
chegar@6505 80 goto cleanUpAndReturn;
chegar@6505 81
duke@0 82 jstr = (*env)->NewStringUTF(env, pwd->pw_name);
chegar@6505 83 (*env)->SetObjectField(env, obj, userNameID, jstr);
duke@0 84
chegar@6505 85 (*env)->SetLongField(env, obj, userID, pwd->pw_uid);
duke@0 86
chegar@6505 87 (*env)->SetLongField(env, obj, groupID, pwd->pw_gid);
duke@0 88
duke@0 89 jgroups = (*env)->NewLongArray(env, numSuppGroups);
duke@0 90 jgroupsAsArray = (*env)->GetLongArrayElements(env, jgroups, 0);
duke@0 91 for (i = 0; i < numSuppGroups; i++)
duke@0 92 jgroupsAsArray[i] = groups[i];
duke@0 93 (*env)->ReleaseLongArrayElements(env, jgroups, jgroupsAsArray, 0);
chegar@6505 94 (*env)->SetObjectField(env, obj, supplementaryGroupID, jgroups);
duke@0 95 }
chegar@6505 96 cleanUpAndReturn:
chegar@6505 97 free(groups);
duke@0 98 return;
duke@0 99 }