2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 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.
16 * Functions to deal with client logins
30 #include "client-cap.h"
40 #include "irc-write.h"
46 static void cb_Read_Auth_Result PARAMS((int r_fd, UNUSED short events));
50 * Initiate client login.
52 * This function is called after the daemon received the required NICK and
53 * USER commands of a new client. If the daemon is compiled with support for
54 * PAM, the authentication sub-processs is forked; otherwise the global server
55 * password is checked.
57 * @param Client The client logging in.
58 * @returns CONNECTED or DISCONNECTED.
61 Login_User(CLIENT * Client)
64 int pipefd[2], result;
69 assert(Client != NULL);
70 conn = Client_Conn(Client);
74 /* Did we receive the "auth PONG" already? */
75 if (Conn_GetAuthPing(conn)) {
76 Client_SetType(Client, CLIENT_WAITAUTHPING);
77 LogDebug("Connection %d: Waiting for AUTH PONG ...", conn);
83 /* Still waiting for "CAP END" command? */
84 if (Client_Cap(Client) & CLIENT_CAP_PENDING) {
85 Client_SetType(Client, CLIENT_WAITCAPEND);
86 LogDebug("Connection %d: Waiting for CAP END ...", conn);
92 /* Don't do any PAM authentication at all, instead emulate
93 * the beahiour of the daemon compiled without PAM support:
94 * because there can't be any "server password", all
95 * passwords supplied are classified as "wrong". */
96 if(Client_Password(Client)[0] == '\0')
97 return Login_User_PostAuth(Client);
98 Client_Reject(Client, "Non-empty password", false);
102 if (Conf_PAMIsOptional && strcmp(Client_Password(Client), "") == 0) {
103 /* Clients are not required to send a password and to be PAM-
104 * authenticated at all. If not, they won't become "identified"
105 * and keep the "~" in their supplied user name.
106 * Therefore it is sensible to either set Conf_PAMisOptional or
107 * to enable IDENT lookups -- not both. */
108 return Login_User_PostAuth(Client);
111 /* Fork child process for PAM authentication; and make sure that the
112 * process timeout is set higher than the login timeout! */
113 pid = Proc_Fork(Conn_GetProcStat(conn), pipefd,
114 cb_Read_Auth_Result, Conf_PongTimeout + 1);
116 LogDebug("Authenticator for connection %d created (PID %d).",
121 Log_Init_Subprocess("Auth");
122 Conn_CloseAllSockets(NONE);
123 result = PAM_Authenticate(Client);
124 if (write(pipefd[1], &result, sizeof(result)) != sizeof(result))
125 Log_Subprocess(LOG_ERR,
126 "Failed to pipe result to parent!");
127 Log_Exit_Subprocess("Auth");
131 /* Check global server password ... */
132 if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
134 Client_Reject(Client, "Bad server password", false);
137 return Login_User_PostAuth(Client);
142 * Finish client registration.
144 * Introduce the new client to the network and send all "hello messages"
145 * to it after authentication has been succeeded.
147 * @param Client The client logging in.
148 * @return CONNECTED or DISCONNECTED.
151 Login_User_PostAuth(CLIENT *Client)
153 assert(Client != NULL);
155 if (Class_HandleServerBans(Client) != CONNECTED)
158 Client_Introduce(NULL, Client, CLIENT_USER);
160 if (!IRC_WriteStrClient
161 (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
163 if (!IRC_WriteStrClient
164 (Client, RPL_YOURHOST_MSG, Client_ID(Client),
165 Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
166 TARGET_VENDOR, TARGET_OS))
168 if (!IRC_WriteStrClient
169 (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
171 if (!IRC_WriteStrClient
172 (Client, RPL_MYINFO_MSG, Client_ID(Client),
173 Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
177 /* Features supported by this server (005 numeric, ISUPPORT),
178 * see <http://www.irc.org/tech_docs/005.html> for details. */
179 if (!IRC_Send_ISUPPORT(Client))
182 if (!IRC_Send_LUSERS(Client))
184 if (!IRC_Show_MOTD(Client))
187 /* Suspend the client for a second ... */
188 IRC_SetPenalty(Client, 1);
196 * Read result of the authenticatior sub-process from pipe
198 * @param r_fd File descriptor of the pipe.
199 * @param events (ignored IO specification)
202 cb_Read_Auth_Result(int r_fd, UNUSED short events)
210 LogDebug("Auth: Got callback on fd %d, events %d", r_fd, events);
211 conn = Conn_GetFromProc(r_fd);
213 /* Ops, none found? Probably the connection has already
214 * been closed!? We'll ignore that ... */
216 LogDebug("Auth: Got callback for unknown connection!?");
219 proc = Conn_GetProcStat(conn);
220 client = Conn_GetClient(conn);
222 /* Read result from pipe */
223 len = Proc_Read(proc, &result, sizeof(result));
228 if (len != sizeof(result)) {
229 Log(LOG_CRIT, "Auth: Got malformed result!");
230 Client_Reject(client, "Internal error", false);
234 if (result == true) {
235 Client_SetUser(client, Client_OrigUser(client), true);
236 (void)Login_User_PostAuth(client);
238 Client_Reject(client, "Bad password", false);