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.40 2004/03/11 22:16:31 alex Exp $";
28 #include "conn-func.h"
37 #include "irc-write.h"
38 #include "cvs-version.h"
41 #include "irc-login.h"
44 LOCAL BOOLEAN Hello_User PARAMS(( CLIENT *Client ));
45 LOCAL VOID Kill_Nick PARAMS(( CHAR *Nick, CHAR *Reason ));
49 IRC_PASS( CLIENT *Client, REQUEST *Req )
51 assert( Client != NULL );
52 assert( Req != NULL );
54 /* Fehler liefern, wenn kein lokaler Client */
55 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
57 if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
59 /* noch nicht registrierte unbekannte Verbindung */
60 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
62 /* Passwort speichern */
63 Client_SetPassword( Client, Req->argv[0] );
65 Client_SetType( Client, CLIENT_GOTPASS );
68 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
70 CHAR c2, c4, *type, *impl, *serverver, *flags, *ptr, *ircflags;
71 INT protohigh, protolow;
73 /* noch nicht registrierte Server-Verbindung */
74 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
76 /* Passwort speichern */
77 Client_SetPassword( Client, Req->argv[0] );
79 /* Protokollversion ermitteln */
80 if( strlen( Req->argv[1] ) >= 4 )
85 Req->argv[1][4] = '\0';
86 protolow = atoi( &Req->argv[1][2] );
87 Req->argv[1][2] = '\0';
88 protohigh = atoi( Req->argv[1] );
93 else protohigh = protolow = 0;
96 if( strlen( Req->argv[1] ) > 4 ) type = &Req->argv[1][4];
99 /* IRC-Flags (nach RFC 2813) */
100 if( Req->argc >= 4 ) ircflags = Req->argv[3];
103 /* Implementation, Version und ngIRCd-Flags */
105 ptr = strchr( impl, '|' );
106 if( ptr ) *ptr = '\0';
108 if( type && ( strcmp( type, PROTOIRCPLUS ) == 0 ))
110 /* auf der anderen Seite laeuft ein Server, der
111 * ebenfalls das IRC+-Protokoll versteht */
113 flags = strchr( serverver, ':' );
120 Log( LOG_INFO, "Peer announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").", impl, serverver, protohigh, protolow, flags );
124 /* auf der anderen Seite laeuft ein Server, der
125 * nur das Originalprotokoll unterstuetzt */
127 if( strchr( ircflags, 'Z' )) flags = "Z";
129 Log( LOG_INFO, "Peer announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").", impl, protohigh, protolow, flags );
132 Client_SetType( Client, CLIENT_GOTPASSSERVER );
133 Client_SetFlags( Client, flags );
137 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
139 /* Falsche Anzahl Parameter? */
140 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
142 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
147 IRC_NICK( CLIENT *Client, REQUEST *Req )
149 CLIENT *intr_c, *target, *c;
152 assert( Client != NULL );
153 assert( Req != NULL );
155 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
157 if( Client_Type( Client ) == CLIENT_UNKNOWN || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTUSER || Client_Type( Client ) == CLIENT_USER || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
159 if( Client_Type( Client ) == CLIENT_UNKNOWN || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_USER || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
162 /* User-Registrierung bzw. Nick-Aenderung */
164 /* Falsche Anzahl Parameter? */
165 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
167 /* "Ziel-Client" ermitteln */
168 if( Client_Type( Client ) == CLIENT_SERVER )
170 target = Client_Search( Req->prefix );
171 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
175 /* Ist der Client "restricted"? */
176 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
181 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
182 * wir nichts. So macht es das Original und mind. Snak hat probleme,
183 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
184 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
187 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
188 * will nur die Gross- und Kleinschreibung aendern. Das darf
189 * er natuerlich machen :-) */
190 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
192 if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
195 if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
198 Log( LOG_DEBUG, "Connection %d: got valid NICK command ...", Client_Conn( Client ));
200 /* Client-Nick registrieren */
201 Client_SetID( target, Req->argv[0] );
203 /* schon ein USER da? Dann registrieren! */
204 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
205 else Client_SetType( Client, CLIENT_GOTNICK );
210 if( Client_Conn( target ) > NONE )
213 Log( LOG_INFO, "User \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".", Client_Mask( target ), Client_Conn( target ), Client_ID( target ), Req->argv[0] );
218 Log( LOG_DEBUG, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
221 /* alle betroffenen User und Server ueber Nick-Aenderung informieren */
222 if( Client_Type( Client ) == CLIENT_USER ) IRC_WriteStrClientPrefix( Client, Client, "NICK :%s", Req->argv[0] );
223 IRC_WriteStrServersPrefix( Client, target, "NICK :%s", Req->argv[0] );
224 IRC_WriteStrRelatedPrefix( target, target, FALSE, "NICK :%s", Req->argv[0] );
226 /* neuen Client-Nick speichern */
227 Client_SetID( target, Req->argv[0] );
228 IRC_SetPenalty( target, 2 );
233 else if( Client_Type( Client ) == CLIENT_SERVER )
235 /* Server fuehrt neuen Client ein */
237 /* Falsche Anzahl Parameter? */
238 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
240 /* Nick ueberpruefen */
241 c = Client_Search( Req->argv[0] );
244 /* Der neue Nick ist auf diesem Server bereits registriert:
245 * sowohl der neue, als auch der alte Client muessen nun
246 * disconnectiert werden. */
247 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
248 Kill_Nick( Req->argv[0], "Nick collision" );
252 /* Server, zu dem der Client connectiert ist, suchen */
253 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
256 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
257 Kill_Nick( Req->argv[0], "Unknown server" );
261 /* Neue Client-Struktur anlegen */
262 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 );
265 /* Eine neue Client-Struktur konnte nicht angelegt werden.
266 * Der Client muss disconnectiert werden, damit der Netz-
267 * status konsistent bleibt. */
268 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
269 Kill_Nick( Req->argv[0], "Server error" );
273 modes = Client_Modes( c );
274 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": "" );
275 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": "" );
277 /* Andere Server, ausser dem Introducer, informieren */
278 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] );
282 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
287 IRC_USER( CLIENT *Client, REQUEST *Req )
293 assert( Client != NULL );
294 assert( Req != NULL );
297 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
299 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
302 /* Wrong number of parameters? */
303 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
307 ptr = Client_User( Client );
308 if( ! ptr || ! *ptr || *ptr == '~' ) Client_SetUser( Client, Req->argv[0], FALSE );
310 Client_SetUser( Client, Req->argv[0], FALSE );
313 /* "Real name" or user info text: Don't set it to the empty string, the original ircd
314 * can't deal with such "real names" (e. g. "USER user * * :") ... */
315 if( *Req->argv[3] ) Client_SetInfo( Client, Req->argv[3] );
316 else Client_SetInfo( Client, "-" );
318 Log( LOG_DEBUG, "Connection %d: got valid USER command ...", Client_Conn( Client ));
319 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
320 else Client_SetType( Client, CLIENT_GOTUSER );
323 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
325 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
327 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
332 IRC_QUIT( CLIENT *Client, REQUEST *Req )
336 assert( Client != NULL );
337 assert( Req != NULL );
339 if ( Client_Type( Client ) == CLIENT_SERVER )
343 /* Falsche Anzahl Parameter? */
344 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
346 target = Client_Search( Req->prefix );
349 /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
350 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
354 if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL, TRUE );
355 else Client_Destroy( target, "Got QUIT command.", Req->argv[0], TRUE );
361 /* User, Service, oder noch nicht registriert */
363 /* Falsche Anzahl Parameter? */
364 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
366 if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
367 else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
375 IRC_PING( CLIENT *Client, REQUEST *Req )
377 CLIENT *target, *from;
379 assert( Client != NULL );
380 assert( Req != NULL );
382 /* Falsche Anzahl Parameter? */
383 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
385 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
390 /* es wurde ein Ziel-Client angegeben */
391 target = Client_Search( Req->argv[1] );
392 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
393 if( target != Client_ThisServer( ))
396 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
398 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
399 return IRC_WriteStrClientPrefix( target, from, "PING %s :%s", Client_ID( from ), Req->argv[1] );
403 Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
404 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
409 IRC_PONG( CLIENT *Client, REQUEST *Req )
411 CLIENT *target, *from;
413 assert( Client != NULL );
414 assert( Req != NULL );
416 /* Falsche Anzahl Parameter? */
417 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
418 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
423 target = Client_Search( Req->argv[1] );
424 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
425 if( target != Client_ThisServer( ))
428 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
430 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
431 return IRC_WriteStrClientPrefix( target, from, "PONG %s :%s", Client_ID( from ), Req->argv[1] );
435 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
436 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
438 if( Client_Conn( Client ) > NONE ) Log( LOG_DEBUG, "Connection %d: received PONG. Lag: %ld seconds.", Client_Conn( Client ), time( NULL ) - Conn_LastPing( Client_Conn( Client )));
439 else Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
446 Hello_User( CLIENT *Client )
449 CHAR ver[12], vertxt[30];
452 assert( Client != NULL );
454 /* Check password ... */
455 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
458 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
459 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
463 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
465 /* Inform other servers */
466 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 ));
469 if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return FALSE;
471 /* Version and system type */
473 strlcpy( ver, CVSDATE, sizeof( ver ));
474 strncpy( ver + 4, ver + 5, 2 );
475 strncpy( ver + 6, ver + 8, 3 );
476 snprintf( vertxt, sizeof( vertxt ), "%s(%s)", PACKAGE_VERSION, ver );
477 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), vertxt, TARGET_CPU, TARGET_VENDOR, TARGET_OS )) return FALSE;
479 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), PACKAGE_VERSION, TARGET_CPU, TARGET_VENDOR, TARGET_OS )) return FALSE;
482 if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return FALSE;
484 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), vertxt, USERMODES, CHANMODES )) return FALSE;
486 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), PACKAGE_VERSION, USERMODES, CHANMODES )) return FALSE;
490 if( ! IRC_WriteStrClient( Client, RPL_ISUPPORT_MSG, Client_ID( Client ), CLIENT_NICK_LEN - 1, CHANNEL_TOPIC_LEN - 1, CLIENT_AWAY_LEN - 1, Conf_MaxJoins )) return DISCONNECTED;
492 Client_SetType( Client, CLIENT_USER );
494 if( ! IRC_Send_LUSERS( Client )) return DISCONNECTED;
495 if( ! IRC_Show_MOTD( Client )) return DISCONNECTED;
497 /* Suspend the client for a second ... */
498 IRC_SetPenalty( Client, 1 );
505 Kill_Nick( CHAR *Nick, CHAR *Reason )
509 assert( Nick != NULL );
510 assert( Reason != NULL );
512 r.prefix = (CHAR *)Client_ThisServer( );
517 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
518 IRC_KILL( Client_ThisServer( ), &r );