smack-setup.c revision b5efdb8af40ea759a1ea584c1bc44ecc81dd00ce
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright (C) 2013 Intel Corporation
Authors:
Nathaniel Chen <nathaniel.chen@intel.com>
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "alloc-util.h"
#include "dirent-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "log.h"
#include "macro.h"
#include "smack-setup.h"
#include "string-util.h"
#include "util.h"
#ifdef HAVE_SMACK
static int write_access2_rules(const char* srcdir) {
int dfd = -1;
int r = 0;
if (load2_fd < 0) {
return -errno; /* negative error */
}
if (change_fd < 0) {
return -errno; /* negative error */
}
/* write rules to load2 or change-rule from every file in the directory */
if (!dir) {
return errno; /* positive on purpose */
}
int fd;
if (!dirent_is_file(entry))
continue;
if (fd < 0) {
if (r == 0)
r = -errno;
continue;
}
if (!policy) {
if (r == 0)
r = -errno;
safe_close(fd);
continue;
}
/* load2 write rules in the kernel require a line buffered stream */
continue;
/* if 3 args -> load rule : subject object access1 */
/* if 4 args -> change rule : subject object access1 access2 */
continue;
}
if (r == 0)
r = -errno;
}
}
}
return r;
}
static int write_cipso2_rules(const char* srcdir) {
int dfd = -1;
int r = 0;
if (cipso2_fd < 0) {
return -errno; /* negative error */
}
/* write rules to cipso2 from every file in the directory */
if (!dir) {
return errno; /* positive on purpose */
}
int fd;
if (!dirent_is_file(entry))
continue;
if (fd < 0) {
if (r == 0)
r = -errno;
continue;
}
if (!policy) {
if (r == 0)
r = -errno;
safe_close(fd);
continue;
}
/* cipso2 write rules in the kernel require a line buffered stream */
continue;
if (r == 0)
r = -errno;
break;
}
}
}
return r;
}
#endif
int mac_smack_setup(bool *loaded_policy) {
#ifdef HAVE_SMACK
int r;
r = write_access2_rules("/etc/smack/accesses.d/");
switch(r) {
case -ENOENT:
log_debug("Smack is not enabled in the kernel.");
return 0;
case ENOENT:
log_debug("Smack access rules directory '/etc/smack/accesses.d/' not found");
return 0;
case 0:
log_info("Successfully loaded Smack policies.");
break;
default:
log_warning_errno(r, "Failed to load Smack access rules, ignoring: %m");
return 0;
}
#ifdef SMACK_RUN_LABEL
if (r)
#endif
r = write_cipso2_rules("/etc/smack/cipso.d/");
switch(r) {
case -ENOENT:
return 0;
case ENOENT:
return 0;
case 0:
break;
default:
log_warning_errno(r, "Failed to load Smack/CIPSO access rules, ignoring: %m");
return 0;
}
*loaded_policy = true;
#endif
return 0;
}