added new auth-check security feature
[NeonServV5.git] / src / WHOHandler.c
index 61f2ed2afec8f894ad4353eba8eea3e976cb4ad7..f8769decabbce38aa79444156a7b1006b3450e83 100644 (file)
 
 struct WHOQueueEntry {
     char type;
+    int whoid;
     struct ChanNode *chan;
     struct UserNode *user;
     struct WHOQueueEntry *next;
     void *callback[MAXCALLBACKS];
     void *data[MAXCALLBACKS];
 };
+
+static int checkWHOID(struct ClientSocket *client, int whoid) {
+    struct WHOQueueEntry *entry;
+    for(entry = client->whoqueue_first; entry; entry = entry->next) {
+        if(entry->whoid == whoid)
+            return 1;
+    }
+    return 0;
+}
+
 static struct WHOQueueEntry* addWHOQueueEntry(struct ClientSocket *client) {
+    SYNCHRONIZE(whohandler_sync);
+    int whoid = 0;
+    do {
+        whoid++;
+    } while(checkWHOID(client, whoid) && whoid < 1000);
+    if(whoid == 1000) {
+        DESYNCHRONIZE(whohandler_sync);
+        return NULL;
+    }
     struct WHOQueueEntry *entry = malloc(sizeof(*entry));
     if (!entry)
     {
         perror("malloc() failed");
+        DESYNCHRONIZE(whohandler_sync);
         return NULL;
     }
     entry->next = NULL;
+    entry->whoid = whoid;
     if(client->whoqueue_last) {
         client->whoqueue_last->next = entry;
     } else {
         client->whoqueue_first = entry;
     }
     client->whoqueue_last = entry;
+    DESYNCHRONIZE(whohandler_sync);
     return entry;
 }
 
-static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int freeEntry) {
+static struct WHOQueueEntry* getNextWHOQueueEntry(struct ClientSocket *client, int whoid, int freeEntry) {
     if(!client->whoqueue_first) return NULL;
-    struct WHOQueueEntry *entry = client->whoqueue_first;
+    SYNCHRONIZE(whohandler_sync);
+    struct WHOQueueEntry *entry;
+    for(entry = client->whoqueue_first; entry; entry = entry->next) {
+        if(entry->whoid == whoid)
+            break;
+    }
+    if(!entry) {
+        DESYNCHRONIZE(whohandler_sync);
+        return NULL;
+    }
     if(freeEntry) {
         client->whoqueue_first = entry->next;
         if(entry == client->whoqueue_last) {
             client->whoqueue_last = NULL;
         }
     }
+    DESYNCHRONIZE(whohandler_sync);
     return entry;
 }
 
 void clear_whoqueue(struct ClientSocket *client) {
     if(!client->whoqueue_first) return;
+    SYNCHRONIZE(whohandler_sync);
     struct WHOQueueEntry *entry, *next;
     for(entry = client->whoqueue_first; entry; entry = next) {
         next = entry->next;
@@ -77,6 +111,7 @@ void clear_whoqueue(struct ClientSocket *client) {
     }
     client->whoqueue_last = NULL;
     client->whoqueue_first = NULL;
+    DESYNCHRONIZE(whohandler_sync);
 }
 
 void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *data) {
@@ -108,7 +143,7 @@ void get_userlist(struct ChanNode *chan, userlist_callback_t callback, void *dat
         entry->data[0] = data;
         for(i = 1; i < MAXCALLBACKS; i++)
             entry->data[i] = NULL;
-        putsock(bot, "WHO %s,%d %%tuihnaf,%d", chan->name, entry->type, entry->type);
+        putsock(bot, "WHO %s,%d %%tuihnaf,%d", chan->name, entry->whoid, entry->whoid);
     } else
         callback(bot, chan, data);
 }
@@ -143,7 +178,7 @@ void _get_userlist_with_invisible(struct ChanNode *chan, userlist_callback_t cal
         entry->data[0] = data;
         for(i = 1; i < MAXCALLBACKS; i++)
             entry->data[i] = NULL;
-        putsock(bot, "WHO %s,%d d%%tuihnaf,%d", chan->name, entry->type, entry->type);
+        putsock(bot, "WHO %s,%d d%%tuihnaf,%d", chan->name, entry->whoid, entry->whoid);
     } else
         callback(bot, chan, data);
 }
@@ -191,18 +226,28 @@ void get_userauth(struct UserNode *user, userauth_callback_t callback, void *dat
     for(i = 1; i < MAXCALLBACKS; i++)
             entry->data[i] = NULL;
     //WHO ".$user->getNick().",".$id." %tuhna,".$id
-    putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->type, entry->type);
+    putsock(bot, "WHO %s,%d %%tuhna,%d", user->nick, entry->whoid, entry->whoid);
 }
 
