initctl: allow 'cond get' read any type of condition

Fixes #265

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2022-05-15 11:52:02 +02:00
1 parent 0e84ecf143
commit 94d55cb7dd
1 file changed
+18 -11
+18 -11
View File
@@ -394,7 +394,7 @@ typedef enum { COND_CLR, COND_SET, COND_GET } condop_t;
static int do_cond_act(char *arg, condop_t op) static int do_cond_act(char *arg, condop_t op)
{ {
char oneshot[256]; char path[256];
size_t off; size_t off;
if (arg && strncmp(arg, COND_USR, strlen(COND_USR)) == 0) if (arg && strncmp(arg, COND_USR, strlen(COND_USR)) == 0)
@@ -402,27 +402,34 @@ static int do_cond_act(char *arg, condop_t op)
if (!arg || !arg[0]) if (!arg || !arg[0])
ERRX(2, "Invalid condition (empty)"); ERRX(2, "Invalid condition (empty)");
if (strchr(arg, '/'))
ERRX(2, "Invalid condition (slashes)");
if (strchr(arg, '.'))
ERRX(2, "Invalid condition (periods)");
snprintf(oneshot, sizeof(oneshot), _PATH_CONDUSR "%s", arg); /* allowed to read any condition, but not set/clr */
if (op != COND_GET) {
if (strchr(arg, '/'))
ERRX(2, "Invalid condition (slashes)");
if (strchr(arg, '.'))
ERRX(2, "Invalid condition (periods)");
}
if (strchr(arg, '/'))
snprintf(path, sizeof(path), _PATH_COND "%s", arg);
else
snprintf(path, sizeof(path), _PATH_CONDUSR "%s", arg);
off = strlen(_PATH_COND); off = strlen(_PATH_COND);
switch (op) { switch (op) {
case COND_GET: case COND_GET:
off = !fexist(oneshot); off = !fexist(path);
if (verbose) if (verbose)
puts(off ? "off" : "on"); puts(off ? "off" : "on");
return off; return off;
case COND_SET: case COND_SET:
if (symlink(_PATH_RECONF, oneshot) && errno != EEXIST) if (symlink(_PATH_RECONF, path) && errno != EEXIST)
ERR(73, "Failed asserting condition <%s>", &oneshot[off]); ERR(73, "Failed asserting condition <%s>", &path[off]);
break; break;
case COND_CLR: case COND_CLR:
if (erase(oneshot) && errno != ENOENT) if (erase(path) && errno != ENOENT)
ERR(73, "Failed deasserting condition <%s>", &oneshot[off]); ERR(73, "Failed deasserting condition <%s>", &path[off]);
break; break;
} }