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 (RFC 1459) ...",
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 (RFC 2813, 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]);
107 /* Protocol version */
108 if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
111 c2 = Req->argv[1][2];
112 c4 = Req->argv[1][4];
114 Req->argv[1][4] = '\0';
115 protolow = atoi(&Req->argv[1][2]);
116 Req->argv[1][2] = '\0';
117 protohigh = atoi(Req->argv[1]);
119 Req->argv[1][2] = c2;
120 Req->argv[1][4] = c4;
122 Client_SetType(Client, CLIENT_GOTPASS_2813);
124 protohigh = protolow = 0;
125 Client_SetType(Client, CLIENT_GOTPASS);
128 /* Protocol type, see doc/Protocol.txt */
129 if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
130 type = &Req->argv[1][4];
134 /* Protocol flags/options */
136 orig_flags = Req->argv[3];
140 /* Implementation, version and IRC+ flags */
141 if (Req->argc >= 3) {
142 char *impl, *ptr, *serverver, *flags;
145 ptr = strchr(impl, '|');
149 if (type && strcmp(type, PROTOIRCPLUS) == 0) {
150 /* The peer seems to be a server which supports the
151 * IRC+ protocol (see doc/Protocol.txt). */
153 flags = strchr(serverver, ':');
160 "Peer announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
161 impl, serverver, protohigh, protolow, flags);
163 /* The peer seems to be a server supporting the
164 * "original" IRC protocol (RFC 2813). */
166 if (strchr(orig_flags, 'Z'))
171 "Peer announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
172 impl, protohigh, protolow, flags);
174 Client_SetFlags(Client, flags);
182 * IRC "NICK" command.
183 * This function implements the IRC command "NICK" which is used to register
184 * with the server, to change already registered nicknames and to introduce
185 * new users which are connected to other servers.
188 IRC_NICK( CLIENT *Client, REQUEST *Req )
190 CLIENT *intr_c, *target, *c;
191 char *nick, *user, *hostname, *modes, *info;
194 assert( Client != NULL );
195 assert( Req != NULL );
198 /* Some IRC clients, for example BitchX, send the NICK and USER
199 * commands in the wrong order ... */
200 if( Client_Type( Client ) == CLIENT_UNKNOWN
201 || Client_Type( Client ) == CLIENT_GOTPASS
202 || Client_Type( Client ) == CLIENT_GOTNICK
203 || Client_Type( Client ) == CLIENT_GOTUSER
204 || Client_Type( Client ) == CLIENT_USER
205 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
207 if( Client_Type( Client ) == CLIENT_UNKNOWN
208 || Client_Type( Client ) == CLIENT_GOTPASS
209 || Client_Type( Client ) == CLIENT_GOTNICK
210 || Client_Type( Client ) == CLIENT_USER
211 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
214 /* User registration or change of nickname */
216 /* Wrong number of arguments? */
218 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
222 /* Search "target" client */
223 if( Client_Type( Client ) == CLIENT_SERVER )
225 target = Client_Search( Req->prefix );
227 return IRC_WriteStrClient( Client,
234 /* Is this a restricted client? */
235 if( Client_HasMode( Client, 'r' ))
236 return IRC_WriteStrClient( Client,
238 Client_ID( Client ));
244 /* If the clients tries to change to its own nickname we won't
245 * do anything. This is how the original ircd behaves and some
246 * clients (for example Snak) expect it to be like this.
247 * But I doubt that this is "really the right thing" ... */
248 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 )
252 /* Check that the new nickname is available. Special case:
253 * the client only changes from/to upper to lower case. */
254 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
256 if( ! Client_CheckNick( target, Req->argv[0] ))
260 if(( Client_Type( target ) != CLIENT_USER )
261 && ( Client_Type( target ) != CLIENT_SERVER ))
264 Log( LOG_DEBUG, "Connection %d: got valid NICK command ...",
265 Client_Conn( Client ));
267 /* Register new nickname of this client */
268 Client_SetID( target, Req->argv[0] );
270 /* If we received a valid USER command already then
271 * register the new client! */
272 if( Client_Type( Client ) == CLIENT_GOTUSER )
273 return Hello_User( Client );
275 Client_SetType( Client, CLIENT_GOTNICK );
279 /* Nickname change */
280 if (Client_Conn(target) > NONE) {
283 "User \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
284 Client_Mask(target), Client_Conn(target),
285 Client_ID(target), Req->argv[0]);
286 Conn_UpdateIdle(Client_Conn(target));
292 "User \"%s\" changed nick: \"%s\" -> \"%s\".",
293 Client_Mask( target ), Client_ID( target ),
297 /* Inform all users and servers (which have to know)
298 * of this nickname change */
299 if( Client_Type( Client ) == CLIENT_USER )
300 IRC_WriteStrClientPrefix( Client, Client,
303 IRC_WriteStrServersPrefix( Client, target,
304 "NICK :%s", Req->argv[0] );
305 IRC_WriteStrRelatedPrefix( target, target, false,
306 "NICK :%s", Req->argv[0] );
308 /* Register old nickname for WHOWAS queries */
309 Client_RegisterWhowas( target );
311 /* Save new nickname */
312 Client_SetID( target, Req->argv[0] );
314 IRC_SetPenalty( target, 2 );
318 } else if(Client_Type(Client) == CLIENT_SERVER ||
319 Client_Type(Client) == CLIENT_SERVICE) {
320 /* Server or service introduces new client */
322 /* Bad number of parameters? */
323 if (Req->argc != 2 && Req->argc != 7)
324 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
325 Client_ID(Client), Req->command);
327 if (Req->argc >= 7) {
328 /* RFC 2813 compatible syntax */
330 hops = atoi(Req->argv[1]);
332 hostname = Req->argv[3];
333 token = atoi(Req->argv[4]);
334 modes = Req->argv[5] + 1;
337 /* RFC 1459 compatible syntax */
341 hostname = Client_ID(Client);
342 token = atoi(Req->argv[1]);
347 /* Nick ueberpruefen */
348 c = Client_Search(nick);
350 /* Der neue Nick ist auf diesem Server bereits registriert:
351 * sowohl der neue, als auch der alte Client muessen nun
352 * disconnectiert werden. */
353 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
354 Kill_Nick( Req->argv[0], "Nick collision" );
358 /* Server, zu dem der Client connectiert ist, suchen */
359 intr_c = Client_GetFromToken(Client, token);
362 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
363 Kill_Nick( Req->argv[0], "Unknown server" );
367 /* Neue Client-Struktur anlegen */
368 c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
369 token, modes, info, true);
372 /* Eine neue Client-Struktur konnte nicht angelegt werden.
373 * Der Client muss disconnectiert werden, damit der Netz-
374 * status konsistent bleibt. */
375 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
376 Kill_Nick( Req->argv[0], "Server error" );
380 /* RFC 2813: client is now fully registered, inform all the
381 * other servers about the new user.
382 * RFC 1459: announce the new client only after receiving the
383 * USER command, first we need more information! */
384 if (Req->argc >= 7) {
385 modes = Client_Modes(c);
386 LogDebug("User \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
387 Client_Mask(c), modes, Client_ID(Client),
388 Client_ID(intr_c), Client_Hops(c),
389 Client_Hops(c) > 1 ? "s": "");
391 Introduce_Client(Client, Req->argv[0],
392 atoi(Req->argv[1]) + 1, Req->argv[2],
393 Req->argv[3], intr_c, Req->argv[5], Req->argv[6]);
395 LogDebug("User \"%s\" is beeing registered (RFC 1459) ...",
397 Client_SetType(c, CLIENT_GOTNICK);
402 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
407 * Handler for the IRC command "USER".
410 IRC_USER(CLIENT * Client, REQUEST * Req)
413 char modes[CLIENT_MODE_LEN + 1] = "+";
418 assert(Client != NULL);
421 if (Client_Type(Client) == CLIENT_GOTNICK ||
423 Client_Type(Client) == CLIENT_UNKNOWN ||
425 Client_Type(Client) == CLIENT_GOTPASS)
429 return IRC_WriteStrClient(Client,
430 ERR_NEEDMOREPARAMS_MSG,
436 ptr = Client_User(Client);
437 if (!ptr || !*ptr || *ptr == '~')
438 Client_SetUser(Client, Req->argv[0], false);
440 Client_SetUser(Client, Req->argv[0], false);
443 /* "Real name" or user info text: Don't set it to the empty
444 * string, the original ircd can't deal with such "real names"
445 * (e. g. "USER user * * :") ... */
447 Client_SetInfo(Client, Req->argv[3]);
449 Client_SetInfo(Client, "-");
451 LogDebug("Connection %d: got valid USER command ...",
452 Client_Conn(Client));
453 if (Client_Type(Client) == CLIENT_GOTNICK)
454 return Hello_User(Client);
456 Client_SetType(Client, CLIENT_GOTUSER);
459 } else if (Client_Type(Client) == CLIENT_SERVER ||
460 Client_Type(Client) == CLIENT_SERVICE) {
461 /* Server/service updating an user */
463 return IRC_WriteStrClient(Client,
464 ERR_NEEDMOREPARAMS_MSG,
467 c = Client_Search(Req->prefix);
469 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
473 Client_SetUser(c, Req->argv[0], true);
474 Client_SetHostname(c, Req->argv[1]);
475 Client_SetInfo(c, Req->argv[3]);
477 LogDebug("Connection %d: got valid USER command for \"%s\".",
478 Client_Conn(Client), Client_Mask(c));
480 /* RFC 1459 style user registration? Inform other servers! */
481 if (Client_Type(c) == CLIENT_GOTNICK) {
482 strlcat(modes, Client_Modes(c), sizeof(modes));
483 Introduce_Client(Client, Client_ID(c), Client_Hops(c),
484 Client_User(c), Client_Hostname(c),
485 Client_Introducer(c), modes,
487 LogDebug("User \"%s\" (%s) registered (via %s, on %s, %d hop%s).",
488 Client_Mask(c), modes, Client_ID(Client),
489 Client_ID(Client_Introducer(c)), Client_Hops(c),
490 Client_Hops(c) > 1 ? "s": "");
491 Client_SetType(c, CLIENT_USER);
495 } else if (Client_Type(Client) == CLIENT_USER) {
496 /* Already registered connection */
497 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
500 /* Unexpected/invalid connection state? */
501 return IRC_WriteStrClient(Client, ERR_NOTREGISTERED_MSG,
508 * Service registration.
509 * ngIRCd does not support services at the moment, so this function is a
510 * dummy that returns ERR_ERRONEUSNICKNAME on each call.
513 IRC_SERVICE(CLIENT *Client, REQUEST *Req)
515 assert(Client != NULL);
518 if (Client_Type(Client) != CLIENT_GOTPASS)
519 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
523 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
524 Client_ID(Client), Req->command);
526 return IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
527 Client_ID(Client), Req->argv[0]);
532 IRC_QUIT( CLIENT *Client, REQUEST *Req )
535 char quitmsg[LINE_LEN];
537 assert( Client != NULL );
538 assert( Req != NULL );
540 /* Wrong number of arguments? */
542 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
545 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
547 if ( Client_Type( Client ) == CLIENT_SERVER )
550 target = Client_Search( Req->prefix );
553 /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
554 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
558 Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
564 if (Req->argc == 1 && quitmsg[0] != '\"') {
565 /* " " to avoid confusion */
566 strlcpy(quitmsg, "\"", sizeof quitmsg);
567 strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
568 strlcat(quitmsg, "\"", sizeof quitmsg );
571 /* User, Service, oder noch nicht registriert */
572 Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
580 IRC_PING(CLIENT *Client, REQUEST *Req)
582 CLIENT *target, *from;
584 assert(Client != NULL);
587 /* Wrong number of arguments? */
589 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
592 /* Don't ignore additional arguments when in "strict" mode */
594 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
595 Client_ID(Client), Req->command);
599 /* A target has been specified ... */
600 target = Client_Search(Req->argv[1]);
602 if (!target || Client_Type(target) != CLIENT_SERVER)
603 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
604 Client_ID(Client), Req->argv[1]);
606 if (target != Client_ThisServer()) {
607 /* Ok, we have to forward the PING */
608 if (Client_Type(Client) == CLIENT_SERVER)
609 from = Client_Search(Req->prefix);
613 return IRC_WriteStrClient(Client,
614 ERR_NOSUCHSERVER_MSG,
615 Client_ID(Client), Req->prefix);
617 return IRC_WriteStrClientPrefix(target, from,
618 "PING %s :%s", Req->argv[0],
623 if (Client_Type(Client) == CLIENT_SERVER) {
625 from = Client_Search(Req->prefix);
629 from = Client_ThisServer();
631 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
632 Client_ID(Client), Req->prefix);
634 Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
635 Client_Conn(Client));
638 return IRC_WriteStrClient(Client, "PONG %s :%s",
639 Client_ID(from), Client_ID(Client));
641 /* Some clients depend on the argument being returned in the PONG
642 * reply (not mentioned in any RFC, though) */
643 return IRC_WriteStrClient(Client, "PONG %s :%s",
644 Client_ID(from), Req->argv[0]);
650 IRC_PONG(CLIENT *Client, REQUEST *Req)
652 CLIENT *target, *from;
655 assert(Client != NULL);
658 /* Wrong number of arguments? */
660 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
663 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
664 Client_ID(Client), Req->command);
667 if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
668 target = Client_Search(Req->argv[0]);
670 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
671 Client_ID(Client), Req->argv[0]);
673 from = Client_Search(Req->prefix);
675 if (target != Client_ThisServer() && target != from) {
676 /* Ok, we have to forward the message. */
678 return IRC_WriteStrClient(Client,
679 ERR_NOSUCHSERVER_MSG,
680 Client_ID(Client), Req->prefix);
682 if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
686 return IRC_WriteStrClientPrefix(target, from,
687 "PONG %s :%s", s, Req->argv[1]);
691 /* The connection timestamp has already been updated when the data has
692 * been read from so socket, so we don't need to update it here. */
694 if (Client_Conn(Client) > NONE)
696 "Connection %d: received PONG. Lag: %ld seconds.",
698 time(NULL) - Conn_LastPing(Client_Conn(Client)));
701 "Connection %d: received PONG.", Client_Conn(Client));
708 Hello_User(CLIENT * Client)
710 char modes[CLIENT_MODE_LEN + 1] = "+";
712 assert(Client != NULL);
714 /* Check password ... */
715 if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
718 "User \"%s\" rejected (connection %d): Bad password!",
719 Client_Mask(Client), Client_Conn(Client));
720 Conn_Close(Client_Conn(Client), NULL, "Bad password", true);
724 Log(LOG_NOTICE, "User \"%s\" registered (connection %d).",
725 Client_Mask(Client), Client_Conn(Client));
727 /* Inform other servers */
728 strlcat(modes, Client_Modes(Client), sizeof(modes));
729 Introduce_Client(NULL, Client_ID(Client), 1, Client_User(Client),
730 Client_Hostname(Client), NULL, modes,
731 Client_Info(Client));
733 if (!IRC_WriteStrClient
734 (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
736 if (!IRC_WriteStrClient
737 (Client, RPL_YOURHOST_MSG, Client_ID(Client),
738 Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
739 TARGET_VENDOR, TARGET_OS))
741 if (!IRC_WriteStrClient
742 (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
744 if (!IRC_WriteStrClient
745 (Client, RPL_MYINFO_MSG, Client_ID(Client),
746 Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
750 /* Features supported by this server (005 numeric, ISUPPORT),
751 * see <http://www.irc.org/tech_docs/005.html> for details. */
752 if (!IRC_Send_ISUPPORT(Client))
755 Client_SetType(Client, CLIENT_USER);
757 if (!IRC_Send_LUSERS(Client))
759 if (!IRC_Show_MOTD(Client))
762 /* Suspend the client for a second ... */
763 IRC_SetPenalty(Client, 1);
770 Kill_Nick( char *Nick, char *Reason )
774 assert( Nick != NULL );
775 assert( Reason != NULL );
777 r.prefix = (char *)Client_ThisServer( );
782 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
783 IRC_KILL( Client_ThisServer( ), &r );
788 Introduce_Client(CLIENT *From, char *Nick, const int HopCount, char *User,
789 char *Host, CLIENT *Server, char *Mode, char *Name)
794 i.hopcount = HopCount;
795 i.user = User ? User : "-";
796 i.host = Host ? Host : "-";
797 i.server = Server ? Server : Client_ThisServer();
798 i.mode = Mode ? Mode : "+";
799 i.name = Name ? Name : "";
801 IRC_WriteStrServersPrefixFlag_CB(From,
802 From != NULL ? From : Client_ThisServer(),
803 '\0', cb_introduceClient, (void *)(&i));
804 } /* Introduce_Client */
808 cb_introduceClient(CLIENT *Client, CLIENT *Prefix, void *data)
810 INTRO_INFO *i = (INTRO_INFO *)data;
813 conn = Client_Conn(Client);
814 if (Conn_Options(conn) & CONN_RFC1459) {
815 /* RFC 1459 mode: separate NICK and USER commands */
816 Conn_WriteStr(conn, "NICK %s :%d", i->nick, i->hopcount);
817 Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
818 i->nick, i->user, i->host,
819 Client_ID(i->server), i->name);
821 /* RFC 2813 mode: one combined NICK command */
822 IRC_WriteStrClientPrefix(Client, Prefix,
823 "NICK %s %d %s %s %d %s :%s",
824 i->nick, i->hopcount, i->user, i->host,
825 Client_MyToken(i->server), i->mode,
828 } /* cb_introduceClient */