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