From b71e2c21fa000761fa80d66597f880496d9f45f2 Mon Sep 17 00:00:00 2001 From: pk910 Date: Thu, 24 Jul 2014 15:04:10 +0200 Subject: [PATCH] added some code --- src/cmd_user.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++ src/ircd_auth.c | 54 +++++++++++++++++++++++++++++++++++++---------- src/ircd_auth.h | 1 + src/ircd_client.c | 20 +++++++++--------- src/ircd_client.h | 12 +++++------ src/ircd_parse.c | 36 ++++++++++++------------------- src/ircd_sock.c | 38 +++++++++++++++++++++++++++------ src/ircd_sock.h | 3 +++ src/ircd_users.c | 9 ++++---- src/ircd_users.h | 23 ++++++++++++++++++++ 10 files changed, 188 insertions(+), 61 deletions(-) create mode 100644 src/cmd_user.c create mode 100644 src/ircd_users.h diff --git a/src/cmd_user.c b/src/cmd_user.c new file mode 100644 index 0000000..ea06a08 --- /dev/null +++ b/src/cmd_user.c @@ -0,0 +1,53 @@ +/* cmd_user.c - NextIRCd + * Copyright (C) 2012-2013 Philipp Kreil (pk910) + * + * This program is free software: you can redistribute it 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 . + */ + +#include + +#include "cmd.h" +#include "struct_auth.h" +#include "ircd_users.h" +#include "ircd_auth.h" + +int cmd_user_cli(struct Client *client, char *argv[], int argc) { + + return 0; +} + +int cmd_user_auth(struct Auth *auth, char *argv[], int argc) { + char *user = argv[0]; + char *mode = argv[1]; + char *realname = argv[3]; + + char *hostname; + if((hostname = strchr(user, '@'))) { + *hostname = '\0'; + if(user == hostname) + user = "NoUser"; + } + if(!is_user_valid(user)) { + // invalid user + return 0; + } + + strncpy(auth->ident, user, USERLEN); + strncpy(auth->realname, realname, REALLEN); + auth->have_user = 1; + + auth_try_finish(auth); + + return 0; +} diff --git a/src/ircd_auth.c b/src/ircd_auth.c index c2ec728..56f4481 100644 --- a/src/ircd_auth.c +++ b/src/ircd_auth.c @@ -14,25 +14,22 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - -#include - #include "struct_auth.h" #include "struct_connection.h" -#include "ircd_auth.h" #include "ircd_client.h" #include "IOHandler/IOSockets.h" #include "IOHandler/IODNSLookup.h" #include "version.h" static IODNS_CALLBACK(auth_dns_callback); +static void auth_free(struct Auth *auth); static struct Auth *authlist_first = NULL; static struct Auth *authlist_last = NULL; -struct Auth *auth_new(struct Connection *conn) { +struct Auth *auth_new(struct Connection *conn) { struct Auth *auth = calloc(1, sizeof(*auth)); - client_printf(conn, "NOTICE AUTH :*** NextIRCd v%d.%d (%s)", VERSION_NUMBER, patchlevel, revision); + socket_printf(conn, "NOTICE AUTH :*** NextIRCd v%d.%d (%s)", VERSION_NUMBER, patchlevel, revision); auth->conn = conn; time(&auth->startup_time); @@ -40,14 +37,16 @@ struct Auth *auth_new(struct Connection *conn) { auth->prev = authlist_last; auth->next = NULL; if(!authlist_last) - authlist_first = auth; + authlist_first = auth authlist_last = auth; + auth_start_dnsreverse(auth); + return auth; } void auth_start_dnsreverse(struct Auth *auth) { - client_printf(auth->conn, "NOTICE AUTH :*** Looking up your hostname"); + socket_printf(auth->conn, "NOTICE AUTH :*** Looking up your hostname"); struct IODNSAddress *sockaddr; sockaddr = iosocket_get_remote_addr(auth->conn->socket); @@ -62,13 +61,21 @@ static IODNS_CALLBACK(auth_dns_callback) { struct Auth *auth = event->query->data; struct IODNSResult *dnsresult = event->result; + auth->dnslookup = NULL; + if(event->type == IODNSEVENT_SUCCESS) { strncpy(auth->host, dnsresult->result.host, HOSTLEN); - client_printf(auth->conn, "NOTICE AUTH :*** Found your hostname (%s)", auth->host); + socket_printf(auth->conn, "NOTICE AUTH :*** Found your hostname (%s)", auth->host); } else { struct IODNSAddress *sockaddr = iosocket_get_remote_addr(auth->conn->socket); - iodns_print_address(sockaddr, auth->conn->socket->ipv6, auth->host, HOSTLEN); - client_printf(auth->conn, "NOTICE AUTH :*** Couldn't look up your hostname. Using your IP instead (%s)", auth->host); + if(sockaddr->addresslen == sizeof(struct sockaddr_in)) { + //ipv4 + inet_ntop(AF_INET, (void *)(&((struct sockaddr_in *)sockaddr->address)->sin_addr), auth->host, HOSTLEN); + } else { + //ipv6 + inet_ntop(AF_INET6, (void *)(&((struct sockaddr_in6 *)sockaddr->address)->sin6_addr), auth->host, HOSTLEN); + } + socket_printf(auth->conn, "NOTICE AUTH :*** Couldn't look up your hostname. Using your IP instead (%s)", auth->host); } if(dnsresult) iodns_free_result(dnsresult); @@ -78,5 +85,30 @@ static IODNS_CALLBACK(auth_dns_callback) { void auth_try_finish(struct Auth *auth) { + if(auth->server) { + + } else { + if(!auth->have_nick || !auth->have_user) + return; + if(!auth->sent_ping) { + + auth->sent_ping = 1; + } else if(auth->have_pong && auth->have_dnsresolv) { + struct Client *client = client_connected(auth); + auth->conn->authed = 1; + auth->conn->data.client = client; + auth_free(auth); + } + } +} + +void auth_abort(struct Auth *auth) { + +} +static void auth_free(struct Auth *auth) { + if(auth->dnslookup) { + + } + } diff --git a/src/ircd_auth.h b/src/ircd_auth.h index fde02c4..f200e5d 100644 --- a/src/ircd_auth.h +++ b/src/ircd_auth.h @@ -21,5 +21,6 @@ struct Auth *auth_new(struct Connection *conn); void auth_start_dnsreverse(struct Auth *auth); void auth_try_finish(struct Auth *auth); +void auth_abort(struct Auth *auth); #endif diff --git a/src/ircd_client.c b/src/ircd_client.c index 6fddf09..088d4c4 100644 --- a/src/ircd_client.c +++ b/src/ircd_client.c @@ -20,14 +20,17 @@ #include "struct_connection.h" #include "struct_auth.h" #include "ircd_config.h" -#include "ircd_auth.h" #include #include #include #define CLIENT_MAXLEN 512 -void client_printf(struct Connection *conn, const char *text, ...) { +void client_connected(struct Auth *auth) { + +} + +void client_printf(struct Client *client, const char *text, ...) { va_list arg_list; char sendBuf[CLIENT_MAXLEN]; int pos; @@ -38,15 +41,12 @@ void client_printf(struct Connection *conn, const char *text, ...) { if (pos < 0 || pos > (CLIENT_MAXLEN - 2)) pos = CLIENT_MAXLEN - 2; sendBuf[pos] = '\n'; sendBuf[pos+1] = '\0'; - socket_send(conn, sendBuf, pos+1); + socket_send(client->conn, sendBuf, pos+1); } -void client_connected(struct Connection *conn) { - struct Auth *auth = auth_new(conn); +void client_exit(struct Client *client, char *reason) { + if(client->conn) { + + } - auth_start_dnsreverse(auth); -} - -void client_disconnected(struct Connection *conn) { - } diff --git a/src/ircd_client.h b/src/ircd_client.h index c4a68a0..4a726af 100644 --- a/src/ircd_client.h +++ b/src/ircd_client.h @@ -18,13 +18,11 @@ #ifndef _ircd_client_h #define _ircd_client_h -struct Connection; +struct Auth; +struct Client; -/* -- called from ircd_sock.c */ -void client_connected(struct Connection *conn); -void client_disconnected(struct Connection *conn); -/* -- */ - -void client_printf(struct Connection *conn, const char *text, ...); +struct Client *client_connected(struct Auth *auth); +void client_printf(struct Client *client, const char *text, ...); +void client_exit(struct Client *client, char *reason); #endif diff --git a/src/ircd_parse.c b/src/ircd_parse.c index 1bfa8b7..12dc6de 100644 --- a/src/ircd_parse.c +++ b/src/ircd_parse.c @@ -15,16 +15,8 @@ * along with this program. If not, see . */ -#include -#include - #include "tools.h" #include "IOHandler/IOSockets.h" -#include "ircd_parse.h" -struct Server; -#include "struct_client.h" -#include "struct_auth.h" -#include "struct_servermsg.h" typedef int cmd_client_t(struct Client *client, char *argv[], int argc); typedef int cmd_auth_t(struct Auth *auth, char *argv[], int argc); @@ -35,7 +27,7 @@ static struct ServerMsg parse_static_srvmsg; #define PARSE_CLIFLAG_OPONLY 0x01 //include all commands -#include "cmd_nick.h" +#include "cmd_ping.h" struct { @@ -62,7 +54,7 @@ struct { unsigned int flags : 8; } server; -} parse_command_list[] = { +} parse_command_list { {{"PING", "P"}, /* Ping Command */ {NULL, 0, 1, 0}, /* Client */ {NULL, 0, 1, 0}, /* Unauthed */ @@ -109,18 +101,18 @@ static char **parse_irc_params(char *data, int *argc, int maxargs) { char **argv = calloc(maxargs, sizeof(*argv)); while(*data) { //skip leading spaces - while (*data == ' ') - *data++ = 0; - if (*data == ':') { + while (*line == ' ') + *line++ = 0; + if (*line == ':') { //the rest is a single parameter - argv[*argc++] = data + 1; + argv[*argc++] = line + 1; break; } - argv[*argc++] = data; - if (*argc >= maxargs) + argv[*argc++] = line; + if (argc >= maxargs) break; - while (*data != ' ' && *data) - data++; + while (*line != ' ' && *line) + line++; } return argv; } @@ -245,7 +237,7 @@ void parse_server_data(struct Server *server, struct IOSocketBuffer *buffer) { srvmsg->destinations = malloc(sizeof(struct ServerMsgDstMap)); srvmsg->destinations->dstcount = 1; srvmsg->destinations->dst[0].destination.srvnum = ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]); - srvmsg->destinations->dst[0].resolved_destination = 0; + srvmsg->destinations->dst[0].destination.resolved_destination = 0; break; case SERVERMSG_TYPE_MULTICAST: if(buflen < 5) { @@ -263,7 +255,7 @@ void parse_server_data(struct Server *server, struct IOSocketBuffer *buffer) { srvmsg->destinations->dstcount = srvcount; for(i = 0; i < srvcount; i++) { srvmsg->destinations->dst[i].destination.srvnum = ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]); - srvmsg->destinations->dst[i].resolved_destination = 0; + srvmsg->destinations->dst[i].destination.resolved_destination = 0; buf += 4; buflen -= 4; } @@ -283,7 +275,7 @@ void parse_server_data(struct Server *server, struct IOSocketBuffer *buffer) { goto parse_server_data_finish; } - srvmsg->arglen = srvmsg->msglen - (buf - (unsigned char *)buffer->buffer); + srvmsg->arglen = srvmsg->msglen - (buf - buffer->buffer); srvmsg->args = buf; int found = 0; @@ -307,7 +299,7 @@ void parse_server_data(struct Server *server, struct IOSocketBuffer *buffer) { memmove(buffer->buffer, buffer->buffer + srvmsg->msglen, srvmsg->msglen - buffer->bufpos); buffer->bufpos -= srvmsg->msglen; } else - buffer->bufpos = 0; + srvmsg->bufpos = 0; } } diff --git a/src/ircd_sock.c b/src/ircd_sock.c index 5e75008..a61686f 100644 --- a/src/ircd_sock.c +++ b/src/ircd_sock.c @@ -19,6 +19,7 @@ #include "ircd_sock.h" #include "ircd_client.h" #include "ircd_parse.h" +#include "ircd_auth.h" #include "struct_connection.h" #include "IOHandler/IOSockets.h" @@ -135,7 +136,7 @@ static void sockets_accept_client(struct IOSocket *new_client, struct Connection new_client->data = connection; sockets_append_list(connection); - client_connected(connection); + auth_new(connection); } static CONFRELOAD_CALLBACK(sockets_config_reload) { @@ -180,9 +181,13 @@ static IOSOCKET_CALLBACK(sockets_iohandler_callback) { sockets_remove_list(connection); if(connection->server) { + } else if(connection->authed) { + connection->data.client->conn = NULL; + client_exit(connection->data.client, "Client disconnected."); } else { - client_disconnected(connection); - } + connection->data.auth->conn = NULL; + auth_abort(connection->data.auth); + } sockets_free_connection(connection); break; @@ -197,12 +202,12 @@ static IOSOCKET_CALLBACK(sockets_iohandler_callback) { break; case IOSOCKETEVENT_RECV: - if(!connection->authed) - parse_unauth_data(connection->data.auth, event->data.recv_str); - else if(connection->server) + if(connection->server) parse_server_data(connection->data.server, event->data.recv_buf); - else + else if(connection->authed) parse_client_data(connection->data.client, event->data.recv_str); + else + parse_unauth_data(connection->data.auth, event->data.recv_str); break; default: @@ -214,6 +219,25 @@ void socket_send(struct Connection *conn, char *data, int len) { iosocket_send(conn->socket, data, len); } +void socket_printf(struct Connection *conn, const char *text, ...) { + va_list arg_list; + char sendBuf[512]; + int pos; + sendBuf[0] = '\0'; + va_start(arg_list, text); + pos = vsnprintf(sendBuf, 512 - 2, text, arg_list); + va_end(arg_list); + if (pos < 0 || pos > (512 - 2)) pos = CLIENT_MAXLEN - 2; + sendBuf[pos] = '\n'; + sendBuf[pos+1] = '\0'; + iosocket_send(conn->socket, sendBuf, pos+1); +} + +void socket_close(struct Connection *conn) { + iosocket_close(conn->socket); + sockets_free_connection(conn); +} + void socket_set_server(struct Connection *conn) { struct IOSocket *iosock = conn->socket; iosock->parse_delimiter = 0; diff --git a/src/ircd_sock.h b/src/ircd_sock.h index d0f52f4..78806d6 100644 --- a/src/ircd_sock.h +++ b/src/ircd_sock.h @@ -23,6 +23,9 @@ void init_sockets(); struct Connection; +void socket_close(struct Connection *conn); void socket_send(struct Connection *conn, char *data, int len); +void socket_printf(struct Connection *conn, const char *text, ...); +void socket_close(struct Connection *conn); #endif diff --git a/src/ircd_users.c b/src/ircd_users.c index 8e5b39f..9faaacd 100644 --- a/src/ircd_users.c +++ b/src/ircd_users.c @@ -14,9 +14,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - -#include - #include "struct_user.h" struct UserDictEntry { @@ -29,9 +26,13 @@ struct UserDict { struct UserDictEntry *users[65536]; // 2^16 (first 16 bit of nickhash as identifier) }; - void find_user_by_nick(char *nick) { //calculate checksum //search in UserDict //ask trusted server multicast } + +int is_nick_valid(char *nick) { + +} + diff --git a/src/ircd_users.h b/src/ircd_users.h new file mode 100644 index 0000000..a45a176 --- /dev/null +++ b/src/ircd_users.h @@ -0,0 +1,23 @@ +/* ircd_users.h - NextIRCd + * Copyright (C) 2012-2013 Philipp Kreil (pk910) + * + * This program is free software: you can redistribute it 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 . + */ + +#ifndef _ircd_users_h +#define _ircd_users_h + +int is_nick_valid(char *nick); + +#endif -- 2.20.1