1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <systemd/sd-bus.h>
#include "npassd/common.h"
#include "npassd/service.h"
#include "util.h"
#define SERVICE_IFACE "org.freedesktop.Secret.Service"
static int handle_open_session(sd_bus_message *msg, void *data,
sd_bus_error *ret_error);
static int handle_search_items(sd_bus_message *msg, void *data,
sd_bus_error *ret_error);
static int handle_create_collection(sd_bus_message *msg, void *data,
sd_bus_error *ret_error);
static int handle_read_alias(sd_bus_message *msg, void *data,
sd_bus_error *ret_error);
static const sd_bus_vtable service_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_METHOD("OpenSession", "sv", "vo", handle_open_session,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("SearchItems", "a{ss}", "aoao", handle_search_items,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("CreateCollection", "a{sv}s", "oo",
handle_create_collection, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ReadAlias", "s", "o", handle_read_alias,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_VTABLE_END,
};
static int handle_search_items(__attribute__((unused)) sd_bus_message *msg,
__attribute__((unused)) void *data,
__attribute__((unused)) sd_bus_error *ret_error)
{
return sd_bus_reply_method_return(msg, "aoao", 0, 0);
}
static int handle_create_collection(sd_bus_message *msg, void *data,
__attribute__((unused))
sd_bus_error *ret_error)
{
const char *label, *alias, *key;
struct service *service = data;
struct collection *collection;
char *root;
int ret;
ret = sd_bus_message_enter_container(msg, SD_BUS_TYPE_ARRAY, "{sv}");
if (ret <= 0) {
print_err("%s", strerror(ret < 0 ? -ret : EINVAL));
return ret < 0 ? ret : -EINVAL;
}
ret = sd_bus_message_enter_container(msg, SD_BUS_TYPE_DICT_ENTRY, "sv");
if (ret <= 0) {
print_err("%s", strerror(ret < 0 ? -ret : EINVAL));
return ret < 0 ? ret : -EINVAL;
}
ret = sd_bus_message_read(msg, "s", &key);
if (ret < 0) {
print_err("%s", strerror(-ret));
return ret;
}
ret = strcmp("org.freedesktop.Secret.Collection.Label", key);
if (ret) {
print_err("Unsupported property key: %s", key);
return -EINVAL;
}
ret = sd_bus_message_read(msg, "v", "s", &label);
if (ret < 0) {
print_err("%s", strerror(-ret));
return ret;
}
ret = sd_bus_message_exit_container(msg);
if (ret < 0) {
print_err("%s", strerror(-ret));
return ret;
}
ret = sd_bus_message_exit_container(msg);
if (ret < 0) {
print_err("%s", strerror(-ret));
return ret;
}
ret = sd_bus_message_read(msg, "s", &alias);
if (ret < 0) {
print_err("%s", strerror(-ret));
return ret;
}
ret = collection_root_make(label, alias, &root);
if (ret < 0)
return ret;
ret = collection_new(service->bus, service->db, &collection, alias,
label, root);
free(root);
if (ret < 0)
return ret;
ret = sd_bus_reply_method_return(msg, "oo", collection->path, "/");
if (ret < 0) {
print_err("%s", strerror(-ret));
return ret;
}
LIST_INSERT_HEAD(&service->collections, collection, dlist);
return ret;
}
static int handle_read_alias(sd_bus_message *msg, void *data,
__attribute__((unused)) sd_bus_error *ret_error)
{
struct service *service = data;
struct collection *collection;
const char *alias;
int ret;
ret = sd_bus_message_read(msg, "s", &alias);
if (ret < 0) {
print_err("%s", strerror(-ret));
return ret;
}
ret = collection_alias_search(&service->collections, alias,
&collection);
if (ret >= 0) {
ret = sd_bus_reply_method_return(msg, "o", collection->path);
if (ret < 0)
print_err("%s", strerror(-ret));
return ret;
}
ret = collection_db_alias_read(service->bus, service->db, &collection,
alias);
if (ret < 0) {
ret = sd_bus_reply_method_return(msg, "o", "/");
if (ret < 0)
print_err("%s", strerror(-ret));
return ret;
}
ret = sd_bus_reply_method_return(msg, "o", collection->path);
if (ret < 0) {
print_err("%s", strerror(-ret));
return ret;
}
LIST_INSERT_HEAD(&service->collections, collection, dlist);
return ret;
}
static int handle_open_session(sd_bus_message *msg, void *data,
sd_bus_error *ret_error)
{
struct service *service = data;
struct session *session;
const char *sender;
char *algo;
int ret;
ret = sd_bus_message_read(msg, "s", &algo);
if (ret < 0)
return ret;
ret = strcmp(algo, "plain");
if (ret) {
sd_bus_error_set_const(ret_error, SD_BUS_ERROR_NOT_SUPPORTED,
"Unsupported encryption algorithm");
return -ENOTSUP;
}
sender = sd_bus_message_get_sender(msg);
ret = session_new(service->bus, &session, sender);
if (ret < 0)
return ret;
ret = sd_bus_reply_method_return(msg, "vo", "s", NULL, session->path);
if (ret < 0) {
print_err("%s", strerror(-ret));
return ret;
}
LIST_INSERT_HEAD(&service->sessions, session, dlist);
return ret;
}
void service_free(struct service *service)
{
struct session *s;
struct collection *c;
LIST_FOREACH (s, &service->sessions, dlist)
session_free(s);
LIST_FOREACH (c, &service->collections, dlist)
collection_free(c);
sd_bus_slot_unref(service->slot);
}
int service_init(sd_bus *bus, struct service *service)
{
int ret;
service->bus = bus;
LIST_INIT(&service->sessions);
LIST_INIT(&service->collections);
ret = sd_bus_add_object_vtable(service->bus, &service->slot,
DBUS_OBJECT_PATH, SERVICE_IFACE,
service_vtable, service);
if (ret < 0)
print_err("Failed to connect to bus: %s", strerror(-ret));
return ret;
}
|