2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2010-09-17 01:26:29 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
2010-10-07 02:34:17 +02:00
|
|
|
#include <stddef.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <sys/un.h>
|
2010-09-17 01:26:29 +02:00
|
|
|
|
2018-10-18 13:40:39 +02:00
|
|
|
#include "alloc-util.h"
|
2019-07-05 11:36:51 +02:00
|
|
|
#include "main-func.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2018-10-18 13:40:39 +02:00
|
|
|
#include "fileio.h"
|
2010-09-17 01:26:29 +02:00
|
|
|
#include "log.h"
|
|
|
|
#include "macro.h"
|
2019-03-14 12:55:37 +01:00
|
|
|
#include "memory-util.h"
|
2016-05-05 22:24:36 +02:00
|
|
|
#include "socket-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2010-09-17 01:26:29 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static int send_on_socket(int fd, const char *socket_name, const void *packet, size_t size) {
|
2018-10-15 13:58:31 +02:00
|
|
|
union sockaddr_union sa = {};
|
|
|
|
int salen;
|
2010-09-17 01:26:29 +02:00
|
|
|
|
|
|
|
assert(fd >= 0);
|
|
|
|
assert(socket_name);
|
|
|
|
assert(packet);
|
|
|
|
|
2018-10-15 13:58:31 +02:00
|
|
|
salen = sockaddr_un_set_path(&sa.un, socket_name);
|
|
|
|
if (salen < 0)
|
|
|
|
return log_error_errno(salen, "Specified socket path for AF_UNIX socket invalid, refusing: %s", socket_name);
|
2010-09-17 01:26:29 +02:00
|
|
|
|
2018-10-15 13:58:31 +02:00
|
|
|
if (sendto(fd, packet, size, MSG_NOSIGNAL, &sa.sa, salen) < 0)
|
2014-11-28 19:57:32 +01:00
|
|
|
return log_error_errno(errno, "Failed to send: %m");
|
2010-09-17 01:26:29 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-05 11:36:51 +02:00
|
|
|
static int run(int argc, char *argv[]) {
|
2019-07-05 11:29:10 +02:00
|
|
|
_cleanup_(erase_and_freep) char *packet = NULL;
|
2015-10-14 22:40:23 +02:00
|
|
|
_cleanup_close_ int fd = -1;
|
2018-10-19 02:37:08 +02:00
|
|
|
size_t length = 0;
|
2015-10-14 22:40:23 +02:00
|
|
|
int r;
|
2010-09-17 01:26:29 +02:00
|
|
|
|
2018-11-20 11:18:22 +01:00
|
|
|
log_setup_service();
|
2010-09-17 01:26:29 +02:00
|
|
|
|
2019-07-05 11:36:51 +02:00
|
|
|
if (argc != 3)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Wrong number of arguments.");
|
2010-09-17 01:26:29 +02:00
|
|
|
|
|
|
|
if (streq(argv[1], "1")) {
|
2019-07-10 13:48:55 +02:00
|
|
|
_cleanup_(erase_and_freep) char *line = NULL;
|
2010-09-17 01:26:29 +02:00
|
|
|
|
2018-10-18 13:40:39 +02:00
|
|
|
r = read_line(stdin, LONG_LINE_MAX, &line);
|
2019-07-05 11:36:51 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to read password: %m");
|
|
|
|
if (r == 0)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EIO),
|
|
|
|
"Got EOF while reading password.");
|
2018-10-18 13:40:39 +02:00
|
|
|
|
|
|
|
packet = strjoin("+", line);
|
2019-07-05 11:36:51 +02:00
|
|
|
if (!packet)
|
|
|
|
return log_oom();
|
2010-09-17 01:26:29 +02:00
|
|
|
|
2018-10-18 13:40:39 +02:00
|
|
|
length = 1 + strlen(line) + 1;
|
|
|
|
|
2010-09-17 01:26:29 +02:00
|
|
|
} else if (streq(argv[1], "0")) {
|
2018-10-18 13:40:39 +02:00
|
|
|
packet = strdup("-");
|
2019-07-05 11:36:51 +02:00
|
|
|
if (!packet)
|
|
|
|
return log_oom();
|
2018-10-18 13:40:39 +02:00
|
|
|
|
2010-09-17 01:26:29 +02:00
|
|
|
length = 1;
|
2018-10-18 13:40:39 +02:00
|
|
|
|
2019-07-05 11:36:51 +02:00
|
|
|
} else
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Invalid first argument %s", argv[1]);
|
2010-09-17 01:26:29 +02:00
|
|
|
|
2014-03-18 19:22:43 +01:00
|
|
|
fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
|
2019-07-05 11:36:51 +02:00
|
|
|
if (fd < 0)
|
|
|
|
return log_error_errno(errno, "socket() failed: %m");
|
2010-09-17 01:26:29 +02:00
|
|
|
|
2019-07-05 11:36:51 +02:00
|
|
|
return send_on_socket(fd, argv[2], packet, length);
|
2010-09-17 01:26:29 +02:00
|
|
|
}
|
2019-07-05 11:36:51 +02:00
|
|
|
|
|
|
|
DEFINE_MAIN_FUNCTION(run);
|