]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/reply-password/reply-password.c
Merge pull request #3520 from keszybz/add-release.md
[thirdparty/systemd.git] / src / reply-password / reply-password.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25
26 #include "fd-util.h"
27 #include "log.h"
28 #include "macro.h"
29 #include "socket-util.h"
30 #include "string-util.h"
31 #include "util.h"
32
33 static int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) {
34 union sockaddr_union sa = {
35 .un.sun_family = AF_UNIX,
36 };
37
38 assert(fd >= 0);
39 assert(socket_name);
40 assert(packet);
41
42 strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path));
43
44 if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0)
45 return log_error_errno(errno, "Failed to send: %m");
46
47 return 0;
48 }
49
50 int main(int argc, char *argv[]) {
51 _cleanup_close_ int fd = -1;
52 char packet[LINE_MAX];
53 size_t length;
54 int r;
55
56 log_set_target(LOG_TARGET_AUTO);
57 log_parse_environment();
58 log_open();
59
60 if (argc != 3) {
61 log_error("Wrong number of arguments.");
62 return EXIT_FAILURE;
63 }
64
65 if (streq(argv[1], "1")) {
66
67 packet[0] = '+';
68 if (!fgets(packet+1, sizeof(packet)-1, stdin)) {
69 r = log_error_errno(errno, "Failed to read password: %m");
70 goto finish;
71 }
72
73 truncate_nl(packet+1);
74 length = 1 + strlen(packet+1) + 1;
75 } else if (streq(argv[1], "0")) {
76 packet[0] = '-';
77 length = 1;
78 } else {
79 log_error("Invalid first argument %s", argv[1]);
80 r = -EINVAL;
81 goto finish;
82 }
83
84 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
85 if (fd < 0) {
86 r = log_error_errno(errno, "socket() failed: %m");
87 goto finish;
88 }
89
90 r = send_on_socket(fd, argv[2], packet, length);
91
92 finish:
93 memory_erase(packet, sizeof(packet));
94
95 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
96 }