]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/reply-password/reply-password.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / reply-password / reply-password.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
490aed58 2
490aed58 3#include <errno.h>
0e098b15 4#include <stddef.h>
07630cea
LP
5#include <string.h>
6#include <sys/socket.h>
7#include <sys/un.h>
490aed58 8
1fd27861 9#include "alloc-util.h"
3ffd4af2 10#include "fd-util.h"
1fd27861 11#include "fileio.h"
490aed58
LP
12#include "log.h"
13#include "macro.h"
fc2fffe7 14#include "socket-util.h"
07630cea 15#include "string-util.h"
490aed58
LP
16#include "util.h"
17
18static int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) {
15a3e96f
LP
19 union sockaddr_union sa = {};
20 int salen;
490aed58
LP
21
22 assert(fd >= 0);
23 assert(socket_name);
24 assert(packet);
25
15a3e96f
LP
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);
490aed58 29
15a3e96f 30 if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, salen) < 0)
4a62c710 31 return log_error_errno(errno, "Failed to send: %m");
490aed58
LP
32
33 return 0;
34}
35
36int main(int argc, char *argv[]) {
1fd27861 37 _cleanup_free_ char *packet = NULL;
1602b008 38 _cleanup_close_ int fd = -1;
fd5ad16c 39 size_t length = 0;
1602b008 40 int r;
490aed58 41
6bf3c61c 42 log_setup_service();
490aed58
LP
43
44 if (argc != 3) {
45 log_error("Wrong number of arguments.");
1602b008 46 return EXIT_FAILURE;
490aed58
LP
47 }
48
49 if (streq(argv[1], "1")) {
1fd27861 50 _cleanup_string_free_erase_ char *line = NULL;
490aed58 51
1fd27861
LP
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();
490aed58
LP
66 goto finish;
67 }
68
1fd27861
LP
69 length = 1 + strlen(line) + 1;
70
490aed58 71 } else if (streq(argv[1], "0")) {
1fd27861
LP
72 packet = strdup("-");
73 if (!packet) {
74 r = log_oom();
75 goto finish;
76 }
77
490aed58 78 length = 1;
1fd27861 79
490aed58
LP
80 } else {
81 log_error("Invalid first argument %s", argv[1]);
1602b008 82 r = -EINVAL;
490aed58
LP
83 goto finish;
84 }
85
03e334a1
LP
86 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
87 if (fd < 0) {
1602b008 88 r = log_error_errno(errno, "socket() failed: %m");
490aed58
LP
89 goto finish;
90 }
91
1602b008 92 r = send_on_socket(fd, argv[2], packet, length);
490aed58
LP
93
94finish:
87f54463 95 explicit_bzero_safe(packet, length);
490aed58 96
1602b008 97 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
490aed58 98}