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