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 behavior 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(Conn_Password(conn)[0] == '\0')
97 return Login_User_PostAuth(Client);
98 Client_Reject(Client, "Non-empty password", false);
102 if (Conf_PAMIsOptional &&
103 strcmp(Conn_Password(conn), "") == 0) {
104 /* Clients are not required to send a password and to be PAM-
105 * authenticated at all. If not, they won't become "identified"
106 * and keep the "~" in their supplied user name.
107 * Therefore it is sensible to either set Conf_PAMisOptional or
108 * to enable IDENT lookups -- not both. */
109 return Login_User_PostAuth(Client);
112 /* Fork child process for PAM authentication; and make sure that the
113 * process timeout is set higher than the login timeout! */
114 pid = Proc_Fork(Conn_GetProcStat(conn), pipefd,
115 cb_Read_Auth_Result, Conf_PongTimeout + 1);
117 LogDebug("Authenticator for connection %d created (PID %d).",
122 Log_Init_Subprocess("Auth");
123 Conn_CloseAllSockets(NONE);
124 result = PAM_Authenticate(Client);
125 if (write(pipefd[1], &result, sizeof(result)) != sizeof(result))
126 Log_Subprocess(LOG_ERR,
127 "Failed to pipe result to parent!");
128 Log_Exit_Subprocess("Auth");
132 /* Check global server password ... */
133 if (strcmp(Conn_Password(conn), Conf_ServerPwd) != 0) {
135 Client_Reject(Client, "Bad server password", false);
138 return Login_User_PostAuth(Client);
143 * Finish client registration.
145 * Introduce the new client to the network and send all "hello messages"
146 * to it after authentication has been succeeded.
148 * @param Client The client logging in.
149 * @return CONNECTED or DISCONNECTED.
152 Login_User_PostAuth(CLIENT *Client)
154 assert(Client != NULL);
156 if (Class_HandleServerBans(Client) != CONNECTED)
159 Client_Introduce(NULL, Client, CLIENT_USER);
161 if (!IRC_WriteStrClient
162 (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
164 if (!IRC_WriteStrClient
165 (Client, RPL_YOURHOST_MSG, Client_ID(Client),
166 Client_ID(Client_ThisServer()), PACKAGE_VERSION, HOST_CPU,
167 HOST_VENDOR, HOST_OS))
169 if (!IRC_WriteStrClient
170 (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
172 if (!IRC_WriteStrClient
173 (Client, RPL_MYINFO_MSG, Client_ID(Client),
174 Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
178 /* Features supported by this server (005 numeric, ISUPPORT),
179 * see <http://www.irc.org/tech_docs/005.html> for details. */
180 if (!IRC_Send_ISUPPORT(Client))
183 if (!IRC_Send_LUSERS(Client))
185 if (!IRC_Show_MOTD(Client))
188 /* Suspend the client for a second ... */
189 IRC_SetPenalty(Client, 1);
197 * Read result of the authenticator sub-process from pipe
199 * @param r_fd File descriptor of the pipe.
200 * @param events (ignored IO specification)
203 cb_Read_Auth_Result(int r_fd, UNUSED short events)
205 char user[CLIENT_USER_LEN], *ptr;
212 LogDebug("Auth: Got callback on fd %d, events %d", r_fd, events);
213 conn = Conn_GetFromProc(r_fd);
215 /* Ops, none found? Probably the connection has already
216 * been closed!? We'll ignore that ... */
218 LogDebug("Auth: Got callback for unknown connection!?");
221 proc = Conn_GetProcStat(conn);
222 client = Conn_GetClient(conn);
224 /* Read result from pipe */
225 len = Proc_Read(proc, &result, sizeof(result));
230 if (len != sizeof(result)) {
231 Log(LOG_CRIT, "Auth: Got malformed result!");
232 Client_Reject(client, "Internal error", false);
236 if (result == true) {
237 /* Authentication succeeded, now set the correct user name
238 * supplied by the client (without prepended '~' for exmaple),
239 * but cut it at the first '@' character: */
240 strlcpy(user, Client_OrigUser(client), sizeof(user));
241 ptr = strchr(user, '@');
244 Client_SetUser(client, user, true);
245 (void)Login_User_PostAuth(client);
247 Client_Reject(client, "Bad password", false);