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