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