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