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