]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/reply-password/reply-password.c
log: introduce new helper call log_setup_service()
[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_setup_service();
43
44 if (argc != 3) {
45 log_error("Wrong number of arguments.");
46 return EXIT_FAILURE;
47 }
48
49 if (streq(argv[1], "1")) {
50 _cleanup_string_free_erase_ char *line = NULL;
51
52 r = read_line(stdin, LONG_LINE_MAX, &line);
53 if (r < 0) {
54 log_error_errno(r, "Failed to read password: %m");
55 goto finish;
56 }
57 if (r == 0) {
58 log_error("Got EOF while reading password.");
59 r = -EIO;
60 goto finish;
61 }
62
63 packet = strjoin("+", line);
64 if (!packet) {
65 r = log_oom();
66 goto finish;
67 }
68
69 length = 1 + strlen(line) + 1;
70
71 } else if (streq(argv[1], "0")) {
72 packet = strdup("-");
73 if (!packet) {
74 r = log_oom();
75 goto finish;
76 }
77
78 length = 1;
79
80 } else {
81 log_error("Invalid first argument %s", argv[1]);
82 r = -EINVAL;
83 goto finish;
84 }
85
86 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
87 if (fd < 0) {
88 r = log_error_errno(errno, "socket() failed: %m");
89 goto finish;
90 }
91
92 r = send_on_socket(fd, argv[2], packet, length);
93
94 finish:
95 explicit_bzero_safe(packet, length);
96
97 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
98 }