]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/reply-password/reply-password.c
1d8b5de5f8d62dd429849ab77138ebe9edb5fc92
[thirdparty/systemd.git] / src / reply-password / reply-password.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2010 Lennart Poettering
4 ***/
5
6 #include <errno.h>
7 #include <stddef.h>
8 #include <string.h>
9 #include <sys/socket.h>
10 #include <sys/un.h>
11
12 #include "fd-util.h"
13 #include "log.h"
14 #include "macro.h"
15 #include "socket-util.h"
16 #include "string-util.h"
17 #include "util.h"
18
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,
22 };
23
24 assert(fd >= 0);
25 assert(socket_name);
26 assert(packet);
27
28 strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path));
29
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");
32
33 return 0;
34 }
35
36 int main(int argc, char *argv[]) {
37 _cleanup_close_ int fd = -1;
38 char packet[LINE_MAX];
39 size_t length;
40 int r;
41
42 log_set_target(LOG_TARGET_AUTO);
43 log_parse_environment();
44 log_open();
45
46 if (argc != 3) {
47 log_error("Wrong number of arguments.");
48 return EXIT_FAILURE;
49 }
50
51 if (streq(argv[1], "1")) {
52
53 packet[0] = '+';
54 if (!fgets(packet+1, sizeof(packet)-1, stdin)) {
55 r = log_error_errno(errno, "Failed to read password: %m");
56 goto finish;
57 }
58
59 truncate_nl(packet+1);
60 length = 1 + strlen(packet+1) + 1;
61 } else if (streq(argv[1], "0")) {
62 packet[0] = '-';
63 length = 1;
64 } else {
65 log_error("Invalid first argument %s", argv[1]);
66 r = -EINVAL;
67 goto finish;
68 }
69
70 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
71 if (fd < 0) {
72 r = log_error_errno(errno, "socket() failed: %m");
73 goto finish;
74 }
75
76 r = send_on_socket(fd, argv[2], packet, length);
77
78 finish:
79 explicit_bzero(packet, sizeof(packet));
80
81 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
82 }