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