condition.c revision 8a9c6071cb7467170010f0287672c987981bdf9c
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
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 <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fnmatch.h>
#include "sd-id128.h"
#include "util.h"
#include "virt.h"
#include "path-util.h"
#include "architecture.h"
#include "smack-util.h"
#include "apparmor-util.h"
#include "ima-util.h"
#include "selinux-util.h"
#include "audit.h"
#include "condition.h"
#include "cap-list.h"
Condition *c;
int r;
if (!c)
return NULL;
if (r < 0) {
free(c);
return NULL;
}
return c;
}
void condition_free(Condition *c) {
assert(c);
free(c);
}
Condition *c, *n;
condition_free(c);
return NULL;
}
static int condition_test_kernel_command_line(Condition *c) {
const char *p;
bool equal;
int r;
assert(c);
r = proc_cmdline(&line);
if (r < 0)
return r;
p = line;
for (;;) {
bool found;
r = unquote_first_word(&p, &word, true);
if (r < 0)
return r;
if (r == 0)
break;
if (equal)
else {
const char *f;
found = f && (*f == '=' || *f == 0);
}
if (found)
return true;
}
return false;
}
static int condition_test_virtualization(Condition *c) {
int b, v;
const char *id;
assert(c);
v = detect_virtualization(&id);
if (v < 0)
return v;
b = parse_boolean(c->parameter);
if (v > 0 && b > 0)
return true;
if (v == 0 && b == 0)
return true;
/* Then, compare categorization */
return true;
return true;
/* Finally compare id */
}
static int condition_test_architecture(Condition *c) {
int a, b;
assert(c);
a = uname_architecture();
if (a < 0)
return a;
b = native_architecture();
else
b = architecture_from_string(c->parameter);
if (b < 0)
return b;
return a == b;
}
static int condition_test_host(Condition *c) {
_cleanup_free_ char *h = NULL;
sd_id128_t x, y;
int r;
assert(c);
if (sd_id128_from_string(c->parameter, &x) >= 0) {
r = sd_id128_get_machine(&y);
if (r < 0)
return r;
return sd_id128_equal(x, y);
}
h = gethostname_malloc();
if (!h)
return -ENOMEM;
}
static int condition_test_ac_power(Condition *c) {
int r;
assert(c);
r = parse_boolean(c->parameter);
if (r < 0)
return r;
return (on_ac_power() != 0) == !!r;
}
static int condition_test_security(Condition *c) {
assert(c);
return mac_selinux_use();
return mac_smack_use();
return mac_apparmor_use();
return use_audit();
return use_ima();
return false;
}
static int condition_test_capability(Condition *c) {
int value;
unsigned long long capabilities = -1;
assert(c);
/* If it's an invalid capability, we don't have it */
if (value < 0)
return -EINVAL;
/* If it's a valid capability we default to assume
* that we have it */
if (!f)
return -errno;
break;
}
}
}
static int condition_test_needs_update(Condition *c) {
const char *p;
assert(c);
/* If the file system is read-only we shouldn't suggest an update */
if (path_is_read_only_fs(c->parameter) > 0)
return false;
/* Any other failure means we should allow the condition to be true,
* so that we rather invoke too many update tools then too
* few. */
if (!path_is_absolute(c->parameter))
return true;
return true;
return true;
}
static int condition_test_first_boot(Condition *c) {
int r;
assert(c);
r = parse_boolean(c->parameter);
if (r < 0)
return r;
}
static int condition_test_path_exists(Condition *c) {
assert(c);
}
static int condition_test_path_exists_glob(Condition *c) {
assert(c);
return glob_exists(c->parameter) > 0;
}
static int condition_test_path_is_directory(Condition *c) {
assert(c);
}
static int condition_test_path_is_symbolic_link(Condition *c) {
assert(c);
return is_symlink(c->parameter) > 0;
}
static int condition_test_path_is_mount_point(Condition *c) {
assert(c);
return path_is_mount_point(c->parameter, true) > 0;
}
static int condition_test_path_is_read_write(Condition *c) {
assert(c);
return path_is_read_only_fs(c->parameter) <= 0;
}
static int condition_test_directory_not_empty(Condition *c) {
int r;
assert(c);
r = dir_is_empty(c->parameter);
return r <= 0 && r != -ENOENT;
}
static int condition_test_file_not_empty(Condition *c) {
assert(c);
}
static int condition_test_file_is_executable(Condition *c) {
assert(c);
}
static int condition_test_null(Condition *c) {
assert(c);
/* Note that during parsing we already evaluate the string and
* store it in c->negate */
return true;
}
int condition_test(Condition *c) {
};
int r, b;
assert(c);
r = condition_tests[c->type](c);
if (r < 0) {
c->result = CONDITION_ERROR;
return r;
}
b = (r > 0) == !c->negate;
return b;
}
void condition_dump(Condition *c, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) {
assert(c);
assert(f);
if (!prefix)
prefix = "";
fprintf(f,
"%s\t%s: %s%s%s %s\n",
c->parameter,
}
void condition_dump_list(Condition *first, FILE *f, const char *prefix, const char *(*to_string)(ConditionType t)) {
Condition *c;
}
static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
[CONDITION_ARCHITECTURE] = "ConditionArchitecture",
[CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
[CONDITION_HOST] = "ConditionHost",
[CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
[CONDITION_SECURITY] = "ConditionSecurity",
[CONDITION_CAPABILITY] = "ConditionCapability",
[CONDITION_AC_POWER] = "ConditionACPower",
[CONDITION_NEEDS_UPDATE] = "ConditionNeedsUpdate",
[CONDITION_FIRST_BOOT] = "ConditionFirstBoot",
[CONDITION_PATH_EXISTS] = "ConditionPathExists",
[CONDITION_PATH_EXISTS_GLOB] = "ConditionPathExistsGlob",
[CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
[CONDITION_PATH_IS_SYMBOLIC_LINK] = "ConditionPathIsSymbolicLink",
[CONDITION_PATH_IS_MOUNT_POINT] = "ConditionPathIsMountPoint",
[CONDITION_PATH_IS_READ_WRITE] = "ConditionPathIsReadWrite",
[CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
[CONDITION_FILE_NOT_EMPTY] = "ConditionFileNotEmpty",
[CONDITION_FILE_IS_EXECUTABLE] = "ConditionFileIsExecutable",
[CONDITION_NULL] = "ConditionNull"
};
static const char* const assert_type_table[_CONDITION_TYPE_MAX] = {
[CONDITION_ARCHITECTURE] = "AssertArchitecture",
[CONDITION_VIRTUALIZATION] = "AssertVirtualization",
[CONDITION_HOST] = "AssertHost",
[CONDITION_KERNEL_COMMAND_LINE] = "AssertKernelCommandLine",
[CONDITION_SECURITY] = "AssertSecurity",
[CONDITION_CAPABILITY] = "AssertCapability",
[CONDITION_AC_POWER] = "AssertACPower",
[CONDITION_NEEDS_UPDATE] = "AssertNeedsUpdate",
[CONDITION_FIRST_BOOT] = "AssertFirstBoot",
[CONDITION_PATH_EXISTS] = "AssertPathExists",
[CONDITION_PATH_EXISTS_GLOB] = "AssertPathExistsGlob",
[CONDITION_PATH_IS_DIRECTORY] = "AssertPathIsDirectory",
[CONDITION_PATH_IS_SYMBOLIC_LINK] = "AssertPathIsSymbolicLink",
[CONDITION_PATH_IS_MOUNT_POINT] = "AssertPathIsMountPoint",
[CONDITION_PATH_IS_READ_WRITE] = "AssertPathIsReadWrite",
[CONDITION_DIRECTORY_NOT_EMPTY] = "AssertDirectoryNotEmpty",
[CONDITION_FILE_NOT_EMPTY] = "AssertFileNotEmpty",
[CONDITION_FILE_IS_EXECUTABLE] = "AssertFileIsExecutable",
[CONDITION_NULL] = "AssertNull"
};
static const char* const condition_result_table[_CONDITION_RESULT_MAX] = {
[CONDITION_UNTESTED] = "untested",
[CONDITION_SUCCEEDED] = "succeeded",
[CONDITION_FAILED] = "failed",
[CONDITION_ERROR] = "error",
};