uci: tighten uci delete operation error handling

- Return UBUS_STATUS_NOT_FOUND if no object could be resolved for delete
- Return UBUS_STATUS_INVALID_ARGUMENT for invalid section names or types

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2018-08-08 22:53:21 +02:00
parent c2c612b67c
commit f91751b767

43
uci.c
View file

@ -946,14 +946,14 @@ rpc_uci_set(struct ubus_context *ctx, struct ubus_object *obj,
* 2) if the blob is of type string, delete the option named after its value * 2) if the blob is of type string, delete the option named after its value
* 3) if the blob is NULL, delete entire section * 3) if the blob is NULL, delete entire section
*/ */
static void static int
rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr) rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr)
{ {
struct blob_attr *cur; struct blob_attr *cur;
int rem; int rem, rv;
if (rpc_uci_lookup(ptr) || !ptr->s) if (rpc_uci_lookup(ptr) || !ptr->s)
return; return UBUS_STATUS_NOT_FOUND;
if (!opt) if (!opt)
{ {
@ -961,9 +961,12 @@ rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr)
ptr->option = NULL; ptr->option = NULL;
uci_delete(cursor, ptr); uci_delete(cursor, ptr);
return 0;
} }
else if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY) else if (blobmsg_type(opt) == BLOBMSG_TYPE_ARRAY)
{ {
rv = UBUS_STATUS_NOT_FOUND;
blobmsg_for_each_attr(cur, opt, rem) blobmsg_for_each_attr(cur, opt, rem)
{ {
if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
@ -976,7 +979,10 @@ rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr)
continue; continue;
uci_delete(cursor, ptr); uci_delete(cursor, ptr);
rv = 0;
} }
return rv;
} }
else if (blobmsg_type(opt) == BLOBMSG_TYPE_STRING) else if (blobmsg_type(opt) == BLOBMSG_TYPE_STRING)
{ {
@ -984,10 +990,13 @@ rpc_uci_merge_delete(struct blob_attr *opt, struct uci_ptr *ptr)
ptr->option = blobmsg_data(opt); ptr->option = blobmsg_data(opt);
if (rpc_uci_lookup(ptr) || !ptr->o) if (rpc_uci_lookup(ptr) || !ptr->o)
return; return UBUS_STATUS_NOT_FOUND;
uci_delete(cursor, ptr); uci_delete(cursor, ptr);
return 0;
} }
return UBUS_STATUS_INVALID_ARGUMENT;
} }
static int static int
@ -999,6 +1008,7 @@ rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
struct uci_package *p = NULL; struct uci_package *p = NULL;
struct uci_element *e, *tmp; struct uci_element *e, *tmp;
struct uci_ptr ptr = { 0 }; struct uci_ptr ptr = { 0 };
int err = 0;
blobmsg_parse(rpc_uci_delete_policy, __RPC_D_MAX, tb, blobmsg_parse(rpc_uci_delete_policy, __RPC_D_MAX, tb,
blob_data(msg), blob_len(msg)); blob_data(msg), blob_len(msg));
@ -1010,6 +1020,14 @@ rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
if (!rpc_uci_write_access(tb[RPC_D_SESSION], tb[RPC_D_CONFIG])) if (!rpc_uci_write_access(tb[RPC_D_SESSION], tb[RPC_D_CONFIG]))
return UBUS_STATUS_PERMISSION_DENIED; return UBUS_STATUS_PERMISSION_DENIED;
if (tb[RPC_D_TYPE] &&
!rpc_uci_verify_type(blobmsg_data(tb[RPC_D_TYPE])))
return UBUS_STATUS_INVALID_ARGUMENT;
if (tb[RPC_D_SECTION] &&
!rpc_uci_verify_section(blobmsg_data(tb[RPC_D_SECTION])))
return UBUS_STATUS_INVALID_ARGUMENT;
ptr.package = blobmsg_data(tb[RPC_D_CONFIG]); ptr.package = blobmsg_data(tb[RPC_D_CONFIG]);
if (uci_load(cursor, ptr.package, &p)) if (uci_load(cursor, ptr.package, &p))
@ -1020,9 +1038,9 @@ rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
ptr.section = blobmsg_data(tb[RPC_D_SECTION]); ptr.section = blobmsg_data(tb[RPC_D_SECTION]);
if (tb[RPC_D_OPTIONS]) if (tb[RPC_D_OPTIONS])
rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr); err = rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
else else
rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr); err = rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
} }
else else
{ {
@ -1036,16 +1054,21 @@ rpc_uci_delete(struct ubus_context *ctx, struct ubus_object *obj,
ptr.section = e->name; ptr.section = e->name;
if (tb[RPC_D_OPTIONS]) if (tb[RPC_D_OPTIONS])
rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr); err = rpc_uci_merge_delete(tb[RPC_D_OPTIONS], &ptr);
else else
rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr); err = rpc_uci_merge_delete(tb[RPC_D_OPTION], &ptr);
}
} }
if (!err && !ptr.section)
err = UBUS_STATUS_NOT_FOUND;
}
if (!err)
uci_save(cursor, p); uci_save(cursor, p);
uci_unload(cursor, p); uci_unload(cursor, p);
return rpc_uci_status(); return err ? err : rpc_uci_status();
} }
static int static int