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.8 2002/03/12 14:37:52 alex Exp $
14 * irc-login.c: Anmeldung und Abmeldung im IRC
29 #include "irc-write.h"
34 #include "irc-login.h"
37 LOCAL BOOLEAN Hello_User( CLIENT *Client );
38 LOCAL VOID Kill_Nick( CHAR *Nick, CHAR *Reason );
41 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
43 assert( Client != NULL );
44 assert( Req != NULL );
46 /* Fehler liefern, wenn kein lokaler Client */
47 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
49 if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
51 /* noch nicht registrierte unbekannte Verbindung */
52 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
54 /* Passwort speichern */
55 Client_SetPassword( Client, Req->argv[0] );
57 Client_SetType( Client, CLIENT_GOTPASS );
60 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
62 /* noch nicht registrierte Server-Verbindung */
63 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
65 /* Passwort speichern */
66 Client_SetPassword( Client, Req->argv[0] );
68 Client_SetType( Client, CLIENT_GOTPASSSERVER );
71 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
73 /* Falsche Anzahl Parameter? */
74 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
76 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
80 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
82 CLIENT *intr_c, *target, *c;
85 assert( Client != NULL );
86 assert( Req != NULL );
88 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
90 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 ))
92 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 ))
95 /* User-Registrierung bzw. Nick-Aenderung */
97 /* Falsche Anzahl Parameter? */
98 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
100 /* "Ziel-Client" ermitteln */
101 if( Client_Type( Client ) == CLIENT_SERVER )
103 target = Client_GetFromID( Req->prefix );
104 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
108 /* Ist der Client "restricted"? */
109 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
114 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
115 * wir nichts. So macht es das Original und mind. Snak hat probleme,
116 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
117 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
120 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
121 * will nur die Gross- und Kleinschreibung aendern. Das darf
122 * er natuerlich machen :-) */
123 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
125 if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
128 if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
131 Log( LOG_DEBUG, "Connection %d: got valid NICK command ...", Client_Conn( Client ));
133 /* Client-Nick registrieren */
134 Client_SetID( target, Req->argv[0] );
136 /* schon ein USER da? Dann registrieren! */
137 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
138 else Client_SetType( Client, CLIENT_GOTNICK );
143 Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
145 /* alle betroffenen User und Server ueber Nick-Aenderung informieren */
146 if( Client_Type( Client ) == CLIENT_USER ) IRC_WriteStrClientPrefix( Client, Client, "NICK :%s", Req->argv[0] );
147 IRC_WriteStrServersPrefix( Client, target, "NICK :%s", Req->argv[0] );
148 IRC_WriteStrRelatedPrefix( target, target, FALSE, "NICK :%s", Req->argv[0] );
150 /* neuen Client-Nick speichern */
151 Client_SetID( target, Req->argv[0] );
156 else if( Client_Type( Client ) == CLIENT_SERVER )
158 /* Server fuehrt neuen Client ein */
160 /* Falsche Anzahl Parameter? */
161 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
163 /* Nick ueberpruefen */
164 c = Client_GetFromID( Req->argv[0] );
167 /* Der neue Nick ist auf diesem Server bereits registriert:
168 * sowohl der neue, als auch der alte Client muessen nun
169 * disconnectiert werden. */
170 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
171 Kill_Nick( Req->argv[0], "Nick collision" );
175 /* Server, zu dem der Client connectiert ist, suchen */
176 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
179 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
180 Kill_Nick( Req->argv[0], "Unknown server" );
184 /* Neue Client-Struktur anlegen */
185 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 );
188 /* Eine neue Client-Struktur konnte nicht angelegt werden.
189 * Der Client muss disconnectiert werden, damit der Netz-
190 * status konsistent bleibt. */
191 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
192 Kill_Nick( Req->argv[0], "Server error" );
196 modes = Client_Modes( c );
197 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": "" );
198 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": "" );
200 /* Andere Server, ausser dem Introducer, informieren */
201 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] );
205 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
209 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
211 assert( Client != NULL );
212 assert( Req != NULL );
215 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
217 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
220 /* Falsche Anzahl Parameter? */
221 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
223 Client_SetUser( Client, Req->argv[0], FALSE );
224 Client_SetInfo( Client, Req->argv[3] );
226 Log( LOG_DEBUG, "Connection %d: got valid USER command ...", Client_Conn( Client ));
227 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
228 else Client_SetType( Client, CLIENT_GOTUSER );
231 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
233 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
235 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
239 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
243 assert( Client != NULL );
244 assert( Req != NULL );
246 if(( Client_Type( Client ) == CLIENT_USER ) || ( Client_Type( Client ) == CLIENT_SERVICE ))
250 /* Falsche Anzahl Parameter? */
251 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
253 if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
254 else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
258 else if ( Client_Type( Client ) == CLIENT_SERVER )
262 /* Falsche Anzahl Parameter? */
263 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
265 target = Client_Search( Req->prefix );
268 /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
269 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
273 if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL, TRUE );
274 else Client_Destroy( target, "Got QUIT command.", Req->argv[0], TRUE );
278 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
282 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
284 CLIENT *target, *from;
286 assert( Client != NULL );
287 assert( Req != NULL );
289 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
291 /* Falsche Anzahl Parameter? */
292 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
294 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
299 /* es wurde ein Ziel-Client angegeben */
300 target = Client_GetFromID( Req->argv[1] );
301 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
302 if( target != Client_ThisServer( ))
305 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
307 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
308 return IRC_WriteStrClientPrefix( target, from, "PING %s :%s", Client_ID( from ), Req->argv[1] );
312 Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
313 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
317 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
319 CLIENT *target, *from;
321 assert( Client != NULL );
322 assert( Req != NULL );
324 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
326 /* Falsche Anzahl Parameter? */
327 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
328 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
333 target = Client_GetFromID( Req->argv[1] );
334 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
335 if( target != Client_ThisServer( ))
338 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
340 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
341 return IRC_WriteStrClientPrefix( target, from, "PONG %s :%s", Client_ID( from ), Req->argv[1] );
345 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
346 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
348 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 )));
349 else Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
355 LOCAL BOOLEAN Hello_User( CLIENT *Client )
357 assert( Client != NULL );
359 /* Passwort ueberpruefen */
360 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
362 /* Falsches Passwort */
363 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
364 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
368 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
370 /* Andere Server informieren */
371 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 ));
373 if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return FALSE;
374 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
375 if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return FALSE;
376 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
378 Client_SetType( Client, CLIENT_USER );
380 if( ! IRC_Send_LUSERS( Client )) return DISCONNECTED;
381 if( ! IRC_Show_MOTD( Client )) return DISCONNECTED;
387 LOCAL VOID Kill_Nick( CHAR *Nick, CHAR *Reason )
391 assert( Nick != NULL );
392 assert( Reason != NULL );
394 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
396 /* andere Server benachrichtigen */
397 IRC_WriteStrServers( NULL, "KILL %s :%s", Nick, Reason );
399 /* Ggf. einen eigenen Client toeten */
400 c = Client_GetFromID( Nick );
401 if( c && ( Client_Conn( c ) != NONE )) Conn_Close( Client_Conn( c ), NULL, Reason, TRUE );