From: Egor Date: Tue, 3 May 2022 08:07:16 +0000 (+0300) Subject: use dynamically allocated nick X-Git-Url: https://git.patchysicp.xyz/?a=commitdiff_plain;h=6292c408e2317006de369e483ad106887e127416;p=patchyircd.git use dynamically allocated nick --- diff --git a/common.h b/common.h index 5038e7a..8ab86e4 100644 --- a/common.h +++ b/common.h @@ -12,9 +12,8 @@ struct client_data { SOCKET FD; char type; - char nick[32]; - size_t nickLen; - char *key; // case-mapped nick + char *nick; + char *key; // case-mapped nick char buf[2048]; size_t bufLen; size_t bufCap; diff --git a/irc.c b/irc.c index 20b8490..78037fe 100644 --- a/irc.c +++ b/irc.c @@ -86,8 +86,9 @@ int processPacket(struct client_data *client) { // User now registered if (!args[0]) // FIXME: error message return 0; - setNick(client, args[0], 32); - printf("USER SET NICKNAME %s\n", client->nick); + setNick(client, args[0]); + if (client->nick) + printf("USER SET NICKNAME %s\n", client->nick); fflush(stdout); } else if(strcmp(command, "USER") == 0) { // Send welcome @@ -96,6 +97,8 @@ int processPacket(struct client_data *client) { // Send welcome sendMOTD(client); } else if(strcmp(command, "JOIN") == 0) { + if (!client->nick) // FIXME: error message + return 0; if (!args[0]) // FIXME: error message return 0; for (char *p = args[0]; *p; p++) // TODO: multiple channels @@ -117,6 +120,8 @@ int processPacket(struct client_data *client) { copy_strf(&databuf, ":%s 366 %s :End of /NAMES list\n", hostname, client->nick); send_log(client->FD, databuf.buf, databuf.len, 0); } else if(strcmp(command, "PRIVMSG") == 0) { + if (!client->nick) // FIXME: error message + return 0; if (argc < 2) // FIXME: error message return 0; copy_strf(&databuf, ":%s PRIVMSG %s :%s\n", client->nick, args[0], args[1]); @@ -130,6 +135,8 @@ int processPacket(struct client_data *client) { } else if(strcmp(command, "PONG") == 0) { // Do nothing } else if(strcmp(command, "MODE") == 0) { + if (!client->nick) // FIXME: error message + return 0; if (argc == 0) // FIXME: error message return 0; char *nick = args[0]; @@ -197,6 +204,8 @@ int processPacket(struct client_data *client) { } } else if(strcmp(command, "NAMES") == 0) { // TODO: actual channel support + if (!client->nick) // FIXME: error message + return 0; if (argc == 0) // FIXME: error message return 0; struct trie_find *f; @@ -209,12 +218,16 @@ int processPacket(struct client_data *client) { copy_strf(&databuf, ":%s 366 %s :End of /NAMES list\n", hostname, client->nick); send_log(client->FD, databuf.buf, databuf.len, 0); } else if(strcmp(command, "USERHOST") == 0) { + if (!client->nick) // FIXME: error message + return 0; if (argc == 0) // FIXME: error message return 0; // TODO: actual host copy_strf(&databuf, ":%s 302 %s :%s=+user@~localhost\n", hostname, client->nick, args[0]); send_log(client->FD, databuf.buf, databuf.len, 0); } else if(strcmp(command, "WHO") == 0) { + if (!client->nick) // FIXME: error message + return 0; if (argc == 0) // FIXME: error message return 0; // TODO: actual host info @@ -258,12 +271,15 @@ void sendMOTD(struct client_data *client) { send_log(client->FD, databuf.buf, databuf.len, 0); } -void setNick(struct client_data *client, char* nickname, size_t size) { +void setNick(struct client_data *client, char* nickname) { // TODO: perform basic sanitization - char *bruh = strchr(nickname, ' '); // FIXME: we should reject those tbh - if (bruh) - *bruh = 0; + char *bruh = strchr(nickname, ' '); + if (bruh || strlen(nickname) >= MAX_NICKLEN) { + copy_strf(&databuf, ":%s 432 * %s :Erroneous nickname\n", hostname, nickname); + send_log(client->FD, databuf.buf, databuf.len, 0); + return; + } char *key = mystrdup(nickname); if (!key) abort(); @@ -278,21 +294,20 @@ void setNick(struct client_data *client, char* nickname, size_t size) { } // Update nickname - if(client->nickLen) { + if(client->nick) { copy_strf(&databuf, ":%s NICK %s\n", client->nick, nickname); send_log(client->FD, databuf.buf, databuf.len, 0); sendall_log(client->FD, databuf.buf, databuf.len, 0); trie_set(&client_map, client->key, NULL, NULL); + free(client->nick); } - strncpy(client->nick, nickname, size); + client->nick = mystrdup(nickname); + if (!client->nick) + abort(); if (trie_set(&client_map, client->key = key, client, 0) == -1) abort(); - // Null term just incase - client->nick[size-1] = 0; - // Set length - client->nickLen = strlen(client->nick); return; } struct client_data *findClient(const char *nickname) diff --git a/irc.h b/irc.h index c3dedb0..9d583b9 100644 --- a/irc.h +++ b/irc.h @@ -8,7 +8,7 @@ int processPacket(struct client_data *client); void newUser(struct client_data *client); void sendMOTD(struct client_data *client); -void setNick(struct client_data *client, char* nickname, size_t size); +void setNick(struct client_data *client, char* nickname); struct client_data *findClient(const char *nickname); extern struct dynarr databuf; diff --git a/net.c b/net.c index 93e510a..a5a7535 100644 --- a/net.c +++ b/net.c @@ -52,25 +52,19 @@ void killConnection(struct client_data *cl, bool byChoice) { #endif // Start GitHub Copilot if(!byChoice) { - databuf.len = 0; - append_str(&databuf, ":"); - append_str(&databuf, cl->nick); - append_str(&databuf, " QUIT :"); - append_str(&databuf, "Has left the building (Forced)\n"); + copy_strf(&databuf, ":%s QUIT :Has left the building (Forced)\n", cl->nick); sendall_log(cl->FD, databuf.buf, databuf.len, 0); } // Send error to client informing them that they will be disconnected // ERROR :Closing Link: NICK () - databuf.len = 0; - append_str(&databuf, "ERROR :Closing Link: "); - append_str(&databuf, cl->nick); - append_str(&databuf, " ()\n"); + copy_strf(&databuf, "ERROR :Closing Link: %s ()\n", cl->nick); send_log(cl->FD, databuf.buf, databuf.len, 0); // End GitHub Copilot shutdown(cl->FD, SHUT_RDWR); closesocket(cl->FD); if (cl->key) trie_set(&client_map, cl->key, NULL, NULL); + free(cl->nick); free(cl->key); free(cl); } diff --git a/util.c b/util.c index 6166c8e..73e84c1 100644 --- a/util.c +++ b/util.c @@ -30,7 +30,8 @@ void append_strf(struct dynarr *d, const char *s, ...){ } if (end[1] == 's') { // %s append_strn(d, s, end-s); - append_str(d, va_arg(ap, char *)); + const char *c = va_arg(ap, const char *); + append_str(d, c ? c : "(null)"); s = end + 2; } else if (end[1] == '%') { // %% append_strn(d, s, end-s+1);