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