added some code
[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 <string.h>
18
19 #include "ircd_auth.h"
20 #include "struct_auth.h"
21 #include "struct_connection.h"
22 #include "ircd_client.h"
23 #include "IOHandler/IOSockets.h"
24 #include "IOHandler/IODNSLookup.h"
25 #include "version.h"
26
27 static void auth_start_dnsreverse(struct Auth *auth);
28 static IODNS_CALLBACK(auth_dns_callback);
29 static void auth_free(struct Auth *auth);
30
31 static struct Auth *authlist_first = NULL;
32 static struct Auth *authlist_last = NULL;
33
34 struct Auth *auth_new(struct Connection *conn) {        
35         struct Auth *auth = calloc(1, sizeof(*auth));
36         socket_printf(conn, "NOTICE AUTH :*** NextIRCd v%d.%d (%s)", VERSION_NUMBER, patchlevel, revision);
37         
38         auth->conn = conn;
39         time(&auth->startup_time);
40         
41         auth->prev = authlist_last;
42         auth->next = NULL;
43         if(!authlist_last)
44                 authlist_first = auth;
45         authlist_last = auth;
46         
47         auth_start_dnsreverse(auth);
48         
49         return auth;
50 }
51
52 static void auth_start_dnsreverse(struct Auth *auth) {
53         socket_printf(auth->conn, "NOTICE AUTH :*** Looking up your hostname");
54         
55         struct IODNSAddress *sockaddr;
56         sockaddr = iosocket_get_remote_addr(auth->conn->socket);
57         if(sockaddr) {
58                 auth->dnslookup = iodns_getnameinfo(sockaddr->address, sockaddr->addresslen, auth_dns_callback, auth);
59         } else {
60                 // critical error!
61         }
62 }
63
64 static IODNS_CALLBACK(auth_dns_callback) {
65         struct Auth *auth = event->query->data;
66         struct IODNSResult *dnsresult = event->result;
67         
68         auth->dnslookup = NULL;
69         
70         if(event->type == IODNSEVENT_SUCCESS) {
71                 strncpy(auth->host, dnsresult->result.host, HOSTLEN);
72                 socket_printf(auth->conn, "NOTICE AUTH :*** Found your hostname (%s)", auth->host);
73         } else {
74                 struct IODNSAddress *sockaddr = iosocket_get_remote_addr(auth->conn->socket);
75                 iodns_print_address(sockaddr, auth->conn->socket->ipv6, auth->host, HOSTLEN);
76                 socket_printf(auth->conn, "NOTICE AUTH :*** Couldn't look up your hostname. Using your IP instead (%s)", auth->host);
77         }
78         if(dnsresult)
79                 iodns_free_result(dnsresult);
80         
81         auth_try_finish(auth);
82 }
83
84
85 void auth_try_finish(struct Auth *auth) {
86         if(auth->server) {
87                 
88         } else {
89                 if(!auth->have_nick || !auth->have_user)
90                         return;
91                 if(!auth->sent_ping) {
92                         
93                         auth->sent_ping = 1;
94                 } else if(auth->have_pong && auth->have_dnsresolv) {
95                         struct Client *client = client_connected(auth);
96                         auth->conn->authed = 1;
97                         auth->conn->data.client = client;
98                         auth_free(auth);
99                 }
100         }
101 }
102
103 void auth_abort(struct Auth *auth) {
104         
105 }
106
107 static void auth_free(struct Auth *auth) {
108         if(auth->dnslookup) {
109                 
110         }
111         
112 }