2 This file is part of systemd.
4 Copyright 2014 Daniel Mack
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
23 #include "bus-endpoint.h"
24 #include "bus-kernel.h"
25 #include "bus-policy.h"
28 int bus_kernel_set_endpoint_policy(int fd
, uid_t uid
, BusEndpoint
*ep
) {
30 struct kdbus_cmd
*update
;
32 BusEndpointPolicy
*po
;
37 size
= ALIGN8(offsetof(struct kdbus_cmd
, items
));
39 HASHMAP_FOREACH(po
, ep
->policy_hash
, i
) {
40 size
+= ALIGN8(offsetof(struct kdbus_item
, str
) + strlen(po
->name
) + 1);
41 size
+= ALIGN8(offsetof(struct kdbus_item
, policy_access
) + sizeof(struct kdbus_policy_access
));
44 update
= alloca0_align(size
, 8);
49 HASHMAP_FOREACH(po
, ep
->policy_hash
, i
) {
50 n
->type
= KDBUS_ITEM_NAME
;
51 n
->size
= offsetof(struct kdbus_item
, str
) + strlen(po
->name
) + 1;
52 strcpy(n
->str
, po
->name
);
53 n
= KDBUS_ITEM_NEXT(n
);
55 n
->type
= KDBUS_ITEM_POLICY_ACCESS
;
56 n
->size
= offsetof(struct kdbus_item
, policy_access
) + sizeof(struct kdbus_policy_access
);
58 n
->policy_access
.type
= KDBUS_POLICY_ACCESS_USER
;
59 n
->policy_access
.access
= bus_kernel_translate_access(po
->access
);
60 n
->policy_access
.id
= uid
;
62 n
= KDBUS_ITEM_NEXT(n
);
65 r
= ioctl(fd
, KDBUS_CMD_ENDPOINT_UPDATE
, update
);
72 int bus_endpoint_new(BusEndpoint
**ep
) {
75 *ep
= new0(BusEndpoint
, 1);
82 int bus_endpoint_add_policy(BusEndpoint
*ep
, const char *name
, BusPolicyAccess access
) {
83 _cleanup_free_ BusEndpointPolicy
*po
= NULL
;
84 _cleanup_free_
char *key
= NULL
;
89 assert(access
> _BUS_POLICY_ACCESS_INVALID
&& access
< _BUS_POLICY_ACCESS_MAX
);
91 /* check if we already have this name in the policy list. If we do, see if the new access level
92 * is higher than the exising one, and upgrade the entry in that case. Otherwise, do nothing.
95 if (ep
->policy_hash
) {
96 po
= hashmap_get(ep
->policy_hash
, name
);
98 if (po
->access
< access
)
104 ep
->policy_hash
= hashmap_new(&string_hash_ops
);
105 if (!ep
->policy_hash
)
109 po
= new0(BusEndpointPolicy
, 1);
120 r
= hashmap_put(ep
->policy_hash
, key
, po
);
129 void bus_endpoint_free(BusEndpoint
*endpoint
) {
133 hashmap_free_free_free(endpoint
->policy_hash
);