Skip to content

Commit

Permalink
Rearranged the init system, this is not well tested yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
Caleb James DeLisle committed Mar 27, 2012
1 parent a7732c9 commit 321e2a9
Show file tree
Hide file tree
Showing 15 changed files with 683 additions and 132 deletions.
58 changes: 35 additions & 23 deletions admin/Admin.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "admin/Admin.h" #include "admin/Admin.h"
#include "benc/String.h" #include "benc/String.h"
#include "benc/Dict.h" #include "benc/Dict.h"
#include "benc/List.h"
#include "benc/serialization/BencSerializer.h" #include "benc/serialization/BencSerializer.h"
#include "benc/serialization/standard/StandardBencSerializer.h" #include "benc/serialization/standard/StandardBencSerializer.h"
#include "dht/CJDHTConstants.h" #include "dht/CJDHTConstants.h"
Expand Down Expand Up @@ -59,6 +60,9 @@ struct Admin
int functionCount; int functionCount;
struct Allocator* allocator; struct Allocator* allocator;
String* password; String* password;

/** Becomes true after the admin process has sent it's first message. */
bool initialized;
}; };


static inline bool authValid(Dict* message, uint8_t* buffer, uint32_t length, struct Admin* admin) static inline bool authValid(Dict* message, uint8_t* buffer, uint32_t length, struct Admin* admin)
Expand Down Expand Up @@ -152,6 +156,13 @@ static void handleRequestFromChild(struct Admin* admin,
if (noFunctionsCalled) { if (noFunctionsCalled) {
Dict* d = Dict_new(allocator); Dict* d = Dict_new(allocator);
Dict_putString(d, BSTR("error"), BSTR("No functions matched your request."), allocator); Dict_putString(d, BSTR("error"), BSTR("No functions matched your request."), allocator);
List* functions = NULL;
for (int i = 0; i < admin->functionCount; i++) {
functions = List_addString(functions, admin->functions[i].name, allocator);
}
if (functions) {
Dict_putList(d, BSTR("availableFunctions"), functions, allocator);
}
Admin_sendMessage(d, txid, admin); Admin_sendMessage(d, txid, admin);
return; return;
} }
Expand Down Expand Up @@ -179,6 +190,12 @@ static void inFromChild(evutil_socket_t socket, short eventType, void* vcontext)
return; return;
} }


if (!admin->initialized) {
admin->initialized = true;
event_base_loopbreak(admin->eventBase);
return;
}

struct Allocator* tempAllocator = admin->allocator->child(admin->allocator); struct Allocator* tempAllocator = admin->allocator->child(admin->allocator);
handleRequestFromChild(admin, buffer, amount, tempAllocator); handleRequestFromChild(admin, buffer, amount, tempAllocator);
tempAllocator->free(tempAllocator); tempAllocator->free(tempAllocator);
Expand Down Expand Up @@ -314,7 +331,10 @@ static void acceptConn(evutil_socket_t socket, short eventType, void* vcontext)
} }


