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