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