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