]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/reply-password/reply-password.c
ed63927fa69175462f0b9723d194f685e466c188
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "socket-util.h"
8 #include "string-util.h"
10 static int send_on_socket(int fd
, const char *socket_name
, const void *packet
, size_t size
) {
11 union sockaddr_union sa
= {};
18 salen
= sockaddr_un_set_path(&sa
.un
, socket_name
);
20 return log_error_errno(salen
, "Specified socket path for AF_UNIX socket invalid, refusing: %s", socket_name
);
22 if (sendto(fd
, packet
, size
, MSG_NOSIGNAL
, &sa
.sa
, salen
) < 0)
23 return log_error_errno(errno
, "Failed to send: %m");
28 static int run(int argc
, char *argv
[]) {
29 _cleanup_(erase_and_freep
) char *packet
= NULL
;
30 _cleanup_close_
int fd
= -EBADF
;
37 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Wrong number of arguments.");
39 if (streq(argv
[1], "1")) {
40 _cleanup_(erase_and_freep
) char *line
= NULL
;
42 r
= read_line(stdin
, LONG_LINE_MAX
, &line
);
44 return log_error_errno(r
, "Failed to read password: %m");
46 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
47 "Got EOF while reading password.");
49 packet
= strjoin("+", line
);
53 length
= 1 + strlen(line
) + 1;
55 } else if (streq(argv
[1], "0")) {
63 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
64 "Invalid first argument %s", argv
[1]);
66 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
68 return log_error_errno(errno
, "socket() failed: %m");
70 return send_on_socket(fd
, argv
[2], packet
, length
);
73 DEFINE_MAIN_FUNCTION(run
);