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 ));
43 static void Introduce_Client PARAMS(( CLIENT *From, const char *Nick,
44 const int HopCount, const char *User,
45 const char *Host, const int Token,
46 const char *Mode, const char *Name ));
50 * Handler for the IRC command "PASS".
51 * See RFC 2813 section 4.1.1, and RFC 2812 section 3.1.1.
54 IRC_PASS( CLIENT *Client, REQUEST *Req )
56 char *type, *orig_flags;
57 int protohigh, protolow;
59 assert( Client != NULL );
60 assert( Req != NULL );
62 /* Return an error if this is not a local client */
63 if (Client_Conn(Client) <= NONE)
64 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
65 Client_ID(Client), Req->command);
67 if (Client_Type(Client) == CLIENT_UNKNOWN && Req->argc == 1) {
68 /* Not yet registered "unknown" connection, PASS with one
69 * argument: either a regular client, service, or server
70 * using the old RFC 1459 section 4.1.1 syntax. */
71 LogDebug("Connection %d: got PASS command ...",
73 } else if ((Client_Type(Client) == CLIENT_UNKNOWN ||
74 Client_Type(Client) == CLIENT_UNKNOWNSERVER) &&
75 (Req->argc == 3 || Req->argc == 4)) {
76 /* Not yet registered "unknown" connection or outgoing server
77 * link, PASS with three or four argument: server using the
78 * RFC 2813 section 4.1.1 syntax. */
79 LogDebug("Connection %d: got PASS command (new server link) ...",
81 } else if (Client_Type(Client) == CLIENT_UNKNOWN ||
82 Client_Type(Client) == CLIENT_UNKNOWNSERVER) {
83 /* Unregistered connection, but wrong number of arguments: */
84 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
85 Client_ID(Client), Req->command);
87 /* Registered connection, PASS command is not allowed! */
88 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
92 Client_SetPassword(Client, Req->argv[0]);
93 Client_SetType(Client, CLIENT_GOTPASS);
95 /* Protocol version */
96 if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
100 c4 = Req->argv[1][4];
102 Req->argv[1][4] = '\0';
103 protolow = atoi(&Req->argv[1][2]);
104 Req->argv[1][2] = '\0';
105 protohigh = atoi(Req->argv[1]);
107 Req->argv[1][2] = c2;
108 Req->argv[1][4] = c4;
110 protohigh = protolow = 0;
112 /* Protocol type, see doc/Protocol.txt */
113 if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
114 type = &Req->argv[1][4];
118 /* Protocol flags/options */
120 orig_flags = Req->argv[3];
124 /* Implementation, version and IRC+ flags */
125 if (Req->argc >= 3) {
126 char *impl, *ptr, *serverver, *flags;
129 ptr = strchr(impl, '|');
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). */
137 flags = strchr(serverver, ':');
144 "Peer announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
145 impl, serverver, protohigh, protolow, flags);
147 /* The peer seems to be a server supporting the
148 * "original" IRC protocol (RFC 2813). */
150 if (strchr(orig_flags, 'Z'))
155 "Peer announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
156 impl, protohigh, protolow, flags);
158 Client_SetFlags(Client, flags);
166 * IRC "NICK" command.
167 * This function implements the IRC command "NICK" which is used to register
168 * with the server, to change already registered nicknames and to introduce
169 * new users which are connected to other servers.
172 IRC_NICK( CLIENT *Client, REQUEST *Req )
174 CLIENT *intr_c, *target, *c;
176 char *nick, *user, *hostname, *modes, *info;
179 assert( Client != NULL );
180 assert( Req != NULL );
183 /* Some IRC clients, for example BitchX, send the NICK and USER
184 * commands in the wrong order ... */
185 if( Client_Type( Client ) == CLIENT_UNKNOWN
186 || Client_Type( Client ) == CLIENT_GOTPASS
187 || Client_Type( Client ) == CLIENT_GOTNICK
188 || Client_Type( Client ) == CLIENT_GOTUSER
189 || Client_Type( Client ) == CLIENT_USER
190 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
192 if( Client_Type( Client ) == CLIENT_UNKNOWN
193 || Client_Type( Client ) == CLIENT_GOTPASS
194 || Client_Type( Client ) == CLIENT_GOTNICK
195 || Client_Type( Client ) == CLIENT_USER
196 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
199 /* User registration or change of nickname */
201 /* Wrong number of arguments? */
203 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
207 /* Search "target" client */
208 if( Client_Type( Client ) == CLIENT_SERVER )
210 target = Client_Search( Req->prefix );
212 return IRC_WriteStrClient( Client,
219 /* Is this a restricted client? */
220 if( Client_HasMode( Client, 'r' ))
221 return IRC_WriteStrClient( Client,
223 Client_ID( Client ));
229 /* If the clients tries to change to its own nickname we won't
230 * do anything. This is how the original ircd behaves and some
231 * clients (for example Snak) expect it to be like this.
232 * But I doubt that this is "really the right thing" ... */
233 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 )
237 /* Check that the new nickname is available. Special case:
238 * the client only changes from/to upper to lower case. */
239 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
241 if( ! Client_CheckNick( target, Req->argv[0] ))
245 if(( Client_Type( target ) != CLIENT_USER )
246 && ( Client_Type( target ) != CLIENT_SERVER ))
249 Log( LOG_DEBUG, "Connection %d: got valid NICK command ...",
250 Client_Conn( Client ));
252 /* Register new nickname of this client */
253 Client_SetID( target, Req->argv[0] );
255 /* If we received a valid USER command already then
256 * register the new client! */
257 if( Client_Type( Client ) == CLIENT_GOTUSER )
258 return Hello_User( Client );
260 Client_SetType( Client, CLIENT_GOTNICK );
264 /* Nickname change */
265 if (Client_Conn(target) > NONE) {
268 "User \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
269 Client_Mask(target), Client_Conn(target),
270 Client_ID(target), Req->argv[0]);
271 Conn_UpdateIdle(Client_Conn(target));
277 "User \"%s\" changed nick: \"%s\" -> \"%s\".",
278 Client_Mask( target ), Client_ID( target ),
282 /* Inform all users and servers (which have to know)
283 * of this nickname change */
284 if( Client_Type( Client ) == CLIENT_USER )
285 IRC_WriteStrClientPrefix( Client, Client,
288 IRC_WriteStrServersPrefix( Client, target,
289 "NICK :%s", Req->argv[0] );
290 IRC_WriteStrRelatedPrefix( target, target, false,
291 "NICK :%s", Req->argv[0] );
293 /* Register old nickname for WHOWAS queries */
294 Client_RegisterWhowas( target );
296 /* Save new nickname */
297 Client_SetID( target, Req->argv[0] );
299 IRC_SetPenalty( target, 2 );
303 } else if(Client_Type(Client) == CLIENT_SERVER ||
304 Client_Type(Client) == CLIENT_SERVICE) {
305 /* Server or service introduces new client */
307 /* Bad number of parameters? */
308 if (Req->argc != 2 && Req->argc != 7)
309 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
310 Client_ID(Client), Req->command);
312 if (Req->argc >= 7) {
313 /* RFC 2813 compatible syntax */
315 hops = atoi(Req->argv[1]);
317 hostname = Req->argv[3];
318 token = atoi(Req->argv[4]);
319 modes = Req->argv[5] + 1;
322 /* RFC 1459 compatible syntax */
326 hostname = Client_ID(Client);
327 token = atoi(Req->argv[1]);
331 conn = Client_Conn(Client);
333 !(Conn_Options(conn) & CONN_RFC1459)) {
335 "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
336 conn, Client_ID(Client));
337 Conn_SetOption(conn, CONN_RFC1459);
341 /* Nick ueberpruefen */
342 c = Client_Search(nick);
344 /* Der neue Nick ist auf diesem Server bereits registriert:
345 * sowohl der neue, als auch der alte Client muessen nun
346 * disconnectiert werden. */
347 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
348 Kill_Nick( Req->argv[0], "Nick collision" );
352 /* Server, zu dem der Client connectiert ist, suchen */
353 intr_c = Client_GetFromToken(Client, token);
356 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
357 Kill_Nick( Req->argv[0], "Unknown server" );
361 /* Neue Client-Struktur anlegen */
362 c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
363 token, modes, info, true);
366 /* Eine neue Client-Struktur konnte nicht angelegt werden.
367 * Der Client muss disconnectiert werden, damit der Netz-
368 * status konsistent bleibt. */
369 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
370 Kill_Nick( Req->argv[0], "Server error" );
374 modes = Client_Modes( c );
375 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": "" );
376 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": "" );
378 /* Inform other servers about the new client */
379 Introduce_Client(Client, Req->argv[0], atoi(Req->argv[1]) + 1,
380 Req->argv[2], Req->argv[3],
381 Client_MyToken(intr_c), Req->argv[5],
386 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
391 * Handler for the IRC command "USER".
394 IRC_USER(CLIENT * Client, REQUEST * Req)
401 assert(Client != NULL);
404 if (Client_Type(Client) == CLIENT_GOTNICK ||
406 Client_Type(Client) == CLIENT_UNKNOWN ||
408 Client_Type(Client) == CLIENT_GOTPASS)
412 return IRC_WriteStrClient(Client,
413 ERR_NEEDMOREPARAMS_MSG,
419 ptr = Client_User(Client);
420 if (!ptr || !*ptr || *ptr == '~')
421 Client_SetUser(Client, Req->argv[0], false);
423 Client_SetUser(Client, Req->argv[0], false);
426 /* "Real name" or user info text: Don't set it to the empty
427 * string, the original ircd can't deal with such "real names"
428 * (e. g. "USER user * * :") ... */
430 Client_SetInfo(Client, Req->argv[3]);
432 Client_SetInfo(Client, "-");
434 LogDebug("Connection %d: got valid USER command ...",
435 Client_Conn(Client));
436 if (Client_Type(Client) == CLIENT_GOTNICK)
437 return Hello_User(Client);
439 Client_SetType(Client, CLIENT_GOTUSER);
442 } else if (Client_Type(Client) == CLIENT_SERVER ||
443 Client_Type(Client) == CLIENT_SERVICE) {
444 /* Server/service updating an user */
446 return IRC_WriteStrClient(Client,
447 ERR_NEEDMOREPARAMS_MSG,
450 c = Client_Search(Req->prefix);
452 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
456 Client_SetUser(c, Req->argv[0], true);
457 Client_SetHostname(c, Req->argv[1]);
458 Client_SetInfo(c, Req->argv[3]);
460 LogDebug("Connection %d: got valid USER command for \"%s\".",
461 Client_Conn(Client), Client_Mask(c));
463 } else if (Client_Type(Client) == CLIENT_USER) {
464 /* Already registered connection */
465 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
468 /* Unexpected/invalid connection state? */
469 return IRC_WriteStrClient(Client, ERR_NOTREGISTERED_MSG,
476 * Service registration.
477 * ngIRCd does not support services at the moment, so this function is a
478 * dummy that returns ERR_ERRONEUSNICKNAME on each call.
481 IRC_SERVICE(CLIENT *Client, REQUEST *Req)
483 assert(Client != NULL);
486 if (Client_Type(Client) != CLIENT_GOTPASS)
487 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
491 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
492 Client_ID(Client), Req->command);
494 return IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
495 Client_ID(Client), Req->argv[0]);
500 IRC_QUIT( CLIENT *Client, REQUEST *Req )
503 char quitmsg[LINE_LEN];
505 assert( Client != NULL );
506 assert( Req != NULL );
508 /* Wrong number of arguments? */
510 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
513 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
515 if ( Client_Type( Client ) == CLIENT_SERVER )
518 target = Client_Search( Req->prefix );
521 /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
522 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
526 Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
532 if (Req->argc == 1 && quitmsg[0] != '\"') {
533 /* " " to avoid confusion */
534 strlcpy(quitmsg, "\"", sizeof quitmsg);
535 strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
536 strlcat(quitmsg, "\"", sizeof quitmsg );
539 /* User, Service, oder noch nicht registriert */
540 Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
548 IRC_PING(CLIENT *Client, REQUEST *Req)
550 CLIENT *target, *from;
552 assert(Client != NULL);
555 /* Wrong number of arguments? */
557 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
560 /* Don't ignore additional arguments when in "strict" mode */
562 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
563 Client_ID(Client), Req->command);
567 /* A target has been specified ... */
568 target = Client_Search(Req->argv[1]);
570 if (!target || Client_Type(target) != CLIENT_SERVER)
571 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
572 Client_ID(Client), Req->argv[1]);
574 if (target != Client_ThisServer()) {
575 /* Ok, we have to forward the PING */
576 if (Client_Type(Client) == CLIENT_SERVER)
577 from = Client_Search(Req->prefix);
581 return IRC_WriteStrClient(Client,
582 ERR_NOSUCHSERVER_MSG,
583 Client_ID(Client), Req->prefix);
585 return IRC_WriteStrClientPrefix(target, from,
586 "PING %s :%s", Req->argv[0],
591 if (Client_Type(Client) == CLIENT_SERVER) {
593 from = Client_Search(Req->prefix);
597 from = Client_ThisServer();
599 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
600 Client_ID(Client), Req->prefix);
602 Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
603 Client_Conn(Client));
606 return IRC_WriteStrClient(Client, "PONG %s :%s",
607 Client_ID(from), Client_ID(Client));
609 /* Some clients depend on the argument being returned in the PONG
610 * reply (not mentioned in any RFC, though) */
611 return IRC_WriteStrClient(Client, "PONG %s :%s",
612 Client_ID(from), Req->argv[0]);
618 IRC_PONG(CLIENT *Client, REQUEST *Req)
620 CLIENT *target, *from;
623 assert(Client != NULL);
626 /* Wrong number of arguments? */
628 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
631 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
632 Client_ID(Client), Req->command);
635 if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
636 target = Client_Search(Req->argv[0]);
638 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
639 Client_ID(Client), Req->argv[0]);
641 from = Client_Search(Req->prefix);
643 if (target != Client_ThisServer() && target != from) {
644 /* Ok, we have to forward the message. */
646 return IRC_WriteStrClient(Client,
647 ERR_NOSUCHSERVER_MSG,
648 Client_ID(Client), Req->prefix);
650 if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
654 return IRC_WriteStrClientPrefix(target, from,
655 "PONG %s :%s", s, Req->argv[1]);
659 /* The connection timestamp has already been updated when the data has
660 * been read from so socket, so we don't need to update it here. */
662 if (Client_Conn(Client) > NONE)
664 "Connection %d: received PONG. Lag: %ld seconds.",
666 time(NULL) - Conn_LastPing(Client_Conn(Client)));
669 "Connection %d: received PONG.", Client_Conn(Client));
676 Hello_User(CLIENT * Client)
678 char modes[CLIENT_MODE_LEN + 1] = "+";
680 assert(Client != NULL);
682 /* Check password ... */
683 if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
686 "User \"%s\" rejected (connection %d): Bad password!",
687 Client_Mask(Client), Client_Conn(Client));
688 Conn_Close(Client_Conn(Client), NULL, "Bad password", true);
692 Log(LOG_NOTICE, "User \"%s\" registered (connection %d).",
693 Client_Mask(Client), Client_Conn(Client));
695 /* Inform other servers */
696 strlcat(modes, Client_Modes(Client), sizeof(modes));
697 Introduce_Client(NULL, Client_ID(Client), 1, Client_User(Client),
698 Client_Hostname(Client), 1, modes, Client_Info(Client));
700 if (!IRC_WriteStrClient
701 (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
703 if (!IRC_WriteStrClient
704 (Client, RPL_YOURHOST_MSG, Client_ID(Client),
705 Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
706 TARGET_VENDOR, TARGET_OS))
708 if (!IRC_WriteStrClient
709 (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
711 if (!IRC_WriteStrClient
712 (Client, RPL_MYINFO_MSG, Client_ID(Client),
713 Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
717 /* Features supported by this server (005 numeric, ISUPPORT),
718 * see <http://www.irc.org/tech_docs/005.html> for details. */
719 if (!IRC_Send_ISUPPORT(Client))
722 Client_SetType(Client, CLIENT_USER);
724 if (!IRC_Send_LUSERS(Client))
726 if (!IRC_Show_MOTD(Client))
729 /* Suspend the client for a second ... */
730 IRC_SetPenalty(Client, 1);
737 Kill_Nick( char *Nick, char *Reason )
741 assert( Nick != NULL );
742 assert( Reason != NULL );
744 r.prefix = (char *)Client_ThisServer( );
749 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
750 IRC_KILL( Client_ThisServer( ), &r );
755 Introduce_Client(CLIENT *From, const char *Nick, const int HopCount,
756 const char *User, const char *Host, const int Token, const char *Mode,
759 IRC_WriteStrServersPrefix(From,
760 From != NULL ? From : Client_ThisServer(),
761 "NICK %s %d %s %s %d %s :%s",
762 Nick, HopCount, User, Host, Token, Mode, Name);
763 } /* Introduce_Client */