Johannes Weißl
2011-05-11 19:03:40 UTC
it was not working before for me
---
main.c | 27 +++++++++++++++------------
server.c | 44 ++++++++++++++++++++++++++++++++++----------
server.h | 1 +
3 files changed, 50 insertions(+), 22 deletions(-)
diff --git a/main.c b/main.c
index 3f0ea39..0b2c19f 100644
--- a/main.c
+++ b/main.c
@@ -57,7 +57,7 @@ static void gethostbyname_failed(void)
die("gethostbyname: %s\n", error);
}
-static void read_answer(void)
+static int read_answer(void)
{
char buf[8192];
int got_nl = 0;
@@ -68,7 +68,7 @@ static void read_answer(void)
if (rc < 0) {
warn_errno("read");
- return;
+ break;
}
if (!rc)
die("unexpected EOF\n");
@@ -78,27 +78,28 @@ static void read_answer(void)
// Last line should be empty (i.e. read "\n" or "...\n\n").
// Write everything but the last \n to stdout.
if (got_nl && buf[0] == '\n')
- return;
+ break;
if (len == 1 && buf[0] == '\n')
- return;
+ break;
if (rc > 1 && buf[rc - 1] == '\n' && buf[rc - 2] == '\n') {
write_all(1, buf, rc - 1);
- return;
+ break;
}
got_nl = buf[rc - 1] == '\n';
write_all(1, buf, rc);
}
+ return len;
}
-static void write_line(const char *line)
+static int write_line(const char *line)
{
if (write_all(sock, line, strlen(line)) == -1)
die_errno("write");
- read_answer();
+ return read_answer();
}
-static void send_cmd(const char *format, ...)
+static int send_cmd(const char *format, ...)
{
char buf[512];
va_list ap;
@@ -107,10 +108,10 @@ static void send_cmd(const char *format, ...)
vsnprintf(buf, sizeof(buf), format, ap);
va_end(ap);
- write_line(buf);
+ return write_line(buf);
}
-static void remote_connect(const char *address)
+static int remote_connect(const char *address)
{
union {
struct sockaddr sa;
@@ -172,7 +173,8 @@ static void remote_connect(const char *address)
}
if (passwd)
- send_cmd("%s\n", passwd);
+ return send_cmd("passwd %s\n", passwd) == 1;
+ return 1;
}
static char *file_url_absolute(const char *str)
@@ -380,7 +382,8 @@ int main(int argc, char *argv[])
server = server_buf;
}
- remote_connect(server);
+ if (!remote_connect(server))
+ return 1;
if (raw_args) {
while (*argv)
diff --git a/server.c b/server.c
index 2d4e231..48579f7 100644
--- a/server.c
+++ b/server.c
@@ -33,6 +33,7 @@
#include "misc.h"
#include "keyval.h"
+#include <stdarg.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
@@ -127,11 +128,24 @@ static int cmd_status(struct client *client)
return ret;
}
+static ssize_t send_answer(int fd, const char *format, ...)
+{
+ char buf[512];
+ va_list ap;
+
+ va_start(ap, format);
+ vsnprintf(buf, sizeof(buf), format, ap);
+ va_end(ap);
+
+ return write_all(fd, buf, strlen(buf));
+}
+
static void read_commands(struct client *client)
{
char buf[1024];
int pos = 0;
- int authenticated = addr.sa.sa_family == AF_UNIX;
+ if (!client->authenticated)
+ client->authenticated = addr.sa.sa_family == AF_UNIX;
while (1) {
int rc, s, i;
@@ -150,7 +164,7 @@ static void read_commands(struct client *client)
s = 0;
for (i = 0; i < pos; i++) {
- const char *line;
+ const char *line, *msg;
char *cmd, *arg;
int ret;
@@ -161,16 +175,23 @@ static void read_commands(struct client *client)
line = buf + s;
s = i + 1;
- if (!authenticated) {
+ if (!client->authenticated) {
if (!server_password) {
- d_print("password is unset, tcp/ip disabled\n");
+ msg = "password is unset, tcp/ip disabled";
+ d_print("%s\n", msg);
+ ret = send_answer(client->fd, "%s\n\n", msg);
goto close;
}
- authenticated = !strcmp(line, server_password);
- if (!authenticated) {
- d_print("authentication failed\n");
+ if (strncmp(line, "passwd ", 7) == 0)
+ line += 7;
+ client->authenticated = !strcmp(line, server_password);
+ if (!client->authenticated) {
+ msg = "authentication failed";
+ d_print("%s\n", msg);
+ ret = send_answer(client->fd, "%s\n\n", msg);
goto close;
}
+ ret = write_all(client->fd, "\n", 1);
continue;
}
@@ -201,9 +222,11 @@ static void read_commands(struct client *client)
if (!strcmp(cmd, "status")) {
ret = cmd_status(client);
} else {
- set_client_fd(client->fd);
- run_parsed_command(cmd, arg);
- set_client_fd(-1);
+ if (strcmp(cmd, "passwd") != 0) {
+ set_client_fd(client->fd);
+ run_parsed_command(cmd, arg);
+ set_client_fd(-1);
+ }
ret = write_all(client->fd, "\n", 1);
}
free(cmd);
@@ -242,6 +265,7 @@ void server_accept(void)
client = xnew(struct client, 1);
client->fd = fd;
+ client->authenticated = 0;
list_add_tail(&client->node, &client_head);
}
diff --git a/server.h b/server.h
index cd6d900..9f1e55d 100644
--- a/server.h
+++ b/server.h
@@ -24,6 +24,7 @@
struct client {
struct list_head node;
int fd;
+ unsigned int authenticated : 1;
};
extern int server_socket;
---
main.c | 27 +++++++++++++++------------
server.c | 44 ++++++++++++++++++++++++++++++++++----------
server.h | 1 +
3 files changed, 50 insertions(+), 22 deletions(-)
diff --git a/main.c b/main.c
index 3f0ea39..0b2c19f 100644
--- a/main.c
+++ b/main.c
@@ -57,7 +57,7 @@ static void gethostbyname_failed(void)
die("gethostbyname: %s\n", error);
}
-static void read_answer(void)
+static int read_answer(void)
{
char buf[8192];
int got_nl = 0;
@@ -68,7 +68,7 @@ static void read_answer(void)
if (rc < 0) {
warn_errno("read");
- return;
+ break;
}
if (!rc)
die("unexpected EOF\n");
@@ -78,27 +78,28 @@ static void read_answer(void)
// Last line should be empty (i.e. read "\n" or "...\n\n").
// Write everything but the last \n to stdout.
if (got_nl && buf[0] == '\n')
- return;
+ break;
if (len == 1 && buf[0] == '\n')
- return;
+ break;
if (rc > 1 && buf[rc - 1] == '\n' && buf[rc - 2] == '\n') {
write_all(1, buf, rc - 1);
- return;
+ break;
}
got_nl = buf[rc - 1] == '\n';
write_all(1, buf, rc);
}
+ return len;
}
-static void write_line(const char *line)
+static int write_line(const char *line)
{
if (write_all(sock, line, strlen(line)) == -1)
die_errno("write");
- read_answer();
+ return read_answer();
}
-static void send_cmd(const char *format, ...)
+static int send_cmd(const char *format, ...)
{
char buf[512];
va_list ap;
@@ -107,10 +108,10 @@ static void send_cmd(const char *format, ...)
vsnprintf(buf, sizeof(buf), format, ap);
va_end(ap);
- write_line(buf);
+ return write_line(buf);
}
-static void remote_connect(const char *address)
+static int remote_connect(const char *address)
{
union {
struct sockaddr sa;
@@ -172,7 +173,8 @@ static void remote_connect(const char *address)
}
if (passwd)
- send_cmd("%s\n", passwd);
+ return send_cmd("passwd %s\n", passwd) == 1;
+ return 1;
}
static char *file_url_absolute(const char *str)
@@ -380,7 +382,8 @@ int main(int argc, char *argv[])
server = server_buf;
}
- remote_connect(server);
+ if (!remote_connect(server))
+ return 1;
if (raw_args) {
while (*argv)
diff --git a/server.c b/server.c
index 2d4e231..48579f7 100644
--- a/server.c
+++ b/server.c
@@ -33,6 +33,7 @@
#include "misc.h"
#include "keyval.h"
+#include <stdarg.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
@@ -127,11 +128,24 @@ static int cmd_status(struct client *client)
return ret;
}
+static ssize_t send_answer(int fd, const char *format, ...)
+{
+ char buf[512];
+ va_list ap;
+
+ va_start(ap, format);
+ vsnprintf(buf, sizeof(buf), format, ap);
+ va_end(ap);
+
+ return write_all(fd, buf, strlen(buf));
+}
+
static void read_commands(struct client *client)
{
char buf[1024];
int pos = 0;
- int authenticated = addr.sa.sa_family == AF_UNIX;
+ if (!client->authenticated)
+ client->authenticated = addr.sa.sa_family == AF_UNIX;
while (1) {
int rc, s, i;
@@ -150,7 +164,7 @@ static void read_commands(struct client *client)
s = 0;
for (i = 0; i < pos; i++) {
- const char *line;
+ const char *line, *msg;
char *cmd, *arg;
int ret;
@@ -161,16 +175,23 @@ static void read_commands(struct client *client)
line = buf + s;
s = i + 1;
- if (!authenticated) {
+ if (!client->authenticated) {
if (!server_password) {
- d_print("password is unset, tcp/ip disabled\n");
+ msg = "password is unset, tcp/ip disabled";
+ d_print("%s\n", msg);
+ ret = send_answer(client->fd, "%s\n\n", msg);
goto close;
}
- authenticated = !strcmp(line, server_password);
- if (!authenticated) {
- d_print("authentication failed\n");
+ if (strncmp(line, "passwd ", 7) == 0)
+ line += 7;
+ client->authenticated = !strcmp(line, server_password);
+ if (!client->authenticated) {
+ msg = "authentication failed";
+ d_print("%s\n", msg);
+ ret = send_answer(client->fd, "%s\n\n", msg);
goto close;
}
+ ret = write_all(client->fd, "\n", 1);
continue;
}
@@ -201,9 +222,11 @@ static void read_commands(struct client *client)
if (!strcmp(cmd, "status")) {
ret = cmd_status(client);
} else {
- set_client_fd(client->fd);
- run_parsed_command(cmd, arg);
- set_client_fd(-1);
+ if (strcmp(cmd, "passwd") != 0) {
+ set_client_fd(client->fd);
+ run_parsed_command(cmd, arg);
+ set_client_fd(-1);
+ }
ret = write_all(client->fd, "\n", 1);
}
free(cmd);
@@ -242,6 +265,7 @@ void server_accept(void)
client = xnew(struct client, 1);
client->fd = fd;
+ client->authenticated = 0;
list_add_tail(&client->node, &client_head);
}
diff --git a/server.h b/server.h
index cd6d900..9f1e55d 100644
--- a/server.h
+++ b/server.h
@@ -24,6 +24,7 @@
struct client {
struct list_head node;
int fd;
+ unsigned int authenticated : 1;
};
extern int server_socket;
--
1.7.5.1
1.7.5.1