// only in child // only in child
static void child(Dict* config, struct ChildContext* context) static void child(struct sockaddr_storage* addr,
int addrLen,
char* user,
struct ChildContext* context)
{ {
context->dataFromParent = context->dataFromParent =
event_new(context->eventBase, event_new(context->eventBase,
Expand All @@ -325,27 +345,10 @@ static void child(Dict* config, struct ChildContext* context)


event_add(context->dataFromParent, NULL); event_add(context->dataFromParent, NULL);


struct sockaddr_storage addr; evutil_socket_t listener = socket(addr->ss_family, SOCK_STREAM, 0);
int addrLen = sizeof(struct sockaddr_storage);
char* bindTo = "127.0.0.1:9999";
String* bindStr = Dict_getString(config, BSTR("bind"));
if (bindStr) {
fprintf(stderr, "Admin: Binding to %s\n", bindStr->bytes);
if (evutil_parse_sockaddr_port(bindStr->bytes, (struct sockaddr*) &addr, &addrLen)) {
fprintf(stderr, "Admin: admin.bind parse failed, calling back on %s\n", bindTo);
bindStr = NULL;
}
}
if (!bindStr) {
fprintf(stderr, "Admin: Binding to %s\n", bindTo);
evutil_parse_sockaddr_port(bindTo, (struct sockaddr*) &addr, &addrLen);
}

evutil_socket_t listener = socket(addr.ss_family, SOCK_STREAM, 0);
evutil_make_socket_nonblocking(listener);
evutil_make_listen_socket_reuseable(listener); evutil_make_listen_socket_reuseable(listener);


if (bind(listener, (struct sockaddr*)&addr, addrLen) < 0) { if (bind(listener, (struct sockaddr*) addr, addrLen) < 0) {
perror("bind"); perror("bind");
return; return;
} }
Expand All @@ -354,6 +357,8 @@ static void child(Dict* config, struct ChildContext* context)
return; return;
} }


evutil_make_socket_nonblocking(listener);

context->socketEvent = context->socketEvent =
event_new(context->eventBase, listener, EV_READ | EV_PERSIST, acceptConn, context); event_new(context->eventBase, listener, EV_READ | EV_PERSIST, acceptConn, context);
event_add(context->socketEvent, NULL); event_add(context->socketEvent, NULL);
Expand All @@ -362,6 +367,9 @@ static void child(Dict* config, struct ChildContext* context)
exit(-1); exit(-1);
} }


// Bump the router process to indicate that we're initialized.
write(context->outFd, "ready", strlen("ready"));

event_base_dispatch(context->eventBase); event_base_dispatch(context->eventBase);
} }


Expand Down Expand Up @@ -413,7 +421,9 @@ void Admin_sendMessage(Dict* message, String* txid, struct Admin* admin)
write(admin->outFd, buff, written); write(admin->outFd, buff, written);
} }


