reindent-20030715
[openafs.git] / src / JAVA / libjafs / UserToken.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 <afs/param.h>
23
24 #include "Internal.h"
25 #include "org_openafs_jafs_Token.h"
26
27 #include <sys/stat.h>
28 #include <sys/ioctl.h>
29 #include <afs/vice.h>
30 #include <afs/venus.h>
31 #include <afs/afs_args.h>
32 /*#include <afs/afs_osi.h>
33   #include <afs/afs_usrops.h>*/
34 #include <pthread.h>
35
36 #ifdef DMALLOC
37 #include "dmalloc.h"
38 #endif
39
40 pthread_mutex_t jafs_init_lock;
41
42 extern pthread_mutex_t jafs_login_lock;
43
44 extern 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 getNativeString call the corresponding free().
55  * - For every Get<type>ArrayElements call the corresponding
56  *   Release<type>ArrayElements
57  * - For every malloc call the corresponding free.
58  */
59
60 int
61 osi_audit(void)
62 {
63     return 0;
64 }
65
66 JNIEXPORT void JNICALL
67 Java_org_openafs_jafs_Token_callDebugger(JNIEnv * env, jobject obj)
68 {
69     fprintf(stderr, "callDebugger called\n");
70     __asm__("int $0x3");
71 }
72
73 /**
74  * Initialize the user space library.
75  *
76  * The user space client must be initialized prior to any
77  * user space related methods, including: klog, unlog, relog,
78  * and shutdown.
79  *
80  * env      the Java environment
81  * cls      the current Java class
82  *
83  * throws AFSException
84  */
85 JNIEXPORT void JNICALL
86 Java_org_openafs_jafs_Token_initUserSpace(JNIEnv * env, jclass cls)
87 {
88     char afsMountPoint[100], afsConfDir[100], afsCacheDir[100], logFile[100];
89     jfieldID fid;
90     int pagval;
91
92     /* Initialize each init parameter with its associated default value */
93     int cacheBlocks = 100000;
94     int cacheFiles = 12500;
95     int cacheStatEntries = 8192;
96     int dCacheSize = 11398;
97     int vCacheSize = 128;
98     int chunkSize = 0;
99     int closeSynch = 0;
100     int debug = 0;
101     int nDaemons = 3;
102     int cacheFlags = -1;
103
104     /* Initialize each init parameter with its associated default value */
105     strcpy(afsMountPoint, "/afs");
106     strcpy(afsConfDir, "/usr/afswsp/etc");
107     strcpy(afsCacheDir, "/usr/afswsp/cache");
108     strcpy(logFile, "/usr/afswsp/log/libjafs.log");
109
110     pthread_mutex_init(&jafs_init_lock, NULL);
111     pthread_mutex_lock(&jafs_init_lock);
112
113     readCacheParms(afsMountPoint, afsConfDir, afsCacheDir, &cacheBlocks,
114                    &cacheFiles, &cacheStatEntries, &dCacheSize, &vCacheSize,
115                    &chunkSize, &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", afsMountPoint, afsConfDir, afsCacheDir, cacheBlocks, cacheFiles, cacheStatEntries, dCacheSize, vCacheSize, chunkSize, closeSynch, debug, nDaemons, cacheFlags, logFile);
124     }
125     uafs_Init("init_native", afsMountPoint, afsConfDir, afsCacheDir,
126               cacheBlocks, cacheFiles, cacheStatEntries, dCacheSize,
127               vCacheSize, chunkSize, closeSynch, debug, nDaemons, cacheFlags,
128               logFile);
129
130
131     /* make the initial pag the unauthenticated pag */
132     afs_setpag();
133     uafs_unlog();
134     pagval = afs_getpag_val();
135
136     fid = (*env)->GetStaticFieldID(env, cls, "ANYUSER_PAG_ID", "I");
137     if (fid == 0) {
138         fprintf(stderr,
139                 "UserToken::init(): GetFieldID (ANYUSER_PAG_ID) failed\n");
140         return;
141     }
142
143     (*env)->SetStaticIntField(env, cls, fid, pagval);
144
145     pthread_mutex_unlock(&jafs_init_lock);
146 }
147
148 /**
149  * Authenticates a user in kas, and binds that authentication
150  * to the current process.
151  *
152  * env      the Java environment
153  * obj      the current Java class
154  * loginUTF    the login to authenticate (expected as username@cellname)
155  * passwordUTF the password of the login
156  * id           the existing pag (or 0)
157  *
158  * returns the assigned pag
159  *
160  * throws AFSException
161  */
162 JNIEXPORT jint JNICALL
163 Java_org_openafs_jafs_Token_klog(JNIEnv * env, jobject obj, jstring jusername,
164                                  jstring jpassword, jstring jcell, jint id)
165 {
166     char *username;
167     char *password;
168     char *cell;
169     char *reason;
170     int code;
171     jint rc = -1;
172
173     if (jcell != NULL) {
174         cell = getNativeString(env, jcell);
175         if (!cell) {
176             fprintf(stderr, "UserToken::klog(): failed to get cell name\n");
177             throwMessageException(env,
178                                   "Internal error, failed to translate cell name.");
179             return -1;
180         }
181     } else {
182         cell = NULL;
183     }
184
185     if (jusername != NULL) {
186         username = getNativeString(env, jusername);
187         if (!username) {
188             if (cell != NULL)
189                 free(cell);
190             fprintf(stderr, "UserToken::klog(): failed to get username\n");
191             throwMessageException(env,
192                                   "Internal error, failed to translate username.");
193             return -1;
194         }
195     } else {
196         if (cell != NULL)
197             free(cell);
198         throwAFSException(env, JAFSNULLUSER);
199         return -1;
200     }
201
202     if (jpassword != NULL) {
203         password = getNativeString(env, jpassword);
204         if (!password) {
205             if (cell != NULL)
206                 free(cell);
207             free(username);
208             fprintf(stderr, "UserToken::klog(): failed to get password\n");
209             throwMessageException(env,
210                                   "Internal error, failed to translate password.");
211             return -1;
212         }
213     } else {
214         if (cell != NULL)
215             free(cell);
216         free(username);
217         throwAFSException(env, JAFSNULLPASS);
218         return -1;
219     }
220
221     if (id == 0) {
222         code = uafs_klog(username, cell, password, &reason);
223     } else {
224         /* Use existing PAG for this thread */
225         code = afs_setpag_val(id);
226         if (code != 0)
227             code = 180492L;     /* KABADARGUMENT */
228         if (!code)
229             code = uafs_klog_nopag(username, cell, password, &reason);
230     }
231
232     if (code != 0) {
233         if (cell != NULL)
234             free(cell);
235         if (username != NULL)
236             free(username);
237         if (password != NULL)
238             free(password);
239         fprintf(stderr,
240                 "UserToken::klog(): uafs_klog failed to cell %s: %s\n", cell,
241                 reason);
242         fprintf(stderr, "code = %d\n", code);
243         throwAFSException(env, code);
244         return -1;
245     }
246
247     /* Get the PAG we were assigned as the return value */
248     rc = afs_getpag_val();
249
250     /* clean up */
251     if (cell != NULL)
252         free(cell);
253     if (username != NULL)
254         free(username);
255     if (password != NULL)
256         free(password);
257
258     /* return PAG ID */
259     return rc;
260 }
261
262 /**
263  * Authenticates a user in KAS by a previously acquired PAG ID, and binds 
264  * that authentication to the current thread or native process.
265  *
266  * <P> This method does not require the user's username and password to
267  * fully authenticate their request.  Rather it utilizes the user's PAG ID
268  * to recapture the user's existing credentials.
269  *
270  * env      the Java environment
271  * obj      the current Java class
272  * id       User's current PAG (process authentication group) ID
273  *
274  * throws AFSException
275  */
276 JNIEXPORT void JNICALL
277 Java_org_openafs_jafs_Token_relog(JNIEnv * env, jobject obj, jint id)
278 {
279     int rc;
280
281     rc = afs_setpag_val(id);
282
283     if (rc != 0) {
284         throwAFSException(env, rc);
285     }
286 }
287
288 /**
289  * Authenticates a user in KAS, and binds that authentication
290  * to the current process.
291  *
292  * env      the Java environment
293  * obj      the current Java class
294  *
295  * throws AFSException
296  */
297 JNIEXPORT void JNICALL
298 Java_org_openafs_jafs_Token_unlog(JNIEnv * env, jobject obj)
299 {
300     int rc;
301
302     rc = uafs_unlog();
303
304     if (rc != 0) {
305         throwAFSException(env, rc);
306     }
307 }
308
309 /**
310  * Inform the native library that the application is 
311  * shutting down and will be unloading.
312  *
313  * <p> The library will make a call informing the file server that it will 
314  * no longer be available for callbacks.
315  *
316  * env      the Java environment
317  * obj      the current Java class
318  *
319  * throws AFSException
320  */
321 JNIEXPORT void JNICALL
322 Java_org_openafs_jafs_Token_shutdown(JNIEnv * env, jobject obj)
323 {
324     uafs_Shutdown();
325 }