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>
34 #include "ircd_alloc.h"
35 #include "ircd_chattr.h"
36 #include "ircd_features.h"
38 #include "ircd_osdep.h"
39 #include "ircd_string.h"
42 #include "querycmds.h"
48 #include "sprintf_irc.h"
50 #include "sys.h" /* TRUE bleah */
52 #include <arpa/inet.h> /* inet_netof */
53 #include <netdb.h> /* struct hostent */
60 #include <sys/socket.h>
62 #include <sys/ioctl.h>
65 * a bit different approach
66 * this replaces the original sendheader macros
71 } HeaderMessages [] = {
72 /* 123456789012345678901234567890123456789012345678901234567890 */
73 { "NOTICE AUTH :*** Looking up your hostname\r\n", 43 },
74 { "NOTICE AUTH :*** Found your hostname\r\n", 38 },
75 { "NOTICE AUTH :*** Found your hostname, cached\r\n", 46 },
76 { "NOTICE AUTH :*** Couldn't look up your hostname\r\n", 49 },
77 { "NOTICE AUTH :*** Checking Ident\r\n", 33 },
78 { "NOTICE AUTH :*** Got ident response\r\n", 37 },
79 { "NOTICE AUTH :*** No ident response\r\n", 36 },
80 { "NOTICE AUTH :*** Your forward and reverse DNS do not match, " \
81 "ignoring hostname.\r\n", 80 }
95 #define sendheader(c, r) \
96 send(cli_fd(c), HeaderMessages[(r)].message, HeaderMessages[(r)].length, 0)
98 struct AuthRequest* AuthPollList = 0; /* GLOBAL - auth queries pending io */
99 static struct AuthRequest* AuthIncompleteList = 0;
101 enum { AUTH_TIMEOUT = 60 };
104 * make_auth_request - allocate a new auth request
106 static struct AuthRequest* make_auth_request(struct Client* client)
108 struct AuthRequest* auth =
109 (struct AuthRequest*) MyMalloc(sizeof(struct AuthRequest));
111 memset(auth, 0, sizeof(struct AuthRequest));
113 auth->client = client;
114 auth->timeout = CurrentTime + AUTH_TIMEOUT;
119 * free_auth_request - cleanup auth request allocations
121 void free_auth_request(struct AuthRequest* auth)
129 * unlink_auth_request - remove auth request from a list
131 static void unlink_auth_request(struct AuthRequest* request,
132 struct AuthRequest** list)
135 request->next->prev = request->prev;
137 request->prev->next = request->next;
139 *list = request->next;
143 * link_auth_request - add auth request to a list
145 static void link_auth_request(struct AuthRequest* request,
146 struct AuthRequest** list)
149 request->next = *list;
151 (*list)->prev = request;
156 * release_auth_client - release auth client from auth system
157 * this adds the client into the local client lists so it can be read by
158 * the main io processing loop
160 static void release_auth_client(struct Client* client)
163 cli_lasttime(client) = cli_since(client) = CurrentTime;
164 if (cli_fd(client) > HighestFd)
165 HighestFd = cli_fd(client);
166 LocalClientArray[cli_fd(client)] = client;
168 add_client_to_list(client);
169 Debug((DEBUG_INFO, "Auth: release_auth_client %s@%s[%s]",
170 cli_username(client), cli_sockhost(client), cli_sock_ip(client)));
173 static void auth_kill_client(struct AuthRequest* auth)
177 unlink_auth_request(auth, (IsDoingAuth(auth)) ? &AuthPollList : &AuthIncompleteList);
179 if (IsDNSPending(auth))
180 delete_resolver_queries(auth);
181 IPcheck_disconnect(auth->client);
182 Count_unknowndisconnects(UserStats);
183 free_client(auth->client);
184 free_auth_request(auth);
188 * auth_dns_callback - called when resolver query finishes
189 * if the query resulted in a successful search, hp will contain
190 * a non-null pointer, otherwise hp will be null.
191 * set the client on it's way to a connection completion, regardless
192 * of success of failure
194 static void auth_dns_callback(void* vptr, struct DNSReply* reply)
196 struct AuthRequest* auth = (struct AuthRequest*) vptr;
200 * need to do this here so auth_kill_client doesn't
201 * try have the resolver delete the query it's about
202 * to delete anyways. --Bleep
204 ClearDNSPending(auth);
207 const struct hostent* hp = reply->hp;
211 * Verify that the host to ip mapping is correct both ways and that
212 * the ip#(s) for the socket is listed for the host.
214 for (i = 0; hp->h_addr_list[i]; ++i) {
215 if (0 == memcmp(hp->h_addr_list[i], &(cli_ip(auth->client)),
216 sizeof(struct in_addr)))
219 if (!hp->h_addr_list[i]) {
220 if (IsUserPort(auth->client))
221 sendheader(auth->client, REPORT_IP_MISMATCH);
222 sendto_opmask_butone(0, SNO_IPMISMATCH, "IP# Mismatch: %s != %s[%s]",
223 cli_sock_ip(auth->client), hp->h_name,
224 ircd_ntoa(hp->h_addr_list[0]));
225 if (feature_bool(FEAT_KILL_IPMISMATCH)) {
226 auth_kill_client(auth);
232 cli_dns_reply(auth->client) = reply;
233 ircd_strncpy(cli_sockhost(auth->client), hp->h_name, HOSTLEN);
234 if (IsUserPort(auth->client))
235 sendheader(auth->client, REPORT_FIN_DNS);
240 * this should have already been done by s_bsd.c in add_connection
242 * strcpy(auth->client->sockhost, auth->client->sock_ip);
244 if (IsUserPort(auth->client))
245 sendheader(auth->client, REPORT_FAIL_DNS);
247 if (!IsDoingAuth(auth)) {
248 release_auth_client(auth->client);
249 unlink_auth_request(auth, &AuthIncompleteList);
250 free_auth_request(auth);
255 * authsenderr - handle auth send errors
257 static void auth_error(struct AuthRequest* auth, int kill)
259 ++ServerStats->is_abad;
265 if (IsUserPort(auth->client))
266 sendheader(auth->client, REPORT_FAIL_ID);
270 * we can't read the client info from the client socket,
271 * close the client connection and free the client
272 * Need to do this before we ClearAuth(auth) so we know
273 * which list to remove the query from. --Bleep
275 auth_kill_client(auth);
280 unlink_auth_request(auth, &AuthPollList);
282 if (IsDNSPending(auth))
283 link_auth_request(auth, &AuthIncompleteList);
285 release_auth_client(auth->client);
286 free_auth_request(auth);
291 * start_auth_query - Flag the client to show that an attempt to
292 * contact the ident server on the client's host. The connect and
293 * subsequently the socket are all put into 'non-blocking' mode.
294 * Should the connect or any later phase of the identifing process fail,
295 * it is aborted and the user is given a username of "unknown".
297 static int start_auth_query(struct AuthRequest* auth)
299 struct sockaddr_in remote_addr;
300 struct sockaddr_in local_addr;
304 assert(0 != auth->client);
306 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
308 report_error(SOCKET_ERROR_MSG, get_client_name(auth->client, HIDE_IP), errno);
310 ++ServerStats->is_abad;
313 if ((MAXCONNECTIONS - 10) < fd) {
315 report_error(CONNLIMIT_ERROR_MSG,
316 get_client_name(auth->client, HIDE_IP), errno);
321 if (!os_set_nonblocking(fd)) {
323 report_error(NONB_ERROR_MSG, get_client_name(auth->client, HIDE_IP), errno);
328 if (IsUserPort(auth->client))
329 sendheader(auth->client, REPORT_DO_ID);
331 * get the local address of the client and bind to that to
332 * make the auth request. This used to be done only for
333 * ifdef VIRTTUAL_HOST, but needs to be done for all clients
334 * since the ident request must originate from that same address--
335 * and machines with multiple IP addresses are common now
337 memset(&local_addr, 0, sizeof(struct sockaddr_in));
338 os_get_sockname(cli_fd(auth->client), &local_addr);
339 local_addr.sin_port = htons(0);
341 if (bind(fd, (struct sockaddr*) &local_addr, sizeof(struct sockaddr_in))) {
343 report_error(BIND_ERROR_MSG,
344 get_client_name(auth->client, HIDE_IP), errno);
350 remote_addr.sin_addr.s_addr = (cli_ip(auth->client)).s_addr;
351 remote_addr.sin_port = htons(113);
352 remote_addr.sin_family = AF_INET;
354 if (!os_connect_nonb(fd, &remote_addr)) {
355 ServerStats->is_abad++;
357 * No error report from this...
360 if (IsUserPort(auth->client))
361 sendheader(auth->client, REPORT_FAIL_ID);
367 SetAuthConnect(auth);
372 enum IdentReplyFields {
380 static char* check_ident_reply(char* reply)
384 char* vector[USERID_TOKEN_COUNT];
385 int count = token_vector(reply, ':', vector, USERID_TOKEN_COUNT);
387 if (USERID_TOKEN_COUNT != count)
390 * second token is the reply type
392 token = vector[IDENT_REPLY_TYPE];
393 if (EmptyString(token))
396 while (IsSpace(*token))
399 if (0 != strncmp(token, "USERID", 6))
403 * third token is the os type
405 token = vector[IDENT_OS_TYPE];
406 if (EmptyString(token))
408 while (IsSpace(*token))
412 * Unless "OTHER" is specified as the operating system
413 * type, the server is expected to return the "normal"
414 * user identification of the owner of this connection.
415 * "Normal" in this context may be taken to mean a string
416 * of characters which uniquely identifies the connection
417 * owner such as a user identifier assigned by the system
418 * administrator and used by such user as a mail
419 * identifier, or as the "user" part of a user/password
420 * pair used to gain access to system resources. When an
421 * operating system is specified (e.g., anything but
422 * "OTHER"), the user identifier is expected to be in a
423 * more or less immediately useful form - e.g., something
424 * that could be used as an argument to "finger" or as a
427 if (0 == strncmp(token, "OTHER", 5))
430 * fourth token is the username
432 token = vector[IDENT_INFO];
433 if (EmptyString(token))
435 while (IsSpace(*token))
438 * look for the end of the username, terminators are '\0, @, <SPACE>, :'
440 for (end = token; *end; ++end) {
441 if (IsSpace(*end) || '@' == *end || ':' == *end)
450 * GetValidIdent - parse ident query reply from identd server
452 * Inputs - pointer to ident buf
453 * Output - NULL if no valid ident found, otherwise pointer to name
456 static char* GetValidIdent(char *buf)
464 char* remotePortString;
466 /* All this to get rid of a sscanf() fun. */
467 remotePortString = buf;
469 colon1Ptr = strchr(remotePortString,':');
475 colon2Ptr = strchr(colon1Ptr,':');
481 commaPtr = strchr(remotePortString, ',');
489 remp = atoi(remotePortString);
493 locp = atoi(commaPtr);
497 /* look for USERID bordered by first pair of colons */
498 if(!strstr(colon1Ptr, "USERID"))
501 colon3Ptr = strchr(colon2Ptr,':');
512 * start_auth - starts auth (identd) and dns queries for a client
514 enum { LOOPBACK = 127 };
516 void start_auth(struct Client* client)
518 struct AuthRequest* auth = 0;
522 auth = make_auth_request(client);
526 if (LOOPBACK == inet_netof(cli_ip(client))) {
527 strcpy(cli_sockhost(client), cli_name(&me));
530 struct DNSQuery query;
533 query.callback = auth_dns_callback;
535 if (IsUserPort(auth->client))
536 sendheader(client, REPORT_DO_DNS);
538 cli_dns_reply(client) = gethost_byaddr((const char*) &(cli_ip(client)), &query);
540 if (cli_dns_reply(client)) {
541 ++(cli_dns_reply(client))->ref_count;
542 ircd_strncpy(cli_sockhost(client), cli_dns_reply(client)->hp->h_name, HOSTLEN);
543 if (IsUserPort(auth->client))
544 sendheader(client, REPORT_FIN_DNSC);
551 if (start_auth_query(auth))
552 link_auth_request(auth, &AuthPollList);
553 else if (IsDNSPending(auth))
554 link_auth_request(auth, &AuthIncompleteList);
556 free_auth_request(auth);
557 release_auth_client(client);
562 * timeout_auth_queries - timeout resolver and identd requests
563 * allow clients through if requests failed
565 void timeout_auth_queries(time_t now)
567 struct AuthRequest* auth;
568 struct AuthRequest* auth_next = 0;
570 for (auth = AuthPollList; auth; auth = auth_next) {
571 auth_next = auth->next;
572 if (auth->timeout < CurrentTime) {
578 if (IsUserPort(auth->client))
579 sendheader(auth->client, REPORT_FAIL_ID);
580 if (IsDNSPending(auth)) {
581 delete_resolver_queries(auth);
582 if (IsUserPort(auth->client))
583 sendheader(auth->client, REPORT_FAIL_DNS);
585 log_write(LS_RESOLVER, L_INFO, 0, "DNS/AUTH timeout %s",
586 get_client_name(auth->client, HIDE_IP));
588 release_auth_client(auth->client);
589 unlink_auth_request(auth, &AuthPollList);
590 free_auth_request(auth);
593 for (auth = AuthIncompleteList; auth; auth = auth_next) {
594 auth_next = auth->next;
595 if (auth->timeout < CurrentTime) {
596 delete_resolver_queries(auth);
597 if (IsUserPort(auth->client))
598 sendheader(auth->client, REPORT_FAIL_DNS);
599 log_write(LS_RESOLVER, L_INFO, 0, "DNS timeout %s",
600 get_client_name(auth->client, HIDE_IP));
602 release_auth_client(auth->client);
603 unlink_auth_request(auth, &AuthIncompleteList);
604 free_auth_request(auth);
610 * send_auth_query - send the ident server a query giving "theirport , ourport"
611 * The write is only attempted *once* so it is deemed to be a fail if the
612 * entire write doesn't write all the data given. This shouldnt be a
613 * problem since the socket should have a write buffer far greater than
614 * this message to store it in should problems arise. -avalon
616 void send_auth_query(struct AuthRequest* auth)
618 struct sockaddr_in us;
619 struct sockaddr_in them;
624 assert(0 != auth->client);
626 if (!os_get_sockname(cli_fd(auth->client), &us) ||
627 !os_get_peername(cli_fd(auth->client), &them)) {
631 sprintf_irc(authbuf, "%u , %u\r\n",
632 (unsigned int) ntohs(them.sin_port),
633 (unsigned int) ntohs(us.sin_port));
635 if (IO_SUCCESS == os_send_nonb(auth->fd, authbuf, strlen(authbuf), &count)) {
636 ClearAuthConnect(auth);
637 SetAuthPending(auth);
645 * read_auth_reply - read the reply (if any) from the ident server
647 * We only give it one shot, if the reply isn't good the first time
648 * fail the authentication entirely. --Bleep
650 void read_auth_reply(struct AuthRequest* auth)
655 * rfc1453 sez we MUST accept 512 bytes
657 char buf[BUFSIZE + 1];
660 assert(0 != auth->client);
662 if (IO_SUCCESS == os_recv_nonb(auth->fd, buf, BUFSIZE, &len)) {
664 username = check_ident_reply(buf);
671 if (!EmptyString(username)) {
672 ircd_strncpy(cli_username(auth->client), username, USERLEN);
674 * Not needed, struct is zeroed by memset
675 * auth->client->username[USERLEN] = '\0';
677 SetGotId(auth->client);
678 ++ServerStats->is_asuc;
679 if (IsUserPort(auth->client))
680 sendheader(auth->client, REPORT_FIN_ID);
683 ++ServerStats->is_abad;
685 strcpy(cli_username(auth->client), "unknown");
688 unlink_auth_request(auth, &AuthPollList);
690 if (IsDNSPending(auth))
691 link_auth_request(auth, &AuthIncompleteList);
693 release_auth_client(auth->client);
694 free_auth_request(auth);