1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved
24*0Sstevel@tonic-gate *
25*0Sstevel@tonic-gate * module:
26*0Sstevel@tonic-gate * acls.c
27*0Sstevel@tonic-gate *
28*0Sstevel@tonic-gate * purpose:
29*0Sstevel@tonic-gate * routines to manipulate access control lists, mapping between
30*0Sstevel@tonic-gate * the data structures required by the filesystem ACL system calls
31*0Sstevel@tonic-gate * and the representation used in our fileinfo structure.
32*0Sstevel@tonic-gate *
33*0Sstevel@tonic-gate */
34*0Sstevel@tonic-gate #ident "%W% %E% SMI"
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate #include <stdio.h>
37*0Sstevel@tonic-gate #include <stdlib.h>
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gate #include "filesync.h"
40*0Sstevel@tonic-gate #include "database.h"
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate #ifdef NO_ACLS
43*0Sstevel@tonic-gate /*
44*0Sstevel@tonic-gate * Solaris 2.4 libc.so does not contain this entry point, so if we
45*0Sstevel@tonic-gate * want to build a 2.4 version of filesync, we need to provide a
46*0Sstevel@tonic-gate * dummy entry point that will fail when-ever it is called.
47*0Sstevel@tonic-gate */
48*0Sstevel@tonic-gate #define acl bogus_acl
49*0Sstevel@tonic-gate
acl(const char * name,int opcode,int count,aclent_t * acls)50*0Sstevel@tonic-gate static int acl(const char *name, int opcode, int count, aclent_t *acls)
51*0Sstevel@tonic-gate {
52*0Sstevel@tonic-gate return (-1);
53*0Sstevel@tonic-gate }
54*0Sstevel@tonic-gate #endif
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gate /*
57*0Sstevel@tonic-gate * routine:
58*0Sstevel@tonic-gate * get_acls
59*0Sstevel@tonic-gate *
60*0Sstevel@tonic-gate * purpose:
61*0Sstevel@tonic-gate * to read the ACL (if any) from a file into a fileinfo structure
62*0Sstevel@tonic-gate *
63*0Sstevel@tonic-gate * parameters:
64*0Sstevel@tonic-gate * name of file
65*0Sstevel@tonic-gate * pointer to fileinfo structure
66*0Sstevel@tonic-gate *
67*0Sstevel@tonic-gate * returns:
68*0Sstevel@tonic-gate * number of ACL entries
69*0Sstevel@tonic-gate */
70*0Sstevel@tonic-gate int
get_acls(const char * name,struct fileinfo * ip)71*0Sstevel@tonic-gate get_acls(const char *name, struct fileinfo *ip)
72*0Sstevel@tonic-gate { int count;
73*0Sstevel@tonic-gate int i;
74*0Sstevel@tonic-gate static aclent_t acls[MAX_ACL_ENTRIES];
75*0Sstevel@tonic-gate aclent_t *list;
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate count = acl(name, GETACL, MAX_ACL_ENTRIES, acls);
78*0Sstevel@tonic-gate if (count <= 0)
79*0Sstevel@tonic-gate return (0);
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate /* with a count of 3 or 4 there may not be any real ones */
82*0Sstevel@tonic-gate if (count > 4)
83*0Sstevel@tonic-gate goto gotsome;
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate /* look for anything beyond the normal unix protection */
86*0Sstevel@tonic-gate for (i = 0; i < count; i++)
87*0Sstevel@tonic-gate switch (acls[i].a_type) {
88*0Sstevel@tonic-gate default: /* weird types are real */
89*0Sstevel@tonic-gate goto gotsome;
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate case USER_OBJ:
92*0Sstevel@tonic-gate case GROUP_OBJ:
93*0Sstevel@tonic-gate case OTHER_OBJ:
94*0Sstevel@tonic-gate case CLASS_OBJ:
95*0Sstevel@tonic-gate continue; /* all file have these */
96*0Sstevel@tonic-gate }
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate return (0); /* nothing interesting */
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate gotsome:
101*0Sstevel@tonic-gate /* allocate an array to hold the acls */
102*0Sstevel@tonic-gate list = (aclent_t *) malloc(count * sizeof (*list));
103*0Sstevel@tonic-gate if (list == 0)
104*0Sstevel@tonic-gate nomem("Access Control List");
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate /* copy the acls into the new list */
107*0Sstevel@tonic-gate for (i = 0; i < count; i++) {
108*0Sstevel@tonic-gate list[i].a_type = acls[i].a_type;
109*0Sstevel@tonic-gate list[i].a_id = acls[i].a_id;
110*0Sstevel@tonic-gate list[i].a_perm = acls[i].a_perm;
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate ip->f_acls = list;
114*0Sstevel@tonic-gate ip->f_numacls = count;
115*0Sstevel@tonic-gate return (ip->f_numacls);
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate /*
119*0Sstevel@tonic-gate * routine:
120*0Sstevel@tonic-gate * cmp_acls
121*0Sstevel@tonic-gate *
122*0Sstevel@tonic-gate * purpose:
123*0Sstevel@tonic-gate * determine whether or not two ACLs are the same
124*0Sstevel@tonic-gate *
125*0Sstevel@tonic-gate * parameters:
126*0Sstevel@tonic-gate * pointer to first fileinfo
127*0Sstevel@tonic-gate * pointer to second fileinfo
128*0Sstevel@tonic-gate *
129*0Sstevel@tonic-gate * returns:
130*0Sstevel@tonic-gate * true equal
131*0Sstevel@tonic-gate * false different
132*0Sstevel@tonic-gate */
133*0Sstevel@tonic-gate int
cmp_acls(struct fileinfo * f1,struct fileinfo * f2)134*0Sstevel@tonic-gate cmp_acls(struct fileinfo *f1, struct fileinfo *f2)
135*0Sstevel@tonic-gate { int i;
136*0Sstevel@tonic-gate
137*0Sstevel@tonic-gate if (f1->f_numacls != f2->f_numacls)
138*0Sstevel@tonic-gate return (0);
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gate if (f1->f_numacls == 0)
141*0Sstevel@tonic-gate return (1);
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate for (i = 0; i < f1->f_numacls; i++) {
144*0Sstevel@tonic-gate if (f1->f_acls[i].a_type != f2->f_acls[i].a_type)
145*0Sstevel@tonic-gate return (0);
146*0Sstevel@tonic-gate if (f1->f_acls[i].a_id != f2->f_acls[i].a_id)
147*0Sstevel@tonic-gate return (0);
148*0Sstevel@tonic-gate if (f1->f_acls[i].a_perm != f2->f_acls[i].a_perm)
149*0Sstevel@tonic-gate return (0);
150*0Sstevel@tonic-gate }
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate return (1);
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate /*
156*0Sstevel@tonic-gate * routine:
157*0Sstevel@tonic-gate * set_acls
158*0Sstevel@tonic-gate *
159*0Sstevel@tonic-gate * purpose:
160*0Sstevel@tonic-gate * to write the ACL of a file
161*0Sstevel@tonic-gate *
162*0Sstevel@tonic-gate * parameters:
163*0Sstevel@tonic-gate * name of file
164*0Sstevel@tonic-gate * fileinfo pointer (which contains an acl pointer)
165*0Sstevel@tonic-gate *
166*0Sstevel@tonic-gate * returns:
167*0Sstevel@tonic-gate * retcode and errno
168*0Sstevel@tonic-gate */
169*0Sstevel@tonic-gate int
set_acls(const char * name,struct fileinfo * fp)170*0Sstevel@tonic-gate set_acls(const char *name, struct fileinfo *fp)
171*0Sstevel@tonic-gate { int rc;
172*0Sstevel@tonic-gate int nacl;
173*0Sstevel@tonic-gate aclent_t acls[4], *list;
174*0Sstevel@tonic-gate
175*0Sstevel@tonic-gate if (fp->f_numacls == 0) {
176*0Sstevel@tonic-gate /* fabricate a standard set of bogus ACLs */
177*0Sstevel@tonic-gate acls[0].a_type = USER_OBJ;
178*0Sstevel@tonic-gate acls[0].a_id = fp->f_uid;
179*0Sstevel@tonic-gate acls[0].a_perm = (fp->f_mode >> 6) & 7;
180*0Sstevel@tonic-gate
181*0Sstevel@tonic-gate acls[1].a_type = GROUP_OBJ;
182*0Sstevel@tonic-gate acls[1].a_id = fp->f_gid;
183*0Sstevel@tonic-gate acls[1].a_perm = (fp->f_mode >> 3) & 7;
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate acls[2].a_type = CLASS_OBJ;
186*0Sstevel@tonic-gate acls[2].a_id = 0;
187*0Sstevel@tonic-gate acls[2].a_perm = (fp->f_mode >> 6) & 7;
188*0Sstevel@tonic-gate
189*0Sstevel@tonic-gate acls[3].a_type = OTHER_OBJ;
190*0Sstevel@tonic-gate acls[3].a_id = 0;
191*0Sstevel@tonic-gate acls[3].a_perm = fp->f_mode & 7;
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate nacl = 4;
194*0Sstevel@tonic-gate list = acls;
195*0Sstevel@tonic-gate } else {
196*0Sstevel@tonic-gate nacl = fp->f_numacls;
197*0Sstevel@tonic-gate list = fp->f_acls;
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate rc = acl(name, SETACL, nacl, list);
201*0Sstevel@tonic-gate
202*0Sstevel@tonic-gate /* non-negative number mean success */
203*0Sstevel@tonic-gate if (rc < 0)
204*0Sstevel@tonic-gate return (rc);
205*0Sstevel@tonic-gate else
206*0Sstevel@tonic-gate return (0);
207*0Sstevel@tonic-gate }
208*0Sstevel@tonic-gate
209*0Sstevel@tonic-gate /*
210*0Sstevel@tonic-gate * routine:
211*0Sstevel@tonic-gate * show_acls
212*0Sstevel@tonic-gate *
213*0Sstevel@tonic-gate * purpose:
214*0Sstevel@tonic-gate * to map an acl into arguments for a setfacl command
215*0Sstevel@tonic-gate *
216*0Sstevel@tonic-gate * paramters:
217*0Sstevel@tonic-gate * number of elements in list
218*0Sstevel@tonic-gate * pointer to list
219*0Sstevel@tonic-gate *
220*0Sstevel@tonic-gate * returns:
221*0Sstevel@tonic-gate * pointer to character buffer containing arguments
222*0Sstevel@tonic-gate */
223*0Sstevel@tonic-gate char
show_acls(int numacl,aclent_t * list)224*0Sstevel@tonic-gate *show_acls(int numacl, aclent_t *list)
225*0Sstevel@tonic-gate { int i, j;
226*0Sstevel@tonic-gate int type, perm, id;
227*0Sstevel@tonic-gate char *s;
228*0Sstevel@tonic-gate static char buf[ MAX_LINE ];
229*0Sstevel@tonic-gate
230*0Sstevel@tonic-gate s = buf;
231*0Sstevel@tonic-gate
232*0Sstevel@tonic-gate if (numacl > 0) {
233*0Sstevel@tonic-gate *s++ = '-';
234*0Sstevel@tonic-gate *s++ = 's';
235*0Sstevel@tonic-gate *s++ = ' ';
236*0Sstevel@tonic-gate } else {
237*0Sstevel@tonic-gate *s++ = '-';
238*0Sstevel@tonic-gate *s++ = 'd';
239*0Sstevel@tonic-gate }
240*0Sstevel@tonic-gate
241*0Sstevel@tonic-gate for (i = 0; i < numacl; i++) {
242*0Sstevel@tonic-gate type = list[i].a_type;
243*0Sstevel@tonic-gate id = list[i].a_id;
244*0Sstevel@tonic-gate perm = list[i].a_perm;
245*0Sstevel@tonic-gate
246*0Sstevel@tonic-gate if (i > 0)
247*0Sstevel@tonic-gate *s++ = ',';
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gate /* note whether this is per-file or default */
250*0Sstevel@tonic-gate if (type & ACL_DEFAULT) {
251*0Sstevel@tonic-gate *s++ = 'd';
252*0Sstevel@tonic-gate *s++ = ':';
253*0Sstevel@tonic-gate }
254*0Sstevel@tonic-gate
255*0Sstevel@tonic-gate /* print out the entry type */
256*0Sstevel@tonic-gate if (type & (USER_OBJ|USER)) {
257*0Sstevel@tonic-gate *s++ = 'u';
258*0Sstevel@tonic-gate *s++ = ':';
259*0Sstevel@tonic-gate } else if (type & (GROUP_OBJ|GROUP)) {
260*0Sstevel@tonic-gate *s++ = 'g';
261*0Sstevel@tonic-gate *s++ = ':';
262*0Sstevel@tonic-gate } else if (type & OTHER_OBJ) {
263*0Sstevel@tonic-gate *s++ = 'o';
264*0Sstevel@tonic-gate *s++ = ':';
265*0Sstevel@tonic-gate } else if (type & CLASS_OBJ) {
266*0Sstevel@tonic-gate *s++ = 'm';
267*0Sstevel@tonic-gate *s++ = ':';
268*0Sstevel@tonic-gate }
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate /* print out the ID for this ACL */
271*0Sstevel@tonic-gate if (type & (USER_OBJ|GROUP_OBJ))
272*0Sstevel@tonic-gate *s++ = ':';
273*0Sstevel@tonic-gate else if (type & (USER|GROUP)) {
274*0Sstevel@tonic-gate for (j = 1; id/j > 10; j *= 10);
275*0Sstevel@tonic-gate
276*0Sstevel@tonic-gate while (j > 0) {
277*0Sstevel@tonic-gate *s++ = '0' + (id/j);
278*0Sstevel@tonic-gate id %= j*10;
279*0Sstevel@tonic-gate j /= 10;
280*0Sstevel@tonic-gate }
281*0Sstevel@tonic-gate
282*0Sstevel@tonic-gate *s++ = ':';
283*0Sstevel@tonic-gate }
284*0Sstevel@tonic-gate
285*0Sstevel@tonic-gate /* print out the permissions for this ACL */
286*0Sstevel@tonic-gate *s++ = (perm & 04) ? 'r' : '-';
287*0Sstevel@tonic-gate *s++ = (perm & 02) ? 'w' : '-';
288*0Sstevel@tonic-gate *s++ = (perm & 01) ? 'x' : '-';
289*0Sstevel@tonic-gate }
290*0Sstevel@tonic-gate
291*0Sstevel@tonic-gate *s = 0;
292*0Sstevel@tonic-gate return (buf);
293*0Sstevel@tonic-gate }
294