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 );
657 assert( Req != NULL );
659 /* Wrong number of arguments? */
661 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
664 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
666 if ( Client_Type( Client ) == CLIENT_SERVER )
669 target = Client_Search( Req->prefix );
672 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
676 Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
682 if (Req->argc == 1 && quitmsg[0] != '\"') {
683 /* " " to avoid confusion */
684 strlcpy(quitmsg, "\"", sizeof quitmsg);
685 strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
686 strlcat(quitmsg, "\"", sizeof quitmsg );
689 /* User, Service, or not yet registered */
690 Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
700 * Handler for HTTP command, e.g. GET and POST
702 * We handle these commands here to avoid the quite long timeout when
703 * some user tries to access this IRC daemon using an web browser ...
705 * @param Client The client from which this command has been received.
706 * @param Req Request structure with prefix and all parameters.
707 * @returns CONNECTED or DISCONNECTED.
710 IRC_QUIT_HTTP( CLIENT *Client, REQUEST *Req )
713 Req->argv[0] = "Oops, HTTP request received? This is IRC!";
714 return IRC_QUIT(Client, Req);
715 } /* IRC_QUIT_HTTP */
721 * Handler for the IRC "PING" command.
723 * See RFC 2812, 3.7.2 "Ping message".
725 * @param Client The client from which this command has been received.
726 * @param Req Request structure with prefix and all parameters.
727 * @returns CONNECTED or DISCONNECTED.
730 IRC_PING(CLIENT *Client, REQUEST *Req)
732 CLIENT *target, *from;
734 assert(Client != NULL);
738 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
741 /* Don't ignore additional arguments when in "strict" mode */
743 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
744 Client_ID(Client), Req->command);
748 /* A target has been specified ... */
749 target = Client_Search(Req->argv[1]);
751 if (!target || Client_Type(target) != CLIENT_SERVER)
752 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
753 Client_ID(Client), Req->argv[1]);
755 if (target != Client_ThisServer()) {
756 /* Ok, we have to forward the PING */
757 if (Client_Type(Client) == CLIENT_SERVER)
758 from = Client_Search(Req->prefix);
762 return IRC_WriteStrClient(Client,
763 ERR_NOSUCHSERVER_MSG,
764 Client_ID(Client), Req->prefix);
766 return IRC_WriteStrClientPrefix(target, from,
767 "PING %s :%s", Req->argv[0],
772 if (Client_Type(Client) == CLIENT_SERVER) {
774 from = Client_Search(Req->prefix);
778 from = Client_ThisServer();
780 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
781 Client_ID(Client), Req->prefix);
783 Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
784 Client_Conn(Client));
787 return IRC_WriteStrClient(Client, "PONG %s :%s",
788 Client_ID(from), Client_ID(Client));
790 /* Some clients depend on the argument being returned in the PONG
791 * reply (not mentioned in any RFC, though) */
792 return IRC_WriteStrClient(Client, "PONG %s :%s",
793 Client_ID(from), Req->argv[0]);
799 * Handler for the IRC "PONG" command.
801 * See RFC 2812, 3.7.3 "Pong message".
803 * @param Client The client from which this command has been received.
804 * @param Req Request structure with prefix and all parameters.
805 * @returns CONNECTED or DISCONNECTED.
808 IRC_PONG(CLIENT *Client, REQUEST *Req)
810 CLIENT *target, *from;
817 assert(Client != NULL);
820 /* Wrong number of arguments? */
822 if (Client_Type(Client) == CLIENT_USER)
823 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
829 if (Client_Type(Client) == CLIENT_USER)
830 return IRC_WriteStrClient(Client,
831 ERR_NEEDMOREPARAMS_MSG,
839 if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
840 target = Client_Search(Req->argv[0]);
842 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
843 Client_ID(Client), Req->argv[0]);
845 from = Client_Search(Req->prefix);
847 if (target != Client_ThisServer() && target != from) {
848 /* Ok, we have to forward the message. */
850 return IRC_WriteStrClient(Client,
851 ERR_NOSUCHSERVER_MSG,
852 Client_ID(Client), Req->prefix);
854 if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
858 return IRC_WriteStrClientPrefix(target, from,
859 "PONG %s :%s", s, Req->argv[1]);
863 /* The connection timestamp has already been updated when the data has
864 * been read from so socket, so we don't need to update it here. */
866 conn = Client_Conn(Client);
869 /* Check authentication PING-PONG ... */
870 auth_ping = Conn_GetAuthPing(conn);
872 LogDebug("AUTH PONG: waiting for token \"%ld\", got \"%s\" ...",
873 auth_ping, Req->argv[0]);
874 if (auth_ping == atoi(Req->argv[0])) {
875 Conn_SetAuthPing(conn, 0);
876 if (Client_Type(Client) == CLIENT_WAITAUTHPING)
879 if (!IRC_WriteStrClient(Client,
880 "To connect, type /QUOTE PONG %ld",
889 "Connection %d: received PONG. Lag: %ld seconds.", conn,
890 time(NULL) - Conn_LastPing(Client_Conn(Client)));
893 "Connection %d: received PONG.", conn);
900 * Initiate client registration.
902 * This function is called after the daemon received the required NICK and
903 * USER commands of a new client. If the daemon is compiled with support for
904 * PAM, the authentication sub-processs is forked; otherwise the global server
905 * password is checked.
907 * @param Client The client logging in.
908 * @returns CONNECTED or DISCONNECTED.
911 Hello_User(CLIENT * Client)
914 int pipefd[2], result;
919 assert(Client != NULL);
920 conn = Client_Conn(Client);
924 /* Did we receive the "auth PONG" already? */
925 if (Conn_GetAuthPing(conn)) {
926 Client_SetType(Client, CLIENT_WAITAUTHPING);
927 LogDebug("Connection %d: Waiting for AUTH PONG ...", conn);
935 /* Don't do any PAM authentication at all, instead emulate
936 * the beahiour of the daemon compiled without PAM support:
937 * because there can't be any "server password", all
938 * passwords supplied are classified as "wrong". */
939 if(Client_Password(Client)[0] == '\0')
940 return Hello_User_PostAuth(Client);
941 Reject_Client(Client);
945 /* Fork child process for PAM authentication; and make sure that the
946 * process timeout is set higher than the login timeout! */
947 pid = Proc_Fork(Conn_GetProcStat(conn), pipefd,
948 cb_Read_Auth_Result, Conf_PongTimeout + 1);
950 LogDebug("Authenticator for connection %d created (PID %d).",
955 Log_Init_Subprocess("Auth");
956 result = PAM_Authenticate(Client);
957 if (write(pipefd[1], &result, sizeof(result)) != sizeof(result))
958 Log_Subprocess(LOG_ERR,
959 "Failed to pipe result to parent!");
960 Log_Exit_Subprocess("Auth");
964 /* Check global server password ... */
965 if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
967 Reject_Client(Client);
970 return Hello_User_PostAuth(Client);
978 * Read result of the authenticatior sub-process from pipe
980 * @param r_fd File descriptor of the pipe.
981 * @param events (ignored IO specification)
984 cb_Read_Auth_Result(int r_fd, UNUSED short events)
992 LogDebug("Auth: Got callback on fd %d, events %d", r_fd, events);
993 conn = Conn_GetFromProc(r_fd);
995 /* Ops, none found? Probably the connection has already
996 * been closed!? We'll ignore that ... */
998 LogDebug("Auth: Got callback for unknown connection!?");
1001 proc = Conn_GetProcStat(conn);
1002 client = Conn_GetClient(conn);
1004 /* Read result from pipe */
1005 len = Proc_Read(proc, &result, sizeof(result));
1009 if (len != sizeof(result)) {
1010 Log(LOG_CRIT, "Auth: Got malformed result!");
1011 Reject_Client(client);
1015 if (result == true) {
1016 Client_SetUser(client, Client_OrigUser(client), true);
1017 (void)Hello_User_PostAuth(client);
1019 Reject_Client(client);
1026 * Reject a client because of wrong password.
1028 * This function is called either when the global server password or a password
1029 * checked using PAM has been wrong.
1031 * @param Client The client to reject.
1034 Reject_Client(CLIENT *Client)
1037 "User \"%s\" rejected (connection %d): Access denied!",
1038 Client_Mask(Client), Client_Conn(Client));
1039 Conn_Close(Client_Conn(Client), NULL,
1040 "Access denied! Bad password?", true);
1045 * Finish client registration.
1047 * Introduce the new client to the network and send all "hello messages"
1048 * to it after authentication has been succeeded.
1050 * @param Client The client logging in.
1051 * @returns CONNECTED or DISCONNECTED.
1054 Hello_User_PostAuth(CLIENT *Client)
1056 Introduce_Client(NULL, Client, CLIENT_USER);
1058 if (!IRC_WriteStrClient
1059 (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
1061 if (!IRC_WriteStrClient
1062 (Client, RPL_YOURHOST_MSG, Client_ID(Client),
1063 Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
1064 TARGET_VENDOR, TARGET_OS))
1066 if (!IRC_WriteStrClient
1067 (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
1069 if (!IRC_WriteStrClient
1070 (Client, RPL_MYINFO_MSG, Client_ID(Client),
1071 Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
1075 /* Features supported by this server (005 numeric, ISUPPORT),
1076 * see <http://www.irc.org/tech_docs/005.html> for details. */
1077 if (!IRC_Send_ISUPPORT(Client))
1078 return DISCONNECTED;
1080 if (!IRC_Send_LUSERS(Client))
1081 return DISCONNECTED;
1082 if (!IRC_Show_MOTD(Client))
1083 return DISCONNECTED;
1085 /* Suspend the client for a second ... */
1086 IRC_SetPenalty(Client, 1);
1093 * Kill all users with a specific nick name in the network.
1095 * @param Nick Nick name.
1096 * @param Reason Reason for the KILL.
1099 Kill_Nick( char *Nick, char *Reason )
1103 assert( Nick != NULL );
1104 assert( Reason != NULL );
1106 r.prefix = (char *)Client_ThisServer( );
1111 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
1112 IRC_KILL( Client_ThisServer( ), &r );
1117 * Introduce a new user or service client in the network.
1119 * @param From Remote server introducing the client or NULL (local).
1120 * @param Client New client.
1121 * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1124 Introduce_Client(CLIENT *From, CLIENT *Client, int Type)
1126 /* Set client type (user or service) */
1127 Client_SetType(Client, Type);
1130 if (Conf_IsService(Conf_GetServer(Client_Conn(From)),
1132 Client_SetType(Client, CLIENT_SERVICE);
1133 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1134 Client_TypeText(Client), Client_Mask(Client),
1135 Client_Modes(Client), Client_ID(From),
1136 Client_ID(Client_Introducer(Client)),
1137 Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1139 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1140 Client_TypeText(Client), Client_Mask(Client),
1141 Client_Conn(Client));
1142 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1143 Client_ID(Client), Client_User(Client),
1144 Client_Hostname(Client),
1145 Conn_IPA(Client_Conn(Client)),
1146 Client_TypeText(Client));
1149 /* Inform other servers */
1150 IRC_WriteStrServersPrefixFlag_CB(From,
1151 From != NULL ? From : Client_ThisServer(),
1152 '\0', cb_introduceClient, (void *)Client);
1153 } /* Introduce_Client */
1157 * Introduce a new user or service client to a remote server.
1159 * This function differentiates between RFC1459 and RFC2813 server links and
1160 * generates the appropriate commands to register the new user or service.
1162 * @param To The remote server to inform.
1163 * @param Prefix Prefix for the generated commands.
1164 * @param data CLIENT structure of the new client.
1167 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1169 CLIENT *c = (CLIENT *)data;
1171 char *modes, *user, *host;
1173 modes = Client_Modes(c);
1174 user = Client_User(c) ? Client_User(c) : "-";
1175 host = Client_Hostname(c) ? Client_Hostname(c) : "-";
1177 conn = Client_Conn(To);
1178 if (Conn_Options(conn) & CONN_RFC1459) {
1179 /* RFC 1459 mode: separate NICK and USER commands */
1180 Conn_WriteStr(conn, "NICK %s :%d", Client_ID(c),
1181 Client_Hops(c) + 1);
1182 Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1183 Client_ID(c), user, host,
1184 Client_ID(Client_Introducer(c)), Client_Info(c));
1186 Conn_WriteStr(conn, ":%s MODE %s +%s",
1187 Client_ID(c), Client_ID(c), modes);
1189 /* RFC 2813 mode: one combined NICK or SERVICE command */
1190 if (Client_Type(c) == CLIENT_SERVICE
1191 && strchr(Client_Flags(To), 'S'))
1192 IRC_WriteStrClientPrefix(To, Prefix,
1193 "SERVICE %s %d * +%s %d :%s",
1195 Client_MyToken(Client_Introducer(c)),
1196 Client_Modes(c), Client_Hops(c) + 1,
1199 IRC_WriteStrClientPrefix(To, Prefix,
1200 "NICK %s %d %s %s %d +%s :%s",
1201 Client_ID(c), Client_Hops(c) + 1,
1203 Client_MyToken(Client_Introducer(c)),
1204 modes, Client_Info(c));
1206 } /* cb_introduceClient */