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