]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-login.c
New functions Client_[Set]OrigUser() to get/set user specified by peer
[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                 Client_SetOrigUser(Client, Req->argv[0]);
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_SetOrigUser(c, Req->argv[0]);
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_SetOrigUser(Client, Req->argv[1]);
581         Client_SetHostname(Client, Req->argv[2]);
582         return CONNECTED;
583 } /* IRC_WEBIRC */
584
585
586 GLOBAL bool
587 IRC_QUIT( CLIENT *Client, REQUEST *Req )
588 {
589         CLIENT *target;
590         char quitmsg[LINE_LEN];
591
592         assert( Client != NULL );
593         assert( Req != NULL );
594
595         /* Wrong number of arguments? */
596         if( Req->argc > 1 )
597                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
598
599         if (Req->argc == 1)
600                 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
601
602         if ( Client_Type( Client ) == CLIENT_SERVER )
603         {
604                 /* Server */
605                 target = Client_Search( Req->prefix );
606                 if( ! target )
607                 {
608                         Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
609                         return CONNECTED;
610                 }
611
612                 Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
613
614                 return CONNECTED;
615         }
616         else
617         {
618                 if (Req->argc == 1 && quitmsg[0] != '\"') {
619                         /* " " to avoid confusion */
620                         strlcpy(quitmsg, "\"", sizeof quitmsg);
621                         strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
622                         strlcat(quitmsg, "\"", sizeof quitmsg );
623                 }
624
625                 /* User, Service, or not yet registered */
626                 Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
627
628                 return DISCONNECTED;
629         }
630 } /* IRC_QUIT */
631
632
633 GLOBAL bool
634 IRC_PING(CLIENT *Client, REQUEST *Req)
635 {
636         CLIENT *target, *from;
637
638         assert(Client != NULL);
639         assert(Req != NULL);
640
641         if (Req->argc < 1)
642                 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
643                                           Client_ID(Client));
644 #ifdef STRICT_RFC
645         /* Don't ignore additional arguments when in "strict" mode */
646         if (Req->argc > 2)
647                  return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
648                                            Client_ID(Client), Req->command);
649 #endif
650
651         if (Req->argc > 1) {
652                 /* A target has been specified ... */
653                 target = Client_Search(Req->argv[1]);
654
655                 if (!target || Client_Type(target) != CLIENT_SERVER)
656                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
657                                         Client_ID(Client), Req->argv[1]);
658
659                 if (target != Client_ThisServer()) {
660                         /* Ok, we have to forward the PING */
661                         if (Client_Type(Client) == CLIENT_SERVER)
662                                 from = Client_Search(Req->prefix);
663                         else
664                                 from = Client;
665                         if (!from)
666                                 return IRC_WriteStrClient(Client,
667                                                 ERR_NOSUCHSERVER_MSG,
668                                                 Client_ID(Client), Req->prefix);
669
670                         return IRC_WriteStrClientPrefix(target, from,
671                                         "PING %s :%s", Req->argv[0],
672                                         Req->argv[1] );
673                 }
674         }
675
676         if (Client_Type(Client) == CLIENT_SERVER) {
677                 if (Req->prefix)
678                         from = Client_Search(Req->prefix);
679                 else
680                         from = Client;
681         } else
682                 from = Client_ThisServer();
683         if (!from)
684                 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
685                                         Client_ID(Client), Req->prefix);
686
687         Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
688             Client_Conn(Client));
689
690 #ifdef STRICT_RFC
691         return IRC_WriteStrClient(Client, "PONG %s :%s",
692                 Client_ID(from), Client_ID(Client));
693 #else
694         /* Some clients depend on the argument being returned in the PONG
695          * reply (not mentioned in any RFC, though) */
696         return IRC_WriteStrClient(Client, "PONG %s :%s",
697                 Client_ID(from), Req->argv[0]);
698 #endif
699 } /* IRC_PING */
700
701
702 GLOBAL bool
703 IRC_PONG(CLIENT *Client, REQUEST *Req)
704 {
705         CLIENT *target, *from;
706         char *s;
707
708         assert(Client != NULL);
709         assert(Req != NULL);
710
711         /* Wrong number of arguments? */
712         if (Req->argc < 1)
713                 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
714                                           Client_ID(Client));
715         if (Req->argc > 2)
716                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
717                                           Client_ID(Client), Req->command);
718
719         /* Forward? */
720         if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
721                 target = Client_Search(Req->argv[0]);
722                 if (!target)
723                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
724                                         Client_ID(Client), Req->argv[0]);
725
726                 from = Client_Search(Req->prefix);
727
728                 if (target != Client_ThisServer() && target != from) {
729                         /* Ok, we have to forward the message. */
730                         if (!from)
731                                 return IRC_WriteStrClient(Client,
732                                                 ERR_NOSUCHSERVER_MSG,
733                                                 Client_ID(Client), Req->prefix);
734
735                         if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
736                                 s = Client_ID(from);
737                         else
738                                 s = Req->argv[0];
739                         return IRC_WriteStrClientPrefix(target, from,
740                                  "PONG %s :%s", s, Req->argv[1]);
741                 }
742         }
743
744         /* The connection timestamp has already been updated when the data has
745          * been read from so socket, so we don't need to update it here. */
746 #ifdef DEBUG
747         if (Client_Conn(Client) > NONE)
748                 Log(LOG_DEBUG,
749                         "Connection %d: received PONG. Lag: %ld seconds.",
750                         Client_Conn(Client),
751                         time(NULL) - Conn_LastPing(Client_Conn(Client)));
752         else
753                  Log(LOG_DEBUG,
754                         "Connection %d: received PONG.", Client_Conn(Client));
755 #endif
756         return CONNECTED;
757 } /* IRC_PONG */
758
759
760 static bool
761 Hello_User(CLIENT * Client)
762 {
763         assert(Client != NULL);
764
765         /* Check password ... */
766         if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
767                 /* Bad password! */
768                 Log(LOG_ERR,
769                     "Client \"%s\" rejected (connection %d): Bad password!",
770                     Client_Mask(Client), Client_Conn(Client));
771                 Conn_Close(Client_Conn(Client), NULL, "Bad password", true);
772                 return DISCONNECTED;
773         }
774
775         Introduce_Client(NULL, Client, CLIENT_USER);
776
777         if (!IRC_WriteStrClient
778             (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
779                 return false;
780         if (!IRC_WriteStrClient
781             (Client, RPL_YOURHOST_MSG, Client_ID(Client),
782              Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
783              TARGET_VENDOR, TARGET_OS))
784                 return false;
785         if (!IRC_WriteStrClient
786             (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
787                 return false;
788         if (!IRC_WriteStrClient
789             (Client, RPL_MYINFO_MSG, Client_ID(Client),
790              Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
791              CHANMODES))
792                 return false;
793
794         /* Features supported by this server (005 numeric, ISUPPORT),
795          * see <http://www.irc.org/tech_docs/005.html> for details. */
796         if (!IRC_Send_ISUPPORT(Client))
797                 return DISCONNECTED;
798
799         if (!IRC_Send_LUSERS(Client))
800                 return DISCONNECTED;
801         if (!IRC_Show_MOTD(Client))
802                 return DISCONNECTED;
803
804         /* Suspend the client for a second ... */
805         IRC_SetPenalty(Client, 1);
806
807         return CONNECTED;
808 } /* Hello_User */
809
810
811 static void
812 Kill_Nick( char *Nick, char *Reason )
813 {
814         REQUEST r;
815
816         assert( Nick != NULL );
817         assert( Reason != NULL );
818
819         r.prefix = (char *)Client_ThisServer( );
820         r.argv[0] = Nick;
821         r.argv[1] = Reason;
822         r.argc = 2;
823
824         Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
825         IRC_KILL( Client_ThisServer( ), &r );
826 } /* Kill_Nick */
827
828
829 static void
830 Introduce_Client(CLIENT *From, CLIENT *Client, int Type)
831 {
832         /* Set client type (user or service) */
833         Client_SetType(Client, Type);
834
835         if (From) {
836                 if (Conf_IsService(Conf_GetServer(Client_Conn(From)),
837                                    Client_ID(Client)))
838                         Client_SetType(Client, CLIENT_SERVICE);
839                 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
840                          Client_TypeText(Client), Client_Mask(Client),
841                          Client_Modes(Client), Client_ID(From),
842                          Client_ID(Client_Introducer(Client)),
843                          Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
844         } else {
845                 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
846                     Client_TypeText(Client), Client_Mask(Client),
847                     Client_Conn(Client));
848                 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
849                                  Client_ID(Client), Client_User(Client),
850                                  Client_Hostname(Client),
851                                  Conn_IPA(Client_Conn(Client)),
852                                  Client_TypeText(Client));
853         }
854
855         /* Inform other servers */
856         IRC_WriteStrServersPrefixFlag_CB(From,
857                                 From != NULL ? From : Client_ThisServer(),
858                                 '\0', cb_introduceClient, (void *)Client);
859 } /* Introduce_Client */
860
861
862 static void
863 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
864 {
865         CLIENT *c = (CLIENT *)data;
866         CONN_ID conn;
867         char *modes, *user, *host;
868
869         modes = Client_Modes(c);
870         user = Client_User(c) ? Client_User(c) : "-";
871         host = Client_Hostname(c) ? Client_Hostname(c) : "-";
872
873         conn = Client_Conn(To);
874         if (Conn_Options(conn) & CONN_RFC1459) {
875                 /* RFC 1459 mode: separate NICK and USER commands */
876                 Conn_WriteStr(conn, "NICK %s :%d", Client_ID(c),
877                               Client_Hops(c) + 1);
878                 Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
879                               Client_ID(c), user, host,
880                               Client_ID(Client_Introducer(c)), Client_Info(c));
881                 if (modes[0])
882                         Conn_WriteStr(conn, ":%s MODE %s +%s",
883                                       Client_ID(c), Client_ID(c), modes);
884         } else {
885                 /* RFC 2813 mode: one combined NICK or SERVICE command */
886                 if (Client_Type(c) == CLIENT_SERVICE
887                     && strchr(Client_Flags(To), 'S'))
888                         IRC_WriteStrClientPrefix(To, Prefix,
889                                          "SERVICE %s %d * +%s %d :%s",
890                                          Client_Mask(c),
891                                          Client_MyToken(Client_Introducer(c)),
892                                          Client_Modes(c), Client_Hops(c) + 1,
893                                          Client_Info(c));
894                 else
895                         IRC_WriteStrClientPrefix(To, Prefix,
896                                          "NICK %s %d %s %s %d +%s :%s",
897                                          Client_ID(c), Client_Hops(c) + 1,
898                                          user, host,
899                                          Client_MyToken(Client_Introducer(c)),
900                                          modes, Client_Info(c));
901         }
902 } /* cb_introduceClient */
903
904
905 /* -eof- */