]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/command_parse.cpp
Added AES
[user/henk/code/inspircd.git] / src / command_parse.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2005 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include "inspircd_io.h"
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/errno.h>
25 #include <sys/ioctl.h>
26 #include <sys/utsname.h>
27 #include <time.h>
28 #include <string>
29 #ifdef GCC3
30 #include <ext/hash_map>
31 #else
32 #include <hash_map>
33 #endif
34 #include <map>
35 #include <sstream>
36 #include <vector>
37 #include <deque>
38 #include <sched.h>
39 #ifdef THREADED_DNS
40 #include <pthread.h>
41 #endif
42 #include "users.h"
43 #include "globals.h"
44 #include "modules.h"
45 #include "dynamic.h"
46 #include "wildcard.h"
47 #include "message.h"
48 #include "mode.h"
49 #include "commands.h"
50 #include "xline.h"
51 #include "inspstring.h"
52 #include "dnsqueue.h"
53 #include "helperfuncs.h"
54 #include "hashcomp.h"
55 #include "socketengine.h"
56 #include "userprocess.h"
57 #include "socket.h"
58 #include "dns.h"
59 #include "typedefs.h"
60 #include "command_parse.h"
61 #include "ctables.h"
62
63 extern InspIRCd* ServerInstance;
64
65 extern std::vector<Module*> modules;
66 extern std::vector<ircd_module*> factory;
67 extern std::vector<InspSocket*> module_sockets;
68 extern std::vector<userrec*> local_users;
69
70 extern int MODCOUNT;
71 extern InspSocket* socket_ref[65535];
72 extern time_t TIME;
73
74 extern SocketEngine* SE;
75
76 // This table references users by file descriptor.
77 // its an array to make it VERY fast, as all lookups are referenced
78 // by an integer, meaning there is no need for a scan/search operation.
79 extern userrec* fd_ref_table[65536];
80
81 extern Server* MyServer;
82 extern ServerConfig *Config;
83
84 extern user_hash clientlist;
85 extern chan_hash chanlist;
86
87 /* This function pokes and hacks at a parameter list like the following:
88  *
89  * PART #winbot,#darkgalaxy :m00!
90  *
91  * to turn it into a series of individual calls like this:
92  *
93  * PART #winbot :m00!
94  * PART #darkgalaxy :m00!
95  *
96  * The seperate calls are sent to a callback function provided by the caller
97  * (the caller will usually call itself recursively). The callback function
98  * must be a command handler. Calling this function on a line with no list causes
99  * no action to be taken. You must provide a starting and ending parameter number
100  * where the range of the list can be found, useful if you have a terminating
101  * parameter as above which is actually not part of the list, or parameters
102  * before the actual list as well. This code is used by many functions which
103  * can function as "one to list" (see the RFC) */
104
105 int CommandParser::LoopCall(command_t* fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
106 {
107         char plist[MAXBUF];
108         char *param;
109         char *pars[32];
110         char blog[32][MAXBUF];
111         char blog2[32][MAXBUF];
112         int j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
113         char keystr[MAXBUF];
114         char moo[MAXBUF];
115
116         for (int i = 0; i <32; i++)
117                 strcpy(blog[i],"");
118
119         for (int i = 0; i <32; i++)
120                 strcpy(blog2[i],"");
121
122         strcpy(moo,"");
123         for (int i = 0; i <10; i++)
124         {
125                 if (!parameters[i])
126                 {
127                         parameters[i] = moo;
128                 }
129         }
130         if (joins)
131         {
132                 if (pcnt > 1) /* we have a key to copy */
133                 {
134                         strlcpy(keystr,parameters[1],MAXBUF);
135                 }
136         }
137
138         if (!parameters[start])
139         {
140                 return 0;
141         }
142         if (!strchr(parameters[start],','))
143         {
144                 return 0;
145         }
146         strcpy(plist,"");
147         for (int i = start; i <= end; i++)
148         {
149                 if (parameters[i])
150                 {
151                         strlcat(plist,parameters[i],MAXBUF);
152                 }
153         }
154
155         j = 0;
156         param = plist;
157
158         t = strlen(plist);
159         for (int i = 0; i < t; i++)
160         {
161                 if (plist[i] == ',')
162                 {
163                         plist[i] = '\0';
164                         strlcpy(blog[j++],param,MAXBUF);
165                         param = plist+i+1;
166                         if (j>20)
167                         {
168                                 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,blog[j-1]);
169                                 return 1;
170                         }
171                 }
172         }
173         strlcpy(blog[j++],param,MAXBUF);
174         total = j;
175
176         if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
177         {
178                 strcat(keystr,",");
179         }
180
181         if ((joins) && (keystr))
182         {
183                 if (strchr(keystr,','))
184                 {
185                         j = 0;
186                         param = keystr;
187                         t2 = strlen(keystr);
188                         for (int i = 0; i < t2; i++)
189                         {
190                                 if (keystr[i] == ',')
191                                 {
192                                         keystr[i] = '\0';
193                                         strlcpy(blog2[j++],param,MAXBUF);
194                                         param = keystr+i+1;
195                                 }
196                         }
197                         strlcpy(blog2[j++],param,MAXBUF);
198                         total2 = j;
199                 }
200         }
201
202         for (j = 0; j < total; j++)
203         {
204                 if (blog[j])
205                 {
206                         pars[0] = blog[j];
207                 }
208                 for (q = end; q < pcnt-1; q++)
209                 {
210                         if (parameters[q+1])
211                         {
212                                 pars[q-end+1] = parameters[q+1];
213                         }
214                 }
215                 if ((joins) && (parameters[1]))
216                 {
217                         if (pcnt > 1)
218                         {
219                                 pars[1] = blog2[j];
220                         }
221                         else
222                         {
223                                 pars[1] = NULL;
224                         }
225                 }
226                 /* repeatedly call the function with the hacked parameter list */
227                 if ((joins) && (pcnt > 1))
228                 {
229                         if (pars[1])
230                         {
231                                 // pars[1] already set up and containing key from blog2[j]
232                                 fn->Handle(pars,2,u);
233                         }
234                         else
235                         {
236                                 pars[1] = parameters[1];
237                                 fn->Handle(pars,2,u);
238                         }
239                 }
240                 else
241                 {
242                         fn->Handle(pars,pcnt-(end-start),u);
243                 }
244         }
245
246         return 1;
247 }
248
249 bool CommandParser::IsValidCommand(std::string &commandname, int pcnt, userrec * user)
250 {
251         for (unsigned int i = 0; i < cmdlist.size(); i++)
252         {
253                 if (cmdlist[i]->command == commandname)
254                 {
255                         if ((pcnt>=cmdlist[i]->min_params) && (cmdlist[i]->source != "<core>"))
256                         {
257                                 if ((strchr(user->modes,cmdlist[i]->flags_needed)) || (!cmdlist[i]->flags_needed))
258                                 {
259                                         if (cmdlist[i]->flags_needed)
260                                         {
261                                                 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
262                                                 {
263                                                         return true;
264                                                 }
265                                                 else
266                                                 {
267                                                         return false;
268                                                 }
269                                         }
270                                         return true;
271                                 }
272                         }
273                 }
274         }
275         return false;
276 }
277
278 // calls a handler function for a command
279
280 void CommandParser::CallHandler(std::string &commandname,char **parameters, int pcnt, userrec *user)
281 {
282         for (unsigned int i = 0; i < cmdlist.size(); i++)
283         {
284                 if (cmdlist[i]->command == commandname)
285                 {
286                         if (pcnt>=cmdlist[i]->min_params)
287                         {
288                                 if ((strchr(user->modes,cmdlist[i]->flags_needed)) || (!cmdlist[i]->flags_needed))
289                                 {
290                                         if (cmdlist[i]->flags_needed)
291                                         {
292                                                 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
293                                                 {
294                                                         cmdlist[i]->Handle(parameters,pcnt,user);
295                                                 }
296                                         }
297                                         else
298                                         {
299                                                 cmdlist[i]->Handle(parameters,pcnt,user);
300                                         }
301                                 }
302                         }
303                 }
304         }
305 }
306
307 int CommandParser::ProcessParameters(char **command_p,char *parameters)
308 {
309         int j = 0;
310         int q = strlen(parameters);
311         if (!q)
312         {
313                 /* no parameters, command_p invalid! */
314                 return 0;
315         }
316         if (parameters[0] == ':')
317         {
318                 command_p[0] = parameters+1;
319                 return 1;
320         }
321         if (q)
322         {
323                 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
324                 {
325                         /* only one parameter */
326                         command_p[0] = parameters;
327                         if (parameters[0] == ':')
328                         {
329                                 if (strchr(parameters,' ') != NULL)
330                                 {
331                                         command_p[0]++;
332                                 }
333                         }
334                         return 1;
335                 }
336         }
337         command_p[j++] = parameters;
338         for (int i = 0; i <= q; i++)
339         {
340                 if (parameters[i] == ' ')
341                 {
342                         command_p[j++] = parameters+i+1;
343                         parameters[i] = '\0';
344                         if (command_p[j-1][0] == ':')
345                         {
346                                 *command_p[j-1]++; /* remove dodgy ":" */
347                                 break;
348                                 /* parameter like this marks end of the sequence */
349                         }
350                 }
351         }
352         return j; /* returns total number of items in the list */
353 }
354
355 void CommandParser::ProcessCommand(userrec *user, char* cmd)
356 {
357         char *parameters;
358         char *command;
359         char *command_p[127];
360         char p[MAXBUF], temp[MAXBUF];
361         int j, items, cmd_found;
362
363         for (int i = 0; i < 127; i++)
364                 command_p[i] = NULL;
365
366         if (!user)
367         {
368                 return;
369         }
370         if (!cmd)
371         {
372                 return;
373         }
374         if (!cmd[0])
375         {
376                 return;
377         }
378
379         int total_params = 0;
380         if (strlen(cmd)>2)
381         {
382                 for (unsigned int q = 0; q < strlen(cmd)-1; q++)
383                 {
384                         if ((cmd[q] == ' ') && (cmd[q+1] == ':'))
385                         {
386                                 total_params++;
387                                 // found a 'trailing', we dont count them after this.
388                                 break;
389                         }
390                         if (cmd[q] == ' ')
391                                 total_params++;
392                 }
393         }
394
395         // another phidjit bug...
396         if (total_params > 126)
397         {
398                 *(strchr(cmd,' ')) = '\0';
399                 WriteServ(user->fd,"421 %s %s :Too many parameters given",user->nick,cmd);
400                 return;
401         }
402
403         strlcpy(temp,cmd,MAXBUF);
404
405         std::string tmp = cmd;
406         for (int i = 0; i <= MODCOUNT; i++)
407         {
408                 std::string oldtmp = tmp;
409                 modules[i]->OnServerRaw(tmp,true,user);
410                 if (oldtmp != tmp)
411                 {
412                         log(DEBUG,"A Module changed the input string!");
413                         log(DEBUG,"New string: %s",tmp.c_str());
414                         log(DEBUG,"Old string: %s",oldtmp.c_str());
415                         break;
416                 }
417         }
418         strlcpy(cmd,tmp.c_str(),MAXBUF);
419         strlcpy(temp,cmd,MAXBUF);
420
421         if (!strchr(cmd,' '))
422         {
423                 /* no parameters, lets skip the formalities and not chop up
424                  * the string */
425                 log(DEBUG,"About to preprocess command with no params");
426                 items = 0;
427                 command_p[0] = NULL;
428                 parameters = NULL;
429                 for (unsigned int i = 0; i <= strlen(cmd); i++)
430                 {
431                         cmd[i] = toupper(cmd[i]);
432                 }
433                 command = cmd;
434         }
435         else
436         {
437                 strcpy(cmd,"");
438                 j = 0;
439                 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
440                 for (unsigned int i = 0; i < strlen(temp); i++)
441                 {
442                         if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
443                         {
444                                 cmd[j++] = temp[i];
445                                 cmd[j] = 0;
446                         }
447                 }
448                 /* split the full string into a command plus parameters */
449                 parameters = p;
450                 strcpy(p," ");
451                 command = cmd;
452                 if (strchr(cmd,' '))
453                 {
454                         for (unsigned int i = 0; i <= strlen(cmd); i++)
455                         {
456                                 /* capitalise the command ONLY, leave params intact */
457                                 cmd[i] = toupper(cmd[i]);
458                                 /* are we nearly there yet?! :P */
459                                 if (cmd[i] == ' ')
460                                 {
461                                         command = cmd;
462                                         parameters = cmd+i+1;
463                                         cmd[i] = '\0';
464                                         break;
465                                 }
466                         }
467                 }
468                 else
469                 {
470                         for (unsigned int i = 0; i <= strlen(cmd); i++)
471                         {
472                                 cmd[i] = toupper(cmd[i]);
473                         }
474                 }
475
476         }
477         cmd_found = 0;
478
479         if (strlen(command)>MAXCOMMAND)
480         {
481                 WriteServ(user->fd,"421 %s %s :Command too long",user->nick,command);
482                 return;
483         }
484
485         for (unsigned int x = 0; x < strlen(command); x++)
486         {
487                 if (((command[x] < 'A') || (command[x] > 'Z')) && (command[x] != '.'))
488                 {
489                         if (((command[x] < '0') || (command[x]> '9')) && (command[x] != '-'))
490                         {
491                                 if (strchr("@!\"$%^&*(){}[]_=+;:'#~,<>/?\\|`",command[x]))
492                                 {
493                                         ServerInstance->stats->statsUnknown++;
494                                         WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
495                                         return;
496                                 }
497                         }
498                 }
499         }
500
501         std::string xcommand = command;
502         for (unsigned int i = 0; i != cmdlist.size(); i++)
503         {
504                         if ((xcommand.length() >= cmdlist[i]->command.length()) && (xcommand == cmdlist[i]->command))
505                         {
506                                 if (parameters)
507                                 {
508                                         if (parameters[0])
509                                         {
510                                                 items = this->ProcessParameters(command_p,parameters);
511                                         }
512                                         else
513                                         {
514                                                 items = 0;
515                                                 command_p[0] = NULL;
516                                         }
517                                 }
518                                 else
519                                 {
520                                         items = 0;
521                                         command_p[0] = NULL;
522                                 }
523
524                                 if (user)
525                                 {
526                                         /* activity resets the ping pending timer */
527                                         user->nping = TIME + user->pingmax;
528                                         if ((items) < cmdlist[i]->min_params)
529                                         {
530                                                 log(DEBUG,"not enough parameters: %s %s",user->nick,command);
531                                                 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
532                                                 return;
533                                         }
534                                         if ((!strchr(user->modes,cmdlist[i]->flags_needed)) && (cmdlist[i]->flags_needed))
535                                         {
536                                                 log(DEBUG,"permission denied: %s %s",user->nick,command);
537                                                 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
538                                                 cmd_found = 1;
539                                                 return;
540                                         }
541                                         if ((cmdlist[i]->flags_needed) && (!user->HasPermission(xcommand)))
542                                         {
543                                                 log(DEBUG,"permission denied: %s %s",user->nick,command);
544                                                 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command);
545                                                 cmd_found = 1;
546                                                 return;
547                                         }
548                                         /* if the command isnt USER, PASS, or NICK, and nick is empty,
549                                          * deny command! */
550                                         if ((strncmp(command,"USER",4)) && (strncmp(command,"NICK",4)) && (strncmp(command,"PASS",4)))
551                                         {
552                                                 if ((!isnick(user->nick)) || (user->registered != 7))
553                                                 {
554                                                         log(DEBUG,"not registered: %s %s",user->nick,command);
555                                                         WriteServ(user->fd,"451 %s :You have not registered",command);
556                                                         return;
557                                                 }
558                                         }
559                                         if ((user->registered == 7) && (!strchr(user->modes,'o')))
560                                         {
561                                                 std::stringstream dcmds(Config->DisabledCommands);
562                                                 while (!dcmds.eof())
563                                                 {
564                                                         std::string thiscmd;
565                                                         dcmds >> thiscmd;
566                                                         if (!strcasecmp(thiscmd.c_str(),command))
567                                                         {
568                                                                 // command is disabled!
569                                                                 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command);
570                                                                 return;
571                                                         }
572                                                 }
573                                         }
574                                         if ((user->registered == 7) || (!strncmp(command,"USER",4)) || (!strncmp(command,"NICK",4)) || (!strncmp(command,"PASS",4)))
575                                         {
576                                                         /* ikky /stats counters */
577                                                         if (temp)
578                                                         {
579                                                                 cmdlist[i]->use_count++;
580                                                                 cmdlist[i]->total_bytes+=strlen(temp);
581                                                         }
582
583                                                         int MOD_RESULT = 0;
584                                                         FOREACH_RESULT(OnPreCommand(command,command_p,items,user));
585                                                         if (MOD_RESULT == 1) {
586                                                                 return;
587                                                         }
588
589                                                         /* WARNING: nothing may come after the
590                                                          * command handler call, as the handler
591                                                          * may free the user structure! */
592
593                                                         cmdlist[i]->Handle(command_p,items,user);
594                                                 return;
595                                         }
596                                         else
597                                         {
598                                                 WriteServ(user->fd,"451 %s :You have not registered",command);
599                                                 return;
600                                         }
601                                 }
602                                 cmd_found = 1;
603                         }
604         }
605         if ((!cmd_found) && (user))
606         {
607                 ServerInstance->stats->statsUnknown++;
608                 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
609         }
610 }
611
612 bool CommandParser::RemoveCommands(const char* source)
613 {
614         bool go_again = true;
615         while (go_again)
616         {
617                 go_again = false;
618                 for (std::deque<command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
619                 {
620                         command_t* x = (command_t*)*i;
621                         if (x->source == std::string(source))
622                         {
623                                 log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
624                                 cmdlist.erase(i);
625                                 go_again = true;
626                                 break;
627                         }
628                 }
629         }
630         return true;
631 }
632
633 void CommandParser::ProcessBuffer(const char* cmdbuf,userrec *user)
634 {
635         if (!user)
636         {
637                 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
638                 return;
639         }
640         char cmd[MAXBUF];
641         if (!cmdbuf)
642         {
643                 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
644                 return;
645         }
646         if (!cmdbuf[0])
647         {
648                 return;
649         }
650         while (*cmdbuf == ' ') cmdbuf++; // strip leading spaces
651
652         strlcpy(cmd,cmdbuf,MAXBUF);
653         if (!cmd[0])
654         {
655                 return;
656         }
657         int sl = strlen(cmd)-1;
658         if ((cmd[sl] == 13) || (cmd[sl] == 10))
659         {
660                 cmd[sl] = '\0';
661         }
662         sl = strlen(cmd)-1;
663         if ((cmd[sl] == 13) || (cmd[sl] == 10))
664         {
665                 cmd[sl] = '\0';
666         }
667         sl = strlen(cmd)-1;
668         while (cmd[sl] == ' ') // strip trailing spaces
669         {
670                 cmd[sl] = '\0';
671                 sl = strlen(cmd)-1;
672         }
673
674         if (!cmd[0])
675         {
676                 return;
677         }
678         log(DEBUG,"CMDIN: %s %s",user->nick,cmd);
679         tidystring(cmd);
680         if ((user) && (cmd))
681         {
682                 this->ProcessCommand(user,cmd);
683         }
684 }
685
686 bool CommandParser::CreateCommand(command_t *f)
687 {
688         /* create the command and push it onto the table */
689         cmdlist.push_back(f);
690         log(DEBUG,"Added command %s (%lu parameters)",f->command.c_str(),(unsigned long)f->min_params);
691         return true;
692 }
693
694 CommandParser::CommandParser()
695 {
696         this->SetupCommandTable();
697 }
698
699 void CommandParser::SetupCommandTable()
700 {
701         this->CreateCommand(new cmd_user);
702         this->CreateCommand(new cmd_nick);
703         this->CreateCommand(new cmd_quit);
704         this->CreateCommand(new cmd_version);
705         this->CreateCommand(new cmd_ping);
706         this->CreateCommand(new cmd_pong);
707         this->CreateCommand(new cmd_admin);
708         this->CreateCommand(new cmd_privmsg);
709         this->CreateCommand(new cmd_info);
710         this->CreateCommand(new cmd_time);
711         this->CreateCommand(new cmd_whois);
712         this->CreateCommand(new cmd_wallops);
713         this->CreateCommand(new cmd_notice);
714         this->CreateCommand(new cmd_join);
715         this->CreateCommand(new cmd_names);
716         this->CreateCommand(new cmd_part);
717         this->CreateCommand(new cmd_kick);
718         this->CreateCommand(new cmd_mode);
719         this->CreateCommand(new cmd_topic);
720         this->CreateCommand(new cmd_who);
721         this->CreateCommand(new cmd_motd);
722         this->CreateCommand(new cmd_rules);
723         this->CreateCommand(new cmd_oper);
724         this->CreateCommand(new cmd_list);
725         this->CreateCommand(new cmd_die);
726         this->CreateCommand(new cmd_restart);
727         this->CreateCommand(new cmd_kill);
728         this->CreateCommand(new cmd_rehash);
729         this->CreateCommand(new cmd_lusers);
730         this->CreateCommand(new cmd_stats);
731         this->CreateCommand(new cmd_userhost);
732         this->CreateCommand(new cmd_away);
733         this->CreateCommand(new cmd_ison);
734         this->CreateCommand(new cmd_summon);
735         this->CreateCommand(new cmd_users);
736         this->CreateCommand(new cmd_invite);
737         this->CreateCommand(new cmd_pass);
738         this->CreateCommand(new cmd_trace);
739         this->CreateCommand(new cmd_whowas);
740         this->CreateCommand(new cmd_connect);
741         this->CreateCommand(new cmd_squit);
742         this->CreateCommand(new cmd_modules);
743         this->CreateCommand(new cmd_links);
744         this->CreateCommand(new cmd_map);
745         this->CreateCommand(new cmd_kline);
746         this->CreateCommand(new cmd_gline);
747         this->CreateCommand(new cmd_zline);
748         this->CreateCommand(new cmd_qline);
749         this->CreateCommand(new cmd_eline);
750         this->CreateCommand(new cmd_loadmodule);
751         this->CreateCommand(new cmd_unloadmodule);
752         this->CreateCommand(new cmd_server);
753         this->CreateCommand(new cmd_commands);
754 }
755