]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/home/homed-bus.c
0193089668d7119969a3a701b02e4c0079048c94
[thirdparty/systemd.git] / src / home / homed-bus.c
1 #include "homed-bus.h"
2 #include "strv.h"
3
4 int bus_message_read_secret(sd_bus_message *m, UserRecord **ret, sd_bus_error *error) {
5 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *full = NULL;
6 _cleanup_(user_record_unrefp) UserRecord *hr = NULL;
7 unsigned line = 0, column = 0;
8 const char *json;
9 int r;
10
11 assert(ret);
12
13 r = sd_bus_message_read(m, "s", &json);
14 if (r < 0)
15 return r;
16
17 r = json_parse(json, JSON_PARSE_SENSITIVE, &v, &line, &column);
18 if (r < 0)
19 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Failed to parse JSON secret record at %u:%u: %m", line, column);
20
21 r = json_build(&full, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("secret", JSON_BUILD_VARIANT(v))));
22 if (r < 0)
23 return r;
24
25 hr = user_record_new();
26 if (!hr)
27 return -ENOMEM;
28
29 r = user_record_load(hr, full, USER_RECORD_REQUIRE_SECRET);
30 if (r < 0)
31 return r;
32
33 *ret = TAKE_PTR(hr);
34 return 0;
35 }
36
37 int bus_message_read_home_record(sd_bus_message *m, UserRecordLoadFlags flags, UserRecord **ret, sd_bus_error *error) {
38 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
39 _cleanup_(user_record_unrefp) UserRecord *hr = NULL;
40 unsigned line = 0, column = 0;
41 const char *json;
42 int r;
43
44 assert(ret);
45
46 r = sd_bus_message_read(m, "s", &json);
47 if (r < 0)
48 return r;
49
50 r = json_parse(json, JSON_PARSE_SENSITIVE, &v, &line, &column);
51 if (r < 0)
52 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Failed to parse JSON identity record at %u:%u: %m", line, column);
53
54 hr = user_record_new();
55 if (!hr)
56 return -ENOMEM;
57
58 r = user_record_load(hr, v, flags);
59 if (r < 0)
60 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "JSON data is not a valid identity record");
61
62 *ret = TAKE_PTR(hr);
63 return 0;
64 }