]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-login.c
Allow ngIRCd to detect services connected to an "virtual services server".
[ngircd-alex.git] / src / ngircd / irc-login.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
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  * Login and logout
12  */
13
14
15 #include "portab.h"
16
17 #include "imp.h"
18 #include <assert.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <strings.h>
23
24 #include "ngircd.h"
25 #include "resolve.h"
26 #include "conn-func.h"
27 #include "conf.h"
28 #include "client.h"
29 #include "channel.h"
30 #include "log.h"
31 #include "messages.h"
32 #include "parse.h"
33 #include "irc.h"
34 #include "irc-info.h"
35 #include "irc-write.h"
36
37 #include "exp.h"
38 #include "irc-login.h"
39
40
41 static bool Hello_User PARAMS(( CLIENT *Client ));
42 static void Kill_Nick PARAMS(( char *Nick, char *Reason ));
43 static void Introduce_Client PARAMS((CLIENT *To, CLIENT *Client));
44 static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
45                                        void *i));
46
47
48 /**
49  * Handler for the IRC command "PASS".
50  * See RFC 2813 section 4.1.1, and RFC 2812 section 3.1.1.
51  */
52 GLOBAL bool
53 IRC_PASS( CLIENT *Client, REQUEST *Req )
54 {
55         char *type, *orig_flags;
56         int protohigh, protolow;
57
58         assert( Client != NULL );
59         assert( Req != NULL );
60
61         /* Return an error if this is not a local client */
62         if (Client_Conn(Client) <= NONE)
63                 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
64                                           Client_ID(Client), Req->command);
65
66         if (Client_Type(Client) == CLIENT_UNKNOWN && Req->argc == 1) {
67                 /* Not yet registered "unknown" connection, PASS with one
68                  * argument: either a regular client, service, or server
69                  * using the old RFC 1459 section 4.1.1 syntax. */
70                 LogDebug("Connection %d: got PASS command (RFC 1459) ...",
71                          Client_Conn(Client));
72         } else if ((Client_Type(Client) == CLIENT_UNKNOWN ||
73                     Client_Type(Client) == CLIENT_UNKNOWNSERVER) &&
74                    (Req->argc == 3 || Req->argc == 4)) {
75                 /* Not yet registered "unknown" connection or outgoing server
76                  * link, PASS with three or four argument: server using the
77                  * RFC 2813 section 4.1.1 syntax. */
78                 LogDebug("Connection %d: got PASS command (RFC 2813, new server link) ...",
79                          Client_Conn(Client));
80         } else if (Client_Type(Client) == CLIENT_UNKNOWN ||
81                    Client_Type(Client) == CLIENT_UNKNOWNSERVER) {
82                 /* Unregistered connection, but wrong number of arguments: */
83                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
84                                           Client_ID(Client), Req->command);
85         } else {
86                 /* Registered connection, PASS command is not allowed! */
87                 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
88                                           Client_ID(Client));
89         }
90
91         Client_SetPassword(Client, Req->argv[0]);
92
93         /* Protocol version */
94         if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
95                 int c2, c4;
96
97                 c2 = Req->argv[1][2];
98                 c4 = Req->argv[1][4];
99
100                 Req->argv[1][4] = '\0';
101                 protolow = atoi(&Req->argv[1][2]);
102                 Req->argv[1][2] = '\0';
103                 protohigh = atoi(Req->argv[1]);
104
105                 Req->argv[1][2] = c2;
106                 Req->argv[1][4] = c4;
107
108                 Client_SetType(Client, CLIENT_GOTPASS_2813);
109         } else {
110                 protohigh = protolow = 0;
111                 Client_SetType(Client, CLIENT_GOTPASS);
112         }
113
114         /* Protocol type, see doc/Protocol.txt */
115         if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
116                 type = &Req->argv[1][4];
117         else
118                 type = NULL;
119
120         /* Protocol flags/options */
121         if (Req->argc >= 4)
122                 orig_flags = Req->argv[3];
123         else
124                 orig_flags = "";
125
126         /* Implementation, version and IRC+ flags */
127         if (Req->argc >= 3) {
128                 char *impl, *ptr, *serverver, *flags;
129
130                 impl = Req->argv[2];
131                 ptr = strchr(impl, '|');
132                 if (ptr)
133                         *ptr = '\0';
134
135                 if (type && strcmp(type, PROTOIRCPLUS) == 0) {
136                         /* The peer seems to be a server which supports the
137                          * IRC+ protocol (see doc/Protocol.txt). */
138                         serverver = ptr + 1;
139                         flags = strchr(serverver, ':');
140                         if (flags) {
141                                 *flags = '\0';
142                                 flags++;
143                         } else
144                                 flags = "";
145                         Log(LOG_INFO,
146                             "Peer announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
147                             impl, serverver, protohigh, protolow, flags);
148                 } else {
149                         /* The peer seems to be a server supporting the
150                          * "original" IRC protocol (RFC 2813). */
151                         serverver = "";
152                         if (strchr(orig_flags, 'Z'))
153                                 flags = "Z";
154                         else
155                                 flags = "";
156                         Log(LOG_INFO,
157                             "Peer announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
158                             impl, protohigh, protolow, flags);
159                 }
160                 Client_SetFlags(Client, flags);
161         }
162
163         return CONNECTED;
164 } /* IRC_PASS */
165
166
167 /**
168  * IRC "NICK" command.
169  * This function implements the IRC command "NICK" which is used to register
170  * with the server, to change already registered nicknames and to introduce
171  * new users which are connected to other servers.
172  */
173 GLOBAL bool
174 IRC_NICK( CLIENT *Client, REQUEST *Req )
175 {
176         CLIENT *intr_c, *target, *c;
177         char *nick, *user, *hostname, *modes, *info;
178         int token, hops;
179
180         assert( Client != NULL );
181         assert( Req != NULL );
182
183 #ifndef STRICT_RFC
184         /* Some IRC clients, for example BitchX, send the NICK and USER
185          * commands in the wrong order ... */
186         if( Client_Type( Client ) == CLIENT_UNKNOWN
187             || Client_Type( Client ) == CLIENT_GOTPASS
188             || Client_Type( Client ) == CLIENT_GOTNICK
189             || Client_Type( Client ) == CLIENT_GOTUSER
190             || Client_Type( Client ) == CLIENT_USER
191             || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
192 #else
193         if( Client_Type( Client ) == CLIENT_UNKNOWN
194             || Client_Type( Client ) == CLIENT_GOTPASS
195             || Client_Type( Client ) == CLIENT_GOTNICK
196             || Client_Type( Client ) == CLIENT_USER
197             || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
198 #endif
199         {
200                 /* User registration or change of nickname */
201
202                 /* Wrong number of arguments? */
203                 if( Req->argc != 1 )
204                         return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
205                                                    Client_ID( Client ),
206                                                    Req->command );
207
208                 /* Search "target" client */
209                 if( Client_Type( Client ) == CLIENT_SERVER )
210                 {
211                         target = Client_Search( Req->prefix );
212                         if( ! target )
213                                 return IRC_WriteStrClient( Client,
214                                                            ERR_NOSUCHNICK_MSG,
215                                                            Client_ID( Client ),
216                                                            Req->argv[0] );
217                 }
218                 else
219                 {
220                         /* Is this a restricted client? */
221                         if( Client_HasMode( Client, 'r' ))
222                                 return IRC_WriteStrClient( Client,
223                                                            ERR_RESTRICTED_MSG,
224                                                            Client_ID( Client ));
225
226                         target = Client;
227                 }
228
229 #ifndef STRICT_RFC
230                 /* If the clients tries to change to its own nickname we won't
231                  * do anything. This is how the original ircd behaves and some
232                  * clients (for example Snak) expect it to be like this.
233                  * But I doubt that this is "really the right thing" ... */
234                 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 )
235                         return CONNECTED;
236 #endif
237
238                 /* Check that the new nickname is available. Special case:
239                  * the client only changes from/to upper to lower case. */
240                 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
241                 {
242                         if( ! Client_CheckNick( target, Req->argv[0] ))
243                                 return CONNECTED;
244                 }
245
246                 if(( Client_Type( target ) != CLIENT_USER )
247                    && ( Client_Type( target ) != CLIENT_SERVER ))
248                 {
249                         /* New client */
250                         Log( LOG_DEBUG, "Connection %d: got valid NICK command ...", 
251                              Client_Conn( Client ));
252
253                         /* Register new nickname of this client */
254                         Client_SetID( target, Req->argv[0] );
255
256                         /* If we received a valid USER command already then
257                          * register the new client! */
258                         if( Client_Type( Client ) == CLIENT_GOTUSER )
259                                 return Hello_User( Client );
260                         else
261                                 Client_SetType( Client, CLIENT_GOTNICK );
262                 }
263                 else
264                 {
265                         /* Nickname change */
266                         if (Client_Conn(target) > NONE) {
267                                 /* Local client */
268                                 Log(LOG_INFO,
269                                     "User \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
270                                     Client_Mask(target), Client_Conn(target),
271                                     Client_ID(target), Req->argv[0]);
272                                 Conn_UpdateIdle(Client_Conn(target));
273                         }
274                         else
275                         {
276                                 /* Remote client */
277                                 Log( LOG_DEBUG,
278                                      "User \"%s\" changed nick: \"%s\" -> \"%s\".",
279                                      Client_Mask( target ), Client_ID( target ),
280                                      Req->argv[0] );
281                         }
282
283                         /* Inform all users and servers (which have to know)
284                          * of this nickname change */
285                         if( Client_Type( Client ) == CLIENT_USER )
286                                 IRC_WriteStrClientPrefix( Client, Client,
287                                                           "NICK :%s",
288                                                           Req->argv[0] );
289                         IRC_WriteStrServersPrefix( Client, target,
290                                                    "NICK :%s", Req->argv[0] );
291                         IRC_WriteStrRelatedPrefix( target, target, false,
292                                                    "NICK :%s", Req->argv[0] );
293
294                         /* Register old nickname for WHOWAS queries */
295                         Client_RegisterWhowas( target );
296
297                         /* Save new nickname */
298                         Client_SetID( target, Req->argv[0] );
299
300                         IRC_SetPenalty( target, 2 );
301                 }
302
303                 return CONNECTED;
304         } else if(Client_Type(Client) == CLIENT_SERVER ||
305                   Client_Type(Client) == CLIENT_SERVICE) {
306                 /* Server or service introduces new client */
307
308                 /* Bad number of parameters? */
309                 if (Req->argc != 2 && Req->argc != 7)
310                         return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
311                                                   Client_ID(Client), Req->command);
312
313                 if (Req->argc >= 7) {
314                         /* RFC 2813 compatible syntax */
315                         nick = Req->argv[0];
316                         hops = atoi(Req->argv[1]);
317                         user = Req->argv[2];
318                         hostname = Req->argv[3];
319                         token = atoi(Req->argv[4]);
320                         modes = Req->argv[5] + 1;
321                         info = Req->argv[6];
322                 } else {
323                         /* RFC 1459 compatible syntax */
324                         nick = Req->argv[0];
325                         hops = 1;
326                         user = Req->argv[0];
327                         hostname = Client_ID(Client);
328                         token = atoi(Req->argv[1]);
329                         modes = "";
330                         info = Req->argv[0];
331                 }
332
333                 /* Nick ueberpruefen */
334                 c = Client_Search(nick);
335                 if(c) {
336                         /* Der neue Nick ist auf diesem Server bereits registriert:
337                          * sowohl der neue, als auch der alte Client muessen nun
338                          * disconnectiert werden. */
339                         Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
340                         Kill_Nick( Req->argv[0], "Nick collision" );
341                         return CONNECTED;
342                 }
343
344                 /* Server, zu dem der Client connectiert ist, suchen */
345                 intr_c = Client_GetFromToken(Client, token);
346                 if( ! intr_c )
347                 {
348                         Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
349                         Kill_Nick( Req->argv[0], "Unknown server" );
350                         return CONNECTED;
351                 }
352
353                 /* Neue Client-Struktur anlegen */
354                 c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
355                                          token, modes, info, true);
356                 if( ! c )
357                 {
358                         /* Eine neue Client-Struktur konnte nicht angelegt werden.
359                          * Der Client muss disconnectiert werden, damit der Netz-
360                          * status konsistent bleibt. */
361                         Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
362                         Kill_Nick( Req->argv[0], "Server error" );
363                         return CONNECTED;
364                 }
365
366                 /* RFC 2813: client is now fully registered, inform all the
367                  * other servers about the new user.
368                  * RFC 1459: announce the new client only after receiving the
369                  * USER command, first we need more information! */
370                 if (Req->argc < 7) {
371                         LogDebug("Client \"%s\" is beeing registered (RFC 1459) ...",
372                                  Client_Mask(c));
373                         Client_SetType(c, CLIENT_GOTNICK);
374                 } else
375                         Introduce_Client(Client, c);
376
377                 return CONNECTED;
378         }
379         else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
380 } /* IRC_NICK */
381
382
383 /**
384  * Handler for the IRC command "USER".
385  */
386 GLOBAL bool
387 IRC_USER(CLIENT * Client, REQUEST * Req)
388 {
389         CLIENT *c;
390 #ifdef IDENTAUTH
391         char *ptr;
392 #endif
393
394         assert(Client != NULL);
395         assert(Req != NULL);
396
397         if (Client_Type(Client) == CLIENT_GOTNICK ||
398 #ifndef STRICT_RFC
399             Client_Type(Client) == CLIENT_UNKNOWN ||
400 #endif
401             Client_Type(Client) == CLIENT_GOTPASS)
402         {
403                 /* New connection */
404                 if (Req->argc != 4)
405                         return IRC_WriteStrClient(Client,
406                                                   ERR_NEEDMOREPARAMS_MSG,
407                                                   Client_ID(Client),
408                                                   Req->command);
409
410                 /* User name */
411 #ifdef IDENTAUTH
412                 ptr = Client_User(Client);
413                 if (!ptr || !*ptr || *ptr == '~')
414                         Client_SetUser(Client, Req->argv[0], false);
415 #else
416                 Client_SetUser(Client, Req->argv[0], false);
417 #endif
418
419                 /* "Real name" or user info text: Don't set it to the empty
420                  * string, the original ircd can't deal with such "real names"
421                  * (e. g. "USER user * * :") ... */
422                 if (*Req->argv[3])
423                         Client_SetInfo(Client, Req->argv[3]);
424                 else
425                         Client_SetInfo(Client, "-");
426
427                 LogDebug("Connection %d: got valid USER command ...",
428                     Client_Conn(Client));
429                 if (Client_Type(Client) == CLIENT_GOTNICK)
430                         return Hello_User(Client);
431                 else
432                         Client_SetType(Client, CLIENT_GOTUSER);
433                 return CONNECTED;
434
435         } else if (Client_Type(Client) == CLIENT_SERVER ||
436                    Client_Type(Client) == CLIENT_SERVICE) {
437                 /* Server/service updating an user */
438                 if (Req->argc != 4)
439                         return IRC_WriteStrClient(Client,
440                                                   ERR_NEEDMOREPARAMS_MSG,
441                                                   Client_ID(Client),
442                                                   Req->command);
443                 c = Client_Search(Req->prefix);
444                 if (!c)
445                         return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
446                                                   Client_ID(Client),
447                                                   Req->prefix);
448
449                 Client_SetUser(c, Req->argv[0], true);
450                 Client_SetHostname(c, Req->argv[1]);
451                 Client_SetInfo(c, Req->argv[3]);
452
453                 LogDebug("Connection %d: got valid USER command for \"%s\".",
454                          Client_Conn(Client), Client_Mask(c));
455
456                 /* RFC 1459 style user registration?
457                  * Introduce client to network: */
458                 if (Client_Type(c) == CLIENT_GOTNICK)
459                         Introduce_Client(Client, c);
460
461                 return CONNECTED;
462         } else if (Client_Type(Client) == CLIENT_USER) {
463                 /* Already registered connection */
464                 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
465                                           Client_ID(Client));
466         } else {
467                 /* Unexpected/invalid connection state? */
468                 return IRC_WriteStrClient(Client, ERR_NOTREGISTERED_MSG,
469                                           Client_ID(Client));
470         }
471 } /* IRC_USER */
472
473
474 /**
475  * Service registration.
476  * ngIRCd does not support services at the moment, so this function is a
477  * dummy that returns ERR_ERRONEUSNICKNAME on each call.
478  */
479 GLOBAL bool
480 IRC_SERVICE(CLIENT *Client, REQUEST *Req)
481 {
482         assert(Client != NULL);
483         assert(Req != NULL);
484
485         if (Client_Type(Client) != CLIENT_GOTPASS)
486                 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
487                                           Client_ID(Client));
488
489         if (Req->argc != 6)
490                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
491                                           Client_ID(Client), Req->command);
492
493         return IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
494                                   Client_ID(Client), Req->argv[0]);
495 } /* IRC_SERVICE */
496
497
498 GLOBAL bool
499 IRC_QUIT( CLIENT *Client, REQUEST *Req )
500 {
501         CLIENT *target;
502         char quitmsg[LINE_LEN];
503
504         assert( Client != NULL );
505         assert( Req != NULL );
506
507         /* Wrong number of arguments? */
508         if( Req->argc > 1 )
509                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
510
511         if (Req->argc == 1)
512                 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
513
514         if ( Client_Type( Client ) == CLIENT_SERVER )
515         {
516                 /* Server */
517                 target = Client_Search( Req->prefix );
518                 if( ! target )
519                 {
520                         /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
521                         Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
522                         return CONNECTED;
523                 }
524
525                 Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
526
527                 return CONNECTED;
528         }
529         else
530         {
531                 if (Req->argc == 1 && quitmsg[0] != '\"') {
532                         /* " " to avoid confusion */
533                         strlcpy(quitmsg, "\"", sizeof quitmsg);
534                         strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
535                         strlcat(quitmsg, "\"", sizeof quitmsg );
536                 }
537
538                 /* User, Service, oder noch nicht registriert */
539                 Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
540
541                 return DISCONNECTED;
542         }
543 } /* IRC_QUIT */
544
545
546 GLOBAL bool
547 IRC_PING(CLIENT *Client, REQUEST *Req)
548 {
549         CLIENT *target, *from;
550
551         assert(Client != NULL);
552         assert(Req != NULL);
553
554         /* Wrong number of arguments? */
555         if (Req->argc < 1)
556                 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
557                                           Client_ID(Client));
558 #ifdef STRICT_RFC
559         /* Don't ignore additional arguments when in "strict" mode */
560         if (Req->argc > 2)
561                  return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
562                                            Client_ID(Client), Req->command);
563 #endif
564
565         if (Req->argc > 1) {
566                 /* A target has been specified ... */
567                 target = Client_Search(Req->argv[1]);
568
569                 if (!target || Client_Type(target) != CLIENT_SERVER)
570                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
571                                         Client_ID(Client), Req->argv[1]);
572
573                 if (target != Client_ThisServer()) {
574                         /* Ok, we have to forward the PING */
575                         if (Client_Type(Client) == CLIENT_SERVER)
576                                 from = Client_Search(Req->prefix);
577                         else
578                                 from = Client;
579                         if (!from)
580                                 return IRC_WriteStrClient(Client,
581                                                 ERR_NOSUCHSERVER_MSG,
582                                                 Client_ID(Client), Req->prefix);
583
584                         return IRC_WriteStrClientPrefix(target, from,
585                                         "PING %s :%s", Req->argv[0],
586                                         Req->argv[1] );
587                 }
588         }
589
590         if (Client_Type(Client) == CLIENT_SERVER) {
591                 if (Req->prefix)
592                         from = Client_Search(Req->prefix);
593                 else
594                         from = Client;
595         } else
596                 from = Client_ThisServer();
597         if (!from)
598                 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
599                                         Client_ID(Client), Req->prefix);
600
601         Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
602             Client_Conn(Client));
603
604 #ifdef STRICT_RFC
605         return IRC_WriteStrClient(Client, "PONG %s :%s",
606                 Client_ID(from), Client_ID(Client));
607 #else
608         /* Some clients depend on the argument being returned in the PONG
609          * reply (not mentioned in any RFC, though) */
610         return IRC_WriteStrClient(Client, "PONG %s :%s",
611                 Client_ID(from), Req->argv[0]);
612 #endif
613 } /* IRC_PING */
614
615
616 GLOBAL bool
617 IRC_PONG(CLIENT *Client, REQUEST *Req)
618 {
619         CLIENT *target, *from;
620         char *s;
621
622         assert(Client != NULL);
623         assert(Req != NULL);
624
625         /* Wrong number of arguments? */
626         if (Req->argc < 1)
627                 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
628                                           Client_ID(Client));
629         if (Req->argc > 2)
630                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
631                                           Client_ID(Client), Req->command);
632
633         /* Forward? */
634         if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
635                 target = Client_Search(Req->argv[0]);
636                 if (!target)
637                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
638                                         Client_ID(Client), Req->argv[0]);
639
640                 from = Client_Search(Req->prefix);
641
642                 if (target != Client_ThisServer() && target != from) {
643                         /* Ok, we have to forward the message. */
644                         if (!from)
645                                 return IRC_WriteStrClient(Client,
646                                                 ERR_NOSUCHSERVER_MSG,
647                                                 Client_ID(Client), Req->prefix);
648
649                         if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
650                                 s = Client_ID(from);
651                         else
652                                 s = Req->argv[0];
653                         return IRC_WriteStrClientPrefix(target, from,
654                                  "PONG %s :%s", s, Req->argv[1]);
655                 }
656         }
657
658         /* The connection timestamp has already been updated when the data has
659          * been read from so socket, so we don't need to update it here. */
660
661         if (Client_Conn(Client) > NONE)
662                 Log(LOG_DEBUG,
663                         "Connection %d: received PONG. Lag: %ld seconds.",
664                         Client_Conn(Client),
665                         time(NULL) - Conn_LastPing(Client_Conn(Client)));
666         else
667                  Log(LOG_DEBUG,
668                         "Connection %d: received PONG.", Client_Conn(Client));
669
670         return CONNECTED;
671 } /* IRC_PONG */
672
673
674 static bool
675 Hello_User(CLIENT * Client)
676 {
677         assert(Client != NULL);
678
679         /* Check password ... */
680         if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
681                 /* Bad password! */
682                 Log(LOG_ERR,
683                     "User \"%s\" rejected (connection %d): Bad password!",
684                     Client_Mask(Client), Client_Conn(Client));
685                 Conn_Close(Client_Conn(Client), NULL, "Bad password", true);
686                 return DISCONNECTED;
687         }
688
689         Introduce_Client(NULL, Client);
690
691         if (!IRC_WriteStrClient
692             (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
693                 return false;
694         if (!IRC_WriteStrClient
695             (Client, RPL_YOURHOST_MSG, Client_ID(Client),
696              Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
697              TARGET_VENDOR, TARGET_OS))
698                 return false;
699         if (!IRC_WriteStrClient
700             (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
701                 return false;
702         if (!IRC_WriteStrClient
703             (Client, RPL_MYINFO_MSG, Client_ID(Client),
704              Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
705              CHANMODES))
706                 return false;
707
708         /* Features supported by this server (005 numeric, ISUPPORT),
709          * see <http://www.irc.org/tech_docs/005.html> for details. */
710         if (!IRC_Send_ISUPPORT(Client))
711                 return DISCONNECTED;
712
713         if (!IRC_Send_LUSERS(Client))
714                 return DISCONNECTED;
715         if (!IRC_Show_MOTD(Client))
716                 return DISCONNECTED;
717
718         /* Suspend the client for a second ... */
719         IRC_SetPenalty(Client, 1);
720
721         return CONNECTED;
722 } /* Hello_User */
723
724
725 static void
726 Kill_Nick( char *Nick, char *Reason )
727 {
728         REQUEST r;
729
730         assert( Nick != NULL );
731         assert( Reason != NULL );
732
733         r.prefix = (char *)Client_ThisServer( );
734         r.argv[0] = Nick;
735         r.argv[1] = Reason;
736         r.argc = 2;
737
738         Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
739         IRC_KILL( Client_ThisServer( ), &r );
740 } /* Kill_Nick */
741
742
743 static void
744 Introduce_Client(CLIENT *From, CLIENT *Client)
745 {
746         char *type;
747
748         Client_SetType(Client, CLIENT_USER);
749
750         if (From) {
751                 if (Conf_IsService(Conf_GetServer(Client_Conn(From)), Client_ID(Client))) {
752                         type = "Service";
753                 } else
754                         type = "User";
755                 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
756                          type, Client_Mask(Client), Client_Modes(Client),
757                          Client_ID(From), Client_ID(Client_Introducer(Client)),
758                          Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
759         } else
760                 Log(LOG_NOTICE, "User \"%s\" registered (connection %d).",
761                     Client_Mask(Client), Client_Conn(Client));
762
763         /* Inform other servers */
764         IRC_WriteStrServersPrefixFlag_CB(From,
765                                 From != NULL ? From : Client_ThisServer(),
766                                 '\0', cb_introduceClient, (void *)Client);
767 } /* Introduce_Client */
768
769
770 static void
771 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
772 {
773         CLIENT *c = (CLIENT *)data;
774         CONN_ID conn;
775         char *modes, *user, *host;
776
777         modes = Client_Modes(c);
778         user = Client_User(c) ? Client_User(c) : "-";
779         host = Client_Hostname(c) ? Client_Hostname(c) : "-";
780
781         conn = Client_Conn(To);
782         if (Conn_Options(conn) & CONN_RFC1459) {
783                 /* RFC 1459 mode: separate NICK and USER commands */
784                 Conn_WriteStr(conn, "NICK %s :%d", Client_ID(c),
785                               Client_Hops(c) + 1);
786                 Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
787                               Client_ID(c), user, host,
788                               Client_ID(Client_Introducer(c)), Client_Info(c));
789                 if (modes[0])
790                         Conn_WriteStr(conn, ":%s MODE %s +%s",
791                                       Client_ID(c), Client_ID(c), modes);
792         } else {
793                 /* RFC 2813 mode: one combined NICK command */
794                 IRC_WriteStrClientPrefix(To, Prefix,
795                                          "NICK %s %d %s %s %d +%s :%s",
796                                          Client_ID(c), Client_Hops(c) + 1,
797                                          user, host,
798                                          Client_MyToken(Client_Introducer(c)),
799                                          modes, Client_Info(c));
800         }
801 } /* cb_introduceClient */
802
803
804 /* -eof- */