]> git.proxmox.com Git - systemd.git/blob - src/reply-password/reply-password.c
Merge tag 'upstream/229'
[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 "string-util.h"
30 #include "util.h"
31
32 static int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) {
33 union {
34 struct sockaddr sa;
35 struct sockaddr_un un;
36 } sa = {
37 .un.sun_family = AF_UNIX,
38 };
39
40 assert(fd >= 0);
41 assert(socket_name);
42 assert(packet);
43
44 strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path));
45
46 if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(socket_name)) < 0)
47 return log_error_errno(errno, "Failed to send: %m");
48
49 return 0;
50 }
51
52 int main(int argc, char *argv[]) {
53 _cleanup_close_ int fd = -1;
54 char packet[LINE_MAX];
55 size_t length;
56 int r;
57
58 log_set_target(LOG_TARGET_AUTO);
59 log_parse_environment();
60 log_open();
61
62 if (argc != 3) {
63 log_error("Wrong number of arguments.");
64 return EXIT_FAILURE;
65 }
66
67 if (streq(argv[1], "1")) {
68
69 packet[0] = '+';
70 if (!fgets(packet+1, sizeof(packet)-1, stdin)) {
71 r = log_error_errno(errno, "Failed to read password: %m");
72 goto finish;
73 }
74
75 truncate_nl(packet+1);
76 length = 1 + strlen(packet+1) + 1;
77 } else if (streq(argv[1], "0")) {
78 packet[0] = '-';
79 length = 1;
80 } else {
81 log_error("Invalid first argument %s", argv[1]);
82 r = -EINVAL;
83 goto finish;
84 }
85
86 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
87 if (fd < 0) {
88 r = log_error_errno(errno, "socket() failed: %m");
89 goto finish;
90 }
91
92 r = send_on_socket(fd, argv[2], packet, length);
93
94 finish:
95 memory_erase(packet, sizeof(packet));
96
97 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
98 }