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