reindent-20030715
[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 *
63 getACL(char *path)
64 {
65     struct ViceIoctl params;
66     char *buffer;
67
68     params.in = NULL;
69     params.out = NULL;
70     params.in_size = 0;
71     params.out_size = 0;
72
73     buffer = (char *)malloc(ACL_LEN);
74
75     if (!buffer) {
76         fprintf(stderr, "ERROR: ACL::getACL -> could not allocate buffer\n");
77         return NULL;
78     }
79
80     params.out = buffer;
81     params.out_size = ACL_LEN;
82
83     if (call_syscall(AFSCALL_PIOCTL, path, VIOCGETAL, &params, 1, 0)) {
84         fprintf(stderr, "ERROR: ACL::getACL -> VIOCGETAL failed: %d\n",
85                 errno);
86         free(buffer);
87         return NULL;
88     }
89
90     return params.out;
91 }
92
93 /**
94  * Sets the ACL for the specified path using the provided string
95  * representation.
96  *
97  * path       the directory path
98  * aclString  string representation of ACL to be set
99  * returns TRUE if the operation succeeds; otherwise FALSE;
100  */
101 jboolean
102 setACL(char *path, char *aclString)
103 {
104     struct ViceIoctl params;
105     char *redirect, *parentURI, *cptr;
106
107     params.in = aclString;
108     params.in_size = strlen(aclString) + 1;
109     params.out = NULL;
110     params.out_size = 0;
111
112     if (call_syscall(AFSCALL_PIOCTL, path, VIOCGETAL, &params, 1)) {
113         fprintf(stderr, "ERROR: ACL::setACL -> VIOCSETAL failed: %d\n",
114                 errno);
115         return JNI_FALSE;
116     }
117
118     return JNI_TRUE;
119 }
120
121 /**
122  * Returns a formatted string representing the ACL for the specified path.
123  *
124  * The string format is in the form of a ViceIoctl and is as follows:
125  * printf("%d\n%d\n", positiveEntriesCount, negativeEntriesCount);
126  * printf("%s\t%d\n", userOrGroupName, rightsMask);
127  *
128  * path     the directory path
129  * returns NULL if an exception is encountered.
130  */
131 JNIEXPORT jstring JNICALL
132 Java_org_openafs_jafs_ACL_getACLString(JNIEnv * env, jobject obj,
133                                        jstring pathUTF)
134 {
135     char *path, *acl;
136     jstring answer = NULL;
137
138     path = getNativeString(env, pathUTF);
139     if (path == NULL) {
140         fprintf(stderr, "ERROR: ACL::getACLString ->");
141         fprintf(stderr, "path = NULL\n");
142         throwAFSException(env, JAFSNULLPATH);
143         return NULL;
144     }
145
146     acl = getACL(path);
147     free(path);
148
149     if (acl) {
150         answer = (*env)->NewStringUTF(env, acl);
151         free(acl);
152     } else {
153         throwAFSException(env, errno);
154     }
155
156     return answer;
157 }
158
159 /**
160  * Sets the ACL for the specified path using the provided string
161  * representation.
162  *
163  * The string format is in the form of a ViceIoctl and is as follows:
164  * printf("%d\n%d\n", positiveEntriesCount, negativeEntriesCount);
165  * printf("%s\t%d\n", userOrGroupName, rightsMask);
166  *
167  * path       the directory path
168  * aclString  string representation of ACL to be set
169  * throws an afsAdminExceptionName if an internal exception is encountered.
170  */
171 JNIEXPORT void JNICALL
172 Java_org_openafs_jafs_ACL_setACLString(JNIEnv * env, jobject obj,
173                                        jstring pathUTF, jstring aclStringUTF)
174 {
175     char *path, *aclString;
176
177     if (pathUTF == NULL) {
178         fprintf(stderr, "ERROR: ACL::setACLString -> pathUTF == NULL\n");
179         throwAFSException(env, JAFSNULLPATH);
180         return;
181     }
182
183     if (aclStringUTF == NULL) {
184         fprintf(stderr, "ERROR: ACL::setACLString -> aclStringUTF == NULL\n");
185         throwAFSException(env, JAFSNULLACL);
186         return;
187     }
188
189     /* path = (char*) (*env)->GetStringUTFChars(env, pathUTF, 0); */
190     path = getNativeString(env, pathUTF);
191     if (path == NULL) {
192         fprintf(stderr, "ERROR: ACL::setACLString -> failed to get path\n");
193         throwMessageException(env, "Failed to get path.");
194         return;
195     }
196
197     /* aclString = (char*) (*env)->GetStringUTFChars(env, aclStringUTF, 0); */
198     aclString = getNativeString(env, aclStringUTF);
199     if (aclString == NULL) {
200         free(path);
201         fprintf(stderr,
202                 "ERROR: ACL::setACLString -> failed to get aclString\n");
203         throwMessageException(env, "Failed to get ACL string.");
204         return;
205     }
206
207     if (!setACL(path, aclString)) {
208         throwAFSException(env, errno);
209     }
210
211     /* Clean up */
212     free(path);
213     free(aclString);
214
215     /*
216      * (*env)->ReleaseStringUTFChars(env, pathUTF, path);
217      * (*env)->ReleaseStringUTFChars(env, aclStringUTF, aclString);
218      */
219 }