]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/reply-password/reply-password.c
97b8e30aa3a51348089d4ee4a1e0ada3467a6ba6
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include <sys/socket.h>
8 #include "alloc-util.h"
14 #include "memory-util.h"
15 #include "socket-util.h"
16 #include "string-util.h"
19 static int send_on_socket(int fd
, const char *socket_name
, const void *packet
, size_t size
) {
20 union sockaddr_union sa
= {};
27 salen
= sockaddr_un_set_path(&sa
.un
, socket_name
);
29 return log_error_errno(salen
, "Specified socket path for AF_UNIX socket invalid, refusing: %s", socket_name
);
31 if (sendto(fd
, packet
, size
, MSG_NOSIGNAL
, &sa
.sa
, salen
) < 0)
32 return log_error_errno(errno
, "Failed to send: %m");
37 static int run(int argc
, char *argv
[]) {
38 _cleanup_(erase_and_freep
) char *packet
= NULL
;
39 _cleanup_close_
int fd
= -1;
46 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Wrong number of arguments.");
48 if (streq(argv
[1], "1")) {
49 _cleanup_(erase_and_freep
) char *line
= NULL
;
51 r
= read_line(stdin
, LONG_LINE_MAX
, &line
);
53 return log_error_errno(r
, "Failed to read password: %m");
55 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
56 "Got EOF while reading password.");
58 packet
= strjoin("+", line
);
62 length
= 1 + strlen(line
) + 1;
64 } else if (streq(argv
[1], "0")) {
72 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
73 "Invalid first argument %s", argv
[1]);
75 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
77 return log_error_errno(errno
, "socket() failed: %m");
79 return send_on_socket(fd
, argv
[2], packet
, length
);
82 DEFINE_MAIN_FUNCTION(run
);