2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 * der GNU General Public License (GPL), wie von der Free Software Foundation
7 * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
12 * $Id: irc-login.c,v 1.23 2002/11/26 23:07:24 alex Exp $
14 * irc-login.c: Anmeldung und Abmeldung im IRC
36 #include "irc-write.h"
39 #include "irc-login.h"
42 LOCAL BOOLEAN Hello_User PARAMS(( CLIENT *Client ));
43 LOCAL VOID Kill_Nick PARAMS(( CHAR *Nick, CHAR *Reason ));
47 IRC_PASS( CLIENT *Client, REQUEST *Req )
49 assert( Client != NULL );
50 assert( Req != NULL );
52 /* Fehler liefern, wenn kein lokaler Client */
53 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
55 if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
57 /* noch nicht registrierte unbekannte Verbindung */
58 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
60 /* Passwort speichern */
61 Client_SetPassword( Client, Req->argv[0] );
63 Client_SetType( Client, CLIENT_GOTPASS );
66 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
68 CHAR c2, c4, *type, *impl, *serverver, *flags, *ptr, *ircflags;
69 INT protohigh, protolow;
71 /* noch nicht registrierte Server-Verbindung */
72 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
74 /* Passwort speichern */
75 Client_SetPassword( Client, Req->argv[0] );
77 /* Protokollversion ermitteln */
78 if( strlen( Req->argv[1] ) >= 4 )
83 Req->argv[1][4] = '\0';
84 protolow = atoi( &Req->argv[1][2] );
85 Req->argv[1][2] = '\0';
86 protohigh = atoi( Req->argv[1] );
91 else protohigh = protolow = 0;
94 if( strlen( Req->argv[1] ) > 4 ) type = &Req->argv[1][4];
97 /* IRC-Flags (nach RFC 2813) */
98 if( Req->argc >= 4 ) ircflags = Req->argv[3];
101 /* Implementation, Version und ngIRCd-Flags */
103 ptr = strchr( impl, '|' );
104 if( ptr ) *ptr = '\0';
106 if( type && ( strcmp( type, PROTOIRCPLUS ) == 0 ))
108 /* auf der anderen Seite laeuft ein Server, der
109 * ebenfalls das IRC+-Protokoll versteht */
111 flags = strchr( serverver, ':' );
118 Log( LOG_INFO, "Connection %d: Peer announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").", Client_Conn( Client ), impl, serverver, protohigh, protolow, flags );
122 /* auf der anderen Seite laeuft ein Server, der
123 * nur das Originalprotokoll unterstuetzt */
125 if( strchr( ircflags, 'Z' )) flags = "Z";
127 Log( LOG_INFO, "Connection %d: Peer announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").", Client_Conn( Client ), impl, protohigh, protolow, flags );
130 Client_SetType( Client, CLIENT_GOTPASSSERVER );
131 Client_SetFlags( Client, flags );
135 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
137 /* Falsche Anzahl Parameter? */
138 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
140 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
145 IRC_NICK( CLIENT *Client, REQUEST *Req )
147 CLIENT *intr_c, *target, *c;
150 assert( Client != NULL );
151 assert( Req != NULL );
153 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
155 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 ))
157 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 ))
160 /* User-Registrierung bzw. Nick-Aenderung */
162 /* Falsche Anzahl Parameter? */
163 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
165 /* "Ziel-Client" ermitteln */
166 if( Client_Type( Client ) == CLIENT_SERVER )
168 target = Client_Search( Req->prefix );
169 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
173 /* Ist der Client "restricted"? */
174 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
179 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
180 * wir nichts. So macht es das Original und mind. Snak hat probleme,
181 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
182 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
185 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
186 * will nur die Gross- und Kleinschreibung aendern. Das darf
187 * er natuerlich machen :-) */
188 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
190 if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
193 if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
196 Log( LOG_DEBUG, "Connection %d: got valid NICK command ...", Client_Conn( Client ));
198 /* Client-Nick registrieren */
199 Client_SetID( target, Req->argv[0] );
201 /* schon ein USER da? Dann registrieren! */
202 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
203 else Client_SetType( Client, CLIENT_GOTNICK );
208 if( Client_Conn( target ) > NONE )
211 Log( LOG_INFO, "User \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".", Client_Mask( target ), Client_Conn( target ), Client_ID( target ), Req->argv[0] );
216 Log( LOG_DEBUG, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
219 /* alle betroffenen User und Server ueber Nick-Aenderung informieren */
220 if( Client_Type( Client ) == CLIENT_USER ) IRC_WriteStrClientPrefix( Client, Client, "NICK :%s", Req->argv[0] );
221 IRC_WriteStrServersPrefix( Client, target, "NICK :%s", Req->argv[0] );
222 IRC_WriteStrRelatedPrefix( target, target, FALSE, "NICK :%s", Req->argv[0] );
224 /* neuen Client-Nick speichern */
225 Client_SetID( target, Req->argv[0] );
230 else if( Client_Type( Client ) == CLIENT_SERVER )
232 /* Server fuehrt neuen Client ein */
234 /* Falsche Anzahl Parameter? */
235 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
237 /* Nick ueberpruefen */
238 c = Client_Search( Req->argv[0] );
241 /* Der neue Nick ist auf diesem Server bereits registriert:
242 * sowohl der neue, als auch der alte Client muessen nun
243 * disconnectiert werden. */
244 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
245 Kill_Nick( Req->argv[0], "Nick collision" );
249 /* Server, zu dem der Client connectiert ist, suchen */
250 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
253 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
254 Kill_Nick( Req->argv[0], "Unknown server" );
258 /* Neue Client-Struktur anlegen */
259 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 );
262 /* Eine neue Client-Struktur konnte nicht angelegt werden.
263 * Der Client muss disconnectiert werden, damit der Netz-
264 * status konsistent bleibt. */
265 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
266 Kill_Nick( Req->argv[0], "Server error" );
270 modes = Client_Modes( c );
271 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": "" );
272 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": "" );
274 /* Andere Server, ausser dem Introducer, informieren */
275 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] );
279 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
284 IRC_USER( CLIENT *Client, REQUEST *Req )
286 assert( Client != NULL );
287 assert( Req != NULL );
290 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
292 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
295 /* Falsche Anzahl Parameter? */
296 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
298 Client_SetUser( Client, Req->argv[0], FALSE );
299 Client_SetInfo( Client, Req->argv[3] );
301 Log( LOG_DEBUG, "Connection %d: got valid USER command ...", Client_Conn( Client ));
302 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
303 else Client_SetType( Client, CLIENT_GOTUSER );
306 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
308 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
310 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
315 IRC_QUIT( CLIENT *Client, REQUEST *Req )
319 assert( Client != NULL );
320 assert( Req != NULL );
322 if ( Client_Type( Client ) == CLIENT_SERVER )
326 /* Falsche Anzahl Parameter? */
327 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
329 target = Client_Search( Req->prefix );
332 /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
333 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
337 if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL, TRUE );
338 else Client_Destroy( target, "Got QUIT command.", Req->argv[0], TRUE );
344 /* User, Service, oder noch nicht registriert */
346 /* Falsche Anzahl Parameter? */
347 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
349 if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
350 else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
358 IRC_PING( CLIENT *Client, REQUEST *Req )
360 CLIENT *target, *from;
362 assert( Client != NULL );
363 assert( Req != NULL );
365 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
367 /* Falsche Anzahl Parameter? */
368 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
370 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
375 /* es wurde ein Ziel-Client angegeben */
376 target = Client_Search( Req->argv[1] );
377 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
378 if( target != Client_ThisServer( ))
381 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
383 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
384 return IRC_WriteStrClientPrefix( target, from, "PING %s :%s", Client_ID( from ), Req->argv[1] );
388 Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
389 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
394 IRC_PONG( CLIENT *Client, REQUEST *Req )
396 CLIENT *target, *from;
398 assert( Client != NULL );
399 assert( Req != NULL );
401 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
403 /* Falsche Anzahl Parameter? */
404 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
405 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
410 target = Client_Search( Req->argv[1] );
411 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
412 if( target != Client_ThisServer( ))
415 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
417 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
418 return IRC_WriteStrClientPrefix( target, from, "PONG %s :%s", Client_ID( from ), Req->argv[1] );
422 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
423 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
425 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 )));
426 else Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
433 Hello_User( CLIENT *Client )
435 assert( Client != NULL );
437 /* Passwort ueberpruefen */
438 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
440 /* Falsches Passwort */
441 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
442 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
446 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
448 /* Andere Server informieren */
449 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 ));
451 if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return FALSE;
452 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), VERSION, TARGET_CPU, TARGET_VENDOR, TARGET_OS )) return FALSE;
453 if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return FALSE;
454 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), VERSION, USERMODES, CHANMODES )) return FALSE;
456 Client_SetType( Client, CLIENT_USER );
458 if( ! IRC_Send_LUSERS( Client )) return DISCONNECTED;
459 if( ! IRC_Show_MOTD( Client )) return DISCONNECTED;
466 Kill_Nick( CHAR *Nick, CHAR *Reason )
470 assert( Nick != NULL );
471 assert( Reason != NULL );
473 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
475 /* andere Server benachrichtigen */
476 IRC_WriteStrServers( NULL, "KILL %s :%s", Nick, Reason );
478 /* Ggf. einen eigenen Client toeten */
479 c = Client_Search( Nick );
480 if( c && ( Client_Conn( c ) != NONE )) Conn_Close( Client_Conn( c ), NULL, Reason, TRUE );