2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
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"
40 #include "irc-write.h"
43 #include "irc-login.h"
46 static bool Hello_User PARAMS(( CLIENT *Client ));
47 static bool Hello_User_PostAuth PARAMS(( CLIENT *Client ));
48 static void Kill_Nick PARAMS(( char *Nick, char *Reason ));
49 static void Introduce_Client PARAMS((CLIENT *To, CLIENT *Client, int Type));
50 static void Reject_Client PARAMS((CLIENT *Client, const char *InternalReason));
52 static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
56 static void cb_Read_Auth_Result PARAMS((int r_fd, UNUSED short events));
60 * Handler for the IRC "PASS" command.
62 * See RFC 2813 section 4.1.1, and RFC 2812 section 3.1.1.
64 * @param Client The client from which this command has been received.
65 * @param Req Request structure with prefix and all parameters.
66 * @returns CONNECTED or DISCONNECTED.
69 IRC_PASS( CLIENT *Client, REQUEST *Req )
71 char *type, *orig_flags;
72 int protohigh, protolow;
74 assert( Client != NULL );
75 assert( Req != NULL );
77 /* Return an error if this is not a local client */
78 if (Client_Conn(Client) <= NONE)
79 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
80 Client_ID(Client), Req->command);
82 if (Client_Type(Client) == CLIENT_UNKNOWN && Req->argc == 1) {
83 /* Not yet registered "unknown" connection, PASS with one
84 * argument: either a regular client, service, or server
85 * using the old RFC 1459 section 4.1.1 syntax. */
86 LogDebug("Connection %d: got PASS command (RFC 1459) ...",
88 } else if ((Client_Type(Client) == CLIENT_UNKNOWN ||
89 Client_Type(Client) == CLIENT_UNKNOWNSERVER) &&
90 (Req->argc == 3 || Req->argc == 4)) {
91 /* Not yet registered "unknown" connection or outgoing server
92 * link, PASS with three or four argument: server using the
93 * RFC 2813 section 4.1.1 syntax. */
94 LogDebug("Connection %d: got PASS command (RFC 2813, new server link) ...",
96 } else if (Client_Type(Client) == CLIENT_UNKNOWN ||
97 Client_Type(Client) == CLIENT_UNKNOWNSERVER) {
98 /* Unregistered connection, but wrong number of arguments: */
99 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
100 Client_ID(Client), Req->command);
102 /* Registered connection, PASS command is not allowed! */
103 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
107 Client_SetPassword(Client, Req->argv[0]);
109 /* Protocol version */
110 if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
113 c2 = Req->argv[1][2];
114 c4 = Req->argv[1][4];
116 Req->argv[1][4] = '\0';
117 protolow = atoi(&Req->argv[1][2]);
118 Req->argv[1][2] = '\0';
119 protohigh = atoi(Req->argv[1]);
121 Req->argv[1][2] = c2;
122 Req->argv[1][4] = c4;
124 Client_SetType(Client, CLIENT_GOTPASS_2813);
126 protohigh = protolow = 0;
127 Client_SetType(Client, CLIENT_GOTPASS);
130 /* Protocol type, see doc/Protocol.txt */
131 if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
132 type = &Req->argv[1][4];
136 /* Protocol flags/options */
138 orig_flags = Req->argv[3];
142 /* Implementation, version and IRC+ flags */
143 if (Req->argc >= 3) {
144 char *impl, *ptr, *serverver, *flags;
147 ptr = strchr(impl, '|');
151 if (type && strcmp(type, PROTOIRCPLUS) == 0) {
152 /* The peer seems to be a server which supports the
153 * IRC+ protocol (see doc/Protocol.txt). */
154 serverver = ptr ? ptr + 1 : "?";
155 flags = strchr(ptr ? serverver : impl, ':');
162 "Peer on conenction %d announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
163 Client_Conn(Client), impl, serverver,
164 protohigh, protolow, flags);
166 /* The peer seems to be a server supporting the
167 * "original" IRC protocol (RFC 2813). */
168 if (strchr(orig_flags, 'Z'))
173 "Peer on connection %d announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
174 Client_Conn(Client), impl,
175 protohigh, protolow, flags);
177 Client_SetFlags(Client, flags);
185 * Handler for the IRC "NICK" command.
187 * See RFC 2812, 3.1.2 "Nick message", and RFC 2813, 4.1.3 "Nick".
189 * This function implements the IRC command "NICK" which is used to register
190 * with the server, to change already registered nicknames and to introduce
191 * new users which are connected to other servers.
193 * @param Client The client from which this command has been received.
194 * @param Req Request structure with prefix and all parameters.
195 * @returns CONNECTED or DISCONNECTED.
198 IRC_NICK( CLIENT *Client, REQUEST *Req )
200 CLIENT *intr_c, *target, *c;
201 char *nick, *user, *hostname, *modes, *info;
204 assert( Client != NULL );
205 assert( Req != NULL );
207 /* Some IRC clients, for example BitchX, send the NICK and USER
208 * commands in the wrong order ... */
209 if(Client_Type(Client) == CLIENT_UNKNOWN
210 || Client_Type(Client) == CLIENT_GOTPASS
211 || Client_Type(Client) == CLIENT_GOTNICK
213 || Client_Type(Client) == CLIENT_GOTUSER
215 || Client_Type(Client) == CLIENT_USER
216 || Client_Type(Client) == CLIENT_SERVICE
217 || (Client_Type(Client) == CLIENT_SERVER && Req->argc == 1))
219 /* User registration or change of nickname */
221 /* Wrong number of arguments? */
223 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
227 /* Search "target" client */
228 if( Client_Type( Client ) == CLIENT_SERVER )
230 target = Client_Search( Req->prefix );
232 return IRC_WriteStrClient( Client,
239 /* Is this a restricted client? */
240 if( Client_HasMode( Client, 'r' ))
241 return IRC_WriteStrClient( Client,
243 Client_ID( Client ));
249 /* If the clients tries to change to its own nickname we won't
250 * do anything. This is how the original ircd behaves and some
251 * clients (for example Snak) expect it to be like this.
252 * But I doubt that this is "really the right thing" ... */
253 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 )
257 /* Check that the new nickname is available. Special case:
258 * the client only changes from/to upper to lower case. */
259 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
261 if( ! Client_CheckNick( target, Req->argv[0] ))
265 if (Client_Type(target) != CLIENT_USER &&
266 Client_Type(target) != CLIENT_SERVICE &&
267 Client_Type(target) != CLIENT_SERVER) {
269 LogDebug("Connection %d: got valid NICK command ...",
270 Client_Conn( Client ));
272 /* Register new nickname of this client */
273 Client_SetID( target, Req->argv[0] );
277 Conn_SetAuthPing(Client_Conn(Client), rand());
278 IRC_WriteStrClient(Client, "PING :%ld",
279 Conn_GetAuthPing(Client_Conn(Client)));
280 LogDebug("Connection %d: sent AUTH PING %ld ...",
282 Conn_GetAuthPing(Client_Conn(Client)));
286 /* If we received a valid USER command already then
287 * register the new client! */
288 if( Client_Type( Client ) == CLIENT_GOTUSER )
289 return Hello_User( Client );
291 Client_SetType( Client, CLIENT_GOTNICK );
293 /* Nickname change */
294 if (Client_Conn(target) > NONE) {
297 "%s \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
298 Client_TypeText(target), Client_Mask(target),
299 Client_Conn(target), Client_ID(target),
301 Conn_UpdateIdle(Client_Conn(target));
304 LogDebug("%s \"%s\" changed nick: \"%s\" -> \"%s\".",
305 Client_TypeText(target),
306 Client_Mask(target), Client_ID(target),
310 /* Inform all users and servers (which have to know)
311 * of this nickname change */
312 if( Client_Type( Client ) == CLIENT_USER )
313 IRC_WriteStrClientPrefix( Client, Client,
316 IRC_WriteStrServersPrefix( Client, target,
317 "NICK :%s", Req->argv[0] );
318 IRC_WriteStrRelatedPrefix( target, target, false,
319 "NICK :%s", Req->argv[0] );
321 /* Register old nickname for WHOWAS queries */
322 Client_RegisterWhowas( target );
324 /* Save new nickname */
325 Client_SetID( target, Req->argv[0] );
327 IRC_SetPenalty( target, 2 );
331 } else if(Client_Type(Client) == CLIENT_SERVER ||
332 Client_Type(Client) == CLIENT_SERVICE) {
333 /* Server or service introduces new client */
335 /* Bad number of parameters? */
336 if (Req->argc != 2 && Req->argc != 7)
337 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
338 Client_ID(Client), Req->command);
340 if (Req->argc >= 7) {
341 /* RFC 2813 compatible syntax */
343 hops = atoi(Req->argv[1]);
345 hostname = Req->argv[3];
346 token = atoi(Req->argv[4]);
347 modes = Req->argv[5] + 1;
350 /* RFC 1459 compatible syntax */
354 hostname = Client_ID(Client);
355 token = atoi(Req->argv[1]);
360 c = Client_Search(nick);
363 * the new nick is already present on this server:
364 * the new and the old one have to be disconnected now.
366 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
367 Kill_Nick( Req->argv[0], "Nick collision" );
371 /* Find the Server this client is connected to */
372 intr_c = Client_GetFromToken(Client, token);
375 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
376 Kill_Nick( Req->argv[0], "Unknown server" );
380 c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
381 token, modes, info, true);
384 /* out of memory, need to disconnect client to keep network state consistent */
385 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
386 Kill_Nick( Req->argv[0], "Server error" );
390 /* RFC 2813: client is now fully registered, inform all the
391 * other servers about the new user.
392 * RFC 1459: announce the new client only after receiving the
393 * USER command, first we need more information! */
395 LogDebug("Client \"%s\" is being registered (RFC 1459) ...",
397 Client_SetType(c, CLIENT_GOTNICK);
399 Introduce_Client(Client, c, CLIENT_USER);
403 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
408 * Handler for the IRC "USER" command.
410 * See RFC 2812, 3.1.3 "User message".
412 * @param Client The client from which this command has been received.
413 * @param Req Request structure with prefix and all parameters.
414 * @returns CONNECTED or DISCONNECTED.
417 IRC_USER(CLIENT * Client, REQUEST * Req)
424 assert(Client != NULL);
427 if (Client_Type(Client) == CLIENT_GOTNICK ||
429 Client_Type(Client) == CLIENT_UNKNOWN ||
431 Client_Type(Client) == CLIENT_GOTPASS)
435 return IRC_WriteStrClient(Client,
436 ERR_NEEDMOREPARAMS_MSG,
442 ptr = Client_User(Client);
443 if (!ptr || !*ptr || *ptr == '~')
444 Client_SetUser(Client, Req->argv[0], false);
446 Client_SetUser(Client, Req->argv[0], false);
448 Client_SetOrigUser(Client, Req->argv[0]);
450 /* "Real name" or user info text: Don't set it to the empty
451 * string, the original ircd can't deal with such "real names"
452 * (e. g. "USER user * * :") ... */
454 Client_SetInfo(Client, Req->argv[3]);
456 Client_SetInfo(Client, "-");
458 LogDebug("Connection %d: got valid USER command ...",
459 Client_Conn(Client));
460 if (Client_Type(Client) == CLIENT_GOTNICK)
461 return Hello_User(Client);
463 Client_SetType(Client, CLIENT_GOTUSER);
466 } else if (Client_Type(Client) == CLIENT_SERVER ||
467 Client_Type(Client) == CLIENT_SERVICE) {
468 /* Server/service updating an user */
470 return IRC_WriteStrClient(Client,
471 ERR_NEEDMOREPARAMS_MSG,
474 c = Client_Search(Req->prefix);
476 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
480 Client_SetUser(c, Req->argv[0], true);
481 Client_SetOrigUser(c, Req->argv[0]);
482 Client_SetHostname(c, Req->argv[1]);
483 Client_SetInfo(c, Req->argv[3]);
485 LogDebug("Connection %d: got valid USER command for \"%s\".",
486 Client_Conn(Client), Client_Mask(c));
488 /* RFC 1459 style user registration?
489 * Introduce client to network: */
490 if (Client_Type(c) == CLIENT_GOTNICK)
491 Introduce_Client(Client, c, CLIENT_USER);
494 } else if (Client_Type(Client) == CLIENT_USER) {
495 /* Already registered connection */
496 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
499 /* Unexpected/invalid connection state? */
500 return IRC_WriteStrClient(Client, ERR_NOTREGISTERED_MSG,
507 * Handler for the IRC "SERVICE" command.
509 * This function implements IRC Services registration using the SERVICE command
510 * defined in RFC 2812 3.1.6 and RFC 2813 4.1.4.
512 * At the moment ngIRCd doesn't support directly linked services, so this
513 * function returns ERR_ERRONEUSNICKNAME when the SERVICE command has not been
514 * received from a peer server.
516 * @param Client The client from which this command has been received.
517 * @param Req Request structure with prefix and all parameters.
518 * @returns CONNECTED or DISCONNECTED..
521 IRC_SERVICE(CLIENT *Client, REQUEST *Req)
524 char *nick, *user, *host, *info, *modes, *ptr;
527 assert(Client != NULL);
530 if (Client_Type(Client) != CLIENT_GOTPASS &&
531 Client_Type(Client) != CLIENT_SERVER)
532 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
536 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
537 Client_ID(Client), Req->command);
539 if (Client_Type(Client) != CLIENT_SERVER)
540 return IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
541 Client_ID(Client), Req->argv[0]);
543 /* Bad number of parameters? */
545 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
546 Client_ID(Client), Req->command);
549 user = NULL; host = NULL;
550 token = atoi(Req->argv[1]);
551 hops = atoi(Req->argv[4]);
554 /* Validate service name ("nick name") */
555 c = Client_Search(nick);
557 /* Nick name collission: disconnect (KILL) both clients! */
558 Log(LOG_ERR, "Server %s introduces already registered service \"%s\"!",
559 Client_ID(Client), nick);
560 Kill_Nick(nick, "Nick collision");
564 /* Get the server to which the service is connected */
565 intr_c = Client_GetFromToken(Client, token);
567 Log(LOG_ERR, "Server %s introduces service \"%s\" on unknown server!?",
568 Client_ID(Client), nick);
569 Kill_Nick(nick, "Unknown server");
573 /* Get user and host name */
574 ptr = strchr(nick, '@');
580 host = Client_Hostname(intr_c);
581 ptr = strchr(nick, '!');
589 /* According to RFC 2812/2813 parameter 4 <type> "is currently reserved
590 * for future usage"; but we use it to transfer the modes and check
591 * that the first character is a '+' sign and ignore it otherwise. */
592 modes = (Req->argv[3][0] == '+') ? ++Req->argv[3] : "";
594 c = Client_NewRemoteUser(intr_c, nick, hops, user, host,
595 token, modes, info, true);
597 /* Couldn't create client structure, so KILL the service to
598 * keep network status consistent ... */
599 Log(LOG_ALERT, "Can't create client structure! (on connection %d)",
600 Client_Conn(Client));
601 Kill_Nick(nick, "Server error");
605 Introduce_Client(Client, c, CLIENT_SERVICE);
611 * Handler for the IRC "WEBIRC" command.
613 * See doc/Protocol.txt, section II.4:
614 * "Update webchat/proxy client information".
616 * @param Client The client from which this command has been received.
617 * @param Req Request structure with prefix and all parameters.
618 * @returns CONNECTED or DISCONNECTED.
621 IRC_WEBIRC(CLIENT *Client, REQUEST *Req)
623 /* Exactly 4 parameters are requited */
625 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
626 Client_ID(Client), Req->command);
628 if (!Conf_WebircPwd[0] || strcmp(Req->argv[0], Conf_WebircPwd) != 0)
629 return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
632 LogDebug("Connection %d: got valid WEBIRC command: user=%s, host=%s, ip=%s",
633 Client_Conn(Client), Req->argv[1], Req->argv[2], Req->argv[3]);
635 Client_SetUser(Client, Req->argv[1], true);
636 Client_SetOrigUser(Client, Req->argv[1]);
637 Client_SetHostname(Client, Req->argv[2]);
643 * Handler for the IRC "QUIT" command.
645 * See RFC 2812, 3.1.7 "Quit", and RFC 2813, 4.1.5 "Quit".
647 * @param Client The client from which this command has been received.
648 * @param Req Request structure with prefix and all parameters.
649 * @returns CONNECTED or DISCONNECTED.
652 IRC_QUIT( CLIENT *Client, REQUEST *Req )
655 char quitmsg[LINE_LEN];
657 assert(Client != NULL);
660 /* Wrong number of arguments? */
662 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
663 Client_ID(Client), Req->command);
666 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
668 if (Client_Type(Client) == CLIENT_SERVER) {
670 target = Client_Search(Req->prefix);
673 "Got QUIT from %s for unknown client!?",
678 if (target != Client) {
679 Client_Destroy(target, "Got QUIT command.",
680 Req->argc == 1 ? quitmsg : NULL, true);
683 Conn_Close(Client_Conn(Client), "Got QUIT command.",
684 Req->argc == 1 ? quitmsg : NULL, true);
688 if (Req->argc == 1 && quitmsg[0] != '\"') {
689 /* " " to avoid confusion */
690 strlcpy(quitmsg, "\"", sizeof quitmsg);
691 strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
692 strlcat(quitmsg, "\"", sizeof quitmsg );
695 /* User, Service, or not yet registered */
696 Conn_Close(Client_Conn(Client), "Got QUIT command.",
697 Req->argc == 1 ? quitmsg : NULL, true);
707 * Handler for HTTP command, e.g. GET and POST
709 * We handle these commands here to avoid the quite long timeout when
710 * some user tries to access this IRC daemon using an web browser ...
712 * @param Client The client from which this command has been received.
713 * @param Req Request structure with prefix and all parameters.
714 * @returns CONNECTED or DISCONNECTED.
717 IRC_QUIT_HTTP( CLIENT *Client, REQUEST *Req )
720 Req->argv[0] = "Oops, HTTP request received? This is IRC!";
721 return IRC_QUIT(Client, Req);
722 } /* IRC_QUIT_HTTP */
728 * Handler for the IRC "PING" command.
730 * See RFC 2812, 3.7.2 "Ping message".
732 * @param Client The client from which this command has been received.
733 * @param Req Request structure with prefix and all parameters.
734 * @returns CONNECTED or DISCONNECTED.
737 IRC_PING(CLIENT *Client, REQUEST *Req)
739 CLIENT *target, *from;
741 assert(Client != NULL);
745 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
748 /* Don't ignore additional arguments when in "strict" mode */
750 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
751 Client_ID(Client), Req->command);
755 /* A target has been specified ... */
756 target = Client_Search(Req->argv[1]);
758 if (!target || Client_Type(target) != CLIENT_SERVER)
759 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
760 Client_ID(Client), Req->argv[1]);
762 if (target != Client_ThisServer()) {
763 /* Ok, we have to forward the PING */
764 if (Client_Type(Client) == CLIENT_SERVER)
765 from = Client_Search(Req->prefix);
769 return IRC_WriteStrClient(Client,
770 ERR_NOSUCHSERVER_MSG,
771 Client_ID(Client), Req->prefix);
773 return IRC_WriteStrClientPrefix(target, from,
774 "PING %s :%s", Req->argv[0],
779 if (Client_Type(Client) == CLIENT_SERVER) {
781 from = Client_Search(Req->prefix);
785 from = Client_ThisServer();
787 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
788 Client_ID(Client), Req->prefix);
790 Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
791 Client_Conn(Client));
794 return IRC_WriteStrClient(Client, "PONG %s :%s",
795 Client_ID(from), Client_ID(Client));
797 /* Some clients depend on the argument being returned in the PONG
798 * reply (not mentioned in any RFC, though) */
799 return IRC_WriteStrClient(Client, "PONG %s :%s",
800 Client_ID(from), Req->argv[0]);
806 * Handler for the IRC "PONG" command.
808 * See RFC 2812, 3.7.3 "Pong message".
810 * @param Client The client from which this command has been received.
811 * @param Req Request structure with prefix and all parameters.
812 * @returns CONNECTED or DISCONNECTED.
815 IRC_PONG(CLIENT *Client, REQUEST *Req)
817 CLIENT *target, *from;
824 assert(Client != NULL);
827 /* Wrong number of arguments? */
829 if (Client_Type(Client) == CLIENT_USER)
830 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
836 if (Client_Type(Client) == CLIENT_USER)
837 return IRC_WriteStrClient(Client,
838 ERR_NEEDMOREPARAMS_MSG,
846 if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
847 target = Client_Search(Req->argv[0]);
849 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
850 Client_ID(Client), Req->argv[0]);
852 from = Client_Search(Req->prefix);
854 if (target != Client_ThisServer() && target != from) {
855 /* Ok, we have to forward the message. */
857 return IRC_WriteStrClient(Client,
858 ERR_NOSUCHSERVER_MSG,
859 Client_ID(Client), Req->prefix);
861 if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
865 return IRC_WriteStrClientPrefix(target, from,
866 "PONG %s :%s", s, Req->argv[1]);
870 /* The connection timestamp has already been updated when the data has
871 * been read from so socket, so we don't need to update it here. */
873 conn = Client_Conn(Client);
876 /* Check authentication PING-PONG ... */
877 auth_ping = Conn_GetAuthPing(conn);
879 LogDebug("AUTH PONG: waiting for token \"%ld\", got \"%s\" ...",
880 auth_ping, Req->argv[0]);
881 if (auth_ping == atoi(Req->argv[0])) {
882 Conn_SetAuthPing(conn, 0);
883 if (Client_Type(Client) == CLIENT_WAITAUTHPING)
886 if (!IRC_WriteStrClient(Client,
887 "To connect, type /QUOTE PONG %ld",
896 "Connection %d: received PONG. Lag: %ld seconds.", conn,
897 time(NULL) - Conn_LastPing(Client_Conn(Client)));
900 "Connection %d: received PONG.", conn);
907 * Initiate client registration.
909 * This function is called after the daemon received the required NICK and
910 * USER commands of a new client. If the daemon is compiled with support for
911 * PAM, the authentication sub-processs is forked; otherwise the global server
912 * password is checked.
914 * @param Client The client logging in.
915 * @returns CONNECTED or DISCONNECTED.
918 Hello_User(CLIENT * Client)
921 int pipefd[2], result;
926 assert(Client != NULL);
927 conn = Client_Conn(Client);
931 /* Did we receive the "auth PONG" already? */
932 if (Conn_GetAuthPing(conn)) {
933 Client_SetType(Client, CLIENT_WAITAUTHPING);
934 LogDebug("Connection %d: Waiting for AUTH PONG ...", conn);
942 /* Don't do any PAM authentication at all, instead emulate
943 * the beahiour of the daemon compiled without PAM support:
944 * because there can't be any "server password", all
945 * passwords supplied are classified as "wrong". */
946 if(Client_Password(Client)[0] == '\0')
947 return Hello_User_PostAuth(Client);
948 Reject_Client(Client, "non-empty password");
952 /* Fork child process for PAM authentication; and make sure that the
953 * process timeout is set higher than the login timeout! */
954 pid = Proc_Fork(Conn_GetProcStat(conn), pipefd,
955 cb_Read_Auth_Result, Conf_PongTimeout + 1);
957 LogDebug("Authenticator for connection %d created (PID %d).",
962 Log_Init_Subprocess("Auth");
963 Conn_CloseAllSockets(NONE);
964 result = PAM_Authenticate(Client);
965 if (write(pipefd[1], &result, sizeof(result)) != sizeof(result))
966 Log_Subprocess(LOG_ERR,
967 "Failed to pipe result to parent!");
968 Log_Exit_Subprocess("Auth");
972 /* Check global server password ... */
973 if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
975 Reject_Client(Client, "bad server password");
978 return Hello_User_PostAuth(Client);
986 * Read result of the authenticatior sub-process from pipe
988 * @param r_fd File descriptor of the pipe.
989 * @param events (ignored IO specification)
992 cb_Read_Auth_Result(int r_fd, UNUSED short events)
1000 LogDebug("Auth: Got callback on fd %d, events %d", r_fd, events);
1001 conn = Conn_GetFromProc(r_fd);
1003 /* Ops, none found? Probably the connection has already
1004 * been closed!? We'll ignore that ... */
1006 LogDebug("Auth: Got callback for unknown connection!?");
1009 proc = Conn_GetProcStat(conn);
1010 client = Conn_GetClient(conn);
1012 /* Read result from pipe */
1013 len = Proc_Read(proc, &result, sizeof(result));
1018 if (len != sizeof(result)) {
1019 Log(LOG_CRIT, "Auth: Got malformed result!");
1020 Reject_Client(client, "internal error");
1024 if (result == true) {
1025 Client_SetUser(client, Client_OrigUser(client), true);
1026 (void)Hello_User_PostAuth(client);
1028 Reject_Client(client, "bad password");
1035 * Reject a client because of wrong password.
1037 * This function is called either when the global server password or a password
1038 * checked using PAM has been wrong.
1040 * @param Client The client to reject.
1043 Reject_Client(CLIENT *Client, const char *InternalReason)
1046 "User \"%s\" rejected (connection %d): %s!",
1047 Client_Mask(Client), Client_Conn(Client), InternalReason);
1048 Conn_Close(Client_Conn(Client), InternalReason,
1049 "Access denied! Bad password?", true);
1054 * Finish client registration.
1056 * Introduce the new client to the network and send all "hello messages"
1057 * to it after authentication has been succeeded.
1059 * @param Client The client logging in.
1060 * @returns CONNECTED or DISCONNECTED.
1063 Hello_User_PostAuth(CLIENT *Client)
1065 if (Class_IsMember(CLASS_GLINE, Client)) {
1066 Reject_Client(Client, "G-Line'd");
1067 return DISCONNECTED;
1069 if (Class_IsMember(CLASS_KLINE, Client)) {
1070 Reject_Client(Client, "K-Line'd");
1071 return DISCONNECTED;
1074 Introduce_Client(NULL, Client, CLIENT_USER);
1076 if (!IRC_WriteStrClient
1077 (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
1079 if (!IRC_WriteStrClient
1080 (Client, RPL_YOURHOST_MSG, Client_ID(Client),
1081 Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
1082 TARGET_VENDOR, TARGET_OS))
1084 if (!IRC_WriteStrClient
1085 (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
1087 if (!IRC_WriteStrClient
1088 (Client, RPL_MYINFO_MSG, Client_ID(Client),
1089 Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
1093 /* Features supported by this server (005 numeric, ISUPPORT),
1094 * see <http://www.irc.org/tech_docs/005.html> for details. */
1095 if (!IRC_Send_ISUPPORT(Client))
1096 return DISCONNECTED;
1098 if (!IRC_Send_LUSERS(Client))
1099 return DISCONNECTED;
1100 if (!IRC_Show_MOTD(Client))
1101 return DISCONNECTED;
1103 /* Suspend the client for a second ... */
1104 IRC_SetPenalty(Client, 1);
1111 * Kill all users with a specific nick name in the network.
1113 * @param Nick Nick name.
1114 * @param Reason Reason for the KILL.
1117 Kill_Nick(char *Nick, char *Reason)
1121 assert (Nick != NULL);
1122 assert (Reason != NULL);
1129 Log(LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s",
1132 IRC_KILL(Client_ThisServer(), &r);
1137 * Introduce a new user or service client in the network.
1139 * @param From Remote server introducing the client or NULL (local).
1140 * @param Client New client.
1141 * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1144 Introduce_Client(CLIENT *From, CLIENT *Client, int Type)
1146 /* Set client type (user or service) */
1147 Client_SetType(Client, Type);
1150 if (Conf_IsService(Conf_GetServer(Client_Conn(From)),
1152 Client_SetType(Client, CLIENT_SERVICE);
1153 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1154 Client_TypeText(Client), Client_Mask(Client),
1155 Client_Modes(Client), Client_ID(From),
1156 Client_ID(Client_Introducer(Client)),
1157 Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1159 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1160 Client_TypeText(Client), Client_Mask(Client),
1161 Client_Conn(Client));
1162 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1163 Client_ID(Client), Client_User(Client),
1164 Client_Hostname(Client),
1165 Conn_IPA(Client_Conn(Client)),
1166 Client_TypeText(Client));
1169 /* Inform other servers */
1170 IRC_WriteStrServersPrefixFlag_CB(From,
1171 From != NULL ? From : Client_ThisServer(),
1172 '\0', cb_introduceClient, (void *)Client);
1173 } /* Introduce_Client */
1177 * Introduce a new user or service client to a remote server.
1179 * This function differentiates between RFC1459 and RFC2813 server links and
1180 * generates the appropriate commands to register the new user or service.
1182 * @param To The remote server to inform.
1183 * @param Prefix Prefix for the generated commands.
1184 * @param data CLIENT structure of the new client.
1187 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1189 CLIENT *c = (CLIENT *)data;
1191 char *modes, *user, *host;
1193 modes = Client_Modes(c);
1194 user = Client_User(c) ? Client_User(c) : "-";
1195 host = Client_Hostname(c) ? Client_Hostname(c) : "-";
1197 conn = Client_Conn(To);
1198 if (Conn_Options(conn) & CONN_RFC1459) {
1199 /* RFC 1459 mode: separate NICK and USER commands */
1200 Conn_WriteStr(conn, "NICK %s :%d", Client_ID(c),
1201 Client_Hops(c) + 1);
1202 Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1203 Client_ID(c), user, host,
1204 Client_ID(Client_Introducer(c)), Client_Info(c));
1206 Conn_WriteStr(conn, ":%s MODE %s +%s",
1207 Client_ID(c), Client_ID(c), modes);
1209 /* RFC 2813 mode: one combined NICK or SERVICE command */
1210 if (Client_Type(c) == CLIENT_SERVICE
1211 && strchr(Client_Flags(To), 'S'))
1212 IRC_WriteStrClientPrefix(To, Prefix,
1213 "SERVICE %s %d * +%s %d :%s",
1215 Client_MyToken(Client_Introducer(c)),
1216 Client_Modes(c), Client_Hops(c) + 1,
1219 IRC_WriteStrClientPrefix(To, Prefix,
1220 "NICK %s %d %s %s %d +%s :%s",
1221 Client_ID(c), Client_Hops(c) + 1,
1223 Client_MyToken(Client_Introducer(c)),
1224 modes, Client_Info(c));
1226 } /* cb_introduceClient */