Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / JAVA / libjafs / AdminToken.c
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 "Internal.h"
23 #include "org_openafs_jafs_Cell.h"
24 #include "org_openafs_jafs_Token.h"
25
26 #include <stdio.h>
27 #include <afs_kasAdmin.h>
28 #include <afs_ptsAdmin.h>
29 #include <afs_clientAdmin.h>
30 #include <kautils.h>
31 #include <cellconfig.h>
32 #include <afs_AdminClientErrors.h>
33 #include <rx/rxkad.h>
34
35 /**
36 * Static function used to initialize the client library and the
37 * jafs library.
38 *
39 * env the Java environment
40 * obj the current Java object
41 */
42 JNIEXPORT void JNICALL
43 Java_org_openafs_jafs_Token_initializeAdminClient(JNIEnv *env, jclass cls)
44 {
45 afs_status_t ast;
46 if( !afsclient_Init( &ast ) ) {
47 throwAFSException( env, ast );
48 return;
49 }
50 }
51
52
53 /**
54 * Authenticates the given user and password in the cell. Returns
55 * a token that can be used to prove this authentication later.
56 *
57 * env the Java environment
58 * obj the current Java object
59 * jcellName the name of the cell in which to authenticate this user
60 * juserName the name of the user to authenticate
61 * jpassword the password of the user
62 * returns a token representing the authentication
63 */
64 JNIEXPORT jlong JNICALL
65 Java_org_openafs_jafs_Token_getToken
66 (JNIEnv *env, jobject obj, jstring jcellName, jstring juserName,
67 jstring jpassword)
68 {
69 afs_status_t ast;
70 const char *cellName;
71 const char *userName;
72 const char *password;
73 void *tokenHandle;
74 int rc;
75 int err;
76
77 // convert java strings
78 if( jcellName != NULL ) {
79 cellName = (*env)->GetStringUTFChars(env, jcellName, 0);
80 if( !cellName ) {
81 throwAFSException( env, JAFSADMNOMEM );
82 return 0;
83 }
84 } else {
85 cellName = NULL;
86 }
87 if( juserName != NULL ) {
88 userName = (*env)->GetStringUTFChars(env, juserName, 0);
89 if( !userName ) {
90 throwAFSException( env, JAFSADMNOMEM );
91 return 0;
92 }
93 } else {
94 userName = NULL;
95 }
96 if( jpassword != NULL ) {
97 password = (*env)->GetStringUTFChars(env, jpassword, 0);
98 if( !password ) {
99 throwAFSException( env, JAFSADMNOMEM );
100 return 0;
101 }
102 } else {
103 password = NULL;
104 }
105
106 err = (password==NULL || userName==NULL)
107 ? afsclient_TokenGetExisting( cellName, &tokenHandle, &ast)
108 : afsclient_TokenGetNew( cellName, userName, password, &tokenHandle, &ast);
109
110 if ( !err ) {
111 // release converted strings
112 if( cellName != NULL ) {
113 (*env)->ReleaseStringUTFChars(env, jcellName, cellName);
114 }
115 if( userName != NULL ) {
116 (*env)->ReleaseStringUTFChars(env, juserName, userName);
117 }
118 if( password != NULL ) {
119 (*env)->ReleaseStringUTFChars(env, jpassword, password);
120 }
121 throwAFSException( env, ast );
122 return 0;
123 }
124
125 // release converted strings
126 if( cellName != NULL ) {
127 (*env)->ReleaseStringUTFChars(env, jcellName, cellName);
128 }
129 if( userName != NULL ) {
130 (*env)->ReleaseStringUTFChars(env, juserName, userName);
131 }
132 if( password != NULL ) {
133 (*env)->ReleaseStringUTFChars(env, jpassword, password);
134 }
135
136 return (jlong) tokenHandle;
137 }
138
139 /**
140 * Closes the given currently open token.
141 *
142 * env the Java environment
143 * obj the current Java object
144 * tokenHandle the token to close
145 */
146 JNIEXPORT void JNICALL
147 Java_org_openafs_jafs_Token_close
148 (JNIEnv *env, jobject obj, jlong tokenHandle)
149 {
150 afs_status_t ast;
151
152 if( !afsclient_TokenClose( (void *) tokenHandle, &ast ) ) {
153 throwAFSException( env, ast );
154 return;
155 }
156 }
157
158 /**
159 * Opens a cell for administrative use, based on the token provided.
160 * Returns a cell handle to be used by other methods as a means of
161 * authentication.
162 *
163 * env the Java environment
164 * obj the current Java object
165 * jcellName the name of the cell for which to get the handle
166 * tokenHandle a token handle previously returned by a call to getToken
167 * returns a handle to the open cell
168 */
169 JNIEXPORT jlong JNICALL
170 Java_org_openafs_jafs_Cell_getCellHandle
171 (JNIEnv *env, jobject obj, jstring jcellName, jlong tokenHandle)
172 {
173 afs_status_t ast;
174 const char *cellName;
175 void *cellHandle;
176
177 if( jcellName != NULL ) {
178 cellName = (*env)->GetStringUTFChars(env, jcellName, 0);
179 if( !cellName ) {
180 throwAFSException( env, JAFSADMNOMEM );
181 return;
182 }
183 } else {
184 cellName = NULL;
185 }
186
187 if( !afsclient_CellOpen( cellName, (void *) tokenHandle,
188 &cellHandle, &ast ) ) {
189 if( cellName != NULL ) {
190 (*env)->ReleaseStringUTFChars(env, jcellName, cellName);
191 }
192 throwAFSException( env, ast );
193 return;
194 }
195
196 if( cellName != NULL ) {
197 (*env)->ReleaseStringUTFChars(env, jcellName, cellName);
198 }
199
200 return (jlong) cellHandle;
201 }
202
203 /**
204 * Closes the given currently open cell handle.
205 *
206 * env the Java environment
207 * obj the current Java object
208 * cellHandle the cell handle to close
209 */
210 JNIEXPORT void JNICALL
211 Java_org_openafs_jafs_Cell_closeCell (JNIEnv *env, jobject obj,
212 jlong cellHandle)
213 {
214
215 afs_status_t ast;
216
217 if( !afsclient_CellClose( (void *) cellHandle, &ast ) ) {
218 throwAFSException( env, ast );
219 return;
220 }
221
222 }
223
224 /**
225 * Opens a server for administrative vos use, based on the cell handle
226 * provided. Returns a vos server handle to be used by other
227 * methods as a means of identification.
228 *
229 * env the Java environment
230 * obj the current Java object
231 * cellHandle a cell handle previously returned by
232 * a call to getCellHandle
233 * jserverName the name of the server for which to retrieve
234 * a vos handle
235 * returns a vos handle to the server
236 */
237 JNIEXPORT jlong JNICALL
238 Java_org_openafs_jafs_Server_getVosServerHandle
239 (JNIEnv *env, jobject obj, jlong cellHandle, jstring jserverName)
240 {
241 afs_status_t ast;
242 void *serverHandle;
243 // convert java string
244 const char *serverName;
245
246 if( jserverName != NULL ) {
247 serverName = (*env)->GetStringUTFChars(env, jserverName, 0);
248 if( !serverName ) {
249 throwAFSException( env, JAFSADMNOMEM );
250 return;
251 }
252 } else {
253 serverName = NULL;
254 }
255
256 if( !vos_ServerOpen( (void *) cellHandle, serverName,
257 (void **) &serverHandle, &ast ) ) {
258 if( serverName != NULL ) {
259 (*env)->ReleaseStringUTFChars(env, jserverName, serverName);
260 }
261 throwAFSException( env, ast );
262 return 0;
263 }
264
265 // release converted string
266 if( serverName != NULL ) {
267 (*env)->ReleaseStringUTFChars(env, jserverName, serverName);
268 }
269
270 return (jlong) serverHandle;
271 }
272
273 /**
274 * Closes the given currently open vos server handle.
275 *
276 * env the Java environment
277 * obj the current Java object
278 * vosServerHandle the vos server handle to close
279 */
280 JNIEXPORT void JNICALL
281 Java_org_openafs_jafs_Server_closeVosServerHandle
282 (JNIEnv *env, jobject obj, jlong vosServerHandle)
283 {
284 afs_status_t ast;
285
286 if( !vos_ServerClose( (void *) vosServerHandle, &ast ) ) {
287 throwAFSException( env, ast );
288 return;
289 }
290 }
291
292 /**
293 * Opens a server for administrative bos use, based on the cell handle
294 * provided. Returns a bos server handle to be used by other methods
295 * as a means of identification.
296 *
297 * env the Java environment
298 * obj the current Java object
299 * cellHandle a cell handle previously returned by a call
300 * to getCellHandle
301 * jserverName the name of the server for which to retrieve
302 * a bos handle
303 * returns a bos handle to the server
304 */
305 JNIEXPORT jlong JNICALL
306 Java_org_openafs_jafs_Server_getBosServerHandle
307 (JNIEnv *env, jobject obj, jlong cellHandle, jstring jserverName)
308 {
309 afs_status_t ast;
310 void *serverHandle;
311 // convert java string
312 const char *serverName;
313
314 if( jserverName != NULL ) {
315 serverName = (*env)->GetStringUTFChars(env, jserverName, 0);
316 if( !serverName ) {
317 throwAFSException( env, JAFSADMNOMEM );
318 return;
319 }
320 } else {
321 serverName = NULL;
322 }
323
324 if( !bos_ServerOpen( (void *) cellHandle, serverName,
325 (void **) &serverHandle, &ast ) ) {
326 if( serverName != NULL ) {
327 (*env)->ReleaseStringUTFChars(env, jserverName, serverName);
328 }
329 throwAFSException( env, ast );
330 return 0;
331 }
332
333 // release converted string
334 if( serverName != NULL ) {
335 (*env)->ReleaseStringUTFChars(env, jserverName, serverName);
336 }
337
338 return (jlong) serverHandle;
339 }
340
341 /**
342 * Closes the given currently open bos server handle.
343 *
344 * env the Java environment
345 * obj the current Java object
346 * bosServerHandle the bos server handle to close
347 */
348 JNIEXPORT void JNICALL
349 Java_org_openafs_jafs_Server_closeBosServerHandle
350 (JNIEnv *env, jobject obj, jlong bosServerHandle)
351 {
352 afs_status_t ast;
353
354 if( !bos_ServerClose( (void *) bosServerHandle, &ast ) ) {
355 throwAFSException( env, ast );
356 return;
357 }
358 }
359
360 /**
361 * Gets the expiration time for a given token.
362 *
363 * env the Java environment
364 * obj the current Java object
365 *
366 * tokenHandle a token handle previously returned by a call
367 * to getToken
368 * returns a long representing the UTC time for the token expiration
369 */
370 JNIEXPORT jlong JNICALL
371 Java_org_openafs_jafs_Token_getExpiration
372 (JNIEnv *env, jobject obj, jlong tokenHandle)
373 {
374 afs_status_t ast;
375 unsigned long expTime;
376 char *prince = malloc( sizeof(char)*KAS_MAX_NAME_LEN );
377 char *inst = malloc( sizeof(char)*KAS_MAX_NAME_LEN );
378 char *cell = malloc( sizeof(char)*AFS_MAX_SERVER_NAME_LEN );
379 int hkt;
380
381 if( !prince || !inst || !cell ) {
382 if( prince ) {
383 free( prince );
384 }
385 if( inst ) {
386 free( inst );
387 }
388 if( cell ) {
389 free( cell );
390 }
391 throwAFSException( env, JAFSADMNOMEM );
392 return;
393 }
394
395 if( !afsclient_TokenQuery( (void *) tokenHandle, &expTime, prince, inst,
396 cell, &hkt, &ast ) ) {
397 free( prince );
398 free( inst );
399 free( cell );
400 throwAFSException( env, ast );
401 return 0;
402 }
403
404 free( prince );
405 free( inst );
406 free( cell );
407
408 return (jlong) expTime;
409 }
410
411 // reclaim global memory used by this portion
412 JNIEXPORT void JNICALL
413 Java_org_openafs_jafs_Token_reclaimAuthMemory (JNIEnv *env, jclass cls)
414 {
415 }
416
417
418
419
420
421