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