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;
173 assert( Client != NULL );
174 assert( Req != NULL );
177 /* Some IRC clients, for example BitchX, send the NICK and USER
178 * commands in the wrong order ... */
179 if( Client_Type( Client ) == CLIENT_UNKNOWN
180 || Client_Type( Client ) == CLIENT_GOTPASS
181 || Client_Type( Client ) == CLIENT_GOTNICK
182 || Client_Type( Client ) == CLIENT_GOTUSER
183 || Client_Type( Client ) == CLIENT_USER
184 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
186 if( Client_Type( Client ) == CLIENT_UNKNOWN
187 || Client_Type( Client ) == CLIENT_GOTPASS
188 || Client_Type( Client ) == CLIENT_GOTNICK
189 || Client_Type( Client ) == CLIENT_USER
190 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
193 /* User registration or change of nickname */
195 /* Wrong number of arguments? */
197 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
201 /* Search "target" client */
202 if( Client_Type( Client ) == CLIENT_SERVER )
204 target = Client_Search( Req->prefix );
206 return IRC_WriteStrClient( Client,
213 /* Is this a restricted client? */
214 if( Client_HasMode( Client, 'r' ))
215 return IRC_WriteStrClient( Client,
217 Client_ID( Client ));
223 /* If the clients tries to change to its own nickname we won't
224 * do anything. This is how the original ircd behaves and some
225 * clients (for example Snak) expect it to be like this.
226 * But I doubt that this is "really the right thing" ... */
227 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 )
231 /* Check that the new nickname is available. Special case:
232 * the client only changes from/to upper to lower case. */
233 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
235 if( ! Client_CheckNick( target, Req->argv[0] ))
239 if(( Client_Type( target ) != CLIENT_USER )
240 && ( Client_Type( target ) != CLIENT_SERVER ))
243 Log( LOG_DEBUG, "Connection %d: got valid NICK command ...",
244 Client_Conn( Client ));
246 /* Register new nickname of this client */
247 Client_SetID( target, Req->argv[0] );
249 /* If we received a valid USER command already then
250 * register the new client! */
251 if( Client_Type( Client ) == CLIENT_GOTUSER )
252 return Hello_User( Client );
254 Client_SetType( Client, CLIENT_GOTNICK );
258 /* Nickname change */
259 if (Client_Conn(target) > NONE) {
262 "User \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
263 Client_Mask(target), Client_Conn(target),
264 Client_ID(target), Req->argv[0]);
265 Conn_UpdateIdle(Client_Conn(target));
271 "User \"%s\" changed nick: \"%s\" -> \"%s\".",
272 Client_Mask( target ), Client_ID( target ),
276 /* Inform all users and servers (which have to know)
277 * of this nickname change */
278 if( Client_Type( Client ) == CLIENT_USER )
279 IRC_WriteStrClientPrefix( Client, Client,
282 IRC_WriteStrServersPrefix( Client, target,
283 "NICK :%s", Req->argv[0] );
284 IRC_WriteStrRelatedPrefix( target, target, false,
285 "NICK :%s", Req->argv[0] );
287 /* Register old nickname for WHOWAS queries */
288 Client_RegisterWhowas( target );
290 /* Save new nickname */
291 Client_SetID( target, Req->argv[0] );
293 IRC_SetPenalty( target, 2 );
298 else if( Client_Type( Client ) == CLIENT_SERVER )
300 /* Server introduces new client */
302 /* Falsche Anzahl Parameter? */
303 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
305 /* Nick ueberpruefen */
306 c = Client_Search( Req->argv[0] );
309 /* Der neue Nick ist auf diesem Server bereits registriert:
310 * sowohl der neue, als auch der alte Client muessen nun
311 * disconnectiert werden. */
312 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
313 Kill_Nick( Req->argv[0], "Nick collision" );
317 /* Server, zu dem der Client connectiert ist, suchen */
318 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
321 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
322 Kill_Nick( Req->argv[0], "Unknown server" );
326 /* Neue Client-Struktur anlegen */
327 c = Client_NewRemoteUser( intr_c, Req->argv[0], atoi( Req->argv[1] ), Req->argv[2], Req->argv[3], atoi( Req->argv[4] ), Req->argv[5] + 1, Req->argv[6], true);
330 /* Eine neue Client-Struktur konnte nicht angelegt werden.
331 * Der Client muss disconnectiert werden, damit der Netz-
332 * status konsistent bleibt. */
333 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
334 Kill_Nick( Req->argv[0], "Server error" );
338 modes = Client_Modes( c );
339 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": "" );
340 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": "" );
342 /* Andere Server, ausser dem Introducer, informieren */
343 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] );
347 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
352 IRC_USER( CLIENT *Client, REQUEST *Req )
358 assert( Client != NULL );
359 assert( Req != NULL );
362 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
364 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
367 /* Wrong number of parameters? */
368 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
372 ptr = Client_User( Client );
373 if( ! ptr || ! *ptr || *ptr == '~' ) Client_SetUser( Client, Req->argv[0], false );
375 Client_SetUser( Client, Req->argv[0], false );
378 /* "Real name" or user info text: Don't set it to the empty string, the original ircd
379 * can't deal with such "real names" (e. g. "USER user * * :") ... */
380 if( *Req->argv[3] ) Client_SetInfo( Client, Req->argv[3] );
381 else Client_SetInfo( Client, "-" );
383 Log( LOG_DEBUG, "Connection %d: got valid USER command ...", Client_Conn( Client ));
384 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
385 else Client_SetType( Client, CLIENT_GOTUSER );
388 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
390 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
392 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
397 * Service registration.
398 * ngIRCd does not support services at the moment, so this function is a
399 * dummy that returns ERR_ERRONEUSNICKNAME on each call.
402 IRC_SERVICE(CLIENT *Client, REQUEST *Req)
404 assert(Client != NULL);
407 if (Client_Type(Client) != CLIENT_GOTPASS)
408 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
412 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
413 Client_ID(Client), Req->command);
415 return IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
416 Client_ID(Client), Req->argv[0]);
421 IRC_QUIT( CLIENT *Client, REQUEST *Req )
424 char quitmsg[LINE_LEN];
426 assert( Client != NULL );
427 assert( Req != NULL );
429 /* Wrong number of arguments? */
431 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
434 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
436 if ( Client_Type( Client ) == CLIENT_SERVER )
439 target = Client_Search( Req->prefix );
442 /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
443 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
447 Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
453 if (Req->argc == 1 && quitmsg[0] != '\"') {
454 /* " " to avoid confusion */
455 strlcpy(quitmsg, "\"", sizeof quitmsg);
456 strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
457 strlcat(quitmsg, "\"", sizeof quitmsg );
460 /* User, Service, oder noch nicht registriert */
461 Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
469 IRC_PING(CLIENT *Client, REQUEST *Req)
471 CLIENT *target, *from;
473 assert(Client != NULL);
476 /* Wrong number of arguments? */
478 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
481 /* Don't ignore additional arguments when in "strict" mode */
483 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
484 Client_ID(Client), Req->command);
488 /* A target has been specified ... */
489 target = Client_Search(Req->argv[1]);
491 if (!target || Client_Type(target) != CLIENT_SERVER)
492 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
493 Client_ID(Client), Req->argv[1]);
495 if (target != Client_ThisServer()) {
496 /* Ok, we have to forward the PING */
497 if (Client_Type(Client) == CLIENT_SERVER)
498 from = Client_Search(Req->prefix);
502 return IRC_WriteStrClient(Client,
503 ERR_NOSUCHSERVER_MSG,
504 Client_ID(Client), Req->prefix);
506 return IRC_WriteStrClientPrefix(target, from,
507 "PING %s :%s", Req->argv[0],
512 if (Client_Type(Client) == CLIENT_SERVER) {
514 from = Client_Search(Req->prefix);
518 from = Client_ThisServer();
520 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
521 Client_ID(Client), Req->prefix);
523 Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
524 Client_Conn(Client));
527 return IRC_WriteStrClient(Client, "PONG %s :%s",
528 Client_ID(from), Client_ID(Client));
530 /* Some clients depend on the argument being returned in the PONG
531 * reply (not mentioned in any RFC, though) */
532 return IRC_WriteStrClient(Client, "PONG %s :%s",
533 Client_ID(from), Req->argv[0]);
539 IRC_PONG(CLIENT *Client, REQUEST *Req)
541 CLIENT *target, *from;
544 assert(Client != NULL);
547 /* Wrong number of arguments? */
549 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
552 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
553 Client_ID(Client), Req->command);
556 if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
557 target = Client_Search(Req->argv[0]);
559 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
560 Client_ID(Client), Req->argv[0]);
562 from = Client_Search(Req->prefix);
564 if (target != Client_ThisServer() && target != from) {
565 /* Ok, we have to forward the message. */
567 return IRC_WriteStrClient(Client,
568 ERR_NOSUCHSERVER_MSG,
569 Client_ID(Client), Req->prefix);
571 if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
575 return IRC_WriteStrClientPrefix(target, from,
576 "PONG %s :%s", s, Req->argv[1]);
580 /* The connection timestamp has already been updated when the data has
581 * been read from so socket, so we don't need to update it here. */
583 if (Client_Conn(Client) > NONE)
585 "Connection %d: received PONG. Lag: %ld seconds.",
587 time(NULL) - Conn_LastPing(Client_Conn(Client)));
590 "Connection %d: received PONG.", Client_Conn(Client));
597 Hello_User(CLIENT * Client)
599 assert(Client != NULL);
601 /* Check password ... */
602 if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
605 "User \"%s\" rejected (connection %d): Bad password!",
606 Client_Mask(Client), Client_Conn(Client));
607 Conn_Close(Client_Conn(Client), NULL, "Bad password", true);
611 Log(LOG_NOTICE, "User \"%s\" registered (connection %d).",
612 Client_Mask(Client), Client_Conn(Client));
614 /* Inform other servers */
615 IRC_WriteStrServers(NULL, "NICK %s 1 %s %s 1 +%s :%s",
616 Client_ID(Client), Client_User(Client),
617 Client_Hostname(Client), Client_Modes(Client),
618 Client_Info(Client));
620 if (!IRC_WriteStrClient
621 (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
623 if (!IRC_WriteStrClient
624 (Client, RPL_YOURHOST_MSG, Client_ID(Client),
625 Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
626 TARGET_VENDOR, TARGET_OS))
628 if (!IRC_WriteStrClient
629 (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
631 if (!IRC_WriteStrClient
632 (Client, RPL_MYINFO_MSG, Client_ID(Client),
633 Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
637 /* Features supported by this server (005 numeric, ISUPPORT),
638 * see <http://www.irc.org/tech_docs/005.html> for details. */
639 if (!IRC_Send_ISUPPORT(Client))
642 Client_SetType(Client, CLIENT_USER);
644 if (!IRC_Send_LUSERS(Client))
646 if (!IRC_Show_MOTD(Client))
649 /* Suspend the client for a second ... */
650 IRC_SetPenalty(Client, 1);
657 Kill_Nick( char *Nick, char *Reason )
661 assert( Nick != NULL );
662 assert( Reason != NULL );
664 r.prefix = (char *)Client_ThisServer( );
669 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
670 IRC_KILL( Client_ThisServer( ), &r );