Added password primer

This commit is contained in:
James Booth
2015-05-16 03:03:24 +01:00
parent 1e327794e9
commit 50cd28a977
10 changed files with 107 additions and 17 deletions

1
.gitignore vendored
View File

@@ -40,3 +40,4 @@ src/client/stabber.lo
src/server/log.lo
config.cache
configure.lineno
src/server/prime.lo

View File

@@ -6,6 +6,7 @@ sources = \
src/server/parser.c src/server/parser.h \
src/server/stanza.c src/server/stanza.h \
src/server/log.c src/server/log.h \
src/server/prime.c src/server/prime.h \
src/client/stabber.c src/client/stabber.h
libstabber_la_LDFLAGS = -export-symbols-regex '^stbbr_'

View File

@@ -17,6 +17,9 @@ PKG_CHECK_MODULES([glib], [glib-2.0 >= 2.26], [],
PKG_CHECK_MODULES([expat], [expat >= 2.0.0], [],
[AC_MSG_ERROR([expat 2.0.0 or higher is required])])
AC_CHECK_LIB([pthread], [main], [],
[AC_MSG_ERROR([pthread is required])])
AM_CFLAGS="-Wall -Wno-deprecated-declarations"
AM_CFLAGS="$AM_CFLAGS -Wunused -Werror"
AM_CPPFLAGS="$AM_CPPFLAGS $glib_CFLAGS $expat_CFLAGS"

View File

@@ -37,5 +37,5 @@ main(int argc , char *argv[])
port = atoi(argv[1]);
}
return stbbr_start(port);
return stbbr_main(port);
}

View File

@@ -1,7 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "server/server.h"
#include "server/prime.h"
typedef struct server_args_t {
int port;
} StabberArgs;
static void*
_start_server_cb(void *data)
{
StabberArgs *args = (StabberArgs*)data;
server_run(args->port);
return NULL;
}
int
stbbr_main(int port)
{
return server_run(port);
}
int
stbbr_start(int port)
{
return server_run(port);
StabberArgs *args = malloc(sizeof(StabberArgs));
args->port = port;
pthread_t server_thread;
int res = pthread_create(&server_thread, NULL, _start_server_cb, (void*)args);
if (res != 0) {
return 0;
}
pthread_detach(server_thread);
return 1;
}
int
stbbr_auth_passwd(char *password)
{
prime_required_passwd(password);
return 1;
}

15
src/server/prime.c Normal file
View File

@@ -0,0 +1,15 @@
#include <string.h>
static char *required_passwd = NULL;
void
prime_required_passwd(char *password)
{
required_passwd = strdup(password);
}
char *
prime_get_passwd(void)
{
return required_passwd;
}

7
src/server/prime.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef __H_PRIME
#define __H_PRIME
void prime_required_passwd(char *password);
char* prime_get_passwd(void);
#endif

View File

@@ -7,6 +7,7 @@
#include "server/xmppclient.h"
#include "server/parser.h"
#include "server/prime.h"
#include "server/stanza.h"
#include "server/server.h"
#include "server/log.h"
@@ -17,9 +18,15 @@
#define FEATURES "<stream:features></stream:features>"
#define AUTH_RESP "<iq id=\"_xmpp_auth1\" type=\"result\"/>"
#define AUTH_FAIL "<iq id=\"_xmpp_auth1\" type=\"error\"/>"
#define STREAM_END "</stream:stream>"
static XMPPClient *client;
static void _shutdown(void);
static int listen_socket;
int
listen_for_xmlstart(XMPPClient *client)
{
@@ -149,6 +156,12 @@ auth_callback(XMPPStanza *stanza, XMPPClient *client)
client->password = strdup(password->content->str);
client->resource = strdup(resource->content->str);
if (g_strcmp0(client->password, prime_get_passwd()) != 0) {
send_to(client, AUTH_FAIL);
send_to(client, STREAM_END);
exit(0);
}
send_to(client, AUTH_RESP);
log_print("RECV: ");
}
@@ -159,11 +172,13 @@ server_run(int port)
log_init();
log_println("Starting on port: %d...", port);
atexit(_shutdown);
struct sockaddr_in server_addr, client_addr;
// create socket
errno = 0;
int listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); // ipv4, tcp, ip
listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); // ipv4, tcp, ip
if (listen_socket == -1) {
char *errmsg = strerror(errno);
log_println("Could not create socket: %s", errmsg);
@@ -208,7 +223,7 @@ server_run(int port)
return 0;
}
XMPPClient *client = xmppclient_new(client_addr, client_socket);
client = xmppclient_new(client_addr, client_socket);
parser_init(client, stream_start_callback, auth_callback);
log_print("RECV: ");
int res = listen_for_xmlstart(client);
@@ -218,11 +233,14 @@ server_run(int port)
log_print("RECV: ");
listen_to(client);
return 1;
}
static void
_shutdown(void)
{
stanza_show_all();
xmppclient_end_session(client);
parser_close();
while (recv(listen_socket, NULL, 1, 0) > 0) {}
@@ -230,6 +248,4 @@ server_run(int port)
close(listen_socket);
log_close();
return 1;
}

View File

@@ -26,13 +26,16 @@ xmppclient_new(struct sockaddr_in client_addr, int socket)
void
xmppclient_end_session(XMPPClient *client)
{
while (recv(client->sock, NULL, 1, 0) > 0) {}
shutdown(client->sock, 2);
close(client->sock);
free(client->ip);
free(client->username);
free(client->password);
free(client->resource);
free(client);
if (client) {
if (client->sock) {
while (recv(client->sock, NULL, 1, 0) > 0) {}
shutdown(client->sock, 2);
close(client->sock);
}
free(client->ip);
free(client->username);
free(client->password);
free(client->resource);
free(client);
}
}

View File

@@ -1,6 +1,9 @@
#ifndef __H_STABBER
#define __H_STABBER
int stbbr_main(int port);
int stbbr_start(int port);
int stbbr_auth_passwd(char *password);
#endif