Debian Codegraph @

modules/mod_facl.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server daemon
3 * Copyright (c) 2004-2024 The ProFTPD Project team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, the ProFTPD Project team and other respective
20 * copyright holders give permission to link this program with OpenSSL, and
21 * distribute the resulting executable, without including the source code
22 * for OpenSSL in the source distribution.
23 */
24
25 /* POSIX ACL checking code (aka POSIX.1e hell)
26 */
27
28 #include "conf.h"
29
30 #define MOD_FACL_VERSION "mod_facl/0.6"
31
32 /* Make sure the version of proftpd is as necessary. */
33 #if PROFTPD_VERSION_NUMBER < 0x0001030101
34 # error "ProFTPD 1.3.1rc1 or later required"
35 #endif
36
37 module facl_module;
38
39 static int facl_engine = TRUE;
40 static const char *trace_channel = "facl";
41
42 #ifdef HAVE_POSIX_ACL
43
44 #ifdef HAVE_SYS_ACL_H
45 # include <sys/acl.h>
46 #endif
47
48 /* This header appears on Linux. */
49 #ifdef HAVE_ACL_LIBACL_H
50 # include <acl/libacl.h>
51 #endif
52
53 static int is_errno_eperm(int xerrno) {
54 if (xerrno == EPERM) {
55 return TRUE;
56 }
57
58 #if defined(ENOTSUP)
59 if (xerrno == ENOTSUP) {
60 return TRUE;
61 }
62 #endif /* !ENOTSUP */
63
64 #if defined(EOPNOTSUPP)
65 if (xerrno == EOPNOTSUPP) {
66 return TRUE;
67 }
68 #endif /* !EOPNOTSUPP */
69
70 return FALSE;
71 }
72
73 static int facl_access(pr_fs_t *fs, const char *path, int mode, uid_t uid,
74 gid_t gid, array_header *suppl_gids) {
75 struct stat st;
76
77 pr_fs_clear_cache2(path);
78 if (pr_fsio_stat(path, &st) < 0) {
79 return -1;
80 }
81
82 return pr_fs_have_access(&st, mode, uid, gid, suppl_gids);
83 }
84
85 static int facl_faccess(pr_fh_t *fh, int mode, uid_t uid, gid_t gid,
86 array_header *suppl_gids) {
87 return facl_access(fh->fh_fs, fh->fh_path, mode, uid, gid, suppl_gids);
88 }
89
90 #if defined(HAVE_BSD_POSIX_ACL) || \
91 defined(HAVE_LINUX_POSIX_ACL)
92 static acl_perm_t get_facl_perm_for_mode(int mode) {
93 acl_perm_t res;
94
95 memset(&res, 0, sizeof(acl_perm_t));
96
97 if (mode & R_OK) {
98 res |= ACL_READ;
99 }
100
101 if (mode & W_OK) {
102 res |= ACL_WRITE;
103 }
104
105 if (mode & X_OK) {
106 res |= ACL_EXECUTE;
107 }
108
109 return res;
110 }
111
112 static int check_bsd_facl(pool *p, const char *path, int mode, void *acl,
113 int nents, struct stat *st, uid_t uid, gid_t gid,
114 array_header *suppl_gids) {
115 register unsigned int i;
116 int have_access_entry = FALSE, res = -1;
117 pool *acl_pool;
118 char *acl_text;
119 acl_t facl = acl;
120 acl_entry_t ae;
121 acl_tag_t ae_type;
122 acl_entry_t acl_user_entry = NULL;
123 acl_entry_t acl_group_entry = NULL;
124 acl_entry_t acl_other_entry = NULL;
125 acl_entry_t acl_mask_entry = NULL;
126 array_header *acl_groups;
127 array_header *acl_users;
128
129 acl_text = acl_to_text(facl, NULL);
130 if (acl_text != NULL) {
131 pr_trace_msg(trace_channel, 8,
132 "checking path '%s', ACL '%s'", path, acl_text);
133 acl_free(acl_text);
134 }
135
136 /* Iterate through all of the ACL entries, sorting them for later
137 * checking.
138 */
139 res = acl_get_entry(facl, ACL_FIRST_ENTRY, &ae);
140 if (res < 0) {
141 pr_trace_msg(trace_channel, 8,
142 "unable to retrieve first ACL entry for '%s': [%d] %s", path,
143 errno, strerror(errno));
144 errno = EACCES;
145 return -1;
146 }
147
148 if (res == 0) {
149 pr_trace_msg(trace_channel, 3, "ill-formed ACL for '%s' has no entries",
150 path);
151 errno = EACCES;
152 return -1;
153 }
154
155 acl_pool = make_sub_pool(p);
156 pr_pool_tag(acl_pool, "BSD/Linux ACL pool");
157
158 acl_groups = make_array(acl_pool, 1, sizeof(acl_entry_t));
159 acl_users = make_array(acl_pool, 1, sizeof(acl_entry_t));
160
161 while (res > 0) {
162 pr_signals_handle();
163
164 if (acl_get_tag_type(ae, &ae_type) < 0) {
165 pr_trace_msg(trace_channel, 5,
166 "error retrieving type of ACL entry for '%s': %s", path,
167 strerror(errno));
168 res = acl_get_entry(facl, ACL_NEXT_ENTRY, &ae);
169 continue;
170 }
171
172 if (ae_type & ACL_USER_OBJ) {
173 acl_user_entry = ae;
174
175 } else if (ae_type & ACL_USER) {
176 acl_entry_t *ae_dup = push_array(acl_users);
177 *ae_dup = ae;
178
179 } else if (ae_type & ACL_GROUP_OBJ) {
180 acl_group_entry = ae;
181
182 } else if (ae_type & ACL_GROUP) {
183 acl_entry_t *ae_dup = push_array(acl_groups);
184 *ae_dup = ae;
185
186 } else if (ae_type & ACL_OTHER) {
187 acl_other_entry = ae;
188
189 } else if (ae_type & ACL_MASK) {
190 acl_mask_entry = ae;
191 }
192
193 res = acl_get_entry(facl, ACL_NEXT_ENTRY, &ae);
194 }
195 ae = NULL;
196
197 /* Select the ACL entry that determines access. */
198 res = -1;
199
200 /* 1. If the given user ID matches the file owner, use that entry for
201 * access.
202 */
203 if (uid == st->st_uid) {
204 /* Check the acl_user_entry for access. */
205 ae = acl_user_entry;
206 ae_type = ACL_USER_OBJ;
207 have_access_entry = TRUE;
208
209 pr_trace_msg(trace_channel, 10, "user ID %s matches ACL owner user ID",
210 pr_uid2str(NULL, uid));
211 }
212
213 /* 2. If not matched above, and if the given user ID matches one of the
214 * named user entries, use that entry for access.
215 */
216 for (i = 0; !have_access_entry && i < acl_users->nelts; i++) {
217 acl_entry_t e = ((acl_entry_t *) acl_users->elts)[i];
218
219 if (uid == *((uid_t *) acl_get_qualifier(e))) {
220
221 /* Check this entry for access. Note that it'll need to
222 * be modified by the mask, if any, later.
223 */
224 ae = e;
225 ae_type = ACL_USER;
226 have_access_entry = TRUE;
227
228 pr_trace_msg(trace_channel, 10,
229 "user ID %s matches ACL allowed users list", pr_uid2str(NULL, uid));
230
231 break;
232 }
233 }
234
235 /* 3. If not matched above, and if one of the group IDs matches the
236 * group owner entry, and the group owner entry contains the
237 * requested permissions, use that entry for access.
238 */
239 if (!have_access_entry &&
240 gid == st->st_gid) {
241 int ret;
242
243 /* Check the acl_group_entry for access. First though, we need to
244 * see if the acl_group_entry contains the requested permissions.
245 */
246 acl_permset_t perms;
247 if (acl_get_permset(acl_group_entry, &perms) < 0) {
248 pr_trace_msg(trace_channel, 5, "error retrieving permission set: %s",
249 strerror(errno));
250 }
251
252 # if defined(HAVE_BSD_POSIX_ACL)
253 ret = acl_get_perm_np(perms, get_facl_perm_for_mode(mode));
254 # elif defined(HAVE_LINUX_POSIX_ACL)
255 ret = acl_get_perm(perms, get_facl_perm_for_mode(mode));
256 # endif
257 if (ret == 1) {
258 ae = acl_group_entry;
259 ae_type = ACL_GROUP_OBJ;
260 have_access_entry = TRUE;
261
262 pr_trace_msg(trace_channel, 10,
263 "primary group ID %s matches ACL owner group ID",
264 pr_gid2str(NULL, gid));
265
266 } else if (ret < 0) {
267 int xerrno = errno;
268
269 pr_trace_msg(trace_channel, 5,
270 "error checking permissions in permission set: %s", strerror(xerrno));
271 errno = xerrno;
272 }
273 }
274
275 if (suppl_gids) {
276 for (i = 0; !have_access_entry && i < suppl_gids->nelts; i++) {
277 gid_t suppl_gid = ((gid_t *) suppl_gids->elts)[i];
278
279 if (suppl_gid == st->st_gid) {
280 int ret;
281
282 /* Check the acl_group_entry for access. First though, we need to
283 * see if the acl_group_entry contains the requested permissions.
284 */
285 acl_permset_t perms;
286 if (acl_get_permset(acl_group_entry, &perms) < 0) {
287 pr_trace_msg(trace_channel, 5, "error retrieving permission set: %s",
288 strerror(errno));
289 }
290
291 # if defined(HAVE_BSD_POSIX_ACL)
292 ret = acl_get_perm_np(perms, get_facl_perm_for_mode(mode));
293 # elif defined(HAVE_LINUX_POSIX_ACL)
294 ret = acl_get_perm(perms, get_facl_perm_for_mode(mode));
295 # endif
296 if (ret == 1) {
297 ae = acl_group_entry;
298 ae_type = ACL_GROUP_OBJ;
299 have_access_entry = TRUE;
300
301 pr_trace_msg(trace_channel, 10,
302 "supplemental group ID %s matches ACL owner group ID",
303 pr_gid2str(NULL, suppl_gid));
304
305 break;
306
307 } else if (ret < 0) {
308 int xerrno = errno;
309
310 pr_trace_msg(trace_channel, 5,
311 "error checking permissions in permission set: %s",
312 strerror(xerrno));
313 errno = xerrno;
314 }
315 }
316 }
317 }
318
319 /* 4. If not matched above, and if one of the group IDs matches one
320 * of the named group entries, and that entry contains the requested
321 * permissions, use that entry for access.
322 */
323 for (i = 0; !have_access_entry && i < acl_groups->nelts; i++) {
324 acl_entry_t e = ((acl_entry_t *) acl_groups->elts)[i];
325
326 if (gid == *((gid_t *) acl_get_qualifier(e))) {
327 int ret;
328
329 /* Check this entry for access. Note that it'll need to
330 * be modified by the mask, if any, later.
331 */
332 acl_permset_t perms;
333 if (acl_get_permset(e, &perms) < 0) {
334 pr_trace_msg(trace_channel, 5, "error retrieving permission set: %s",
335 strerror(errno));
336 }
337
338 # if defined(HAVE_BSD_POSIX_ACL)
339 ret = acl_get_perm_np(perms, get_facl_perm_for_mode(mode));
340 # elif defined(HAVE_LINUX_POSIX_ACL)
341 ret = acl_get_perm(perms, get_facl_perm_for_mode(mode));
342 # endif
343 if (ret == 1) {
344 ae = e;
345 ae_type = ACL_GROUP;
346 have_access_entry = TRUE;
347
348 pr_trace_msg(trace_channel, 10,
349 "primary group ID %s matches ACL allowed groups list",
350 pr_gid2str(NULL, gid));
351
352 break;
353
354 } else if (ret < 0) {
355 int xerrno = errno;
356
357 pr_trace_msg(trace_channel, 5,
358 "error checking permissions in permission set: %s", strerror(errno));
359 errno = xerrno;
360 }
361 }
362
363 if (suppl_gids) {
364 register unsigned int j;
365
366 for (j = 0; !have_access_entry && j < suppl_gids->nelts; j++) {
367 gid_t suppl_gid = ((gid_t *) suppl_gids->elts)[j];
368
369 if (suppl_gid == *((gid_t *) acl_get_qualifier(e))) {
370 int ret;
371
372 /* Check this entry for access. Note that it'll need to
373 * be modified by the mask, if any, later.
374 */
375 acl_permset_t perms;
376 if (acl_get_permset(e, &perms) < 0) {
377 pr_trace_msg(trace_channel, 5,
378 "error retrieving permission set: %s", strerror(errno));
379 }
380
381 # if defined(HAVE_BSD_POSIX_ACL)
382 ret = acl_get_perm_np(perms, get_facl_perm_for_mode(mode));
383 # elif defined(HAVE_LINUX_POSIX_ACL)
384 ret = acl_get_perm(perms, get_facl_perm_for_mode(mode));
385 # endif
386 if (ret == 1) {
387 ae = e;
388 ae_type = ACL_GROUP;
389 have_access_entry = TRUE;
390
391 pr_trace_msg(trace_channel, 10,
392 "supplemental group ID %s matches ACL allowed groups list",
393 pr_gid2str(NULL, suppl_gid));
394
395 break;
396
397 } else if (ret < 0) {
398 int xerrno = errno;
399
400 pr_trace_msg(trace_channel, 5,
401 "error checking permissions in permission set: %s",
402 strerror(xerrno));
403 errno = xerrno;
404 }
405 }
406 }
407 }
408 }
409
410 /* 5. If not matched above, and if one of the group IDs matches
411 * the group owner or any of the named group entries, but neither
412 * the group owner entry nor any of the named group entries contains
413 * the requested permissions, access is denied.
414 */
415
416 /* XXX implement this condition properly */
417
418 /* 6. If not matched above, the other entry determines access.
419 */
420 if (!have_access_entry) {
421 ae = acl_other_entry;
422 ae_type = ACL_OTHER;
423 have_access_entry = TRUE;
424
425 pr_trace_msg(trace_channel, 10, "using ACL 'other' list");
426 }
427
428 /* Access determination:
429 *
430 * If either the user owner entry or other entry were used, and the
431 * entry contains the requested permissions, access is permitted.
432 *
433 * Otherwise, if the selected entry and the mask entry both contain
434 * the requested permissions (or there is no mask entry), access is
435 * permitted.
436 *
437 * Otherwise, access is denied.
438 */
439 switch (ae_type) {
440 case ACL_USER_OBJ:
441 case ACL_OTHER: {
442 int ret;
443
444 acl_permset_t perms;
445 if (acl_get_permset(ae, &perms) < 0) {
446 pr_trace_msg(trace_channel, 5, "error retrieving permission set: %s",
447 strerror(errno));
448 }
449
450 # if defined(HAVE_BSD_POSIX_ACL)
451 ret = acl_get_perm_np(perms, get_facl_perm_for_mode(mode));
452 # elif defined(HAVE_LINUX_POSIX_ACL)
453 ret = acl_get_perm(perms, get_facl_perm_for_mode(mode));
454 # endif
455 if (ret == 1) {
456 res = 0;
457
458 } else if (ret < 0) {
459 int xerrno = errno;
460
461 pr_trace_msg(trace_channel, 5,
462 "error checking permissions in permission set: %s", strerror(xerrno));
463 errno = xerrno;
464 }
465
466 break;
467 }
468
469 default: {
470 int ret1, ret2;
471 acl_permset_t ent_perms, mask_perms;
472
473 if (acl_get_permset(ae, &ent_perms) < 0) {
474 pr_trace_msg(trace_channel, 5, "error retrieving permission set: %s",
475 strerror(errno));
476 }
477
478 # if defined(HAVE_BSD_POSIX_ACL)
479 ret1 = acl_get_perm_np(ent_perms, get_facl_perm_for_mode(mode));
480 # elif defined(HAVE_LINUX_POSIX_ACL)
481 ret1 = acl_get_perm(ent_perms, get_facl_perm_for_mode(mode));
482 # endif
483
484 if (acl_mask_entry != NULL) {
485 if (acl_get_permset(acl_mask_entry, &mask_perms) < 0) {
486 pr_trace_msg(trace_channel, 5,
487 "error retrieving mask permission set: %s", strerror(errno));
488 }
489
490 # if defined(HAVE_BSD_POSIX_ACL)
491 ret2 = acl_get_perm_np(mask_perms, get_facl_perm_for_mode(mode));
492 # elif defined(HAVE_LINUX_POSIX_ACL)
493 ret2 = acl_get_perm(mask_perms, get_facl_perm_for_mode(mode));
494 # endif
495
496 } else {
497 /* If there is no mask entry, then access should be granted. */
498 ret2 = 1;
499 }
500
501 if (ret1 == 1 && ret2 == 1) {
502 res = 0;
503
504 } else {
505 if (ret1 < 0) {
506 int xerrno = errno;
507
508 pr_trace_msg(trace_channel, 5,
509 "error checking permissions in entry permission set: %s",
510 strerror(xerrno));
511 errno = xerrno;
512 }
513
514 if (ret2 < 0) {
515 int xerrno = errno;
516
517 pr_trace_msg(trace_channel, 5,
518 "error checking permissions in mask permission set: %s",
519 strerror(xerrno));
520 errno = xerrno;
521 }
522 }
523
524 break;
525 }
526 }
527
528 destroy_pool(acl_pool);
529
530 if (res < 0) {
531 pr_trace_msg(trace_channel, 3,
532 "returning EACCES for path '%s', user ID %s", path,
533 pr_uid2str(NULL, uid));
534 errno = EACCES;
535 }
536
537 return res;
538 }
539 #endif /* BSD/Linux POSIX ACL */
540
541 #if defined(HAVE_MACOSX_POSIX_ACL)
542 static acl_perm_t get_facl_perm_for_mode(int mode) {
543 acl_perm_t res;
544
545 memset(&res, 0, sizeof(acl_perm_t));
546
547 if (mode & R_OK) {
548 res |= ACL_READ_DATA;
549 }
550
551 if (mode & W_OK) {
552 res |= ACL_WRITE_DATA;
553 }
554
555 if (mode & X_OK) {
556 res |= ACL_EXECUTE;
557 }
558
559 return res;
560 }
561
562 static int check_macosx_facl(pool *p, const char *path, int mode, void *acl,
563 int nents, struct stat *st, uid_t uid, gid_t gid,
564 array_header *suppl_gids) {
565 int have_access = FALSE, res = -1;
566 char *acl_text;
567 acl_t facl = acl;
568 acl_entry_t ae;
569
570 acl_text = acl_to_text(facl, NULL);
571 if (acl_text != NULL) {
572 pr_trace_msg(trace_channel, 8,
573 "checking path '%s', ACL '%s'", path, acl_text);
574 acl_free(acl_text);
575 }
576
577 /* Iterate through all of the ACL entries, sorting them for later
578 * checking.
579 */
580 res = acl_get_entry(facl, ACL_FIRST_ENTRY, &ae);
581 if (res < 0) {
582 pr_trace_msg(trace_channel, 8,
583 "unable to retrieve first ACL entry for '%s': [%d] %s", path,
584 errno, strerror(errno));
585 errno = EACCES;
586 return -1;
587 }
588
589 if (res == 0) {
590 pr_trace_msg(trace_channel, 3, "ill-formed ACL for '%s' has no entries",
591 path);
592 errno = EACCES;
593 return -1;
594 }
595
596 while (res > 0) {
597 acl_tag_t ae_type;
598
599 pr_signals_handle();
600
601 if (acl_get_tag_type(ae, &ae_type) < 0) {
602 pr_trace_msg(trace_channel, 5,
603 "error retrieving type of ACL entry for '%s': %s", path,
604 strerror(errno));
605 res = acl_get_entry(facl, ACL_NEXT_ENTRY, &ae);
606 continue;
607 }
608
609 if (ae_type & ACL_TYPE_EXTENDED) {
610 int ret;
611
612 acl_permset_t perms;
613 if (acl_get_permset(ae, &perms) < 0) {
614 pr_trace_msg(trace_channel, 5, "error retrieving permission set: %s",
615 strerror(errno));
616 }
617
618 ret = acl_get_perm_np(perms, get_facl_perm_for_mode(mode));
619 if (ret == 1) {
620 have_access = TRUE;
621 break;
622
623 } else if (ret < 0) {
624 int xerrno = errno;
625
626 pr_trace_msg(trace_channel, 5,
627 "error checking permissions in permission set: %s", strerror(xerrno));
628 errno = xerrno;
629 }
630 }
631
632 res = acl_get_entry(facl, ACL_NEXT_ENTRY, &ae);
633 }
634
635 if (!have_access) {
636 pr_trace_msg(trace_channel, 3,
637 "returning EACCES for path '%s', user ID %s", path,
638 pr_uid2str(NULL, uid));
639 errno = EACCES;
640 return -1;
641 }
642
643 return 0;
644 }
645 #endif /* MacOSX POSIX ACL */
646
647 #if defined(HAVE_SOLARIS_POSIX_ACL)
648 static int check_solaris_facl(pool *p, const char *path, int mode, void *acl,
649 int nents, struct stat *st, uid_t uid, gid_t gid,
650 array_header *suppl_gids) {
651 register unsigned int i;
652 int have_access_entry = FALSE, have_mask_entry = FALSE, idx, res = -1;
653 pool *acl_pool;
654 aclent_t *acls = acl;
655 aclent_t ae;
656 int ae_type = 0;
657 aclent_t acl_user_entry;
658 aclent_t acl_group_entry;
659 aclent_t acl_other_entry;
660 aclent_t acl_mask_entry;
661 array_header *acl_groups;
662 array_header *acl_users;
663
664 /* In the absence of any clear documentation, I'll assume that
665 * Solaris ACLs follow the same selection and checking algorithm
666 * as do BSD and Linux.
667 */
668
669 res = aclcheck(acls, nents, &idx);
670 switch (res) {
671 case 0:
672 break;
673
674 case GRP_ERROR:
675 pr_trace_msg(trace_channel, 3, "ill-formed ACL for '%s': %s",
676 path, "too many GROUP entries");
677 errno = EACCES;
678 return -1;
679
680 case USER_ERROR:
681 pr_trace_msg(trace_channel, 3, "ill-formed ACL for '%s': %s",
682 path, "too many USER entries");
683 errno = EACCES;
684 return -1;
685
686 case OTHER_ERROR:
687 pr_trace_msg(trace_channel, 3, "ill-formed ACL for '%s': %s",
688 path, "too many OTHER entries");
689 errno = EACCES;
690 return -1;
691
692 case CLASS_ERROR:
693 pr_trace_msg(trace_channel, 3, "ill-formed ACL for '%s': %s",
694 path, "too many CLASS entries");
695 errno = EACCES;
696 return -1;
697
698 case DUPLICATE_ERROR:
699 pr_trace_msg(trace_channel, 3, "ill-formed ACL for '%s': %s",
700 path, "duplicate entries");
701 errno = EACCES;
702 return -1;
703
704 case MISS_ERROR:
705 pr_trace_msg(trace_channel, 3, "ill-formed ACL for '%s': %s",
706 path, "missing required entry");
707 errno = EACCES;
708 return -1;
709
710 case MEM_ERROR:
711 pr_trace_msg(trace_channel, 3, "ill-formed ACL for '%s': %s",
712 path, "Out of memory!");
713 errno = EACCES;
714 return -1;
715
716 case ENTRY_ERROR:
717 pr_trace_msg(trace_channel, 3, "ill-formed ACL for '%s': %s",
718 path, "invalid entry type");
719 errno = EACCES;
720 return -1;
721 }
722
723 /* Iterate through all of the ACL entries, sorting them for later
724 * checking.
725 */
726
727 acl_pool = make_sub_pool(p);
728 pr_pool_tag(acl_pool, "Solaris ACL pool");
729
730 acl_groups = make_array(acl_pool, 1, sizeof(aclent_t));
731 acl_users = make_array(acl_pool, 1, sizeof(aclent_t));
732
733 for (i = 0; i < nents; i++) {
734 if (acls[i].a_type & USER_OBJ) {
735 memcpy(&acl_user_entry, &(acls[i]), sizeof(aclent_t));
736
737 } else if (acls[i].a_type & USER) {
738 aclent_t *ae_dup = push_array(acl_users);
739 memcpy(ae_dup, &(acls[i]), sizeof(aclent_t));
740
741 } else if (acls[i].a_type & GROUP_OBJ) {
742 memcpy(&acl_group_entry, &(acls[i]), sizeof(aclent_t));
743
744 } else if (acls[i].a_type & GROUP) {
745 aclent_t *ae_dup = push_array(acl_groups);
746 memcpy(ae_dup, &(acls[i]), sizeof(aclent_t));
747
748 } else if (acls[i].a_type & OTHER_OBJ) {
749 memcpy(&acl_other_entry, &(acls[i]), sizeof(aclent_t));
750
751 } else if (acls[i].a_type & CLASS_OBJ) {
752 memcpy(&acl_mask_entry, &(acls[i]), sizeof(aclent_t));
753 have_mask_entry = TRUE;
754 }
755 }
756
757 /* Select the ACL entry that determines access. */
758 res = -1;
759
760 /* 1. If the given user ID matches the file owner, use that entry for
761 * access.
762 */
763 if (uid == st->st_uid) {
764 /* Check the acl_user_entry for access. */
765 memcpy(&ae, &acl_user_entry, sizeof(aclent_t));
766 ae_type = USER_OBJ;
767 have_access_entry = TRUE;
768
769 pr_trace_msg(trace_channel, 10, "user ID %s matches ACL owner user ID",
770 pr_uid2str(NULL, uid));
771 }
772
773 /* 2. If not matched above, and f the given user ID matches one of the
774 * named user entries, use that entry for access.
775 */
776 for (i = 0; !have_access_entry && i < acl_users->nelts; i++) {
777 aclent_t e;
778 memcpy(&e, &(((aclent_t *) acl_users->elts)[i]), sizeof(aclent_t));
779
780 if (uid == e.a_id) {
781
782 /* Check this entry for access. Note that it'll need to
783 * be modified by the mask, if any, later.
784 */
785 memcpy(&ae, &e, sizeof(aclent_t));
786 ae_type = USER;
787 have_access_entry = TRUE;
788
789 pr_trace_msg(trace_channel, 10,
790 "user ID %s matches ACL allowed users list", pr_uid2str(NULL, uid));
791
792 break;
793 }
794 }
795
796 /* 3. If not matched above, and if one of the group IDs matches the
797 * group owner entry, and the group owner entry contains the
798 * requested permissions, use that entry for access.
799 */
800 if (!have_access_entry &&
801 gid == st->st_gid) {
802
803 /* Check the acl_group_entry for access. First though, we need to
804 * see if the acl_group_entry contains the requested permissions.
805 */
806 if (acl_group_entry.a_perm & mode) {
807 memcpy(&ae, &acl_group_entry, sizeof(aclent_t));
808 ae_type = GROUP_OBJ;
809 have_access_entry = TRUE;
810
811 pr_trace_msg(trace_channel, 10,
812 "primary group ID %s matches ACL owner group ID",
813 pr_gid2str(NULL, gid));
814 }
815 }
816
817 if (suppl_gids) {
818 for (i = 0; !have_access_entry && i < suppl_gids->nelts; i++) {
819 gid_t suppl_gid = ((gid_t *) suppl_gids->elts)[i];
820
821 if (suppl_gid == st->st_gid) {
822 /* Check the acl_group_entry for access. First though, we need to
823 * see if the acl_group_entry contains the requested permissions.
824 */
825 if (acl_group_entry.a_perm & mode) {
826 memcpy(&ae, &acl_group_entry, sizeof(aclent_t));
827 ae_type = GROUP_OBJ;
828 have_access_entry = TRUE;
829
830 pr_trace_msg(trace_channel, 10,
831 "supplemental group ID %s matches ACL owner group ID",
832 pr_gid2str(NULL, suppl_gid));
833
834 break;
835 }
836 }
837 }
838 }
839
840 /* 4. If not matched above, and if one of the group IDs matches one
841 * of the named group entries, and that entry contains the requested
842 * permissions, use that entry for access.
843 */
844 for (i = 0; !have_access_entry && i < acl_groups->nelts; i++) {
845 aclent_t e;
846 memcpy(&e, &(((aclent_t *) acl_groups->elts)[i]), sizeof(aclent_t));
847
848 if (gid == e.a_id) {
849
850 /* Check this entry for access. Note that it'll need to
851 * be modified by the mask, if any, later.
852 */
853 if (e.a_perm & mode) {
854 memcpy(&ae, &e, sizeof(aclent_t));
855 ae_type = GROUP;
856 have_access_entry = TRUE;
857
858 pr_trace_msg(trace_channel, 10,
859 "primary group ID %s matches ACL allowed groups list",
860 pr_gid2str(NULL, gid));
861
862 break;
863 }
864 }
865
866 if (suppl_gids) {
867 register unsigned int j;
868
869 for (j = 0; !have_access_entry && j < suppl_gids->nelts; j++) {
870 gid_t suppl_gid = ((gid_t *) suppl_gids->elts)[j];
871
872 if (suppl_gid == e.a_id) {
873 /* Check this entry for access. Note that it'll need to
874 * be modified by the mask, if any, later.
875 */
876 if (e.a_perm & mode) {
877 memcpy(&ae, &e, sizeof(aclent_t));
878 ae_type = GROUP;
879 have_access_entry = TRUE;
880
881 pr_trace_msg(trace_channel, 10,
882 "supplemental group ID %s matches ACL allowed groups list",
883 pr_gid2str(NULL, suppl_gid));
884
885 break;
886 }
887 }
888 }
889 }
890 }
891
892 /* 5. If not matched above, and if one of the group IDs matches
893 * the group owner or any of the named group entries, but neither
894 * the group owner entry nor any of the named group entries contains
895 * the requested permissions, access is denied.
896 */
897
898 /* XXX implement this condition properly */
899
900 /* 6. If not matched above, the other entry determines access.
901 */
902 if (!have_access_entry) {
903 memcpy(&ae, &acl_other_entry, sizeof(aclent_t));
904 ae_type = OTHER_OBJ;
905 have_access_entry = TRUE;
906
907 pr_trace_msg(trace_channel, 10, "using ACL 'other' list");
908 }
909
910 /* Access determination:
911 *
912 * If either the user owner entry or other entry were used, and the
913 * entry contains the requested permissions, access is permitted.
914 *
915 * Otherwise, if the selected entry and the mask entry both contain
916 * the requested permissions (or there is no mask entry), access is
917 * permitted.
918 *
919 * Otherwise, access is denied.
920 */
921 switch (ae_type) {
922 case USER_OBJ:
923 case OTHER_OBJ:
924 if (ae.a_perm & mode) {
925 res = 0;
926 }
927 break;
928
929 default:
930 if (have_mask_entry) {
931 if ((ae.a_perm & mode) &&
932 (acl_mask_entry.a_perm & mode)) {
933 res = 0;
934 }
935
936 } else {
937
938 /* If there is no mask entry, then access should be granted. */
939 if (ae.a_perm & mode) {
940 res = 0;
941 }
942 }
943
944 break;
945 }
946
947 destroy_pool(acl_pool);
948
949 if (res < 0) {
950 pr_trace_msg(trace_channel, 3,
951 "returning EACCES for path '%s', user ID %s", path,
952 pr_uid2str(NULL, uid));
953 errno = EACCES;
954 }
955
956 return res;
957 }
958 #endif /* Solaris POSIX ACL */
959
960 static int check_facl(pool *p, const char *path, int mode, void *acl, int nents,
961 struct stat *st, uid_t uid, gid_t gid, array_header *suppl_gids) {
962 int res = -1;
963
964 # if defined(HAVE_BSD_POSIX_ACL) || \
965 defined(HAVE_LINUX_POSIX_ACL)
966 res = check_bsd_facl(p, path, mode, acl, nents, st, uid, gid, suppl_gids);
967
968 # elif defined(HAVE_MACOSX_POSIX_ACL)
969 res = check_macosx_facl(p, path, mode, acl, nents, st, uid, gid, suppl_gids);
970
971 # elif defined(HAVE_SOLARIS_POSIX_ACL)
972 res = check_solaris_facl(p, path, mode, acl, nents, st, uid, gid, suppl_gids);
973 # endif
974
975 return res;
976 }
977
978 # if defined(PR_USE_FACL)
979
980 /* FSIO handlers
981 */
982
983 static int facl_fsio_access(pr_fs_t *fs, const char *path, int mode,
984 uid_t uid, gid_t gid, array_header *suppl_gids) {
985 const char *real_path = NULL;
986 int nents = 0, res, xerrno;
987 struct stat st;
988 void *acls;
989 pool *tmp_pool = NULL;
990
991 pr_fs_clear_cache2(path);
992 if (pr_fsio_stat(path, &st) < 0) {
993 return -1;
994 }
995
996 tmp_pool = make_sub_pool(fs->fs_pool);
997 pr_pool_tag(tmp_pool, "mod_facl access(2) pool");
998
999 real_path = pr_fsio_realpath(tmp_pool, path);
1000 if (real_path != NULL) {
1001 path = real_path;
1002 }
1003
1004 /* Look up the acl for this path. */
1005 # if defined(HAVE_BSD_POSIX_ACL) || \
1006 defined(HAVE_LINUX_POSIX_ACL) || \
1007 defined(HAVE_MACOSX_POSIX_ACL)
1008 acls = acl_get_file(path, ACL_TYPE_ACCESS);
1009 if (acls == NULL) {
1010 xerrno = errno;
1011
1012 pr_trace_msg(trace_channel, 5, "unable to retrieve ACL for '%s': [%d] %s",
1013 path, xerrno, strerror(xerrno));
1014
1015 if (is_errno_eperm(xerrno) == TRUE) {
1016 pr_trace_msg(trace_channel, 3, "ACL retrieval operation not supported "
1017 "for '%s', falling back to normal access check", path);
1018
1019 if (facl_access(fs, path, mode, uid, gid, suppl_gids) < 0) {
1020 xerrno = errno;
1021
1022 pr_trace_msg(trace_channel, 6, "normal access check for '%s' "
1023 "failed: %s", path, strerror(xerrno));
1024 destroy_pool(tmp_pool);
1025 errno = xerrno;
1026 return -1;
1027 }
1028
1029 destroy_pool(tmp_pool);
1030 return 0;
1031 }
1032
1033 destroy_pool(tmp_pool);
1034 errno = xerrno;
1035 return -1;
1036 }
1037
1038 # elif defined(HAVE_SOLARIS_POSIX_ACL)
1039
1040 nents = acl(path, GETACLCNT, 0, NULL);
1041 if (nents < 0) {
1042 xerrno = errno;
1043
1044 pr_trace_msg(trace_channel, 5,
1045 "unable to retrieve ACL count for '%s': [%d] %s", path, xerrno,
1046 strerror(xerrno));
1047
1048 if (is_errno_eperm(xerrno) == TRUE) {
1049 pr_trace_msg(trace_channel, 3, "ACL retrieval operation not supported "
1050 "for '%s', falling back to normal access check", path);
1051
1052 if (facl_access(fs, path, mode, uid, gid, suppl_gids) < 0) {
1053 xerrno = errno;
1054
1055 pr_trace_msg(trace_channel, 6, "normal access check for '%s' "
1056 "failed: %s", path, strerror(xerrno));
1057 destroy_pool(tmp_pool);
1058 errno = xerrno;
1059 return -1;
1060 }
1061
1062 destroy_pool(tmp_pool);
1063 return 0;
1064 }
1065
1066 destroy_pool(tmp_pool);
1067 errno = xerrno;
1068 return -1;
1069 }
1070
1071 pr_trace_msg(trace_channel, 10,
1072 "acl(2) returned %d ACL entries for path '%s'", nents, path);
1073
1074 acls = pcalloc(tmp_pool, nents * sizeof(aclent_t));
1075
1076 nents = acl(path, GETACL, nents, acls);
1077 if (nents < 0) {
1078 xerrno = errno;
1079
1080 pr_trace_msg(trace_channel, 5,
1081 "unable to retrieve ACL for '%s': [%d] %s", path, xerrno,
1082 strerror(xerrno));
1083
1084 if (is_errno_eperm(xerrno) == TRUE) {
1085 pr_trace_msg(trace_channel, 3, "ACL retrieval operation not supported "
1086 "for '%s', falling back to normal access check", path);
1087
1088 if (facl_access(fs, path, mode, uid, gid, suppl_gids) < 0) {
1089 xerrno = errno;
1090
1091 pr_trace_msg(trace_channel, 6, "normal access check for '%s' "
1092 "failed: %s", path, strerror(xerrno));
1093 destroy_pool(tmp_pool);
1094 errno = xerrno;
1095 return -1;
1096 }
1097
1098 destroy_pool(tmp_pool);
1099 return 0;
1100 }
1101
1102 destroy_pool(tmp_pool);
1103 errno = xerrno;
1104 return -1;
1105 }
1106 # endif
1107
1108 res = check_facl(tmp_pool, path, mode, acls, nents, &st, uid, gid,
1109 suppl_gids);
1110 xerrno = errno;
1111
1112 # if defined(HAVE_BSD_POSIX_ACL) || \
1113 defined(HAVE_LINUX_POSIX_ACL) || \
1114 defined(HAVE_MACOSX_POSIX_ACL)
1115 acl_free(acls);
1116 # endif
1117 destroy_pool(tmp_pool);
1118
1119 errno = xerrno;
1120 return res;
1121 }
1122
1123 static int facl_fsio_faccess(pr_fh_t *fh, int mode, uid_t uid, gid_t gid,
1124 array_header *suppl_gids) {
1125 const char *real_path = NULL;
1126 int nents = 0, res, xerrno;
1127 struct stat st;
1128 void *acls;
1129 pool *tmp_pool = NULL;
1130
1131 if (pr_fsio_fstat(fh, &st) < 0) {
1132 return -1;
1133 }
1134
1135 /* Look up the acl for this fd. */
1136 # if defined(HAVE_BSD_POSIX_ACL) || \
1137 defined(HAVE_LINUX_POSIX_ACL) || \
1138 defined(HAVE_MACOSX_POSIX_ACL)
1139 acls = acl_get_fd(PR_FH_FD(fh));
1140 if (acls == NULL) {
1141 xerrno = errno;
1142
1143 pr_trace_msg(trace_channel, 10,
1144 "unable to retrieve ACL for '%s': [%d] %s", fh->fh_path, xerrno,
1145 strerror(xerrno));
1146
1147 if (is_errno_eperm(xerrno) == TRUE) {
1148 pr_trace_msg(trace_channel, 3, "ACL retrieval operation not supported "
1149 "for '%s', falling back to normal access check", fh->fh_path);
1150
1151 if (facl_faccess(fh, mode, uid, gid, suppl_gids) < 0) {
1152 xerrno = errno;
1153
1154 pr_trace_msg(trace_channel, 6, "normal access check for '%s' "
1155 "failed: %s", fh->fh_path, strerror(xerrno));
1156 errno = xerrno;
1157 return -1;
1158 }
1159
1160 return 0;
1161 }
1162
1163 errno = xerrno;
1164 return -1;
1165 }
1166
1167 # elif defined(HAVE_SOLARIS_POSIX_ACL)
1168
1169 nents = facl(PR_FH_FD(fh), GETACLCNT, 0, NULL);
1170 if (nents < 0) {
1171 xerrno = errno;
1172
1173 pr_trace_msg(trace_channel, 10,
1174 "unable to retrieve ACL count for '%s': [%d] %s", fh->fh_path,
1175 xerrno, strerror(xerrno));
1176
1177 if (is_errno_eperm(xerrno) == TRUE) {
1178 pr_trace_msg(trace_channel, 3, "ACL retrieval operation not supported "
1179 "for '%s', falling back to normal access check", fh->fh_path);
1180
1181 if (facl_faccess(fh, mode, uid, gid, suppl_gids) < 0) {
1182 xerrno = errno;
1183
1184 pr_trace_msg(trace_channel, 6, "normal access check for '%s' "
1185 "failed: %s", fh->fh_path, strerror(xerrno));
1186 errno = xerrno;
1187 return -1;
1188 }
1189
1190 return 0;
1191 }
1192
1193 errno = xerrno;
1194 return -1;
1195 }
1196
1197 if (tmp_pool == NULL) {
1198 tmp_pool = make_sub_pool(fh->fh_fs->fs_pool);
1199 pr_pool_tag(tmp_pool, "mod_facl faccess(2) pool");
1200 }
1201
1202 acls = pcalloc(tmp_pool, nents * sizeof(aclent_t));
1203
1204 nents = facl(PR_FH_FD(fh), GETACL, nents, acls);
1205 if (nents < 0) {
1206 xerrno = errno;
1207
1208 destroy_pool(tmp_pool);
1209 tmp_pool = NULL;
1210
1211 pr_trace_msg(trace_channel, 10,
1212 "unable to retrieve ACL for '%s': [%d] %s", fh->fh_path, xerrno,
1213 strerror(xerrno));
1214
1215 if (is_errno_eperm(xerrno) == TRUE) {
1216 pr_trace_msg(trace_channel, 3, "ACL retrieval operation not supported "
1217 "for '%s', falling back to normal access check", fh->fh_path);
1218
1219 if (facl_faccess(fh, mode, uid, gid, suppl_gids) < 0) {
1220 xerrno = errno;
1221
1222 pr_trace_msg(trace_channel, 6, "normal access check for '%s' "
1223 "failed: %s", fh->fh_path, strerror(xerrno));
1224 errno = xerrno;
1225 return -1;
1226 }
1227
1228 return 0;
1229 }
1230
1231 errno = xerrno;
1232 return -1;
1233 }
1234 # endif
1235
1236 if (tmp_pool == NULL) {
1237 tmp_pool = make_sub_pool(fh->fh_fs->fs_pool);
1238 pr_pool_tag(tmp_pool, "mod_facl faccess(2) pool");
1239 }
1240
1241 real_path = pr_fsio_realpath(tmp_pool, fh->fh_path);
1242 if (real_path == NULL) {
1243 real_path = fh->fh_path;
1244 }
1245
1246 res = check_facl(tmp_pool, real_path, mode, acls, nents, &st, uid, gid,
1247 suppl_gids);
1248 xerrno = errno;
1249
1250 # if defined(HAVE_BSD_POSIX_ACL) || \
1251 defined(HAVE_LINUX_POSIX_ACL) || \
1252 defined(HAVE_MACOSX_POSIX_ACL)
1253 acl_free(acls);
1254 # endif
1255 destroy_pool(tmp_pool);
1256
1257 errno = xerrno;
1258 return res;
1259 }
1260 # endif /* !PR_USE_FACL */
1261 #endif /* HAVE_POSIX_ACL */
1262
1263 /* Configuration handlers
1264 */
1265
1266 /* usage: FACLEngine on|off */
1267 MODRET set_faclengine(cmd_rec *cmd) {
1268 int engine = -1;
1269
1270 CHECK_ARGS(cmd, 1);
1271 CHECK_CONF(cmd, CONF_ROOT);
1272
1273 engine = get_boolean(cmd, 1);
1274 if (engine == -1) {
1275 CONF_ERROR(cmd, "expected Boolean parameter");
1276 }
1277
1278 facl_engine = engine;
1279 return PR_HANDLED(cmd);
1280 }
1281
1282 /* Event listeners
1283 */
1284
1285 static void unmount_facl(void) {
1286 pr_fs_t *fs;
1287
1288 fs = pr_unmount_fs("/", "facl");
1289 if (fs != NULL) {
1290 destroy_pool(fs->fs_pool);
1291 return;
1292 }
1293
1294 if (errno != ENOENT) {
1295 pr_log_debug(DEBUG0, MOD_FACL_VERSION
1296 ": error unmounting 'facl' FS: %s", strerror(errno));
1297 }
1298 }
1299
1300 #if defined(PR_SHARED_MODULE) && \
1301 defined(PR_USE_FACL) && \
1302 defined(HAVE_POSIX_ACL)
1303 static void facl_mod_unload_ev(const void *event_data, void *user_data) {
1304 if (strcmp("mod_facl.c", (const char *) event_data) == 0) {
1305 pr_event_unregister(&facl_module, NULL, NULL);
1306 unmount_facl();
1307 }
1308 }
1309 #endif /* !PR_SHARED_MODULE */
1310
1311 static void facl_postparse_ev(const void *event_data, void *user_data) {
1312 #if defined(PR_USE_FACL) && \
1313 defined(HAVE_POSIX_ACL)
1314 pr_fs_t *fs;
1315 #endif /* PR_USE_FACL and HAVE_POSIX_ACL */
1316
1317 if (facl_engine == FALSE) {
1318 return;
1319 }
1320
1321 #if defined(PR_USE_FACL) && \
1322 defined(HAVE_POSIX_ACL)
1323 fs = pr_register_fs(permanent_pool, "facl", "/");
1324 if (fs == NULL) {
1325 int xerrno = errno;
1326
1327 pr_log_pri(PR_LOG_WARNING,
1328 MOD_FACL_VERSION ": error registering 'facl' FS: %s", strerror(xerrno));
1329 pr_session_disconnect(&facl_module, PR_SESS_DISCONNECT_BY_APPLICATION,
1330 NULL);
1331 }
1332 pr_log_debug(DEBUG6, MOD_FACL_VERSION ": registered 'facl' FS");
1333
1334 /* Ensure that our ACL-checking handlers are used. */
1335 fs->access = facl_fsio_access;
1336 fs->faccess = facl_fsio_faccess;
1337 #endif /* PR_USE_FACL and HAVE_POSIX_ACL */
1338 }
1339
1340 static void facl_restart_ev(const void *event_data, void *user_data) {
1341 if (facl_engine == FALSE) {
1342 return;
1343 }
1344
1345 unmount_facl();
1346 }
1347
1348 /* Initialization routines
1349 */
1350
1351 static int facl_init(void) {
1352 #if defined(PR_USE_FACL) && \
1353 defined(HAVE_POSIX_ACL)
1354 # if defined(PR_SHARED_MODULE)
1355 pr_event_register(&facl_module, "core.module-unload", facl_mod_unload_ev,
1356 NULL);
1357 # endif /* !PR_SHARED_MODULE */
1358 #endif /* PR_USE_FACL and HAVE_POSIX_ACL */
1359 pr_event_register(&facl_module, "core.postparse", facl_postparse_ev, NULL);
1360 pr_event_register(&facl_module, "core.restart", facl_restart_ev, NULL);
1361
1362 return 0;
1363 }
1364
1365 /* Module Tables
1366 */
1367
1368 static conftable facl_conftab[] = {
1369 { "FACLEngine", set_faclengine, NULL },
1370 { NULL }
1371 };
1372
1373 module facl_module = {
1374 /* Always NULL */
1375 NULL, NULL,
1376
1377 /* Module API version */
1378 0x20,
1379
1380 /* Module name */
1381 "facl",
1382
1383 /* Module configuration directive handlers */
1384 facl_conftab,
1385
1386 /* Module command handlers */
1387 NULL,
1388
1389 /* Module authentication handlers */
1390 NULL,
1391
1392 /* Module initialization */
1393 facl_init,
1394
1395 /* Session initialization */
1396 NULL,
1397
1398 /* Module version */
1399 MOD_FACL_VERSION
1400 };