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