jdk/src/solaris/native/com/sun/security/auth/module/Solaris.c
changeset 22648 7bcdb55f70ff
parent 16488 816b9df68a9f
child 22658 30164afa0211
equal deleted inserted replaced
22647:7e45e0a1088d 22648:7bcdb55f70ff
    30 #include <unistd.h>
    30 #include <unistd.h>
    31 #include <stdlib.h>
    31 #include <stdlib.h>
    32 #include <string.h>
    32 #include <string.h>
    33 #include <pwd.h>
    33 #include <pwd.h>
    34 
    34 
       
    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 
    35 JNIEXPORT void JNICALL
    41 JNIEXPORT void JNICALL
    36 Java_com_sun_security_auth_module_SolarisSystem_getSolarisInfo
    42 Java_com_sun_security_auth_module_SolarisSystem_getSolarisInfo
    37                                                 (JNIEnv *env, jobject obj) {
    43                                                 (JNIEnv *env, jobject obj) {
    38 
    44 
    39     int i;
    45     int i;
    49 
    55 
    50     groups = (gid_t *)calloc(numSuppGroups, sizeof(gid_t));
    56     groups = (gid_t *)calloc(numSuppGroups, sizeof(gid_t));
    51 
    57 
    52     if (groups == NULL) {
    58     if (groups == NULL) {
    53         jclass cls = (*env)->FindClass(env,"java/lang/OutOfMemoryError");
    59         jclass cls = (*env)->FindClass(env,"java/lang/OutOfMemoryError");
    54         if(cls != 0)
    60         if (cls != NULL)
    55             (*env)->ThrowNew(env, cls, NULL);
    61             (*env)->ThrowNew(env, cls, NULL);
    56         return;
    62         return;
    57     }
    63     }
    58 
    64 
    59     cls = (*env)->GetObjectClass(env, obj);
    65     cls = (*env)->GetObjectClass(env, obj);
    65         /*
    71         /*
    66          * set username
    72          * set username
    67          */
    73          */
    68         fid = (*env)->GetFieldID(env, cls, "username", "Ljava/lang/String;");
    74         fid = (*env)->GetFieldID(env, cls, "username", "Ljava/lang/String;");
    69         if (fid == 0) {
    75         if (fid == 0) {
    70             jclass newExcCls =
    76             (*env)->ExceptionClear(env);
    71                 (*env)->FindClass(env, "java/lang/IllegalArgumentException");
    77             throwIllegalArgumentException(env, "invalid field: username");
    72             if (newExcCls == 0) {
    78             return;
    73                 /* Unable to find the new exception class, give up. */
       
    74                 return;
       
    75             }
       
    76             (*env)->ThrowNew(env, newExcCls, "invalid field: username");
       
    77         }
    79         }
    78         jstr = (*env)->NewStringUTF(env, pwd.pw_name);
    80         jstr = (*env)->NewStringUTF(env, pwd.pw_name);
       
    81         if (jstr == NULL)
       
    82             return;
    79         (*env)->SetObjectField(env, obj, fid, jstr);
    83         (*env)->SetObjectField(env, obj, fid, jstr);
    80 
    84 
    81         /*
    85         /*
    82          * set uid
    86          * set uid
    83          */
    87          */
    84         fid = (*env)->GetFieldID(env, cls, "uid", "J");
    88         fid = (*env)->GetFieldID(env, cls, "uid", "J");
    85         if (fid == 0) {
    89         if (fid == 0) {
    86             jclass newExcCls =
    90             (*env)->ExceptionClear(env);
    87                 (*env)->FindClass(env, "java/lang/IllegalArgumentException");
    91             throwIllegalArgumentException(env, "invalid field: uid");
    88             if (newExcCls == 0) {
    92             return;
    89                 /* Unable to find the new exception class, give up. */
       
    90                 return;
       
    91             }
       
    92             (*env)->ThrowNew(env, newExcCls, "invalid field: username");
       
    93         }
    93         }
    94         (*env)->SetLongField(env, obj, fid, pwd.pw_uid);
    94         (*env)->SetLongField(env, obj, fid, pwd.pw_uid);
    95 
    95 
    96         /*
    96         /*
    97          * set gid
    97          * set gid
    98          */
    98          */
    99         fid = (*env)->GetFieldID(env, cls, "gid", "J");
    99         fid = (*env)->GetFieldID(env, cls, "gid", "J");
   100         if (fid == 0) {
   100         if (fid == 0) {
   101             jclass newExcCls =
   101             (*env)->ExceptionClear(env);
   102                 (*env)->FindClass(env, "java/lang/IllegalArgumentException");
   102             throwIllegalArgumentException(env, "invalid field: gid");
   103             if (newExcCls == 0) {
   103             return;
   104                 /* Unable to find the new exception class, give up. */
       
   105                 return;
       
   106             }
       
   107             (*env)->ThrowNew(env, newExcCls, "invalid field: username");
       
   108         }
   104         }
   109         (*env)->SetLongField(env, obj, fid, pwd.pw_gid);
   105         (*env)->SetLongField(env, obj, fid, pwd.pw_gid);
   110 
   106 
   111         /*
   107         /*
   112          * set supplementary groups
   108          * set supplementary groups
   113          */
   109          */
   114         fid = (*env)->GetFieldID(env, cls, "groups", "[J");
   110         fid = (*env)->GetFieldID(env, cls, "groups", "[J");
   115         if (fid == 0) {
   111         if (fid == 0) {
   116             jclass newExcCls =
   112             (*env)->ExceptionClear(env);
   117                 (*env)->FindClass(env, "java/lang/IllegalArgumentException");
   113             throwIllegalArgumentException(env, "invalid field: groups");
   118             if (newExcCls == 0) {
   114             return;
   119                 /* Unable to find the new exception class, give up. */
       
   120                 return;
       
   121             }
       
   122             (*env)->ThrowNew(env, newExcCls, "invalid field: username");
       
   123         }
   115         }
   124 
   116 
   125         jgroups = (*env)->NewLongArray(env, numSuppGroups);
   117         jgroups = (*env)->NewLongArray(env, numSuppGroups);
       
   118         if (jgroups == NULL)
       
   119             return;
   126         jgroupsAsArray = (*env)->GetLongArrayElements(env, jgroups, 0);
   120         jgroupsAsArray = (*env)->GetLongArrayElements(env, jgroups, 0);
       
   121         if (jgroupsAsArray == NULL)
       
   122             return;
   127         for (i = 0; i < numSuppGroups; i++)
   123         for (i = 0; i < numSuppGroups; i++)
   128             jgroupsAsArray[i] = groups[i];
   124             jgroupsAsArray[i] = groups[i];
   129         (*env)->ReleaseLongArrayElements(env, jgroups, jgroupsAsArray, 0);
   125         (*env)->ReleaseLongArrayElements(env, jgroups, jgroupsAsArray, 0);
   130         (*env)->SetObjectField(env, obj, fid, jgroups);
   126         (*env)->SetObjectField(env, obj, fid, jgroups);
   131     }
   127     }