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
31 #include "client-cap.h"
42 #include "irc-write.h"
48 static void cb_Read_Auth_Result PARAMS((int r_fd, UNUSED short events));
52 * Initiate client login.
54 * This function is called after the daemon received the required NICK and
55 * USER commands of a new client. If the daemon is compiled with support for
56 * PAM, the authentication sub-processs is forked; otherwise the global server
57 * password is checked.
59 * @param Client The client logging in.
60 * @returns CONNECTED or DISCONNECTED.
63 Login_User(CLIENT * Client)
66 int pipefd[2], result;
71 assert(Client != NULL);
72 conn = Client_Conn(Client);
76 /* Did we receive the "auth PONG" already? */
77 if (Conn_GetAuthPing(conn)) {
78 Client_SetType(Client, CLIENT_WAITAUTHPING);
79 LogDebug("Connection %d: Waiting for AUTH PONG ...", conn);
85 /* Still waiting for "CAP END" command? */
86 if (Client_Cap(Client) & CLIENT_CAP_PENDING) {
87 Client_SetType(Client, CLIENT_WAITCAPEND);
88 LogDebug("Connection %d: Waiting for CAP END ...", conn);
94 /* Don't do any PAM authentication at all, instead emulate
95 * the behavior of the daemon compiled without PAM support:
96 * because there can't be any "server password", all
97 * passwords supplied are classified as "wrong". */
98 if(Conn_Password(conn)[0] == '\0')
99 return Login_User_PostAuth(Client);
100 Client_Reject(Client, "Non-empty password", false);
104 if (Conf_PAMIsOptional &&
105 strcmp(Conn_Password(conn), "") == 0) {
106 /* Clients are not required to send a password and to be PAM-
107 * authenticated at all. If not, they won't become "identified"
108 * and keep the "~" in their supplied user name.
109 * Therefore it is sensible to either set Conf_PAMisOptional or
110 * to enable IDENT lookups -- not both. */
111 return Login_User_PostAuth(Client);
114 /* Fork child process for PAM authentication; and make sure that the
115 * process timeout is set higher than the login timeout! */
116 pid = Proc_Fork(Conn_GetProcStat(conn), pipefd,
117 cb_Read_Auth_Result, Conf_PongTimeout + 1);
119 LogDebug("Authenticator for connection %d created (PID %d).",
124 Log_Init_Subprocess("Auth");
125 Conn_CloseAllSockets(NONE);
126 result = PAM_Authenticate(Client);
127 if (write(pipefd[1], &result, sizeof(result)) != sizeof(result))
128 Log_Subprocess(LOG_ERR,
129 "Failed to pipe result to parent!");
130 Log_Exit_Subprocess("Auth");
134 /* Check global server password ... */
135 if (strcmp(Conn_Password(conn), Conf_ServerPwd) != 0) {
137 Client_Reject(Client, "Bad server password", false);
140 return Login_User_PostAuth(Client);
145 * Finish client registration.
147 * Introduce the new client to the network and send all "hello messages"
148 * to it after authentication has been succeeded.
150 * @param Client The client logging in.
151 * @return CONNECTED or DISCONNECTED.
154 Login_User_PostAuth(CLIENT *Client)
157 char modes[CLIENT_MODE_LEN + 1];
159 assert(Client != NULL);
161 if (Class_HandleServerBans(Client) != CONNECTED)
164 Client_Introduce(NULL, Client, CLIENT_USER);
166 if (!IRC_WriteStrClient
167 (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
169 if (!IRC_WriteStrClient
170 (Client, RPL_YOURHOST_MSG, Client_ID(Client),
171 Client_ID(Client_ThisServer()), PACKAGE_VERSION, HOST_CPU,
172 HOST_VENDOR, HOST_OS))
174 if (!IRC_WriteStrClient
175 (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
177 if (!IRC_WriteStrClient
178 (Client, RPL_MYINFO_MSG, Client_ID(Client),
179 Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
183 /* Features supported by this server (005 numeric, ISUPPORT),
184 * see <http://www.irc.org/tech_docs/005.html> for details. */
185 if (!IRC_Send_ISUPPORT(Client))
188 if (!IRC_Send_LUSERS(Client))
190 if (!IRC_Show_MOTD(Client))
193 /* Set default user modes */
194 if (Conf_DefaultUserModes[0]) {
195 snprintf(modes, sizeof(modes), "+%s", Conf_DefaultUserModes);
196 Req.prefix = Client_ThisServer();
197 Req.command = "MODE";
199 Req.argv[0] = Client_ID(Client);
201 IRC_MODE(Client, &Req);
203 IRC_SetPenalty(Client, 1);
211 * Read result of the authenticator sub-process from pipe
213 * @param r_fd File descriptor of the pipe.
214 * @param events (ignored IO specification)
217 cb_Read_Auth_Result(int r_fd, UNUSED short events)
219 char user[CLIENT_USER_LEN], *ptr;
226 LogDebug("Auth: Got callback on fd %d, events %d", r_fd, events);
227 conn = Conn_GetFromProc(r_fd);
229 /* Ops, none found? Probably the connection has already
230 * been closed!? We'll ignore that ... */
232 LogDebug("Auth: Got callback for unknown connection!?");
235 proc = Conn_GetProcStat(conn);
236 client = Conn_GetClient(conn);
238 /* Read result from pipe */
239 len = Proc_Read(proc, &result, sizeof(result));
244 if (len != sizeof(result)) {
245 Log(LOG_CRIT, "Auth: Got malformed result!");
246 Client_Reject(client, "Internal error", false);
250 if (result == true) {
251 /* Authentication succeeded, now set the correct user name
252 * supplied by the client (without prepended '~' for exmaple),
253 * but cut it at the first '@' character: */
254 strlcpy(user, Client_OrigUser(client), sizeof(user));
255 ptr = strchr(user, '@');
258 Client_SetUser(client, user, true);
259 (void)Login_User_PostAuth(client);
261 Client_Reject(client, "Bad password", false);