1 /*
   2  * Copyright (c) 2000, 2018, 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 "jni_util.h"
  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 
  35 #include <pwd.h>
  36 
  37 /*
  38  * Declare library specific JNI_Onload entry if static build
  39  */
  40 DEF_STATIC_JNI_OnLoad
  41 
  42 JNIEXPORT void JNICALL
  43 Java_com_sun_security_auth_module_UnixSystem_getUnixInfo
  44                                                 (JNIEnv *env, jobject obj) {
  45 
  46     int i;
  47     char pwd_buf[1024];
  48     struct passwd *pwd;
  49     struct passwd resbuf;
  50     jfieldID userNameID;
  51     jfieldID userID;
  52     jfieldID groupID;
  53     jfieldID supplementaryGroupID;
  54 
  55     jstring jstr;
  56     jlongArray jgroups;
  57     jlong *jgroupsAsArray;
  58     jsize numSuppGroups;
  59     gid_t *groups;
  60     jclass cls;
  61 
  62     numSuppGroups = getgroups(0, NULL);
  63     if (numSuppGroups == -1) {
  64         return;
  65     }
  66     groups = (gid_t *)calloc(numSuppGroups, sizeof(gid_t));
  67     if (groups == NULL) {
  68         jclass cls = (*env)->FindClass(env,"java/lang/OutOfMemoryError");
  69         if (cls != NULL)
  70             (*env)->ThrowNew(env, cls, NULL);
  71         return;
  72     }
  73 
  74     cls = (*env)->GetObjectClass(env, obj);
  75 
  76     memset(pwd_buf, 0, sizeof(pwd_buf));
  77 
  78     if (getpwuid_r(getuid(), &resbuf, pwd_buf, sizeof(pwd_buf), &pwd) == 0 &&
  79         pwd != NULL &&
  80         getgroups(numSuppGroups, groups) != -1) {
  81 
  82         userNameID = (*env)->GetFieldID(env, cls, "username", "Ljava/lang/String;");
  83         if (userNameID == 0)
  84             goto cleanUpAndReturn;
  85 
  86         userID = (*env)->GetFieldID(env, cls, "uid", "J");
  87         if (userID == 0)
  88             goto cleanUpAndReturn;
  89 
  90         groupID = (*env)->GetFieldID(env, cls, "gid", "J");
  91         if (groupID == 0)
  92             goto cleanUpAndReturn;
  93 
  94         supplementaryGroupID = (*env)->GetFieldID(env, cls, "groups", "[J");
  95         if (supplementaryGroupID == 0)
  96             goto cleanUpAndReturn;
  97 
  98         jstr = (*env)->NewStringUTF(env, pwd->pw_name);
  99         if (jstr == NULL)
 100             goto cleanUpAndReturn;
 101         (*env)->SetObjectField(env, obj, userNameID, jstr);
 102 
 103         (*env)->SetLongField(env, obj, userID, pwd->pw_uid);
 104 
 105         (*env)->SetLongField(env, obj, groupID, pwd->pw_gid);
 106 
 107         jgroups = (*env)->NewLongArray(env, numSuppGroups);
 108         if (jgroups == NULL)
 109             goto cleanUpAndReturn;
 110         jgroupsAsArray = (*env)->GetLongArrayElements(env, jgroups, 0);
 111         if (jgroupsAsArray == NULL)
 112             goto cleanUpAndReturn;
 113         for (i = 0; i < numSuppGroups; i++)
 114             jgroupsAsArray[i] = groups[i];
 115         (*env)->ReleaseLongArrayElements(env, jgroups, jgroupsAsArray, 0);
 116         (*env)->SetObjectField(env, obj, supplementaryGroupID, jgroups);
 117     }
 118 cleanUpAndReturn:
 119     free(groups);
 120     return;
 121 }