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