2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
3 * Copyright (C) 2018 Hans Dedecker <dedeckeh@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <sys/socket.h>
17 #include <sys/types.h>
26 #include <libubox/vlist.h>
27 #include <libubox/blobmsg_json.h>
28 #include <libubox/avl-cmp.h>
40 struct ubusd_acl_obj
{
42 struct list_head list
;
49 struct blob_attr
*methods
;
50 struct blob_attr
*tags
;
51 struct blob_attr
*priv
;
57 struct ubusd_acl_file
{
58 struct vlist_node avl
;
63 struct blob_attr
*blob
;
69 const char *ubusd_acl_dir
= "/usr/share/acl.d";
70 static struct blob_buf bbuf
;
71 static struct avl_tree ubusd_acls
;
72 static int ubusd_acl_seq
;
73 static struct ubus_object
*acl_obj
;
76 ubusd_acl_match_cred(struct ubus_client
*cl
, struct ubusd_acl_obj
*obj
)
78 if (obj
->user
&& !strcmp(cl
->user
, obj
->user
))
81 if (obj
->group
&& !strcmp(cl
->group
, obj
->group
))
88 ubusd_acl_check(struct ubus_client
*cl
, const char *obj
,
89 const char *method
, enum ubusd_acl_type type
)
91 struct ubusd_acl_obj
*acl
;
94 if (!cl
|| !cl
->uid
|| !obj
)
98 * Since this tree is sorted alphabetically, we can only expect
99 * to find matching entries as long as the number of matching
100 * characters between the access list string and the object path
101 * is monotonically increasing.
103 avl_for_each_element(&ubusd_acls
, acl
, avl
) {
104 const char *key
= acl
->avl
.key
;
108 full_match
= ubus_strmatch_len(obj
, key
, &cur_match_len
);
109 if (cur_match_len
< match_len
)
112 match_len
= cur_match_len
;
118 if (match_len
!= strlen(key
))
122 if (ubusd_acl_match_cred(cl
, acl
))
126 case UBUS_ACL_PUBLISH
:
131 case UBUS_ACL_SUBSCRIBE
:
136 case UBUS_ACL_LISTEN
:
141 case UBUS_ACL_ACCESS
:
143 struct blob_attr
*cur
;
146 blobmsg_for_each_attr(cur
, acl
->methods
, rem
)
147 if (blobmsg_type(cur
) == BLOBMSG_TYPE_STRING
)
148 if (!strcmp(method
, blobmsg_get_string(cur
)))
159 ubusd_acl_init_client(struct ubus_client
*cl
, int fd
)
166 unsigned int len
= sizeof(struct ucred
);
168 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &cred
, &len
) == -1)
171 memset(&cred
, 0, sizeof(cred
));
174 pwd
= getpwuid(cred
.uid
);
178 group
= getgrgid(cred
.gid
);
185 cl
->group
= strdup(group
->gr_name
);
186 cl
->user
= strdup(pwd
->pw_name
);
192 ubusd_acl_free_client(struct ubus_client
*cl
)
199 ubusd_acl_file_free(struct ubusd_acl_file
*file
)
201 struct ubusd_acl_obj
*p
, *q
;
203 list_for_each_entry_safe(p
, q
, &file
->acl
, list
) {
204 avl_delete(&ubusd_acls
, &p
->avl
);
219 static const struct blobmsg_policy acl_obj_policy
[__ACL_ACCESS_MAX
] = {
220 [ACL_ACCESS_METHODS
] = { .name
= "methods", .type
= BLOBMSG_TYPE_ARRAY
},
221 [ACL_ACCESS_TAGS
] = { .name
= "tags", .type
= BLOBMSG_TYPE_ARRAY
},
222 [ACL_ACCESS_PRIV
] = { .name
= "acl", .type
= BLOBMSG_TYPE_TABLE
},
225 static struct ubusd_acl_obj
*
226 ubusd_acl_alloc_obj(struct ubusd_acl_file
*file
, const char *obj
)
228 struct ubusd_acl_obj
*o
;
229 int len
= strlen(obj
);
231 bool partial
= false;
233 if (obj
[len
- 1] == '*') {
238 o
= calloc_a(sizeof(*o
), &k
, len
+ 1);
239 o
->partial
= partial
;
240 o
->user
= file
->user
;
241 o
->group
= file
->group
;
242 o
->avl
.key
= memcpy(k
, obj
, len
);
244 list_add(&o
->list
, &file
->acl
);
245 avl_insert(&ubusd_acls
, &o
->avl
);
251 ubusd_acl_add_access(struct ubusd_acl_file
*file
, struct blob_attr
*obj
)
253 struct blob_attr
*tb
[__ACL_ACCESS_MAX
];
254 struct ubusd_acl_obj
*o
;
256 blobmsg_parse(acl_obj_policy
, __ACL_ACCESS_MAX
, tb
, blobmsg_data(obj
),
257 blobmsg_data_len(obj
));
259 if (!tb
[ACL_ACCESS_METHODS
] && !tb
[ACL_ACCESS_TAGS
] && !tb
[ACL_ACCESS_PRIV
])
262 o
= ubusd_acl_alloc_obj(file
, blobmsg_name(obj
));
264 o
->methods
= tb
[ACL_ACCESS_METHODS
];
265 o
->tags
= tb
[ACL_ACCESS_TAGS
];
266 o
->priv
= tb
[ACL_ACCESS_PRIV
];
268 if (file
->user
|| file
->group
)
273 ubusd_acl_add_subscribe(struct ubusd_acl_file
*file
, const char *obj
)
275 struct ubusd_acl_obj
*o
= ubusd_acl_alloc_obj(file
, obj
);
281 ubusd_acl_add_publish(struct ubusd_acl_file
*file
, const char *obj
)
283 struct ubusd_acl_obj
*o
= ubusd_acl_alloc_obj(file
, obj
);
288 static void ubusd_acl_add_listen(struct ubusd_acl_file
*file
, const char *obj
)
290 struct ubusd_acl_obj
*o
= ubusd_acl_alloc_obj(file
, obj
);
306 static const struct blobmsg_policy acl_policy
[__ACL_MAX
] = {
307 [ACL_USER
] = { .name
= "user", .type
= BLOBMSG_TYPE_STRING
},
308 [ACL_GROUP
] = { .name
= "group", .type
= BLOBMSG_TYPE_STRING
},
309 [ACL_ACCESS
] = { .name
= "access", .type
= BLOBMSG_TYPE_TABLE
},
310 [ACL_PUBLISH
] = { .name
= "publish", .type
= BLOBMSG_TYPE_ARRAY
},
311 [ACL_SUBSCRIBE
] = { .name
= "subscribe", .type
= BLOBMSG_TYPE_ARRAY
},
312 [ACL_INHERIT
] = { .name
= "inherit", .type
= BLOBMSG_TYPE_ARRAY
},
313 [ACL_LISTEN
] = { .name
= "listen", .type
= BLOBMSG_TYPE_ARRAY
},
317 ubusd_acl_file_add(struct ubusd_acl_file
*file
)
319 struct blob_attr
*tb
[__ACL_MAX
], *cur
;
322 blobmsg_parse(acl_policy
, __ACL_MAX
, tb
, blob_data(file
->blob
),
323 blob_len(file
->blob
));
326 file
->user
= blobmsg_get_string(tb
[ACL_USER
]);
327 else if (tb
[ACL_GROUP
])
328 file
->group
= blobmsg_get_string(tb
[ACL_GROUP
]);
333 blobmsg_for_each_attr(cur
, tb
[ACL_ACCESS
], rem
)
334 ubusd_acl_add_access(file
, cur
);
336 if (tb
[ACL_SUBSCRIBE
])
337 blobmsg_for_each_attr(cur
, tb
[ACL_SUBSCRIBE
], rem
)
338 if (blobmsg_type(cur
) == BLOBMSG_TYPE_STRING
)
339 ubusd_acl_add_subscribe(file
, blobmsg_get_string(cur
));
342 blobmsg_for_each_attr(cur
, tb
[ACL_PUBLISH
], rem
)
343 if (blobmsg_type(cur
) == BLOBMSG_TYPE_STRING
)
344 ubusd_acl_add_publish(file
, blobmsg_get_string(cur
));
347 blobmsg_for_each_attr(cur
, tb
[ACL_LISTEN
], rem
)
348 if (blobmsg_type(cur
) == BLOBMSG_TYPE_STRING
)
349 ubusd_acl_add_listen(file
, blobmsg_get_string(cur
));
353 ubusd_acl_update_cb(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
354 struct vlist_node
*node_old
)
356 struct ubusd_acl_file
*file
;
359 file
= container_of(node_old
, struct ubusd_acl_file
, avl
);
360 ubusd_acl_file_free(file
);
364 file
= container_of(node_new
, struct ubusd_acl_file
, avl
);
365 ubusd_acl_file_add(file
);
369 static struct ubus_msg_buf
*
370 ubusd_create_sequence_event_msg(void *priv
, const char *id
)
374 blob_buf_init(&b
, 0);
375 blob_put_int32(&b
, UBUS_ATTR_OBJID
, 0);
376 blob_put_string(&b
, UBUS_ATTR_METHOD
, id
);
377 s
= blob_nest_start(&b
, UBUS_ATTR_DATA
);
378 blobmsg_add_u32(&b
, "sequence", ubusd_acl_seq
);
379 blob_nest_end(&b
, s
);
381 return ubus_msg_new(b
.head
, blob_raw_len(b
.head
), true);
384 static VLIST_TREE(ubusd_acl_files
, avl_strcmp
, ubusd_acl_update_cb
, false, false);
387 ubusd_acl_load_file(const char *filename
)
389 struct ubusd_acl_file
*file
;
392 blob_buf_init(&bbuf
, 0);
393 if (!blobmsg_add_json_from_file(&bbuf
, filename
)) {
394 syslog(LOG_ERR
, "failed to parse %s\n", filename
);
398 file
= calloc_a(sizeof(*file
), &blob
, blob_raw_len(bbuf
.head
));
404 memcpy(blob
, bbuf
.head
, blob_raw_len(bbuf
.head
));
405 INIT_LIST_HEAD(&file
->acl
);
407 vlist_add(&ubusd_acl_files
, &file
->avl
, filename
);
408 syslog(LOG_INFO
, "loading %s\n", filename
);
419 const char *suffix
= "/*.json";
420 char *path
= alloca(strlen(ubusd_acl_dir
) + strlen(suffix
) + 1);
422 sprintf(path
, "%s%s", ubusd_acl_dir
, suffix
);
423 if (glob(path
, GLOB_NOESCAPE
| GLOB_MARK
, NULL
, &gl
))
426 vlist_update(&ubusd_acl_files
);
427 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
428 if (stat(gl
.gl_pathv
[j
], &st
) || !S_ISREG(st
.st_mode
))
431 if (st
.st_uid
|| st
.st_gid
) {
432 syslog(LOG_ERR
, "%s has wrong owner\n", gl
.gl_pathv
[j
]);
435 if (st
.st_mode
& (S_IWOTH
| S_IWGRP
| S_IXOTH
)) {
436 syslog(LOG_ERR
, "%s has wrong permissions\n", gl
.gl_pathv
[j
]);
439 ubusd_acl_load_file(gl
.gl_pathv
[j
]);
443 vlist_flush(&ubusd_acl_files
);
445 ubusd_send_event(NULL
, "ubus.acl.sequence", ubusd_create_sequence_event_msg
, NULL
);
449 ubusd_reply_add(struct ubus_object
*obj
)
451 struct ubusd_acl_obj
*acl
;
458 * Since this tree is sorted alphabetically, we can only expect
459 * to find matching entries as long as the number of matching
460 * characters between the access list string and the object path
461 * is monotonically increasing.
463 avl_for_each_element(&ubusd_acls
, acl
, avl
) {
464 const char *key
= acl
->avl
.key
;
472 full_match
= ubus_strmatch_len(obj
->path
.key
, key
, &cur_match_len
);
473 if (cur_match_len
< match_len
)
476 match_len
= cur_match_len
;
482 if (match_len
!= strlen(key
))
486 c
= blobmsg_open_table(&b
, NULL
);
487 blobmsg_add_string(&b
, "obj", obj
->path
.key
);
489 blobmsg_add_string(&b
, "user", acl
->user
);
491 blobmsg_add_string(&b
, "group", acl
->group
);
493 blobmsg_add_field(&b
, blobmsg_type(acl
->priv
), "acl",
494 blobmsg_data(acl
->priv
), blobmsg_data_len(acl
->priv
));
496 blobmsg_close_table(&b
, c
);
500 static int ubusd_reply_query(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
, struct blob_attr
*msg
)
502 struct ubus_object
*obj
;
505 if (!attr
[UBUS_ATTR_OBJID
])
506 return UBUS_STATUS_INVALID_ARGUMENT
;
508 obj
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
510 return UBUS_STATUS_NOT_FOUND
;
512 blob_buf_init(&b
, 0);
513 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
.id
);
514 d
= blob_nest_start(&b
, UBUS_ATTR_DATA
);
516 blobmsg_add_u32(&b
, "seq", ubusd_acl_seq
);
517 a
= blobmsg_open_array(&b
, "acl");
518 list_for_each_entry(obj
, &cl
->objects
, list
)
519 ubusd_reply_add(obj
);
520 blobmsg_close_table(&b
, a
);
522 blob_nest_end(&b
, d
);
524 ubus_proto_send_msg_from_blob(cl
, ub
, UBUS_MSG_DATA
);
529 static int ubusd_acl_recv(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, const char *method
, struct blob_attr
*msg
)
531 if (!strcmp(method
, "query"))
532 return ubusd_reply_query(cl
, ub
, ubus_parse_msg(ub
->data
), msg
);
534 return UBUS_STATUS_INVALID_COMMAND
;
537 void ubusd_acl_init(void)
539 ubus_init_string_tree(&ubusd_acls
, true);
540 acl_obj
= ubusd_create_object_internal(NULL
, UBUS_SYSTEM_OBJECT_ACL
);
541 acl_obj
->recv_msg
= ubusd_acl_recv
;