]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/login.c
Implement core IRC capability handling and "CAP" command
[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 <strings.h>
23 #include <unistd.h>
24
25 #include "defines.h"
26 #include "conn.h"
27 #include "class.h"
28 #include "client.h"
29 #include "client-cap.h"
30 #include "channel.h"
31 #include "conf.h"
32 #include "io.h"
33 #include "parse.h"
34 #include "log.h"
35 #include "messages.h"
36 #include "ngircd.h"
37 #include "pam.h"
38 #include "irc-info.h"
39 #include "irc-write.h"
40
41 #include "exp.h"
42 #include "login.h"
43
44 #ifdef PAM
45 static void cb_Read_Auth_Result PARAMS((int r_fd, UNUSED short events));
46 #endif
47
48 /**
49  * Initiate client login.
50  *
51  * This function is called after the daemon received the required NICK and
52  * USER commands of a new client. If the daemon is compiled with support for
53  * PAM, the authentication sub-processs is forked; otherwise the global server
54  * password is checked.
55  *
56  * @param Client The client logging in.
57  * @returns CONNECTED or DISCONNECTED.
58  */
59 GLOBAL bool
60 Login_User(CLIENT * Client)
61 {
62 #ifdef PAM
63         int pipefd[2], result;
64         pid_t pid;
65 #endif
66         CONN_ID conn;
67
68         assert(Client != NULL);
69         conn = Client_Conn(Client);
70
71 #ifndef STRICT_RFC
72         if (Conf_AuthPing) {
73                 /* Did we receive the "auth PONG" already? */
74                 if (Conn_GetAuthPing(conn)) {
75                         Client_SetType(Client, CLIENT_WAITAUTHPING);
76                         LogDebug("Connection %d: Waiting for AUTH PONG ...", conn);
77                         return CONNECTED;
78                 }
79         }
80 #endif
81
82         /* Still waiting for "CAP END" command? */
83         if (Client_Cap(Client) & CLIENT_CAP_PENDING)
84                 return CONNECTED;
85
86 #ifdef PAM
87         if (!Conf_PAM) {
88                 /* Don't do any PAM authentication at all, instead emulate
89                  * the beahiour of the daemon compiled without PAM support:
90                  * because there can't be any "server password", all
91                  * passwords supplied are classified as "wrong". */
92                 if(Client_Password(Client)[0] == '\0')
93                         return Login_User_PostAuth(Client);
94                 Client_Reject(Client, "Non-empty password", false);
95                 return DISCONNECTED;
96         }
97
98         if (Conf_PAMIsOptional && strcmp(Client_Password(Client), "") == 0) {
99                 /* Clients are not required to send a password and to be PAM-
100                  * authenticated at all. If not, they won't become "identified"
101                  * and keep the "~" in their supplied user name.
102                  * Therefore it is sensible to either set Conf_PAMisOptional or
103                  * to enable IDENT lookups -- not both. */
104                 return Login_User_PostAuth(Client);
105         }
106
107         /* Fork child process for PAM authentication; and make sure that the
108          * process timeout is set higher than the login timeout! */
109         pid = Proc_Fork(Conn_GetProcStat(conn), pipefd,
110                         cb_Read_Auth_Result, Conf_PongTimeout + 1);
111         if (pid > 0) {
112                 LogDebug("Authenticator for connection %d created (PID %d).",
113                          conn, pid);
114                 return CONNECTED;
115         } else {
116                 /* Sub process */
117                 Log_Init_Subprocess("Auth");
118                 Conn_CloseAllSockets(NONE);
119                 result = PAM_Authenticate(Client);
120                 if (write(pipefd[1], &result, sizeof(result)) != sizeof(result))
121                         Log_Subprocess(LOG_ERR,
122                                        "Failed to pipe result to parent!");
123                 Log_Exit_Subprocess("Auth");
124                 exit(0);
125         }
126 #else
127         /* Check global server password ... */
128         if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
129                 /* Bad password! */
130                 Client_Reject(Client, "Bad server password", false);
131                 return DISCONNECTED;
132         }
133         return Login_User_PostAuth(Client);
134 #endif
135 }
136
137 /**
138  * Finish client registration.
139  *
140  * Introduce the new client to the network and send all "hello messages"
141  * to it after authentication has been succeeded.
142  *
143  * @param Client The client logging in.
144  * @return CONNECTED or DISCONNECTED.
145  */
146 GLOBAL bool
147 Login_User_PostAuth(CLIENT *Client)
148 {
149         assert(Client != NULL);
150
151         if (Class_HandleServerBans(Client) != CONNECTED)
152                 return DISCONNECTED;
153
154         Client_Introduce(NULL, Client, CLIENT_USER);
155
156         if (!IRC_WriteStrClient
157             (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
158                 return false;
159         if (!IRC_WriteStrClient
160             (Client, RPL_YOURHOST_MSG, Client_ID(Client),
161              Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
162              TARGET_VENDOR, TARGET_OS))
163                 return false;
164         if (!IRC_WriteStrClient
165             (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
166                 return false;
167         if (!IRC_WriteStrClient
168             (Client, RPL_MYINFO_MSG, Client_ID(Client),
169              Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
170              CHANMODES))
171                 return false;
172
173         /* Features supported by this server (005 numeric, ISUPPORT),
174          * see <http://www.irc.org/tech_docs/005.html> for details. */
175         if (!IRC_Send_ISUPPORT(Client))
176                 return DISCONNECTED;
177
178         if (!IRC_Send_LUSERS(Client))
179                 return DISCONNECTED;
180         if (!IRC_Show_MOTD(Client))
181                 return DISCONNECTED;
182
183         /* Suspend the client for a second ... */
184         IRC_SetPenalty(Client, 1);
185
186         return CONNECTED;
187 }
188
189 #ifdef PAM
190
191 /**
192  * Read result of the authenticatior sub-process from pipe
193  *
194  * @param r_fd          File descriptor of the pipe.
195  * @param events        (ignored IO specification)
196  */
197 static void
198 cb_Read_Auth_Result(int r_fd, UNUSED short events)
199 {
200         CONN_ID conn;
201         CLIENT *client;
202         int result;
203         size_t len;
204         PROC_STAT *proc;
205
206         LogDebug("Auth: Got callback on fd %d, events %d", r_fd, events);
207         conn = Conn_GetFromProc(r_fd);
208         if (conn == NONE) {
209                 /* Ops, none found? Probably the connection has already
210                  * been closed!? We'll ignore that ... */
211                 io_close(r_fd);
212                 LogDebug("Auth: Got callback for unknown connection!?");
213                 return;
214         }
215         proc = Conn_GetProcStat(conn);
216         client = Conn_GetClient(conn);
217
218         /* Read result from pipe */
219         len = Proc_Read(proc, &result, sizeof(result));
220         Proc_Close(proc);
221         if (len == 0)
222                 return;
223
224         if (len != sizeof(result)) {
225                 Log(LOG_CRIT, "Auth: Got malformed result!");
226                 Client_Reject(client, "Internal error", false);
227                 return;
228         }
229
230         if (result == true) {
231                 Client_SetUser(client, Client_OrigUser(client), true);
232                 (void)Login_User_PostAuth(client);
233         } else
234                 Client_Reject(client, "Bad password", false);
235 }
236
237 #endif
238
239 /* -eof- */