1 |
#error "do not use it" |
2 |
/* wptDNSKeys.cpp - Support for retrieving keys via DNS |
3 |
* Copyright (C) 2006 Timo Schulz |
4 |
* |
5 |
* This file is part of WinPT. |
6 |
* |
7 |
* WinPT is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* WinPT is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
20 |
*/ |
21 |
|
22 |
#include <windows.h> |
23 |
#include <windns.h> |
24 |
|
25 |
#include "wptDNSKeys.h" |
26 |
|
27 |
|
28 |
/* typedef for the function signature. */ |
29 |
typedef DNS_STATUS (*dns_query_fnc) (LPSTR, WORD, DWORD, PIP4_ARRAY, |
30 |
PDNS_RECORD*, PVOID *); |
31 |
|
32 |
typedef void (*dns_record_list_free_fnc) (PDNS_RECORD, DNS_FREE_TYPE); |
33 |
|
34 |
/* function pointer. */ |
35 |
static dns_query_fnc dns_query = NULL; |
36 |
static dns_record_list_free_fnc dns_record_free = NULL; |
37 |
|
38 |
/* hinstance handle to the DLL. */ |
39 |
static HINSTANCE dns_api = NULL; |
40 |
|
41 |
/* 1 if the DNS api is not available. */ |
42 |
static int dns_failed = 0; |
43 |
|
44 |
|
45 |
|
46 |
/* Initialize the DNS sub system. We do this via dynamic loading |
47 |
because older NT/9X systems do not have this API. */ |
48 |
static int |
49 |
dns_init (void) |
50 |
{ |
51 |
if (dns_query) |
52 |
return 0; |
53 |
if (dns_failed) |
54 |
return -1; |
55 |
dns_api = LoadLibrary ("dnsapi"); |
56 |
if (!dns_api) { |
57 |
dns_failed = 1; |
58 |
return -1; |
59 |
} |
60 |
dns_query = (dns_query_fnc)GetProcAddress (dns_api, "DnsQuery_A"); |
61 |
if (!dns_query) { |
62 |
dns_failed = 1; |
63 |
return -1; |
64 |
} |
65 |
dns_record_free = (dns_record_list_free_fnc) |
66 |
GetProcAddress (dns_api, "DnsRecordListFree"); |
67 |
if (!dns_record_free) { |
68 |
dns_failed = 1; |
69 |
return -1; |
70 |
} |
71 |
return 0; |
72 |
} |
73 |
|
74 |
|
75 |
/* Cleanup static structs. */ |
76 |
void |
77 |
dns_cleanup (void) |
78 |
{ |
79 |
if (dns_api != NULL) |
80 |
FreeLibrary (dns_api); |
81 |
dns_api = NULL; |
82 |
dns_failed = 0; |
83 |
} |
84 |
|
85 |
|
86 |
/* build a DNS name for the PKA lookup. */ |
87 |
static char* |
88 |
email_get_pka_addr (const char *uid) |
89 |
{ |
90 |
const char *fmt = "._pka."; |
91 |
char *bo; |
92 |
char *pka; |
93 |
int pos=0; |
94 |
|
95 |
/* check that the @uid really contains an email address. */ |
96 |
if ((bo=strchr (uid, '<')) && strchr (uid, '>')) |
97 |
uid += (bo-uid+1); |
98 |
if (!strchr (uid, '@')) |
99 |
return NULL; |
100 |
|
101 |
/* create the user@_pka.domain-part.tlp string. */ |
102 |
pka = (char*)calloc (1, strlen (uid)+strlen (fmt)+1); |
103 |
while (uid && *uid != '@') |
104 |
pka[pos++] = *uid++; |
105 |
uid++; |
106 |
strcat (pka, fmt); pos += strlen (fmt); |
107 |
while (uid && *uid && *uid != '>') |
108 |
pka[pos++] = *uid++; |
109 |
return pka; |
110 |
} |
111 |
|
112 |
|
113 |
/* Convert the returned data from the PKA (txt) record. */ |
114 |
pka_info_t |
115 |
parse_pka_data (const char *data) |
116 |
{ |
117 |
enum pka_col_t { COL_VER=1, COL_FPR, COL_URI }; |
118 |
pka_info_t pka; |
119 |
char *p; |
120 |
int pos = 1; |
121 |
|
122 |
if (strncmp (data, "v=pka1;", 8)) |
123 |
return NULL; |
124 |
pka = (pka_info_t)calloc (1, sizeof *pka); |
125 |
p = strtok ((char*)data, ";"); |
126 |
while (p != NULL) { |
127 |
switch (pos) { |
128 |
case COL_VER: |
129 |
pka->ver = 1; |
130 |
break; |
131 |
|
132 |
case COL_FPR: |
133 |
pka->fpr = strdup (p+strlen ("fpr=")); |
134 |
break; |
135 |
|
136 |
case COL_URI: /* optional */ |
137 |
pka->uri = strdup (p+strlen ("uri=")); |
138 |
break; |
139 |
|
140 |
default: |
141 |
break; |
142 |
} |
143 |
pos++; |
144 |
} |
145 |
if (pos != 3) { |
146 |
dns_free_pka_record (pka); |
147 |
pka = NULL; |
148 |
} |
149 |
return pka; |
150 |
} |
151 |
|
152 |
|
153 |
/* Retrieve a PKA record from the DNS. |
154 |
@userid is used to extract the email address. */ |
155 |
extern "C" int |
156 |
dns_get_pka_record (const char *userid, pka_info_t *r_pka) |
157 |
{ |
158 |
DNS_STATUS err; |
159 |
DNS_RECORD *rec; |
160 |
char *addr; |
161 |
|
162 |
*r_pka = NULL; |
163 |
if (dns_init ()) |
164 |
return -1; |
165 |
addr = email_get_pka_addr (userid); |
166 |
if (!addr) |
167 |
return -1; |
168 |
err = dns_query (addr, DNS_TYPE_TEXT, 0, NULL, &rec, NULL); |
169 |
if (err) { |
170 |
free (addr); |
171 |
return -1; |
172 |
} |
173 |
*r_pka = parse_pka_data (rec->Data.Txt.pStringArray[0]); |
174 |
|
175 |
dns_record_free (rec, DnsFreeRecordList); |
176 |
free (addr); |
177 |
return 0; |
178 |
} |
179 |
|
180 |
|
181 |
/* Release the memory of the @pka structure. */ |
182 |
extern "C" void |
183 |
dns_free_pka_record (pka_info_t pka) |
184 |
{ |
185 |
if (pka->fpr != NULL) |
186 |
free (pka->fpr); |
187 |
pka->fpr = NULL; |
188 |
if (pka->uri != NULL) |
189 |
free (pka->uri); |
190 |
pka->uri = NULL; |
191 |
free (pka); |
192 |
} |