]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-login.c
- Log-Level von SQUIT und QUIT bei unbekannten Clients auf DEBUG herabgesetzt.
[ngircd-alex.git] / src / ngircd / irc-login.c
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.5 2002/03/11 17:33:40 alex Exp $
13  *
14  * irc-login.c: Anmeldung und Abmeldung im IRC
15  *
16  * $Log: irc-login.c,v $
17  * Revision 1.5  2002/03/11 17:33:40  alex
18  * - Log-Level von SQUIT und QUIT bei unbekannten Clients auf DEBUG herabgesetzt.
19  *
20  * Revision 1.4  2002/03/10 22:40:22  alex
21  * - IRC_PING() ist, wenn nicht im "strict RFC"-Mode, toleranter und akzptiert
22  *   beliebig viele Parameter: z.B. BitchX sendet soetwas.
23  *
24  * Revision 1.3  2002/03/03 17:15:11  alex
25  * - Source in weitere Module fuer IRC-Befehle aufgesplitted.
26  *
27  * Revision 1.2  2002/03/02 00:49:11  alex
28  * - Bei der USER-Registrierung wird NICK nicht mehr sofort geforwarded,
29  *   sondern erst dann, wenn auch ein gueltiges USER empfangen wurde.
30  *
31  * Revision 1.1  2002/02/27 23:26:21  alex
32  * - Modul aus irc.c bzw. irc.h ausgegliedert.
33  */
34
35
36 #include <portab.h>
37 #include "global.h"
38
39 #include <imp.h>
40 #include <assert.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include "ngircd.h"
46 #include "conf.h"
47 #include "irc.h"
48 #include "irc-write.h"
49 #include "log.h"
50 #include "messages.h"
51
52 #include <exp.h>
53 #include "irc-login.h"
54
55
56 LOCAL BOOLEAN Hello_User( CLIENT *Client );
57 LOCAL VOID Kill_Nick( CHAR *Nick, CHAR *Reason );
58
59
60 GLOBAL BOOLEAN IRC_PASS( CLIENT *Client, REQUEST *Req )
61 {
62         assert( Client != NULL );
63         assert( Req != NULL );
64
65         /* Fehler liefern, wenn kein lokaler Client */
66         if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
67         
68         if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
69         {
70                 /* noch nicht registrierte unbekannte Verbindung */
71                 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
72
73                 /* Passwort speichern */
74                 Client_SetPassword( Client, Req->argv[0] );
75
76                 Client_SetType( Client, CLIENT_GOTPASS );
77                 return CONNECTED;
78         }
79         else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
80         {
81                 /* noch nicht registrierte Server-Verbindung */
82                 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
83
84                 /* Passwort speichern */
85                 Client_SetPassword( Client, Req->argv[0] );
86
87                 Client_SetType( Client, CLIENT_GOTPASSSERVER );
88                 return CONNECTED;
89         }
90         else if(( Client_Type( Client ) == CLIENT_UNKNOWN  ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
91         {
92                 /* Falsche Anzahl Parameter? */
93                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
94         }
95         else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
96 } /* IRC_PASS */
97
98
99 GLOBAL BOOLEAN IRC_NICK( CLIENT *Client, REQUEST *Req )
100 {
101         CLIENT *intr_c, *target, *c;
102         CHAR *modes;
103
104         assert( Client != NULL );
105         assert( Req != NULL );
106
107         /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
108 #ifndef STRICT_RFC
109         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 ))
110 #else
111         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 ))
112 #endif
113         {
114                 /* User-Registrierung bzw. Nick-Aenderung */
115
116                 /* Falsche Anzahl Parameter? */
117                 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
118
119                 /* "Ziel-Client" ermitteln */
120                 if( Client_Type( Client ) == CLIENT_SERVER )
121                 {
122                         target = Client_GetFromID( Req->prefix );
123                         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
124                 }
125                 else
126                 {
127                         /* Ist der Client "restricted"? */
128                         if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
129                         target = Client;
130                 }
131
132 #ifndef STRICT_RFC
133                 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
134                  * wir nichts. So macht es das Original und mind. Snak hat probleme,
135                  * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
136                 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
137 #endif
138                 
139                 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
140                  * will nur die Gross- und Kleinschreibung aendern. Das darf
141                  * er natuerlich machen :-) */
142                 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
143                 {
144                         if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
145                 }
146
147                 if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
148                 {
149                         /* Neuer Client */
150                         Log( LOG_DEBUG, "Connection %d: got valid NICK command ...", Client_Conn( Client ));
151
152                         /* Client-Nick registrieren */
153                         Client_SetID( target, Req->argv[0] );
154
155                         /* schon ein USER da? Dann registrieren! */
156                         if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
157                         else Client_SetType( Client, CLIENT_GOTNICK );
158                 }
159                 else
160                 {
161                         /* Nick-Aenderung */
162                         Log( LOG_INFO, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
163
164                         /* alle betroffenen User und Server ueber Nick-Aenderung informieren */
165                         if( Client_Type( Client ) == CLIENT_USER ) IRC_WriteStrClientPrefix( Client, Client, "NICK :%s", Req->argv[0] );
166                         IRC_WriteStrServersPrefix( Client, target, "NICK :%s", Req->argv[0] );
167                         IRC_WriteStrRelatedPrefix( target, target, FALSE, "NICK :%s", Req->argv[0] );
168                         
169                         /* neuen Client-Nick speichern */
170                         Client_SetID( target, Req->argv[0] );
171                 }
172
173                 return CONNECTED;
174         }
175         else if( Client_Type( Client ) == CLIENT_SERVER )
176         {
177                 /* Server fuehrt neuen Client ein */
178
179                 /* Falsche Anzahl Parameter? */
180                 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
181
182                 /* Nick ueberpruefen */
183                 c = Client_GetFromID( Req->argv[0] );
184                 if( c )
185                 {
186                         /* Der neue Nick ist auf diesem Server bereits registriert:
187                          * sowohl der neue, als auch der alte Client muessen nun
188                          * disconnectiert werden. */
189                         Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
190                         Kill_Nick( Req->argv[0], "Nick collision" );
191                         return CONNECTED;
192                 }
193
194                 /* Server, zu dem der Client connectiert ist, suchen */
195                 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
196                 if( ! intr_c )
197                 {
198                         Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
199                         Kill_Nick( Req->argv[0], "Unknown server" );
200                         return CONNECTED;
201                 }
202
203                 /* Neue Client-Struktur anlegen */
204                 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 );
205                 if( ! c )
206                 {
207                         /* Eine neue Client-Struktur konnte nicht angelegt werden.
208                          * Der Client muss disconnectiert werden, damit der Netz-
209                          * status konsistent bleibt. */
210                         Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
211                         Kill_Nick( Req->argv[0], "Server error" );
212                         return CONNECTED;
213                 }
214
215                 modes = Client_Modes( c );
216                 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": "" );
217                 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": "" );
218
219                 /* Andere Server, ausser dem Introducer, informieren */
220                 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] );
221
222                 return CONNECTED;
223         }
224         else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
225 } /* IRC_NICK */
226
227
228 GLOBAL BOOLEAN IRC_USER( CLIENT *Client, REQUEST *Req )
229 {
230         assert( Client != NULL );
231         assert( Req != NULL );
232
233 #ifndef STRICT_RFC
234         if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
235 #else
236         if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
237 #endif
238         {
239                 /* Falsche Anzahl Parameter? */
240                 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
241
242                 Client_SetUser( Client, Req->argv[0], FALSE );
243                 Client_SetInfo( Client, Req->argv[3] );
244
245                 Log( LOG_DEBUG, "Connection %d: got valid USER command ...", Client_Conn( Client ));
246                 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
247                 else Client_SetType( Client, CLIENT_GOTUSER );
248                 return CONNECTED;
249         }
250         else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
251         {
252                 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
253         }
254         else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
255 } /* IRC_USER */
256
257
258 GLOBAL BOOLEAN IRC_QUIT( CLIENT *Client, REQUEST *Req )
259 {
260         CLIENT *target;
261         
262         assert( Client != NULL );
263         assert( Req != NULL );
264
265         if(( Client_Type( Client ) == CLIENT_USER ) || ( Client_Type( Client ) == CLIENT_SERVICE ))
266         {
267                 /* User / Service */
268                 
269                 /* Falsche Anzahl Parameter? */
270                 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
271
272                 if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
273                 else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
274                 
275                 return DISCONNECTED;
276         }
277         else if ( Client_Type( Client ) == CLIENT_SERVER )
278         {
279                 /* Server */
280
281                 /* Falsche Anzahl Parameter? */
282                 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
283
284                 target = Client_Search( Req->prefix );
285                 if( ! target )
286                 {
287                         /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
288                         Log( LOG_DEBUG, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
289                         return CONNECTED;
290                 }
291
292                 if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL );
293                 else Client_Destroy( target, "Got QUIT command.", Req->argv[0] );
294
295                 return CONNECTED;
296         }
297         else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
298 } /* IRC_QUIT */
299
300
301 GLOBAL BOOLEAN IRC_PING( CLIENT *Client, REQUEST *Req )
302 {
303         CLIENT *target, *from;
304
305         assert( Client != NULL );
306         assert( Req != NULL );
307
308         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
309
310         /* Falsche Anzahl Parameter? */
311         if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
312 #ifdef STRICT_RFC
313         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
314 #endif
315
316         if( Req->argc > 1 )
317         {
318                 /* es wurde ein Ziel-Client angegeben */
319                 target = Client_GetFromID( Req->argv[1] );
320                 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
321                 if( target != Client_ThisServer( ))
322                 {
323                         /* ok, forwarden */
324                         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
325                         else from = Client;
326                         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
327                         return IRC_WriteStrClientPrefix( target, from, "PING %s :%s", Client_ID( from ), Req->argv[1] );
328                 }
329         }
330
331         Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
332         return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
333 } /* IRC_PING */
334
335
336 GLOBAL BOOLEAN IRC_PONG( CLIENT *Client, REQUEST *Req )
337 {
338         CLIENT *target, *from;
339
340         assert( Client != NULL );
341         assert( Req != NULL );
342
343         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
344
345         /* Falsche Anzahl Parameter? */
346         if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
347         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
348
349         /* forwarden? */
350         if( Req->argc == 2 )
351         {
352                 target = Client_GetFromID( Req->argv[1] );
353                 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
354                 if( target != Client_ThisServer( ))
355                 {
356                         /* ok, forwarden */
357                         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_GetFromID( Req->prefix );
358                         else from = Client;
359                         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
360                         return IRC_WriteStrClientPrefix( target, from, "PONG %s :%s", Client_ID( from ), Req->argv[1] );
361                 }
362         }
363
364         /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
365                 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
366
367         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 )));
368         else Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
369
370         return CONNECTED;
371 } /* IRC_PONG */
372
373
374 LOCAL BOOLEAN Hello_User( CLIENT *Client )
375 {
376         assert( Client != NULL );
377
378         /* Passwort ueberpruefen */
379         if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
380         {
381                 /* Falsches Passwort */
382                 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
383                 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
384                 return DISCONNECTED;
385         }
386
387         Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
388
389         /* Andere Server informieren */
390         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 ));
391
392         if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return FALSE;
393         if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
394         if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return FALSE;
395         if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )))) return FALSE;
396
397         Client_SetType( Client, CLIENT_USER );
398
399         if( ! IRC_Send_LUSERS( Client )) return DISCONNECTED;
400         if( ! IRC_Show_MOTD( Client )) return DISCONNECTED;
401
402         return CONNECTED;
403 } /* Hello_User */
404
405
406 LOCAL VOID Kill_Nick( CHAR *Nick, CHAR *Reason )
407 {
408         CLIENT *c;
409
410         assert( Nick != NULL );
411         assert( Reason != NULL );
412
413         Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
414
415         /* andere Server benachrichtigen */
416         IRC_WriteStrServers( NULL, "KILL %s :%s", Nick, Reason );
417
418         /* Ggf. einen eigenen Client toeten */
419         c = Client_GetFromID( Nick );
420         if( c && ( Client_Conn( c ) != NONE )) Conn_Close( Client_Conn( c ), NULL, Reason, TRUE );
421 } /* Kill_Nick */
422
423
424 /* -eof- */