aboutsummaryrefslogtreecommitdiff
path: root/src/npassd/npassd.c
blob: 22164ddcc385b29444f72ecf26d1f20eca723da5 (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
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <systemd/sd-bus.h>

#include "libnpass/libnpass.h"
#include "npassd/db.h"
#include "npassd/service.h"
#include "util.h"

#define DBUS_WELL_KNOWN_NAME "org.freedesktop.secrets"

int main(void)
{
	int ret;
	sd_bus *bus;
	char *store_path;
	struct service service;

	ret = pass_store_path_set(&store_path);
	if (ret < 0)
		return EXIT_FAILURE;

	ret = db_init(store_path, &service.db);
	if (ret < 0)
		return EXIT_FAILURE;

	ret = sd_bus_open_user(&bus);
	if (ret < 0) {
		print_err("Failed to connect to bus: %s", strerror(-ret));
		return EXIT_FAILURE;
	}

	ret = sd_bus_request_name(bus, DBUS_WELL_KNOWN_NAME, 0);
	if (ret < 0) {
		print_err("Failed to acquire service name: %s", strerror(-ret));
		if (ret == -EEXIST)
			print_err(
				"%s",
				"Is a secret-service daemon already running?");

		goto out_free_bus;
	}

	ret = service_init(bus, &service);
	if (ret < 0)
		goto out_free_bus;

	for (;;) {
		ret = sd_bus_process(bus, NULL);
		if (ret < 0) {
			fprintf(stderr, "Failed to process bus: %s\n",
				strerror(-ret));
			goto out_free_service;
		}
		if (ret > 0)
			continue;

		ret = sd_bus_wait(bus, UINT64_MAX);
		if (ret < 0) {
			fprintf(stderr, "Failed to wait on bus: %s\n",
				strerror(-ret));
			goto out_free_service;
		}
	}

out_free_service:
	service_free(&service);
out_free_bus:
	sd_bus_unref(bus);

	return ret < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}