Skip to content

Commit 3ea8df6

Browse files
committed
device: implement access control to the boards
Limit access control to the boards according to the passed username. If the user has no access, the board becoms completely invisible: it is not listed, it is not possible to fetch board description, etc. Signed-off-by: Dmitry Baryshkov <[email protected]>
1 parent b5664c2 commit 3ea8df6

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

cdba-server.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "list.h"
4848

4949
static bool quit_invoked;
50+
static const char *username;
5051

5152
struct device *selected_device;
5253

@@ -121,7 +122,7 @@ static void msg_select_board(const void *param)
121122
{
122123
struct msg reply = { MSG_SELECT_BOARD, 0 };
123124

124-
selected_device = device_open(param, &fastboot_ops);
125+
selected_device = device_open(param, username, &fastboot_ops);
125126
if (!selected_device) {
126127
fprintf(stderr, "failed to open %s\n", (const char *)param);
127128
quit_invoked = true;
@@ -231,10 +232,10 @@ static int handle_stdin(int fd, void *buf)
231232
device_send_break(selected_device);
232233
break;
233234
case MSG_LIST_DEVICES:
234-
device_list_devices();
235+
device_list_devices(username);
235236
break;
236237
case MSG_BOARD_INFO:
237-
device_info(msg->data, msg->len);
238+
device_info(username, msg->data, msg->len);
238239
break;
239240
default:
240241
fprintf(stderr, "unk %d len %d\n", msg->type, msg->len);
@@ -364,6 +365,8 @@ int main(int argc, char **argv)
364365

365366
signal(SIGPIPE, sigpipe_handler);
366367

368+
username = getenv("CDBA_USER");
369+
367370
ret = device_parser(".cdba");
368371
if (ret) {
369372
ret = device_parser("/etc/cdba");

device.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,27 @@ static void device_lock(struct device *device)
8383
err(1, "failed to lock lockfile %s", lock);
8484
}
8585

86+
static bool device_check_access(struct device *device,
87+
const char *username)
88+
{
89+
struct device_user *user;
90+
91+
if (!device->users)
92+
return true;
93+
94+
if (!username)
95+
return false;
96+
97+
list_for_each_entry(user, device->users, node) {
98+
if (!strcmp(user->username, username))
99+
return true;
100+
}
101+
102+
return false;
103+
}
104+
86105
struct device *device_open(const char *board,
106+
const char *username,
87107
struct fastboot_ops *fastboot_ops)
88108
{
89109
struct device *device;
@@ -98,6 +118,9 @@ struct device *device_open(const char *board,
98118
found:
99119
assert(device->open || device->console_dev);
100120

121+
if (!device_check_access(device, username))
122+
return NULL;
123+
101124
device_lock(device);
102125

103126
if (device->open) {
@@ -269,14 +292,17 @@ void device_send_break(struct device *device)
269292
device->send_break(device);
270293
}
271294

272-
void device_list_devices(void)
295+
void device_list_devices(const char *username)
273296
{
274297
struct device *device;
275298
struct msg hdr;
276299
size_t len;
277300
char buf[80];
278301

279302
list_for_each_entry(device, &devices, node) {
303+
if (!device_check_access(device, username))
304+
continue;
305+
280306
if (device->name)
281307
len = snprintf(buf, sizeof(buf), "%-20s %s", device->board, device->name);
282308
else
@@ -293,15 +319,21 @@ void device_list_devices(void)
293319
write(STDOUT_FILENO, &hdr, sizeof(hdr));
294320
}
295321

296-
void device_info(const void *data, size_t dlen)
322+
void device_info(const char *username, const void *data, size_t dlen)
297323
{
298324
struct device *device;
299325
struct msg hdr;
300326
char *description;
301327
size_t len = 0;
302328

303329
list_for_each_entry(device, &devices, node) {
304-
if (!strncmp(device->board, data, dlen) && device->description) {
330+
if (strncmp(device->board, data, dlen))
331+
continue;
332+
333+
if (!device_check_access(device, username))
334+
continue;
335+
336+
if (device->description) {
305337
description = device->description;
306338
len = strlen(device->description);
307339
break;

device.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct device {
1515
char *name;
1616
char *serial;
1717
char *description;
18+
struct list_head *users;
1819
unsigned voltage;
1920
bool tickle_mmc;
2021
bool usb_always_on;
@@ -45,9 +46,17 @@ struct device {
4546
struct list_head node;
4647
};
4748

49+
struct device_user {
50+
const char *username;
51+
52+
struct list_head node;
53+
};
54+
4855
void device_add(struct device *device);
4956

50-
struct device *device_open(const char *board, struct fastboot_ops *fastboot_ops);
57+
struct device *device_open(const char *board,
58+
const char *username,
59+
struct fastboot_ops *fastboot_ops);
5160
void device_close(struct device *dev);
5261
int device_power(struct device *device, bool on);
5362

@@ -60,8 +69,8 @@ void device_boot(struct device *device, const void *data, size_t len);
6069
void device_fastboot_boot(struct device *device);
6170
void device_fastboot_flash_reboot(struct device *device);
6271
void device_send_break(struct device *device);
63-
void device_list_devices(void);
64-
void device_info(const void *data, size_t dlen);
72+
void device_list_devices(const char *username);
73+
void device_info(const char *username, const void *data, size_t dlen);
6574

6675
enum {
6776
DEVICE_KEY_FASTBOOT,

device_parser.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,28 @@ static void parse_board(struct device_parser *dp)
9090
dev = calloc(1, sizeof(*dev));
9191

9292
while (accept(dp, YAML_SCALAR_EVENT, key)) {
93+
if (!strcmp(key, "users")) {
94+
dev->users = calloc(1, sizeof(*dev->users));
95+
list_init(dev->users);
96+
97+
if (accept(dp, YAML_SCALAR_EVENT, value))
98+
continue;
99+
100+
expect(dp, YAML_SEQUENCE_START_EVENT, NULL);
101+
102+
while (accept(dp, YAML_SCALAR_EVENT, key)) {
103+
struct device_user *user = calloc(1, sizeof(*user));
104+
105+
user->username = strdup(key);
106+
107+
list_add(dev->users, &user->node);
108+
}
109+
110+
expect(dp, YAML_SEQUENCE_END_EVENT, NULL);
111+
112+
continue;
113+
}
114+
93115
expect(dp, YAML_SCALAR_EVENT, value);
94116

95117
if (!strcmp(key, "board")) {

0 commit comments

Comments
 (0)