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] );
274 /* If we received a valid USER command already then
275 * register the new client! */
276 if( Client_Type( Client ) == CLIENT_GOTUSER )
277 return Hello_User( Client );
279 Client_SetType( Client, CLIENT_GOTNICK );
281 /* Nickname change */
282 if (Client_Conn(target) > NONE) {
285 "%s \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
286 Client_TypeText(target), Client_Mask(target),
287 Client_Conn(target), Client_ID(target),
289 Conn_UpdateIdle(Client_Conn(target));
292 LogDebug("%s \"%s\" changed nick: \"%s\" -> \"%s\".",
293 Client_TypeText(target),
294 Client_Mask(target), Client_ID(target),
298 /* Inform all users and servers (which have to know)
299 * of this nickname change */
300 if( Client_Type( Client ) == CLIENT_USER )
301 IRC_WriteStrClientPrefix( Client, Client,
304 IRC_WriteStrServersPrefix( Client, target,
305 "NICK :%s", Req->argv[0] );
306 IRC_WriteStrRelatedPrefix( target, target, false,
307 "NICK :%s", Req->argv[0] );
309 /* Register old nickname for WHOWAS queries */
310 Client_RegisterWhowas( target );
312 /* Save new nickname */
313 Client_SetID( target, Req->argv[0] );
315 IRC_SetPenalty( target, 2 );
319 } else if(Client_Type(Client) == CLIENT_SERVER ||
320 Client_Type(Client) == CLIENT_SERVICE) {
321 /* Server or service introduces new client */
323 /* Bad number of parameters? */
324 if (Req->argc != 2 && Req->argc != 7)
325 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
326 Client_ID(Client), Req->command);
328 if (Req->argc >= 7) {
329 /* RFC 2813 compatible syntax */
331 hops = atoi(Req->argv[1]);
333 hostname = Req->argv[3];
334 token = atoi(Req->argv[4]);
335 modes = Req->argv[5] + 1;
338 /* RFC 1459 compatible syntax */
342 hostname = Client_ID(Client);
343 token = atoi(Req->argv[1]);
348 c = Client_Search(nick);
351 * the new nick is already present on this server:
352 * the new and the old one have to be disconnected now.
354 Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
355 Kill_Nick( Req->argv[0], "Nick collision" );
359 /* Find the Server this client is connected to */
360 intr_c = Client_GetFromToken(Client, token);
363 Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
364 Kill_Nick( Req->argv[0], "Unknown server" );
368 c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
369 token, modes, info, true);
372 /* out of memory, need to disconnect client to keep network state consistent */
373 Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
374 Kill_Nick( Req->argv[0], "Server error" );
378 /* RFC 2813: client is now fully registered, inform all the
379 * other servers about the new user.
380 * RFC 1459: announce the new client only after receiving the
381 * USER command, first we need more information! */
383 LogDebug("Client \"%s\" is being registered (RFC 1459) ...",
385 Client_SetType(c, CLIENT_GOTNICK);
387 Introduce_Client(Client, c, CLIENT_USER);
391 else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
396 * Handler for the IRC "USER" command.
398 * See RFC 2812, 3.1.3 "User message".
400 * @param Client The client from which this command has been received.
401 * @param Req Request structure with prefix and all parameters.
402 * @returns CONNECTED or DISCONNECTED.
405 IRC_USER(CLIENT * Client, REQUEST * Req)
412 assert(Client != NULL);
415 if (Client_Type(Client) == CLIENT_GOTNICK ||
417 Client_Type(Client) == CLIENT_UNKNOWN ||
419 Client_Type(Client) == CLIENT_GOTPASS)
423 return IRC_WriteStrClient(Client,
424 ERR_NEEDMOREPARAMS_MSG,
430 ptr = Client_User(Client);
431 if (!ptr || !*ptr || *ptr == '~')
432 Client_SetUser(Client, Req->argv[0], false);
434 Client_SetUser(Client, Req->argv[0], false);
436 Client_SetOrigUser(Client, Req->argv[0]);
438 /* "Real name" or user info text: Don't set it to the empty
439 * string, the original ircd can't deal with such "real names"
440 * (e. g. "USER user * * :") ... */
442 Client_SetInfo(Client, Req->argv[3]);
444 Client_SetInfo(Client, "-");
446 LogDebug("Connection %d: got valid USER command ...",
447 Client_Conn(Client));
448 if (Client_Type(Client) == CLIENT_GOTNICK)
449 return Hello_User(Client);
451 Client_SetType(Client, CLIENT_GOTUSER);
454 } else if (Client_Type(Client) == CLIENT_SERVER ||
455 Client_Type(Client) == CLIENT_SERVICE) {
456 /* Server/service updating an user */
458 return IRC_WriteStrClient(Client,
459 ERR_NEEDMOREPARAMS_MSG,
462 c = Client_Search(Req->prefix);
464 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
468 Client_SetUser(c, Req->argv[0], true);
469 Client_SetOrigUser(c, Req->argv[0]);
470 Client_SetHostname(c, Req->argv[1]);
471 Client_SetInfo(c, Req->argv[3]);
473 LogDebug("Connection %d: got valid USER command for \"%s\".",
474 Client_Conn(Client), Client_Mask(c));
476 /* RFC 1459 style user registration?
477 * Introduce client to network: */
478 if (Client_Type(c) == CLIENT_GOTNICK)
479 Introduce_Client(Client, c, CLIENT_USER);
482 } else if (Client_Type(Client) == CLIENT_USER) {
483 /* Already registered connection */
484 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
487 /* Unexpected/invalid connection state? */
488 return IRC_WriteStrClient(Client, ERR_NOTREGISTERED_MSG,
495 * Handler for the IRC "SERVICE" command.
497 * This function implements IRC Services registration using the SERVICE command
498 * defined in RFC 2812 3.1.6 and RFC 2813 4.1.4.
500 * At the moment ngIRCd doesn't support directly linked services, so this
501 * function returns ERR_ERRONEUSNICKNAME when the SERVICE command has not been
502 * received from a peer server.
504 * @param Client The client from which this command has been received.
505 * @param Req Request structure with prefix and all parameters.
506 * @returns CONNECTED or DISCONNECTED..
509 IRC_SERVICE(CLIENT *Client, REQUEST *Req)
512 char *nick, *user, *host, *info, *modes, *ptr;
515 assert(Client != NULL);
518 if (Client_Type(Client) != CLIENT_GOTPASS &&
519 Client_Type(Client) != CLIENT_SERVER)
520 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
524 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
525 Client_ID(Client), Req->command);
527 if (Client_Type(Client) != CLIENT_SERVER)
528 return IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
529 Client_ID(Client), Req->argv[0]);
531 /* Bad number of parameters? */
533 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
534 Client_ID(Client), Req->command);
537 user = NULL; host = NULL;
538 token = atoi(Req->argv[1]);
539 hops = atoi(Req->argv[4]);
542 /* Validate service name ("nick name") */
543 c = Client_Search(nick);
545 /* Nick name collission: disconnect (KILL) both clients! */
546 Log(LOG_ERR, "Server %s introduces already registered service \"%s\"!",
547 Client_ID(Client), nick);
548 Kill_Nick(nick, "Nick collision");
552 /* Get the server to which the service is connected */
553 intr_c = Client_GetFromToken(Client, token);
555 Log(LOG_ERR, "Server %s introduces service \"%s\" on unknown server!?",
556 Client_ID(Client), nick);
557 Kill_Nick(nick, "Unknown server");
561 /* Get user and host name */
562 ptr = strchr(nick, '@');
568 host = Client_Hostname(intr_c);
569 ptr = strchr(nick, '!');
577 /* According to RFC 2812/2813 parameter 4 <type> "is currently reserved
578 * for future usage"; but we use it to transfer the modes and check
579 * that the first character is a '+' sign and ignore it otherwise. */
580 modes = (Req->argv[3][0] == '+') ? ++Req->argv[3] : "";
582 c = Client_NewRemoteUser(intr_c, nick, hops, user, host,
583 token, modes, info, true);
585 /* Couldn't create client structure, so KILL the service to
586 * keep network status consistent ... */
587 Log(LOG_ALERT, "Can't create client structure! (on connection %d)",
588 Client_Conn(Client));
589 Kill_Nick(nick, "Server error");
593 Introduce_Client(Client, c, CLIENT_SERVICE);
599 * Handler for the IRC "WEBIRC" command.
601 * See doc/Protocol.txt, section II.4:
602 * "Update webchat/proxy client information".
604 * @param Client The client from which this command has been received.
605 * @param Req Request structure with prefix and all parameters.
606 * @returns CONNECTED or DISCONNECTED.
609 IRC_WEBIRC(CLIENT *Client, REQUEST *Req)
611 /* Exactly 4 parameters are requited */
613 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
614 Client_ID(Client), Req->command);
616 if (!Conf_WebircPwd[0] || strcmp(Req->argv[0], Conf_WebircPwd) != 0)
617 return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
620 LogDebug("Connection %d: got valid WEBIRC command: user=%s, host=%s, ip=%s",
621 Client_Conn(Client), Req->argv[1], Req->argv[2], Req->argv[3]);
623 Client_SetUser(Client, Req->argv[1], true);
624 Client_SetOrigUser(Client, Req->argv[1]);
625 Client_SetHostname(Client, Req->argv[2]);
631 * Handler for the IRC "QUIT" command.
633 * See RFC 2812, 3.1.7 "Quit", and RFC 2813, 4.1.5 "Quit".
635 * @param Client The client from which this command has been received.
636 * @param Req Request structure with prefix and all parameters.
637 * @returns CONNECTED or DISCONNECTED.
640 IRC_QUIT( CLIENT *Client, REQUEST *Req )
643 char quitmsg[LINE_LEN];
645 assert( Client != NULL );
646 assert( Req != NULL );
648 /* Wrong number of arguments? */
650 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
653 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
655 if ( Client_Type( Client ) == CLIENT_SERVER )
658 target = Client_Search( Req->prefix );
661 Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
665 Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
671 if (Req->argc == 1 && quitmsg[0] != '\"') {
672 /* " " to avoid confusion */
673 strlcpy(quitmsg, "\"", sizeof quitmsg);
674 strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
675 strlcat(quitmsg, "\"", sizeof quitmsg );
678 /* User, Service, or not yet registered */
679 Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
687 IRC_QUIT_HTTP( CLIENT *Client, REQUEST *Req )
690 Req->argv[0] = "Oops, HTTP request received? This is IRC!";
691 return IRC_QUIT(Client, Req);
692 } /* IRC_QUIT_HTTP */
696 * Handler for the IRC "PING" command.
698 * See RFC 2812, 3.7.2 "Ping message".
700 * @param Client The client from which this command has been received.
701 * @param Req Request structure with prefix and all parameters.
702 * @returns CONNECTED or DISCONNECTED.
705 IRC_PING(CLIENT *Client, REQUEST *Req)
707 CLIENT *target, *from;
709 assert(Client != NULL);
713 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
716 /* Don't ignore additional arguments when in "strict" mode */
718 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
719 Client_ID(Client), Req->command);
723 /* A target has been specified ... */
724 target = Client_Search(Req->argv[1]);
726 if (!target || Client_Type(target) != CLIENT_SERVER)
727 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
728 Client_ID(Client), Req->argv[1]);
730 if (target != Client_ThisServer()) {
731 /* Ok, we have to forward the PING */
732 if (Client_Type(Client) == CLIENT_SERVER)
733 from = Client_Search(Req->prefix);
737 return IRC_WriteStrClient(Client,
738 ERR_NOSUCHSERVER_MSG,
739 Client_ID(Client), Req->prefix);
741 return IRC_WriteStrClientPrefix(target, from,
742 "PING %s :%s", Req->argv[0],
747 if (Client_Type(Client) == CLIENT_SERVER) {
749 from = Client_Search(Req->prefix);
753 from = Client_ThisServer();
755 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
756 Client_ID(Client), Req->prefix);
758 Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
759 Client_Conn(Client));
762 return IRC_WriteStrClient(Client, "PONG %s :%s",
763 Client_ID(from), Client_ID(Client));
765 /* Some clients depend on the argument being returned in the PONG
766 * reply (not mentioned in any RFC, though) */
767 return IRC_WriteStrClient(Client, "PONG %s :%s",
768 Client_ID(from), Req->argv[0]);
774 * Handler for the IRC "PONG" command.
776 * See RFC 2812, 3.7.3 "Pong message".
778 * @param Client The client from which this command has been received.
779 * @param Req Request structure with prefix and all parameters.
780 * @returns CONNECTED or DISCONNECTED.
783 IRC_PONG(CLIENT *Client, REQUEST *Req)
785 CLIENT *target, *from;
788 assert(Client != NULL);
791 /* Wrong number of arguments? */
793 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
796 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
797 Client_ID(Client), Req->command);
800 if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
801 target = Client_Search(Req->argv[0]);
803 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
804 Client_ID(Client), Req->argv[0]);
806 from = Client_Search(Req->prefix);
808 if (target != Client_ThisServer() && target != from) {
809 /* Ok, we have to forward the message. */
811 return IRC_WriteStrClient(Client,
812 ERR_NOSUCHSERVER_MSG,
813 Client_ID(Client), Req->prefix);
815 if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
819 return IRC_WriteStrClientPrefix(target, from,
820 "PONG %s :%s", s, Req->argv[1]);
824 /* The connection timestamp has already been updated when the data has
825 * been read from so socket, so we don't need to update it here. */
827 if (Client_Conn(Client) > NONE)
829 "Connection %d: received PONG. Lag: %ld seconds.",
831 time(NULL) - Conn_LastPing(Client_Conn(Client)));
834 "Connection %d: received PONG.", Client_Conn(Client));
841 * Initiate client registration.
843 * This function is called after the daemon received the required NICK and
844 * USER commands of a new client. If the daemon is compiled with support for
845 * PAM, the authentication sub-processs is forked; otherwise the global server
846 * password is checked.
848 * @param Client The client logging in.
849 * @returns CONNECTED or DISCONNECTED.
852 Hello_User(CLIENT * Client)
855 int pipefd[2], result;
859 assert(Client != NULL);
860 conn = Client_Conn(Client);
863 /* Don't do any PAM authentication at all, instead emulate
864 * the beahiour of the daemon compiled without PAM support:
865 * because there can't be any "server password", all
866 * passwords supplied are classified as "wrong". */
867 if(Client_Password(Client)[0] == '\0')
868 return Hello_User_PostAuth(Client);
869 Reject_Client(Client);
873 /* Fork child process for PAM authentication; and make sure that the
874 * process timeout is set higher than the login timeout! */
875 pid = Proc_Fork(Conn_GetProcStat(conn), pipefd,
876 cb_Read_Auth_Result, Conf_PongTimeout + 1);
878 LogDebug("Authenticator for connection %d created (PID %d).",
883 Log_Init_Subprocess("Auth");
884 result = PAM_Authenticate(Client);
885 if (write(pipefd[1], &result, sizeof(result)) != sizeof(result))
886 Log_Subprocess(LOG_ERR,
887 "Failed to pipe result to parent!");
888 Log_Exit_Subprocess("Auth");
892 assert(Client != NULL);
894 /* Check global server password ... */
895 if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
897 Reject_Client(Client);
900 return Hello_User_PostAuth(Client);
908 * Read result of the authenticatior sub-process from pipe
910 * @param r_fd File descriptor of the pipe.
911 * @param events (ignored IO specification)
914 cb_Read_Auth_Result(int r_fd, UNUSED short events)
922 LogDebug("Auth: Got callback on fd %d, events %d", r_fd, events);
923 conn = Conn_GetFromProc(r_fd);
925 /* Ops, none found? Probably the connection has already
926 * been closed!? We'll ignore that ... */
928 LogDebug("Auth: Got callback for unknown connection!?");
931 proc = Conn_GetProcStat(conn);
932 client = Conn_GetClient(conn);
934 /* Read result from pipe */
935 len = Proc_Read(proc, &result, sizeof(result));
939 if (len != sizeof(result)) {
940 Log(LOG_CRIT, "Auth: Got malformed result!");
941 Reject_Client(client);
945 if (result == true) {
946 Client_SetUser(client, Client_OrigUser(client), true);
947 (void)Hello_User_PostAuth(client);
949 Reject_Client(client);
956 * Reject a client because of wrong password.
958 * This function is called either when the global server password or a password
959 * checked using PAM has been wrong.
961 * @param Client The client to reject.
964 Reject_Client(CLIENT *Client)
967 "User \"%s\" rejected (connection %d): Access denied!",
968 Client_Mask(Client), Client_Conn(Client));
969 Conn_Close(Client_Conn(Client), NULL,
970 "Access denied! Bad password?", true);
975 * Finish client registration.
977 * Introduce the new client to the network and send all "hello messages"
978 * to it after authentication has been succeeded.
980 * @param Client The client logging in.
981 * @returns CONNECTED or DISCONNECTED.
984 Hello_User_PostAuth(CLIENT *Client)
986 Introduce_Client(NULL, Client, CLIENT_USER);
988 if (!IRC_WriteStrClient
989 (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
991 if (!IRC_WriteStrClient
992 (Client, RPL_YOURHOST_MSG, Client_ID(Client),
993 Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
994 TARGET_VENDOR, TARGET_OS))
996 if (!IRC_WriteStrClient
997 (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
999 if (!IRC_WriteStrClient
1000 (Client, RPL_MYINFO_MSG, Client_ID(Client),
1001 Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
1005 /* Features supported by this server (005 numeric, ISUPPORT),
1006 * see <http://www.irc.org/tech_docs/005.html> for details. */
1007 if (!IRC_Send_ISUPPORT(Client))
1008 return DISCONNECTED;
1010 if (!IRC_Send_LUSERS(Client))
1011 return DISCONNECTED;
1012 if (!IRC_Show_MOTD(Client))
1013 return DISCONNECTED;
1015 /* Suspend the client for a second ... */
1016 IRC_SetPenalty(Client, 1);
1023 * Kill all users with a specific nick name in the network.
1025 * @param Nick Nick name.
1026 * @param Reason Reason for the KILL.
1029 Kill_Nick( char *Nick, char *Reason )
1033 assert( Nick != NULL );
1034 assert( Reason != NULL );
1036 r.prefix = (char *)Client_ThisServer( );
1041 Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
1042 IRC_KILL( Client_ThisServer( ), &r );
1047 * Introduce a new user or service client in the network.
1049 * @param From Remote server introducing the client or NULL (local).
1050 * @param Client New client.
1051 * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1054 Introduce_Client(CLIENT *From, CLIENT *Client, int Type)
1056 /* Set client type (user or service) */
1057 Client_SetType(Client, Type);
1060 if (Conf_IsService(Conf_GetServer(Client_Conn(From)),
1062 Client_SetType(Client, CLIENT_SERVICE);
1063 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1064 Client_TypeText(Client), Client_Mask(Client),
1065 Client_Modes(Client), Client_ID(From),
1066 Client_ID(Client_Introducer(Client)),
1067 Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1069 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1070 Client_TypeText(Client), Client_Mask(Client),
1071 Client_Conn(Client));
1072 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1073 Client_ID(Client), Client_User(Client),
1074 Client_Hostname(Client),
1075 Conn_IPA(Client_Conn(Client)),
1076 Client_TypeText(Client));
1079 /* Inform other servers */
1080 IRC_WriteStrServersPrefixFlag_CB(From,
1081 From != NULL ? From : Client_ThisServer(),
1082 '\0', cb_introduceClient, (void *)Client);
1083 } /* Introduce_Client */
1087 * Introduce a new user or service client to a remote server.
1089 * This function differentiates between RFC1459 and RFC2813 server links and
1090 * generates the appropriate commands to register the new user or service.
1092 * @param To The remote server to inform.
1093 * @param Prefix Prefix for the generated commands.
1094 * @param data CLIENT structure of the new client.
1097 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1099 CLIENT *c = (CLIENT *)data;
1101 char *modes, *user, *host;
1103 modes = Client_Modes(c);
1104 user = Client_User(c) ? Client_User(c) : "-";
1105 host = Client_Hostname(c) ? Client_Hostname(c) : "-";
1107 conn = Client_Conn(To);
1108 if (Conn_Options(conn) & CONN_RFC1459) {
1109 /* RFC 1459 mode: separate NICK and USER commands */
1110 Conn_WriteStr(conn, "NICK %s :%d", Client_ID(c),
1111 Client_Hops(c) + 1);
1112 Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1113 Client_ID(c), user, host,
1114 Client_ID(Client_Introducer(c)), Client_Info(c));
1116 Conn_WriteStr(conn, ":%s MODE %s +%s",
1117 Client_ID(c), Client_ID(c), modes);
1119 /* RFC 2813 mode: one combined NICK or SERVICE command */
1120 if (Client_Type(c) == CLIENT_SERVICE
1121 && strchr(Client_Flags(To), 'S'))
1122 IRC_WriteStrClientPrefix(To, Prefix,
1123 "SERVICE %s %d * +%s %d :%s",
1125 Client_MyToken(Client_Introducer(c)),
1126 Client_Modes(c), Client_Hops(c) + 1,
1129 IRC_WriteStrClientPrefix(To, Prefix,
1130 "NICK %s %d %s %s %d +%s :%s",
1131 Client_ID(c), Client_Hops(c) + 1,
1133 Client_MyToken(Client_Introducer(c)),
1134 modes, Client_Info(c));
1136 } /* cb_introduceClient */