]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/reply-password/reply-password.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / reply-password / reply-password.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <stddef.h>
5 #include <string.h>
6 #include <sys/socket.h>
7 #include <sys/un.h>
8
9 #include "alloc-util.h"
10 #include "fd-util.h"
11 #include "fileio.h"
12 #include "log.h"
13 #include "macro.h"
14 #include "socket-util.h"
15 #include "string-util.h"
16 #include "util.h"
17
18 static int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) {
19 union sockaddr_union sa = {};
20 int salen;
21
22 assert(fd >= 0);
23 assert(socket_name);
24 assert(packet);
25
26 salen = sockaddr_un_set_path(&sa.un, socket_name);
27 if (salen < 0)
28 return log_error_errno(salen, "Specified socket path for AF_UNIX socket invalid, refusing: %s", socket_name);
29
30 if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, salen) < 0)
31 return log_error_errno(errno, "Failed to send: %m");
32
33 return 0;
34 }
35
36 int main(int argc, char *argv[]) {
37 _cleanup_free_ char *packet = NULL;
38 _cleanup_close_ int fd = -1;
39 size_t length = 0;
40 int r;
41
42 log_set_target(LOG_TARGET_AUTO);
43 log_parse_environment();
44 log_open();
45
46 if (argc != 3) {
47 log_error("Wrong number of arguments.");
48 return EXIT_FAILURE;
49 }
50
51 if (streq(argv[1], "1")) {
52 _cleanup_string_free_erase_ char *line = NULL;
53
54 r = read_line(stdin, LONG_LINE_MAX, &line);
55 if (r < 0) {
56 log_error_errno(r, "Failed to read password: %m");
57 goto finish;
58 }
59 if (r == 0) {
60 log_error("Got EOF while reading password.");
61 r = -EIO;
62 goto finish;
63 }
64
65 packet = strjoin("+", line);
66 if (!packet) {
67 r = log_oom();
68 goto finish;
69 }
70
71 length = 1 + strlen(line) + 1;
72
73 } else if (streq(argv[1], "0")) {
74 packet = strdup("-");
75 if (!packet) {
76 r = log_oom();
77 goto finish;
78 }
79
80 length = 1;
81
82 } else {
83 log_error("Invalid first argument %s", argv[1]);
84 r = -EINVAL;
85 goto finish;
86 }
87
88 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
89 if (fd < 0) {
90 r = log_error_errno(errno, "socket() failed: %m");
91 goto finish;
92 }
93
94 r = send_on_socket(fd, argv[2], packet, length);
95
96 finish:
97 explicit_bzero_safe(packet, length);
98
99 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
100 }