1 /************************************************************************
2 * IRC - Internet Relay Chat, src/s_auth.c
3 * Copyright (C) 1992 Darren Reed
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 1, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * July 6, 1999 - Rewrote most of the code here. When a client connects
23 * to the server and passes initial socket validation checks, it
24 * is owned by this module (auth) which returns it to the rest of the
25 * server when dns and auth queries are finished. Until the client is
26 * released, the server does not know it exists and does not process
27 * any messages from it.
28 * --Bleep Thomas Helvey <tomh@inxpress.net>
36 #include "ircd_alloc.h"
37 #include "ircd_chattr.h"
38 #include "ircd_events.h"
39 #include "ircd_features.h"
41 #include "ircd_osdep.h"
42 #include "ircd_snprintf.h"
43 #include "ircd_string.h"
46 #include "querycmds.h"
53 #include "sys.h" /* TRUE bleah */
55 #include <arpa/inet.h> /* inet_netof */
56 #include <netdb.h> /* struct hostent */
63 #include <sys/socket.h>
65 #include <sys/ioctl.h>
68 * a bit different approach
69 * this replaces the original sendheader macros
74 } HeaderMessages [] = {
75 /* 123456789012345678901234567890123456789012345678901234567890 */
76 { "NOTICE AUTH :*** Looking up your hostname\r\n", 43 },
77 { "NOTICE AUTH :*** Found your hostname\r\n", 38 },
78 { "NOTICE AUTH :*** Found your hostname, cached\r\n", 46 },
79 { "NOTICE AUTH :*** Couldn't look up your hostname\r\n", 49 },
80 { "NOTICE AUTH :*** Checking Ident\r\n", 33 },
81 { "NOTICE AUTH :*** Got ident response\r\n", 37 },
82 { "NOTICE AUTH :*** No ident response\r\n", 36 },
83 { "NOTICE AUTH :*** Your forward and reverse DNS do not match, "
84 "ignoring hostname.\r\n", 80 },
85 {"NOTICE AUTH :*** Invalid hostname\r\n", 35 }
100 #define sendheader(c, r) \
101 send(cli_fd(c), HeaderMessages[(r)].message, HeaderMessages[(r)].length, 0)
103 struct AuthRequest* AuthPollList = 0; /* GLOBAL - auth queries pending io */
104 static struct AuthRequest* AuthIncompleteList = 0;
106 static void release_auth_client(struct Client* client);
107 static void unlink_auth_request(struct AuthRequest* request,
108 struct AuthRequest** list);
109 void free_auth_request(struct AuthRequest* auth);
111 /* auth_verify_hostname - verify that a hostname is valid, i.e., only
112 * contains characters valid for a hostname and that a hostname is not
116 auth_verify_hostname(char *host, int maxlen)
120 /* Walk through the host name */
121 for (i = 0; host[i]; i++)
122 /* If it's not a hostname character or if it's too long, return false */
123 if (!IsHostChar(host[i]) || i >= maxlen)
126 return 1; /* it's a valid hostname */
130 * auth_timeout - timeout a given auth request
132 static void auth_timeout_callback(struct Event* ev)
134 struct AuthRequest* auth;
136 assert(0 != ev_timer(ev));
137 assert(0 != t_data(ev_timer(ev)));
139 auth = (struct AuthRequest*) t_data(ev_timer(ev));
141 if (ev_type(ev) == ET_DESTROY) { /* being destroyed */
142 auth->flags &= ~AM_TIMEOUT;
144 if (!(auth->flags & AM_FREE_MASK)) {
145 Debug((DEBUG_LIST, "Freeing auth from timeout callback; %p [%p]", auth,
147 MyFree(auth); /* done with it, finally */
150 assert(ev_type(ev) == ET_EXPIRE);
152 destroy_auth_request(auth, 1);
157 * auth_sock_callback - called when an event occurs on the socket
159 static void auth_sock_callback(struct Event* ev)
161 struct AuthRequest* auth;
163 assert(0 != ev_socket(ev));
164 assert(0 != s_data(ev_socket(ev)));
166 auth = (struct AuthRequest*) s_data(ev_socket(ev));
168 switch (ev_type(ev)) {
169 case ET_DESTROY: /* being destroyed */
170 auth->flags &= ~AM_SOCKET;
172 if (!(auth->flags & AM_FREE_MASK)) {
173 Debug((DEBUG_LIST, "Freeing auth from sock callback; %p [%p]", auth,
175 MyFree(auth); /* done with it finally */
179 case ET_CONNECT: /* socket connection completed */
180 Debug((DEBUG_LIST, "Connection completed for auth %p [%p]; sending query",
181 auth, ev_socket(ev)));
182 socket_state(&auth->socket, SS_CONNECTED);
183 send_auth_query(auth);
186 case ET_READ: /* socket is readable */
187 case ET_EOF: /* end of file on socket */
188 case ET_ERROR: /* error on socket */
189 Debug((DEBUG_LIST, "Auth socket %p [%p] readable", auth, ev_socket(ev)));
190 read_auth_reply(auth);
195 abort(); /* unrecognized event */
202 * destroy_auth_request - stop an auth request completely
204 void destroy_auth_request(struct AuthRequest* auth, int send_reports)
206 struct AuthRequest** authList;
208 if (IsDoingAuth(auth)) {
209 authList = &AuthPollList;
213 socket_del(&auth->socket);
216 if (send_reports && IsUserPort(auth->client))
217 sendheader(auth->client, REPORT_FAIL_ID);
219 authList = &AuthIncompleteList;
221 if (IsDNSPending(auth)) {
222 delete_resolver_queries(auth);
223 if (send_reports && IsUserPort(auth->client))
224 sendheader(auth->client, REPORT_FAIL_DNS);
228 log_write(LS_RESOLVER, L_INFO, 0, "DNS/AUTH timeout %s",
229 get_client_name(auth->client, HIDE_IP));
230 release_auth_client(auth->client);
233 unlink_auth_request(auth, authList);
234 free_auth_request(auth);
238 * make_auth_request - allocate a new auth request
240 static struct AuthRequest* make_auth_request(struct Client* client)
242 struct AuthRequest* auth =
243 (struct AuthRequest*) MyMalloc(sizeof(struct AuthRequest));
245 memset(auth, 0, sizeof(struct AuthRequest));
246 auth->flags = AM_TIMEOUT;
248 auth->client = client;
249 cli_auth(client) = auth;
250 timer_add(timer_init(&auth->timeout), auth_timeout_callback, (void*) auth,
251 TT_RELATIVE, feature_int(FEAT_AUTH_TIMEOUT));
256 * free_auth_request - cleanup auth request allocations
258 void free_auth_request(struct AuthRequest* auth)
262 Debug((DEBUG_LIST, "Deleting auth socket for %p", auth->client));
263 socket_del(&auth->socket);
265 Debug((DEBUG_LIST, "Deleting auth timeout timer for %p", auth->client));
266 timer_del(&auth->timeout);
270 * unlink_auth_request - remove auth request from a list
272 static void unlink_auth_request(struct AuthRequest* request,
273 struct AuthRequest** list)
276 request->next->prev = request->prev;
278 request->prev->next = request->next;
280 *list = request->next;
284 * link_auth_request - add auth request to a list
286 static void link_auth_request(struct AuthRequest* request,
287 struct AuthRequest** list)
290 request->next = *list;
292 (*list)->prev = request;
297 * release_auth_client - release auth client from auth system
298 * this adds the client into the local client lists so it can be read by
299 * the main io processing loop
301 static void release_auth_client(struct Client* client)
304 cli_auth(client) = 0;
305 cli_lasttime(client) = cli_since(client) = CurrentTime;
306 if (cli_fd(client) > HighestFd)
307 HighestFd = cli_fd(client);
308 LocalClientArray[cli_fd(client)] = client;
310 add_client_to_list(client);
311 socket_events(&(cli_socket(client)), SOCK_ACTION_SET | SOCK_EVENT_READABLE);
312 Debug((DEBUG_INFO, "Auth: release_auth_client %s@%s[%s]",
313 cli_username(client), cli_sockhost(client), cli_sock_ip(client)));
316 static void auth_kill_client(struct AuthRequest* auth)
320 unlink_auth_request(auth, (IsDoingAuth(auth)) ? &AuthPollList : &AuthIncompleteList);
322 if (IsDNSPending(auth))
323 delete_resolver_queries(auth);
324 IPcheck_disconnect(auth->client);
325 Count_unknowndisconnects(UserStats);
326 cli_auth(auth->client) = 0;
327 free_client(auth->client);
328 free_auth_request(auth);
332 * auth_dns_callback - called when resolver query finishes
333 * if the query resulted in a successful search, hp will contain
334 * a non-null pointer, otherwise hp will be null.
335 * set the client on it's way to a connection completion, regardless
336 * of success of failure
338 static void auth_dns_callback(void* vptr, struct DNSReply* hp)
340 struct AuthRequest* auth = (struct AuthRequest*) vptr;
343 * need to do this here so auth_kill_client doesn't
344 * try have the resolver delete the query it's about
345 * to delete anyways. --Bleep
347 ClearDNSPending(auth);
350 struct sockaddr_in *sin = (struct sockaddr_in*)&hp->addr;
352 * Verify that the host to ip mapping is correct both ways and that
353 * the ip#(s) for the socket is listed for the host.
355 if (memcmp(&sin->sin_addr, &cli_ip(auth->client), sizeof(struct in_addr))) {
356 if (IsUserPort(auth->client))
357 sendheader(auth->client, REPORT_IP_MISMATCH);
358 sendto_opmask_butone(0, SNO_IPMISMATCH, "IP# Mismatch: %s != %s[%s]",
359 cli_sock_ip(auth->client), hp->h_name,
360 ircd_ntoa((const char*)&sin->sin_addr));
361 if (feature_bool(FEAT_KILL_IPMISMATCH)) {
362 auth_kill_client(auth);
366 else if (!auth_verify_hostname(hp->h_name, HOSTLEN))
368 if (IsUserPort(auth->client))
369 sendheader(auth->client, REPORT_INVAL_DNS);
373 cli_dns_reply(auth->client) = hp;
374 ircd_strncpy(cli_sockhost(auth->client), hp->h_name, HOSTLEN);
375 if (IsUserPort(auth->client))
376 sendheader(auth->client, REPORT_FIN_DNS);
381 * this should have already been done by s_bsd.c in add_connection
383 * strcpy(auth->client->sockhost, auth->client->sock_ip);
385 if (IsUserPort(auth->client))
386 sendheader(auth->client, REPORT_FAIL_DNS);
388 if (!IsDoingAuth(auth)) {
389 release_auth_client(auth->client);
390 unlink_auth_request(auth, &AuthIncompleteList);
391 free_auth_request(auth);
396 * authsenderr - handle auth send errors
398 static void auth_error(struct AuthRequest* auth, int kill)
400 ++ServerStats->is_abad;
405 socket_del(&auth->socket);
407 if (IsUserPort(auth->client))
408 sendheader(auth->client, REPORT_FAIL_ID);
412 * we can't read the client info from the client socket,
413 * close the client connection and free the client
414 * Need to do this before we ClearAuth(auth) so we know
415 * which list to remove the query from. --Bleep
417 auth_kill_client(auth);
422 unlink_auth_request(auth, &AuthPollList);
424 if (IsDNSPending(auth))
425 link_auth_request(auth, &AuthIncompleteList);
427 release_auth_client(auth->client);
428 free_auth_request(auth);
433 * start_auth_query - Flag the client to show that an attempt to
434 * contact the ident server on the client's host. The connect and
435 * subsequently the socket are all put into 'non-blocking' mode.
436 * Should the connect or any later phase of the identifing process fail,
437 * it is aborted and the user is given a username of "unknown".
439 static int start_auth_query(struct AuthRequest* auth)
441 struct sockaddr_in remote_addr;
442 struct sockaddr_in local_addr;
447 assert(0 != auth->client);
449 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
450 ++ServerStats->is_abad;
453 if ((MAXCONNECTIONS - 10) < fd) {
457 if (!os_set_nonblocking(fd)) {
461 if (IsUserPort(auth->client))
462 sendheader(auth->client, REPORT_DO_ID);
464 * get the local address of the client and bind to that to
465 * make the auth request. This used to be done only for
466 * ifdef VIRTTUAL_HOST, but needs to be done for all clients
467 * since the ident request must originate from that same address--
468 * and machines with multiple IP addresses are common now
470 memset(&local_addr, 0, sizeof(struct sockaddr_in));
471 os_get_sockname(cli_fd(auth->client), &local_addr);
472 local_addr.sin_port = htons(0);
474 if (bind(fd, (struct sockaddr*) &local_addr, sizeof(struct sockaddr_in))) {
479 remote_addr.sin_addr.s_addr = (cli_ip(auth->client)).s_addr;
480 remote_addr.sin_port = htons(113);
481 remote_addr.sin_family = AF_INET;
483 if ((result = os_connect_nonb(fd, &remote_addr)) == IO_FAILURE ||
484 !socket_add(&auth->socket, auth_sock_callback, (void*) auth,
485 result == IO_SUCCESS ? SS_CONNECTED : SS_CONNECTING,
486 SOCK_EVENT_READABLE, fd)) {
487 ServerStats->is_abad++;
489 * No error report from this...
492 if (IsUserPort(auth->client))
493 sendheader(auth->client, REPORT_FAIL_ID);
497 auth->flags |= AM_SOCKET;
500 SetAuthConnect(auth);
501 if (result == IO_SUCCESS)
502 send_auth_query(auth); /* this does a SetAuthPending(auth) for us */
508 enum IdentReplyFields {
516 static char* check_ident_reply(char* reply)
520 char* vector[USERID_TOKEN_COUNT];
521 int count = token_vector(reply, ':', vector, USERID_TOKEN_COUNT);
523 if (USERID_TOKEN_COUNT != count)
526 * second token is the reply type
528 token = vector[IDENT_REPLY_TYPE];
529 if (EmptyString(token))
532 while (IsSpace(*token))
535 if (0 != strncmp(token, "USERID", 6))
539 * third token is the os type
541 token = vector[IDENT_OS_TYPE];
542 if (EmptyString(token))
544 while (IsSpace(*token))
548 * Unless "OTHER" is specified as the operating system
549 * type, the server is expected to return the "normal"
550 * user identification of the owner of this connection.
551 * "Normal" in this context may be taken to mean a string
552 * of characters which uniquely identifies the connection
553 * owner such as a user identifier assigned by the system
554 * administrator and used by such user as a mail
555 * identifier, or as the "user" part of a user/password
556 * pair used to gain access to system resources. When an
557 * operating system is specified (e.g., anything but
558 * "OTHER"), the user identifier is expected to be in a
559 * more or less immediately useful form - e.g., something
560 * that could be used as an argument to "finger" or as a
563 if (0 == strncmp(token, "OTHER", 5))
566 * fourth token is the username
568 token = vector[IDENT_INFO];
569 if (EmptyString(token))
571 while (IsSpace(*token))
574 * look for the end of the username, terminators are '\0, @, <SPACE>, :'
576 for (end = token; *end; ++end) {
577 if (IsSpace(*end) || '@' == *end || ':' == *end)
585 * start_auth - starts auth (identd) and dns queries for a client
587 enum { LOOPBACK = 127 };
589 void start_auth(struct Client* client)
591 struct AuthRequest* auth = 0;
595 auth = make_auth_request(client);
598 Debug((DEBUG_INFO, "Beginning auth request on client %p", client));
600 if (!feature_bool(FEAT_NODNS)) {
601 if (LOOPBACK == inet_netof(cli_ip(client)))
602 strcpy(cli_sockhost(client), cli_name(&me));
604 struct DNSQuery query;
607 query.callback = auth_dns_callback;
609 if (IsUserPort(auth->client))
610 sendheader(client, REPORT_DO_DNS);
612 gethost_byinaddr(&(cli_ip(client)), &query);
617 if (start_auth_query(auth)) {
618 Debug((DEBUG_LIST, "identd query for %p initiated successfully",
620 link_auth_request(auth, &AuthPollList);
621 } else if (IsDNSPending(auth)) {
622 Debug((DEBUG_LIST, "identd query for %p not initiated successfully; "
623 "waiting on DNS", auth->client));
624 link_auth_request(auth, &AuthIncompleteList);
626 Debug((DEBUG_LIST, "identd query for %p not initiated successfully; "
627 "no DNS pending; releasing immediately", auth->client));
628 free_auth_request(auth);
629 release_auth_client(client);
634 * send_auth_query - send the ident server a query giving "theirport , ourport"
635 * The write is only attempted *once* so it is deemed to be a fail if the
636 * entire write doesn't write all the data given. This shouldnt be a
637 * problem since the socket should have a write buffer far greater than
638 * this message to store it in should problems arise. -avalon
640 void send_auth_query(struct AuthRequest* auth)
642 struct sockaddr_in us;
643 struct sockaddr_in them;
648 assert(0 != auth->client);
650 if (!os_get_sockname(cli_fd(auth->client), &us) ||
651 !os_get_peername(cli_fd(auth->client), &them)) {
655 ircd_snprintf(0, authbuf, sizeof(authbuf), "%u , %u\r\n",
656 (unsigned int) ntohs(them.sin_port),
657 (unsigned int) ntohs(us.sin_port));
659 if (IO_SUCCESS == os_send_nonb(auth->fd, authbuf, strlen(authbuf), &count)) {
660 ClearAuthConnect(auth);
661 SetAuthPending(auth);
669 * read_auth_reply - read the reply (if any) from the ident server
671 * We only give it one shot, if the reply isn't good the first time
672 * fail the authentication entirely. --Bleep
674 void read_auth_reply(struct AuthRequest* auth)
679 * rfc1453 sez we MUST accept 512 bytes
681 char buf[BUFSIZE + 1];
684 assert(0 != auth->client);
685 assert(auth == cli_auth(auth->client));
687 if (IO_SUCCESS == os_recv_nonb(auth->fd, buf, BUFSIZE, &len)) {
689 Debug((DEBUG_LIST, "Auth %p [%p] reply: %s", auth, &auth->socket, buf));
690 username = check_ident_reply(buf);
691 Debug((DEBUG_LIST, "Username: %s", username));
696 Debug((DEBUG_LIST, "Deleting auth [%p] socket %p", auth, &auth->socket));
697 socket_del(&auth->socket);
700 if (!EmptyString(username)) {
701 ircd_strncpy(cli_username(auth->client), username, USERLEN);
703 * Not needed, struct is zeroed by memset
704 * auth->client->username[USERLEN] = '\0';
706 SetGotId(auth->client);
707 ++ServerStats->is_asuc;
708 if (IsUserPort(auth->client))
709 sendheader(auth->client, REPORT_FIN_ID);
712 ++ServerStats->is_abad;
714 unlink_auth_request(auth, &AuthPollList);
716 if (IsDNSPending(auth))
717 link_auth_request(auth, &AuthIncompleteList);
719 release_auth_client(auth->client);
720 free_auth_request(auth);