2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include <sys/socket.h>
16 #include <sys/types.h>
25 #include <libubox/vlist.h>
26 #include <libubox/blobmsg_json.h>
27 #include <libubox/avl-cmp.h>
39 struct ubusd_acl_obj
{
41 struct list_head list
;
46 struct blob_attr
*methods
;
47 struct blob_attr
*tags
;
48 struct blob_attr
*priv
;
53 struct ubusd_acl_file
{
54 struct vlist_node avl
;
59 struct blob_attr
*blob
;
65 static struct blob_buf bbuf
;
66 static struct avl_tree ubusd_acls
;
67 static int ubusd_acl_seq
;
68 static struct ubus_object
*acl_obj
;
71 ubusd_acl_match_path(const void *k1
, const void *k2
, void *ptr
)
73 const char *name
= k1
;
74 const char *match
= k2
;
75 char *wildcard
= strstr(match
, "\t");
78 return strncmp(name
, match
, wildcard
- match
);
80 return strcmp(name
, match
);
84 ubusd_acl_match_cred(struct ubus_client
*cl
, struct ubusd_acl_obj
*obj
)
86 if (obj
->user
&& !strcmp(cl
->user
, obj
->user
))
89 if (obj
->group
&& !strcmp(cl
->group
, obj
->group
))
96 ubusd_acl_check(struct ubus_client
*cl
, const char *obj
,
97 const char *method
, enum ubusd_acl_type type
)
99 struct ubusd_acl_obj
*acl
;
100 struct blob_attr
*cur
;
103 if (!cl
->gid
&& !cl
->gid
)
106 acl
= avl_find_ge_element(&ubusd_acls
, obj
, acl
, avl
);
107 while (acl
&& !avl_is_last(&ubusd_acls
, &acl
->avl
)) {
108 int diff
= ubusd_acl_match_path(obj
, acl
->avl
.key
, NULL
);
113 if (ubusd_acl_match_cred(cl
, acl
)) {
114 acl
= avl_next_element(acl
, avl
);
119 case UBUS_ACL_PUBLISH
:
124 case UBUS_ACL_SUBSCRIBE
:
129 case UBUS_ACL_ACCESS
:
131 blobmsg_for_each_attr(cur
, acl
->methods
, rem
)
132 if (blobmsg_type(cur
) == BLOBMSG_TYPE_STRING
)
133 if (!ubusd_acl_match_path(method
, blobmsg_get_string(cur
), NULL
))
137 acl
= avl_next_element(acl
, avl
);
144 ubusd_acl_init_client(struct ubus_client
*cl
, int fd
)
151 unsigned int len
= sizeof(struct ucred
);
153 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &cred
, &len
) == -1)
156 memset(&cred
, 0, sizeof(cred
));
159 pwd
= getpwuid(cred
.uid
);
163 group
= getgrgid(cred
.gid
);
170 cl
->group
= strdup(group
->gr_name
);
171 cl
->user
= strdup(pwd
->pw_name
);
177 ubusd_acl_file_free(struct ubusd_acl_file
*file
)
179 struct ubusd_acl_obj
*p
, *q
;
181 list_for_each_entry_safe(p
, q
, &file
->acl
, list
) {
182 avl_delete(&ubusd_acls
, &p
->avl
);
197 static const struct blobmsg_policy acl_obj_policy
[__ACL_ACCESS_MAX
] = {
198 [ACL_ACCESS_METHODS
] = { .name
= "methods", .type
= BLOBMSG_TYPE_ARRAY
},
199 [ACL_ACCESS_TAGS
] = { .name
= "tags", .type
= BLOBMSG_TYPE_ARRAY
},
200 [ACL_ACCESS_PRIV
] = { .name
= "acl", .type
= BLOBMSG_TYPE_TABLE
},
203 static struct ubusd_acl_obj
*
204 ubusd_acl_alloc_obj(struct ubusd_acl_file
*file
, const char *obj
)
206 struct ubusd_acl_obj
*o
;
209 o
= calloc_a(1, sizeof(*o
), &k
, strlen(obj
) + 1);
210 o
->user
= file
->user
;
211 o
->group
= file
->group
;
221 list_add(&o
->list
, &file
->acl
);
222 avl_insert(&ubusd_acls
, &o
->avl
);
228 ubusd_acl_add_access(struct ubusd_acl_file
*file
, struct blob_attr
*obj
)
230 struct blob_attr
*tb
[__ACL_ACCESS_MAX
];
231 struct ubusd_acl_obj
*o
;
233 blobmsg_parse(acl_obj_policy
, __ACL_ACCESS_MAX
, tb
, blobmsg_data(obj
),
234 blobmsg_data_len(obj
));
236 if (!tb
[ACL_ACCESS_METHODS
] && !tb
[ACL_ACCESS_TAGS
] && !tb
[ACL_ACCESS_PRIV
])
239 o
= ubusd_acl_alloc_obj(file
, blobmsg_name(obj
));
241 o
->methods
= tb
[ACL_ACCESS_METHODS
];
242 o
->tags
= tb
[ACL_ACCESS_TAGS
];
243 o
->priv
= tb
[ACL_ACCESS_PRIV
];
245 if (file
->user
|| file
->group
)
250 ubusd_acl_add_subscribe(struct ubusd_acl_file
*file
, const char *obj
)
252 struct ubusd_acl_obj
*o
= ubusd_acl_alloc_obj(file
, obj
);
258 ubusd_acl_add_publish(struct ubusd_acl_file
*file
, const char *obj
)
260 struct ubusd_acl_obj
*o
= ubusd_acl_alloc_obj(file
, obj
);
275 static const struct blobmsg_policy acl_policy
[__ACL_MAX
] = {
276 [ACL_USER
] = { .name
= "user", .type
= BLOBMSG_TYPE_STRING
},
277 [ACL_GROUP
] = { .name
= "group", .type
= BLOBMSG_TYPE_STRING
},
278 [ACL_ACCESS
] = { .name
= "access", .type
= BLOBMSG_TYPE_TABLE
},
279 [ACL_PUBLISH
] = { .name
= "publish", .type
= BLOBMSG_TYPE_ARRAY
},
280 [ACL_SUBSCRIBE
] = { .name
= "subscribe", .type
= BLOBMSG_TYPE_ARRAY
},
281 [ACL_INHERIT
] = { .name
= "inherit", .type
= BLOBMSG_TYPE_ARRAY
},
285 ubusd_acl_file_add(struct ubusd_acl_file
*file
)
287 struct blob_attr
*tb
[__ACL_MAX
], *cur
;
290 blobmsg_parse(acl_policy
, __ACL_MAX
, tb
, blob_data(file
->blob
),
291 blob_len(file
->blob
));
294 file
->user
= blobmsg_get_string(tb
[ACL_USER
]);
295 else if (tb
[ACL_GROUP
])
296 file
->group
= blobmsg_get_string(tb
[ACL_GROUP
]);
300 if (!tb
[ACL_ACCESS
] && !tb
[ACL_PUBLISH
] && !tb
[ACL_INHERIT
])
304 blobmsg_for_each_attr(cur
, tb
[ACL_ACCESS
], rem
)
305 ubusd_acl_add_access(file
, cur
);
307 if (tb
[ACL_SUBSCRIBE
])
308 blobmsg_for_each_attr(cur
, tb
[ACL_SUBSCRIBE
], rem
)
309 if (blobmsg_type(cur
) == BLOBMSG_TYPE_STRING
)
310 ubusd_acl_add_subscribe(file
, blobmsg_get_string(cur
));
313 blobmsg_for_each_attr(cur
, tb
[ACL_PUBLISH
], rem
)
314 if (blobmsg_type(cur
) == BLOBMSG_TYPE_STRING
)
315 ubusd_acl_add_publish(file
, blobmsg_get_string(cur
));
319 ubusd_acl_update_cb(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
320 struct vlist_node
*node_old
)
322 struct ubusd_acl_file
*file
;
325 file
= container_of(node_old
, struct ubusd_acl_file
, avl
);
326 ubusd_acl_file_free(file
);
330 file
= container_of(node_new
, struct ubusd_acl_file
, avl
);
331 ubusd_acl_file_add(file
);
335 static struct ubus_msg_buf
*
336 ubusd_create_sequence_event_msg(void *priv
, const char *id
)
340 blob_buf_init(&b
, 0);
341 blob_put_int32(&b
, UBUS_ATTR_OBJID
, 0);
342 blob_put_string(&b
, UBUS_ATTR_METHOD
, id
);
343 s
= blob_nest_start(&b
, UBUS_ATTR_DATA
);
344 blobmsg_add_u32(&b
, "sequence", ubusd_acl_seq
);
345 blob_nest_end(&b
, s
);
347 return ubus_msg_new(b
.head
, blob_raw_len(b
.head
), true);
350 static VLIST_TREE(ubusd_acl_files
, avl_strcmp
, ubusd_acl_update_cb
, false, false);
353 ubusd_acl_load_file(const char *filename
)
355 struct ubusd_acl_file
*file
;
358 blob_buf_init(&bbuf
, 0);
359 if (!blobmsg_add_json_from_file(&bbuf
, filename
)) {
360 syslog(LOG_ERR
, "failed to parse %s\n", filename
);
364 file
= calloc_a(sizeof(*file
), &blob
, blob_raw_len(bbuf
.head
));
370 memcpy(blob
, bbuf
.head
, blob_raw_len(bbuf
.head
));
371 INIT_LIST_HEAD(&file
->acl
);
373 vlist_add(&ubusd_acl_files
, &file
->avl
, filename
);
374 syslog(LOG_INFO
, "loading %s\n", filename
);
386 if (glob("/usr/share/acl.d/*.json", GLOB_NOESCAPE
| GLOB_MARK
, NULL
, &gl
))
389 vlist_update(&ubusd_acl_files
);
390 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
391 if (stat(gl
.gl_pathv
[j
], &st
) || !S_ISREG(st
.st_mode
))
394 if (st
.st_uid
|| st
.st_gid
) {
395 syslog(LOG_ERR
, "%s has wrong owner\n", gl
.gl_pathv
[j
]);
398 if (st
.st_mode
& (S_IWOTH
| S_IWGRP
| S_IXOTH
)) {
399 syslog(LOG_ERR
, "%s has wrong permissions\n", gl
.gl_pathv
[j
]);
402 ubusd_acl_load_file(gl
.gl_pathv
[j
]);
406 vlist_flush(&ubusd_acl_files
);
408 ubusd_send_event(NULL
, "ubus.acl.sequence", ubusd_create_sequence_event_msg
, NULL
);
412 ubusd_reply_add(struct ubus_object
*obj
)
414 struct ubusd_acl_obj
*acl
;
418 acl
= avl_find_ge_element(&ubusd_acls
, obj
->path
.key
, acl
, avl
);
419 while (acl
&& !avl_is_last(&ubusd_acls
, &acl
->avl
) &&
420 !ubusd_acl_match_path(obj
->path
.key
, acl
->avl
.key
, NULL
)) {
423 void *c
= blobmsg_open_table(&b
, NULL
);
425 blobmsg_add_string(&b
, "obj", obj
->path
.key
);
427 blobmsg_add_string(&b
, "user", acl
->user
);
429 blobmsg_add_string(&b
, "group", acl
->group
);
432 blobmsg_add_field(&b
, blobmsg_type(acl
->priv
), "acl",
433 blobmsg_data(acl
->priv
), blobmsg_data_len(acl
->priv
));
435 blobmsg_close_table(&b
, c
);
437 acl
= avl_next_element(acl
, avl
);
440 static int ubusd_reply_query(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, struct blob_attr
**attr
, struct blob_attr
*msg
)
442 struct ubus_object
*obj
;
445 if (!attr
[UBUS_ATTR_OBJID
])
446 return UBUS_STATUS_INVALID_ARGUMENT
;
448 obj
= ubusd_find_object(blob_get_u32(attr
[UBUS_ATTR_OBJID
]));
450 return UBUS_STATUS_NOT_FOUND
;
452 blob_buf_init(&b
, 0);
453 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
.id
);
454 d
= blob_nest_start(&b
, UBUS_ATTR_DATA
);
456 blobmsg_add_u32(&b
, "seq", ubusd_acl_seq
);
457 a
= blobmsg_open_array(&b
, "acl");
458 list_for_each_entry(obj
, &cl
->objects
, list
)
459 ubusd_reply_add(obj
);
460 blobmsg_close_table(&b
, a
);
462 blob_nest_end(&b
, d
);
464 ubus_proto_send_msg_from_blob(cl
, ub
, UBUS_MSG_DATA
);
469 static int ubusd_acl_recv(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, const char *method
, struct blob_attr
*msg
)
471 if (!strcmp(method
, "query"))
472 return ubusd_reply_query(cl
, ub
, ubus_parse_msg(ub
->data
), msg
);
474 return UBUS_STATUS_INVALID_COMMAND
;
477 void ubusd_acl_init(void)
479 avl_init(&ubusd_acls
, ubusd_acl_match_path
, true, NULL
);
480 acl_obj
= ubusd_create_object_internal(NULL
, UBUS_SYSTEM_OBJECT_ACL
);
481 acl_obj
->recv_msg
= ubusd_acl_recv
;