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