added some files
[NextIRCd.git] / src / ircd_auth.c
1 /* ircd_auth.c - NextIRCd
2  * Copyright (C) 2012-2013  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17 #include "struct_auth.h"
18 #include "struct_connection.h"
19 #include "ircd_client.h"
20 #include "IOHandler/IOSockets.h"
21 #include "IOHandler/IODNSLookup.h"
22 #include "version.h"
23
24 static IODNS_CALLBACK(auth_dns_callback);
25
26 static struct Auth *authlist_first = NULL;
27 static struct Auth *authlist_last = NULL;
28
29 struct Auth *auth_new(struct Connection *conn) {
30         struct Auth *auth = calloc(1, sizeof(*auth));
31         client_printf(conn, "NOTICE AUTH :*** NextIRCd v%d.%d (%s)", VERSION_NUMBER, patchlevel, revision);
32         
33         auth->conn = conn;
34         time(&auth->startup_time);
35         
36         auth->prev = authlist_last;
37         auth->next = NULL;
38         if(!authlist_last)
39                 authlist_first = auth
40         authlist_last = auth;
41         
42         return auth;
43 }
44
45 void auth_start_dnsreverse(struct Auth *auth) {
46         client_printf(auth->conn, "NOTICE AUTH :*** Looking up your hostname");
47         
48         struct IODNSAddress *sockaddr;
49         sockaddr = iosocket_get_remote_addr(auth->conn->socket);
50         if(sockaddr) {
51                 auth->dnslookup = iodns_getnameinfo(sockaddr->address, sockaddr->addresslen, auth_dns_callback, auth);
52         } else {
53                 // critical error!
54         }
55 }
56
57 static IODNS_CALLBACK(auth_dns_callback) {
58         struct Auth *auth = event->query->data;
59         struct IODNSResult *dnsresult = event->result;
60         
61         if(event->type == IODNSEVENT_SUCCESS) {
62                 strncpy(auth->host, dnsresult->result.host, HOSTLEN);
63                 client_printf(auth->conn, "NOTICE AUTH :*** Found your hostname (%s)", auth->host);
64         } else {
65                 struct IODNSAddress *sockaddr = iosocket_get_remote_addr(auth->conn->socket);
66                 if(sockaddr->addresslen == sizeof(struct sockaddr_in)) {
67                         //ipv4
68                         inet_ntop(AF_INET, (void *)(&((struct sockaddr_in *)sockaddr->address)->sin_addr), auth->host, HOSTLEN);
69                 } else {
70                         //ipv6
71                         inet_ntop(AF_INET6, (void *)(&((struct sockaddr_in6 *)sockaddr->address)->sin6_addr), auth->host, HOSTLEN);
72                 }
73                 client_printf(auth->conn, "NOTICE AUTH :*** Couldn't look up your hostname. Using your IP instead (%s)", auth->host);
74         }
75         if(dnsresult)
76                 iodns_free_result(dnsresult);
77         
78         auth_try_finish(auth);
79 }
80
81
82 void auth_try_finish(struct Auth *auth) {
83
84 }