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 static bool Hello_User PARAMS(( CLIENT *Client ));
42 static void Kill_Nick PARAMS(( char *Nick, char *Reason ));
46 * Handler for the IRC command "PASS".
47 * See RFC 2813 section 4.1.1, and RFC 2812 section 3.1.1.
50 IRC_PASS( CLIENT *Client, REQUEST *Req )
52 char *type, *orig_flags;
53 int protohigh, protolow;
55 assert( Client != NULL );
56 assert( Req != NULL );
58 /* Return an error if this is not a local client */
59 if (Client_Conn(Client) <= NONE)
60 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
61 Client_ID(Client), Req->command);
63 if (Client_Type(Client) == CLIENT_UNKNOWN && Req->argc == 1) {
64 /* Not yet registered "unknown" connection, PASS with one
65 * argument: either a regular client, service, or server
66 * using the old RFC 1459 section 4.1.1 syntax. */
67 LogDebug("Connection %d: got PASS command ...",
69 } else if ((Client_Type(Client) == CLIENT_UNKNOWN ||
70 Client_Type(Client) == CLIENT_UNKNOWNSERVER) &&
71 (Req->argc == 3 || Req->argc == 4)) {
72 /* Not yet registered "unknown" connection or outgoing server
73 * link, PASS with three or four argument: server using the
74 * RFC 2813 section 4.1.1 syntax. */
75 LogDebug("Connection %d: got PASS command (new server link) ...",
77 } else if (Client_Type(Client) == CLIENT_UNKNOWN ||
78 Client_Type(Client) == CLIENT_UNKNOWNSERVER) {
79 /* Unregistered connection, but wrong number of arguments: */
80 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
81 Client_ID(Client), Req->command);
83 /* Registered connection, PASS command is not allowed! */
84 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
88 Client_SetPassword(Client, Req->argv[0]);
89 Client_SetType(Client, CLIENT_GOTPASS);
91 /* Protocol version */
92 if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
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]);
103 Req->argv[1][2] = c2;
104 Req->argv[1][4] = c4;
106 protohigh = protolow = 0;
108 /* Protocol type, see doc/Protocol.txt */
109 if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
110 type = &Req->argv[1][4];
114 /* Protocol flags/options */
116 orig_flags = Req->argv[3];
120 /* Implementation, version and IRC+ flags */
121 if (Req->argc >= 3) {
122 char *impl, *ptr, *serverver, *flags;
125 ptr = strchr(impl, '|');
129 if (type && strcmp(type, PROTOIRCPLUS) == 0) {
130 /* The peer seems to be a server which supports the
131 * IRC+ protocol (see doc/Protocol.txt). */
133 flags = strchr(serverver, ':');
140 "Peer announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
141 impl, serverver, protohigh, protolow, flags);
143 /* The peer seems to be a server supporting the
144 * "original" IRC protocol (RFC 2813). */
146 if (strchr(orig_flags, 'Z'))
151 "Peer announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
152 impl, protohigh, protolow, flags);
154 Client_SetFlags(Client, flags);
162 * IRC "NICK" command.
163 * This function implements the IRC command "NICK" which is used to register
164 * with the server, to change already registered nicknames and to introduce
165 * new users which are connected to other servers.
168 IRC_NICK( CLIENT *Client, REQUEST *Req )
170 CLIENT *intr_c, *target, *c;
172 char *nick, *user, *hostname, *modes, *info;
175 assert( Client != NULL );
176 assert( Req != NULL );
179 /* Some IRC clients, for example BitchX, send the NICK and USER
180 * commands in the wrong order ... */
181 if( Client_Type( Client ) == CLIENT_UNKNOWN
182 || Client_Type( Client ) == CLIENT_GOTPASS
183 || Client_Type( Client ) == CLIENT_GOTNICK
184 || Client_Type( Client ) == CLIENT_GOTUSER
185 || Client_Type( Client ) == CLIENT_USER
186 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
188 if( Client_Type( Client ) == CLIENT_UNKNOWN
189 || Client_Type( Client ) == CLIENT_GOTPASS
190 || Client_Type( Client ) == CLIENT_GOTNICK
191 || Client_Type( Client ) == CLIENT_USER
192 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
195 /* User registration or change of nickname */
197 /* Wrong number of arguments? */
199 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
203 /* Search "target" client */
204 if( Client_Type( Client ) == CLIENT_SERVER )
206 target = Client_Search( Req->prefix );
208 return IRC_WriteStrClient( Client,
215 /* Is this a restricted client? */
216 if( Client_HasMode( Client, 'r' ))
217 return IRC_WriteStrClient( Client,
219 Client_ID( Client ));
225 /* If the clients tries to change to its own nickname we won't
226 * do anything. This is how the original ircd behaves and some
227 * clients (for example Snak) expect it to be like this.
228 * But I doubt that this is "really the right thing" ... */
229 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 )
233 /* Check that the new nickname is available. Special case:
234 * the client only changes from/to upper to lower case. */
235 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
237 if( ! Client_CheckNick( target, Req->argv[0] ))
241 if(( Client_Type( target ) != CLIENT_USER )
242 && ( Client_Type( target ) != CLIENT_SERVER ))
245 Log( LOG_DEBUG, "Connection %d: got valid NICK command ...",
246 Client_Conn( Client ));
248 /* Register new nickname of this client */
249 Client_SetID( target, Req->argv[0] );
251 /* If we received a valid USER command already then
252 * register the new client! */
253 if( Client_Type( Client ) == CLIENT_GOTUSER )
254 return Hello_User( Client );
256 Client_SetType( Client, CLIENT_GOTNICK );
260 /* Nickname change */
261 if (Client_Conn(target) > NONE) {
264 "User \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
265 Client_Mask(target), Client_Conn(target),
266 Client_ID(target), Req->argv[0]);
267 Conn_UpdateIdle(Client_Conn(target));
273 "User \"%s\" changed nick: \"%s\" -> \"%s\".",
274 Client_Mask( target ), Client_ID( target ),
278 /* Inform all users and servers (which have to know)
279 * of this nickname change */
280 if( Client_Type( Client ) == CLIENT_USER )
281 IRC_WriteStrClientPrefix( Client, Client,
284 IRC_WriteStrServersPrefix( Client, target,
285 "NICK :%s", Req->argv[0] );
286 IRC_WriteStrRelatedPrefix( target, target, false,
287 "NICK :%s", Req->argv[0] );
289 /* Register old nickname for WHOWAS queries */
290 Client_RegisterWhowas( target );
292 /* Save new nickname */
293 Client_SetID( target, Req->argv[0] );
295 IRC_SetPenalty( target, 2 );
299 } else if(Client_Type(Client) == CLIENT_SERVER ||
300 Client_Type(Client) == CLIENT_SERVICE) {
301 /* Server or service introduces new client */
303 /* Bad number of parameters? */
304 if (Req->argc != 2 && Req->argc != 7)
305 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
306 Client_ID(Client), Req->command);
308 if (Req->argc >= 7) {
309 /* RFC 2813 compatible syntax */
311 hops = atoi(Req->argv[1]);
313 hostname = Req->argv[3];
314 token = atoi(Req->argv[4]);
315 modes = Req->argv[5] + 1;
318 /* RFC 1459 compatible syntax */
322 hostname = Client_ID(Client);
323 token = atoi(Req->argv[1]);
327 conn = Client_Conn(Client);
329 !(Conn_Options(conn) & CONN_RFC1459)) {
331 "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
332 conn, Client_ID(Client));
333 Conn_SetOption(conn, CONN_RFC1459);
337 /* Nick ueberpruefen */
338 c = Client_Search(nick);
340 /* Der neue Nick ist auf diesem Server bereits registriert:
341 * sowohl der neue, als auch der alte Client muessen nun
342 * disconnectiert werden. */
343 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
344 Kill_Nick( Req->argv[0], "Nick collision" );
348 /* Server, zu dem der Client connectiert ist, suchen */
349 intr_c = Client_GetFromToken(Client, token);
352 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
353 Kill_Nick( Req->argv[0], "Unknown server" );
357 /* Neue Client-Struktur anlegen */
358 c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
359 token, modes, info, true);
362 /* Eine neue Client-Struktur konnte nicht angelegt werden.
363 * Der Client muss disconnectiert werden, damit der Netz-
364 * status konsistent bleibt. */
365 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
366 Kill_Nick( Req->argv[0], "Server error" );
370 modes = Client_Modes( c );
371 if( *modes ) Log( LOG_DEBUG, "User \"%s\" (+%s) registered (via %s, on %s, %d hop%s).", Client_Mask( c ), modes, Client_ID( Client ), Client_ID( intr_c ), Client_Hops( c ), Client_Hops( c ) > 1 ? "s": "" );
372 else Log( LOG_DEBUG, "User \"%s\" registered (via %s, on %s, %d hop%s).", Client_Mask( c ), Client_ID( Client ), Client_ID( intr_c ), Client_Hops( c ), Client_Hops( c ) > 1 ? "s": "" );
374 /* Andere Server, ausser dem Introducer, informieren */
375 IRC_WriteStrServersPrefix( Client, Client, "NICK %s %d %s %s %d %s :%s", Req->argv[0], atoi( Req->argv[1] ) + 1, Req->argv[2], Req->argv[3], Client_MyToken( intr_c ), Req->argv[5], Req->argv[6] );
379 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
384 * Handler for the IRC command "USER".
387 IRC_USER(CLIENT * Client, REQUEST * Req)
394 assert(Client != NULL);
397 if (Client_Type(Client) == CLIENT_GOTNICK ||
399 Client_Type(Client) == CLIENT_UNKNOWN ||
401 Client_Type(Client) == CLIENT_GOTPASS)
405 return IRC_WriteStrClient(Client,
406 ERR_NEEDMOREPARAMS_MSG,
412 ptr = Client_User(Client);
413 if (!ptr || !*ptr || *ptr == '~')
414 Client_SetUser(Client, Req->argv[0], false);
416 Client_SetUser(Client, Req->argv[0], false);
419 /* "Real name" or user info text: Don't set it to the empty
420 * string, the original ircd can't deal with such "real names"
421 * (e. g. "USER user * * :") ... */
423 Client_SetInfo(Client, Req->argv[3]);
425 Client_SetInfo(Client, "-");
427 LogDebug("Connection %d: got valid USER command ...",
428 Client_Conn(Client));
429 if (Client_Type(Client) == CLIENT_GOTNICK)
430 return Hello_User(Client);
432 Client_SetType(Client, CLIENT_GOTUSER);
435 } else if (Client_Type(Client) == CLIENT_SERVER ||
436 Client_Type(Client) == CLIENT_SERVICE) {
437 /* Server/service updating an user */
439 return IRC_WriteStrClient(Client,
440 ERR_NEEDMOREPARAMS_MSG,
443 c = Client_Search(Req->prefix);
445 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
449 Client_SetUser(c, Req->argv[0], true);
450 Client_SetHostname(c, Req->argv[1]);
451 Client_SetInfo(c, Req->argv[3]);
453 LogDebug("Connection %d: got valid USER command for \"%s\".",
454 Client_Conn(Client), Client_Mask(c));
456 } else if (Client_Type(Client) == CLIENT_USER) {
457 /* Already registered connection */
458 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
461 /* Unexpected/invalid connection state? */
462 return IRC_WriteStrClient(Client, ERR_NOTREGISTERED_MSG,
469 * Service registration.
470 * ngIRCd does not support services at the moment, so this function is a
471 * dummy that returns ERR_ERRONEUSNICKNAME on each call.
474 IRC_SERVICE(CLIENT *Client, REQUEST *Req)
476 assert(Client != NULL);
479 if (Client_Type(Client) != CLIENT_GOTPASS)
480 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
484 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
485 Client_ID(Client), Req->command);
487 return IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
488 Client_ID(Client), Req->argv[0]);
493 IRC_QUIT( CLIENT *Client, REQUEST *Req )
496 char quitmsg[LINE_LEN];
498 assert( Client != NULL );
499 assert( Req != NULL );
501 /* Wrong number of arguments? */
503 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
506 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
508 if ( Client_Type( Client ) == CLIENT_SERVER )
511 target = Client_Search( Req->prefix );
514 /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
515 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
519 Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
525 if (Req->argc == 1 && quitmsg[0] != '\"') {
526 /* " " to avoid confusion */
527 strlcpy(quitmsg, "\"", sizeof quitmsg);
528 strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
529 strlcat(quitmsg, "\"", sizeof quitmsg );
532 /* User, Service, oder noch nicht registriert */
533 Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
541 IRC_PING(CLIENT *Client, REQUEST *Req)
543 CLIENT *target, *from;
545 assert(Client != NULL);
548 /* Wrong number of arguments? */
550 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
553 /* Don't ignore additional arguments when in "strict" mode */
555 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
556 Client_ID(Client), Req->command);
560 /* A target has been specified ... */
561 target = Client_Search(Req->argv[1]);
563 if (!target || Client_Type(target) != CLIENT_SERVER)
564 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
565 Client_ID(Client), Req->argv[1]);
567 if (target != Client_ThisServer()) {
568 /* Ok, we have to forward the PING */
569 if (Client_Type(Client) == CLIENT_SERVER)
570 from = Client_Search(Req->prefix);
574 return IRC_WriteStrClient(Client,
575 ERR_NOSUCHSERVER_MSG,
576 Client_ID(Client), Req->prefix);
578 return IRC_WriteStrClientPrefix(target, from,
579 "PING %s :%s", Req->argv[0],
584 if (Client_Type(Client) == CLIENT_SERVER) {
586 from = Client_Search(Req->prefix);
590 from = Client_ThisServer();
592 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
593 Client_ID(Client), Req->prefix);
595 Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
596 Client_Conn(Client));
599 return IRC_WriteStrClient(Client, "PONG %s :%s",
600 Client_ID(from), Client_ID(Client));
602 /* Some clients depend on the argument being returned in the PONG
603 * reply (not mentioned in any RFC, though) */
604 return IRC_WriteStrClient(Client, "PONG %s :%s",
605 Client_ID(from), Req->argv[0]);
611 IRC_PONG(CLIENT *Client, REQUEST *Req)
613 CLIENT *target, *from;
616 assert(Client != NULL);
619 /* Wrong number of arguments? */
621 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
624 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
625 Client_ID(Client), Req->command);
628 if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
629 target = Client_Search(Req->argv[0]);
631 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
632 Client_ID(Client), Req->argv[0]);
634 from = Client_Search(Req->prefix);
636 if (target != Client_ThisServer() && target != from) {
637 /* Ok, we have to forward the message. */
639 return IRC_WriteStrClient(Client,
640 ERR_NOSUCHSERVER_MSG,
641 Client_ID(Client), Req->prefix);
643 if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
647 return IRC_WriteStrClientPrefix(target, from,
648 "PONG %s :%s", s, Req->argv[1]);
652 /* The connection timestamp has already been updated when the data has
653 * been read from so socket, so we don't need to update it here. */
655 if (Client_Conn(Client) > NONE)
657 "Connection %d: received PONG. Lag: %ld seconds.",
659 time(NULL) - Conn_LastPing(Client_Conn(Client)));
662 "Connection %d: received PONG.", Client_Conn(Client));
669 Hello_User(CLIENT * Client)
671 assert(Client != NULL);
673 /* Check password ... */
674 if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
677 "User \"%s\" rejected (connection %d): Bad password!",
678 Client_Mask(Client), Client_Conn(Client));
679 Conn_Close(Client_Conn(Client), NULL, "Bad password", true);
683 Log(LOG_NOTICE, "User \"%s\" registered (connection %d).",
684 Client_Mask(Client), Client_Conn(Client));
686 /* Inform other servers */
687 IRC_WriteStrServers(NULL, "NICK %s 1 %s %s 1 +%s :%s",
688 Client_ID(Client), Client_User(Client),
689 Client_Hostname(Client), Client_Modes(Client),
690 Client_Info(Client));
692 if (!IRC_WriteStrClient
693 (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
695 if (!IRC_WriteStrClient
696 (Client, RPL_YOURHOST_MSG, Client_ID(Client),
697 Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
698 TARGET_VENDOR, TARGET_OS))
700 if (!IRC_WriteStrClient
701 (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
703 if (!IRC_WriteStrClient
704 (Client, RPL_MYINFO_MSG, Client_ID(Client),
705 Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
709 /* Features supported by this server (005 numeric, ISUPPORT),
710 * see <http://www.irc.org/tech_docs/005.html> for details. */
711 if (!IRC_Send_ISUPPORT(Client))
714 Client_SetType(Client, CLIENT_USER);
716 if (!IRC_Send_LUSERS(Client))
718 if (!IRC_Show_MOTD(Client))
721 /* Suspend the client for a second ... */
722 IRC_SetPenalty(Client, 1);
729 Kill_Nick( char *Nick, char *Reason )
733 assert( Nick != NULL );
734 assert( Reason != NULL );
736 r.prefix = (char *)Client_ThisServer( );
741 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
742 IRC_KILL( Client_ThisServer( ), &r );