Blob


1 /*
2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4 *
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.
11 *
12 * $Id: irc-login.c,v 1.22 2002/09/22 21:40:33 alex Exp $
13 *
14 * irc-login.c: Anmeldung und Abmeldung im IRC
15 */
18 #include "portab.h"
20 #include "imp.h"
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "ngircd.h"
27 #include "resolve.h"
28 #include "conf.h"
29 #include "conn.h"
30 #include "client.h"
31 #include "channel.h"
32 #include "log.h"
33 #include "messages.h"
34 #include "parse.h"
35 #include "irc.h"
36 #include "irc-write.h"
38 #include "exp.h"
39 #include "irc-login.h"
42 LOCAL BOOLEAN Hello_User PARAMS(( CLIENT *Client ));
43 LOCAL VOID Kill_Nick PARAMS(( CHAR *Nick, CHAR *Reason ));
46 GLOBAL BOOLEAN
47 IRC_PASS( CLIENT *Client, REQUEST *Req )
48 {
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))
56 {
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 );
64 return CONNECTED;
65 }
66 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
67 {
68 CHAR c2, c4, *type, *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 ermitteln */
78 if( strlen( Req->argv[1] ) >= 4 )
79 {
80 c2 = Req->argv[1][2];
81 c4 = 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] );
88 Req->argv[1][2] = c2;
89 Req->argv[1][4] = c4;
90 }
91 else protohigh = protolow = 0;
93 /* Protokoll-Typ */
94 if( strlen( Req->argv[1] ) > 4 ) type = &Req->argv[1][4];
95 else type = NULL;
97 /* Implementation, Version und ngIRCd-Flags */
98 impl = Req->argv[2];
99 ptr = strchr( impl, '|' );
100 if( ptr ) *ptr = '\0';
102 if( type && ( strcmp( type, PROTOIRCPLUS ) == 0 ))
104 /* auf der anderen Seite laeuft ein Server, der
105 * ebenfalls das IRC+-Protokoll versteht */
106 serverver = ptr + 1;
107 flags = strchr( serverver, ':' );
108 if( flags )
110 *flags = '\0';
111 flags++;
113 else flags = "";
114 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 );
116 else
118 serverver = flags = "";
119 Log( LOG_INFO, "Connection %d: Peer announces itself as \"%s\" using protocol %d.%d.", Client_Conn( Client ), impl, protohigh, protolow );
122 Client_SetType( Client, CLIENT_GOTPASSSERVER );
123 Client_SetFlags( Client, flags );
125 return CONNECTED;
127 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
129 /* Falsche Anzahl Parameter? */
130 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
132 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
133 } /* IRC_PASS */
136 GLOBAL BOOLEAN
137 IRC_NICK( CLIENT *Client, REQUEST *Req )
139 CLIENT *intr_c, *target, *c;
140 CHAR *modes;
142 assert( Client != NULL );
143 assert( Req != NULL );
145 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
146 #ifndef STRICT_RFC
147 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 ))
148 #else
149 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 ))
150 #endif
152 /* User-Registrierung bzw. Nick-Aenderung */
154 /* Falsche Anzahl Parameter? */
155 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
157 /* "Ziel-Client" ermitteln */
158 if( Client_Type( Client ) == CLIENT_SERVER )
160 target = Client_Search( Req->prefix );
161 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
163 else
165 /* Ist der Client "restricted"? */
166 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
167 target = Client;
170 #ifndef STRICT_RFC
171 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
172 * wir nichts. So macht es das Original und mind. Snak hat probleme,
173 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
174 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
175 #endif
177 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
178 * will nur die Gross- und Kleinschreibung aendern. Das darf
179 * er natuerlich machen :-) */
180 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
182 if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
185 if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
187 /* Neuer Client */
188 Log( LOG_DEBUG, "Connection %d: got valid NICK command ...", Client_Conn( Client ));
190 /* Client-Nick registrieren */
191 Client_SetID( target, Req->argv[0] );
193 /* schon ein USER da? Dann registrieren! */
194 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
195 else Client_SetType( Client, CLIENT_GOTNICK );
197 else
199 /* Nick-Aenderung */
200 if( Client_Conn( target ) > NONE )
202 /* lokaler Client */
203 Log( LOG_INFO, "User \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".", Client_Mask( target ), Client_Conn( target ), Client_ID( target ), Req->argv[0] );
205 else
207 /* Remote-Client */
208 Log( LOG_DEBUG, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
211 /* alle betroffenen User und Server ueber Nick-Aenderung informieren */
212 if( Client_Type( Client ) == CLIENT_USER ) IRC_WriteStrClientPrefix( Client, Client, "NICK :%s", Req->argv[0] );
213 IRC_WriteStrServersPrefix( Client, target, "NICK :%s", Req->argv[0] );
214 IRC_WriteStrRelatedPrefix( target, target, FALSE, "NICK :%s", Req->argv[0] );
216 /* neuen Client-Nick speichern */
217 Client_SetID( target, Req->argv[0] );
220 return CONNECTED;
222 else if( Client_Type( Client ) == CLIENT_SERVER )
224 /* Server fuehrt neuen Client ein */
226 /* Falsche Anzahl Parameter? */
227 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
229 /* Nick ueberpruefen */
230 c = Client_Search( Req->argv[0] );
231 if( c )
233 /* Der neue Nick ist auf diesem Server bereits registriert:
234 * sowohl der neue, als auch der alte Client muessen nun
235 * disconnectiert werden. */
236 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
237 Kill_Nick( Req->argv[0], "Nick collision" );
238 return CONNECTED;
241 /* Server, zu dem der Client connectiert ist, suchen */
242 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
243 if( ! intr_c )
245 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
246 Kill_Nick( Req->argv[0], "Unknown server" );
247 return CONNECTED;
250 /* Neue Client-Struktur anlegen */
251 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 );
252 if( ! c )
254 /* Eine neue Client-Struktur konnte nicht angelegt werden.
255 * Der Client muss disconnectiert werden, damit der Netz-
256 * status konsistent bleibt. */
257 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
258 Kill_Nick( Req->argv[0], "Server error" );
259 return CONNECTED;
262 modes = Client_Modes( c );
263 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": "" );
264 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": "" );
266 /* Andere Server, ausser dem Introducer, informieren */
267 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] );
269 return CONNECTED;
271 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
272 } /* IRC_NICK */
275 GLOBAL BOOLEAN
276 IRC_USER( CLIENT *Client, REQUEST *Req )
278 assert( Client != NULL );
279 assert( Req != NULL );
281 #ifndef STRICT_RFC
282 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
283 #else
284 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
285 #endif
287 /* Falsche Anzahl Parameter? */
288 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
290 Client_SetUser( Client, Req->argv[0], FALSE );
291 Client_SetInfo( Client, Req->argv[3] );
293 Log( LOG_DEBUG, "Connection %d: got valid USER command ...", Client_Conn( Client ));
294 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
295 else Client_SetType( Client, CLIENT_GOTUSER );
296 return CONNECTED;
298 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
300 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
302 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
303 } /* IRC_USER */
306 GLOBAL BOOLEAN
307 IRC_QUIT( CLIENT *Client, REQUEST *Req )
309 CLIENT *target;
311 assert( Client != NULL );
312 assert( Req != NULL );
314 if ( Client_Type( Client ) == CLIENT_SERVER )
316 /* Server */
318 /* Falsche Anzahl Parameter? */
319 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
321 target = Client_Search( Req->prefix );
322 if( ! target )
324 /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
325 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
326 return CONNECTED;
329 if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL, TRUE );
330 else Client_Destroy( target, "Got QUIT command.", Req->argv[0], TRUE );
332 return CONNECTED;
334 else
336 /* User, Service, oder noch nicht registriert */
338 /* Falsche Anzahl Parameter? */
339 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
341 if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
342 else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
344 return DISCONNECTED;
346 } /* IRC_QUIT */
349 GLOBAL BOOLEAN
350 IRC_PING( CLIENT *Client, REQUEST *Req )
352 CLIENT *target, *from;
354 assert( Client != NULL );
355 assert( Req != NULL );
357 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
359 /* Falsche Anzahl Parameter? */
360 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
361 #ifdef STRICT_RFC
362 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
363 #endif
365 if( Req->argc > 1 )
367 /* es wurde ein Ziel-Client angegeben */
368 target = Client_Search( Req->argv[1] );
369 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
370 if( target != Client_ThisServer( ))
372 /* ok, forwarden */
373 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
374 else from = Client;
375 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
376 return IRC_WriteStrClientPrefix( target, from, "PING %s :%s", Client_ID( from ), Req->argv[1] );
380 Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
381 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
382 } /* IRC_PING */
385 GLOBAL BOOLEAN
386 IRC_PONG( CLIENT *Client, REQUEST *Req )
388 CLIENT *target, *from;
390 assert( Client != NULL );
391 assert( Req != NULL );
393 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
395 /* Falsche Anzahl Parameter? */
396 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
397 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
399 /* forwarden? */
400 if( Req->argc == 2 )
402 target = Client_Search( Req->argv[1] );
403 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
404 if( target != Client_ThisServer( ))
406 /* ok, forwarden */
407 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
408 else from = Client;
409 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
410 return IRC_WriteStrClientPrefix( target, from, "PONG %s :%s", Client_ID( from ), Req->argv[1] );
414 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
415 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
417 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 )));
418 else Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
420 return CONNECTED;
421 } /* IRC_PONG */
424 LOCAL BOOLEAN
425 Hello_User( CLIENT *Client )
427 assert( Client != NULL );
429 /* Passwort ueberpruefen */
430 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
432 /* Falsches Passwort */
433 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
434 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
435 return DISCONNECTED;
438 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
440 /* Andere Server informieren */
441 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 ));
443 if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return FALSE;
444 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), VERSION, TARGET_CPU, TARGET_VENDOR, TARGET_OS )) return FALSE;
445 if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return FALSE;
446 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), VERSION, USERMODES, CHANMODES )) return FALSE;
448 Client_SetType( Client, CLIENT_USER );
450 if( ! IRC_Send_LUSERS( Client )) return DISCONNECTED;
451 if( ! IRC_Show_MOTD( Client )) return DISCONNECTED;
453 return CONNECTED;
454 } /* Hello_User */
457 LOCAL VOID
458 Kill_Nick( CHAR *Nick, CHAR *Reason )
460 CLIENT *c;
462 assert( Nick != NULL );
463 assert( Reason != NULL );
465 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
467 /* andere Server benachrichtigen */
468 IRC_WriteStrServers( NULL, "KILL %s :%s", Nick, Reason );
470 /* Ggf. einen eigenen Client toeten */
471 c = Client_Search( Nick );
472 if( c && ( Client_Conn( c ) != NONE )) Conn_Close( Client_Conn( c ), NULL, Reason, TRUE );
473 } /* Kill_Nick */
476 /* -eof- */