Permalink
Browse files

Rearranged the init system, this is not well tested yet.

  • Loading branch information...
1 parent a7732c9 commit 321e2a9c6c23a782cf922b67d5798f03322103d1 Caleb James DeLisle committed Mar 27, 2012
Showing with 683 additions and 132 deletions.
  1. +35 −23 admin/Admin.c
  2. +3 −1 admin/Admin.h
  3. +96 −0 admin/AuthorizedPasswords.c
  4. +53 −0 admin/AuthorizedPasswords.h
  5. +5 −1 admin/CMakeLists.txt
  6. +263 −0 admin/Configurator.c
  7. +32 −0 admin/Configurator.h
  8. +16 −0 benc/Dict.h
  9. +23 −0 benc/Int.h
  10. +7 −0 benc/String.h
  11. +100 −100 cjdroute.c
  12. +29 −5 crypto/CryptoAuth.c
  13. +14 −2 crypto/CryptoAuth.h
  14. +1 −0 crypto/test/Exports.c
  15. +6 −0 util/Log.h
View
@@ -14,6 +14,7 @@
#include "admin/Admin.h"
#include "benc/String.h"
#include "benc/Dict.h"
+#include "benc/List.h"
#include "benc/serialization/BencSerializer.h"
#include "benc/serialization/standard/StandardBencSerializer.h"
#include "dht/CJDHTConstants.h"
@@ -59,6 +60,9 @@ struct Admin
int functionCount;
struct Allocator* allocator;
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)
@@ -152,6 +156,13 @@ static void handleRequestFromChild(struct Admin* admin,
if (noFunctionsCalled) {
Dict* d = Dict_new(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);
return;
}
@@ -179,6 +190,12 @@ static void inFromChild(evutil_socket_t socket, short eventType, void* vcontext)
return;
}
+ if (!admin->initialized) {
+ admin->initialized = true;
+ event_base_loopbreak(admin->eventBase);
+ return;
+ }
+
struct Allocator* tempAllocator = admin->allocator->child(admin->allocator);
handleRequestFromChild(admin, buffer, amount, tempAllocator);
tempAllocator->free(tempAllocator);
@@ -314,7 +331,10 @@ static void acceptConn(evutil_socket_t socket, short eventType, void* vcontext)
}
// 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 =
event_new(context->eventBase,
@@ -325,27 +345,10 @@ static void child(Dict* config, struct ChildContext* context)
event_add(context->dataFromParent, NULL);
- struct sockaddr_storage addr;
- 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_socket_t listener = socket(addr->ss_family, SOCK_STREAM, 0);
evutil_make_listen_socket_reuseable(listener);
- if (bind(listener, (struct sockaddr*)&addr, addrLen) < 0) {
+ if (bind(listener, (struct sockaddr*) addr, addrLen) < 0) {
perror("bind");
return;
}
@@ -354,6 +357,8 @@ static void child(Dict* config, struct ChildContext* context)
return;
}
+ evutil_make_socket_nonblocking(listener);
+
context->socketEvent =
event_new(context->eventBase, listener, EV_READ | EV_PERSIST, acceptConn, context);
event_add(context->socketEvent, NULL);
@@ -362,6 +367,9 @@ static void child(Dict* config, struct ChildContext* context)
exit(-1);
}
+ // Bump the router process to indicate that we're initialized.
+ write(context->outFd, "ready", strlen("ready"));
+
event_base_dispatch(context->eventBase);
}
@@ -413,7 +421,9 @@ void Admin_sendMessage(Dict* message, String* txid, struct Admin* admin)
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,
struct event_base* eventBase,
struct ExceptionHandler* eh,
@@ -455,7 +465,7 @@ struct Admin* Admin_new(Dict* config,
context.allocator = allocator;
event_reinit(eventBase);
context.eventBase = eventBase;
- child(config, &context);
+ child(addr, addrLen, user, &context);
exit(0);
}
@@ -467,9 +477,11 @@ struct Admin* Admin_new(Dict* config,
admin->allocator = allocator;
admin->functionCount = 0;
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);
event_add(admin->pipeEv, NULL);
+ event_base_dispatch(eventBase);
+
return admin;
}
View
@@ -32,7 +32,9 @@ void Admin_registerFunction(char* name,
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,
struct event_base* eventBase,
struct ExceptionHandler* eh,
@@ -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);
+}
@@ -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
@@ -11,6 +11,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
add_subdirectory(http)
-add_library(cjdadmin Admin.c)
+add_library(cjdadmin
+ Admin.c
+ AuthorizedPasswords.c
+ Configurator.c
+)
target_link_libraries(cjdadmin crypto)
Oops, something went wrong.

0 comments on commit 321e2a9

Please sign in to comment.