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