fileserver & friends: Don't cast from malloc()
[openafs.git] / src / libacl / test / acltest.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
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
10 /*
11         Information Technology Center
12         Carnegie-Mellon University
13 */
14
15 #include <afsconfig.h>
16 #include <afs/param.h>
17
18 #include <roken.h>
19
20 #include <rx/xdr.h>
21 #include <rx/rx.h>
22 #include <ptclient.h>
23
24 #include "acl.h"
25 #include "prs_fs.h"
26
27 struct acl_accessList *aclstore[20];
28 char *externalstore[20];
29
30 int
31 PRights(arights)
32      long arights;
33 {
34     if (arights & PRSFS_READ)
35         printf("r");
36     if (arights & PRSFS_LOOKUP)
37         printf("l");
38     if (arights & PRSFS_INSERT)
39         printf("i");
40     if (arights & PRSFS_DELETE)
41         printf("d");
42     if (arights & PRSFS_WRITE)
43         printf("w");
44     if (arights & PRSFS_LOCK)
45         printf("k");
46     if (arights & PRSFS_ADMINISTER)
47         printf("a");
48 }
49
50 long
51 Convert(arights)
52      char *arights;
53 {
54     int i, len;
55     long mode;
56     char tc;
57     if (!strcmp(arights, "read"))
58         return PRSFS_READ | PRSFS_LOOKUP;
59     if (!strcmp(arights, "write"))
60         return PRSFS_READ | PRSFS_LOOKUP | PRSFS_INSERT | PRSFS_DELETE |
61             PRSFS_WRITE | PRSFS_LOCK;
62     if (!strcmp(arights, "mail"))
63         return PRSFS_INSERT | PRSFS_LOCK | PRSFS_LOOKUP;
64     if (!strcmp(arights, "all"))
65         return PRSFS_READ | PRSFS_LOOKUP | PRSFS_INSERT | PRSFS_DELETE |
66             PRSFS_WRITE | PRSFS_LOCK | PRSFS_ADMINISTER;
67     if (!strcmp(arights, "none"))
68         return 0;
69     len = strlen(arights);
70     mode = 0;
71     for (i = 0; i < len; i++) {
72         tc = *arights++;
73         if (tc == 'r')
74             mode |= PRSFS_READ;
75         else if (tc == 'l')
76             mode |= PRSFS_LOOKUP;
77         else if (tc == 'i')
78             mode |= PRSFS_INSERT;
79         else if (tc == 'd')
80             mode |= PRSFS_DELETE;
81         else if (tc == 'w')
82             mode |= PRSFS_WRITE;
83         else if (tc == 'k')
84             mode |= PRSFS_LOCK;
85         else if (tc == 'a')
86             mode |= PRSFS_ADMINISTER;
87         else {
88             printf("Bogus rights character '%c'.\n", tc);
89             exit(1);
90         }
91     }
92     return mode;
93 }
94
95
96
97 main()
98 {
99     long code;
100
101     char op[3];
102     char name[64];
103     char rights[10];
104     long which;
105     long n, p;
106     long realrights;
107     long i, j;
108     char *ptr;
109     char *tptr;
110     long size;
111     idlist ids;
112     namelist names;
113     prlist cps;
114
115     struct acl_accessList *alist;
116     char foo[200];
117
118     code = pr_Initialize(0, "/usr/afs/etc", 0);
119     if (code) {
120         fprintf(stderr, "Couldn't initialize wrt to protection server.\n");
121         exit(1);
122     }
123     for (i = 0; i < 20; i++) {
124         externalstore[i] = NULL;
125         aclstore[i] = NULL;
126     }
127
128     printf("acl> ");
129     while (1) {
130         scanf("%s", op);
131         if (!strcmp(op, "q"))
132             exit(2);
133         else if (!strcmp(op, "ex")) {
134             scanf("%d", &which);
135             if (aclstore[which] == NULL) {
136                 printf("No internal acl in %d.\n", which);
137                 printf("acl> ");
138                 continue;
139             }
140             if (externalstore[which] != NULL) {
141                 code = acl_FreeExternalACL(&externalstore[which]);
142                 if (code) {
143                     printf("Couldn't free current ACL.\n");
144                     printf("acl> ");
145                     continue;
146                 }
147             }
148             code = acl_Externalize(aclstore[which], &externalstore[which]);
149             if (code)
150                 printf("Couldn't externalize -- code is %d.\n", code);
151         } else if (!strcmp(op, "in")) {
152             scanf("%d", &which);
153             if (externalstore[which] == NULL) {
154                 printf("No external acl in %d.\n", which);
155                 printf("acl> ");
156                 continue;
157             }
158             if (aclstore[which] != NULL) {
159                 code = acl_FreeACL(&aclstore[which]);
160                 if (code) {
161                     printf("Couldn't free current ACL.\n");
162                     printf("acl> ");
163                     continue;
164                 }
165             }
166             code = acl_Internalize(externalstore[which], &aclstore[which]);
167             if (code)
168                 printf("Couldn't internalize. Code is %d\n", code);
169         } else if (!strcmp(op, "sa")) {
170             scanf("%d %s %s", &which, name, rights);
171             realrights = (long)Convert(rights);
172             if (externalstore[which] != NULL) {
173                 /* we're adding to access list */
174                 size = strlen(externalstore[which]);
175                 ptr = malloc(size);
176                 sscanf(externalstore[which], "%d\n%d\n", &p, &n);
177                 strncpy(ptr, externalstore[which], size);
178                 p++;
179                 free(externalstore[which]);
180                 code = acl_NewExternalACL((p + n), &externalstore[which]);
181                 if (code) {
182                     printf("Couldn't allocate external list.\n");
183                     exit(2);
184                 }
185                 sprintf(externalstore[which], "%d", p);
186                 tptr = externalstore[which] + 1;
187                 ptr++;
188                 sprintf(tptr, "%s", ptr);
189                 ptr = externalstore[which] + size;
190                 sprintf(ptr, "%s\t%d\n", name, realrights);
191             } else {
192                 /* new external list */
193                 code = acl_NewExternalACL(1, &externalstore[which]);
194                 if (code) {
195                     printf("Couldn't allocate external list.\n");
196                     exit(2);
197                 }
198                 p = 1;
199                 n = 0;
200                 sprintf(externalstore[which], "%d\n%d\n%s\t%d\n", p, n, name,
201                         realrights);
202             }
203         } else if (!strcmp(op, "la")) {
204             scanf("%d", &which);
205             if (externalstore[which] == NULL) {
206                 printf("No acl in %d.\n", which);
207                 printf("acl> ");
208                 continue;
209             }
210             ptr = externalstore[which];
211             sscanf(ptr, "%d\n%d\n", &p, &n);
212             skip(&ptr);
213             skip(&ptr);
214             for (i = 0; i < p; i++) {
215                 sscanf(ptr, "%s\t%d\n", name, &realrights);
216                 printf("%s\t", name);
217                 PRights(realrights);
218                 printf("\n");
219                 skip(&ptr);
220             }
221             if (n > 0) {
222                 printf("Negative rights: \n");
223                 for (i = 0; i < n; i++) {
224                     scanf(ptr, "%s\t%d\n", name, &realrights);
225                     printf("%s\t", name);
226                     PRights(realrights);
227                     printf("\n");
228                 }
229             }
230         } else if (!strcmp(op, "cr")) {
231             scanf("%s %d", name, &which);
232             if (aclstore[which] == NULL) {
233                 printf("No acl in %d.\n", which);
234                 printf("acl> ");
235                 continue;
236             }
237             names.namelist_len = 1;
238             names.namelist_val = malloc(strlen(name) + 1);
239             strncpy(names.namelist_val, name, PR_MAXNAMELEN);
240             code = pr_NameToId(&names, &ids);
241             if (code) {
242                 printf("Couldn't translate %s\n", name);
243                 printf("acl> ");
244                 continue;
245             }
246             code = pr_GetCPS(*ids.idlist_val, &cps);
247             if (code) {
248                 printf("Couldn't get cps\n");
249                 printf("acl> ");
250                 continue;
251             }
252             code = acl_CheckRights(aclstore[which], &cps, &realrights);
253             if (code) {
254                 printf("Couldn't check rights\n");
255                 printf("acl> ");
256                 continue;
257             }
258             printf("Rights for %s on %d are:\n", name, which);
259             PRights(realrights);
260             printf("\n");
261         } else
262             printf("Unknown op!\n");
263         printf("acl> ");
264     }
265 }
266
267 skip(s)
268      char **s;
269 {
270     while (**s != '\n' && **s != '\0')
271         (*s)++;
272     if (**s == '\n')
273         (*s)++;
274 }