stroke: Use stream abstraction to communicate with stroke plugin

Without this changing charon.plugins.stroke.socket would not really
work.
This commit is contained in:
Tobias Brunner
2014-06-19 13:56:37 +02:00
parent b384daafde
commit 906a409608
+23 -43
View File
@@ -1,5 +1,5 @@
/* Stroke for charon is the counterpart to whack from pluto /*
* Copyright (C) 2007-2012 Tobias Brunner * Copyright (C) 2007-2014 Tobias Brunner
* Copyright (C) 2006 Martin Willi * Copyright (C) 2006 Martin Willi
* Hochschule fuer Technik Rapperswil * Hochschule fuer Technik Rapperswil
* *
@@ -15,15 +15,8 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stddef.h>
#include <string.h> #include <string.h>
#include <library.h> #include <library.h>
@@ -56,48 +49,36 @@ static char* push_string(stroke_msg_t *msg, char *string)
static int send_stroke_msg (stroke_msg_t *msg) static int send_stroke_msg (stroke_msg_t *msg)
{ {
struct sockaddr_un ctl_addr; stream_t *stream;
int sock, byte_count; char *uri, buffer[512], *pass;
char buffer[512], *pass; int count;
ctl_addr.sun_family = AF_UNIX;
strcpy(ctl_addr.sun_path, STROKE_SOCKET);
msg->output_verbosity = output_verbosity; msg->output_verbosity = output_verbosity;
sock = socket(AF_UNIX, SOCK_STREAM, 0); uri = lib->settings->get_str(lib->settings, "charon.plugins.stroke.socket",
if (sock < 0) "unix://" STROKE_SOCKET);
stream = lib->streams->connect(lib->streams, uri);
if (!stream)
{ {
fprintf(stderr, "Opening unix socket %s: %s\n", STROKE_SOCKET, strerror(errno)); fprintf(stderr, "failed to connect to stroke socket '%s'\n", uri);
return -1;
}
if (connect(sock, (struct sockaddr *)&ctl_addr,
offsetof(struct sockaddr_un, sun_path) + strlen(ctl_addr.sun_path)) < 0)
{
fprintf(stderr, "Connect to socket failed: %s\n", strerror(errno));
close(sock);
return -1; return -1;
} }
/* send message */ if (!stream->write_all(stream, msg, msg->length))
if (write(sock, msg, msg->length) != msg->length)
{ {
fprintf(stderr, "writing to socket failed: %s\n", strerror(errno)); fprintf(stderr, "sending stroke message failed\n");
close(sock); stream->destroy(stream);
return -1; return -1;
} }
while ((byte_count = read(sock, buffer, sizeof(buffer)-1)) > 0) while ((count = stream->read(stream, buffer, sizeof(buffer)-1, TRUE)) > 0)
{ {
buffer[byte_count] = '\0'; buffer[count] = '\0';
/* we prompt if we receive a magic keyword */ /* we prompt if we receive a magic keyword */
if ((byte_count >= 12 && if ((count >= 12 && streq(buffer + count - 12, "Passphrase:\n")) ||
streq(buffer + byte_count - 12, "Passphrase:\n")) || (count >= 10 && streq(buffer + count - 10, "Password:\n")) ||
(byte_count >= 10 && (count >= 5 && streq(buffer + count - 5, "PIN:\n")))
streq(buffer + byte_count - 10, "Password:\n")) ||
(byte_count >= 5 &&
streq(buffer + byte_count - 5, "PIN:\n")))
{ {
/* remove trailing newline */ /* remove trailing newline */
pass = strrchr(buffer, '\n'); pass = strrchr(buffer, '\n');
@@ -112,8 +93,8 @@ static int send_stroke_msg (stroke_msg_t *msg)
#endif #endif
if (pass) if (pass)
{ {
ignore_result(write(sock, pass, strlen(pass))); stream->write_all(stream, pass, strlen(pass));
ignore_result(write(sock, "\n", 1)); stream->write_all(stream, "\n", 1);
} }
} }
else else
@@ -121,12 +102,11 @@ static int send_stroke_msg (stroke_msg_t *msg)
printf("%s", buffer); printf("%s", buffer);
} }
} }
if (byte_count < 0) if (count < 0)
{ {
fprintf(stderr, "reading from socket failed: %s\n", strerror(errno)); fprintf(stderr, "reading stroke response failed\n");
} }
stream->destroy(stream);
close(sock);
return 0; return 0;
} }