2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2010 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.
29 #include "conn-func.h"
39 #include "irc-write.h"
42 #include "irc-login.h"
45 static bool Hello_User PARAMS(( CLIENT *Client ));
46 static bool Hello_User_PostAuth PARAMS(( CLIENT *Client ));
47 static void Kill_Nick PARAMS(( char *Nick, char *Reason ));
48 static void Introduce_Client PARAMS((CLIENT *To, CLIENT *Client, int Type));
49 static void Reject_Client PARAMS((CLIENT *Client));
51 static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
55 static void cb_Read_Auth_Result PARAMS((int r_fd, UNUSED short events));
59 * Handler for the IRC "PASS" command.
61 * See RFC 2813 section 4.1.1, and RFC 2812 section 3.1.1.
63 * @param Client The client from which this command has been received.
64 * @param Req Request structure with prefix and all parameters.
65 * @returns CONNECTED or DISCONNECTED.
68 IRC_PASS( CLIENT *Client, REQUEST *Req )
70 char *type, *orig_flags;
71 int protohigh, protolow;
73 assert( Client != NULL );
74 assert( Req != NULL );
76 /* Return an error if this is not a local client */
77 if (Client_Conn(Client) <= NONE)
78 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
79 Client_ID(Client), Req->command);
81 if (Client_Type(Client) == CLIENT_UNKNOWN && Req->argc == 1) {
82 /* Not yet registered "unknown" connection, PASS with one
83 * argument: either a regular client, service, or server
84 * using the old RFC 1459 section 4.1.1 syntax. */
85 LogDebug("Connection %d: got PASS command (RFC 1459) ...",
87 } else if ((Client_Type(Client) == CLIENT_UNKNOWN ||
88 Client_Type(Client) == CLIENT_UNKNOWNSERVER) &&
89 (Req->argc == 3 || Req->argc == 4)) {
90 /* Not yet registered "unknown" connection or outgoing server
91 * link, PASS with three or four argument: server using the
92 * RFC 2813 section 4.1.1 syntax. */
93 LogDebug("Connection %d: got PASS command (RFC 2813, new server link) ...",
95 } else if (Client_Type(Client) == CLIENT_UNKNOWN ||
96 Client_Type(Client) == CLIENT_UNKNOWNSERVER) {
97 /* Unregistered connection, but wrong number of arguments: */
98 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
99 Client_ID(Client), Req->command);
101 /* Registered connection, PASS command is not allowed! */
102 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
106 Client_SetPassword(Client, Req->argv[0]);
108 /* Protocol version */
109 if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
112 c2 = Req->argv[1][2];
113 c4 = Req->argv[1][4];
115 Req->argv[1][4] = '\0';
116 protolow = atoi(&Req->argv[1][2]);
117 Req->argv[1][2] = '\0';
118 protohigh = atoi(Req->argv[1]);
120 Req->argv[1][2] = c2;
121 Req->argv[1][4] = c4;
123 Client_SetType(Client, CLIENT_GOTPASS_2813);
125 protohigh = protolow = 0;
126 Client_SetType(Client, CLIENT_GOTPASS);
129 /* Protocol type, see doc/Protocol.txt */
130 if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
131 type = &Req->argv[1][4];
135 /* Protocol flags/options */
137 orig_flags = Req->argv[3];
141 /* Implementation, version and IRC+ flags */
142 if (Req->argc >= 3) {
143 char *impl, *ptr, *serverver, *flags;
146 ptr = strchr(impl, '|');
150 if (type && strcmp(type, PROTOIRCPLUS) == 0) {
151 /* The peer seems to be a server which supports the
152 * IRC+ protocol (see doc/Protocol.txt). */
153 serverver = ptr ? ptr + 1 : "?";
154 flags = strchr(ptr ? serverver : impl, ':');
161 "Peer on conenction %d announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
162 Client_Conn(Client), impl, serverver,
163 protohigh, protolow, flags);
165 /* The peer seems to be a server supporting the
166 * "original" IRC protocol (RFC 2813). */
167 if (strchr(orig_flags, 'Z'))
172 "Peer on connection %d announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
173 Client_Conn(Client), impl,
174 protohigh, protolow, flags);
176 Client_SetFlags(Client, flags);
184 * Handler for the IRC "NICK" command.
186 * See RFC 2812, 3.1.2 "Nick message", and RFC 2813, 4.1.3 "Nick".
188 * This function implements the IRC command "NICK" which is used to register
189 * with the server, to change already registered nicknames and to introduce
190 * new users which are connected to other servers.
192 * @param Client The client from which this command has been received.
193 * @param Req Request structure with prefix and all parameters.
194 * @returns CONNECTED or DISCONNECTED.
197 IRC_NICK( CLIENT *Client, REQUEST *Req )
199 CLIENT *intr_c, *target, *c;
200 char *nick, *user, *hostname, *modes, *info;
203 assert( Client != NULL );
204 assert( Req != NULL );
206 /* Some IRC clients, for example BitchX, send the NICK and USER
207 * commands in the wrong order ... */
208 if(Client_Type(Client) == CLIENT_UNKNOWN
209 || Client_Type(Client) == CLIENT_GOTPASS
210 || Client_Type(Client) == CLIENT_GOTNICK
212 || Client_Type(Client) == CLIENT_GOTUSER
214 || Client_Type(Client) == CLIENT_USER
215 || Client_Type(Client) == CLIENT_SERVICE
216 || (Client_Type(Client) == CLIENT_SERVER && Req->argc == 1))
218 /* User registration or change of nickname */
220 /* Wrong number of arguments? */
222 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
226 /* Search "target" client */
227 if( Client_Type( Client ) == CLIENT_SERVER )
229 target = Client_Search( Req->prefix );
231 return IRC_WriteStrClient( Client,
238 /* Is this a restricted client? */
239 if( Client_HasMode( Client, 'r' ))
240 return IRC_WriteStrClient( Client,
242 Client_ID( Client ));
248 /* If the clients tries to change to its own nickname we won't
249 * do anything. This is how the original ircd behaves and some
250 * clients (for example Snak) expect it to be like this.
251 * But I doubt that this is "really the right thing" ... */
252 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 )
256 /* Check that the new nickname is available. Special case:
257 * the client only changes from/to upper to lower case. */
258 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
260 if( ! Client_CheckNick( target, Req->argv[0] ))
264 if (Client_Type(target) != CLIENT_USER &&
265 Client_Type(target) != CLIENT_SERVICE &&
266 Client_Type(target) != CLIENT_SERVER) {
268 LogDebug("Connection %d: got valid NICK command ...",
269 Client_Conn( Client ));
271 /* Register new nickname of this client */
272 Client_SetID( target, Req->argv[0] );
276 Conn_SetAuthPing(Client_Conn(Client), rand());
277 IRC_WriteStrClient(Client, "PING :%ld",
278 Conn_GetAuthPing(Client_Conn(Client)));
279 LogDebug("Connection %d: sent AUTH PING %ld ...",
281 Conn_GetAuthPing(Client_Conn(Client)));
285 /* If we received a valid USER command already then
286 * register the new client! */
287 if( Client_Type( Client ) == CLIENT_GOTUSER )
288 return Hello_User( Client );
290 Client_SetType( Client, CLIENT_GOTNICK );
292 /* Nickname change */
293 if (Client_Conn(target) > NONE) {
296 "%s \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
297 Client_TypeText(target), Client_Mask(target),
298 Client_Conn(target), Client_ID(target),
300 Conn_UpdateIdle(Client_Conn(target));
303 LogDebug("%s \"%s\" changed nick: \"%s\" -> \"%s\".",
304 Client_TypeText(target),
305 Client_Mask(target), Client_ID(target),
309 /* Inform all users and servers (which have to know)
310 * of this nickname change */
311 if( Client_Type( Client ) == CLIENT_USER )
312 IRC_WriteStrClientPrefix( Client, Client,
315 IRC_WriteStrServersPrefix( Client, target,
316 "NICK :%s", Req->argv[0] );
317 IRC_WriteStrRelatedPrefix( target, target, false,
318 "NICK :%s", Req->argv[0] );
320 /* Register old nickname for WHOWAS queries */
321 Client_RegisterWhowas( target );
323 /* Save new nickname */
324 Client_SetID( target, Req->argv[0] );
326 IRC_SetPenalty( target, 2 );
330 } else if(Client_Type(Client) == CLIENT_SERVER ||
331 Client_Type(Client) == CLIENT_SERVICE) {
332 /* Server or service introduces new client */
334 /* Bad number of parameters? */
335 if (Req->argc != 2 && Req->argc != 7)
336 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
337 Client_ID(Client), Req->command);
339 if (Req->argc >= 7) {
340 /* RFC 2813 compatible syntax */
342 hops = atoi(Req->argv[1]);
344 hostname = Req->argv[3];
345 token = atoi(Req->argv[4]);
346 modes = Req->argv[5] + 1;
349 /* RFC 1459 compatible syntax */
353 hostname = Client_ID(Client);
354 token = atoi(Req->argv[1]);
359 c = Client_Search(nick);
362 * the new nick is already present on this server:
363 * the new and the old one have to be disconnected now.
365 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
366 Kill_Nick( Req->argv[0], "Nick collision" );
370 /* Find the Server this client is connected to */
371 intr_c = Client_GetFromToken(Client, token);
374 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
375 Kill_Nick( Req->argv[0], "Unknown server" );
379 c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
380 token, modes, info, true);
383 /* out of memory, need to disconnect client to keep network state consistent */
384 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
385 Kill_Nick( Req->argv[0], "Server error" );
389 /* RFC 2813: client is now fully registered, inform all the
390 * other servers about the new user.
391 * RFC 1459: announce the new client only after receiving the
392 * USER command, first we need more information! */
394 LogDebug("Client \"%s\" is being registered (RFC 1459) ...",
396 Client_SetType(c, CLIENT_GOTNICK);
398 Introduce_Client(Client, c, CLIENT_USER);
402 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
407 * Handler for the IRC "USER" command.
409 * See RFC 2812, 3.1.3 "User message".
411 * @param Client The client from which this command has been received.
412 * @param Req Request structure with prefix and all parameters.
413 * @returns CONNECTED or DISCONNECTED.
416 IRC_USER(CLIENT * Client, REQUEST * Req)
423 assert(Client != NULL);
426 if (Client_Type(Client) == CLIENT_GOTNICK ||
428 Client_Type(Client) == CLIENT_UNKNOWN ||
430 Client_Type(Client) == CLIENT_GOTPASS)
434 return IRC_WriteStrClient(Client,
435 ERR_NEEDMOREPARAMS_MSG,
441 ptr = Client_User(Client);
442 if (!ptr || !*ptr || *ptr == '~')
443 Client_SetUser(Client, Req->argv[0], false);
445 Client_SetUser(Client, Req->argv[0], false);
447 Client_SetOrigUser(Client, Req->argv[0]);
449 /* "Real name" or user info text: Don't set it to the empty
450 * string, the original ircd can't deal with such "real names"
451 * (e. g. "USER user * * :") ... */
453 Client_SetInfo(Client, Req->argv[3]);
455 Client_SetInfo(Client, "-");
457 LogDebug("Connection %d: got valid USER command ...",
458 Client_Conn(Client));
459 if (Client_Type(Client) == CLIENT_GOTNICK)
460 return Hello_User(Client);
462 Client_SetType(Client, CLIENT_GOTUSER);
465 } else if (Client_Type(Client) == CLIENT_SERVER ||
466 Client_Type(Client) == CLIENT_SERVICE) {
467 /* Server/service updating an user */
469 return IRC_WriteStrClient(Client,
470 ERR_NEEDMOREPARAMS_MSG,
473 c = Client_Search(Req->prefix);
475 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
479 Client_SetUser(c, Req->argv[0], true);
480 Client_SetOrigUser(c, Req->argv[0]);
481 Client_SetHostname(c, Req->argv[1]);
482 Client_SetInfo(c, Req->argv[3]);
484 LogDebug("Connection %d: got valid USER command for \"%s\".",
485 Client_Conn(Client), Client_Mask(c));
487 /* RFC 1459 style user registration?
488 * Introduce client to network: */
489 if (Client_Type(c) == CLIENT_GOTNICK)
490 Introduce_Client(Client, c, CLIENT_USER);
493 } else if (Client_Type(Client) == CLIENT_USER) {
494 /* Already registered connection */
495 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
498 /* Unexpected/invalid connection state? */
499 return IRC_WriteStrClient(Client, ERR_NOTREGISTERED_MSG,
506 * Handler for the IRC "SERVICE" command.
508 * This function implements IRC Services registration using the SERVICE command
509 * defined in RFC 2812 3.1.6 and RFC 2813 4.1.4.
511 * At the moment ngIRCd doesn't support directly linked services, so this
512 * function returns ERR_ERRONEUSNICKNAME when the SERVICE command has not been
513 * received from a peer server.
515 * @param Client The client from which this command has been received.
516 * @param Req Request structure with prefix and all parameters.
517 * @returns CONNECTED or DISCONNECTED..
520 IRC_SERVICE(CLIENT *Client, REQUEST *Req)
523 char *nick, *user, *host, *info, *modes, *ptr;
526 assert(Client != NULL);
529 if (Client_Type(Client) != CLIENT_GOTPASS &&
530 Client_Type(Client) != CLIENT_SERVER)
531 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
535 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
536 Client_ID(Client), Req->command);
538 if (Client_Type(Client) != CLIENT_SERVER)
539 return IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
540 Client_ID(Client), Req->argv[0]);
542 /* Bad number of parameters? */
544 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
545 Client_ID(Client), Req->command);
548 user = NULL; host = NULL;
549 token = atoi(Req->argv[1]);
550 hops = atoi(Req->argv[4]);
553 /* Validate service name ("nick name") */
554 c = Client_Search(nick);
556 /* Nick name collission: disconnect (KILL) both clients! */
557 Log(LOG_ERR, "Server %s introduces already registered service \"%s\"!",
558 Client_ID(Client), nick);
559 Kill_Nick(nick, "Nick collision");
563 /* Get the server to which the service is connected */
564 intr_c = Client_GetFromToken(Client, token);
566 Log(LOG_ERR, "Server %s introduces service \"%s\" on unknown server!?",
567 Client_ID(Client), nick);
568 Kill_Nick(nick, "Unknown server");
572 /* Get user and host name */
573 ptr = strchr(nick, '@');
579 host = Client_Hostname(intr_c);
580 ptr = strchr(nick, '!');
588 /* According to RFC 2812/2813 parameter 4 <type> "is currently reserved
589 * for future usage"; but we use it to transfer the modes and check
590 * that the first character is a '+' sign and ignore it otherwise. */
591 modes = (Req->argv[3][0] == '+') ? ++Req->argv[3] : "";
593 c = Client_NewRemoteUser(intr_c, nick, hops, user, host,
594 token, modes, info, true);
596 /* Couldn't create client structure, so KILL the service to
597 * keep network status consistent ... */
598 Log(LOG_ALERT, "Can't create client structure! (on connection %d)",
599 Client_Conn(Client));
600 Kill_Nick(nick, "Server error");
604 Introduce_Client(Client, c, CLIENT_SERVICE);
610 * Handler for the IRC "WEBIRC" command.
612 * See doc/Protocol.txt, section II.4:
613 * "Update webchat/proxy client information".
615 * @param Client The client from which this command has been received.
616 * @param Req Request structure with prefix and all parameters.
617 * @returns CONNECTED or DISCONNECTED.
620 IRC_WEBIRC(CLIENT *Client, REQUEST *Req)
622 /* Exactly 4 parameters are requited */
624 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
625 Client_ID(Client), Req->command);
627 if (!Conf_WebircPwd[0] || strcmp(Req->argv[0], Conf_WebircPwd) != 0)
628 return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
631 LogDebug("Connection %d: got valid WEBIRC command: user=%s, host=%s, ip=%s",
632 Client_Conn(Client), Req->argv[1], Req->argv[2], Req->argv[3]);
634 Client_SetUser(Client, Req->argv[1], true);
635 Client_SetOrigUser(Client, Req->argv[1]);
636 Client_SetHostname(Client, Req->argv[2]);
642 * Handler for the IRC "QUIT" command.
644 * See RFC 2812, 3.1.7 "Quit", and RFC 2813, 4.1.5 "Quit".
646 * @param Client The client from which this command has been received.
647 * @param Req Request structure with prefix and all parameters.
648 * @returns CONNECTED or DISCONNECTED.
651 IRC_QUIT( CLIENT *Client, REQUEST *Req )
654 char quitmsg[LINE_LEN];
656 assert(Client != NULL);
659 /* Wrong number of arguments? */
661 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
662 Client_ID(Client), Req->command);
665 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
667 if (Client_Type(Client) == CLIENT_SERVER) {
669 target = Client_Search(Req->prefix);
672 "Got QUIT from %s for unknown client!?",
677 if (target != Client) {
678 Client_Destroy(target, "Got QUIT command.",
679 Req->argc == 1 ? quitmsg : NULL, true);
682 Conn_Close(Client_Conn(Client), "Got QUIT command.",
683 Req->argc == 1 ? quitmsg : NULL, true);
687 if (Req->argc == 1 && quitmsg[0] != '\"') {
688 /* " " to avoid confusion */
689 strlcpy(quitmsg, "\"", sizeof quitmsg);
690 strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
691 strlcat(quitmsg, "\"", sizeof quitmsg );
694 /* User, Service, or not yet registered */
695 Conn_Close(Client_Conn(Client), "Got QUIT command.",
696 Req->argc == 1 ? quitmsg : NULL, true);
706 * Handler for HTTP command, e.g. GET and POST
708 * We handle these commands here to avoid the quite long timeout when
709 * some user tries to access this IRC daemon using an web browser ...
711 * @param Client The client from which this command has been received.
712 * @param Req Request structure with prefix and all parameters.
713 * @returns CONNECTED or DISCONNECTED.
716 IRC_QUIT_HTTP( CLIENT *Client, REQUEST *Req )
719 Req->argv[0] = "Oops, HTTP request received? This is IRC!";
720 return IRC_QUIT(Client, Req);
721 } /* IRC_QUIT_HTTP */
727 * Handler for the IRC "PING" command.
729 * See RFC 2812, 3.7.2 "Ping message".
731 * @param Client The client from which this command has been received.
732 * @param Req Request structure with prefix and all parameters.
733 * @returns CONNECTED or DISCONNECTED.
736 IRC_PING(CLIENT *Client, REQUEST *Req)
738 CLIENT *target, *from;
740 assert(Client != NULL);
744 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
747 /* Don't ignore additional arguments when in "strict" mode */
749 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
750 Client_ID(Client), Req->command);
754 /* A target has been specified ... */
755 target = Client_Search(Req->argv[1]);
757 if (!target || Client_Type(target) != CLIENT_SERVER)
758 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
759 Client_ID(Client), Req->argv[1]);
761 if (target != Client_ThisServer()) {
762 /* Ok, we have to forward the PING */
763 if (Client_Type(Client) == CLIENT_SERVER)
764 from = Client_Search(Req->prefix);
768 return IRC_WriteStrClient(Client,
769 ERR_NOSUCHSERVER_MSG,
770 Client_ID(Client), Req->prefix);
772 return IRC_WriteStrClientPrefix(target, from,
773 "PING %s :%s", Req->argv[0],
778 if (Client_Type(Client) == CLIENT_SERVER) {
780 from = Client_Search(Req->prefix);
784 from = Client_ThisServer();
786 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
787 Client_ID(Client), Req->prefix);
789 Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
790 Client_Conn(Client));
793 return IRC_WriteStrClient(Client, "PONG %s :%s",
794 Client_ID(from), Client_ID(Client));
796 /* Some clients depend on the argument being returned in the PONG
797 * reply (not mentioned in any RFC, though) */
798 return IRC_WriteStrClient(Client, "PONG %s :%s",
799 Client_ID(from), Req->argv[0]);
805 * Handler for the IRC "PONG" command.
807 * See RFC 2812, 3.7.3 "Pong message".
809 * @param Client The client from which this command has been received.
810 * @param Req Request structure with prefix and all parameters.
811 * @returns CONNECTED or DISCONNECTED.
814 IRC_PONG(CLIENT *Client, REQUEST *Req)
816 CLIENT *target, *from;
823 assert(Client != NULL);
826 /* Wrong number of arguments? */
828 if (Client_Type(Client) == CLIENT_USER)
829 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
835 if (Client_Type(Client) == CLIENT_USER)
836 return IRC_WriteStrClient(Client,
837 ERR_NEEDMOREPARAMS_MSG,
845 if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
846 target = Client_Search(Req->argv[0]);
848 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
849 Client_ID(Client), Req->argv[0]);
851 from = Client_Search(Req->prefix);
853 if (target != Client_ThisServer() && target != from) {
854 /* Ok, we have to forward the message. */
856 return IRC_WriteStrClient(Client,
857 ERR_NOSUCHSERVER_MSG,
858 Client_ID(Client), Req->prefix);
860 if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
864 return IRC_WriteStrClientPrefix(target, from,
865 "PONG %s :%s", s, Req->argv[1]);
869 /* The connection timestamp has already been updated when the data has
870 * been read from so socket, so we don't need to update it here. */
872 conn = Client_Conn(Client);
875 /* Check authentication PING-PONG ... */
876 auth_ping = Conn_GetAuthPing(conn);
878 LogDebug("AUTH PONG: waiting for token \"%ld\", got \"%s\" ...",
879 auth_ping, Req->argv[0]);
880 if (auth_ping == atoi(Req->argv[0])) {
881 Conn_SetAuthPing(conn, 0);
882 if (Client_Type(Client) == CLIENT_WAITAUTHPING)
885 if (!IRC_WriteStrClient(Client,
886 "To connect, type /QUOTE PONG %ld",
895 "Connection %d: received PONG. Lag: %ld seconds.", conn,
896 time(NULL) - Conn_LastPing(Client_Conn(Client)));
899 "Connection %d: received PONG.", conn);
906 * Initiate client registration.
908 * This function is called after the daemon received the required NICK and
909 * USER commands of a new client. If the daemon is compiled with support for
910 * PAM, the authentication sub-processs is forked; otherwise the global server
911 * password is checked.
913 * @param Client The client logging in.
914 * @returns CONNECTED or DISCONNECTED.
917 Hello_User(CLIENT * Client)
920 int pipefd[2], result;
925 assert(Client != NULL);
926 conn = Client_Conn(Client);
930 /* Did we receive the "auth PONG" already? */
931 if (Conn_GetAuthPing(conn)) {
932 Client_SetType(Client, CLIENT_WAITAUTHPING);
933 LogDebug("Connection %d: Waiting for AUTH PONG ...", conn);
941 /* Don't do any PAM authentication at all, instead emulate
942 * the beahiour of the daemon compiled without PAM support:
943 * because there can't be any "server password", all
944 * passwords supplied are classified as "wrong". */
945 if(Client_Password(Client)[0] == '\0')
946 return Hello_User_PostAuth(Client);
947 Reject_Client(Client);
951 /* Fork child process for PAM authentication; and make sure that the
952 * process timeout is set higher than the login timeout! */
953 pid = Proc_Fork(Conn_GetProcStat(conn), pipefd,
954 cb_Read_Auth_Result, Conf_PongTimeout + 1);
956 LogDebug("Authenticator for connection %d created (PID %d).",
961 Log_Init_Subprocess("Auth");
962 result = PAM_Authenticate(Client);
963 if (write(pipefd[1], &result, sizeof(result)) != sizeof(result))
964 Log_Subprocess(LOG_ERR,
965 "Failed to pipe result to parent!");
966 Log_Exit_Subprocess("Auth");
970 /* Check global server password ... */
971 if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
973 Reject_Client(Client);
976 return Hello_User_PostAuth(Client);
984 * Read result of the authenticatior sub-process from pipe
986 * @param r_fd File descriptor of the pipe.
987 * @param events (ignored IO specification)
990 cb_Read_Auth_Result(int r_fd, UNUSED short events)
998 LogDebug("Auth: Got callback on fd %d, events %d", r_fd, events);
999 conn = Conn_GetFromProc(r_fd);
1001 /* Ops, none found? Probably the connection has already
1002 * been closed!? We'll ignore that ... */
1004 LogDebug("Auth: Got callback for unknown connection!?");
1007 proc = Conn_GetProcStat(conn);
1008 client = Conn_GetClient(conn);
1010 /* Read result from pipe */
1011 len = Proc_Read(proc, &result, sizeof(result));
1016 if (len != sizeof(result)) {
1017 Log(LOG_CRIT, "Auth: Got malformed result!");
1018 Reject_Client(client);
1022 if (result == true) {
1023 Client_SetUser(client, Client_OrigUser(client), true);
1024 (void)Hello_User_PostAuth(client);
1026 Reject_Client(client);
1033 * Reject a client because of wrong password.
1035 * This function is called either when the global server password or a password
1036 * checked using PAM has been wrong.
1038 * @param Client The client to reject.
1041 Reject_Client(CLIENT *Client)
1044 "User \"%s\" rejected (connection %d): Access denied!",
1045 Client_Mask(Client), Client_Conn(Client));
1046 Conn_Close(Client_Conn(Client), NULL,
1047 "Access denied! Bad password?", true);
1052 * Finish client registration.
1054 * Introduce the new client to the network and send all "hello messages"
1055 * to it after authentication has been succeeded.
1057 * @param Client The client logging in.
1058 * @returns CONNECTED or DISCONNECTED.
1061 Hello_User_PostAuth(CLIENT *Client)
1063 Introduce_Client(NULL, Client, CLIENT_USER);
1065 if (!IRC_WriteStrClient
1066 (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
1068 if (!IRC_WriteStrClient
1069 (Client, RPL_YOURHOST_MSG, Client_ID(Client),
1070 Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
1071 TARGET_VENDOR, TARGET_OS))
1073 if (!IRC_WriteStrClient
1074 (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
1076 if (!IRC_WriteStrClient
1077 (Client, RPL_MYINFO_MSG, Client_ID(Client),
1078 Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
1082 /* Features supported by this server (005 numeric, ISUPPORT),
1083 * see <http://www.irc.org/tech_docs/005.html> for details. */
1084 if (!IRC_Send_ISUPPORT(Client))
1085 return DISCONNECTED;
1087 if (!IRC_Send_LUSERS(Client))
1088 return DISCONNECTED;
1089 if (!IRC_Show_MOTD(Client))
1090 return DISCONNECTED;
1092 /* Suspend the client for a second ... */
1093 IRC_SetPenalty(Client, 1);
1100 * Kill all users with a specific nick name in the network.
1102 * @param Nick Nick name.
1103 * @param Reason Reason for the KILL.
1106 Kill_Nick(char *Nick, char *Reason)
1110 assert (Nick != NULL);
1111 assert (Reason != NULL);
1118 Log(LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s",
1121 IRC_KILL(Client_ThisServer(), &r);
1126 * Introduce a new user or service client in the network.
1128 * @param From Remote server introducing the client or NULL (local).
1129 * @param Client New client.
1130 * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1133 Introduce_Client(CLIENT *From, CLIENT *Client, int Type)
1135 /* Set client type (user or service) */
1136 Client_SetType(Client, Type);
1139 if (Conf_IsService(Conf_GetServer(Client_Conn(From)),
1141 Client_SetType(Client, CLIENT_SERVICE);
1142 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1143 Client_TypeText(Client), Client_Mask(Client),
1144 Client_Modes(Client), Client_ID(From),
1145 Client_ID(Client_Introducer(Client)),
1146 Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1148 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1149 Client_TypeText(Client), Client_Mask(Client),
1150 Client_Conn(Client));
1151 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1152 Client_ID(Client), Client_User(Client),
1153 Client_Hostname(Client),
1154 Conn_IPA(Client_Conn(Client)),
1155 Client_TypeText(Client));
1158 /* Inform other servers */
1159 IRC_WriteStrServersPrefixFlag_CB(From,
1160 From != NULL ? From : Client_ThisServer(),
1161 '\0', cb_introduceClient, (void *)Client);
1162 } /* Introduce_Client */
1166 * Introduce a new user or service client to a remote server.
1168 * This function differentiates between RFC1459 and RFC2813 server links and
1169 * generates the appropriate commands to register the new user or service.
1171 * @param To The remote server to inform.
1172 * @param Prefix Prefix for the generated commands.
1173 * @param data CLIENT structure of the new client.
1176 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1178 CLIENT *c = (CLIENT *)data;
1180 char *modes, *user, *host;
1182 modes = Client_Modes(c);
1183 user = Client_User(c) ? Client_User(c) : "-";
1184 host = Client_Hostname(c) ? Client_Hostname(c) : "-";
1186 conn = Client_Conn(To);
1187 if (Conn_Options(conn) & CONN_RFC1459) {
1188 /* RFC 1459 mode: separate NICK and USER commands */
1189 Conn_WriteStr(conn, "NICK %s :%d", Client_ID(c),
1190 Client_Hops(c) + 1);
1191 Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1192 Client_ID(c), user, host,
1193 Client_ID(Client_Introducer(c)), Client_Info(c));
1195 Conn_WriteStr(conn, ":%s MODE %s +%s",
1196 Client_ID(c), Client_ID(c), modes);
1198 /* RFC 2813 mode: one combined NICK or SERVICE command */
1199 if (Client_Type(c) == CLIENT_SERVICE
1200 && strchr(Client_Flags(To), 'S'))
1201 IRC_WriteStrClientPrefix(To, Prefix,
1202 "SERVICE %s %d * +%s %d :%s",
1204 Client_MyToken(Client_Introducer(c)),
1205 Client_Modes(c), Client_Hops(c) + 1,
1208 IRC_WriteStrClientPrefix(To, Prefix,
1209 "NICK %s %d %s %s %d +%s :%s",
1210 Client_ID(c), Client_Hops(c) + 1,
1212 Client_MyToken(Client_Introducer(c)),
1213 modes, Client_Info(c));
1215 } /* cb_introduceClient */