2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
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.
17 static char UNUSED id[] = "$Id: irc-login.c,v 1.45 2005/07/28 16:23:55 fw Exp $";
28 #include "conn-func.h"
37 #include "irc-write.h"
38 #include "cvs-version.h"
41 #include "irc-login.h"
44 LOCAL bool Hello_User PARAMS(( CLIENT *Client ));
45 LOCAL void Kill_Nick PARAMS(( char *Nick, char *Reason ));
49 IRC_PASS( CLIENT *Client, REQUEST *Req )
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))
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 );
68 else if((( Client_Type( Client ) == CLIENT_UNKNOWN ) || ( Client_Type( Client ) == CLIENT_UNKNOWNSERVER )) && (( Req->argc == 3 ) || ( Req->argc == 4 )))
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 )
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] );
93 else protohigh = protolow = 0;
96 if( strlen( Req->argv[1] ) > 4 ) type = &Req->argv[1][4];
99 /* IRC-Flags (nach RFC 2813) */
100 if( Req->argc >= 4 ) ircflags = Req->argv[3];
103 /* Implementation, Version und ngIRCd-Flags */
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 */
113 flags = strchr( serverver, ':' );
120 Log( LOG_INFO, "Peer announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").", impl, serverver, protohigh, protolow, flags );
124 /* auf der anderen Seite laeuft ein Server, der
125 * nur das Originalprotokoll unterstuetzt */
127 if( strchr( ircflags, 'Z' )) flags = "Z";
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 );
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 ));
147 * IRC "NICK" command.
148 * This function implements the IRC command "NICK" which is used to register
149 * with the server, to change already registered nicknames and to introduce
150 * new users which are connected to other servers.
153 IRC_NICK( CLIENT *Client, REQUEST *Req )
155 CLIENT *intr_c, *target, *c;
158 assert( Client != NULL );
159 assert( Req != NULL );
162 /* Some IRC clients, for example BitchX, send the NICK and USER
163 * commands in the wrong order ... */
164 if( Client_Type( Client ) == CLIENT_UNKNOWN
165 || Client_Type( Client ) == CLIENT_GOTPASS
166 || Client_Type( Client ) == CLIENT_GOTNICK
167 || Client_Type( Client ) == CLIENT_GOTUSER
168 || Client_Type( Client ) == CLIENT_USER
169 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
171 if( Client_Type( Client ) == CLIENT_UNKNOWN
172 || Client_Type( Client ) == CLIENT_GOTPASS
173 || Client_Type( Client ) == CLIENT_GOTNICK
174 || Client_Type( Client ) == CLIENT_USER
175 || ( Client_Type( Client ) == CLIENT_SERVER && Req->argc == 1 ))
178 /* User registration or change of nickname */
180 /* Wrong number of arguments? */
182 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
186 /* Search "target" client */
187 if( Client_Type( Client ) == CLIENT_SERVER )
189 target = Client_Search( Req->prefix );
191 return IRC_WriteStrClient( Client,
198 /* Is this a restricted client? */
199 if( Client_HasMode( Client, 'r' ))
200 return IRC_WriteStrClient( Client,
202 Client_ID( Client ));
208 /* If the clients tries to change to its own nickname we won't
209 * do anything. This is how the original ircd behaves and some
210 * clients (for example Snak) expect it to be like this.
211 * But I doubt that this is "really the right thing" ... */
212 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 )
216 /* Check that the new nickname is available. Special case:
217 * the client only changes from/to upper to lower case. */
218 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
220 if( ! Client_CheckNick( target, Req->argv[0] ))
224 if(( Client_Type( target ) != CLIENT_USER )
225 && ( Client_Type( target ) != CLIENT_SERVER ))
228 Log( LOG_DEBUG, "Connection %d: got valid NICK command ...",
229 Client_Conn( Client ));
231 /* Register new nickname of this client */
232 Client_SetID( target, Req->argv[0] );
234 /* If we received a valid USER command already then
235 * register the new client! */
236 if( Client_Type( Client ) == CLIENT_GOTUSER )
237 return Hello_User( Client );
239 Client_SetType( Client, CLIENT_GOTNICK );
243 /* Nickname change */
244 if( Client_Conn( target ) > NONE )
248 "User \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
249 Client_Mask( target ), Client_Conn( target ),
250 Client_ID( target ), Req->argv[0] );
256 "User \"%s\" changed nick: \"%s\" -> \"%s\".",
257 Client_Mask( target ), Client_ID( target ),
261 /* Inform all users and servers (which have to know)
262 * of this nickname change */
263 if( Client_Type( Client ) == CLIENT_USER )
264 IRC_WriteStrClientPrefix( Client, Client,
267 IRC_WriteStrServersPrefix( Client, target,
268 "NICK :%s", Req->argv[0] );
269 IRC_WriteStrRelatedPrefix( target, target, false,
270 "NICK :%s", Req->argv[0] );
272 /* Register old nickname for WHOWAS queries */
273 Client_RegisterWhowas( target );
275 /* Save new nickname */
276 Client_SetID( target, Req->argv[0] );
278 IRC_SetPenalty( target, 2 );
283 else if( Client_Type( Client ) == CLIENT_SERVER )
285 /* Server introduces new client */
287 /* Falsche Anzahl Parameter? */
288 if( Req->argc != 7 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
290 /* Nick ueberpruefen */
291 c = Client_Search( Req->argv[0] );
294 /* Der neue Nick ist auf diesem Server bereits registriert:
295 * sowohl der neue, als auch der alte Client muessen nun
296 * disconnectiert werden. */
297 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
298 Kill_Nick( Req->argv[0], "Nick collision" );
302 /* Server, zu dem der Client connectiert ist, suchen */
303 intr_c = Client_GetFromToken( Client, atoi( Req->argv[4] ));
306 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
307 Kill_Nick( Req->argv[0], "Unknown server" );
311 /* Neue Client-Struktur anlegen */
312 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);
315 /* Eine neue Client-Struktur konnte nicht angelegt werden.
316 * Der Client muss disconnectiert werden, damit der Netz-
317 * status konsistent bleibt. */
318 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
319 Kill_Nick( Req->argv[0], "Server error" );
323 modes = Client_Modes( c );
324 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": "" );
325 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": "" );
327 /* Andere Server, ausser dem Introducer, informieren */
328 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] );
332 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
337 IRC_USER( CLIENT *Client, REQUEST *Req )
343 assert( Client != NULL );
344 assert( Req != NULL );
347 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS || Client_Type( Client ) == CLIENT_UNKNOWN )
349 if( Client_Type( Client ) == CLIENT_GOTNICK || Client_Type( Client ) == CLIENT_GOTPASS )
352 /* Wrong number of parameters? */
353 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
357 ptr = Client_User( Client );
358 if( ! ptr || ! *ptr || *ptr == '~' ) Client_SetUser( Client, Req->argv[0], false );
360 Client_SetUser( Client, Req->argv[0], false );
363 /* "Real name" or user info text: Don't set it to the empty string, the original ircd
364 * can't deal with such "real names" (e. g. "USER user * * :") ... */
365 if( *Req->argv[3] ) Client_SetInfo( Client, Req->argv[3] );
366 else Client_SetInfo( Client, "-" );
368 Log( LOG_DEBUG, "Connection %d: got valid USER command ...", Client_Conn( Client ));
369 if( Client_Type( Client ) == CLIENT_GOTNICK ) return Hello_User( Client );
370 else Client_SetType( Client, CLIENT_GOTUSER );
373 else if( Client_Type( Client ) == CLIENT_USER || Client_Type( Client ) == CLIENT_SERVER || Client_Type( Client ) == CLIENT_SERVICE )
375 return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
377 else return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
382 IRC_QUIT( CLIENT *Client, REQUEST *Req )
385 char quitmsg[LINE_LEN];
387 assert( Client != NULL );
388 assert( Req != NULL );
390 /* Wrong number of arguments? */
392 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
395 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
397 if ( Client_Type( Client ) == CLIENT_SERVER )
400 target = Client_Search( Req->prefix );
403 /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
404 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
408 Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
414 if (Req->argc == 1 && quitmsg[0] != '\"') {
415 /* " " to avoid confusion */
416 strlcpy(quitmsg, "\"", sizeof quitmsg);
417 strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
418 strlcat(quitmsg, "\"", sizeof quitmsg );
421 /* User, Service, oder noch nicht registriert */
422 Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
430 IRC_PING( CLIENT *Client, REQUEST *Req )
432 CLIENT *target, *from;
434 assert( Client != NULL );
435 assert( Req != NULL );
437 /* Falsche Anzahl Parameter? */
438 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
440 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
445 /* es wurde ein Ziel-Client angegeben */
446 target = Client_Search( Req->argv[1] );
447 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
448 if( target != Client_ThisServer( ))
451 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
453 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
454 return IRC_WriteStrClientPrefix( target, from, "PING %s :%s", Client_ID( from ), Req->argv[1] );
458 Log( LOG_DEBUG, "Connection %d: got PING, sending PONG ...", Client_Conn( Client ));
459 return IRC_WriteStrClient( Client, "PONG %s :%s", Client_ID( Client_ThisServer( )), Client_ID( Client ));
464 IRC_PONG( CLIENT *Client, REQUEST *Req )
466 CLIENT *target, *from;
468 assert( Client != NULL );
469 assert( Req != NULL );
471 /* Falsche Anzahl Parameter? */
472 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NOORIGIN_MSG, Client_ID( Client ));
473 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
478 target = Client_Search( Req->argv[1] );
479 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->argv[1] );
480 if( target != Client_ThisServer( ))
483 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
485 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG, Client_ID( Client ), Req->prefix );
486 return IRC_WriteStrClientPrefix( target, from, "PONG %s :%s", Client_ID( from ), Req->argv[1] );
490 /* Der Connection-Timestamp wurde schon beim Lesen aus dem Socket
491 * aktualisiert, daher muss das hier nicht mehr gemacht werden. */
493 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 )));
494 else Log( LOG_DEBUG, "Connection %d: received PONG.", Client_Conn( Client ));
501 Hello_User( CLIENT *Client )
504 char ver[12], vertxt[30];
507 assert( Client != NULL );
509 /* Check password ... */
510 if( strcmp( Client_Password( Client ), Conf_ServerPwd ) != 0 )
513 Log( LOG_ERR, "User \"%s\" rejected (connection %d): Bad password!", Client_Mask( Client ), Client_Conn( Client ));
514 Conn_Close( Client_Conn( Client ), NULL, "Bad password", true);
518 Log( LOG_NOTICE, "User \"%s\" registered (connection %d).", Client_Mask( Client ), Client_Conn( Client ));
520 /* Inform other servers */
521 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 ));
524 if( ! IRC_WriteStrClient( Client, RPL_WELCOME_MSG, Client_ID( Client ), Client_Mask( Client ))) return false;
526 /* Version and system type */
528 strlcpy( ver, CVSDATE, sizeof( ver ));
529 strncpy( ver + 4, ver + 5, 2 );
530 strncpy( ver + 6, ver + 8, 3 );
531 snprintf( vertxt, sizeof( vertxt ), "%s(%s)", PACKAGE_VERSION, ver );
532 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), vertxt, TARGET_CPU, TARGET_VENDOR, TARGET_OS )) return false;
534 if( ! IRC_WriteStrClient( Client, RPL_YOURHOST_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), PACKAGE_VERSION, TARGET_CPU, TARGET_VENDOR, TARGET_OS )) return false;
537 if( ! IRC_WriteStrClient( Client, RPL_CREATED_MSG, Client_ID( Client ), NGIRCd_StartStr )) return false;
539 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), vertxt, USERMODES, CHANMODES )) return false;
541 if( ! IRC_WriteStrClient( Client, RPL_MYINFO_MSG, Client_ID( Client ), Client_ID( Client_ThisServer( )), PACKAGE_VERSION, USERMODES, CHANMODES )) return false;
545 if( ! IRC_WriteStrClient( Client, RPL_ISUPPORT_MSG, Client_ID( Client ), CLIENT_NICK_LEN - 1,
546 COMMAND_LEN - 23, CLIENT_AWAY_LEN - 1, Conf_MaxJoins )) return DISCONNECTED;
548 Client_SetType( Client, CLIENT_USER );
550 if( ! IRC_Send_LUSERS( Client )) return DISCONNECTED;
551 if( ! IRC_Show_MOTD( Client )) return DISCONNECTED;
553 /* Suspend the client for a second ... */
554 IRC_SetPenalty( Client, 1 );
561 Kill_Nick( char *Nick, char *Reason )
565 assert( Nick != NULL );
566 assert( Reason != NULL );
568 r.prefix = (char *)Client_ThisServer( );
573 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
574 IRC_KILL( Client_ThisServer( ), &r );