Blob


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 */
15 #include "portab.h"
17 static char UNUSED id[] = "$Id: irc-login.c,v 1.40 2004/03/11 22:16:31 alex Exp $";
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <strings.h>
26 #include "ngircd.h"
27 #include "resolve.h"
28 #include "conn-func.h"
29 #include "conf.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-info.h"
37 #include "irc-write.h"
38 #include "cvs-version.h"
40 #include "exp.h"
41 #include "irc-login.h"
44 LOCAL BOOLEAN Hello_User PARAMS(( CLIENT *Client ));
45 LOCAL VOID Kill_Nick PARAMS(( CHAR *Nick, CHAR *Reason ));
48 GLOBAL BOOLEAN
49 IRC_PASS( CLIENT *Client, REQUEST *Req )
50 {
51 assert( Client != NULL );
52 assert( Req != NULL );
54 /* Fehler liefern, wenn kein lokaler Client */
55 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
57 if(( Client_Type( Client ) == CLIENT_UNKNOWN ) && ( Req->argc == 1))
58 {
59 /* noch nicht registrierte unbekannte Verbindung */
60 Log( LOG_DEBUG, "Connection %d: got PASS command ...", Client_Conn( Client ));
62 /* Passwort speichern */
63 Client_SetPassword( Client, Req->argv[0] );
65 Client_SetType( Client, CLIENT_GOTPASS );
66 return CONNECTED;
67 }
68 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
69 {
70 CHAR c2, c4, *type, *impl, *serverver, *flags, *ptr, *ircflags;
71 INT protohigh, protolow;
73 /* noch nicht registrierte Server-Verbindung */
74 Log( LOG_DEBUG, "Connection %d: got PASS command (new server link) ...", Client_Conn( Client ));
76 /* Passwort speichern */
77 Client_SetPassword( Client, Req->argv[0] );
79 /* Protokollversion ermitteln */
80 if( strlen( Req->argv[1] ) >= 4 )
81 {
82 c2 = Req->argv[1][2];
83 c4 = Req->argv[1][4];
85 Req->argv[1][4] = '\0';
86 protolow = atoi( &Req->argv[1][2] );
87 Req->argv[1][2] = '\0';
88 protohigh = atoi( Req->argv[1] );
90 Req->argv[1][2] = c2;
91 Req->argv[1][4] = c4;
92 }
93 else protohigh = protolow = 0;
95 /* Protokoll-Typ */
96 if( strlen( Req->argv[1] ) > 4 ) type = &Req->argv[1][4];
97 else type = NULL;
99 /* IRC-Flags (nach RFC 2813) */
100 if( Req->argc >= 4 ) ircflags = Req->argv[3];
101 else ircflags = "";
103 /* Implementation, Version und ngIRCd-Flags */
104 impl = Req->argv[2];
105 ptr = strchr( impl, '|' );
106 if( ptr ) *ptr = '\0';
108 if( type && ( strcmp( type, PROTOIRCPLUS ) == 0 ))
110 /* auf der anderen Seite laeuft ein Server, der
111 * ebenfalls das IRC+-Protokoll versteht */
112 serverver = ptr + 1;
113 flags = strchr( serverver, ':' );
114 if( flags )
116 *flags = '\0';
117 flags++;
119 else flags = "";
120 Log( LOG_INFO, "Peer announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").", impl, serverver, protohigh, protolow, flags );
122 else
124 /* auf der anderen Seite laeuft ein Server, der
125 * nur das Originalprotokoll unterstuetzt */
126 serverver = "";
127 if( strchr( ircflags, 'Z' )) flags = "Z";
128 else flags = "";
129 Log( LOG_INFO, "Peer announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").", impl, protohigh, protolow, flags );
132 Client_SetType( Client, CLIENT_GOTPASSSERVER );
133 Client_SetFlags( Client, flags );
135 return CONNECTED;
137 else if(( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER ))
139 /* Falsche Anzahl Parameter? */
140 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
142 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
143 } /* IRC_PASS */
146 GLOBAL BOOLEAN
147 IRC_NICK( CLIENT *Client, REQUEST *Req )
149 CLIENT *intr_c, *target, *c;
150 CHAR *modes;
152 assert( Client != NULL );
153 assert( Req != NULL );
155 /* Zumindest BitchX sendet NICK-USER in der falschen Reihenfolge. */
156 #ifndef STRICT_RFC
157 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 ))
158 #else
159 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 ))
160 #endif
162 /* User-Registrierung bzw. Nick-Aenderung */
164 /* Falsche Anzahl Parameter? */
165 if( Req->argc != 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
167 /* "Ziel-Client" ermitteln */
168 if( Client_Type( Client ) == CLIENT_SERVER )
170 target = Client_Search( Req->prefix );
171 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
173 else
175 /* Ist der Client "restricted"? */
176 if( Client_HasMode( Client, 'r' )) return IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
177 target = Client;
180 #ifndef STRICT_RFC
181 /* Wenn der Client zu seinem eigenen Nick wechseln will, so machen
182 * wir nichts. So macht es das Original und mind. Snak hat probleme,
183 * wenn wir es nicht so machen. Ob es so okay ist? Hm ... */
184 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 ) return CONNECTED;
185 #endif
187 /* pruefen, ob Nick bereits vergeben. Speziallfall: der Client
188 * will nur die Gross- und Kleinschreibung aendern. Das darf
189 * er natuerlich machen :-) */
190 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
192 if( ! Client_CheckNick( target, Req->argv[0] )) return CONNECTED;
195 if(( Client_Type( target ) != CLIENT_USER ) && ( Client_Type( target ) != CLIENT_SERVER ))
197 /* Neuer Client */
198 Log( LOG_DEBUG, "Connection %d: got valid NICK command ...", Client_Conn( Client ));
200 /* Client-Nick registrieren */
201 Client_SetID( target, Req->argv[0] );
203 /* schon ein USER da? Dann registrieren! */
204 if( Client_Type( Client ) == CLIENT_GOTUSER ) return Hello_User( Client );
205 else Client_SetType( Client, CLIENT_GOTNICK );
207 else
209 /* Nick-Aenderung */
210 if( Client_Conn( target ) > NONE )
212 /* lokaler Client */
213 Log( LOG_INFO, "User \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".", Client_Mask( target ), Client_Conn( target ), Client_ID( target ), Req->argv[0] );
215 else
217 /* Remote-Client */
218 Log( LOG_DEBUG, "User \"%s\" changed nick: \"%s\" -> \"%s\".", Client_Mask( target ), Client_ID( target ), Req->argv[0] );
221 /* alle betroffenen User und Server ueber Nick-Aenderung informieren */
222 if( Client_Type( Client ) == CLIENT_USER ) IRC_WriteStrClientPrefix( Client, Client, "NICK :%s", Req->argv[0] );
223 IRC_WriteStrServersPrefix( Client, target, "NICK :%s", Req->argv[0] );
224 IRC_WriteStrRelatedPrefix( target, target, FALSE, "NICK :%s", Req->argv[0] );
226 /* neuen Client-Nick speichern */
227 Client_SetID( target, Req->argv[0] );
228 IRC_SetPenalty( target, 2 );
231 return CONNECTED;
233 else if( Client_Type( Client ) == CLIENT_SERVER )
235 /* Server fuehrt neuen Client ein */
237 /* Falsche Anzahl Parameter? */
238 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
240 /* Nick ueberpruefen */
241 c = Client_Search( Req->argv[0] );
242 if( c )
244 /* Der neue Nick ist auf diesem Server bereits registriert:
245 * sowohl der neue, als auch der alte Client muessen nun
246 * disconnectiert werden. */
247 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
248 Kill_Nick( Req->argv[0], "Nick collision" );
249 return CONNECTED;
252 /* Server, zu dem der Client connectiert ist, suchen */
253 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
254 if( ! intr_c )
256 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
257 Kill_Nick( Req->argv[0], "Unknown server" );
258 return CONNECTED;
261 /* Neue Client-Struktur anlegen */
262 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 );
263 if( ! c )
265 /* Eine neue Client-Struktur konnte nicht angelegt werden.
266 * Der Client muss disconnectiert werden, damit der Netz-
267 * status konsistent bleibt. */
268 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
269 Kill_Nick( Req->argv[0], "Server error" );
270 return CONNECTED;
273 modes = Client_Modes( c );
274 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": "" );
275 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": "" );
277 /* Andere Server, ausser dem Introducer, informieren */
278 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] );
280 return CONNECTED;
282 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
283 } /* IRC_NICK */
286 GLOBAL BOOLEAN
287 IRC_USER( CLIENT *Client, REQUEST *Req )
289 #ifdef IDENTAUTH
290 CHAR *ptr;
291 #endif
293 assert( Client != NULL );
294 assert( Req != NULL );
296 #ifndef STRICT_RFC
297 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
298 #else
299 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
300 #endif
302 /* Wrong number of parameters? */
303 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
305 /* User name */
306 #ifdef IDENTAUTH
307 ptr = Client_User( Client );
308 if( ! ptr || ! *ptr || *ptr == '~' ) Client_SetUser( Client, Req->argv[0], FALSE );
309 #else
310 Client_SetUser( Client, Req->argv[0], FALSE );
311 #endif
313 /* "Real name" or user info text: Don't set it to the empty string, the original ircd
314 * can't deal with such "real names" (e. g. "USER user * * :") ... */
315 if( *Req->argv[3] ) Client_SetInfo( Client, Req->argv[3] );
316 else Client_SetInfo( Client, "-" );
318 Log( LOG_DEBUG, "Connection %d: got valid USER command ...", Client_Conn( Client ));
319 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
320 else Client_SetType( Client, CLIENT_GOTUSER );
321 return CONNECTED;
323 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
325 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
327 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
328 } /* IRC_USER */
331 GLOBAL BOOLEAN
332 IRC_QUIT( CLIENT *Client, REQUEST *Req )
334 CLIENT *target;
336 assert( Client != NULL );
337 assert( Req != NULL );
339 if ( Client_Type( Client ) == CLIENT_SERVER )
341 /* Server */
343 /* Falsche Anzahl Parameter? */
344 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
346 target = Client_Search( Req->prefix );
347 if( ! target )
349 /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
350 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
351 return CONNECTED;
354 if( Req->argc == 0 ) Client_Destroy( target, "Got QUIT command.", NULL, TRUE );
355 else Client_Destroy( target, "Got QUIT command.", Req->argv[0], TRUE );
357 return CONNECTED;
359 else
361 /* User, Service, oder noch nicht registriert */
363 /* Falsche Anzahl Parameter? */
364 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
366 if( Req->argc == 0 ) Conn_Close( Client_Conn( Client ), "Got QUIT command.", NULL, TRUE );
367 else Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argv[0], TRUE );
369 return DISCONNECTED;
371 } /* IRC_QUIT */
374 GLOBAL BOOLEAN
375 IRC_PING( CLIENT *Client, REQUEST *Req )
377 CLIENT *target, *from;
379 assert( Client != NULL );
380 assert( Req != NULL );
382 /* Falsche Anzahl Parameter? */
383 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
384 #ifdef STRICT_RFC
385 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
386 #endif
388 if( Req->argc > 1 )
390 /* es wurde ein Ziel-Client angegeben */
391 target = Client_Search( Req->argv[1] );
392 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
393 if( target != Client_ThisServer( ))
395 /* ok, forwarden */
396 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
397 else from = Client;
398 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
399 return IRC_WriteStrClientPrefix( target, from, "PING %s :%s", Client_ID( from ), Req->argv[1] );
403 Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
404 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
405 } /* IRC_PING */
408 GLOBAL BOOLEAN
409 IRC_PONG( CLIENT *Client, REQUEST *Req )
411 CLIENT *target, *from;
413 assert( Client != NULL );
414 assert( Req != NULL );
416 /* Falsche Anzahl Parameter? */
417 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
418 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
420 /* forwarden? */
421 if( Req->argc == 2 )
423 target = Client_Search( Req->argv[1] );
424 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
425 if( target != Client_ThisServer( ))
427 /* ok, forwarden */
428 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
429 else from = Client;
430 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
431 return IRC_WriteStrClientPrefix( target, from, "PONG %s :%s", Client_ID( from ), Req->argv[1] );
435 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
436 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
438 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 )));
439 else Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
441 return CONNECTED;
442 } /* IRC_PONG */
445 LOCAL BOOLEAN
446 Hello_User( CLIENT *Client )
448 #ifdef CVSDATE
449 CHAR ver[12], vertxt[30];
450 #endif
452 assert( Client != NULL );
454 /* Check password ... */
455 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
457 /* Bad password! */
458 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
459 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
460 return DISCONNECTED;
463 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
465 /* Inform other servers */
466 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 ));
468 /* Welcome :-) */
469 if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return FALSE;
471 /* Version and system type */
472 #ifdef CVSDATE
473 strlcpy( ver, CVSDATE, sizeof( ver ));
474 strncpy( ver + 4, ver + 5, 2 );
475 strncpy( ver + 6, ver + 8, 3 );
476 snprintf( vertxt, sizeof( vertxt ), "%s(%s)", PACKAGE_VERSION, ver );
477 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), vertxt, TARGET_CPU, TARGET_VENDOR, TARGET_OS )) return FALSE;
478 #else
479 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), PACKAGE_VERSION, TARGET_CPU, TARGET_VENDOR, TARGET_OS )) return FALSE;
480 #endif
482 if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return FALSE;
483 #ifdef CVSDATE
484 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), vertxt, USERMODES, CHANMODES )) return FALSE;
485 #else
486 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), PACKAGE_VERSION, USERMODES, CHANMODES )) return FALSE;
487 #endif
489 /* Features */
490 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;
492 Client_SetType( Client, CLIENT_USER );
494 if( ! IRC_Send_LUSERS( Client )) return DISCONNECTED;
495 if( ! IRC_Show_MOTD( Client )) return DISCONNECTED;
497 /* Suspend the client for a second ... */
498 IRC_SetPenalty( Client, 1 );
500 return CONNECTED;
501 } /* Hello_User */
504 LOCAL VOID
505 Kill_Nick( CHAR *Nick, CHAR *Reason )
507 REQUEST r;
509 assert( Nick != NULL );
510 assert( Reason != NULL );
512 r.prefix = (CHAR *)Client_ThisServer( );
513 r.argv[0] = Nick;
514 r.argv[1] = Reason;
515 r.argc = 2;
517 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
518 IRC_KILL( Client_ThisServer( ), &r );
519 } /* Kill_Nick */
522 /* -eof- */