]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/reply-password/reply-password.c
1d8b5de5f8d62dd429849ab77138ebe9edb5fc92
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
9 #include <sys/socket.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
= {
21 .un
.sun_family
= AF_UNIX
,
28 strncpy(sa
.un
.sun_path
, socket_name
, sizeof(sa
.un
.sun_path
));
30 if (sendto(fd
, packet
, size
, MSG_NOSIGNAL
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
)) < 0)
31 return log_error_errno(errno
, "Failed to send: %m");
36 int main(int argc
, char *argv
[]) {
37 _cleanup_close_
int fd
= -1;
38 char packet
[LINE_MAX
];
42 log_set_target(LOG_TARGET_AUTO
);
43 log_parse_environment();
47 log_error("Wrong number of arguments.");
51 if (streq(argv
[1], "1")) {
54 if (!fgets(packet
+1, sizeof(packet
)-1, stdin
)) {
55 r
= log_error_errno(errno
, "Failed to read password: %m");
59 truncate_nl(packet
+1);
60 length
= 1 + strlen(packet
+1) + 1;
61 } else if (streq(argv
[1], "0")) {
65 log_error("Invalid first argument %s", argv
[1]);
70 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
72 r
= log_error_errno(errno
, "socket() failed: %m");
76 r
= send_on_socket(fd
, argv
[2], packet
, length
);
79 explicit_bzero(packet
, sizeof(packet
));
81 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;