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