ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/auth.c
Revision: 4853
Committed: Thu Nov 6 17:39:08 2014 UTC (9 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 14030 byte(s)
Log Message:
- auth.c: constification

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

Properties

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