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;
171 char *nick, *user, *hostname, *modes, *info;
174 assert( Client != NULL );
175 assert( Req != NULL );
178 /* Some IRC clients, for example BitchX, send the NICK and USER
179 * commands in the wrong order ... */
180 if( Client_Type( Client ) == CLIENT_UNKNOWN
181 || Client_Type( Client ) == CLIENT_GOTPASS
182 || Client_Type( Client ) == CLIENT_GOTNICK
183 || Client_Type( Client ) == CLIENT_GOTUSER
184 || Client_Type( Client ) == CLIENT_USER
185 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
187 if( Client_Type( Client ) == CLIENT_UNKNOWN
188 || Client_Type( Client ) == CLIENT_GOTPASS
189 || Client_Type( Client ) == CLIENT_GOTNICK
190 || Client_Type( Client ) == CLIENT_USER
191 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
194 /* User registration or change of nickname */
196 /* Wrong number of arguments? */
198 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
202 /* Search "target" client */
203 if( Client_Type( Client ) == CLIENT_SERVER )
205 target = Client_Search( Req->prefix );
207 return IRC_WriteStrClient( Client,
214 /* Is this a restricted client? */
215 if( Client_HasMode( Client, 'r' ))
216 return IRC_WriteStrClient( Client,
218 Client_ID( Client ));
224 /* If the clients tries to change to its own nickname we won't
225 * do anything. This is how the original ircd behaves and some
226 * clients (for example Snak) expect it to be like this.
227 * But I doubt that this is "really the right thing" ... */
228 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 )
232 /* Check that the new nickname is available. Special case:
233 * the client only changes from/to upper to lower case. */
234 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
236 if( ! Client_CheckNick( target, Req->argv[0] ))
240 if(( Client_Type( target ) != CLIENT_USER )
241 && ( Client_Type( target ) != CLIENT_SERVER ))
244 Log( LOG_DEBUG, "Connection %d: got valid NICK command ...",
245 Client_Conn( Client ));
247 /* Register new nickname of this client */
248 Client_SetID( target, Req->argv[0] );
250 /* If we received a valid USER command already then
251 * register the new client! */
252 if( Client_Type( Client ) == CLIENT_GOTUSER )
253 return Hello_User( Client );
255 Client_SetType( Client, CLIENT_GOTNICK );
259 /* Nickname change */
260 if (Client_Conn(target) > NONE) {
263 "User \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
264 Client_Mask(target), Client_Conn(target),
265 Client_ID(target), Req->argv[0]);
266 Conn_UpdateIdle(Client_Conn(target));
272 "User \"%s\" changed nick: \"%s\" -> \"%s\".",
273 Client_Mask( target ), Client_ID( target ),
277 /* Inform all users and servers (which have to know)
278 * of this nickname change */
279 if( Client_Type( Client ) == CLIENT_USER )
280 IRC_WriteStrClientPrefix( Client, Client,
283 IRC_WriteStrServersPrefix( Client, target,
284 "NICK :%s", Req->argv[0] );
285 IRC_WriteStrRelatedPrefix( target, target, false,
286 "NICK :%s", Req->argv[0] );
288 /* Register old nickname for WHOWAS queries */
289 Client_RegisterWhowas( target );
291 /* Save new nickname */
292 Client_SetID( target, Req->argv[0] );
294 IRC_SetPenalty( target, 2 );
298 } else if(Client_Type(Client) == CLIENT_SERVER ||
299 Client_Type(Client) == CLIENT_SERVICE) {
300 /* Server or service introduces new client */
302 /* Bad number of parameters? */
303 if (Req->argc != 2 && Req->argc != 7)
304 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
305 Client_ID(Client), Req->command);
307 if (Req->argc >= 7) {
309 hops = atoi(Req->argv[1]);
311 hostname = Req->argv[3];
312 token = atoi(Req->argv[4]);
313 modes = Req->argv[5] + 1;
319 hostname = Client_ID(Client);
320 token = atoi(Req->argv[1]);
325 /* Nick ueberpruefen */
326 c = Client_Search(nick);
328 /* Der neue Nick ist auf diesem Server bereits registriert:
329 * sowohl der neue, als auch der alte Client muessen nun
330 * disconnectiert werden. */
331 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
332 Kill_Nick( Req->argv[0], "Nick collision" );
336 /* Server, zu dem der Client connectiert ist, suchen */
337 intr_c = Client_GetFromToken(Client, token);
340 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
341 Kill_Nick( Req->argv[0], "Unknown server" );
345 /* Neue Client-Struktur anlegen */
346 c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
347 token, modes, info, true);
350 /* Eine neue Client-Struktur konnte nicht angelegt werden.
351 * Der Client muss disconnectiert werden, damit der Netz-
352 * status konsistent bleibt. */
353 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
354 Kill_Nick( Req->argv[0], "Server error" );
358 modes = Client_Modes( c );
359 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": "" );
360 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": "" );
362 /* Andere Server, ausser dem Introducer, informieren */
363 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] );
367 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
372 IRC_USER( CLIENT *Client, REQUEST *Req )
378 assert( Client != NULL );
379 assert( Req != NULL );
382 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
384 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
387 /* Wrong number of parameters? */
388 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
392 ptr = Client_User( Client );
393 if( ! ptr || ! *ptr || *ptr == '~' ) Client_SetUser( Client, Req->argv[0], false );
395 Client_SetUser( Client, Req->argv[0], false );
398 /* "Real name" or user info text: Don't set it to the empty string, the original ircd
399 * can't deal with such "real names" (e. g. "USER user * * :") ... */
400 if( *Req->argv[3] ) Client_SetInfo( Client, Req->argv[3] );
401 else Client_SetInfo( Client, "-" );
403 Log( LOG_DEBUG, "Connection %d: got valid USER command ...", Client_Conn( Client ));
404 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
405 else Client_SetType( Client, CLIENT_GOTUSER );
408 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
410 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
412 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
417 * Service registration.
418 * ngIRCd does not support services at the moment, so this function is a
419 * dummy that returns ERR_ERRONEUSNICKNAME on each call.
422 IRC_SERVICE(CLIENT *Client, REQUEST *Req)
424 assert(Client != NULL);
427 if (Client_Type(Client) != CLIENT_GOTPASS)
428 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
432 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
433 Client_ID(Client), Req->command);
435 return IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
436 Client_ID(Client), Req->argv[0]);
441 IRC_QUIT( CLIENT *Client, REQUEST *Req )
444 char quitmsg[LINE_LEN];
446 assert( Client != NULL );
447 assert( Req != NULL );
449 /* Wrong number of arguments? */
451 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
454 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
456 if ( Client_Type( Client ) == CLIENT_SERVER )
459 target = Client_Search( Req->prefix );
462 /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
463 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
467 Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
473 if (Req->argc == 1 && quitmsg[0] != '\"') {
474 /* " " to avoid confusion */
475 strlcpy(quitmsg, "\"", sizeof quitmsg);
476 strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
477 strlcat(quitmsg, "\"", sizeof quitmsg );
480 /* User, Service, oder noch nicht registriert */
481 Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
489 IRC_PING(CLIENT *Client, REQUEST *Req)
491 CLIENT *target, *from;
493 assert(Client != NULL);
496 /* Wrong number of arguments? */
498 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
501 /* Don't ignore additional arguments when in "strict" mode */
503 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
504 Client_ID(Client), Req->command);
508 /* A target has been specified ... */
509 target = Client_Search(Req->argv[1]);
511 if (!target || Client_Type(target) != CLIENT_SERVER)
512 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
513 Client_ID(Client), Req->argv[1]);
515 if (target != Client_ThisServer()) {
516 /* Ok, we have to forward the PING */
517 if (Client_Type(Client) == CLIENT_SERVER)
518 from = Client_Search(Req->prefix);
522 return IRC_WriteStrClient(Client,
523 ERR_NOSUCHSERVER_MSG,
524 Client_ID(Client), Req->prefix);
526 return IRC_WriteStrClientPrefix(target, from,
527 "PING %s :%s", Req->argv[0],
532 if (Client_Type(Client) == CLIENT_SERVER) {
534 from = Client_Search(Req->prefix);
538 from = Client_ThisServer();
540 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
541 Client_ID(Client), Req->prefix);
543 Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
544 Client_Conn(Client));
547 return IRC_WriteStrClient(Client, "PONG %s :%s",
548 Client_ID(from), Client_ID(Client));
550 /* Some clients depend on the argument being returned in the PONG
551 * reply (not mentioned in any RFC, though) */
552 return IRC_WriteStrClient(Client, "PONG %s :%s",
553 Client_ID(from), Req->argv[0]);
559 IRC_PONG(CLIENT *Client, REQUEST *Req)
561 CLIENT *target, *from;
564 assert(Client != NULL);
567 /* Wrong number of arguments? */
569 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
572 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
573 Client_ID(Client), Req->command);
576 if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
577 target = Client_Search(Req->argv[0]);
579 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
580 Client_ID(Client), Req->argv[0]);
582 from = Client_Search(Req->prefix);
584 if (target != Client_ThisServer() && target != from) {
585 /* Ok, we have to forward the message. */
587 return IRC_WriteStrClient(Client,
588 ERR_NOSUCHSERVER_MSG,
589 Client_ID(Client), Req->prefix);
591 if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
595 return IRC_WriteStrClientPrefix(target, from,
596 "PONG %s :%s", s, Req->argv[1]);
600 /* The connection timestamp has already been updated when the data has
601 * been read from so socket, so we don't need to update it here. */
603 if (Client_Conn(Client) > NONE)
605 "Connection %d: received PONG. Lag: %ld seconds.",
607 time(NULL) - Conn_LastPing(Client_Conn(Client)));
610 "Connection %d: received PONG.", Client_Conn(Client));
617 Hello_User(CLIENT * Client)
619 assert(Client != NULL);
621 /* Check password ... */
622 if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
625 "User \"%s\" rejected (connection %d): Bad password!",
626 Client_Mask(Client), Client_Conn(Client));
627 Conn_Close(Client_Conn(Client), NULL, "Bad password", true);
631 Log(LOG_NOTICE, "User \"%s\" registered (connection %d).",
632 Client_Mask(Client), Client_Conn(Client));
634 /* Inform other servers */
635 IRC_WriteStrServers(NULL, "NICK %s 1 %s %s 1 +%s :%s",
636 Client_ID(Client), Client_User(Client),
637 Client_Hostname(Client), Client_Modes(Client),
638 Client_Info(Client));
640 if (!IRC_WriteStrClient
641 (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
643 if (!IRC_WriteStrClient
644 (Client, RPL_YOURHOST_MSG, Client_ID(Client),
645 Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
646 TARGET_VENDOR, TARGET_OS))
648 if (!IRC_WriteStrClient
649 (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
651 if (!IRC_WriteStrClient
652 (Client, RPL_MYINFO_MSG, Client_ID(Client),
653 Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
657 /* Features supported by this server (005 numeric, ISUPPORT),
658 * see <http://www.irc.org/tech_docs/005.html> for details. */
659 if (!IRC_Send_ISUPPORT(Client))
662 Client_SetType(Client, CLIENT_USER);
664 if (!IRC_Send_LUSERS(Client))
666 if (!IRC_Show_MOTD(Client))
669 /* Suspend the client for a second ... */
670 IRC_SetPenalty(Client, 1);
677 Kill_Nick( char *Nick, char *Reason )
681 assert( Nick != NULL );
682 assert( Reason != NULL );
684 r.prefix = (char *)Client_ThisServer( );
689 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
690 IRC_KILL( Client_ThisServer( ), &r );