struct Admin* Admin_new(Dict* config, struct Admin* Admin_new(struct sockaddr_storage* addr,
int addrLen,
String* password,
char* user, char* user,
struct event_base* eventBase, struct event_base* eventBase,
struct ExceptionHandler* eh, struct ExceptionHandler* eh,
Expand Down Expand Up @@ -455,7 +465,7 @@ struct Admin* Admin_new(Dict* config,
context.allocator = allocator; context.allocator = allocator;
event_reinit(eventBase); event_reinit(eventBase);
context.eventBase = eventBase; context.eventBase = eventBase;
child(config, &context); child(addr, addrLen, user, &context);
exit(0); exit(0);
} }


Expand All @@ -467,9 +477,11 @@ struct Admin* Admin_new(Dict* config,
admin->allocator = allocator; admin->allocator = allocator;
admin->functionCount = 0; admin->functionCount = 0;
admin->eventBase = eventBase; admin->eventBase = eventBase;
admin->password = Dict_getString(config, BSTR("password")); admin->password = password;
admin->pipeEv = event_new(eventBase, inFd, EV_READ | EV_PERSIST, inFromChild, admin); admin->pipeEv = event_new(eventBase, inFd, EV_READ | EV_PERSIST, inFromChild, admin);
event_add(admin->pipeEv, NULL); event_add(admin->pipeEv, NULL);


event_base_dispatch(eventBase);

return admin; return admin;
} }
4 changes: 3 additions & 1 deletion admin/Admin.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ void Admin_registerFunction(char* name,


void Admin_sendMessage(Dict* message, String* txid, struct Admin* admin); void Admin_sendMessage(Dict* message, String* txid, struct Admin* admin);


struct Admin* Admin_new(Dict* config, struct Admin* Admin_new(struct sockaddr_storage* addr,
int addrLen,
String* password,
char* user, char* user,
struct event_base* eventBase, struct event_base* eventBase,
struct ExceptionHandler* eh, struct ExceptionHandler* eh,
Expand Down
96 changes: 96 additions & 0 deletions admin/AuthorizedPasswords.c
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "admin/AuthorizedPasswords.h"
#include "memory/BufferAllocator.h"

struct Context
{
struct Admin* admin;
struct CryptoAuth* ca;
struct Allocator* allocator;
};

struct User
{
uint64_t trust;
};

static void sendResponse(String* msg, struct Admin* admin, String* txid)
{
#define BUFFERSZ 1024
uint8_t buffer[BUFFERSZ];
struct Allocator* alloc = BufferAllocator_new(buffer, BUFFERSZ);
Dict* output = Dict_new(alloc);
Dict_putString(output, String_CONST("error"), msg, alloc);
Admin_sendMessage(output, txid, admin);
}

static void add(Dict* ap, void* vcontext, String* txid)
{
struct Context* context = (struct Context*) vcontext;

String* passwd = Dict_getString(ap, String_CONST("password"));
int64_t* authType = Dict_getInt(ap, String_CONST("authType"));

String* msg = NULL;
if (!(passwd && authType)) {
msg = String_CONST("Must specify authType, and password.");
} else if (*authType < 1 || *authType > 255) {
msg = String_CONST("Auth must be between 1 and 255 inclusive.");
} else {
struct User* u = context->allocator->malloc(sizeof(struct User), context->allocator);
// At some point this will be implemented...
u->trust = 0;
int32_t ret = CryptoAuth_addUser(passwd, *authType, u, context->ca);

switch (ret) {
case 0:
msg = String_CONST("none");
break;
case CryptoAuth_addUser_INVALID_AUTHTYPE:
msg = String_CONST("Specified auth type is not supported.");
break;
case CryptoAuth_addUser_OUT_OF_SPACE:
msg = String_CONST("Out of memory to store password.");
break;
case CryptoAuth_addUser_DUPLICATE:
msg = String_CONST("Password already added.");
break;
default:
msg = String_CONST("Unknown error.");
};
}

sendResponse(msg, context->admin, txid);
}

static void flush(Dict* ap, void* vcontext, String* txid)
{
struct Context* context = (struct Context*) vcontext;
CryptoAuth_flushUsers(context->ca);
sendResponse(String_CONST("none"), context->admin, txid);
}

void AuthorizedPasswords_init(struct Admin* admin,
struct CryptoAuth* ca,
struct Allocator* allocator)
{
struct Context* context = allocator->malloc(sizeof(struct Context), allocator);
context->admin = admin;
context->allocator = allocator;
context->ca = ca;
Admin_registerFunction("AuthorizedPasswords_add", add, context, true, admin);
Admin_registerFunction("AuthorizedPasswords_flush", flush, context, true, admin);
}
53 changes: 53 additions & 0 deletions admin/AuthorizedPasswords.h
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef AuthorizedPasswords_H
#define AuthorizedPasswords_H

#include "admin/Admin.h"
#include "crypto/CryptoAuth.h"
#include "memory/Allocator.h"

/**
* Init the AuthorizedPasswords admin function.
* This function exports the following calls,
* AuthorizedPasswords_add() --- Add an authorized password routers to connect.
* - requires a password.
* - inputs
* password: (String, mandatory)
* The password to authorize.
* authType: (Integer, mandatory)
* The way in which nodes will be allowed to authenticate with
* the password.
*
* - outputs
* error: (String)
* A message explaining what went wrong, if everything went ok,
* the error will be "none".
*
* AuthorizedPasswords_flush() --- Remove all authorized passwords.
* - requires a password
* - inputs (none)
* - outputs
* error: (String)
* always "none".
*
* @param admin the admin object.
* @param ca the cryptoauth to add users to.
* @param allocator a persistent memory allocator.
*/
void AuthorizedPasswords_init(struct Admin* admin,
struct CryptoAuth* ca,
struct Allocator* allocator);

#endif
6 changes: 5 additions & 1 deletion admin/CMakeLists.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
add_subdirectory(http) add_subdirectory(http)


add_library(cjdadmin Admin.c) add_library(cjdadmin
Admin.c
AuthorizedPasswords.c
Configurator.c
)


target_link_libraries(cjdadmin crypto) target_link_libraries(cjdadmin crypto)
Loading

0 comments on commit 321e2a9

Please sign in to comment.