2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by 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.
17 static char UNUSED id[] = "$Id: irc-login.c,v 1.54 2007/11/21 12:16:36 alex Exp $";
28 #include "conn-func.h"
37 #include "irc-write.h"
38 #include "cvs-version.h"
41 #include "irc-login.h"
44 static bool Hello_User PARAMS(( CLIENT *Client ));
45 static void Kill_Nick PARAMS(( char *Nick, char *Reason ));
49 * Handler for the IRC command "PASS".
50 * See RFC 2813 section 4.1.1, and RFC 2812 section 3.1.1.
53 IRC_PASS( CLIENT *Client, REQUEST *Req )
55 char *type, *orig_flags;
56 int protohigh, protolow;
58 assert( Client != NULL );
59 assert( Req != NULL );
61 /* Return an error if this is not a local client */
62 if (Client_Conn(Client) <= NONE)
63 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
64 Client_ID(Client), Req->command);
66 if (Client_Type(Client) == CLIENT_UNKNOWN && Req->argc == 1) {
67 /* Not yet registered "unknown" connection, PASS with one
68 * argument: either a regular client, service, or server
69 * using the old RFC 1459 section 4.1.1 syntax. */
70 LogDebug("Connection %d: got PASS command ...",
72 } else if ((Client_Type(Client) == CLIENT_UNKNOWN ||
73 Client_Type(Client) == CLIENT_UNKNOWNSERVER) &&
74 (Req->argc == 3 || Req->argc == 4)) {
75 /* Not yet registered "unknown" connection or outgoing server
76 * link, PASS with three or four argument: server using the
77 * RFC 2813 section 4.1.1 syntax. */
78 LogDebug("Connection %d: got PASS command (new server link) ...",
80 } else if (Client_Type(Client) == CLIENT_UNKNOWN ||
81 Client_Type(Client) == CLIENT_UNKNOWNSERVER) {
82 /* Unregistered connection, but wrong number of arguments: */
83 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
84 Client_ID(Client), Req->command);
86 /* Registered connection, PASS command is not allowed! */
87 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
91 Client_SetPassword(Client, Req->argv[0]);
92 Client_SetType(Client, CLIENT_GOTPASS);
94 /* Protocol version */
95 if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
101 Req->argv[1][4] = '\0';
102 protolow = atoi(&Req->argv[1][2]);
103 Req->argv[1][2] = '\0';
104 protohigh = atoi(Req->argv[1]);
106 Req->argv[1][2] = c2;
107 Req->argv[1][4] = c4;
109 protohigh = protolow = 0;
111 /* Protocol type, see doc/Protocol.txt */
112 if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
113 type = &Req->argv[1][4];
117 /* Protocol flags/options */
119 orig_flags = Req->argv[3];
123 /* Implementation, version and IRC+ flags */
124 if (Req->argc >= 3) {
125 char *impl, *ptr, *serverver, *flags;
128 ptr = strchr(impl, '|');
132 if (type && strcmp(type, PROTOIRCPLUS) == 0) {
133 /* The peer seems to be a server which supports the
134 * IRC+ protocol (see doc/Protocol.txt). */
136 flags = strchr(serverver, ':');
143 "Peer announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
144 impl, serverver, protohigh, protolow, flags);
146 /* The peer seems to be a server supporting the
147 * "original" IRC protocol (RFC 2813). */
149 if (strchr(orig_flags, 'Z'))
154 "Peer announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
155 impl, protohigh, protolow, flags);
157 Client_SetFlags(Client, flags);
165 * IRC "NICK" command.
166 * This function implements the IRC command "NICK" which is used to register
167 * with the server, to change already registered nicknames and to introduce
168 * new users which are connected to other servers.
171 IRC_NICK( CLIENT *Client, REQUEST *Req )
173 CLIENT *intr_c, *target, *c;
176 assert( Client != NULL );
177 assert( Req != NULL );
180 /* Some IRC clients, for example BitchX, send the NICK and USER
181 * commands in the wrong order ... */
182 if( Client_Type( Client ) == CLIENT_UNKNOWN
183 || Client_Type( Client ) == CLIENT_GOTPASS
184 || Client_Type( Client ) == CLIENT_GOTNICK
185 || Client_Type( Client ) == CLIENT_GOTUSER
186 || Client_Type( Client ) == CLIENT_USER
187 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
189 if( Client_Type( Client ) == CLIENT_UNKNOWN
190 || Client_Type( Client ) == CLIENT_GOTPASS
191 || Client_Type( Client ) == CLIENT_GOTNICK
192 || Client_Type( Client ) == CLIENT_USER
193 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
196 /* User registration or change of nickname */
198 /* Wrong number of arguments? */
200 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
204 /* Search "target" client */
205 if( Client_Type( Client ) == CLIENT_SERVER )
207 target = Client_Search( Req->prefix );
209 return IRC_WriteStrClient( Client,
216 /* Is this a restricted client? */
217 if( Client_HasMode( Client, 'r' ))
218 return IRC_WriteStrClient( Client,
220 Client_ID( Client ));
226 /* If the clients tries to change to its own nickname we won't
227 * do anything. This is how the original ircd behaves and some
228 * clients (for example Snak) expect it to be like this.
229 * But I doubt that this is "really the right thing" ... */
230 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 )
234 /* Check that the new nickname is available. Special case:
235 * the client only changes from/to upper to lower case. */
236 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
238 if( ! Client_CheckNick( target, Req->argv[0] ))
242 if(( Client_Type( target ) != CLIENT_USER )
243 && ( Client_Type( target ) != CLIENT_SERVER ))
246 Log( LOG_DEBUG, "Connection %d: got valid NICK command ...",
247 Client_Conn( Client ));
249 /* Register new nickname of this client */
250 Client_SetID( target, Req->argv[0] );
252 /* If we received a valid USER command already then
253 * register the new client! */
254 if( Client_Type( Client ) == CLIENT_GOTUSER )
255 return Hello_User( Client );
257 Client_SetType( Client, CLIENT_GOTNICK );
261 /* Nickname change */
262 if( Client_Conn( target ) > NONE )
266 "User \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
267 Client_Mask( target ), Client_Conn( target ),
268 Client_ID( target ), Req->argv[0] );
274 "User \"%s\" changed nick: \"%s\" -> \"%s\".",
275 Client_Mask( target ), Client_ID( target ),
279 /* Inform all users and servers (which have to know)
280 * of this nickname change */
281 if( Client_Type( Client ) == CLIENT_USER )
282 IRC_WriteStrClientPrefix( Client, Client,
285 IRC_WriteStrServersPrefix( Client, target,
286 "NICK :%s", Req->argv[0] );
287 IRC_WriteStrRelatedPrefix( target, target, false,
288 "NICK :%s", Req->argv[0] );
290 /* Register old nickname for WHOWAS queries */
291 Client_RegisterWhowas( target );
293 /* Save new nickname */
294 Client_SetID( target, Req->argv[0] );
296 IRC_SetPenalty( target, 2 );
301 else if( Client_Type( Client ) == CLIENT_SERVER )
303 /* Server introduces new client */
305 /* Falsche Anzahl Parameter? */
306 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
308 /* Nick ueberpruefen */
309 c = Client_Search( Req->argv[0] );
312 /* Der neue Nick ist auf diesem Server bereits registriert:
313 * sowohl der neue, als auch der alte Client muessen nun
314 * disconnectiert werden. */
315 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
316 Kill_Nick( Req->argv[0], "Nick collision" );
320 /* Server, zu dem der Client connectiert ist, suchen */
321 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
324 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
325 Kill_Nick( Req->argv[0], "Unknown server" );
329 /* Neue Client-Struktur anlegen */
330 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);
333 /* Eine neue Client-Struktur konnte nicht angelegt werden.
334 * Der Client muss disconnectiert werden, damit der Netz-
335 * status konsistent bleibt. */
336 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
337 Kill_Nick( Req->argv[0], "Server error" );
341 modes = Client_Modes( c );
342 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": "" );
343 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": "" );
345 /* Andere Server, ausser dem Introducer, informieren */
346 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] );
350 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
355 IRC_USER( CLIENT *Client, REQUEST *Req )
361 assert( Client != NULL );
362 assert( Req != NULL );
365 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
367 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
370 /* Wrong number of parameters? */
371 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
375 ptr = Client_User( Client );
376 if( ! ptr || ! *ptr || *ptr == '~' ) Client_SetUser( Client, Req->argv[0], false );
378 Client_SetUser( Client, Req->argv[0], false );
381 /* "Real name" or user info text: Don't set it to the empty string, the original ircd
382 * can't deal with such "real names" (e. g. "USER user * * :") ... */
383 if( *Req->argv[3] ) Client_SetInfo( Client, Req->argv[3] );
384 else Client_SetInfo( Client, "-" );
386 Log( LOG_DEBUG, "Connection %d: got valid USER command ...", Client_Conn( Client ));
387 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
388 else Client_SetType( Client, CLIENT_GOTUSER );
391 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
393 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
395 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
400 IRC_QUIT( CLIENT *Client, REQUEST *Req )
403 char quitmsg[LINE_LEN];
405 assert( Client != NULL );
406 assert( Req != NULL );
408 /* Wrong number of arguments? */
410 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
413 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
415 if ( Client_Type( Client ) == CLIENT_SERVER )
418 target = Client_Search( Req->prefix );
421 /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
422 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
426 Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
432 if (Req->argc == 1 && quitmsg[0] != '\"') {
433 /* " " to avoid confusion */
434 strlcpy(quitmsg, "\"", sizeof quitmsg);
435 strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
436 strlcat(quitmsg, "\"", sizeof quitmsg );
439 /* User, Service, oder noch nicht registriert */
440 Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
448 IRC_PING(CLIENT *Client, REQUEST *Req)
450 CLIENT *target, *from;
452 assert(Client != NULL);
455 /* Wrong number of arguments? */
457 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
460 /* Don't ignore additional arguments when in "strict" mode */
462 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
463 Client_ID(Client), Req->command);
467 /* A target has been specified ... */
468 target = Client_Search(Req->argv[1]);
470 if (!target || Client_Type(target) != CLIENT_SERVER)
471 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
472 Client_ID(Client), Req->argv[1]);
474 if (target != Client_ThisServer()) {
475 /* Ok, we have to forward the PING */
476 if (Client_Type(Client) == CLIENT_SERVER)
477 from = Client_Search(Req->prefix);
481 return IRC_WriteStrClient(Client,
482 ERR_NOSUCHSERVER_MSG,
483 Client_ID(Client), Req->prefix);
485 return IRC_WriteStrClientPrefix(target, from,
486 "PING %s :%s", Req->argv[0],
491 if (Client_Type(Client) == CLIENT_SERVER) {
493 from = Client_Search(Req->prefix);
497 from = Client_ThisServer();
499 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
500 Client_ID(Client), Req->prefix);
502 Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
503 Client_Conn(Client));
506 return IRC_WriteStrClient(Client, "PONG %s :%s",
507 Client_ID(from), Client_ID(Client));
509 /* Some clients depend on the argument being returned in the PONG
510 * reply (not mentioned in any RFC, though) */
511 return IRC_WriteStrClient(Client, "PONG %s :%s",
512 Client_ID(from), Req->argv[0]);
518 IRC_PONG(CLIENT *Client, REQUEST *Req)
520 CLIENT *target, *from;
523 assert(Client != NULL);
526 /* Wrong number of arguments? */
528 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
531 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
532 Client_ID(Client), Req->command);
535 if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
536 target = Client_Search(Req->argv[0]);
538 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
539 Client_ID(Client), Req->argv[0]);
541 from = Client_Search(Req->prefix);
543 if (target != Client_ThisServer() && target != from) {
544 /* Ok, we have to forward the message. */
546 return IRC_WriteStrClient(Client,
547 ERR_NOSUCHSERVER_MSG,
548 Client_ID(Client), Req->prefix);
550 if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
554 return IRC_WriteStrClientPrefix(target, from,
555 "PONG %s :%s", s, Req->argv[1]);
559 /* The connection timestamp has already been updated when the data has
560 * been read from so socket, so we don't need to update it here. */
562 if (Client_Conn(Client) > NONE)
564 "Connection %d: received PONG. Lag: %ld seconds.",
566 time(NULL) - Conn_LastPing(Client_Conn(Client)));
569 "Connection %d: received PONG.", Client_Conn(Client));
576 Hello_User( CLIENT *Client )
579 char ver[12], vertxt[30];
582 assert( Client != NULL );
584 /* Check password ... */
585 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
588 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
589 Conn_Close( Client_Conn( Client ), NULL, "Bad password", true);
593 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
595 /* Inform other servers */
596 IRC_WriteStrServers( NULL, "NICK %s 1 %s %s 1 +%s :%s", Client_ID( Client ), Client_User( Client ), Client_Hostname( Client ), Client_Modes( Client ), Client_Info( Client ));
599 if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return false;
601 /* Version and system type */
603 strlcpy( ver, CVSDATE, sizeof( ver ));
604 strncpy( ver + 4, ver + 5, 2 );
605 strncpy( ver + 6, ver + 8, 3 );
606 snprintf( vertxt, sizeof( vertxt ), "%s(%s)", PACKAGE_VERSION, ver );
607 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), vertxt, TARGET_CPU, TARGET_VENDOR, TARGET_OS )) return false;
609 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), PACKAGE_VERSION, TARGET_CPU, TARGET_VENDOR, TARGET_OS )) return false;
612 if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return false;
614 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), vertxt, USERMODES, CHANMODES )) return false;
616 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), PACKAGE_VERSION, USERMODES, CHANMODES )) return false;
619 /* Features supported by this server (005 numeric, ISUPPORT),
620 * see <http://www.irc.org/tech_docs/005.html> for details. */
621 if (! IRC_Send_ISUPPORT(Client))
624 Client_SetType( Client, CLIENT_USER );
626 if( ! IRC_Send_LUSERS( Client )) return DISCONNECTED;
627 if( ! IRC_Show_MOTD( Client )) return DISCONNECTED;
629 /* Suspend the client for a second ... */
630 IRC_SetPenalty( Client, 1 );
637 Kill_Nick( char *Nick, char *Reason )
641 assert( Nick != NULL );
642 assert( Reason != NULL );
644 r.prefix = (char *)Client_ThisServer( );
649 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
650 IRC_KILL( Client_ThisServer( ), &r );