ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/auth.c
Revision: 8339
Committed: Sat Mar 3 22:47:06 2018 UTC (8 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 14974 byte(s)
Log Message:
- Restore fd_table. No longer allocate fde_t items from within any other structures like the AuthRequest, or Connection structure
- struct AuthRequest once again is no longer allocated from within the Connection structure

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 8279 * Copyright (c) 1997-2018 ircd-hybrid development team
5 adx 30 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 adx 30 * USA
20     */
21    
22 michael 3324 /*! \file auth.c
23 michael 8306 * \brief Implementation of DNS and ident lookups.
24 michael 2916 * \version $Id$
25     */
26    
27 adx 30 /*
28     * Changes:
29     * July 6, 1999 - Rewrote most of the code here. When a client connects
30     * to the server and passes initial socket validation checks, it
31     * is owned by this module (auth) which returns it to the rest of the
32     * server when dns and auth queries are finished. Until the client is
33     * released, the server does not know it exists and does not process
34     * any messages from it.
35     * --Bleep Thomas Helvey <tomh@inxpress.net>
36     */
37 michael 1309
38 adx 30 #include "stdinc.h"
39     #include "list.h"
40 michael 1011 #include "ircd_defs.h"
41     #include "fdlist.h"
42 michael 3324 #include "auth.h"
43 michael 1309 #include "conf.h"
44 adx 30 #include "client.h"
45     #include "event.h"
46     #include "irc_string.h"
47     #include "ircd.h"
48     #include "packet.h"
49 michael 3322 #include "res.h"
50 adx 30 #include "s_bsd.h"
51 michael 1309 #include "log.h"
52 adx 30 #include "send.h"
53 michael 1654 #include "mempool.h"
54 adx 30
55 michael 1011
56 michael 2916 enum
57     {
58 adx 30 REPORT_DO_DNS,
59     REPORT_FIN_DNS,
60     REPORT_FAIL_DNS,
61     REPORT_DO_ID,
62     REPORT_FIN_ID,
63     REPORT_FAIL_ID,
64     REPORT_IP_MISMATCH,
65 michael 8217 REPORT_HOST_TOOLONG,
66     REPORT_HOST_INVALID
67 adx 30 };
68    
69 michael 5888 static const char *const HeaderMessages[] =
70     {
71     [REPORT_DO_DNS] = ":*** Looking up your hostname",
72     [REPORT_FIN_DNS] = ":*** Found your hostname",
73     [REPORT_FAIL_DNS] = ":*** Couldn't look up your hostname",
74     [REPORT_DO_ID] = ":*** Checking Ident",
75     [REPORT_FIN_ID] = ":*** Got Ident response",
76     [REPORT_FAIL_ID] = ":*** No Ident response",
77     [REPORT_IP_MISMATCH] = ":*** Your forward and reverse DNS do not match, ignoring hostname",
78 michael 8217 [REPORT_HOST_TOOLONG] = ":*** Your hostname is too long, ignoring hostname",
79     [REPORT_HOST_INVALID] = ":*** Your hostname contains illegal characters, ignoring hostname"
80 michael 5888 };
81    
82 michael 8306 #define auth_sendheader(c, i) sendto_one_notice((c), &me, "%s", HeaderMessages[(i)])
83 adx 30
84 michael 8339 static mp_pool_t *auth_pool;
85 michael 6478 static dlink_list auth_list;
86 adx 30
87 michael 4094
88 adx 30 /*
89 michael 8306 * auth_make_request - allocate a new auth request
90 adx 30 */
91     static struct AuthRequest *
92 michael 8339 auth_make(struct Client *client)
93 adx 30 {
94 michael 8339 struct AuthRequest *auth = mp_pool_get(auth_pool);;
95 adx 30
96 michael 6690 auth->client = client;
97     auth->timeout = CurrentTime + CONNECTTIMEOUT;
98 michael 2181
99 michael 6690 return auth;
100 adx 30 }
101    
102 michael 8306 /*! \brief Release auth client from auth system. This adds the client into the
103     * local client lists so it can be read by the main io processing loop.
104     * \param auth Pointer to AuthRequest struct to release
105 adx 30 */
106 michael 8304 static void
107 michael 8306 auth_release_client(struct AuthRequest *auth)
108 adx 30 {
109 michael 4854 struct Client *const client = auth->client;
110 michael 992
111     if (IsDoingAuth(auth) || IsDNSPending(auth))
112     return;
113    
114 michael 8339 dlinkDelete(&auth->node, &auth_list);
115 michael 992
116 michael 8339 mp_pool_release(auth);
117     client->connection->auth = NULL;
118    
119 adx 30 /*
120     * When a client has auth'ed, we want to start reading what it sends
121     * us. This is what read_packet() does.
122     * -- adrian
123     */
124 michael 4588 client->connection->allow_read = MAX_FLOOD;
125 michael 8339 comm_setflush(client->connection->fd, 1000, flood_recalc, client);
126 michael 650
127 michael 4588 client->connection->since = CurrentTime;
128     client->connection->lasttime = CurrentTime;
129     client->connection->firsttime = CurrentTime;
130 michael 8298 AddFlag(client, FLAGS_FINISHED_AUTH);
131 michael 650
132 michael 8214 strlcpy(client->realhost, client->host, sizeof(client->realhost));
133    
134 michael 8339 read_packet(client->connection->fd, client);
135 adx 30 }
136 michael 2916
137 michael 8218 /*! Checks if a hostname is valid and doesn't contain illegal characters
138     * \param hostname The string to verify
139     * \return 1 if it is valid, 0 if it isn't
140     */
141 michael 8217 static int
142     auth_verify_hostname(const char *hostname)
143     {
144     const char *p = hostname;
145    
146     assert(p);
147    
148     if (EmptyString(p) || *p == '.' || *p == ':')
149     return 0;
150    
151     for (; *p; ++p)
152     if (!IsHostChar(*p))
153     return 0;
154    
155     return 1;
156     }
157    
158 michael 8306 /*! \brief Handle a complete DNS lookup. Send the client on its way to a connection
159     * completion, regardless of success or failure.
160     * \param vptr The pending struct AuthRequest.
161     * \param addr IP address being resolved.
162     * \param name Resolved name, or NULL if lookup failed.
163     * \param namelength String length of the resolved hostname pointed by 'name'
164 adx 30 */
165     static void
166 michael 4408 auth_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name, size_t namelength)
167 adx 30 {
168 michael 4854 struct AuthRequest *const auth = vptr;
169 adx 30
170     ClearDNSPending(auth);
171    
172 michael 4408 if (!EmptyString(name))
173 adx 30 {
174 michael 4588 if (auth->client->connection->ip.ss.ss_family == AF_INET6)
175 adx 30 {
176 michael 4858 const struct sockaddr_in6 *const v6 = (const struct sockaddr_in6 *)&auth->client->connection->ip;
177     const struct sockaddr_in6 *const v6dns = (const struct sockaddr_in6 *)addr;
178 michael 3250
179 michael 5902 if (memcmp(&v6->sin6_addr, &v6dns->sin6_addr, sizeof(struct in6_addr)))
180 adx 30 {
181 michael 8306 auth_sendheader(auth->client, REPORT_IP_MISMATCH);
182     auth_release_client(auth);
183 michael 4408 return;
184 adx 30 }
185     }
186     else
187     {
188 michael 4858 const struct sockaddr_in *const v4 = (const struct sockaddr_in *)&auth->client->connection->ip;
189     const struct sockaddr_in *const v4dns = (const struct sockaddr_in *)addr;
190 michael 3250
191     if (v4->sin_addr.s_addr != v4dns->sin_addr.s_addr)
192 adx 30 {
193 michael 8306 auth_sendheader(auth->client, REPORT_IP_MISMATCH);
194     auth_release_client(auth);
195 michael 4408 return;
196 adx 30 }
197     }
198 michael 3250
199 michael 4408 if (namelength > HOSTLEN)
200 michael 8306 auth_sendheader(auth->client, REPORT_HOST_TOOLONG);
201 michael 8217 else if (!auth_verify_hostname(name))
202 michael 8306 auth_sendheader(auth->client, REPORT_HOST_INVALID);
203 michael 4408 else
204 adx 30 {
205 michael 4408 strlcpy(auth->client->host, name, sizeof(auth->client->host));
206 michael 8306 auth_sendheader(auth->client, REPORT_FIN_DNS);
207 adx 30 }
208     }
209     else
210 michael 8306 auth_sendheader(auth->client, REPORT_FAIL_DNS);
211 adx 30
212 michael 8306 auth_release_client(auth);
213 adx 30 }
214    
215     /*
216 michael 8306 * auth_error - handle auth send errors
217 adx 30 */
218     static void
219     auth_error(struct AuthRequest *auth)
220     {
221 michael 896 ++ServerStats.is_abad;
222 adx 30
223 michael 8339 fd_close(auth->fd);
224     auth->fd = NULL;
225    
226 adx 30 ClearAuth(auth);
227    
228 michael 8306 auth_sendheader(auth->client, REPORT_FAIL_ID);
229 adx 30
230 michael 8306 auth_release_client(auth);
231 adx 30 }
232    
233 michael 4309 /** Enum used to index ident reply fields in a human-readable way. */
234     enum IdentReplyFields
235     {
236     IDENT_PORT_NUMBERS,
237     IDENT_REPLY_TYPE,
238     IDENT_OS_TYPE,
239     IDENT_INFO,
240     USERID_TOKEN_COUNT
241     };
242    
243     /** Parse an ident reply line and extract the userid from it.
244     * \param reply The ident reply line.
245     * \return The userid, or NULL on parse failure.
246     */
247     static const char *
248 michael 8306 auth_check_ident_reply(char *const reply)
249 michael 4309 {
250     char *token = NULL, *end = NULL;
251     char *vector[USERID_TOKEN_COUNT];
252 michael 7584 const unsigned int count = token_vector(reply, ':', vector, USERID_TOKEN_COUNT);
253 michael 4309
254     if (USERID_TOKEN_COUNT != count)
255     return NULL;
256    
257     /*
258     * Second token is the reply type
259     */
260     token = vector[IDENT_REPLY_TYPE];
261    
262     if (EmptyString(token))
263     return NULL;
264    
265     while (IsSpace(*token))
266     ++token;
267    
268     if (strncmp(token, "USERID", 6))
269     return NULL;
270    
271     /*
272     * Third token is the os type
273     */
274     token = vector[IDENT_OS_TYPE];
275    
276     if (EmptyString(token))
277     return NULL;
278    
279     while (IsSpace(*token))
280     ++token;
281    
282     /*
283     * Unless "OTHER" is specified as the operating system type, the server
284     * is expected to return the "normal" user identification of the owner
285     * of this connection. "Normal" in this context may be taken to mean a
286     * string of characters which uniquely identifies the connection owner
287     * such as a user identifier assigned by the system administrator and
288     * used by such user as a mail identifier, or as the "user" part of a
289     * user/password pair used to gain access to system resources. When an
290     * operating system is specified (e.g., anything but "OTHER"), the user
291     * identifier is expected to be in a more or less immediately useful
292     * form - e.g., something that could be used as an argument to "finger"
293     * or as a mail address.
294     */
295     if (!strncmp(token, "OTHER", 5))
296     return NULL;
297    
298     /*
299     * Fourth token is the username
300     */
301     token = vector[IDENT_INFO];
302    
303     if (EmptyString(token))
304     return NULL;
305    
306     while (IsSpace(*token))
307     ++token;
308    
309     while (*token == '~' || *token == '^')
310     ++token;
311    
312     /*
313     * Look for the end of the username, terminators are '\0, @, <SPACE>, :'
314     */
315     for (end = token; *end; ++end)
316     if (IsSpace(*end) || '@' == *end || ':' == *end)
317     break;
318     *end = '\0';
319    
320     return token;
321     }
322    
323 michael 8306 /*! \brief Read the reply (if any) from the ident server we connected to. We
324     * only give it one shot, if the reply isn't good the first time fail
325     * the authentication entirely. --Bleep
326     * \param fd The socket/fd to read from.
327     * \param data The request to read.
328 adx 30 */
329     static void
330 michael 7955 auth_read_reply(fde_t *fd, void *data)
331 adx 30 {
332 michael 4854 struct AuthRequest *const auth = data;
333 michael 4309 const char *username = NULL;
334     ssize_t len = 0;
335     char buf[RFC1413_BUFSIZ + 1];
336 adx 30
337 michael 8339 assert(fd == auth->fd);
338    
339     if ((len = recv(auth->fd->fd, buf, RFC1413_BUFSIZ, 0)) > 0)
340 adx 30 {
341     buf[len] = '\0';
342 michael 8306 username = auth_check_ident_reply(buf);
343 adx 30 }
344    
345 michael 8339 fd_close(auth->fd);
346     auth->fd = NULL;
347    
348 adx 30 ClearAuth(auth);
349    
350 michael 4309 if (EmptyString(username))
351 adx 30 {
352 michael 8306 auth_sendheader(auth->client, REPORT_FAIL_ID);
353 michael 896 ++ServerStats.is_abad;
354 adx 30 }
355     else
356     {
357 michael 4309 strlcpy(auth->client->username, username, sizeof(auth->client->username));
358 michael 8306 auth_sendheader(auth->client, REPORT_FIN_ID);
359 michael 896 ++ServerStats.is_asuc;
360 michael 6313 AddFlag(auth->client, FLAGS_GOTID);
361 adx 30 }
362    
363 michael 8306 auth_release_client(auth);
364 adx 30 }
365    
366     /*
367 michael 8306 * auth_connect_callback() - deal with the result of comm_connect_tcp()
368     *
369     * If the connection failed, we simply close the auth fd and report
370     * a failure. If the connection suceeded send the ident server a query
371     * giving "theirport , ourport". The write is only attempted *once* so
372     * it is deemed to be a fail if the entire write doesn't write all the
373     * data given. This shouldnt be a problem since the socket should have
374     * a write buffer far greater than this message to store it in should
375     * problems arise. -avalon
376     */
377     static void
378     auth_connect_callback(fde_t *fd, int error, void *data)
379     {
380     struct AuthRequest *const auth = data;
381     struct irc_ssaddr us;
382     struct irc_ssaddr them;
383     char authbuf[16];
384     ssize_t len = 0;
385     socklen_t ulen = sizeof(struct irc_ssaddr);
386     socklen_t tlen = sizeof(struct irc_ssaddr);
387     uint16_t uport, tport;
388     struct sockaddr_in6 *v6;
389    
390     if (error != COMM_OK)
391     {
392     auth_error(auth);
393     return;
394     }
395    
396 michael 8339 assert(fd == auth->fd);
397    
398     if (getsockname(auth->client->connection->fd->fd, (struct sockaddr *)&us, &ulen) ||
399     getpeername(auth->client->connection->fd->fd, (struct sockaddr *)&them, &tlen))
400 michael 8306 {
401     report_error(L_ALL, "auth get{sock,peer}name error %s:%s",
402     client_get_name(auth->client, SHOW_IP), errno);
403     auth_error(auth);
404     return;
405     }
406    
407     v6 = (struct sockaddr_in6 *)&us;
408     uport = ntohs(v6->sin6_port);
409     v6 = (struct sockaddr_in6 *)&them;
410     tport = ntohs(v6->sin6_port);
411    
412     len = snprintf(authbuf, sizeof(authbuf), "%u, %u\r\n", tport, uport);
413    
414     if (send(fd->fd, authbuf, len, 0) != len)
415     {
416     auth_error(auth);
417     return;
418     }
419    
420     comm_setselect(fd, COMM_SELECT_READ, auth_read_reply, auth, 0);
421     }
422    
423     /*! \brief Flag the client to show an attempt to contact the ident server on
424     * the client's host. Should the connect or any later phase of the
425     * identifying process fail, it is aborted and the user is given a
426     * username of "unknown".
427     * \param auth The request for which to start the ident lookup.
428     */
429     static void
430     auth_start_query(struct AuthRequest *auth)
431     {
432     struct irc_ssaddr localaddr;
433     socklen_t locallen = sizeof(struct irc_ssaddr);
434     struct sockaddr_in6 *v6;
435    
436     /* Open a socket of the same type as the client socket */
437 michael 8339 int fd = comm_socket(auth->client->connection->ip.ss.ss_family, SOCK_STREAM, 0);
438     if (fd == -1)
439 michael 8306 {
440     report_error(L_ALL, "creating auth stream socket %s:%s",
441     client_get_name(auth->client, SHOW_IP), errno);
442     ++ServerStats.is_abad;
443     return;
444     }
445    
446 michael 8339 auth->fd = fd_open(fd, 1, "ident");
447    
448 michael 8306 SetDoingAuth(auth);
449     auth_sendheader(auth->client, REPORT_DO_ID);
450    
451     /*
452     * Get the local address of the client and bind to that to
453     * make the auth request.
454     */
455     memset(&localaddr, 0, locallen);
456 michael 8339 getsockname(auth->client->connection->fd->fd, (struct sockaddr *)&localaddr, &locallen);
457 michael 8306
458     remove_ipv6_mapping(&localaddr);
459     v6 = (struct sockaddr_in6 *)&localaddr;
460     v6->sin6_port = htons(0);
461     localaddr.ss_port = htons(0);
462    
463 michael 8339 comm_connect_tcp(auth->fd, auth->client->sockhost, RFC1413_PORT,
464 michael 8306 (struct sockaddr *)&localaddr, localaddr.ss_len, auth_connect_callback,
465     auth, auth->client->connection->ip.ss.ss_family,
466     GlobalSetOptions.ident_timeout);
467     }
468    
469     /*
470     * auth_start
471     *
472     * inputs - pointer to client to auth
473     * output - NONE
474     * side effects - starts auth (identd) and dns queries for a client
475     */
476     void
477     auth_start(struct Client *client_p)
478     {
479 michael 8339 struct AuthRequest *auth = auth_make(client_p);
480 michael 8306
481     dlinkAddTail(auth, &auth->node, &auth_list);
482    
483     auth_sendheader(client_p, REPORT_DO_DNS);
484    
485     SetDNSPending(auth);
486    
487     if (ConfigGeneral.disable_auth == 0)
488     auth_start_query(auth);
489    
490     gethost_byaddr(auth_dns_callback, auth, &client_p->connection->ip);
491     }
492    
493     /*
494 michael 7955 * auth_delete()
495 adx 30 */
496 michael 2916 void
497 michael 7955 auth_delete(struct AuthRequest *auth)
498 adx 30 {
499 michael 8306 if (IsDoingAuth(auth))
500     {
501 michael 8339 fd_close(auth->fd);
502     auth->fd = NULL;
503 michael 8306 ClearAuth(auth);
504     }
505    
506 michael 992 if (IsDNSPending(auth))
507 michael 8300 {
508 michael 992 delete_resolver_queries(auth);
509 michael 8300 ClearDNSPending(auth);
510     }
511 adx 30
512 michael 8339 dlinkDelete(&auth->node, &auth_list);
513     mp_pool_release(auth);
514 adx 30 }
515 michael 4309
516 michael 8306 /*
517     * auth_timeout_queries - timeout resolver and identd requests
518     * allow clients through if requests failed
519     */
520     static void
521     auth_timeout_queries(void *notused)
522     {
523     dlink_node *node, *node_next;
524    
525     DLINK_FOREACH_SAFE(node, node_next, auth_list.head)
526     {
527     struct AuthRequest *auth = node->data;
528    
529     if (auth->timeout > CurrentTime)
530     break;
531    
532     if (IsDoingAuth(auth))
533     {
534     ++ServerStats.is_abad;
535 michael 8339 fd_close(auth->fd);
536     auth->fd = NULL;
537    
538 michael 8306 ClearAuth(auth);
539    
540     auth_sendheader(auth->client, REPORT_FAIL_ID);
541     }
542    
543     if (IsDNSPending(auth))
544     {
545     delete_resolver_queries(auth);
546     ClearDNSPending(auth);
547    
548     auth_sendheader(auth->client, REPORT_FAIL_DNS);
549     }
550    
551     auth_release_client(auth);
552     }
553     }
554    
555 michael 4309 /* auth_init
556     *
557     * Initialise the auth code
558     */
559     void
560     auth_init(void)
561     {
562     static struct event timeout_auth_queries =
563     {
564 michael 8306 .name = "auth_timeout_queries",
565     .handler = auth_timeout_queries,
566 michael 4309 .when = 1
567     };
568    
569 michael 8339 auth_pool = mp_pool_new(sizeof(struct AuthRequest), MP_CHUNK_SIZE_AUTH);
570 michael 4309 event_add(&timeout_auth_queries, NULL);
571     }

Properties

Name Value
svn:eol-style native
svn:keywords Id