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.27 2002/12/12 12:24:18 alex Exp $";
35 #include "irc-write.h"
38 #include "irc-login.h"
41 LOCAL BOOLEAN Hello_User PARAMS(( CLIENT *Client ));
42 LOCAL VOID Kill_Nick PARAMS(( CHAR *Nick, CHAR *Reason ));
46 IRC_PASS( CLIENT *Client, REQUEST *Req )
48 assert( Client != NULL );
49 assert( Req != NULL );
51 /* Fehler liefern, wenn kein lokaler Client */
52 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
54 if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
56 /* noch nicht registrierte unbekannte Verbindung */
57 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
59 /* Passwort speichern */
60 Client_SetPassword( Client, Req->argv[0] );
62 Client_SetType( Client, CLIENT_GOTPASS );
65 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
67 CHAR c2, c4, *type, *impl, *serverver, *flags, *ptr, *ircflags;
68 INT protohigh, protolow;
70 /* noch nicht registrierte Server-Verbindung */
71 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
73 /* Passwort speichern */
74 Client_SetPassword( Client, Req->argv[0] );
76 /* Protokollversion ermitteln */
77 if( strlen( Req->argv[1] ) >= 4 )
82 Req->argv[1][4] = '\0';
83 protolow = atoi( &Req->argv[1][2] );
84 Req->argv[1][2] = '\0';
85 protohigh = atoi( Req->argv[1] );
90 else protohigh = protolow = 0;
93 if( strlen( Req->argv[1] ) > 4 ) type = &Req->argv[1][4];
96 /* IRC-Flags (nach RFC 2813) */
97 if( Req->argc >= 4 ) ircflags = Req->argv[3];
100 /* Implementation, Version und ngIRCd-Flags */
102 ptr = strchr( impl, '|' );
103 if( ptr ) *ptr = '\0';
105 if( type && ( strcmp( type, PROTOIRCPLUS ) == 0 ))
107 /* auf der anderen Seite laeuft ein Server, der
108 * ebenfalls das IRC+-Protokoll versteht */
110 flags = strchr( serverver, ':' );
117 Log( LOG_INFO, "Peer announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").", impl, serverver, protohigh, protolow, flags );
121 /* auf der anderen Seite laeuft ein Server, der
122 * nur das Originalprotokoll unterstuetzt */
124 if( strchr( ircflags, 'Z' )) flags = "Z";
126 Log( LOG_INFO, "Peer announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").", impl, protohigh, protolow, flags );
129 Client_SetType( Client, CLIENT_GOTPASSSERVER );
130 Client_SetFlags( Client, flags );
134 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
136 /* Falsche Anzahl Parameter? */
137 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
139 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
144 IRC_NICK( CLIENT *Client, REQUEST *Req )
146 CLIENT *intr_c, *target, *c;
149 assert( Client != NULL );
150 assert( Req != NULL );
152 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
154 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 ))
156 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 ))
159 /* User-Registrierung bzw. Nick-Aenderung */
161 /* Falsche Anzahl Parameter? */
162 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
164 /* "Ziel-Client" ermitteln */
165 if( Client_Type( Client ) == CLIENT_SERVER )
167 target = Client_Search( Req->prefix );
168 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
172 /* Ist der Client "restricted"? */
173 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
178 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
179 * wir nichts. So macht es das Original und mind. Snak hat probleme,
180 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
181 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
184 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
185 * will nur die Gross- und Kleinschreibung aendern. Das darf
186 * er natuerlich machen :-) */
187 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
189 if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
192 if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
195 Log( LOG_DEBUG, "Connection %d: got valid NICK command ...", Client_Conn( Client ));
197 /* Client-Nick registrieren */
198 Client_SetID( target, Req->argv[0] );
200 /* schon ein USER da? Dann registrieren! */
201 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
202 else Client_SetType( Client, CLIENT_GOTNICK );
207 if( Client_Conn( target ) > NONE )
210 Log( LOG_INFO, "User \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".", Client_Mask( target ), Client_Conn( target ), Client_ID( target ), Req->argv[0] );
215 Log( LOG_DEBUG, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
218 /* alle betroffenen User und Server ueber Nick-Aenderung informieren */
219 if( Client_Type( Client ) == CLIENT_USER ) IRC_WriteStrClientPrefix( Client, Client, "NICK :%s", Req->argv[0] );
220 IRC_WriteStrServersPrefix( Client, target, "NICK :%s", Req->argv[0] );
221 IRC_WriteStrRelatedPrefix( target, target, FALSE, "NICK :%s", Req->argv[0] );
223 /* neuen Client-Nick speichern */
224 Client_SetID( target, Req->argv[0] );
229 else if( Client_Type( Client ) == CLIENT_SERVER )
231 /* Server fuehrt neuen Client ein */
233 /* Falsche Anzahl Parameter? */
234 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
236 /* Nick ueberpruefen */
237 c = Client_Search( Req->argv[0] );
240 /* Der neue Nick ist auf diesem Server bereits registriert:
241 * sowohl der neue, als auch der alte Client muessen nun
242 * disconnectiert werden. */
243 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
244 Kill_Nick( Req->argv[0], "Nick collision" );
248 /* Server, zu dem der Client connectiert ist, suchen */
249 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
252 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
253 Kill_Nick( Req->argv[0], "Unknown server" );
257 /* Neue Client-Struktur anlegen */
258 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 );
261 /* Eine neue Client-Struktur konnte nicht angelegt werden.
262 * Der Client muss disconnectiert werden, damit der Netz-
263 * status konsistent bleibt. */
264 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
265 Kill_Nick( Req->argv[0], "Server error" );
269 modes = Client_Modes( c );
270 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": "" );
271 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": "" );
273 /* Andere Server, ausser dem Introducer, informieren */
274 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] );
278 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
283 IRC_USER( CLIENT *Client, REQUEST *Req )
285 assert( Client != NULL );
286 assert( Req != NULL );
289 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
291 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
294 /* Falsche Anzahl Parameter? */
295 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
297 Client_SetUser( Client, Req->argv[0], FALSE );
298 Client_SetInfo( Client, Req->argv[3] );
300 Log( LOG_DEBUG, "Connection %d: got valid USER command ...", Client_Conn( Client ));
301 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
302 else Client_SetType( Client, CLIENT_GOTUSER );
305 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
307 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
309 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
314 IRC_QUIT( CLIENT *Client, REQUEST *Req )
318 assert( Client != NULL );
319 assert( Req != NULL );
321 if ( Client_Type( Client ) == CLIENT_SERVER )
325 /* Falsche Anzahl Parameter? */
326 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
328 target = Client_Search( Req->prefix );
331 /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
332 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
336 if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL, TRUE );
337 else Client_Destroy( target, "Got QUIT command.", Req->argv[0], TRUE );
343 /* User, Service, oder noch nicht registriert */
345 /* Falsche Anzahl Parameter? */
346 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
348 if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
349 else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
357 IRC_PING( CLIENT *Client, REQUEST *Req )
359 CLIENT *target, *from;
361 assert( Client != NULL );
362 assert( Req != NULL );
364 /* Falsche Anzahl Parameter? */
365 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
367 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
372 /* es wurde ein Ziel-Client angegeben */
373 target = Client_Search( Req->argv[1] );
374 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
375 if( target != Client_ThisServer( ))
378 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
380 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
381 return IRC_WriteStrClientPrefix( target, from, "PING %s :%s", Client_ID( from ), Req->argv[1] );
385 Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
386 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
391 IRC_PONG( CLIENT *Client, REQUEST *Req )
393 CLIENT *target, *from;
395 assert( Client != NULL );
396 assert( Req != NULL );
398 /* Falsche Anzahl Parameter? */
399 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
400 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
405 target = Client_Search( Req->argv[1] );
406 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
407 if( target != Client_ThisServer( ))
410 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
412 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
413 return IRC_WriteStrClientPrefix( target, from, "PONG %s :%s", Client_ID( from ), Req->argv[1] );
417 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
418 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
420 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 )));
421 else Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
428 Hello_User( CLIENT *Client )
430 assert( Client != NULL );
432 /* Passwort ueberpruefen */
433 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
435 /* Falsches Passwort */
436 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
437 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
441 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
443 /* Andere Server informieren */
444 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 ));
446 if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return FALSE;
447 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), VERSION, TARGET_CPU, TARGET_VENDOR, TARGET_OS )) return FALSE;
448 if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return FALSE;
449 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), VERSION, USERMODES, CHANMODES )) return FALSE;
451 Client_SetType( Client, CLIENT_USER );
453 if( ! IRC_Send_LUSERS( Client )) return DISCONNECTED;
454 if( ! IRC_Show_MOTD( Client )) return DISCONNECTED;
461 Kill_Nick( CHAR *Nick, CHAR *Reason )
465 assert( Nick != NULL );
466 assert( Reason != NULL );
468 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
470 /* andere Server benachrichtigen */
471 IRC_WriteStrServers( NULL, "KILL %s :%s", Nick, Reason );
473 /* Ggf. einen eigenen Client toeten */
474 c = Client_Search( Nick );
475 if( c && ( Client_Conn( c ) != NONE )) Conn_Close( Client_Conn( c ), NULL, Reason, TRUE );