2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 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
27 #include "client-cap.h"
36 #include "irc-write.h"
45 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 if PAM is not
93 * enabled, instead emulate the behavior of the daemon
94 * compiled without PAM support. */
95 if (strcmp(Conn_Password(conn), Conf_ServerPwd) == 0)
96 return Login_User_PostAuth(Client);
97 Client_Reject(Client, "Bad server password", false);
101 if (Conf_PAMIsOptional &&
102 strcmp(Conn_Password(conn), "") == 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);
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");
131 } else return CONNECTED;
133 /* Check global server password ... */
134 if (strcmp(Conn_Password(conn), Conf_ServerPwd) != 0) {
136 Client_Reject(Client, "Bad server password", false);
139 return Login_User_PostAuth(Client);
144 * Finish client registration.
146 * Introduce the new client to the network and send all "hello messages"
147 * to it after authentication has been succeeded.
149 * @param Client The client logging in.
150 * @return CONNECTED or DISCONNECTED.
153 Login_User_PostAuth(CLIENT *Client)
156 char modes[CLIENT_MODE_LEN + 1];
158 assert(Client != NULL);
160 if (Class_HandleServerBans(Client) != CONNECTED)
163 Client_Introduce(NULL, Client, CLIENT_USER);
165 if (!IRC_WriteStrClient
166 (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
168 if (!IRC_WriteStrClient
169 (Client, RPL_YOURHOST_MSG, Client_ID(Client),
170 Client_ID(Client_ThisServer()), PACKAGE_VERSION, HOST_CPU,
171 HOST_VENDOR, HOST_OS))
173 if (!IRC_WriteStrClient
174 (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
176 if (!IRC_WriteStrClient
177 (Client, RPL_MYINFO_MSG, Client_ID(Client),
178 Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
182 /* Features supported by this server (005 numeric, ISUPPORT),
183 * see <http://www.irc.org/tech_docs/005.html> for details. */
184 if (!IRC_Send_ISUPPORT(Client))
187 if (!IRC_Send_LUSERS(Client))
189 if (!IRC_Show_MOTD(Client))
192 /* Set default user modes */
193 if (Conf_DefaultUserModes[0]) {
194 snprintf(modes, sizeof(modes), "+%s", Conf_DefaultUserModes);
195 Req.prefix = Client_ID(Client_ThisServer());
196 Req.command = "MODE";
198 Req.argv[0] = Client_ID(Client);
200 IRC_MODE(Client, &Req);
202 IRC_SetPenalty(Client, 1);
210 * Read result of the authenticator sub-process from pipe
212 * @param r_fd File descriptor of the pipe.
213 * @param events (ignored IO specification)
216 cb_Read_Auth_Result(int r_fd, UNUSED short events)
218 char user[CLIENT_USER_LEN], *ptr;
225 LogDebug("Auth: Got callback on fd %d, events %d", r_fd, events);
226 conn = Conn_GetFromProc(r_fd);
228 /* Ops, none found? Probably the connection has already
229 * been closed!? We'll ignore that ... */
231 LogDebug("Auth: Got callback for unknown connection!?");
234 proc = Conn_GetProcStat(conn);
235 client = Conn_GetClient(conn);
237 /* Read result from pipe */
238 len = Proc_Read(proc, &result, sizeof(result));
243 if (len != sizeof(result)) {
244 Log(LOG_CRIT, "Auth: Got malformed result!");
245 Client_Reject(client, "Internal error", false);
249 if (result == true) {
250 /* Authentication succeeded, now set the correct user name
251 * supplied by the client (without prepended '~' for exmaple),
252 * but cut it at the first '@' character: */
253 strlcpy(user, Client_OrigUser(client), sizeof(user));
254 ptr = strchr(user, '@');
257 Client_SetUser(client, user, true);
258 (void)Login_User_PostAuth(client);
260 Client_Reject(client, "Bad password", false);