]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/reply-password/reply-password.c
sd-*.h: clean up exported (or to-be-exported) header files
[thirdparty/systemd.git] / src / reply-password / reply-password.c
CommitLineData
490aed58
LP
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
5430f7f2
LP
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
490aed58
LP
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
5430f7f2 16 Lesser General Public License for more details.
490aed58 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
490aed58
LP
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
490aed58 22#include <errno.h>
0e098b15 23#include <stddef.h>
07630cea
LP
24#include <string.h>
25#include <sys/socket.h>
26#include <sys/un.h>
490aed58
LP
27
28#include "log.h"
29#include "macro.h"
07630cea 30#include "string-util.h"
490aed58
LP
31#include "util.h"
32
33static int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) {
34 union {
35 struct sockaddr sa;
36 struct sockaddr_un un;
b92bea5d
ZJS
37 } sa = {
38 .un.sun_family = AF_UNIX,
39 };
490aed58
LP
40
41 assert(fd >= 0);
42 assert(socket_name);
43 assert(packet);
44
ec863ba6 45 strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path));
490aed58 46
4a62c710
MS
47 if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(socket_name)) < 0)
48 return log_error_errno(errno, "Failed to send: %m");
490aed58
LP
49
50 return 0;
51}
52
53int main(int argc, char *argv[]) {
1602b008 54 _cleanup_close_ int fd = -1;
490aed58
LP
55 char packet[LINE_MAX];
56 size_t length;
1602b008 57 int r;
490aed58 58
4cfa2c99 59 log_set_target(LOG_TARGET_AUTO);
490aed58
LP
60 log_parse_environment();
61 log_open();
62
63 if (argc != 3) {
64 log_error("Wrong number of arguments.");
1602b008 65 return EXIT_FAILURE;
490aed58
LP
66 }
67
68 if (streq(argv[1], "1")) {
69
70 packet[0] = '+';
71 if (!fgets(packet+1, sizeof(packet)-1, stdin)) {
1602b008 72 r = log_error_errno(errno, "Failed to read password: %m");
490aed58
LP
73 goto finish;
74 }
75
76 truncate_nl(packet+1);
d55f4f3f 77 length = 1 + strlen(packet+1) + 1;
490aed58
LP
78 } else if (streq(argv[1], "0")) {
79 packet[0] = '-';
80 length = 1;
81 } else {
82 log_error("Invalid first argument %s", argv[1]);
1602b008 83 r = -EINVAL;
490aed58
LP
84 goto finish;
85 }
86
03e334a1
LP
87 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
88 if (fd < 0) {
1602b008 89 r = log_error_errno(errno, "socket() failed: %m");
490aed58
LP
90 goto finish;
91 }
92
1602b008 93 r = send_on_socket(fd, argv[2], packet, length);
490aed58
LP
94
95finish:
1602b008 96 memory_erase(packet, sizeof(packet));
490aed58 97
1602b008 98 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
490aed58 99}