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