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"
37 #include "ircd_osdep.h"
38 #include "ircd_string.h"
41 #include "querycmds.h"
47 #include "sprintf_irc.h"
49 #include "sys.h" /* TRUE bleah */
51 #include <netdb.h> /* struct hostent */
58 #include <sys/socket.h>
60 #include <sys/ioctl.h>
63 * a bit different approach
64 * this replaces the original sendheader macros
69 } HeaderMessages [] = {
70 /* 123456789012345678901234567890123456789012345678901234567890 */
71 { "NOTICE AUTH :*** Looking up your hostname\r\n", 43 },
72 { "NOTICE AUTH :*** Found your hostname\r\n", 38 },
73 { "NOTICE AUTH :*** Found your hostname, cached\r\n", 46 },
74 { "NOTICE AUTH :*** Couldn't look up your hostname\r\n", 49 },
75 { "NOTICE AUTH :*** Checking Ident\r\n", 33 },
76 { "NOTICE AUTH :*** Got ident response\r\n", 37 },
77 { "NOTICE AUTH :*** No ident response\r\n", 36 },
78 { "NOTICE AUTH :*** Your forward and reverse DNS do not match, " \
79 "ignoring hostname.\r\n", 80 }
93 #define sendheader(c, r) \
94 send((c)->fd, HeaderMessages[(r)].message, HeaderMessages[(r)].length, 0)
96 struct AuthRequest* AuthPollList = 0; /* GLOBAL - auth queries pending io */
97 static struct AuthRequest* AuthIncompleteList = 0;
100 * make_auth_request - allocate a new auth request
102 static struct AuthRequest* make_auth_request(struct Client* client)
104 struct AuthRequest* auth =
105 (struct AuthRequest*) MyMalloc(sizeof(struct AuthRequest));
107 memset(auth, 0, sizeof(struct AuthRequest));
109 auth->client = client;
110 auth->timeout = CurrentTime + CONNECTTIMEOUT;
115 * free_auth_request - cleanup auth request allocations
117 void free_auth_request(struct AuthRequest* auth)
125 * unlink_auth_request - remove auth request from a list
127 static void unlink_auth_request(struct AuthRequest* request,
128 struct AuthRequest** list)
131 request->next->prev = request->prev;
133 request->prev->next = request->next;
135 *list = request->next;
139 * link_auth_request - add auth request to a list
141 static void link_auth_request(struct AuthRequest* request,
142 struct AuthRequest** list)
145 request->next = *list;
147 (*list)->prev = request;
152 * release_auth_client - release auth client from auth system
153 * this adds the client into the local client lists so it can be read by
154 * the main io processing loop
156 static void release_auth_client(struct Client* client)
159 client->lasttime = client->since = CurrentTime;
160 if (client->fd > HighestFd)
161 HighestFd = client->fd;
162 LocalClientArray[client->fd] = client;
164 add_client_to_list(client);
165 Debug((DEBUG_INFO, "Auth: release_auth_client %s@%s[%s]",
166 client->username, client->sockhost, client->sock_ip));
169 static void auth_kill_client(struct AuthRequest* auth)
173 unlink_auth_request(auth, (IsDoingAuth(auth)) ? &AuthPollList : &AuthIncompleteList);
175 if (IsDNSPending(auth))
176 delete_resolver_queries(auth);
177 IPcheck_disconnect(auth->client);
178 Count_unknowndisconnects(UserStats);
179 free_client(auth->client);
180 free_auth_request(auth);
184 * auth_dns_callback - called when resolver query finishes
185 * if the query resulted in a successful search, hp will contain
186 * a non-null pointer, otherwise hp will be null.
187 * set the client on it's way to a connection completion, regardless
188 * of success of failure
190 static void auth_dns_callback(void* vptr, struct DNSReply* reply)
192 struct AuthRequest* auth = (struct AuthRequest*) vptr;
196 * need to do this here so auth_kill_client doesn't
197 * try have the resolver delete the query it's about
198 * to delete anyways. --Bleep
200 ClearDNSPending(auth);
203 const struct hostent* hp = reply->hp;
207 * Verify that the host to ip mapping is correct both ways and that
208 * the ip#(s) for the socket is listed for the host.
210 for (i = 0; hp->h_addr_list[i]; ++i) {
211 if (0 == memcmp(hp->h_addr_list[i], &auth->client->ip,
212 sizeof(struct in_addr)))
215 if (!hp->h_addr_list[i]) {
216 if (IsUserPort(auth->client))
217 sendheader(auth->client, REPORT_IP_MISMATCH);
218 sendto_op_mask(SNO_IPMISMATCH, "IP# Mismatch: %s != %s[%s]",
219 auth->client->sock_ip, hp->h_name,
220 ircd_ntoa(hp->h_addr_list[0]));
221 #if defined(KILL_IPMISMATCH)
222 auth_kill_client(auth);
228 auth->client->dns_reply = reply;
229 ircd_strncpy(auth->client->sockhost, hp->h_name, HOSTLEN);
230 if (IsUserPort(auth->client))
231 sendheader(auth->client, REPORT_FIN_DNS);
236 * this should have already been done by s_bsd.c in add_connection
238 * strcpy(auth->client->sockhost, auth->client->sock_ip);
240 if (IsUserPort(auth->client))
241 sendheader(auth->client, REPORT_FAIL_DNS);
243 if (!IsDoingAuth(auth)) {
244 release_auth_client(auth->client);
245 unlink_auth_request(auth, &AuthIncompleteList);
246 free_auth_request(auth);
251 * authsenderr - handle auth send errors
253 static void auth_error(struct AuthRequest* auth, int kill)
255 ++ServerStats->is_abad;
261 if (IsUserPort(auth->client))
262 sendheader(auth->client, REPORT_FAIL_ID);
266 * we can't read the client info from the client socket,
267 * close the client connection and free the client
268 * Need to do this before we ClearAuth(auth) so we know
269 * which list to remove the query from. --Bleep
271 auth_kill_client(auth);
276 unlink_auth_request(auth, &AuthPollList);
278 if (IsDNSPending(auth))
279 link_auth_request(auth, &AuthIncompleteList);
281 release_auth_client(auth->client);
282 free_auth_request(auth);
287 * start_auth_query - Flag the client to show that an attempt to
288 * contact the ident server on the client's host. The connect and
289 * subsequently the socket are all put into 'non-blocking' mode.
290 * Should the connect or any later phase of the identifing process fail,
291 * it is aborted and the user is given a username of "unknown".
293 static int start_auth_query(struct AuthRequest* auth)
295 struct sockaddr_in remote_addr;
296 struct sockaddr_in local_addr;
300 assert(0 != auth->client);
302 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
304 report_error(SOCKET_ERROR_MSG, get_client_name(auth->client, HIDE_IP), errno);
306 ++ServerStats->is_abad;
309 if ((MAXCONNECTIONS - 10) < fd) {
311 report_error(CONNLIMIT_ERROR_MSG,
312 get_client_name(auth->client, HIDE_IP), errno);
317 if (!os_set_nonblocking(fd)) {
319 report_error(NONB_ERROR_MSG, get_client_name(auth->client, HIDE_IP), errno);
324 if (IsUserPort(auth->client))
325 sendheader(auth->client, REPORT_DO_ID);
327 * get the local address of the client and bind to that to
328 * make the auth request. This used to be done only for
329 * ifdef VIRTTUAL_HOST, but needs to be done for all clients
330 * since the ident request must originate from that same address--
331 * and machines with multiple IP addresses are common now
333 memset(&local_addr, 0, sizeof(struct sockaddr_in));
334 os_get_sockname(auth->client->fd, &local_addr);
335 local_addr.sin_port = htons(0);
337 if (bind(fd, (struct sockaddr*) &local_addr, sizeof(struct sockaddr_in))) {
339 report_error(BIND_ERROR_MSG,
340 get_client_name(auth->client, HIDE_IP), errno);
346 remote_addr.sin_addr.s_addr = auth->client->ip.s_addr;
347 remote_addr.sin_port = htons(113);
348 remote_addr.sin_family = AF_INET;
350 if (!os_connect_nonb(fd, &remote_addr)) {
351 ServerStats->is_abad++;
353 * No error report from this...
356 if (IsUserPort(auth->client))
357 sendheader(auth->client, REPORT_FAIL_ID);
363 SetAuthConnect(auth);
368 enum IdentReplyFields {
376 static char* check_ident_reply(char* reply)
380 char* vector[USERID_TOKEN_COUNT];
381 int count = token_vector(reply, ':', vector, USERID_TOKEN_COUNT);
383 if (USERID_TOKEN_COUNT != count)
386 * second token is the reply type
388 token = vector[IDENT_REPLY_TYPE];
389 if (EmptyString(token))
392 while (IsSpace(*token))
395 if (0 != strncmp(token, "USERID", 6))
399 * third token is the os type
401 token = vector[IDENT_OS_TYPE];
402 if (EmptyString(token))
404 while (IsSpace(*token))
408 * Unless "OTHER" is specified as the operating system
409 * type, the server is expected to return the "normal"
410 * user identification of the owner of this connection.
411 * "Normal" in this context may be taken to mean a string
412 * of characters which uniquely identifies the connection
413 * owner such as a user identifier assigned by the system
414 * administrator and used by such user as a mail
415 * identifier, or as the "user" part of a user/password
416 * pair used to gain access to system resources. When an
417 * operating system is specified (e.g., anything but
418 * "OTHER"), the user identifier is expected to be in a
419 * more or less immediately useful form - e.g., something
420 * that could be used as an argument to "finger" or as a
423 if (0 == strncmp(token, "OTHER", 5))
426 * fourth token is the username
428 token = vector[IDENT_INFO];
429 if (EmptyString(token))
431 while (IsSpace(*token))
434 * look for the end of the username, terminators are '\0, @, <SPACE>, :'
436 for (end = token; *end; ++end) {
437 if (IsSpace(*end) || '@' == *end || ':' == *end)
446 * GetValidIdent - parse ident query reply from identd server
448 * Inputs - pointer to ident buf
449 * Output - NULL if no valid ident found, otherwise pointer to name
452 static char* GetValidIdent(char *buf)
460 char* remotePortString;
462 /* All this to get rid of a sscanf() fun. */
463 remotePortString = buf;
465 colon1Ptr = strchr(remotePortString,':');
471 colon2Ptr = strchr(colon1Ptr,':');
477 commaPtr = strchr(remotePortString, ',');
485 remp = atoi(remotePortString);
489 locp = atoi(commaPtr);
493 /* look for USERID bordered by first pair of colons */
494 if(!strstr(colon1Ptr, "USERID"))
497 colon3Ptr = strchr(colon2Ptr,':');
508 * start_auth - starts auth (identd) and dns queries for a client
510 void start_auth(struct Client* client)
512 struct DNSQuery query;
513 struct AuthRequest* auth = 0;
517 auth = make_auth_request(client);
521 query.callback = auth_dns_callback;
523 if (IsUserPort(auth->client))
524 sendheader(client, REPORT_DO_DNS);
527 client->dns_reply = gethost_byaddr((const char*) &client->ip, &query);
528 if (client->dns_reply) {
529 ++client->dns_reply->ref_count;
530 ircd_strncpy(client->sockhost, client->dns_reply->hp->h_name, HOSTLEN);
531 if (IsUserPort(auth->client))
532 sendheader(client, REPORT_FIN_DNSC);
538 if (start_auth_query(auth))
539 link_auth_request(auth, &AuthPollList);
540 else if (IsDNSPending(auth))
541 link_auth_request(auth, &AuthIncompleteList);
543 free_auth_request(auth);
544 release_auth_client(client);
549 * timeout_auth_queries - timeout resolver and identd requests
550 * allow clients through if requests failed
552 void timeout_auth_queries(time_t now)
554 struct AuthRequest* auth;
555 struct AuthRequest* auth_next = 0;
557 for (auth = AuthPollList; auth; auth = auth_next) {
558 auth_next = auth->next;
559 if (auth->timeout < CurrentTime) {
565 if (IsUserPort(auth->client))
566 sendheader(auth->client, REPORT_FAIL_ID);
567 if (IsDNSPending(auth)) {
568 delete_resolver_queries(auth);
569 if (IsUserPort(auth->client))
570 sendheader(auth->client, REPORT_FAIL_DNS);
572 ircd_log(L_INFO, "DNS/AUTH timeout %s",
573 get_client_name(auth->client, HIDE_IP));
575 release_auth_client(auth->client);
576 unlink_auth_request(auth, &AuthPollList);
577 free_auth_request(auth);
580 for (auth = AuthIncompleteList; auth; auth = auth_next) {
581 auth_next = auth->next;
582 if (auth->timeout < CurrentTime) {
583 delete_resolver_queries(auth);
584 if (IsUserPort(auth->client))
585 sendheader(auth->client, REPORT_FAIL_DNS);
586 ircd_log(L_INFO, "DNS timeout %s", get_client_name(auth->client, HIDE_IP));
588 release_auth_client(auth->client);
589 unlink_auth_request(auth, &AuthIncompleteList);
590 free_auth_request(auth);
596 * send_auth_query - send the ident server a query giving "theirport , ourport"
597 * The write is only attempted *once* so it is deemed to be a fail if the
598 * entire write doesn't write all the data given. This shouldnt be a
599 * problem since the socket should have a write buffer far greater than
600 * this message to store it in should problems arise. -avalon
602 void send_auth_query(struct AuthRequest* auth)
604 struct sockaddr_in us;
605 struct sockaddr_in them;
610 assert(0 != auth->client);
612 if (!os_get_sockname(auth->client->fd, &us) ||
613 !os_get_peername(auth->client->fd, &them)) {
617 sprintf_irc(authbuf, "%u , %u\r\n",
618 (unsigned int) ntohs(them.sin_port),
619 (unsigned int) ntohs(us.sin_port));
621 if (IO_SUCCESS == os_send_nonb(auth->fd, authbuf, strlen(authbuf), &count)) {
622 ClearAuthConnect(auth);
623 SetAuthPending(auth);
631 * read_auth_reply - read the reply (if any) from the ident server
633 * We only give it one shot, if the reply isn't good the first time
634 * fail the authentication entirely. --Bleep
636 void read_auth_reply(struct AuthRequest* auth)
641 * rfc1453 sez we MUST accept 512 bytes
643 char buf[BUFSIZE + 1];
646 assert(0 != auth->client);
648 if (IO_SUCCESS == os_recv_nonb(auth->fd, buf, BUFSIZE, &len)) {
650 username = check_ident_reply(buf);
657 if (!EmptyString(username)) {
658 ircd_strncpy(auth->client->username, username, USERLEN);
660 * Not needed, struct is zeroed by memset
661 * auth->client->username[USERLEN] = '\0';
663 SetGotId(auth->client);
664 ++ServerStats->is_asuc;
665 if (IsUserPort(auth->client))
666 sendheader(auth->client, REPORT_FIN_ID);
669 ++ServerStats->is_abad;
671 strcpy(auth->client->username, "unknown");
674 unlink_auth_request(auth, &AuthPollList);
676 if (IsDNSPending(auth))
677 link_auth_request(auth, &AuthIncompleteList);
679 release_auth_client(auth->client);
680 free_auth_request(auth);