java-jafs-update-20030619
[openafs.git] / src / JAVA / libjafs / ACL.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 #include <afs/stds.h>
24
25 #include "Internal.h"
26 #include "org_openafs_jafs_ACL.h"
27
28 #include <stdio.h>
29 #include <sys/ioctl.h>
30 #include <afs/vice.h>
31 #include <afs/venus.h>
32 #include <afs/afs_args.h>
33
34 /*
35   #include <afs/afs_osi.h>
36 */
37
38 /* just for debugging */
39 #define MAXHOSTS  13
40 #define OMAXHOSTS 8
41 #define MAXNAME   100
42 #define MAXSIZE 2048
43 #define MAXINSIZE 1300    /* pioctl complains if data is larger than this */
44 #define VMSGSIZE  128      /* size of msg buf in volume hdr */
45
46 static char space[MAXSIZE];
47
48 #ifdef DMALLOC
49 #include "dmalloc.h"
50 #endif
51
52 #define ACL_LEN      1024
53
54 extern int errno;
55
56 /**
57  * Returns a formatted string representing the ACL for the specified path.
58  *
59  * path     the directory path
60  * returns NULL if an exception is encountered.
61  */
62 char* getACL(char *path)
63 {
64     struct ViceIoctl params;
65     char *buffer;
66
67     params.in  = NULL;
68     params.out = NULL;
69     params.in_size  = 0; 
70     params.out_size = 0;
71     
72     buffer = (char*) malloc(ACL_LEN);
73
74     if (!buffer) {
75       fprintf(stderr, "ERROR: ACL::getACL -> could not allocate buffer\n");
76       return NULL;
77     }
78
79     params.out = buffer;
80     params.out_size = ACL_LEN; 
81
82     if ( call_syscall(AFSCALL_PIOCTL, path, VIOCGETAL, &params, 1, 0) ) {
83       fprintf(stderr, "ERROR: ACL::getACL -> VIOCGETAL failed: %d\n", errno);
84       free( buffer );
85       return NULL;
86     }
87
88     return params.out;
89 }
90
91 /**
92  * Sets the ACL for the specified path using the provided string
93  * representation.
94  *
95  * path       the directory path
96  * aclString  string representation of ACL to be set
97  * returns TRUE if the operation succeeds; otherwise FALSE;
98  */
99 jboolean setACL(char *path, char *aclString)
100 {
101     struct ViceIoctl params;
102     char *redirect, *parentURI, *cptr;
103
104     params.in = aclString;
105     params.in_size = strlen(aclString) + 1;
106     params.out = NULL;
107     params.out_size = 0;
108
109     if ( call_syscall(AFSCALL_PIOCTL, path, VIOCGETAL, &params, 1) ) {
110       fprintf(stderr, "ERROR: ACL::setACL -> VIOCSETAL failed: %d\n", errno);
111       return JNI_FALSE;
112     }
113
114     return JNI_TRUE;
115 }
116
117 /**
118  * Returns a formatted string representing the ACL for the specified path.
119  *
120  * The string format is in the form of a ViceIoctl and is as follows:
121  * printf("%d\n%d\n", positiveEntriesCount, negativeEntriesCount);
122  * printf("%s\t%d\n", userOrGroupName, rightsMask);
123  *
124  * path     the directory path
125  * returns NULL if an exception is encountered.
126  */
127 JNIEXPORT jstring JNICALL Java_org_openafs_jafs_ACL_getACLString
128   (JNIEnv *env, jobject obj, jstring pathUTF)
129 {
130     char *path, *acl;
131     jstring answer = NULL;
132
133     path = getNativeString(env, pathUTF);
134     if ( path == NULL ) {
135       fprintf(stderr, "ERROR: ACL::getACLString ->");
136       fprintf(stderr, "path = NULL\n");
137       throwAFSException( env, JAFSNULLPATH ); 
138       return NULL;
139     }
140
141     acl = getACL( path );
142     free( path );
143
144     if ( acl ) {
145       answer = (*env)->NewStringUTF( env, acl );
146       free( acl );
147     } else {
148       throwAFSException( env, errno );
149     }
150
151     return answer;
152 }
153
154 /**
155  * Sets the ACL for the specified path using the provided string
156  * representation.
157  *
158  * The string format is in the form of a ViceIoctl and is as follows:
159  * printf("%d\n%d\n", positiveEntriesCount, negativeEntriesCount);
160  * printf("%s\t%d\n", userOrGroupName, rightsMask);
161  *
162  * path       the directory path
163  * aclString  string representation of ACL to be set
164  * throws an afsAdminExceptionName if an internal exception is encountered.
165  */
166 JNIEXPORT void JNICALL Java_org_openafs_jafs_ACL_setACLString
167   (JNIEnv *env, jobject obj, jstring pathUTF, jstring aclStringUTF)
168 {
169     char *path, *aclString;
170
171     if ( pathUTF == NULL ) {
172       fprintf(stderr, "ERROR: ACL::setACLString -> pathUTF == NULL\n");
173       throwAFSException( env, JAFSNULLPATH );
174       return;
175     }
176
177     if ( aclStringUTF == NULL ) {
178       fprintf(stderr, "ERROR: ACL::setACLString -> aclStringUTF == NULL\n");
179       throwAFSException( env, JAFSNULLACL );
180       return;
181     }
182
183     /* path = (char*) (*env)->GetStringUTFChars(env, pathUTF, 0); */
184     path = getNativeString( env, pathUTF );
185     if ( path == NULL ) {
186       fprintf(stderr, "ERROR: ACL::setACLString -> failed to get path\n");
187       throwMessageException( env, "Failed to get path." );
188       return;
189     }
190
191     /* aclString = (char*) (*env)->GetStringUTFChars(env, aclStringUTF, 0); */
192     aclString = getNativeString( env, aclStringUTF );
193     if ( aclString == NULL ) {
194       free( path );
195       fprintf(stderr, "ERROR: ACL::setACLString -> failed to get aclString\n");
196       throwMessageException( env, "Failed to get ACL string." ); 
197       return;
198     }
199
200     if ( !setACL(path, aclString) ) {
201       throwAFSException( env, errno );
202     }
203
204     /* Clean up */
205     free( path );
206     free( aclString );
207
208     /*
209     (*env)->ReleaseStringUTFChars(env, pathUTF, path);
210     (*env)->ReleaseStringUTFChars(env, aclStringUTF, aclString);
211     */
212 }