File: Cns_acl.c

package info (click to toggle)
lfc-postgres 1.7.4.7-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,676 kB
  • ctags: 10,779
  • sloc: ansic: 146,136; sh: 13,176; perl: 11,142; python: 5,529; cpp: 5,113; sql: 1,790; makefile: 861; fortran: 113
file content (398 lines) | stat: -rw-r--r-- 9,694 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * Copyright (C) 2003-2010 by CERN/IT/ADC/CA
 * All rights reserved
 */
 
#ifndef lint
static char sccsid[] = "@(#)$RCSfile: Cns_acl.c,v $ $Revision: 3465 $ $Date: 2010-03-05 12:52:51 +0100 (Fri, 05 Mar 2010) $ CERN IT-ADC/CA Jean-Philippe Baud";
#endif /* not lint */
 
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include "Cns.h"
#include "Cns_server.h"
#include "serrno.h"
int Cgroupmatch (gid_t, int, gid_t *);

/*	Cns_acl_chmod - propagate new mode to access ACL */

Cns_acl_chmod (struct Cns_file_metadata *fmd_entry)
{
	int entry_len;
	char *iacl;
	char *nacl;
	char oldacl[CA_MAXACLENTRIES*13];
	char *p;

	strcpy (oldacl, fmd_entry->acl);
	iacl = oldacl;
	nacl = fmd_entry->acl;
	while (1) {
		p = strchr (iacl, ',');
		if (nacl != fmd_entry->acl)
			*nacl++ = ',';
		*nacl++ = *iacl;
		switch (*iacl - '@') {
		case CNS_ACL_USER_OBJ:
			*nacl++ = (fmd_entry->filemode >> 6 & 07) + '0';
			break;
		case CNS_ACL_GROUP_OBJ:
		case CNS_ACL_MASK:
			*nacl++ = (fmd_entry->filemode >> 3 & 07) + '0';
			break;
		case CNS_ACL_OTHER:
			*nacl++ = (fmd_entry->filemode & 07) + '0';
			break;
		default:
			*nacl++ = *(iacl+1);
		}
		if (p)
			entry_len = p - iacl - 2;
		else
			entry_len = strlen (iacl) - 2;
		strncpy (nacl, iacl + 2, entry_len);
		nacl += entry_len;
		if (! p) break;
		iacl = p + 1;
	}
	*nacl = '\0';
	return (0);
}

/*	Cns_acl_chown - propagate new ownership to access ACL */

Cns_acl_chown (struct Cns_file_metadata *fmd_entry)
{
	int entry_len;
	char *iacl;
	char *nacl;
	char oldacl[CA_MAXACLENTRIES*13];
	char *p;

	strcpy (oldacl, fmd_entry->acl);
	iacl = oldacl;
	nacl = fmd_entry->acl;
	while (1) {
		p = strchr (iacl, ',');
		if (nacl != fmd_entry->acl)
			*nacl++ = ',';
		*nacl++ = *iacl;
		*nacl++ = *(iacl+1);
		switch (*iacl - '@') {
		case CNS_ACL_USER_OBJ:
			nacl += sprintf (nacl, "%d", fmd_entry->uid);
			break;
		case CNS_ACL_GROUP_OBJ:
			nacl += sprintf (nacl, "%d", fmd_entry->gid);
			break;
		default:
			if (p)
				entry_len = p - iacl - 2;
			else
				entry_len = strlen (iacl) - 2;
			strncpy (nacl, iacl + 2, entry_len);
			nacl += entry_len;
		}
		if (! p) break;
		iacl = p + 1;
	}
	*nacl = '\0';
	return (0);
}

/*	Cns_acl_compare - routine used by qsort to order ACL entries */

Cns_acl_compare (const void *acl1, const void *acl2)
{
	if (((struct Cns_acl *)acl1)->a_type < ((struct Cns_acl *)acl2)->a_type)
		return (-1);
	if (((struct Cns_acl *)acl1)->a_type > ((struct Cns_acl *)acl2)->a_type)
		return (1);
	if (((struct Cns_acl *)acl1)->a_id < ((struct Cns_acl *)acl2)->a_id)
		return (-1);
	if (((struct Cns_acl *)acl1)->a_id > ((struct Cns_acl *)acl2)->a_id)
		return (1);
	return (0);
}

/*      Cns_acl_inherit - inherit ACLs from parent default ACL entries */

