2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
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.
26 #include "conn-func.h"
35 #include "irc-write.h"
38 #include "irc-login.h"
41 typedef struct _INTRO_INFO {
42 char *nick; /* Nick name */
43 int hopcount; /* Hop count */
44 char *user; /* User name */
45 char *host; /* Host name */
46 CLIENT *server; /* Server the client is connected to */
47 char *mode; /* User modes */
48 char *name; /* Real name */
52 static bool Hello_User PARAMS(( CLIENT *Client ));
53 static void Kill_Nick PARAMS(( char *Nick, char *Reason ));
54 static void Introduce_Client PARAMS((CLIENT *From, char *Nick,
55 const int HopCount, char *User,
56 char *Host, CLIENT *Server,
57 char *Mode, char *Name ));
58 static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
63 * Handler for the IRC command "PASS".
64 * See RFC 2813 section 4.1.1, and RFC 2812 section 3.1.1.
67 IRC_PASS( CLIENT *Client, REQUEST *Req )
69 char *type, *orig_flags;
70 int protohigh, protolow;
72 assert( Client != NULL );
73 assert( Req != NULL );
75 /* Return an error if this is not a local client */
76 if (Client_Conn(Client) <= NONE)
77 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
78 Client_ID(Client), Req->command);
80 if (Client_Type(Client) == CLIENT_UNKNOWN && Req->argc == 1) {
81 /* Not yet registered "unknown" connection, PASS with one
82 * argument: either a regular client, service, or server
83 * using the old RFC 1459 section 4.1.1 syntax. */
84 LogDebug("Connection %d: got PASS command ...",
86 } else if ((Client_Type(Client) == CLIENT_UNKNOWN ||
87 Client_Type(Client) == CLIENT_UNKNOWNSERVER) &&
88 (Req->argc == 3 || Req->argc == 4)) {
89 /* Not yet registered "unknown" connection or outgoing server
90 * link, PASS with three or four argument: server using the
91 * RFC 2813 section 4.1.1 syntax. */
92 LogDebug("Connection %d: got PASS command (new server link) ...",
94 } else if (Client_Type(Client) == CLIENT_UNKNOWN ||
95 Client_Type(Client) == CLIENT_UNKNOWNSERVER) {
96 /* Unregistered connection, but wrong number of arguments: */
97 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
98 Client_ID(Client), Req->command);
100 /* Registered connection, PASS command is not allowed! */
101 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
105 Client_SetPassword(Client, Req->argv[0]);
106 Client_SetType(Client, CLIENT_GOTPASS);
108 /* Protocol version */
109 if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
112 c2 = Req->argv[1][2];
113 c4 = Req->argv[1][4];
115 Req->argv[1][4] = '\0';
116 protolow = atoi(&Req->argv[1][2]);
117 Req->argv[1][2] = '\0';
118 protohigh = atoi(Req->argv[1]);
120 Req->argv[1][2] = c2;
121 Req->argv[1][4] = c4;
123 protohigh = protolow = 0;
125 /* Protocol type, see doc/Protocol.txt */
126 if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
127 type = &Req->argv[1][4];
131 /* Protocol flags/options */
133 orig_flags = Req->argv[3];
137 /* Implementation, version and IRC+ flags */
138 if (Req->argc >= 3) {
139 char *impl, *ptr, *serverver, *flags;
142 ptr = strchr(impl, '|');
146 if (type && strcmp(type, PROTOIRCPLUS) == 0) {
147 /* The peer seems to be a server which supports the
148 * IRC+ protocol (see doc/Protocol.txt). */
150 flags = strchr(serverver, ':');
157 "Peer announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
158 impl, serverver, protohigh, protolow, flags);
160 /* The peer seems to be a server supporting the
161 * "original" IRC protocol (RFC 2813). */
163 if (strchr(orig_flags, 'Z'))
168 "Peer announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
169 impl, protohigh, protolow, flags);
171 Client_SetFlags(Client, flags);
179 * IRC "NICK" command.
180 * This function implements the IRC command "NICK" which is used to register
181 * with the server, to change already registered nicknames and to introduce
182 * new users which are connected to other servers.
185 IRC_NICK( CLIENT *Client, REQUEST *Req )
187 CLIENT *intr_c, *target, *c;
189 char *nick, *user, *hostname, *modes, *info;
192 assert( Client != NULL );
193 assert( Req != NULL );
196 /* Some IRC clients, for example BitchX, send the NICK and USER
197 * commands in the wrong order ... */
198 if( Client_Type( Client ) == CLIENT_UNKNOWN
199 || Client_Type( Client ) == CLIENT_GOTPASS
200 || Client_Type( Client ) == CLIENT_GOTNICK
201 || Client_Type( Client ) == CLIENT_GOTUSER
202 || Client_Type( Client ) == CLIENT_USER
203 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
205 if( Client_Type( Client ) == CLIENT_UNKNOWN
206 || Client_Type( Client ) == CLIENT_GOTPASS
207 || Client_Type( Client ) == CLIENT_GOTNICK
208 || Client_Type( Client ) == CLIENT_USER
209 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
212 /* User registration or change of nickname */
214 /* Wrong number of arguments? */
216 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
220 /* Search "target" client */
221 if( Client_Type( Client ) == CLIENT_SERVER )
223 target = Client_Search( Req->prefix );
225 return IRC_WriteStrClient( Client,
232 /* Is this a restricted client? */
233 if( Client_HasMode( Client, 'r' ))
234 return IRC_WriteStrClient( Client,
236 Client_ID( Client ));
242 /* If the clients tries to change to its own nickname we won't
243 * do anything. This is how the original ircd behaves and some
244 * clients (for example Snak) expect it to be like this.
245 * But I doubt that this is "really the right thing" ... */
246 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 )
250 /* Check that the new nickname is available. Special case:
251 * the client only changes from/to upper to lower case. */
252 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
254 if( ! Client_CheckNick( target, Req->argv[0] ))
258 if(( Client_Type( target ) != CLIENT_USER )
259 && ( Client_Type( target ) != CLIENT_SERVER ))
262 Log( LOG_DEBUG, "Connection %d: got valid NICK command ...",
263 Client_Conn( Client ));
265 /* Register new nickname of this client */
266 Client_SetID( target, Req->argv[0] );
268 /* If we received a valid USER command already then
269 * register the new client! */
270 if( Client_Type( Client ) == CLIENT_GOTUSER )
271 return Hello_User( Client );
273 Client_SetType( Client, CLIENT_GOTNICK );
277 /* Nickname change */
278 if (Client_Conn(target) > NONE) {
281 "User \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
282 Client_Mask(target), Client_Conn(target),
283 Client_ID(target), Req->argv[0]);
284 Conn_UpdateIdle(Client_Conn(target));
290 "User \"%s\" changed nick: \"%s\" -> \"%s\".",
291 Client_Mask( target ), Client_ID( target ),
295 /* Inform all users and servers (which have to know)
296 * of this nickname change */
297 if( Client_Type( Client ) == CLIENT_USER )
298 IRC_WriteStrClientPrefix( Client, Client,
301 IRC_WriteStrServersPrefix( Client, target,
302 "NICK :%s", Req->argv[0] );
303 IRC_WriteStrRelatedPrefix( target, target, false,
304 "NICK :%s", Req->argv[0] );
306 /* Register old nickname for WHOWAS queries */
307 Client_RegisterWhowas( target );
309 /* Save new nickname */
310 Client_SetID( target, Req->argv[0] );
312 IRC_SetPenalty( target, 2 );
316 } else if(Client_Type(Client) == CLIENT_SERVER ||
317 Client_Type(Client) == CLIENT_SERVICE) {
318 /* Server or service introduces new client */
320 /* Bad number of parameters? */
321 if (Req->argc != 2 && Req->argc != 7)
322 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
323 Client_ID(Client), Req->command);
325 if (Req->argc >= 7) {
326 /* RFC 2813 compatible syntax */
328 hops = atoi(Req->argv[1]);
330 hostname = Req->argv[3];
331 token = atoi(Req->argv[4]);
332 modes = Req->argv[5] + 1;
335 /* RFC 1459 compatible syntax */
339 hostname = Client_ID(Client);
340 token = atoi(Req->argv[1]);
344 conn = Client_Conn(Client);
346 !(Conn_Options(conn) & CONN_RFC1459)) {
348 "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
349 conn, Client_ID(Client));
350 Conn_SetOption(conn, CONN_RFC1459);
354 /* Nick ueberpruefen */
355 c = Client_Search(nick);
357 /* Der neue Nick ist auf diesem Server bereits registriert:
358 * sowohl der neue, als auch der alte Client muessen nun
359 * disconnectiert werden. */
360 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
361 Kill_Nick( Req->argv[0], "Nick collision" );
365 /* Server, zu dem der Client connectiert ist, suchen */
366 intr_c = Client_GetFromToken(Client, token);
369 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
370 Kill_Nick( Req->argv[0], "Unknown server" );
374 /* Neue Client-Struktur anlegen */
375 c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
376 token, modes, info, true);
379 /* Eine neue Client-Struktur konnte nicht angelegt werden.
380 * Der Client muss disconnectiert werden, damit der Netz-
381 * status konsistent bleibt. */
382 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
383 Kill_Nick( Req->argv[0], "Server error" );
387 /* RFC 2813: client is now fully registered, inform all the
388 * other servers about the new user.
389 * RFC 1459: announce the new client only after receiving the
390 * USER command, first we need more information! */
391 if (Req->argc >= 7) {
392 modes = Client_Modes(c);
393 LogDebug("User \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
394 Client_Mask(c), modes, Client_ID(Client),
395 Client_ID(intr_c), Client_Hops(c),
396 Client_Hops(c) > 1 ? "s": "");
398 Introduce_Client(Client, Req->argv[0],
399 atoi(Req->argv[1]) + 1, Req->argv[2],
400 Req->argv[3], intr_c, Req->argv[5], Req->argv[6]);
402 LogDebug("User \"%s\" is beeing registered (RFC 1459) ...",
404 Client_SetType(c, CLIENT_GOTNICK);
409 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
414 * Handler for the IRC command "USER".
417 IRC_USER(CLIENT * Client, REQUEST * Req)
420 char modes[CLIENT_MODE_LEN + 1] = "+";
425 assert(Client != NULL);
428 if (Client_Type(Client) == CLIENT_GOTNICK ||
430 Client_Type(Client) == CLIENT_UNKNOWN ||
432 Client_Type(Client) == CLIENT_GOTPASS)
436 return IRC_WriteStrClient(Client,
437 ERR_NEEDMOREPARAMS_MSG,
443 ptr = Client_User(Client);
444 if (!ptr || !*ptr || *ptr == '~')
445 Client_SetUser(Client, Req->argv[0], false);
447 Client_SetUser(Client, Req->argv[0], false);
450 /* "Real name" or user info text: Don't set it to the empty
451 * string, the original ircd can't deal with such "real names"
452 * (e. g. "USER user * * :") ... */
454 Client_SetInfo(Client, Req->argv[3]);
456 Client_SetInfo(Client, "-");
458 LogDebug("Connection %d: got valid USER command ...",
459 Client_Conn(Client));
460 if (Client_Type(Client) == CLIENT_GOTNICK)
461 return Hello_User(Client);
463 Client_SetType(Client, CLIENT_GOTUSER);
466 } else if (Client_Type(Client) == CLIENT_SERVER ||
467 Client_Type(Client) == CLIENT_SERVICE) {
468 /* Server/service updating an user */
470 return IRC_WriteStrClient(Client,
471 ERR_NEEDMOREPARAMS_MSG,
474 c = Client_Search(Req->prefix);
476 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
480 Client_SetUser(c, Req->argv[0], true);
481 Client_SetHostname(c, Req->argv[1]);
482 Client_SetInfo(c, Req->argv[3]);
484 LogDebug("Connection %d: got valid USER command for \"%s\".",
485 Client_Conn(Client), Client_Mask(c));
487 /* RFC 1459 style user registration? Inform other servers! */
488 if (Client_Type(c) == CLIENT_GOTNICK) {
489 strlcat(modes, Client_Modes(c), sizeof(modes));
490 Introduce_Client(Client, Client_ID(c), Client_Hops(c),
491 Client_User(c), Client_Hostname(c),
492 Client_Introducer(c), modes,
494 LogDebug("User \"%s\" (%s) registered (via %s, on %s, %d hop%s).",
495 Client_Mask(c), modes, Client_ID(Client),
496 Client_ID(Client_Introducer(c)), Client_Hops(c),
497 Client_Hops(c) > 1 ? "s": "");
498 Client_SetType(c, CLIENT_USER);
502 } else if (Client_Type(Client) == CLIENT_USER) {
503 /* Already registered connection */
504 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
507 /* Unexpected/invalid connection state? */
508 return IRC_WriteStrClient(Client, ERR_NOTREGISTERED_MSG,
515 * Service registration.
516 * ngIRCd does not support services at the moment, so this function is a
517 * dummy that returns ERR_ERRONEUSNICKNAME on each call.
520 IRC_SERVICE(CLIENT *Client, REQUEST *Req)
522 assert(Client != NULL);
525 if (Client_Type(Client) != CLIENT_GOTPASS)
526 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
530 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
531 Client_ID(Client), Req->command);
533 return IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
534 Client_ID(Client), Req->argv[0]);
539 IRC_QUIT( CLIENT *Client, REQUEST *Req )
542 char quitmsg[LINE_LEN];
544 assert( Client != NULL );
545 assert( Req != NULL );
547 /* Wrong number of arguments? */
549 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
552 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
554 if ( Client_Type( Client ) == CLIENT_SERVER )
557 target = Client_Search( Req->prefix );
560 /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
561 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
565 Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
571 if (Req->argc == 1 && quitmsg[0] != '\"') {
572 /* " " to avoid confusion */
573 strlcpy(quitmsg, "\"", sizeof quitmsg);
574 strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
575 strlcat(quitmsg, "\"", sizeof quitmsg );
578 /* User, Service, oder noch nicht registriert */
579 Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
587 IRC_PING(CLIENT *Client, REQUEST *Req)
589 CLIENT *target, *from;
591 assert(Client != NULL);
594 /* Wrong number of arguments? */
596 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
599 /* Don't ignore additional arguments when in "strict" mode */
601 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
602 Client_ID(Client), Req->command);
606 /* A target has been specified ... */
607 target = Client_Search(Req->argv[1]);
609 if (!target || Client_Type(target) != CLIENT_SERVER)
610 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
611 Client_ID(Client), Req->argv[1]);
613 if (target != Client_ThisServer()) {
614 /* Ok, we have to forward the PING */
615 if (Client_Type(Client) == CLIENT_SERVER)
616 from = Client_Search(Req->prefix);
620 return IRC_WriteStrClient(Client,
621 ERR_NOSUCHSERVER_MSG,
622 Client_ID(Client), Req->prefix);
624 return IRC_WriteStrClientPrefix(target, from,
625 "PING %s :%s", Req->argv[0],
630 if (Client_Type(Client) == CLIENT_SERVER) {
632 from = Client_Search(Req->prefix);
636 from = Client_ThisServer();
638 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
639 Client_ID(Client), Req->prefix);
641 Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
642 Client_Conn(Client));
645 return IRC_WriteStrClient(Client, "PONG %s :%s",
646 Client_ID(from), Client_ID(Client));
648 /* Some clients depend on the argument being returned in the PONG
649 * reply (not mentioned in any RFC, though) */
650 return IRC_WriteStrClient(Client, "PONG %s :%s",
651 Client_ID(from), Req->argv[0]);
657 IRC_PONG(CLIENT *Client, REQUEST *Req)
659 CLIENT *target, *from;
662 assert(Client != NULL);
665 /* Wrong number of arguments? */
667 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
670 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
671 Client_ID(Client), Req->command);
674 if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
675 target = Client_Search(Req->argv[0]);
677 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
678 Client_ID(Client), Req->argv[0]);
680 from = Client_Search(Req->prefix);
682 if (target != Client_ThisServer() && target != from) {
683 /* Ok, we have to forward the message. */
685 return IRC_WriteStrClient(Client,
686 ERR_NOSUCHSERVER_MSG,
687 Client_ID(Client), Req->prefix);
689 if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
693 return IRC_WriteStrClientPrefix(target, from,
694 "PONG %s :%s", s, Req->argv[1]);
698 /* The connection timestamp has already been updated when the data has
699 * been read from so socket, so we don't need to update it here. */
701 if (Client_Conn(Client) > NONE)
703 "Connection %d: received PONG. Lag: %ld seconds.",
705 time(NULL) - Conn_LastPing(Client_Conn(Client)));
708 "Connection %d: received PONG.", Client_Conn(Client));
715 Hello_User(CLIENT * Client)
717 char modes[CLIENT_MODE_LEN + 1] = "+";
719 assert(Client != NULL);
721 /* Check password ... */
722 if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
725 "User \"%s\" rejected (connection %d): Bad password!",
726 Client_Mask(Client), Client_Conn(Client));
727 Conn_Close(Client_Conn(Client), NULL, "Bad password", true);
731 Log(LOG_NOTICE, "User \"%s\" registered (connection %d).",
732 Client_Mask(Client), Client_Conn(Client));
734 /* Inform other servers */
735 strlcat(modes, Client_Modes(Client), sizeof(modes));
736 Introduce_Client(NULL, Client_ID(Client), 1, Client_User(Client),
737 Client_Hostname(Client), NULL, modes,
738 Client_Info(Client));
740 if (!IRC_WriteStrClient
741 (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
743 if (!IRC_WriteStrClient
744 (Client, RPL_YOURHOST_MSG, Client_ID(Client),
745 Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
746 TARGET_VENDOR, TARGET_OS))
748 if (!IRC_WriteStrClient
749 (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
751 if (!IRC_WriteStrClient
752 (Client, RPL_MYINFO_MSG, Client_ID(Client),
753 Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
757 /* Features supported by this server (005 numeric, ISUPPORT),
758 * see <http://www.irc.org/tech_docs/005.html> for details. */
759 if (!IRC_Send_ISUPPORT(Client))
762 Client_SetType(Client, CLIENT_USER);
764 if (!IRC_Send_LUSERS(Client))
766 if (!IRC_Show_MOTD(Client))
769 /* Suspend the client for a second ... */
770 IRC_SetPenalty(Client, 1);
777 Kill_Nick( char *Nick, char *Reason )
781 assert( Nick != NULL );
782 assert( Reason != NULL );
784 r.prefix = (char *)Client_ThisServer( );
789 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
790 IRC_KILL( Client_ThisServer( ), &r );
795 Introduce_Client(CLIENT *From, char *Nick, const int HopCount, char *User,
796 char *Host, CLIENT *Server, char *Mode, char *Name)
801 i.hopcount = HopCount;
802 i.user = User ? User : "-";
803 i.host = Host ? Host : "-";
804 i.server = Server ? Server : Client_ThisServer();
805 i.mode = Mode ? Mode : "+";
806 i.name = Name ? Name : "";
808 IRC_WriteStrServersPrefixFlag_CB(From,
809 From != NULL ? From : Client_ThisServer(),
810 '\0', cb_introduceClient, (void *)(&i));
811 } /* Introduce_Client */
815 cb_introduceClient(CLIENT *Client, CLIENT *Prefix, void *data)
817 INTRO_INFO *i = (INTRO_INFO *)data;
820 conn = Client_Conn(Client);
821 if (Conn_Options(conn) & CONN_RFC1459) {
822 /* RFC 1459 mode: separate NICK and USER commands */
823 Conn_WriteStr(conn, "NICK %s :%d", i->nick, i->hopcount);
824 Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
825 i->nick, i->user, i->host,
826 Client_ID(i->server), i->name);
828 /* RFC 2813 mode: one combined NICK command */
829 IRC_WriteStrClientPrefix(Client, Prefix,
830 "NICK %s %d %s %s %d %s :%s",
831 i->nick, i->hopcount, i->user, i->host,
832 Client_MyToken(i->server), i->mode,
835 } /* cb_introduceClient */