]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/login.c
Login_User(): use "conn" insted of calling Client_Conn(Client)
[ngircd-alex.git] / src / ngircd / login.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2012 Alexander Barton (alex@barton.de) and Contributors.
4  *
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.
10  */
11
12 #include "portab.h"
13
14 /**
15  * @file
16  * Functions to deal with client logins
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <strings.h>
24 #include <unistd.h>
25
26 #include "defines.h"
27 #include "conn.h"
28 #include "class.h"
29 #include "client.h"
30 #include "client-cap.h"
31 #include "channel.h"
32 #include "conf.h"
33 #include "io.h"
34 #include "parse.h"
35 #include "log.h"
36 #include "messages.h"
37 #include "ngircd.h"
38 #include "pam.h"
39 #include "irc-info.h"
40 #include "irc-write.h"
41
42 #include "exp.h"
43 #include "login.h"
44
45 #ifdef PAM
46 static void cb_Read_Auth_Result PARAMS((int r_fd, UNUSED short events));
47 #endif
48
49 /**
50  * Initiate client login.
51  *
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.
56  *
57  * @param Client The client logging in.
58  * @returns CONNECTED or DISCONNECTED.
59  */
60 GLOBAL bool
61 Login_User(CLIENT * Client)
62 {
63 #ifdef PAM
64         int pipefd[2], result;
65         pid_t pid;
66 #endif
67         CONN_ID conn;
68
69         assert(Client != NULL);
70         conn = Client_Conn(Client);
71
72 #ifndef STRICT_RFC
73         if (Conf_AuthPing) {
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);
78                         return CONNECTED;
79                 }
80         }
81 #endif
82
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);
87                 return CONNECTED;
88         }
89
90 #ifdef PAM
91         if (!Conf_PAM) {
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(Conn_Password(conn)[0] == '\0')
97                         return Login_User_PostAuth(Client);
98                 Client_Reject(Client, "Non-empty password", false);
99                 return DISCONNECTED;
100         }
101
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);
110         }
111
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);
116         if (pid > 0) {
117                 LogDebug("Authenticator for connection %d created (PID %d).",
118                          conn, pid);
119                 return CONNECTED;
120         } else {
121                 /* Sub process */
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");
129                 exit(0);
130         }
131 #else
132         /* Check global server password ... */
133         if (strcmp(Conn_Password(conn), Conf_ServerPwd) != 0) {
134                 /* Bad password! */
135                 Client_Reject(Client, "Bad server password", false);
136                 return DISCONNECTED;
137         }
138         return Login_User_PostAuth(Client);
139 #endif
140 }
141
142 /**
143  * Finish client registration.
144  *
145  * Introduce the new client to the network and send all "hello messages"
146  * to it after authentication has been succeeded.
147  *
148  * @param Client The client logging in.
149  * @return CONNECTED or DISCONNECTED.
150  */
151 GLOBAL bool
152 Login_User_PostAuth(CLIENT *Client)
153 {
154         assert(Client != NULL);
155
156         if (Class_HandleServerBans(Client) != CONNECTED)
157                 return DISCONNECTED;
158
159         Client_Introduce(NULL, Client, CLIENT_USER);
160
161         if (!IRC_WriteStrClient
162             (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
163                 return false;
164         if (!IRC_WriteStrClient
165             (Client, RPL_YOURHOST_MSG, Client_ID(Client),
166              Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
167              TARGET_VENDOR, TARGET_OS))
168                 return false;
169         if (!IRC_WriteStrClient
170             (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
171                 return false;
172         if (!IRC_WriteStrClient
173             (Client, RPL_MYINFO_MSG, Client_ID(Client),
174              Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
175              CHANMODES))
176                 return false;
177
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))
181                 return DISCONNECTED;
182
183         if (!IRC_Send_LUSERS(Client))
184                 return DISCONNECTED;
185         if (!IRC_Show_MOTD(Client))
186                 return DISCONNECTED;
187
188         /* Suspend the client for a second ... */
189         IRC_SetPenalty(Client, 1);
190
191         return CONNECTED;
192 }
193
194 #ifdef PAM
195
196 /**
197  * Read result of the authenticatior sub-process from pipe
198  *
199  * @param r_fd          File descriptor of the pipe.
200  * @param events        (ignored IO specification)
201  */
202 static void
203 cb_Read_Auth_Result(int r_fd, UNUSED short events)
204 {
205         CONN_ID conn;
206         CLIENT *client;
207         int result;
208         size_t len;
209         PROC_STAT *proc;
210
211         LogDebug("Auth: Got callback on fd %d, events %d", r_fd, events);
212         conn = Conn_GetFromProc(r_fd);
213         if (conn == NONE) {
214                 /* Ops, none found? Probably the connection has already
215                  * been closed!? We'll ignore that ... */
216                 io_close(r_fd);
217                 LogDebug("Auth: Got callback for unknown connection!?");
218                 return;
219         }
220         proc = Conn_GetProcStat(conn);
221         client = Conn_GetClient(conn);
222
223         /* Read result from pipe */
224         len = Proc_Read(proc, &result, sizeof(result));
225         Proc_Close(proc);
226         if (len == 0)
227                 return;
228
229         if (len != sizeof(result)) {
230                 Log(LOG_CRIT, "Auth: Got malformed result!");
231                 Client_Reject(client, "Internal error", false);
232                 return;
233         }
234
235         if (result == true) {
236                 Client_SetUser(client, Client_OrigUser(client), true);
237                 (void)Login_User_PostAuth(client);
238         } else
239                 Client_Reject(client, "Bad password", false);
240 }
241
242 #endif
243
244 /* -eof- */