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