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