summaryrefslogtreecommitdiffstats
path: root/pki/base/osutil/src/com/netscape/osutil/UserID.c
blob: 456fe10fdfbe6313147a3a7c51d39940364892bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---

/*
 * Native wrappers for setuid/getuid
 */

#include <jni.h>
#include "com_netscape_osutil_UserID.h"
#include "unixdefs.h"
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <errno.h>
#include <stdlib.h>

/*
 * Throw an IllegalArgumentException 
 */
void
unix_throw_exception(JNIEnv *env, const char *exception, const char *reason)
{
	jclass exc;

	exc = (*env)->FindClass(env, exception);
	if (exc) /* If unable to find the new exception class, give up. */
		(*env)->ThrowNew(env, exc, reason);
}

/*
 * Convert a jstring name into a uid_t value
 */
static uid_t
name_to_uid(JNIEnv *env, jstring name) 
{
	const char *username = (*env)->GetStringUTFChars(env, name, 0);
	struct passwd *pw;
	int ret;

	if (NULL == username) {
		unix_throw_exception(env, ILLARG, "can't convert username");
		return -1;
	}

	pw = getpwnam(username);

	if (NULL == pw) {
		/* XXX I suppose the failed user name would be useful here */
		unix_throw_exception(env, ILLARG, "no such user");
		return -1;
	}

	ret = pw->pw_uid;
	free(pw);
	return ret;
}

/*
 * Class:     com_netscape_osutil_UserID
 * Method:    get
 * Signature: ()I
 */
JNIEXPORT jint JNICALL
Java_com_netscape_osutil_UserID_get(JNIEnv *env, jclass cls)
{
	return getuid();
}

/*
 * Class:     com_netscape_certsrv_unix_UserID
 * Method:    getEffective
 * Signature: ()I
 */
JNIEXPORT jint JNICALL
Java_com_netscape_osutil_UserID_getEffective(JNIEnv *env, jclass cls)
{
	return geteuid();
}

/*
 * Class:     com_netscape_certsrv_unix_UserID
 * Method:    set
 * Signature: (I)Z
 */
JNIEXPORT void JNICALL
Java_com_netscape_osutil_UserID_set__I(JNIEnv *env, jclass cls, jint id)
{
	int status = setuid(id);

	if (status != 0) {
		switch (errno) {
		case EINVAL:
			unix_throw_exception(env, ILLARG, "bad uid value");
			break;
		case EPERM:
			unix_throw_exception(env, SECURITY, "permission denied");
			break;
		} 
	}
}

/*
 * Class:     com_netscape_certsrv_unix_UserID
 * Method:    set
 * Signature: (Ljava/lang/String;)Z
 */
JNIEXPORT void JNICALL
Java_com_netscape_osutil_UserID_set__Ljava_lang_String_2(JNIEnv *env, jclass cls, jstring name)
{
	int id = name_to_uid(env, name);

	if (id >= 0)
		Java_com_netscape_osutil_UserID_set__I(env, cls, id);
}

/*
 * Class:     com_netscape_certsrv_unix_UserID
 * Method:    setEffective
 * Signature: (I)Z
 */
JNIEXPORT void JNICALL
Java_com_netscape_osutil_UserID_setEffective__I(JNIEnv *env, jclass cls, jint id)
{
	int status = seteuid(id);

	if (status != 0) {
		switch (errno) {
		case EINVAL:
			unix_throw_exception(env, ILLARG, "bad uid value");
			break;
		case EPERM:
			unix_throw_exception(env, SECURITY, "permission denied");
			break;
		} 
	}
}

/*
 * Class:     com_netscape_certsrv_unix_UserID
 * Method:    setEffective
 * Signature: (Ljava/lang/String;)Z
 */
JNIEXPORT void JNICALL
Java_com_netscape_osutil_UserID_setEffective__Ljava_lang_String_2(JNIEnv *env, jclass cls, jstring name)
{
	int id = name_to_uid(env, name);

	if (id >= 0)
		Java_com_netscape_osutil_UserID_setEffective__I(env, cls, id);
}