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