Skip to content

Commit

Permalink
ls(1): add selinux support
Browse files Browse the repository at this point in the history
  • Loading branch information
WavyEbuilder authored and q66 committed Dec 3, 2024
1 parent 25f1036 commit 6e02a9a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src.freebsd/coreutils/ls/ls.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ static char sccsid[] = "@(#)ls.c 8.5 (Berkeley) 4/2/94";
#include <termcap.h>
#include <signal.h>
#endif
#ifdef HAVE_SELINUX
#include <selinux/selinux.h>
#endif

#include "ls.h"
#include "extern.h"
Expand Down Expand Up @@ -932,6 +935,26 @@ display(const FTSENT *p, FTSENT *list, int options __unused)
}
mac_free(label);
label_out:
#elif defined(HAVE_SELINUX)
char *context = NULL;
int error;

char name[PATH_MAX + 1];
if (cur->fts_level == FTS_ROOTLEVEL)
snprintf(name, sizeof(name), "%s", cur->fts_name);
else
snprintf(name, sizeof(name), "%s/%s", cur->fts_parent->fts_accpath, cur->fts_name);

if (options & FTS_LOGICAL)
error = getfilecon(name, &context);
else
error = lgetfilecon(name, &context);

if (error == -1) {
warn("SELinux context for %s/%s", cur->fts_parent->fts_path, cur->fts_name);
labelstr = strdup("?");
} else
labelstr = context;
#endif
if (labelstr == NULL)
labelstr = strdup("-");
Expand All @@ -949,6 +972,11 @@ display(const FTSENT *p, FTSENT *list, int options __unused)
np->group = &np->data[ulen + 1];
(void)strcpy(np->group, group);

if (labelstr != NULL) {
np->label = &np->data[ulen + glen + 2];
(void)strcpy(np->label, labelstr);
}

if (S_ISCHR(sp->st_mode) ||
S_ISBLK(sp->st_mode)) {
sizelen = snprintf(NULL, 0,
Expand Down
4 changes: 4 additions & 0 deletions src.freebsd/coreutils/ls/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ if get_option('color_ls')
endif
endif

if libselinux.found()
ls_deps += [libselinux]
endif

ls_prog = executable(
'ls', ['cmp.c', 'ls.c', 'print.c', 'util.c'],
c_args: ls_cargs,
Expand Down

0 comments on commit 6e02a9a

Please sign in to comment.