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