]> git.proxmox.com Git - systemd.git/blame - src/reply-password/reply-password.c
Imported Upstream version 218
[systemd.git] / src / reply-password / reply-password.c
CommitLineData
663996b3
MS
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
40static 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
f47781d8
MP
54 if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(socket_name)) < 0)
55 return log_error_errno(errno, "Failed to send: %m");
663996b3
MS
56
57 return 0;
58}
59
60int main(int argc, char *argv[]) {
61 int fd = -1, r = EXIT_FAILURE;
62 char packet[LINE_MAX];
63 size_t length;
64
65 log_set_target(LOG_TARGET_AUTO);
66 log_parse_environment();
67 log_open();
68
69 if (argc != 3) {
70 log_error("Wrong number of arguments.");
71 goto finish;
72 }
73
74 if (streq(argv[1], "1")) {
75
76 packet[0] = '+';
77 if (!fgets(packet+1, sizeof(packet)-1, stdin)) {
f47781d8 78 log_error_errno(errno, "Failed to read password: %m");
663996b3
MS
79 goto finish;
80 }
81
82 truncate_nl(packet+1);
83 length = 1 + strlen(packet+1) + 1;
84 } else if (streq(argv[1], "0")) {
85 packet[0] = '-';
86 length = 1;
87 } else {
88 log_error("Invalid first argument %s", argv[1]);
89 goto finish;
90 }
91
60f067b4
JS
92 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
93 if (fd < 0) {
f47781d8 94 log_error_errno(errno, "socket() failed: %m");
663996b3
MS
95 goto finish;
96 }
97
98 if (send_on_socket(fd, argv[2], packet, length) < 0)
99 goto finish;
100
101 r = EXIT_SUCCESS;
102
103finish:
60f067b4 104 safe_close(fd);
663996b3
MS
105
106 return r;
107}