Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / JAVA / libjafs / UserToken.c
CommitLineData
805e021f
CE
1/*
2 * Copyright (c) 2001-2002 International Business Machines Corp.
3 * All rights reserved.
4 *
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 *
9 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
12 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
13 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
14 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
15 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
16 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
17 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
18 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
19 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20 */
21
22#include <afsconfig.h>
23#include <afs/param.h>
24
25#include "Internal.h"
26#include "org_openafs_jafs_Token.h"
27
28#include <sys/stat.h>
29#include <sys/ioctl.h>
30#include <afs/vice.h>
31#include <netinet/in.h>
32#include <afs/venus.h>
33#include <afs/afs_args.h>
34/*#include <afs/afs_osi.h>
35 #include <afs/afs_usrops.h>*/
36#include <pthread.h>
37
38#ifdef DMALLOC
39#include "dmalloc.h"
40#endif
41
42pthread_mutex_t jafs_init_lock;
43extern pthread_mutex_t jafs_login_lock;
44extern int readCacheParms(char *afsMountPoint, char *afsConfDir,
45 char *afsCacheDir, int *cacheBlocks,
46 int *cacheFiles, int *cacheStatEntries,
47 int *dCacheSize, int *vCacheSize, int *chunkSize,
48 int *closeSynch, int *debug, int *nDaemons,
49 int *cacheFlags, char *logFile);
50
51/**
52 * Be carefull with the memory management:
53 *
54 * - For every GetStringUTFChars call the corresponding ReleaseStringUTFChars.
55 * - For every Get<type>ArrayElements call the corresponding
56 * Release<type>ArrayElements
57 * - For every malloc call the corresponding free.
58 */
59
60int osi_audit(void)
61{
62 return 0;
63}
64
65/*JNIEXPORT void JNICALL Java_org_openafs_jafs_Token_callDebugger
66 (JNIEnv *env, jobject obj)
67{
68 fprintf(stderr, "callDebugger called\n");
69 __asm__("int $0x3");
70}*/
71
72/**
73 * Initialize the user space library.
74 *
75 * The user space client must be initialized prior to any
76 * user space related methods, including: klog, unlog, relog,
77 * and shutdown.
78 *
79 * env the Java environment
80 * cls the current Java class
81 *
82 * throws AFSException
83 */
84JNIEXPORT void JNICALL Java_org_openafs_jafs_Token_initUserSpace
85 (JNIEnv *env, jclass cls)
86{
87 char afsMountPoint[100], afsConfDir[100], afsCacheDir[100], logFile[100];
88 jfieldID fid;
89 int pagval;
90
91 /* Initialize each init parameter with its associated default value */
92 int cacheBlocks = 100000;
93 int cacheFiles = 12500;
94 int cacheStatEntries = 8192;
95 int dCacheSize = 11398;
96 int vCacheSize = 128;
97 int chunkSize = 0;
98 int closeSynch = 0;
99 int debug = 0;
100 int nDaemons = 3;
101 int cacheFlags = -1;
102
103 /* Initialize each init parameter with its associated default value */
104 strcpy(afsMountPoint, "/afs");
105 strcpy(afsConfDir, "/usr/afswsp/etc");
106 strcpy(afsCacheDir, "/usr/afswsp/cache");
107 strcpy(logFile, "/usr/afswsp/log/libjafs.log");
108
109 pthread_mutex_init(&jafs_init_lock, NULL);
110 pthread_mutex_lock(&jafs_init_lock);
111
112 readCacheParms(afsMountPoint, afsConfDir, afsCacheDir,
113 &cacheBlocks, &cacheFiles, &cacheStatEntries,
114 &dCacheSize, &vCacheSize, &chunkSize,
115 &closeSynch, &debug, &nDaemons, &cacheFlags,
116 logFile);
117
118 /* See cache.tune for configuration details */
119 if (debug) {
120 fprintf(stderr, "uafs_Init(\"init_native\", \"%s\", \"%s\", \"%s\"",
121 "%d, %d, %d,"
122 "%d, %d, %d,"
123 "%d, %d, %d, %d, \"%s\");\n",
124 afsMountPoint, afsConfDir, afsCacheDir,
125 cacheBlocks, cacheFiles, cacheStatEntries,
126 dCacheSize, vCacheSize, chunkSize,
127 closeSynch, debug, nDaemons, cacheFlags, logFile);
128 }
129 uafs_Init("init_native", afsMountPoint, afsConfDir, afsCacheDir,
130 cacheBlocks, cacheFiles, cacheStatEntries,
131 dCacheSize, vCacheSize, chunkSize,
132 closeSynch, debug, nDaemons, cacheFlags, logFile);
133
134
135 /* make the initial pag the unauthenticated pag */
136 afs_setpag();
137 uafs_unlog();
138 pagval = afs_getpag_val();
139
140 fid = (*env)->GetStaticFieldID(env, cls, "ANYUSER_PAG_ID", "I");
141 if (fid == 0) {
142 fprintf(stderr,
143 "UserToken::init(): GetFieldID (ANYUSER_PAG_ID) failed\n");
144 return;
145 }
146
147 (*env)->SetStaticIntField(env, cls, fid, pagval);
148
149 pthread_mutex_unlock(&jafs_init_lock);
150}
151
152/**
153 * Authenticates a user in kas, and binds that authentication
154 * to the current process.
155 *
156 * env the Java environment
157 * obj the current Java class
158 * loginUTF the login to authenticate (expected as username@cellname)
159 * passwordUTF the password of the login
160 * id the existing pag (or 0)
161 *
162 * returns the assigned pag
163 *
164 * throws AFSException
165 */
166JNIEXPORT jint JNICALL
167Java_org_openafs_jafs_Token_klog (JNIEnv *env, jobject obj,
168 jstring jusername, jstring jpassword, jstring jcell, jint id)
169{
170 char *username;
171 char *password;
172 char *cell;
173 char *reason;
174 jint rc = -1;
175 int code;
176
177 if( jcell != NULL ) {
178 cell = (char*) (*env)->GetStringUTFChars(env, jcell, 0);
179 if( !cell ) {
180 char *error = "UserToken::klog(): failed to get cell name\n";
181 fprintf(stderr, error);
182 throwMessageException( env, error );
183 return -1;
184 }
185 } else {
186 cell = NULL;
187 }
188
189 if( jusername != NULL ) {
190 username = (char*) (*env)->GetStringUTFChars(env, jusername, 0);
191 if( !username ) {
192 char *error = "UserToken::klog(): failed to get username\n";
193 (*env)->ReleaseStringUTFChars(env, jcell, cell);
194 fprintf(stderr, error);
195 throwMessageException( env, error );
196 return -1;
197 }
198 } else {
199 username = NULL;
200 }
201 if( jpassword != NULL ) {
202 password = (char*) (*env)->GetStringUTFChars(env, jpassword, 0);
203 if( !password ) {
204 char *error = "UserToken::klog(): failed to get password\n";
205 (*env)->ReleaseStringUTFChars(env, jcell, cell);
206 (*env)->ReleaseStringUTFChars(env, jusername, username);
207 fprintf(stderr, error);
208 throwMessageException( env, error );
209 return -1;
210 }
211 } else {
212 password = NULL;
213 }
214
215 if (id == 0) {
216 code = uafs_klog(username, cell, password, &reason);
217 } else {
218 /* Use existing PAG for this thread */
219 code = afs_setpag_val(id);
220 if (code != 0) code = 180492L; /* KABADARGUMENT */
221 if (!code) code = uafs_klog_nopag(username, cell, password, &reason);
222 }
223
224 if (code != 0) {
225 if( cell != NULL ) {
226 (*env)->ReleaseStringUTFChars(env, jcell, cell);
227 }
228 if( username != NULL ) {
229 (*env)->ReleaseStringUTFChars(env, jusername, username);
230 }
231 if( password != NULL ) {
232 (*env)->ReleaseStringUTFChars(env, jpassword, password);
233 }
234 fprintf(stderr, "UserToken::klog(): uafs_klog failed to cell %s: %s\n",
235 cell, reason);
236 fprintf(stderr, "code = %d\n", code);
237 throwAFSException( env, code );
238 }
239
240 /* Get the PAG we were assigned as the return value */
241 rc = afs_getpag_val();
242
243 /* clean up */
244 if( cell != NULL ) {
245 (*env)->ReleaseStringUTFChars(env, jcell, cell);
246 }
247 if( username != NULL ) {
248 (*env)->ReleaseStringUTFChars(env, jusername, username);
249 }
250 if( password != NULL ) {
251 (*env)->ReleaseStringUTFChars(env, jpassword, password);
252 }
253
254 /* return PAG ID */
255 return rc;
256}
257
258/**
259 * Authenticates a user in KAS by a previously acquired PAG ID, and binds
260 * that authentication to the current thread or native process.
261 *
262 * <P> This method does not require the user's username and password to
263 * fully authenticate their request. Rather it utilizes the user's PAG ID
264 * to recapture the user's existing credentials.
265 *
266 * env the Java environment
267 * obj the current Java class
268 * id User's current PAG (process authentication group) ID
269 *
270 * throws AFSException
271 */
272JNIEXPORT void JNICALL Java_org_openafs_jafs_Token_relog
273 (JNIEnv *env, jobject obj, jint id)
274{
275 int rc;
276
277 rc = afs_setpag_val(id);
278
279 if (rc != 0) {
280 throwAFSException( env, rc );
281 }
282}
283
284/**
285 * Authenticates a user in KAS, and binds that authentication
286 * to the current process.
287 *
288 * env the Java environment
289 * obj the current Java class
290 *
291 * throws AFSException
292 */
293JNIEXPORT void JNICALL Java_org_openafs_jafs_Token_unlog
294 (JNIEnv *env, jobject obj)
295{
296 int rc;
297
298 rc = uafs_unlog();
299
300 if (rc != 0) {
301 throwAFSException( env, rc );
302 }
303}
304
305/**
306 * Inform the native library that the application is
307 * shutting down and will be unloading.
308 *
309 * <p> The library will make a call informing the file server that it will
310 * no longer be available for callbacks.
311 *
312 * env the Java environment
313 * obj the current Java class
314 *
315 * throws AFSException
316 */
317JNIEXPORT void JNICALL Java_org_openafs_jafs_Token_shutdown
318 (JNIEnv *env, jobject obj)
319{
320 uafs_Shutdown();
321}
322
323
324
325