ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/auth.c
Revision: 8721
Committed: Tue Dec 11 20:54:22 2018 UTC (7 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 15261 byte(s)
Log Message:
- auth: replace AM_DOING_AUTH and AM_DNS_PENDING with dedicated variables

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 8385 #include "memory.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 6478 static dlink_list auth_list;
85 adx 30
86 michael 4094
87 michael 8349 /*! \brief Allocate a new auth request.
88     * \param client The client being looked up.
89     * \return The newly allocated auth request.
90 adx 30 */
91     static struct AuthRequest *
92 michael 8339 auth_make(struct Client *client)
93 adx 30 {
94 michael 8385 struct AuthRequest *auth = xcalloc(sizeof(*auth));
95 adx 30
96 michael 6690 auth->client = client;
97 michael 8470 auth->client->connection->auth = auth;
98 michael 6690 auth->timeout = CurrentTime + CONNECTTIMEOUT;
99 michael 2181
100 michael 6690 return auth;
101 adx 30 }
102    
103 michael 8470 /*! \brief Unlink auth request from auth_list and free memory
104     * \param auth The allocated auth request to cleanup.
105     */
106     static void
107     auth_free(struct AuthRequest *auth)
108     {
109     assert(dlinkFind(&auth_list, auth));
110     dlinkDelete(&auth->node, &auth_list);
111     assert(dlinkFind(&auth_list, auth) == NULL);
112    
113     xfree(auth);
114     }
115    
116 michael 8306 /*! \brief Release auth client from auth system. This adds the client into the
117     * local client lists so it can be read by the main io processing loop.
118     * \param auth Pointer to AuthRequest struct to release
119 adx 30 */
120 michael 8304 static void
121 michael 8306 auth_release_client(struct AuthRequest *auth)
122 adx 30 {
123 michael 8470 struct Client *client = auth->client;
124 michael 992
125 michael 8721 if (auth->ident_pending == true || auth->dns_pending == true)
126 michael 992 return;
127    
128 michael 8470 auth_free(auth);
129 michael 8339 client->connection->auth = NULL;
130    
131 adx 30 /*
132     * When a client has auth'ed, we want to start reading what it sends
133     * us. This is what read_packet() does.
134     * -- adrian
135     */
136 michael 8339 comm_setflush(client->connection->fd, 1000, flood_recalc, client);
137 michael 650
138 michael 4588 client->connection->since = CurrentTime;
139     client->connection->lasttime = CurrentTime;
140     client->connection->firsttime = CurrentTime;
141 michael 8298 AddFlag(client, FLAGS_FINISHED_AUTH);
142 michael 650
143 michael 8214 strlcpy(client->realhost, client->host, sizeof(client->realhost));
144    
145 michael 8339 read_packet(client->connection->fd, client);
146 adx 30 }
147 michael 2916
148 michael 8218 /*! Checks if a hostname is valid and doesn't contain illegal characters
149     * \param hostname The string to verify
150     * \return 1 if it is valid, 0 if it isn't
151     */
152 michael 8656 static bool
153 michael 8217 auth_verify_hostname(const char *hostname)
154     {
155     const char *p = hostname;
156    
157     assert(p);
158    
159     if (EmptyString(p) || *p == '.' || *p == ':')
160 michael 8656 return false;
161 michael 8217
162     for (; *p; ++p)
163     if (!IsHostChar(*p))
164 michael 8656 return false;
165 michael 8217
166 michael 8656 return true;
167 michael 8217 }
168    
169 michael 8306 /*! \brief Handle a complete DNS lookup. Send the client on its way to a connection
170     * completion, regardless of success or failure.
171     * \param vptr The pending struct AuthRequest.
172     * \param addr IP address being resolved.
173     * \param name Resolved name, or NULL if lookup failed.
174     * \param namelength String length of the resolved hostname pointed by 'name'
175 adx 30 */
176     static void
177 michael 4408 auth_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name, size_t namelength)
178 adx 30 {
179 michael 4854 struct AuthRequest *const auth = vptr;
180 adx 30
181 michael 8721 auth->dns_pending = false;
182 adx 30
183 michael 8431 if (EmptyString(name))
184     auth_sendheader(auth->client, REPORT_FAIL_DNS);
185     else
186 adx 30 {
187 michael 8496 if (auth->client->ip.ss.ss_family == AF_INET6)
188 adx 30 {
189 michael 8496 const struct sockaddr_in6 *const v6 = (const struct sockaddr_in6 *)&auth->client->ip;
190 michael 4858 const struct sockaddr_in6 *const v6dns = (const struct sockaddr_in6 *)addr;
191 michael 3250
192 michael 5902 if (memcmp(&v6->sin6_addr, &v6dns->sin6_addr, sizeof(struct in6_addr)))
193 adx 30 {
194 michael 8306 auth_sendheader(auth->client, REPORT_IP_MISMATCH);
195     auth_release_client(auth);
196 michael 4408 return;
197 adx 30 }
198     }
199     else
200     {
201 michael 8496 const struct sockaddr_in *const v4 = (const struct sockaddr_in *)&auth->client->ip;
202 michael 4858 const struct sockaddr_in *const v4dns = (const struct sockaddr_in *)addr;
203 michael 3250
204     if (v4->sin_addr.s_addr != v4dns->sin_addr.s_addr)
205 adx 30 {
206 michael 8306 auth_sendheader(auth->client, REPORT_IP_MISMATCH);
207     auth_release_client(auth);
208 michael 4408 return;
209 adx 30 }
210     }
211 michael 3250
212 michael 4408 if (namelength > HOSTLEN)
213 michael 8306 auth_sendheader(auth->client, REPORT_HOST_TOOLONG);
214 michael 8656 else if (auth_verify_hostname(name) == false)
215 michael 8306 auth_sendheader(auth->client, REPORT_HOST_INVALID);
216 michael 4408 else
217 adx 30 {
218 michael 4408 strlcpy(auth->client->host, name, sizeof(auth->client->host));
219 michael 8306 auth_sendheader(auth->client, REPORT_FIN_DNS);
220 adx 30 }
221     }
222    
223 michael 8306 auth_release_client(auth);
224 adx 30 }
225    
226     /*
227 michael 8306 * auth_error - handle auth send errors
228 adx 30 */
229     static void
230     auth_error(struct AuthRequest *auth)
231     {
232 michael 8524 assert(auth);
233     assert(auth->fd);
234    
235 michael 896 ++ServerStats.is_abad;
236 adx 30
237 michael 8339 fd_close(auth->fd);
238     auth->fd = NULL;
239 michael 8721 auth->ident_pending = false;
240 michael 8339
241 michael 8306 auth_sendheader(auth->client, REPORT_FAIL_ID);
242 adx 30
243 michael 8306 auth_release_client(auth);
244 adx 30 }
245    
246 michael 4309 /** Enum used to index ident reply fields in a human-readable way. */
247     enum IdentReplyFields
248     {
249     IDENT_PORT_NUMBERS,
250     IDENT_REPLY_TYPE,
251     IDENT_OS_TYPE,
252     IDENT_INFO,
253     USERID_TOKEN_COUNT
254     };
255    
256     /** Parse an ident reply line and extract the userid from it.
257     * \param reply The ident reply line.
258     * \return The userid, or NULL on parse failure.
259     */
260     static const char *
261 michael 8306 auth_check_ident_reply(char *const reply)
262 michael 4309 {
263     char *token = NULL, *end = NULL;
264     char *vector[USERID_TOKEN_COUNT];
265 michael 7584 const unsigned int count = token_vector(reply, ':', vector, USERID_TOKEN_COUNT);
266 michael 4309
267     if (USERID_TOKEN_COUNT != count)
268     return NULL;
269    
270     /*
271     * Second token is the reply type
272     */
273     token = vector[IDENT_REPLY_TYPE];
274    
275     if (EmptyString(token))
276     return NULL;
277    
278     while (IsSpace(*token))
279     ++token;
280    
281     if (strncmp(token, "USERID", 6))
282     return NULL;
283    
284     /*
285     * Third token is the os type
286     */
287     token = vector[IDENT_OS_TYPE];
288    
289     if (EmptyString(token))
290     return NULL;
291    
292     while (IsSpace(*token))
293     ++token;
294    
295     /*
296     * Unless "OTHER" is specified as the operating system type, the server
297     * is expected to return the "normal" user identification of the owner
298     * of this connection. "Normal" in this context may be taken to mean a
299     * string of characters which uniquely identifies the connection owner
300     * such as a user identifier assigned by the system administrator and
301     * used by such user as a mail identifier, or as the "user" part of a
302     * user/password pair used to gain access to system resources. When an
303     * operating system is specified (e.g., anything but "OTHER"), the user
304     * identifier is expected to be in a more or less immediately useful
305     * form - e.g., something that could be used as an argument to "finger"
306     * or as a mail address.
307     */
308 michael 8620 if (strncmp(token, "OTHER", 5) == 0)
309 michael 4309 return NULL;
310    
311     /*
312     * Fourth token is the username
313     */
314     token = vector[IDENT_INFO];
315    
316     if (EmptyString(token))
317     return NULL;
318    
319     while (IsSpace(*token))
320     ++token;
321    
322     while (*token == '~' || *token == '^')
323     ++token;
324    
325     /*
326     * Look for the end of the username, terminators are '\0, @, <SPACE>, :'
327     */
328     for (end = token; *end; ++end)
329     if (IsSpace(*end) || '@' == *end || ':' == *end)
330     break;
331     *end = '\0';
332    
333     return token;
334     }
335    
336 michael 8306 /*! \brief Read the reply (if any) from the ident server we connected to. We
337     * only give it one shot, if the reply isn't good the first time fail
338     * the authentication entirely. --Bleep
339 michael 8349 * \param F The socket/fd to read from.
340 michael 8306 * \param data The request to read.
341 adx 30 */
342     static void
343 michael 8349 auth_read_reply(fde_t *F, void *data)
344 adx 30 {
345 michael 4854 struct AuthRequest *const auth = data;
346 michael 4309 const char *username = NULL;
347     ssize_t len = 0;
348     char buf[RFC1413_BUFSIZ + 1];
349 adx 30
350 michael 8349 assert(auth->fd == F);
351 michael 8339
352     if ((len = recv(auth->fd->fd, buf, RFC1413_BUFSIZ, 0)) > 0)
353 adx 30 {
354     buf[len] = '\0';
355 michael 8306 username = auth_check_ident_reply(buf);
356 adx 30 }
357    
358 michael 8339 fd_close(auth->fd);
359     auth->fd = NULL;
360 michael 8721 auth->ident_pending = false;
361 michael 8339
362 michael 4309 if (EmptyString(username))
363 adx 30 {
364 michael 8306 auth_sendheader(auth->client, REPORT_FAIL_ID);
365 michael 896 ++ServerStats.is_abad;
366 adx 30 }
367     else
368     {
369 michael 4309 strlcpy(auth->client->username, username, sizeof(auth->client->username));
370 michael 8306 auth_sendheader(auth->client, REPORT_FIN_ID);
371 michael 896 ++ServerStats.is_asuc;
372 michael 6313 AddFlag(auth->client, FLAGS_GOTID);
373 adx 30 }
374    
375 michael 8306 auth_release_client(auth);
376 adx 30 }
377    
378     /*
379 michael 8306 * auth_connect_callback() - deal with the result of comm_connect_tcp()
380     *
381     * If the connection failed, we simply close the auth fd and report
382     * a failure. If the connection suceeded send the ident server a query
383     * giving "theirport , ourport". The write is only attempted *once* so
384     * it is deemed to be a fail if the entire write doesn't write all the
385     * data given. This shouldnt be a problem since the socket should have
386     * a write buffer far greater than this message to store it in should
387     * problems arise. -avalon
388     */
389     static void
390 michael 8349 auth_connect_callback(fde_t *F, int error, void *data)
391 michael 8306 {
392     struct AuthRequest *const auth = data;
393     struct irc_ssaddr us;
394     struct irc_ssaddr them;
395     char authbuf[16];
396     ssize_t len = 0;
397     socklen_t ulen = sizeof(struct irc_ssaddr);
398     socklen_t tlen = sizeof(struct irc_ssaddr);
399     uint16_t uport, tport;
400     struct sockaddr_in6 *v6;
401    
402     if (error != COMM_OK)
403     {
404     auth_error(auth);
405     return;
406     }
407    
408 michael 8349 assert(auth->fd == F);
409 michael 8339
410     if (getsockname(auth->client->connection->fd->fd, (struct sockaddr *)&us, &ulen) ||
411     getpeername(auth->client->connection->fd->fd, (struct sockaddr *)&them, &tlen))
412 michael 8306 {
413     report_error(L_ALL, "auth get{sock,peer}name error %s:%s",
414     client_get_name(auth->client, SHOW_IP), errno);
415     auth_error(auth);
416     return;
417     }
418    
419     v6 = (struct sockaddr_in6 *)&us;
420     uport = ntohs(v6->sin6_port);
421     v6 = (struct sockaddr_in6 *)&them;
422     tport = ntohs(v6->sin6_port);
423    
424     len = snprintf(authbuf, sizeof(authbuf), "%u, %u\r\n", tport, uport);
425    
426 michael 8349 if (send(F->fd, authbuf, len, 0) != len)
427 michael 8306 {
428     auth_error(auth);
429     return;
430     }
431    
432 michael 8349 comm_setselect(F, COMM_SELECT_READ, auth_read_reply, auth, 0);
433 michael 8306 }
434    
435     /*! \brief Flag the client to show an attempt to contact the ident server on
436     * the client's host. Should the connect or any later phase of the
437     * identifying process fail, it is aborted and the user is given a
438     * username of "unknown".
439     * \param auth The request for which to start the ident lookup.
440     */
441     static void
442     auth_start_query(struct AuthRequest *auth)
443     {
444     struct irc_ssaddr localaddr;
445     socklen_t locallen = sizeof(struct irc_ssaddr);
446     struct sockaddr_in6 *v6;
447    
448     /* Open a socket of the same type as the client socket */
449 michael 8496 int fd = comm_socket(auth->client->ip.ss.ss_family, SOCK_STREAM, 0);
450 michael 8339 if (fd == -1)
451 michael 8306 {
452     report_error(L_ALL, "creating auth stream socket %s:%s",
453     client_get_name(auth->client, SHOW_IP), errno);
454     ++ServerStats.is_abad;
455     return;
456     }
457    
458 michael 8658 auth->fd = fd_open(fd, true, "ident");
459 michael 8721 auth->ident_pending = true;
460 michael 8339
461 michael 8306 auth_sendheader(auth->client, REPORT_DO_ID);
462    
463     /*
464     * Get the local address of the client and bind to that to
465     * make the auth request.
466     */
467     memset(&localaddr, 0, locallen);
468 michael 8339 getsockname(auth->client->connection->fd->fd, (struct sockaddr *)&localaddr, &locallen);
469 michael 8306
470     remove_ipv6_mapping(&localaddr);
471     v6 = (struct sockaddr_in6 *)&localaddr;
472     v6->sin6_port = htons(0);
473     localaddr.ss_port = htons(0);
474    
475 michael 8339 comm_connect_tcp(auth->fd, auth->client->sockhost, RFC1413_PORT,
476 michael 8306 (struct sockaddr *)&localaddr, localaddr.ss_len, auth_connect_callback,
477 michael 8496 auth, auth->client->ip.ss.ss_family,
478 michael 8306 GlobalSetOptions.ident_timeout);
479     }
480    
481     /*
482     * auth_start
483     *
484     * inputs - pointer to client to auth
485     * output - NONE
486     * side effects - starts auth (identd) and dns queries for a client
487     */
488     void
489     auth_start(struct Client *client_p)
490     {
491 michael 8339 struct AuthRequest *auth = auth_make(client_p);
492 michael 8306
493     dlinkAddTail(auth, &auth->node, &auth_list);
494    
495     auth_sendheader(client_p, REPORT_DO_DNS);
496    
497 michael 8721 auth->dns_pending = true;
498 michael 8306
499     if (ConfigGeneral.disable_auth == 0)
500     auth_start_query(auth);
501    
502 michael 8496 gethost_byaddr(auth_dns_callback, auth, &client_p->ip);
503 michael 8306 }
504    
505     /*
506 michael 7955 * auth_delete()
507 adx 30 */
508 michael 2916 void
509 michael 7955 auth_delete(struct AuthRequest *auth)
510 adx 30 {
511 michael 8721 if (auth->ident_pending == true)
512 michael 8306 {
513 michael 8339 fd_close(auth->fd);
514     auth->fd = NULL;
515 michael 8721 auth->ident_pending = false;
516 michael 8306 }
517    
518 michael 8721 if (auth->dns_pending == true)
519 michael 8300 {
520 michael 992 delete_resolver_queries(auth);
521 michael 8721 auth->dns_pending = false;
522 michael 8300 }
523 adx 30
524 michael 8470 auth_free(auth);
525 adx 30 }
526 michael 4309
527 michael 8306 /*
528     * auth_timeout_queries - timeout resolver and identd requests
529     * allow clients through if requests failed
530     */
531     static void
532     auth_timeout_queries(void *notused)
533     {
534     dlink_node *node, *node_next;
535    
536     DLINK_FOREACH_SAFE(node, node_next, auth_list.head)
537     {
538     struct AuthRequest *auth = node->data;
539    
540     if (auth->timeout > CurrentTime)
541     break;
542    
543 michael 8721 if (auth->ident_pending == true)
544 michael 8306 {
545     ++ServerStats.is_abad;
546 michael 8349
547 michael 8339 fd_close(auth->fd);
548     auth->fd = NULL;
549 michael 8721 auth->ident_pending = false;
550 michael 8339
551 michael 8306 auth_sendheader(auth->client, REPORT_FAIL_ID);
552     }
553    
554 michael 8721 if (auth->dns_pending == true)
555 michael 8306 {
556     delete_resolver_queries(auth);
557 michael 8721 auth->dns_pending = false;
558 michael 8306
559     auth_sendheader(auth->client, REPORT_FAIL_DNS);
560     }
561    
562     auth_release_client(auth);
563     }
564     }
565    
566 michael 4309 /* auth_init
567     *
568     * Initialise the auth code
569     */
570     void
571     auth_init(void)
572     {
573     static struct event timeout_auth_queries =
574     {
575 michael 8306 .name = "auth_timeout_queries",
576     .handler = auth_timeout_queries,
577 michael 4309 .when = 1
578     };
579    
580     event_add(&timeout_auth_queries, NULL);
581     }

Properties

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