reply-password.c revision 4cfa2c999dea269ddc646bfeba6c7f1021a73843
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt/***
d710d36375bacbf9cfb8903107bedf2c0aada84eMichal Schmidt This file is part of systemd.
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
fff87a35d9e26c0d4ea41273a963c0eb20e18da4Zbigniew Jędrzejewski-Szmek Copyright 2010 Lennart Poettering
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt systemd is free software; you can redistribute it and/or modify it
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt under the terms of the GNU General Public License as published by
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt the Free Software Foundation; either version 2 of the License, or
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt (at your option) any later version.
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt systemd is distributed in the hope that it will be useful, but
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt WITHOUT ANY WARRANTY; without even the implied warranty of
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt General Public License for more details.
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt You should have received a copy of the GNU General Public License
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt along with systemd; If not, see <http://www.gnu.org/licenses/>.
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt***/
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt#include <sys/socket.h>
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt#include <sys/poll.h>
41a5ef639b248aa3d10609d9af5d0043b909d93cHarald Hoyer#include <sys/types.h>
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt#include <assert.h>
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt#include <string.h>
41a5ef639b248aa3d10609d9af5d0043b909d93cHarald Hoyer#include <errno.h>
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt#include <unistd.h>
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt#include <fcntl.h>
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt#include <sys/un.h>
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt#include <sys/stat.h>
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt#include <sys/signalfd.h>
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt#include <getopt.h>
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt#include <stddef.h>
7bcd865d386d96caac83cb1c589fdb8f9ce3b081Zbigniew Jędrzejewski-Szmek
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt#include "log.h"
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt#include "macro.h"
41a5ef639b248aa3d10609d9af5d0043b909d93cHarald Hoyer#include "util.h"
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
41a5ef639b248aa3d10609d9af5d0043b909d93cHarald Hoyerstatic int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) {
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt union {
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt struct sockaddr sa;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt struct sockaddr_un un;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt } sa;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt assert(fd >= 0);
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt assert(socket_name);
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt assert(packet);
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt zero(sa);
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt sa.un.sun_family = AF_UNIX;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path));
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(socket_name)) < 0) {
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt log_error("Failed to send: %m");
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt return -1;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt }
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt return 0;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt}
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidtint main(int argc, char *argv[]) {
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt int fd = -1, r = EXIT_FAILURE;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt char packet[LINE_MAX];
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt size_t length;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt log_set_target(LOG_TARGET_AUTO);
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt log_parse_environment();
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt log_open();
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt if (argc != 3) {
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt log_error("Wrong number of arguments.");
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt goto finish;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt }
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt if (streq(argv[1], "1")) {
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt packet[0] = '+';
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt if (!fgets(packet+1, sizeof(packet)-1, stdin)) {
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt log_error("Failed to read password: %m");
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt goto finish;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt }
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt truncate_nl(packet+1);
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt length = 1 + strlen(packet+1) + 1;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt } else if (streq(argv[1], "0")) {
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt packet[0] = '-';
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt length = 1;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt } else {
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt log_error("Invalid first argument %s", argv[1]);
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt goto finish;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt }
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt log_error("socket() failed: %m");
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt goto finish;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt }
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt if (send_on_socket(fd, argv[2], packet, length) < 0)
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt goto finish;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt r = EXIT_SUCCESS;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidtfinish:
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt if (fd >= 0)
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt close_nointr_nofail(fd);
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt return r;
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt}
b5da077ddf01546c4a85688624d6957766d6c00cMichal Schmidt