]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/reply-password/reply-password.c
tree-wide: drop string.h when string-util.h or friends are included
[thirdparty/systemd.git] / src / reply-password / reply-password.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
490aed58 2
490aed58 3#include <errno.h>
0e098b15 4#include <stddef.h>
07630cea
LP
5#include <sys/socket.h>
6#include <sys/un.h>
490aed58 7
1fd27861 8#include "alloc-util.h"
c7dd2095 9#include "main-func.h"
3ffd4af2 10#include "fd-util.h"
1fd27861 11#include "fileio.h"
490aed58
LP
12#include "log.h"
13#include "macro.h"
090a9c1e 14#include "memory-util.h"
fc2fffe7 15#include "socket-util.h"
07630cea 16#include "string-util.h"
490aed58
LP
17#include "util.h"
18
19static int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) {
15a3e96f
LP
20 union sockaddr_union sa = {};
21 int salen;
490aed58
LP
22
23 assert(fd >= 0);
24 assert(socket_name);
25 assert(packet);
26
15a3e96f
LP
27 salen = sockaddr_un_set_path(&sa.un, socket_name);
28 if (salen < 0)
29 return log_error_errno(salen, "Specified socket path for AF_UNIX socket invalid, refusing: %s", socket_name);
490aed58 30
15a3e96f 31 if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, salen) < 0)
4a62c710 32 return log_error_errno(errno, "Failed to send: %m");
490aed58
LP
33
34 return 0;
35}
36
c7dd2095 37static int run(int argc, char *argv[]) {
e693a932 38 _cleanup_(erase_and_freep) char *packet = NULL;
1602b008 39 _cleanup_close_ int fd = -1;
fd5ad16c 40 size_t length = 0;
1602b008 41 int r;
490aed58 42
6bf3c61c 43 log_setup_service();
490aed58 44
c7dd2095
ZJS
45 if (argc != 3)
46 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Wrong number of arguments.");
490aed58
LP
47
48 if (streq(argv[1], "1")) {
9ae4ef49 49 _cleanup_(erase_and_freep) char *line = NULL;
490aed58 50
1fd27861 51 r = read_line(stdin, LONG_LINE_MAX, &line);
c7dd2095
ZJS
52 if (r < 0)
53 return log_error_errno(r, "Failed to read password: %m");
54 if (r == 0)
55 return log_error_errno(SYNTHETIC_ERRNO(EIO),
56 "Got EOF while reading password.");
1fd27861
LP
57
58 packet = strjoin("+", line);
c7dd2095
ZJS
59 if (!packet)
60 return log_oom();
490aed58 61
1fd27861
LP
62 length = 1 + strlen(line) + 1;
63
490aed58 64 } else if (streq(argv[1], "0")) {
1fd27861 65 packet = strdup("-");
c7dd2095
ZJS
66 if (!packet)
67 return log_oom();
1fd27861 68
490aed58 69 length = 1;
1fd27861 70
c7dd2095
ZJS
71 } else
72 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
73 "Invalid first argument %s", argv[1]);
490aed58 74
03e334a1 75 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
c7dd2095
ZJS
76 if (fd < 0)
77 return log_error_errno(errno, "socket() failed: %m");
490aed58 78
c7dd2095 79 return send_on_socket(fd, argv[2], packet, length);
490aed58 80}
c7dd2095
ZJS
81
82DEFINE_MAIN_FUNCTION(run);