+static void _recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc);
 void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
+    _recv_whohandler_354(client, argv, argc);
+}
+
+static void _recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int argc) {
     int i;
     if(argc < 2) return;
     int type = atoi(argv[1]);
     if(!type) return;
-    if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
-    struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 0);
-    if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
-    if(type & WHOQUEUETYPE_USERLIST) {
+    struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, type, 0);
+    if(entry == NULL) return;
+    #ifdef HAVE_THREADS
+    unsigned long tid = syscall(SYS_gettid);
+    while(!clientsocket_parseorder_top(tid)) {
+        usleep(1000); //1ms
+    }
+    #endif
+    if(entry->type & WHOQUEUETYPE_USERLIST) {
         if(argc < 7) return;
         //:OGN2.OnlineGamesNet.net 354 skynet 1 pk910 2001:41d0:2:1d3b::babe skynet H@ pk910
         struct ChanNode *chan = entry->chan;
@@ -234,9 +279,11 @@ void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int
         
         struct UserNode *user = getUserByNick(argv[5]);
         struct ChanUser *chanuser;
-        if((chanuserflags & CHANUSERFLAG_INVISIBLE) && (!user || !isBot(user))) {
-            user = createTempUser(argv[5]);
-            user->flags |= USERFLAG_ISTMPUSER;
+        if((chanuserflags & CHANUSERFLAG_INVISIBLE)) {
+            if(!user) {
+                user = createTempUser(argv[5]);
+                user->flags |= USERFLAG_ISTMPUSER;
+            }
             chan->flags |= CHANFLAG_HAVE_INVISIBLES;
             chanuser = addInvisibleChanUser(chan, user);
             chanuser->flags = (chanuser->flags & ~CHANUSERFLAG_OPPED_OR_VOICED) | chanuserflags;
@@ -261,7 +308,7 @@ void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int
             strcpy(user->auth, argv[7]);
             user->flags |= USERFLAG_ISAUTHED;
         }
-    } else if((type & WHOQUEUETYPE_USERAUTH) && !(entry->type & WHOQUEUETYPE_FOUND)) {
+    } else if((entry->type & WHOQUEUETYPE_USERAUTH) && !(entry->type & WHOQUEUETYPE_FOUND)) {
         //:OGN2.OnlineGamesNet.net 354 Skynet 1 pk910 2001:41d0:2:1d3b::babe Skynet pk910
         entry->type |= WHOQUEUETYPE_FOUND;
         entry->user->last_who = time(0);
@@ -277,16 +324,27 @@ void recv_whohandler_354(struct ClientSocket *client, char **argv, unsigned int
     }
 }
 
+static void _recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc);
 void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
+    _recv_whohandler_315(client, argv, argc);
+}
+
+static void _recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int argc) {
     if(argc < 2) return;
     char *typestr = strstr(argv[1], ",");
     if(!typestr) return;
     typestr++;
     int type = atoi(typestr);
-    if(!(type & WHOQUEUETYPE_ISONQUEUE)) return;
-    struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, 1);
-    if(entry == NULL || (entry->type & WHOQUEUETYPE_CHECKTYPE) != (type & WHOQUEUETYPE_CHECKTYPE)) return;
-    if(type & WHOQUEUETYPE_USERLIST) {
+    struct WHOQueueEntry* entry = getNextWHOQueueEntry(client, type, 0);
+    if(entry == NULL) return;
+    #ifdef HAVE_THREADS
+    unsigned long tid = syscall(SYS_gettid);
+    while(!clientsocket_parseorder_top(tid)) {
+        usleep(1000); //1ms
+    }
+    #endif
+    getNextWHOQueueEntry(client, type, 1);
+    if(entry->type & WHOQUEUETYPE_USERLIST) {
         //:OGN2.OnlineGamesNet.net 315 skynet #pk910,1 :End of /WHO list.
         entry->chan->flags |= CHANFLAG_RECEIVED_USERLIST;
         userlist_callback_t *callback;
@@ -306,7 +364,7 @@ void recv_whohandler_315(struct ClientSocket *client, char **argv, unsigned int
                 }
             }
         }
-    } else if(type & WHOQUEUETYPE_USERAUTH) {
+    } else if(entry->type & WHOQUEUETYPE_USERAUTH) {
         if(!(entry->type & WHOQUEUETYPE_FOUND)) {
             userauth_callback_t *callback;
             int i;