]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/reply-password/reply-password.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include <sys/socket.h>
12 #include "socket-util.h"
13 #include "string-util.h"
16 static int send_on_socket(int fd
, const char *socket_name
, const void *packet
, size_t size
) {
17 union sockaddr_union sa
= {
18 .un
.sun_family
= AF_UNIX
,
25 strncpy(sa
.un
.sun_path
, socket_name
, sizeof(sa
.un
.sun_path
));
27 if (sendto(fd
, packet
, size
, MSG_NOSIGNAL
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
)) < 0)
28 return log_error_errno(errno
, "Failed to send: %m");
33 int main(int argc
, char *argv
[]) {
34 _cleanup_close_
int fd
= -1;
35 char packet
[LINE_MAX
];
39 log_set_target(LOG_TARGET_AUTO
);
40 log_parse_environment();
44 log_error("Wrong number of arguments.");
48 if (streq(argv
[1], "1")) {
51 if (!fgets(packet
+1, sizeof(packet
)-1, stdin
)) {
52 r
= log_error_errno(errno
, "Failed to read password: %m");
56 truncate_nl(packet
+1);
57 length
= 1 + strlen(packet
+1) + 1;
58 } else if (streq(argv
[1], "0")) {
62 log_error("Invalid first argument %s", argv
[1]);
67 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
69 r
= log_error_errno(errno
, "socket() failed: %m");
73 r
= send_on_socket(fd
, argv
[2], packet
, length
);
76 explicit_bzero(packet
, sizeof(packet
));
78 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;