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
38 #include "irc-write.h"
44 static void cb_Read_Auth_Result PARAMS((int r_fd, UNUSED short events));
48 * Initiate client login.
50 * This function is called after the daemon received the required NICK and
51 * USER commands of a new client. If the daemon is compiled with support for
52 * PAM, the authentication sub-processs is forked; otherwise the global server
53 * password is checked.
55 * @param Client The client logging in.
56 * @returns CONNECTED or DISCONNECTED.
59 Login_User(CLIENT * Client)
62 int pipefd[2], result;
67 assert(Client != NULL);
68 conn = Client_Conn(Client);
72 /* Did we receive the "auth PONG" already? */
73 if (Conn_GetAuthPing(conn)) {
74 Client_SetType(Client, CLIENT_WAITAUTHPING);
75 LogDebug("Connection %d: Waiting for AUTH PONG ...", conn);
83 /* Don't do any PAM authentication at all, instead emulate
84 * the beahiour of the daemon compiled without PAM support:
85 * because there can't be any "server password", all
86 * passwords supplied are classified as "wrong". */
87 if(Client_Password(Client)[0] == '\0')
88 return Login_User_PostAuth(Client);
89 Client_Reject(Client, "Non-empty password", false);
93 if (Conf_PAMIsOptional && strcmp(Client_Password(Client), "") == 0) {
94 /* Clients are not required to send a password and to be PAM-
95 * authenticated at all. If not, they won't become "identified"
96 * and keep the "~" in their supplied user name.
97 * Therefore it is sensible to either set Conf_PAMisOptional or
98 * to enable IDENT lookups -- not both. */
99 return Login_User_PostAuth(Client);
102 /* Fork child process for PAM authentication; and make sure that the
103 * process timeout is set higher than the login timeout! */
104 pid = Proc_Fork(Conn_GetProcStat(conn), pipefd,
105 cb_Read_Auth_Result, Conf_PongTimeout + 1);
107 LogDebug("Authenticator for connection %d created (PID %d).",
112 Log_Init_Subprocess("Auth");
113 Conn_CloseAllSockets(NONE);
114 result = PAM_Authenticate(Client);
115 if (write(pipefd[1], &result, sizeof(result)) != sizeof(result))
116 Log_Subprocess(LOG_ERR,
117 "Failed to pipe result to parent!");
118 Log_Exit_Subprocess("Auth");
122 /* Check global server password ... */
123 if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
125 Client_Reject(Client, "Bad server password", false);
128 return Login_User_PostAuth(Client);
133 * Finish client registration.
135 * Introduce the new client to the network and send all "hello messages"
136 * to it after authentication has been succeeded.
138 * @param Client The client logging in.
139 * @return CONNECTED or DISCONNECTED.
142 Login_User_PostAuth(CLIENT *Client)
144 assert(Client != NULL);
146 if (Class_HandleServerBans(Client) != CONNECTED)
149 Client_Introduce(NULL, Client, CLIENT_USER);
151 if (!IRC_WriteStrClient
152 (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
154 if (!IRC_WriteStrClient
155 (Client, RPL_YOURHOST_MSG, Client_ID(Client),
156 Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
157 TARGET_VENDOR, TARGET_OS))
159 if (!IRC_WriteStrClient
160 (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
162 if (!IRC_WriteStrClient
163 (Client, RPL_MYINFO_MSG, Client_ID(Client),
164 Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
168 /* Features supported by this server (005 numeric, ISUPPORT),
169 * see <http://www.irc.org/tech_docs/005.html> for details. */
170 if (!IRC_Send_ISUPPORT(Client))
173 if (!IRC_Send_LUSERS(Client))
175 if (!IRC_Show_MOTD(Client))
178 /* Suspend the client for a second ... */
179 IRC_SetPenalty(Client, 1);
187 * Read result of the authenticatior sub-process from pipe
189 * @param r_fd File descriptor of the pipe.
190 * @param events (ignored IO specification)
193 cb_Read_Auth_Result(int r_fd, UNUSED short events)
201 LogDebug("Auth: Got callback on fd %d, events %d", r_fd, events);
202 conn = Conn_GetFromProc(r_fd);
204 /* Ops, none found? Probably the connection has already
205 * been closed!? We'll ignore that ... */
207 LogDebug("Auth: Got callback for unknown connection!?");
210 proc = Conn_GetProcStat(conn);
211 client = Conn_GetClient(conn);
213 /* Read result from pipe */
214 len = Proc_Read(proc, &result, sizeof(result));
219 if (len != sizeof(result)) {
220 Log(LOG_CRIT, "Auth: Got malformed result!");
221 Client_Reject(client, "Internal error", false);
225 if (result == true) {
226 Client_SetUser(client, Client_OrigUser(client), true);
227 (void)Login_User_PostAuth(client);
229 Client_Reject(client, "Bad password", false);