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
;
56 struct ubusd_acl_file
{
57 struct vlist_node avl
;
62 struct blob_attr
*blob
;
68 const char *ubusd_acl_dir
= "/usr/share/acl.d";
69 static struct blob_buf bbuf
;
70 static struct avl_tree ubusd_acls
;
71 static int ubusd_acl_seq
;
72 static struct ubus_object
*acl_obj
;
75 ubusd_acl_match_cred(struct ubus_client
*cl
, struct ubusd_acl_obj
*obj
)
77 if (obj
->user
&& !strcmp(cl
->user
, obj
->user
))
80 if (obj
->group
&& !strcmp(cl
->group
, obj
->group
))
87 ubusd_acl_check(struct ubus_client
*cl
, const char *obj
,
88 const char *method
, enum ubusd_acl_type type
)
90 struct ubusd_acl_obj
*acl
;
93 if (!cl
|| !cl
->uid
|| !obj
)
97 * Since this tree is sorted alphabetically, we can only expect
98 * to find matching entries as long as the number of matching
99 * characters between the access list string and the object path
100 * is monotonically increasing.
102 avl_for_each_element(&ubusd_acls
, acl
, avl
) {
103 const char *key
= acl
->avl
.key
;
107 full_match
= ubus_strmatch_len(obj
, key
, &cur_match_len
);
108 if (cur_match_len
< match_len
)
111 match_len
= cur_match_len
;
117 if (match_len
!= strlen(key
))
121 if (ubusd_acl_match_cred(cl
, acl
))
125 case UBUS_ACL_PUBLISH
:
130 case UBUS_ACL_SUBSCRIBE
:
135 case UBUS_ACL_ACCESS
:
137 struct blob_attr
*cur
;
140 blobmsg_for_each_attr(cur
, acl
->methods
, rem
)
141 if (blobmsg_type(cur
) == BLOBMSG_TYPE_STRING
)
142 if (!strcmp(method
, blobmsg_get_string(cur
)))
153 ubusd_acl_init_client(struct ubus_client
*cl
, int fd
)
160 unsigned int len
= sizeof(struct ucred
);
162 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &cred
, &len
) == -1)
165 memset(&cred
, 0, sizeof(cred
));
168 pwd
= getpwuid(cred
.uid
);
172 group
= getgrgid(cred
.gid
);
179 cl
->group
= strdup(group
->gr_name
);
180 cl
->user
= strdup(pwd
->pw_name
);
186 ubusd_acl_free_client(struct ubus_client
*cl
)
193 ubusd_acl_file_free(struct ubusd_acl_file
*file
)
195 struct ubusd_acl_obj
*p
, *q
;
197 list_for_each_entry_safe(p
, q
, &file
->acl
, list
) {
198 avl_delete(&ubusd_acls
, &p
->avl
);
213 static const struct blobmsg_policy acl_obj_policy
[__ACL_ACCESS_MAX
] = {
214 [ACL_ACCESS_METHODS
] = { .name
= "methods", .type
= BLOBMSG_TYPE_ARRAY
},
215 [ACL_ACCESS_TAGS
] = { .name
= "tags", .type
= BLOBMSG_TYPE_ARRAY
},
216 [ACL_ACCESS_PRIV
] = { .name
= "acl", .type
= BLOBMSG_TYPE_TABLE
},
219 static struct ubusd_acl_obj
*
220 ubusd_acl_alloc_obj(struct ubusd_acl_file
*file
, const char *obj
)
222 struct ubusd_acl_obj
*o
;
223 int len
= strlen(obj
);
225 bool partial
= false;
227 if (obj
[len
- 1] == '*') {
232 o
= calloc_a(sizeof(*o
), &k
, len
+ 1);
233 o
->partial
= partial
;
234 o
->user
= file
->user
;
235 o
->group
= file
->group
;
236 o
->avl
.key
= memcpy(k
, obj
, len
);
238 list_add(&o
->list
, &file
->acl
);
239 avl_insert(&ubusd_acls
, &o
->avl
);
245 ubusd_acl_add_access(struct ubusd_acl_file
*file
, struct blob_attr
*obj
)
247 struct blob_attr
*tb
[__ACL_ACCESS_MAX
];
248 struct ubusd_acl_obj
*o
;
250 blobmsg_parse(acl_obj_policy
, __ACL_ACCESS_MAX
, tb
, blobmsg_data(obj
),
251 blobmsg_data_len(obj
));
253 if (!tb
[ACL_ACCESS_METHODS
] && !tb
[ACL_ACCESS_TAGS
] && !tb
[ACL_ACCESS_PRIV
])
256 o
= ubusd_acl_alloc_obj(file
, blobmsg_name(obj
));
258 o
->methods
= tb
[ACL_ACCESS_METHODS
];
259 o
->tags
= tb
[ACL_ACCESS_TAGS
];
260 o
->priv
= tb
[ACL_ACCESS_PRIV
];
262 if (file
->user
|| file
->group
)
267 ubusd_acl_add_subscribe(struct ubusd_acl_file
*file
, const char *obj
)
269 struct ubusd_acl_obj
*o
= ubusd_acl_alloc_obj(file
, obj
);
275 ubusd_acl_add_publish(struct ubusd_acl_file
*file
, const char *obj
)
277 struct ubusd_acl_obj
*o
= ubusd_acl_alloc_obj(file
, obj
);
292 static const struct blobmsg_policy acl_policy
[__ACL_MAX
] = {
293 [ACL_USER
] = { .name
= "user", .type
= BLOBMSG_TYPE_STRING
},
294 [ACL_GROUP
] = { .name
= "group", .type
= BLOBMSG_TYPE_STRING
},
295 [ACL_ACCESS
] = { .name
= "access", .type
= BLOBMSG_TYPE_TABLE
},
296 [ACL_PUBLISH
] = { .name
= "publish", .type
= BLOBMSG_TYPE_ARRAY
},
297 [ACL_SUBSCRIBE
] = { .name
= "subscribe", .type
= BLOBMSG_TYPE_ARRAY
},
298 [ACL_INHERIT
] = { .name
= "inherit", .type
= BLOBMSG_TYPE_ARRAY
},
302 ubusd_acl_file_add(struct ubusd_acl_file
*file
)
304 struct blob_attr
*tb
[__ACL_MAX
], *cur
;
307 blobmsg_parse(acl_policy
, __ACL_MAX
, tb
, blob_data(file
->blob
),
308 blob_len(file
->blob
));
311 file
->user
= blobmsg_get_string(tb
[ACL_USER
]);
312 else if (tb
[ACL_GROUP
])
313 file
->group
= blobmsg_get_string(tb
[ACL_GROUP
]);
318 blobmsg_for_each_attr(cur
, tb
[ACL_ACCESS
], rem
)
319 ubusd_acl_add_access(file
, cur
);
321 if (tb
[ACL_SUBSCRIBE
])
322 blobmsg_for_each_attr(cur
, tb
[ACL_SUBSCRIBE
], rem
)
323 if (blobmsg_type(cur
) == BLOBMSG_TYPE_STRING
)
324 ubusd_acl_add_subscribe(file
, blobmsg_get_string(cur
));
327 blobmsg_for_each_attr(cur
, tb
[ACL_PUBLISH
], rem
)
328 if (blobmsg_type(cur
) == BLOBMSG_TYPE_STRING
)
329 ubusd_acl_add_publish(file
, blobmsg_get_string(cur
));
333 ubusd_acl_update_cb(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
334 struct vlist_node
*node_old
)
336 struct ubusd_acl_file
*file
;
339 file
= container_of(node_old
, struct ubusd_acl_file
, avl
);
340 ubusd_acl_file_free(file
);
344 file
= container_of(node_new
, struct ubusd_acl_file
, avl
);
345 ubusd_acl_file_add(file
);
349 static struct ubus_msg_buf
*
350 ubusd_create_sequence_event_msg(void *priv
, const char *id
)
354 blob_buf_init(&b
, 0);
355 blob_put_int32(&b
, UBUS_ATTR_OBJID
, 0);
356 blob_put_string(&b
, UBUS_ATTR_METHOD
, id
);
357 s
= blob_nest_start(&b
, UBUS_ATTR_DATA
);
358 blobmsg_add_u32(&b
, "sequence", ubusd_acl_seq
);
359 blob_nest_end(&b
, s
);
361 return ubus_msg_new(b
.head
, blob_raw_len(b
.head
), true);
364 static VLIST_TREE(ubusd_acl_files
, avl_strcmp
, ubusd_acl_update_cb
, false, false);
367 ubusd_acl_load_file(const char *filename
)
369 struct ubusd_acl_file
*file
;
372 blob_buf_init(&bbuf
, 0);
373 if (!blobmsg_add_json_from_file(&bbuf
, filename
)) {
374 syslog(LOG_ERR
, "failed to parse %s\n", filename
);
378 file
= calloc_a(sizeof(*file
), &blob
, blob_raw_len(bbuf
.head
));
384 memcpy(blob
, bbuf
.head
, blob_raw_len(bbuf
.head
));
385 INIT_LIST_HEAD(&file
->acl
);
387 vlist_add(&ubusd_acl_files
, &file
->avl
, filename
);
388 syslog(LOG_INFO
, "loading %s\n", filename
);
399 const char *suffix
= "/*.json";
400 char *path
= alloca(strlen(ubusd_acl_dir
) + strlen(suffix
) + 1);
402 sprintf(path
, "%s%s", ubusd_acl_dir
, suffix
);
403 if (glob(path
, GLOB_NOESCAPE
| GLOB_MARK
, NULL
, &gl
))
406 vlist_update(&ubusd_acl_files
);
407 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
408 if (stat(gl
.gl_pathv
[j
], &st
) || !S_ISREG(st
.st_mode
))
411 if (st
.st_uid
|| st
.st_gid
) {
412 syslog(LOG_ERR
, "%s has wrong owner\n", gl
.gl_pathv
[j
]);
415 if (st
.st_mode
& (S_IWOTH
| S_IWGRP
| S_IXOTH
)) {
416 syslog(LOG_ERR
, "%s has wrong permissions\n", gl
.gl_pathv
[j
]);
419 ubusd_acl_load_file(gl
.gl_pathv
[j
]);
423 vlist_flush(&ubusd_acl_files
);
425 ubusd_send_event(NULL
, "ubus.acl.sequence", ubusd_create_sequence_event_msg
, NULL
);
429 ubusd_reply_add(struct ubus_object
*obj
)
431 struct ubusd_acl_obj
*acl
;
438 * Since this tree is sorted alphabetically, we can only expect
439 * to find matching entries as long as the number of matching
440 * characters between the access list string and the object path
441 * is monotonically increasing.
443 avl_for_each_element(&ubusd_acls
, acl
, avl
) {
444 const char *key
= acl
->avl
.key
;
452 full_match
= ubus_strmatch_len(obj
->path
.key
, key
, &cur_match_len
);
453 if (cur_match_len
< match_len
)
456 match_len
= cur_match_len
;
462 if (match_len
!= strlen(key
))
466 c
= blobmsg_open_table(&b
, NULL
);
467 blobmsg_add_string(&b
, "obj", obj
->path
.key
);
469 blobmsg_add_string(&b
, "user", acl
->user
);
471 blobmsg_add_string(&b
, "group", acl
->group
);
473 blobmsg_add_field(&b
, blobmsg_type(acl
->priv
), "acl",
474 blobmsg_data(acl
->priv
), blobmsg_data_len(acl
->priv
));
476 blobmsg_close_table(&b
, c
);
480 static int ubusd_reply_query(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
, struct blob_attr
*msg
)
482 struct ubus_object
*obj
;
485 if (!attr
[UBUS_ATTR_OBJID
])
486 return UBUS_STATUS_INVALID_ARGUMENT
;
488 obj
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
490 return UBUS_STATUS_NOT_FOUND
;
492 blob_buf_init(&b
, 0);
493 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
.id
);
494 d
= blob_nest_start(&b
, UBUS_ATTR_DATA
);
496 blobmsg_add_u32(&b
, "seq", ubusd_acl_seq
);
497 a
= blobmsg_open_array(&b
, "acl");
498 list_for_each_entry(obj
, &cl
->objects
, list
)
499 ubusd_reply_add(obj
);
500 blobmsg_close_table(&b
, a
);
502 blob_nest_end(&b
, d
);
504 ubus_proto_send_msg_from_blob(cl
, ub
, UBUS_MSG_DATA
);
509 static int ubusd_acl_recv(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, const char *method
, struct blob_attr
*msg
)
511 if (!strcmp(method
, "query"))
512 return ubusd_reply_query(cl
, ub
, ubus_parse_msg(ub
->data
), msg
);
514 return UBUS_STATUS_INVALID_COMMAND
;
517 void ubusd_acl_init(void)
519 ubus_init_string_tree(&ubusd_acls
, true);
520 acl_obj
= ubusd_create_object_internal(NULL
, UBUS_SYSTEM_OBJECT_ACL
);
521 acl_obj
->recv_msg
= ubusd_acl_recv
;