Cns_acl_inherit (struct Cns_file_metadata *parent_dir, struct Cns_file_metadata *fmd_entry, mode_t mode)
{
	char acl_mask = 0x7F;
	int entry_len;
	char *iacl;
	char *nacl;		/* ACL for new CNS entry */
	char *p;
	char *pacl;		/* parent default ACL */

	pacl = strchr (parent_dir->acl, CNS_ACL_DEFAULT|CNS_ACL_USER_OBJ|'@');
	if (! pacl)
		return (0);	/* no default acl */
	nacl = fmd_entry->acl;

	/* Get CNS_ACL_MASK if any */

	if (iacl = strchr (pacl, CNS_ACL_DEFAULT|CNS_ACL_MASK|'@'))
		acl_mask = *(iacl + 1) - '0';

	/* Build access ACL */

	iacl = pacl;
	if (acl_mask == 0x7F &&		/* no default mask */
	    (fmd_entry->filemode & S_IFDIR) == 0) {	/* not a directory */
		/* no need to build access ACLs, just update filemode */
		while (1) {
			p = strchr (iacl, ',');
			switch (*iacl - '@') {
			case CNS_ACL_DEFAULT | CNS_ACL_USER_OBJ:
				fmd_entry->filemode = fmd_entry->filemode & 0177077 |
					(mode & (*(iacl+1) - '0') << 6);
				break;
			case CNS_ACL_DEFAULT | CNS_ACL_GROUP_OBJ:
				fmd_entry->filemode = fmd_entry->filemode & 0177707 |
					(mode & (*(iacl+1) - '0') << 3);
				break;
			case CNS_ACL_DEFAULT | CNS_ACL_OTHER:
				fmd_entry->filemode = fmd_entry->filemode & 0177770 |
					(mode & (*(iacl+1) - '0'));
				break;
			}
			if (! p) break;
			iacl = p + 1;
		}
	} else {
		while (1) {
			p = strchr (iacl, ',');
			if (nacl != fmd_entry->acl)
				*nacl++ = ',';
			*nacl++ = *iacl & ~ CNS_ACL_DEFAULT;
			switch (*iacl - '@') {
			case CNS_ACL_DEFAULT | CNS_ACL_USER_OBJ:
				*nacl++ = (*(iacl+1) & (mode >> 6 & 7)) + '0';
				fmd_entry->filemode = fmd_entry->filemode & 0177077 |
					(mode & (*(iacl+1) - '0') << 6);
				nacl += sprintf (nacl, "%d", fmd_entry->uid);
				break;
			case CNS_ACL_DEFAULT | CNS_ACL_GROUP_OBJ:
				*nacl++ = (*(iacl+1) & (mode >> 3 & 7)) + '0';
				fmd_entry->filemode = fmd_entry->filemode & 0177707 |
					(mode & (*(iacl+1) - '0') << 3);
				nacl += sprintf (nacl, "%d", fmd_entry->gid);
				break;
			case CNS_ACL_DEFAULT | CNS_ACL_MASK:
				*nacl++ = (*(iacl+1) & (mode >> 3 & 7)) + '0';
				fmd_entry->filemode = fmd_entry->filemode & 0177707 |
					(mode & (*(iacl+1) - '0') << 3);
				*nacl++ = '0';
				break;
			case CNS_ACL_DEFAULT | CNS_ACL_OTHER:
				*nacl++ = (*(iacl+1) & (mode & 7)) + '0';
				fmd_entry->filemode = fmd_entry->filemode & 0177770 |
					(mode & (*(iacl+1) - '0'));
				*nacl++ = '0';
				break;
			default:
				if (p)
					entry_len = p - iacl - 1;
				else
					entry_len = strlen (iacl) - 1;
				strncpy (nacl, iacl + 1, entry_len);
				nacl += entry_len;
			}
			if (! p) break;
			iacl = p + 1;
		}
	}

	/* if new CNS entry is a directory, copy default ACL */

	if (fmd_entry->filemode & S_IFDIR) {
		if (nacl != fmd_entry->acl)
			*nacl++ = ',';
		strcpy (nacl, pacl);
	} else
		*nacl = '\0';
	return (0);
}

/*      Cns_acl_validate - validate set of ACL entries */

