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.18 2002/09/03 18:55:03 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, "Connection %d: Peer announces itself as %s-%s (flags: \"%s\") using protocol version %d.%d.", Client_Conn( Client ), impl, serverver, flags, protohigh, protolow );
107 serverver = flags = "";
108 Log( LOG_INFO, "Connection %d: Peer announces itself as server of type \"%s\" usinf protocol version %d.%d.", Client_Conn( Client ), impl, protohigh, protolow );
111 Client_SetType( Client, CLIENT_GOTPASSSERVER );
112 Client_SetFlags( Client, flags );
116 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
118 /* Falsche Anzahl Parameter? */
119 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
121 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
126 IRC_NICK( CLIENT *Client, REQUEST *Req )
128 CLIENT *intr_c, *target, *c;
131 assert( Client != NULL );
132 assert( Req != NULL );
134 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
136 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 ))
138 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 ))
141 /* User-Registrierung bzw. Nick-Aenderung */
143 /* Falsche Anzahl Parameter? */
144 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
146 /* "Ziel-Client" ermitteln */
147 if( Client_Type( Client ) == CLIENT_SERVER )
149 target = Client_Search( Req->prefix );
150 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
154 /* Ist der Client "restricted"? */
155 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
160 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
161 * wir nichts. So macht es das Original und mind. Snak hat probleme,
162 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
163 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
166 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
167 * will nur die Gross- und Kleinschreibung aendern. Das darf
168 * er natuerlich machen :-) */
169 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
171 if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
174 if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
177 Log( LOG_DEBUG, "Connection %d: got valid NICK command ...", Client_Conn( Client ));
179 /* Client-Nick registrieren */
180 Client_SetID( target, Req->argv[0] );
182 /* schon ein USER da? Dann registrieren! */
183 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
184 else Client_SetType( Client, CLIENT_GOTNICK );
189 if( Client_Conn( target ) > NONE )
192 Log( LOG_INFO, "User \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".", Client_Mask( target ), Client_Conn( target ), Client_ID( target ), Req->argv[0] );
197 Log( LOG_DEBUG, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
200 /* alle betroffenen User und Server ueber Nick-Aenderung informieren */
201 if( Client_Type( Client ) == CLIENT_USER ) IRC_WriteStrClientPrefix( Client, Client, "NICK :%s", Req->argv[0] );
202 IRC_WriteStrServersPrefix( Client, target, "NICK :%s", Req->argv[0] );
203 IRC_WriteStrRelatedPrefix( target, target, FALSE, "NICK :%s", Req->argv[0] );
205 /* neuen Client-Nick speichern */
206 Client_SetID( target, Req->argv[0] );
211 else if( Client_Type( Client ) == CLIENT_SERVER )
213 /* Server fuehrt neuen Client ein */
215 /* Falsche Anzahl Parameter? */
216 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
218 /* Nick ueberpruefen */
219 c = Client_Search( Req->argv[0] );
222 /* Der neue Nick ist auf diesem Server bereits registriert:
223 * sowohl der neue, als auch der alte Client muessen nun
224 * disconnectiert werden. */
225 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
226 Kill_Nick( Req->argv[0], "Nick collision" );
230 /* Server, zu dem der Client connectiert ist, suchen */
231 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
234 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
235 Kill_Nick( Req->argv[0], "Unknown server" );
239 /* Neue Client-Struktur anlegen */
240 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 );
243 /* Eine neue Client-Struktur konnte nicht angelegt werden.
244 * Der Client muss disconnectiert werden, damit der Netz-
245 * status konsistent bleibt. */
246 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
247 Kill_Nick( Req->argv[0], "Server error" );
251 modes = Client_Modes( c );
252 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": "" );
253 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": "" );
255 /* Andere Server, ausser dem Introducer, informieren */
256 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] );
260 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
265 IRC_USER( CLIENT *Client, REQUEST *Req )
267 assert( Client != NULL );
268 assert( Req != NULL );
271 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
273 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
276 /* Falsche Anzahl Parameter? */
277 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
279 Client_SetUser( Client, Req->argv[0], FALSE );
280 Client_SetInfo( Client, Req->argv[3] );
282 Log( LOG_DEBUG, "Connection %d: got valid USER command ...", Client_Conn( Client ));
283 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
284 else Client_SetType( Client, CLIENT_GOTUSER );
287 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
289 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
291 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
296 IRC_QUIT( CLIENT *Client, REQUEST *Req )
300 assert( Client != NULL );
301 assert( Req != NULL );
303 if ( Client_Type( Client ) == CLIENT_SERVER )
307 /* Falsche Anzahl Parameter? */
308 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
310 target = Client_Search( Req->prefix );
313 /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
314 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
318 if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL, TRUE );
319 else Client_Destroy( target, "Got QUIT command.", Req->argv[0], TRUE );
325 /* User, Service, oder noch nicht registriert */
327 /* Falsche Anzahl Parameter? */
328 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
330 if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
331 else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
339 IRC_PING( CLIENT *Client, REQUEST *Req )
341 CLIENT *target, *from;
343 assert( Client != NULL );
344 assert( Req != NULL );
346 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
348 /* Falsche Anzahl Parameter? */
349 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
351 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
356 /* es wurde ein Ziel-Client angegeben */
357 target = Client_Search( Req->argv[1] );
358 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
359 if( target != Client_ThisServer( ))
362 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
364 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
365 return IRC_WriteStrClientPrefix( target, from, "PING %s :%s", Client_ID( from ), Req->argv[1] );
369 Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
370 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
375 IRC_PONG( CLIENT *Client, REQUEST *Req )
377 CLIENT *target, *from;
379 assert( Client != NULL );
380 assert( Req != NULL );
382 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
384 /* Falsche Anzahl Parameter? */
385 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
386 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
391 target = Client_Search( Req->argv[1] );
392 if( ! target ) 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, "PONG %s :%s", Client_ID( from ), Req->argv[1] );
403 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
404 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
406 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 )));
407 else Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
414 Hello_User( CLIENT *Client )
416 assert( Client != NULL );
418 /* Passwort ueberpruefen */
419 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
421 /* Falsches Passwort */
422 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
423 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
427 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
429 /* Andere Server informieren */
430 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 ));
432 if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return FALSE;
433 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), VERSION, TARGET_CPU, TARGET_CPU, TARGET_OS )) return FALSE;
434 if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return FALSE;
435 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), VERSION, USERMODES, CHANMODES )) return FALSE;
437 Client_SetType( Client, CLIENT_USER );
439 if( ! IRC_Send_LUSERS( Client )) return DISCONNECTED;
440 if( ! IRC_Show_MOTD( Client )) return DISCONNECTED;
447 Kill_Nick( CHAR *Nick, CHAR *Reason )
451 assert( Nick != NULL );
452 assert( Reason != NULL );
454 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
456 /* andere Server benachrichtigen */
457 IRC_WriteStrServers( NULL, "KILL %s :%s", Nick, Reason );
459 /* Ggf. einen eigenen Client toeten */
460 c = Client_Search( Nick );
461 if( c && ( Client_Conn( c ) != NONE )) Conn_Close( Client_Conn( c ), NULL, Reason, TRUE );