]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/reply-password/reply-password.c
Merge pull request #10450 from poettering/foreach-line-excorcism
[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
LP
9#include "alloc-util.h"
10#include "def.h"
3ffd4af2 11#include "fd-util.h"
1fd27861 12#include "fileio.h"
490aed58
LP
13#include "log.h"
14#include "macro.h"
fc2fffe7 15#include "socket-util.h"
07630cea 16#include "string-util.h"
490aed58
LP
17#include "util.h"
18
19static int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) {
15a3e96f
LP
20 union sockaddr_union sa = {};
21 int salen;
490aed58
LP
22
23 assert(fd >= 0);
24 assert(socket_name);
25 assert(packet);
26
15a3e96f
LP
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);
490aed58 30
15a3e96f 31 if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, salen) < 0)
4a62c710 32 return log_error_errno(errno, "Failed to send: %m");
490aed58
LP
33
34 return 0;
35}
36
37int main(int argc, char *argv[]) {
1fd27861 38 _cleanup_free_ char *packet = NULL;
1602b008 39 _cleanup_close_ int fd = -1;
490aed58 40 size_t length;
1602b008 41 int r;
490aed58 42
4cfa2c99 43 log_set_target(LOG_TARGET_AUTO);
490aed58
LP
44 log_parse_environment();
45 log_open();
46
47 if (argc != 3) {
48 log_error("Wrong number of arguments.");
1602b008 49 return EXIT_FAILURE;
490aed58
LP
50 }
51
52 if (streq(argv[1], "1")) {
1fd27861 53 _cleanup_string_free_erase_ char *line = NULL;
490aed58 54
1fd27861
LP
55 r = read_line(stdin, LONG_LINE_MAX, &line);
56 if (r < 0) {
57 log_error_errno(r, "Failed to read password: %m");
58 goto finish;
59 }
60 if (r == 0) {
61 log_error("Got EOF while reading password.");
62 r = -EIO;
63 goto finish;
64 }
65
66 packet = strjoin("+", line);
67 if (!packet) {
68 r = log_oom();
490aed58
LP
69 goto finish;
70 }
71
1fd27861
LP
72 length = 1 + strlen(line) + 1;
73
490aed58 74 } else if (streq(argv[1], "0")) {
1fd27861
LP
75 packet = strdup("-");
76 if (!packet) {
77 r = log_oom();
78 goto finish;
79 }
80
490aed58 81 length = 1;
1fd27861 82
490aed58
LP
83 } else {
84 log_error("Invalid first argument %s", argv[1]);
1602b008 85 r = -EINVAL;
490aed58
LP
86 goto finish;
87 }
88
03e334a1
LP
89 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
90 if (fd < 0) {
1602b008 91 r = log_error_errno(errno, "socket() failed: %m");
490aed58
LP
92 goto finish;
93 }
94
1602b008 95 r = send_on_socket(fd, argv[2], packet, length);
490aed58
LP
96
97finish:
1fd27861 98 explicit_bzero(packet, length);
490aed58 99
1602b008 100 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
490aed58 101}