Cns_acl_validate (struct Cns_acl *acl, int nentries, char *errbuf)
{
	struct Cns_acl *aclp;
	int i;
	int ndefs = 0;
	int ndg = 0;
	int ndgo = 0;
	int ndm = 0;
	int ndo = 0;
	int ndu = 0;
	int nduo = 0;
	int ng = 0;
	int ngo = 0;
	int nm = 0;
	int no = 0;
	int nu = 0;
	int nuo = 0;

	for (i = 0, aclp = acl; i < nentries; i++, aclp++) {
		switch (aclp->a_type) {
		case CNS_ACL_USER_OBJ:
			nuo++;
			break;
		case CNS_ACL_USER:
			nu++;
			break;
		case CNS_ACL_GROUP_OBJ:
			ngo++;
			break;
		case CNS_ACL_GROUP:
			ng++;
			break;
		case CNS_ACL_MASK:
			nm++;
			break;
		case CNS_ACL_OTHER:
			no++;
			break;
		case CNS_ACL_DEFAULT | CNS_ACL_USER_OBJ:
			ndefs++;
			nduo++;
			break;
		case CNS_ACL_DEFAULT | CNS_ACL_USER:
			ndefs++;
			ndu++;
			break;
		case CNS_ACL_DEFAULT | CNS_ACL_GROUP_OBJ:
			ndefs++;
			ndgo++;
			break;
		case CNS_ACL_DEFAULT | CNS_ACL_GROUP:
			ndefs++;
			ndg++;
			break;
		case CNS_ACL_DEFAULT | CNS_ACL_MASK:
			ndefs++;
			ndm++;
			break;
		case CNS_ACL_DEFAULT | CNS_ACL_OTHER:
			ndefs++;
			ndo++;
			break;
		default:
			strcpy (errbuf, "invalid acl type");
			return (-1);
		}
		if (aclp->a_perm > 7) {
			strcpy (errbuf, "invalid permission");
			return (-1);
		}
	}
	/* There must be one and only one of each type USER_OBJ, GROUP_OBJ, OTHER */
	if (nuo != 1 || ngo != 1 || no != 1) {
		strcpy (errbuf,
		    "There must be one and only one of each type USER_OBJ, GROUP_OBJ, OTHER");
		return (-1);
	}
	/* If there is any USER or GROUP entry, there must be a MASK entry */
	if ((nu || ng) && nm != 1) {
		strcpy (errbuf,
		    "If there is any USER or GROUP entry, there must be a MASK entry");
		return (-1);
	}
	/* If there are any default ACL entries, there must be one and only one
	   entry of each type DEF_USER_OBJ, DEF_GROUP_OBJ, DEF_OTHER */
	if (ndefs && (nduo != 1 || ndgo != 1 || ndo != 1)) {
		strcpy (errbuf,
		    "If there are any default ACL entries, there must be one and only one entry of each type DEF_USER_OBJ, DEF_GROUP_OBJ, DEF_OTHER");
		return (-1);
	}
	if ((ndu || ndg) && ndm != 1) {
		strcpy (errbuf,
		    "If there is any default USER or default GROUP entry, there must be a default MASK entry");
		return (-1);
	}

	/* check for duplicate entries USER or GROUP */

	for (i = 1, aclp = acl + 1; i < nentries; i++, aclp++) {
		if ((aclp->a_type == (aclp-1)->a_type &&
		    aclp->a_id == (aclp-1)->a_id)) {
			strcpy (errbuf,
			    "duplicate USER or GROUP entry");
			return (-1);
		}
	}
	return (0);
}

/*	Cns_chkaclperm - check access permissions */

Cns_chkaclperm (struct Cns_file_metadata *fmd_entry, int mode, uid_t uid, int nbgids, gid_t *gids)
{
	int acc_perm = 0;
	char acl_mask = 0x7F;
	int acl_id;
	char *iacl;
	int nbgroups = 0;
	char *p;

	/* check USER */

	if (fmd_entry->uid == uid)
		return ((fmd_entry->filemode & mode) != mode);

	/* Get CNS_ACL_MASK if any */

	if (iacl = strchr (fmd_entry->acl, CNS_ACL_MASK|'@'))
		acl_mask = *(iacl + 1) - '0';
	mode >>= 6;

	/* check CNS_ACL_USER entries if any */

	for (iacl = fmd_entry->acl; iacl; iacl = p) {
		if (*iacl - '@' > CNS_ACL_USER) break;
		p = strchr (iacl, ',');
		if (p) p++;
		if (*iacl - '@' < CNS_ACL_USER) continue;
		acl_id = atoi (iacl + 2);
		if (uid == acl_id)
			return ((*(iacl + 1) & acl_mask & mode) != mode);
		if (uid < acl_id) break;
	}

	/* check GROUP */

	iacl = strchr (iacl, CNS_ACL_GROUP_OBJ|'@');
	if (Cgroupmatch (fmd_entry->gid, nbgids, gids)) {
		acc_perm = *(iacl + 1);
		nbgroups++;
		if (acl_mask == 0x7F)	/* no extended ACLs */
			return ((acc_perm & acl_mask & mode) != mode);
	}

	/* check CNS_ACL_GROUP entries if any */

	for ( ; iacl; iacl = p) {
		if (*iacl - '@' > CNS_ACL_GROUP) break;
		p = strchr (iacl, ',');
		if (p) p++;
		if (*iacl - '@' < CNS_ACL_GROUP) continue;
		acl_id = atoi (iacl + 2);
		if (Cgroupmatch (acl_id, nbgids, gids)) {
			acc_perm |= *(iacl + 1);
			nbgroups++;
		}
	}
	if (nbgroups)
		return ((acc_perm & acl_mask & mode) != mode);

	/* OTHER */

	return ((fmd_entry->filemode & mode) != mode);
}