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