ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/auth.c
Revision: 8298
Committed: Sun Feb 25 11:15:47 2018 UTC (8 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 14575 byte(s)
Log Message:
- auth.c:release_auth_client(): use the AddFlag macro

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

Properties

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