]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-login.c
Don't use a standard message number
[ngircd-alex.git] / src / ngircd / irc-login.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  */
11
12 #include "portab.h"
13
14 /**
15  * @file
16  * Login and logout
17  */
18
19 #include <assert.h>
20 #include <ctype.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <strings.h>
24 #include <time.h>
25
26 #include "conn-func.h"
27 #include "conf.h"
28 #include "channel.h"
29 #include "log.h"
30 #include "login.h"
31 #include "messages.h"
32 #include "parse.h"
33 #include "irc.h"
34 #include "irc-macros.h"
35 #include "irc-write.h"
36
37 #include "irc-login.h"
38
39 static void Change_Nick PARAMS((CLIENT * Origin, CLIENT * Target, char *NewNick,
40                                 bool InformClient));
41
42 /**
43  * Handler for the IRC "PASS" command.
44  *
45  * @param Client The client from which this command has been received.
46  * @param Req Request structure with prefix and all parameters.
47  * @return CONNECTED or DISCONNECTED.
48  */
49 GLOBAL bool
50 IRC_PASS( CLIENT *Client, REQUEST *Req )
51 {
52         char *type, *orig_flags;
53         int protohigh, protolow;
54
55         assert( Client != NULL );
56         assert( Req != NULL );
57
58         /* Return an error if this is not a local client */
59         if (Client_Conn(Client) <= NONE)
60                 return IRC_WriteErrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
61                                           Client_ID(Client), Req->command);
62
63         if (Client_Type(Client) == CLIENT_UNKNOWN && Req->argc == 1) {
64                 /* Not yet registered "unknown" connection, PASS with one
65                  * argument: either a regular client, service, or server
66                  * using the old RFC 1459 section 4.1.1 syntax. */
67                 LogDebug("Connection %d: got PASS command (RFC 1459) ...",
68                          Client_Conn(Client));
69         } else if ((Client_Type(Client) == CLIENT_UNKNOWN ||
70                     Client_Type(Client) == CLIENT_UNKNOWNSERVER) &&
71                    (Req->argc == 3 || Req->argc == 4)) {
72                 /* Not yet registered "unknown" connection or outgoing server
73                  * link, PASS with three or four argument: server using the
74                  * RFC 2813 section 4.1.1 syntax. */
75                 LogDebug("Connection %d: got PASS command (RFC 2813, new server link) ...",
76                          Client_Conn(Client));
77         } else if (Client_Type(Client) == CLIENT_UNKNOWN ||
78                    Client_Type(Client) == CLIENT_UNKNOWNSERVER) {
79                 /* Unregistered connection, but wrong number of arguments: */
80                 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
81                                           Client_ID(Client), Req->command);
82         } else {
83                 /* Registered connection, PASS command is not allowed! */
84                 return IRC_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
85                                           Client_ID(Client));
86         }
87
88         Conn_SetPassword(Client_Conn(Client), Req->argv[0]);
89
90         /* Protocol version */
91         if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
92                 int c2, c4;
93
94                 c2 = Req->argv[1][2];
95                 c4 = Req->argv[1][4];
96
97                 Req->argv[1][4] = '\0';
98                 protolow = atoi(&Req->argv[1][2]);
99                 Req->argv[1][2] = '\0';
100                 protohigh = atoi(Req->argv[1]);
101
102                 Req->argv[1][2] = c2;
103                 Req->argv[1][4] = c4;
104
105                 Client_SetType(Client, CLIENT_GOTPASS_2813);
106         } else {
107                 protohigh = protolow = 0;
108                 Client_SetType(Client, CLIENT_GOTPASS);
109         }
110
111         /* Protocol type, see doc/Protocol.txt */
112         if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
113                 type = &Req->argv[1][4];
114         else
115                 type = NULL;
116
117         /* Protocol flags/options */
118         if (Req->argc >= 4)
119                 orig_flags = Req->argv[3];
120         else
121                 orig_flags = "";
122
123         /* Implementation, version and IRC+ flags */
124         if (Req->argc >= 3) {
125                 char *impl, *ptr, *serverver, *flags;
126
127                 impl = Req->argv[2];
128                 ptr = strchr(impl, '|');
129                 if (ptr)
130                         *ptr = '\0';
131
132                 if (type && strcmp(type, PROTOIRCPLUS) == 0) {
133                         /* The peer seems to be a server which supports the
134                          * IRC+ protocol (see doc/Protocol.txt). */
135                         serverver = ptr ? ptr + 1 : "?";
136                         flags = strchr(ptr ? serverver : impl, ':');
137                         if (flags) {
138                                 *flags = '\0';
139                                 flags++;
140                         } else
141                                 flags = "";
142                         Log(LOG_INFO,
143                             "Peer on connection %d announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
144                             Client_Conn(Client), impl, serverver,
145                             protohigh, protolow, flags);
146                 } else {
147                         /* The peer seems to be a server supporting the
148                          * "original" IRC protocol (RFC 2813). */
149                         if (strchr(orig_flags, 'Z'))
150                                 flags = "Z";
151                         else
152                                 flags = "";
153                         Log(LOG_INFO,
154                             "Peer on connection %d announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
155                             Client_Conn(Client), impl,
156                             protohigh, protolow, flags);
157                 }
158                 Client_SetFlags(Client, flags);
159         }
160
161         return CONNECTED;
162 } /* IRC_PASS */
163
164 /**
165  * Handler for the IRC "NICK" command.
166  *
167  * @param Client The client from which this command has been received.
168  * @param Req Request structure with prefix and all parameters.
169  * @return CONNECTED or DISCONNECTED.
170  */
171 GLOBAL bool
172 IRC_NICK( CLIENT *Client, REQUEST *Req )
173 {
174         CLIENT *intr_c, *target, *c;
175         CHANNEL *chan;
176         char *nick, *user, *hostname, *modes, *info;
177         int token, hops;
178
179         assert( Client != NULL );
180         assert( Req != NULL );
181
182         /* Some IRC clients, for example BitchX, send the NICK and USER
183          * commands in the wrong order ... */
184         if(Client_Type(Client) == CLIENT_UNKNOWN
185             || Client_Type(Client) == CLIENT_GOTPASS
186             || Client_Type(Client) == CLIENT_GOTNICK
187 #ifndef STRICT_RFC
188             || Client_Type(Client) == CLIENT_GOTUSER
189 #endif
190             || Client_Type(Client) == CLIENT_USER
191             || Client_Type(Client) == CLIENT_SERVICE
192             || (Client_Type(Client) == CLIENT_SERVER && Req->argc == 1))
193         {
194                 /* User registration or change of nickname */
195                 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 1)
196
197                 /* Search "target" client */
198                 if (Client_Type(Client) == CLIENT_SERVER) {
199                         target = Client_Search(Req->prefix);
200                         if (!target)
201                                 return IRC_WriteErrClient(Client,
202                                                           ERR_NOSUCHNICK_MSG,
203                                                           Client_ID(Client),
204                                                           Req->argv[0]);
205                 } else {
206                         /* Is this a restricted client? */
207                         if (Client_HasMode(Client, 'r'))
208                                 return IRC_WriteErrClient(Client,
209                                                           ERR_RESTRICTED_MSG,
210                                                           Client_ID(Client));
211                         target = Client;
212                 }
213
214 #ifndef STRICT_RFC
215                 /* If the clients tries to change to its own nickname we won't
216                  * do anything. This is how the original ircd behaves and some
217                  * clients (for example Snak) expect it to be like this.
218                  * But I doubt that this is "really the right thing" ... */
219                 if (strcmp(Client_ID(target), Req->argv[0]) == 0)
220                         return CONNECTED;
221 #endif
222
223                 /* Check that the new nickname is available. Special case:
224                  * the client only changes from/to upper to lower case. */
225                 if (strcasecmp(Client_ID(target), Req->argv[0]) != 0) {
226                         if (!Client_CheckNick(target, Req->argv[0]))
227                                 return CONNECTED;
228                 }
229
230                 if (Client_Type(target) != CLIENT_USER &&
231                     Client_Type(target) != CLIENT_SERVICE &&
232                     Client_Type(target) != CLIENT_SERVER) {
233                         /* New client */
234                         LogDebug("Connection %d: got valid NICK command ...",
235                              Client_Conn( Client ));
236
237                         /* Register new nickname of this client */
238                         Client_SetID( target, Req->argv[0] );
239
240 #ifndef STRICT_RFC
241                         if (Conf_AuthPing) {
242 #ifdef HAVE_ARC4RANDOM
243                                 Conn_SetAuthPing(Client_Conn(Client), arc4random());
244 #else
245                                 Conn_SetAuthPing(Client_Conn(Client), rand());
246 #endif
247                                 Conn_WriteStr(Client_Conn(Client), "PING :%ld",
248                                         Conn_GetAuthPing(Client_Conn(Client)));
249                                 LogDebug("Connection %d: sent AUTH PING %ld ...",
250                                         Client_Conn(Client),
251                                         Conn_GetAuthPing(Client_Conn(Client)));
252                         }
253 #endif
254
255                         /* If we received a valid USER command already then
256                          * register the new client! */
257                         if( Client_Type( Client ) == CLIENT_GOTUSER )
258                                 return Login_User( Client );
259                         else
260                                 Client_SetType( Client, CLIENT_GOTNICK );
261                 } else {
262                         /* Nickname change */
263
264                         /* Check that the user isn't on any channels set +N */
265                         chan = Channel_First();
266                         while (chan) {
267                                 if(Channel_IsMemberOf(chan, Client) &&
268                                    Channel_HasMode(chan, 'N') &&
269                                    !Client_HasMode(Client, 'o'))
270                                         return IRC_WriteErrClient(Client,
271                                                                   ERR_NONICKCHANGE_MSG,
272                                                                   Client_ID(Client),
273                                                                   Channel_Name(chan));
274                                 chan = Channel_Next(chan);
275                         }
276
277                         Change_Nick(Client, target, Req->argv[0],
278                                     Client_Type(Client) == CLIENT_USER ? true : false);
279                         IRC_SetPenalty(target, 2);
280                 }
281
282                 return CONNECTED;
283         } else if(Client_Type(Client) == CLIENT_SERVER ||
284                   Client_Type(Client) == CLIENT_SERVICE) {
285                 /* Server or service introduces new client */
286
287                 /* Bad number of parameters? */
288                 if (Req->argc != 2 && Req->argc != 7)
289                         return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
290                                                   Client_ID(Client), Req->command);
291
292                 if (Req->argc >= 7) {
293                         /* RFC 2813 compatible syntax */
294                         nick = Req->argv[0];
295                         hops = atoi(Req->argv[1]);
296                         user = Req->argv[2];
297                         hostname = Req->argv[3];
298                         token = atoi(Req->argv[4]);
299                         modes = Req->argv[5] + 1;
300                         info = Req->argv[6];
301                 } else {
302                         /* RFC 1459 compatible syntax */
303                         nick = Req->argv[0];
304                         hops = 1;
305                         user = Req->argv[0];
306                         hostname = Client_ID(Client);
307                         token = atoi(Req->argv[1]);
308                         modes = "";
309                         info = Req->argv[0];
310                 }
311
312                 c = Client_Search(nick);
313                 if(c) {
314                         /*
315                          * the new nick is already present on this server:
316                          * the new and the old one have to be disconnected now.
317                          */
318                         Log(LOG_ERR,
319                             "Server %s introduces already registered nick \"%s\"!",
320                             Client_ID(Client), Req->argv[0]);
321                         return IRC_KillClient(Client, NULL, Req->argv[0],
322                                               "Nick collision");
323                 }
324
325                 /* Find the Server this client is connected to */
326                 intr_c = Client_GetFromToken(Client, token);
327                 if (!intr_c) {
328                         Log(LOG_ERR,
329                             "Server %s introduces nick \"%s\" on unknown server!?",
330                             Client_ID(Client), Req->argv[0]);
331                         return IRC_KillClient(Client, NULL, Req->argv[0],
332                                               "Unknown server");
333                 }
334
335                 c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
336                                          token, modes, info, true);
337                 if (!c) {
338                         /* Out of memory, we need to disconnect client to keep
339                          * network state consistent! */
340                         Log(LOG_ALERT,
341                             "Can't create client structure! (on connection %d)",
342                             Client_Conn(Client));
343                         return IRC_KillClient(Client, NULL, Req->argv[0],
344                                               "Server error");
345                 }
346
347                 /* RFC 2813: client is now fully registered, inform all the
348                  * other servers about the new user.
349                  * RFC 1459: announce the new client only after receiving the
350                  * USER command, first we need more information! */
351                 if (Req->argc < 7) {
352                         LogDebug("Client \"%s\" is being registered (RFC 1459) ...",
353                                  Client_Mask(c));
354                         Client_SetType(c, CLIENT_GOTNICK);
355                 } else
356                         Client_Introduce(Client, c, CLIENT_USER);
357
358                 return CONNECTED;
359         }
360         else
361                 return IRC_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
362                                           Client_ID(Client));
363 } /* IRC_NICK */
364
365 /**
366  * Handler for the IRC "SVSNICK" command.
367  *
368  * @param Client The client from which this command has been received.
369  * @param Req Request structure with prefix and all parameters.
370  * @return CONNECTED or DISCONNECTED.
371  */
372 GLOBAL bool
373 IRC_SVSNICK(CLIENT *Client, REQUEST *Req)
374 {
375         CLIENT *from, *target;
376
377         assert(Client != NULL);
378         assert(Req != NULL);
379
380         /* Search the originator */
381         from = Client_Search(Req->prefix);
382         if (!from)
383                 from = Client;
384
385         /* Search the target */
386         target = Client_Search(Req->argv[0]);
387         if (!target || Client_Type(target) != CLIENT_USER)
388                 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
389                                           Client_ID(Client), Req->argv[0]);
390
391         if (Client_Conn(target) <= NONE) {
392                 /* We have to forward the message to the server handling
393                  * this user; this is required to make sure all servers
394                  * in the network do follow the nick name change! */
395                 return IRC_WriteStrClientPrefix(Client_NextHop(target), from,
396                                                 "SVSNICK %s %s",
397                                                 Req->argv[0], Req->argv[1]);
398         }
399
400         /* Make sure that the new nickname is valid */
401         if (!Client_CheckNick(from, Req->argv[1]))
402                 return CONNECTED;
403
404         Change_Nick(from, target, Req->argv[1], true);
405         return CONNECTED;
406 }
407
408 /**
409  * Handler for the IRC "USER" command.
410  *
411  * @param Client The client from which this command has been received.
412  * @param Req Request structure with prefix and all parameters.
413  * @return CONNECTED or DISCONNECTED.
414  */
415 GLOBAL bool
416 IRC_USER(CLIENT * Client, REQUEST * Req)
417 {
418         CLIENT *c;
419         char *ptr;
420
421         assert(Client != NULL);
422         assert(Req != NULL);
423
424         if (Client_Type(Client) == CLIENT_GOTNICK ||
425 #ifndef STRICT_RFC
426             Client_Type(Client) == CLIENT_UNKNOWN ||
427 #endif
428             Client_Type(Client) == CLIENT_GOTPASS)
429         {
430                 /* New connection */
431                 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 4)
432
433                 /* User name: only alphanumeric characters and limited
434                    punctuation is allowed.*/
435                 ptr = Req->argv[0];
436                 while (*ptr) {
437                         if (!isalnum((int)*ptr) &&
438                             *ptr != '+' && *ptr != '-' && *ptr != '@' &&
439                             *ptr != '.' && *ptr != '_') {
440                                 Conn_Close(Client_Conn(Client), NULL,
441                                            "Invalid user name", true);
442                                 return DISCONNECTED;
443                         }
444                         ptr++;
445                 }
446
447                 /* Save the received username for authentication, and use
448                  * it up to the first '@' as default user name (like ircd2.11,
449                  * bahamut, ircd-seven, ...), prefixed with '~', if needed: */
450                 Client_SetOrigUser(Client, Req->argv[0]);
451                 ptr = strchr(Req->argv[0], '@');
452                 if (ptr)
453                         *ptr = '\0';
454 #ifdef IDENTAUTH
455                 ptr = Client_User(Client);
456                 if (!ptr || !*ptr || *ptr == '~')
457                         Client_SetUser(Client, Req->argv[0], false);
458 #else
459                 Client_SetUser(Client, Req->argv[0], false);
460 #endif
461
462                 /* "Real name" or user info text: Don't set it to the empty
463                  * string, the original ircd can't deal with such "real names"
464                  * (e. g. "USER user * * :") ... */
465                 if (*Req->argv[3])
466                         Client_SetInfo(Client, Req->argv[3]);
467                 else
468                         Client_SetInfo(Client, "-");
469
470                 LogDebug("Connection %d: got valid USER command ...",
471                     Client_Conn(Client));
472                 if (Client_Type(Client) == CLIENT_GOTNICK)
473                         return Login_User(Client);
474                 else
475                         Client_SetType(Client, CLIENT_GOTUSER);
476                 return CONNECTED;
477
478         } else if (Client_Type(Client) == CLIENT_SERVER ||
479                    Client_Type(Client) == CLIENT_SERVICE) {
480                 /* Server/service updating an user */
481                 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 4)
482
483                 c = Client_Search(Req->prefix);
484                 if (!c)
485                         return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
486                                                   Client_ID(Client),
487                                                   Req->prefix);
488
489                 Client_SetUser(c, Req->argv[0], true);
490                 Client_SetOrigUser(c, Req->argv[0]);
491                 Client_SetHostname(c, Req->argv[1]);
492                 Client_SetInfo(c, Req->argv[3]);
493
494                 LogDebug("Connection %d: got valid USER command for \"%s\".",
495                          Client_Conn(Client), Client_Mask(c));
496
497                 /* RFC 1459 style user registration?
498                  * Introduce client to network: */
499                 if (Client_Type(c) == CLIENT_GOTNICK)
500                         Client_Introduce(Client, c, CLIENT_USER);
501
502                 return CONNECTED;
503         } else if (Client_Type(Client) == CLIENT_USER) {
504                 /* Already registered connection */
505                 return IRC_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
506                                           Client_ID(Client));
507         } else {
508                 /* Unexpected/invalid connection state? */
509                 return IRC_WriteErrClient(Client, ERR_NOTREGISTERED_MSG,
510                                           Client_ID(Client));
511         }
512 } /* IRC_USER */
513
514 /**
515  * Handler for the IRC "SERVICE" command.
516  *
517  * At the moment ngIRCd doesn't support directly linked services, so this
518  * function returns ERR_ERRONEUSNICKNAME when the SERVICE command has not been
519  * received from a peer server.
520  *
521  * @param Client The client from which this command has been received.
522  * @param Req Request structure with prefix and all parameters.
523  * @return CONNECTED or DISCONNECTED.
524  */
525 GLOBAL bool
526 IRC_SERVICE(CLIENT *Client, REQUEST *Req)
527 {
528         CLIENT *c, *intr_c;
529         char *nick, *user, *host, *info, *modes, *ptr;
530         int token, hops;
531
532         assert(Client != NULL);
533         assert(Req != NULL);
534
535         if (Client_Type(Client) != CLIENT_GOTPASS &&
536             Client_Type(Client) != CLIENT_SERVER)
537                 return IRC_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
538                                           Client_ID(Client));
539
540         if (Client_Type(Client) != CLIENT_SERVER)
541                 return IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
542                                   Client_ID(Client), Req->argv[0]);
543
544         nick = Req->argv[0];
545         user = NULL; host = NULL;
546         token = atoi(Req->argv[1]);
547         hops = atoi(Req->argv[4]);
548         info = Req->argv[5];
549
550         /* Validate service name ("nickname") */
551         c = Client_Search(nick);
552         if(c) {
553                 /* Nickname collision: disconnect (KILL) both clients! */
554                 Log(LOG_ERR,
555                     "Server %s introduces already registered service \"%s\"!",
556                     Client_ID(Client), nick);
557                 return IRC_KillClient(Client, NULL, nick, "Nick collision");
558         }
559
560         /* Get the server to which the service is connected */
561         intr_c = Client_GetFromToken(Client, token);
562         if (! intr_c) {
563                 Log(LOG_ERR,
564                     "Server %s introduces service \"%s\" on unknown server!?",
565                     Client_ID(Client), nick);
566                 return IRC_KillClient(Client, NULL, nick, "Unknown server");
567         }
568
569         /* Get user and host name */
570         ptr = strchr(nick, '@');
571         if (ptr) {
572                 *ptr = '\0';
573                 host = ++ptr;
574         }
575         if (!host)
576                 host = Client_Hostname(intr_c);
577         ptr = strchr(nick, '!');
578         if (ptr) {
579                 *ptr = '\0';
580                 user = ++ptr;
581         }
582         if (!user)
583                 user = nick;
584
585         /* According to RFC 2812/2813 parameter 4 <type> "is currently reserved
586          * for future usage"; but we use it to transfer the modes and check
587          * that the first character is a '+' sign and ignore it otherwise. */
588         modes = (Req->argv[3][0] == '+') ? ++Req->argv[3] : "";
589
590         c = Client_NewRemoteUser(intr_c, nick, hops, user, host,
591                                  token, modes, info, true);
592         if (! c) {
593                 /* Couldn't create client structure, so KILL the service to
594                  * keep network status consistent ... */
595                 Log(LOG_ALERT,
596                     "Can't create client structure! (on connection %d)",
597                     Client_Conn(Client));
598                 return IRC_KillClient(Client, NULL, nick, "Server error");
599         }
600
601         Client_Introduce(Client, c, CLIENT_SERVICE);
602         return CONNECTED;
603 } /* IRC_SERVICE */
604
605 /**
606  * Handler for the IRC "WEBIRC" command.
607  *
608  * @param Client The client from which this command has been received.
609  * @param Req Request structure with prefix and all parameters.
610  * @return CONNECTED or DISCONNECTED.
611  */
612 GLOBAL bool
613 IRC_WEBIRC(CLIENT *Client, REQUEST *Req)
614 {
615         if (!Conf_WebircPwd[0] || strcmp(Req->argv[0], Conf_WebircPwd) != 0)
616                 return IRC_WriteErrClient(Client, ERR_PASSWDMISMATCH_MSG,
617                                           Client_ID(Client));
618
619         LogDebug("Connection %d: got valid WEBIRC command: user=%s, host=%s, ip=%s",
620                  Client_Conn(Client), Req->argv[1], Req->argv[2], Req->argv[3]);
621
622         Client_SetUser(Client, Req->argv[1], true);
623         Client_SetOrigUser(Client, Req->argv[1]);
624         if (Conf_DNS)
625                 Client_SetHostname(Client, Req->argv[2]);
626         else
627                 Client_SetHostname(Client, Req->argv[3]);
628         Client_SetIPAText(Client, Req->argv[3]);
629
630         return CONNECTED;
631 } /* IRC_WEBIRC */
632
633 /**
634  * Handler for the IRC "QUIT" command.
635  *
636  * @param Client The client from which this command has been received.
637  * @param Req Request structure with prefix and all parameters.
638  * @return CONNECTED or DISCONNECTED.
639  */
640 GLOBAL bool
641 IRC_QUIT( CLIENT *Client, REQUEST *Req )
642 {
643         CLIENT *target;
644         char quitmsg[COMMAND_LEN];
645
646         assert(Client != NULL);
647         assert(Req != NULL);
648
649         if (Req->argc == 1)
650                 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
651
652         if (Client_Type(Client) == CLIENT_SERVER) {
653                 /* Server */
654                 target = Client_Search(Req->prefix);
655                 if (!target) {
656                         Log(LOG_WARNING,
657                             "Got QUIT from %s for unknown client!?",
658                             Client_ID(Client));
659                         return CONNECTED;
660                 }
661
662                 if (target != Client) {
663                         Client_Destroy(target, "Got QUIT command",
664                                        Req->argc == 1 ? quitmsg : NULL, true);
665                         return CONNECTED;
666                 } else {
667                         Conn_Close(Client_Conn(Client), "Got QUIT command",
668                                    Req->argc == 1 ? quitmsg : NULL, true);
669                         return DISCONNECTED;
670                 }
671         } else {
672                 if (Req->argc == 1 && quitmsg[0] != '\"') {
673                         /* " " to avoid confusion */
674                         strlcpy(quitmsg, "\"", sizeof quitmsg);
675                         strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
676                         strlcat(quitmsg, "\"", sizeof quitmsg );
677                 }
678
679                 /* User, Service, or not yet registered */
680                 Conn_Close(Client_Conn(Client), "Got QUIT command",
681                            Req->argc == 1 ? quitmsg : NULL, true);
682
683                 return DISCONNECTED;
684         }
685 } /* IRC_QUIT */
686
687 #ifndef STRICT_RFC
688
689 /**
690  * Handler for HTTP command, e.g. GET and POST
691  *
692  * We handle these commands here to avoid the quite long timeout when
693  * some user tries to access this IRC daemon using an web browser ...
694  *
695  * @param Client The client from which this command has been received.
696  * @param Req Request structure with prefix and all parameters.
697  * @return CONNECTED or DISCONNECTED.
698  */
699 GLOBAL bool
700 IRC_QUIT_HTTP( CLIENT *Client, REQUEST *Req )
701 {
702         Req->argc = 1;
703         Req->argv[0] = "Oops, HTTP request received? This is IRC!";
704         return IRC_QUIT(Client, Req);
705 } /* IRC_QUIT_HTTP */
706
707 #endif
708
709 /**
710  * Handler for the IRC "PING" command.
711  *
712  * @param Client The client from which this command has been received.
713  * @param Req Request structure with prefix and all parameters.
714  * @return CONNECTED or DISCONNECTED.
715  */
716 GLOBAL bool
717 IRC_PING(CLIENT *Client, REQUEST *Req)
718 {
719         CLIENT *target, *from;
720
721         assert(Client != NULL);
722         assert(Req != NULL);
723
724         if (Req->argc < 1)
725                 return IRC_WriteErrClient(Client, ERR_NOORIGIN_MSG,
726                                           Client_ID(Client));
727 #ifdef STRICT_RFC
728         /* Don't ignore additional arguments when in "strict" mode */
729         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
730 #endif
731
732         if (Req->argc > 1) {
733                 /* A target has been specified ... */
734                 target = Client_Search(Req->argv[1]);
735
736                 if (!target || Client_Type(target) != CLIENT_SERVER)
737                         return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
738                                         Client_ID(Client), Req->argv[1]);
739
740                 if (target != Client_ThisServer()) {
741                         /* Ok, we have to forward the PING */
742                         if (Client_Type(Client) == CLIENT_SERVER)
743                                 from = Client_Search(Req->prefix);
744                         else
745                                 from = Client;
746                         if (!from)
747                                 return IRC_WriteErrClient(Client,
748                                                 ERR_NOSUCHSERVER_MSG,
749                                                 Client_ID(Client), Req->prefix);
750
751                         return IRC_WriteStrClientPrefix(target, from,
752                                         "PING %s :%s", Req->argv[0],
753                                         Req->argv[1] );
754                 }
755         }
756
757         if (Client_Type(Client) == CLIENT_SERVER) {
758                 if (Req->prefix)
759                         from = Client_Search(Req->prefix);
760                 else
761                         from = Client;
762         } else
763                 from = Client_ThisServer();
764         if (!from)
765                 return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
766                                         Client_ID(Client), Req->prefix);
767
768         Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
769             Client_Conn(Client));
770
771 #ifdef STRICT_RFC
772         return IRC_WriteStrClient(Client, "PONG %s :%s",
773                 Client_ID(from), Client_ID(Client));
774 #else
775         /* Some clients depend on the argument being returned in the PONG
776          * reply (not mentioned in any RFC, though) */
777         return IRC_WriteStrClient(Client, "PONG %s :%s",
778                 Client_ID(from), Req->argv[0]);
779 #endif
780 } /* IRC_PING */
781
782 /**
783  * Handler for the IRC "PONG" command.
784  *
785  * @param Client The client from which this command has been received.
786  * @param Req Request structure with prefix and all parameters.
787  * @return CONNECTED or DISCONNECTED.
788  */
789 GLOBAL bool
790 IRC_PONG(CLIENT *Client, REQUEST *Req)
791 {
792         CLIENT *target, *from;
793         CONN_ID conn;
794 #ifndef STRICT_RFC
795         long auth_ping;
796 #endif
797         char *s;
798
799         assert(Client != NULL);
800         assert(Req != NULL);
801
802         /* Wrong number of arguments? */
803         if (Req->argc < 1) {
804                 if (Client_Type(Client) == CLIENT_USER)
805                         return IRC_WriteErrClient(Client, ERR_NOORIGIN_MSG,
806                                                   Client_ID(Client));
807                 else
808                         return CONNECTED;
809         }
810         if (Client_Type(Client) == CLIENT_USER) {
811                 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
812         }
813
814         /* Forward? */
815         if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
816                 target = Client_Search(Req->argv[0]);
817                 if (!target)
818                         return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
819                                         Client_ID(Client), Req->argv[0]);
820
821                 from = Client_Search(Req->prefix);
822
823                 if (target != Client_ThisServer() && target != from) {
824                         /* Ok, we have to forward the message. */
825                         if (!from)
826                                 return IRC_WriteErrClient(Client,
827                                                 ERR_NOSUCHSERVER_MSG,
828                                                 Client_ID(Client), Req->prefix);
829
830                         if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
831                                 s = Client_ID(from);
832                         else
833                                 s = Req->argv[0];
834                         return IRC_WriteStrClientPrefix(target, from,
835                                  "PONG %s :%s", s, Req->argv[1]);
836                 }
837         }
838
839         /* The connection timestamp has already been updated when the data has
840          * been read from so socket, so we don't need to update it here. */
841
842         conn = Client_Conn(Client);
843
844 #ifndef STRICT_RFC
845         /* Check authentication PING-PONG ... */
846         auth_ping = Conn_GetAuthPing(conn);
847         if (auth_ping) {
848                 LogDebug("AUTH PONG: waiting for token \"%ld\", got \"%s\" ...",
849                          auth_ping, Req->argv[0]);
850                 if (auth_ping == atol(Req->argv[0])) {
851                         Conn_SetAuthPing(conn, 0);
852                         if (Client_Type(Client) == CLIENT_WAITAUTHPING)
853                                 Login_User(Client);
854                 } else
855                         if (!IRC_WriteStrClient(Client,
856                                         "NOTICE %s :To connect, type /QUOTE PONG %ld",
857                                         Client_ID(Client), auth_ping))
858                                 return DISCONNECTED;
859         }
860 #endif
861
862         if (Client_Type(Client) == CLIENT_SERVER && Conn_LastPing(conn) == 0) {
863                 Log(LOG_INFO,
864                     "Synchronization with \"%s\" done (connection %d): %ld second%s [%ld users, %ld channels].",
865                     Client_ID(Client), conn, time(NULL) - Conn_GetSignon(conn),
866                     time(NULL) - Conn_GetSignon(conn) == 1 ? "" : "s",
867                     Client_UserCount(), Channel_CountVisible(NULL));
868                 Conn_UpdatePing(conn);
869         } else
870                 LogDebug("Connection %d: received PONG. Lag: %ld seconds.",
871                          conn, time(NULL) - Conn_LastPing(conn));
872
873         return CONNECTED;
874 } /* IRC_PONG */
875
876 /**
877  * Change the nickname of a client.
878  *
879  * @param Origin The client which caused the nickname change.
880  * @param Target The client of which the nickname should be changed.
881  * @param NewNick The new nickname.
882  */
883 static void
884 Change_Nick(CLIENT *Origin, CLIENT *Target, char *NewNick, bool InformClient)
885 {
886         if (Client_Conn(Target) > NONE) {
887                 /* Local client */
888                 Log(LOG_INFO,
889                     "%s \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
890                     Client_TypeText(Target), Client_Mask(Target),
891                     Client_Conn(Target), Client_ID(Target), NewNick);
892                 Conn_UpdateIdle(Client_Conn(Target));
893         } else {
894                 /* Remote client */
895                 LogDebug("%s \"%s\" changed nick: \"%s\" -> \"%s\".",
896                          Client_TypeText(Target),
897                          Client_Mask(Target), Client_ID(Target), NewNick);
898         }
899
900         /* Inform all servers and users (which have to know) of the new name */
901         if (InformClient) {
902                 IRC_WriteStrClientPrefix(Target, Target, "NICK :%s", NewNick);
903                 IRC_WriteStrServersPrefix(NULL, Target, "NICK :%s", NewNick);
904         } else
905                 IRC_WriteStrServersPrefix(Origin, Target, "NICK :%s", NewNick);
906         IRC_WriteStrRelatedPrefix(Target, Target, false, "NICK :%s", NewNick);
907
908         /* Register old nickname for WHOWAS queries */
909         Client_RegisterWhowas(Target);
910
911         /* Save new nickname */
912         Client_SetID(Target, NewNick);
913 }
914
915 /* -eof- */