aboutsummaryrefslogtreecommitdiff
path: root/src/npassd/service.c
blob: c93073a16792ae4dd7c26a0a3150bfcee507c761 (plain) (blame)
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
#include <errno.h>
#include <stdio.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 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_VTABLE_END,
};

static int handle_open_session(sd_bus_message *msg, void *data,
			       sd_bus_error *ret_error)
{
	struct service *service = data;
	int ret, session_slot_no;
	const char *sender;
	char *algo;

	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;
	}

	session_slot_no = session_slot_available(service->session, MAX_SESSION);
	if (session_slot_no < 0) {
		print_err("No free slot available: %s",
			  strerror(-session_slot_no));
		return ret;
	}

	sender = sd_bus_message_get_sender(msg);
	ret = session_new(service->bus, &service->session[session_slot_no],
			  session_slot_no, sender);
	if (ret < 0)
		return ret;

	return sd_bus_reply_method_return(
		msg, "vo", "s", NULL, service->session[session_slot_no].path);
}

void service_free(struct service *service)
{
	for (int i = 0; i < MAX_SESSION; i++)
		session_free(&service->session[i]);

	sd_bus_slot_unref(service->slot);
}

int service_init(sd_bus *bus, struct service *service)
{
	int ret;

	service->bus = bus;
	session_init(service->session, MAX_SESSION);

	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;
}