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.16 2002/09/02 19:04:30 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 *impl, *serverver, *flags, *ptr;
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 ueberpruefen */
78 if( strlen( Req->argv[1] ) > 4 ) Req->argv[1][4] = '\0';
79 if( strlen( Req->argv[1] ) != 4 ) protohigh = protolow = 0;
82 protolow = atoi( &Req->argv[1][2] );
83 Req->argv[1][2] = '\0';
84 protohigh = atoi( Req->argv[1] );
87 /* Implementation, Version und ngIRCd-Flags */
89 ptr = strchr( impl, '|' );
90 if( ptr ) *ptr = '\0';
92 if( strcmp( impl, PACKAGE ) == 0 )
94 /* auf der anderen Seite laeuft auch ein ngIRCd */
96 flags = strchr( serverver, ':' );
103 Log( LOG_INFO, "Other server is \"%s\" %s (flags: \"%s\"), using protocol version %d.%d.", impl, serverver, flags, protohigh, protolow );
107 serverver = flags = "";
108 Log( LOG_INFO, "Other server is \"%s\" using protocol version %d.%d.", impl, protohigh, protolow );
111 Client_SetType( Client, CLIENT_GOTPASSSERVER );
114 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
116 /* Falsche Anzahl Parameter? */
117 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
119 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
124 IRC_NICK( CLIENT *Client, REQUEST *Req )
126 CLIENT *intr_c, *target, *c;
129 assert( Client != NULL );
130 assert( Req != NULL );
132 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
134 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 ))
136 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 ))
139 /* User-Registrierung bzw. Nick-Aenderung */
141 /* Falsche Anzahl Parameter? */
142 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
144 /* "Ziel-Client" ermitteln */
145 if( Client_Type( Client ) == CLIENT_SERVER )
147 target = Client_Search( Req->prefix );
148 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
152 /* Ist der Client "restricted"? */
153 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
158 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
159 * wir nichts. So macht es das Original und mind. Snak hat probleme,
160 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
161 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
164 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
165 * will nur die Gross- und Kleinschreibung aendern. Das darf
166 * er natuerlich machen :-) */
167 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
169 if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
172 if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
175 Log( LOG_DEBUG, "Connection %d: got valid NICK command ...", Client_Conn( Client ));
177 /* Client-Nick registrieren */
178 Client_SetID( target, Req->argv[0] );
180 /* schon ein USER da? Dann registrieren! */
181 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
182 else Client_SetType( Client, CLIENT_GOTNICK );
187 if( Client_Conn( target ) > NONE )
190 Log( LOG_INFO, "User \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".", Client_Mask( target ), Client_Conn( target ), Client_ID( target ), Req->argv[0] );
195 Log( LOG_DEBUG, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
198 /* alle betroffenen User und Server ueber Nick-Aenderung informieren */
199 if( Client_Type( Client ) == CLIENT_USER ) IRC_WriteStrClientPrefix( Client, Client, "NICK :%s", Req->argv[0] );
200 IRC_WriteStrServersPrefix( Client, target, "NICK :%s", Req->argv[0] );
201 IRC_WriteStrRelatedPrefix( target, target, FALSE, "NICK :%s", Req->argv[0] );
203 /* neuen Client-Nick speichern */
204 Client_SetID( target, Req->argv[0] );
209 else if( Client_Type( Client ) == CLIENT_SERVER )
211 /* Server fuehrt neuen Client ein */
213 /* Falsche Anzahl Parameter? */
214 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
216 /* Nick ueberpruefen */
217 c = Client_Search( Req->argv[0] );
220 /* Der neue Nick ist auf diesem Server bereits registriert:
221 * sowohl der neue, als auch der alte Client muessen nun
222 * disconnectiert werden. */
223 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
224 Kill_Nick( Req->argv[0], "Nick collision" );
228 /* Server, zu dem der Client connectiert ist, suchen */
229 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
232 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
233 Kill_Nick( Req->argv[0], "Unknown server" );
237 /* Neue Client-Struktur anlegen */
238 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 );
241 /* Eine neue Client-Struktur konnte nicht angelegt werden.
242 * Der Client muss disconnectiert werden, damit der Netz-
243 * status konsistent bleibt. */
244 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
245 Kill_Nick( Req->argv[0], "Server error" );
249 modes = Client_Modes( c );
250 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": "" );
251 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": "" );
253 /* Andere Server, ausser dem Introducer, informieren */
254 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] );
258 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
263 IRC_USER( CLIENT *Client, REQUEST *Req )
265 assert( Client != NULL );
266 assert( Req != NULL );
269 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
271 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
274 /* Falsche Anzahl Parameter? */
275 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
277 Client_SetUser( Client, Req->argv[0], FALSE );
278 Client_SetInfo( Client, Req->argv[3] );
280 Log( LOG_DEBUG, "Connection %d: got valid USER command ...", Client_Conn( Client ));
281 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
282 else Client_SetType( Client, CLIENT_GOTUSER );
285 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
287 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
289 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
294 IRC_QUIT( CLIENT *Client, REQUEST *Req )
298 assert( Client != NULL );
299 assert( Req != NULL );
301 if ( Client_Type( Client ) == CLIENT_SERVER )
305 /* Falsche Anzahl Parameter? */
306 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
308 target = Client_Search( Req->prefix );
311 /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
312 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
316 if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL, TRUE );
317 else Client_Destroy( target, "Got QUIT command.", Req->argv[0], TRUE );
323 /* User, Service, oder noch nicht registriert */
325 /* Falsche Anzahl Parameter? */
326 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
328 if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
329 else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
337 IRC_PING( CLIENT *Client, REQUEST *Req )
339 CLIENT *target, *from;
341 assert( Client != NULL );
342 assert( Req != NULL );
344 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
346 /* Falsche Anzahl Parameter? */
347 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
349 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
354 /* es wurde ein Ziel-Client angegeben */
355 target = Client_Search( Req->argv[1] );
356 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
357 if( target != Client_ThisServer( ))
360 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
362 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
363 return IRC_WriteStrClientPrefix( target, from, "PING %s :%s", Client_ID( from ), Req->argv[1] );
367 Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
368 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
373 IRC_PONG( CLIENT *Client, REQUEST *Req )
375 CLIENT *target, *from;
377 assert( Client != NULL );
378 assert( Req != NULL );
380 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
382 /* Falsche Anzahl Parameter? */
383 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
384 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
389 target = Client_Search( Req->argv[1] );
390 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
391 if( target != Client_ThisServer( ))
394 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
396 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
397 return IRC_WriteStrClientPrefix( target, from, "PONG %s :%s", Client_ID( from ), Req->argv[1] );
401 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
402 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
404 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 )));
405 else Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
412 Hello_User( CLIENT *Client )
414 assert( Client != NULL );
416 /* Passwort ueberpruefen */
417 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
419 /* Falsches Passwort */
420 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
421 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
425 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
427 /* Andere Server informieren */
428 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 ));
430 if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return FALSE;
431 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), VERSION, TARGET_CPU, TARGET_CPU, TARGET_OS )) return FALSE;
432 if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return FALSE;
433 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), VERSION, USERMODES, CHANMODES )) return FALSE;
435 Client_SetType( Client, CLIENT_USER );
437 if( ! IRC_Send_LUSERS( Client )) return DISCONNECTED;
438 if( ! IRC_Show_MOTD( Client )) return DISCONNECTED;
445 Kill_Nick( CHAR *Nick, CHAR *Reason )
449 assert( Nick != NULL );
450 assert( Reason != NULL );
452 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
454 /* andere Server benachrichtigen */
455 IRC_WriteStrServers( NULL, "KILL %s :%s", Nick, Reason );
457 /* Ggf. einen eigenen Client toeten */
458 c = Client_Search( Nick );
459 if( c && ( Client_Conn( c ) != NONE )) Conn_Close( Client_Conn( c ), NULL, Reason, TRUE );