]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/reply-password/reply-password.c
97b8e30aa3a51348089d4ee4a1e0ada3467a6ba6
[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 <sys/socket.h>
6 #include <sys/un.h>
7
8 #include "alloc-util.h"
9 #include "main-func.h"
10 #include "fd-util.h"
11 #include "fileio.h"
12 #include "log.h"
13 #include "macro.h"
14 #include "memory-util.h"
15 #include "socket-util.h"
16 #include "string-util.h"
17 #include "util.h"
18
19 static int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) {
20 union sockaddr_union sa = {};
21 int salen;
22
23 assert(fd >= 0);
24 assert(socket_name);
25 assert(packet);
26
27 salen = sockaddr_un_set_path(&sa.un, socket_name);
28 if (salen < 0)
29 return log_error_errno(salen, "Specified socket path for AF_UNIX socket invalid, refusing: %s", socket_name);
30
31 if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, salen) < 0)
32 return log_error_errno(errno, "Failed to send: %m");
33
34 return 0;
35 }
36
37 static int run(int argc, char *argv[]) {
38 _cleanup_(erase_and_freep) char *packet = NULL;
39 _cleanup_close_ int fd = -1;
40 size_t length = 0;
41 int r;
42
43 log_setup_service();
44
45 if (argc != 3)
46 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Wrong number of arguments.");
47
48 if (streq(argv[1], "1")) {
49 _cleanup_(erase_and_freep) char *line = NULL;
50
51 r = read_line(stdin, LONG_LINE_MAX, &line);
52 if (r < 0)
53 return log_error_errno(r, "Failed to read password: %m");
54 if (r == 0)
55 return log_error_errno(SYNTHETIC_ERRNO(EIO),
56 "Got EOF while reading password.");
57
58 packet = strjoin("+", line);
59 if (!packet)
60 return log_oom();
61
62 length = 1 + strlen(line) + 1;
63
64 } else if (streq(argv[1], "0")) {
65 packet = strdup("-");
66 if (!packet)
67 return log_oom();
68
69 length = 1;
70
71 } else
72 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
73 "Invalid first argument %s", argv[1]);
74
75 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
76 if (fd < 0)
77 return log_error_errno(errno, "socket() failed: %m");
78
79 return send_on_socket(fd, argv[2], packet, length);
80 }
81
82 DEFINE_MAIN_FUNCTION(run);