summaryrefslogtreecommitdiffstats
path: root/tests/test-ubusd.h
blob: 2424b5568a469d8b8f579f7da284e0ff3f9545a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#ifndef __TEST_UBUSD_H
#define __TEST_UBUSD_H

#include <fcntl.h>
#include <limits.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>

#include "libubus.h"

#define TEST_SKIPPED	77

static const char *ubusd_bin;
static char sock_path[PATH_MAX];
static char sock_dir[] = "/tmp/ubus-test-XXXXXX";
static volatile pid_t ubusd_pid;
static int failures;

#define CHECK(cond) do {						\
	if (!(cond)) {							\
		fprintf(stderr, "%s:%d: check failed: %s\n",		\
			__FILE__, __LINE__, #cond);			\
		failures++;						\
	}								\
} while (0)

static inline void ubusd_start(void)
{
	struct ubus_context *probe;
	int i;

	unlink(sock_path);
	ubusd_pid = fork();
	if (ubusd_pid < 0)
		exit(2);

	if (!ubusd_pid) {
		/* a test that crashes must not leave ubusd behind */
		prctl(PR_SET_PDEATHSIG, SIGKILL);
		execl(ubusd_bin, ubusd_bin, "-s", sock_path, NULL);
		_exit(127);
	}

	for (i = 0; i < 100; i++) {
		probe = ubus_connect(sock_path);
		if (probe) {
			ubus_free(probe);
			return;
		}
		usleep(50 * 1000);
	}

	fprintf(stderr, "ubusd did not start\n");
	exit(2);
}

static inline void ubusd_reap(void)
{
	waitpid(ubusd_pid, NULL, 0);
	ubusd_pid = 0;
	unlink(sock_path);
}

static inline void ubusd_kill(void)
{
	kill(ubusd_pid, SIGKILL);
	ubusd_reap();
}

static inline void ubusd_kill_alarm(int sig)
{
	kill(ubusd_pid, SIGKILL);
}

static inline bool object_visible(const char *name)
{
	struct ubus_context *ctx = ubus_connect(sock_path);
	uint32_t id = 0;
	int ret;

	if (!ctx)
		return false;

	/* ubusd answers OK with no id for an object that has no type */
	ret = ubus_lookup_id(ctx, name, &id);
	ubus_free(ctx);

	return !ret && id;
}

static inline bool proc_file_write(const char *path, const char *data)
{
	ssize_t len = strlen(data);
	bool ret;
	int fd;

	fd = open(path, O_WRONLY);
	if (fd < 0)
		return false;

	ret = write(fd, data, len) == len;
	close(fd);

	return ret;
}

/*
 * ubusd applies its ACLs to every client that is not root, and it loads only
 * ACL files that root owns.
 */
static inline bool userns_enter(void)
{
	uid_t uid = getuid();
	gid_t gid = getgid();
	char map[32];

	if (!uid)
		return true;

	if (unshare(CLONE_NEWUSER))
		return false;

	snprintf(map, sizeof(map), "0 %u 1", (unsigned int) uid);
	if (!proc_file_write("/proc/self/uid_map", map) ||
	    !proc_file_write("/proc/self/setgroups", "deny"))
		return false;

	snprintf(map, sizeof(map), "0 %u 1", (unsigned int) gid);

	return proc_file_write("/proc/self/gid_map", map);
}

static inline int test_ubusd_init(int argc, char **argv)
{
	if (argc != 2) {
		fprintf(stderr, "usage: %s <ubusd>\n", argv[0]);
		return 2;
	}

	if (!userns_enter()) {
		fprintf(stderr, "not root and no user namespace, skipped\n");
		return TEST_SKIPPED;
	}

	ubusd_bin = argv[1];
	signal(SIGPIPE, SIG_IGN);

	if (!mkdtemp(sock_dir))
		return 2;

	snprintf(sock_path, sizeof(sock_path), "%s/ubus.sock", sock_dir);

	return 0;
}

static inline int test_ubusd_done(void)
{
	rmdir(sock_dir);

	if (failures)
		fprintf(stderr, "%d check(s) failed\n", failures);

	return failures ? 1 : 0;
}

#endif