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