Debian Codegraph @

src/authplugins/dnsauth.cpp

raw
1 // DNS auth plugin
2
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 2 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, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18 // INCLUDES
19
20 #ifdef HAVE_CONFIG_H
21 #include "e2config.h"
22 #endif
23
24 #include "../Auth.hpp"
25 #include "../RegExp.hpp"
26 #include "../OptionContainer.hpp"
27 #include "../Logger.hpp"
28
29 #include <sys/types.h>
30 #include <algorithm>
31 #include <unistd.h>
32 #include <iostream>
33 #include <fstream>
34 #include <errno.h>
35 #include <netinet/in.h>
36 #include <netdb.h>
37 #include <arpa/nameser.h>
38 #include <resolv.h>
39
40 // GLOBALS
41
42 extern OptionContainer o;
43 extern int h_errno;
44
45 // DECLARATIONS
46
47 // user record struct
48 struct userstruct {
49 String ippath;
50 String user;
51 int group;
52 };
53
54 String basedomain;
55
56 String authurl;
57
58 String authprefix;
59
60 bool redirect_to_auth;
61
62 // class name is relevant!
63 class dnsauthinstance : public AuthPlugin
64 {
65 public:
66 // keep credentials for the whole of a connection - IP isn't going to change.
67 // not quite true - what about downstream proxy with x-forwarded-for?
68 dnsauthinstance(ConfigVar &definition)
69 : AuthPlugin(definition)
70 {
71 client_ip_based = true;
72 if (!o.use_xforwardedfor)
73 is_connection_based = true;
74 };
75
76 int identify(Socket &peercon, Socket &proxycon, HTTPHeader &h, std::string &string, bool &is_real_user,auth_rec &authrec,NaughtyFilter &cm);
77 int determineGroup(std::string &user, int &fg, ListContainer &uglc);
78
79 int init(void *args);
80 int quit();
81
82 private:
83 userstruct userst;
84 bool getdnstxt(String &ippath);
85 String dns_error(int herror);
86 bool inAuthByPassLists(HTTPHeader &h);
87 };
88
89 // IMPLEMENTATION
90
91 // class factory code *MUST* be included in every plugin
92
93 AuthPlugin *dnsauthcreate(ConfigVar &definition)
94 {
95 return new dnsauthinstance(definition);
96 }
97
98 // end of Class factory
99
100 //
101
102 // Standard plugin funcs
103 //
104 //
105
106 // plugin quit - clear IP, subnet & range lists
107 int dnsauthinstance::quit()
108 {
109 return 0;
110 }
111
112 // plugin init - read in vars
113 int dnsauthinstance::init(void *args)
114 {
115 AuthPlugin::init(args);
116 basedomain = cv["basedomain"];
117 authurl = cv["authurl"];
118 authprefix = cv["prefix_auth"];
119 String t;
120 t = cv["redirect_to_auth"];
121 if ((t.length() < 1) || (t == "yes")) {
122 redirect_to_auth = true;
123 } else {
124 redirect_to_auth = false;
125 };
126 if (basedomain.length() < 1) {
127 E2LOGGER_error("No basedomain defined in DNS auth plugin config");
128 return -1;
129 }
130 if (authurl.length() < 1) {
131 E2LOGGER_error("No authurl defined in DNS auth plugin config");
132 return -1;
133 }
134 if (authprefix.length() < 1) {
135 E2LOGGER_error("No prefix_auth defined in DNS auth plugin config");
136 return -1;
137 }
138 read_def_fg();
139
140 DEBUG_auth( "basedomain is ", basedomain, " authurl is ", authurl);
141 return 0;
142 }
143
144 // DNS-AUTH filter group determination
145 // never actually return NOUSER from this, because we don't actually look in the filtergroupslist.
146 // NOUSER stops ConnectionHandler from querying subsequent plugins.
147 int dnsauthinstance::identify(Socket &peercon, Socket &proxycon, HTTPHeader &h, /*int &fg,*/ std::string &string, bool &is_real_user,auth_rec &authrec,NaughtyFilter &cm)
148 {
149 String p1, p2, ippath;
150
151 p1 = peercon.getPeerIP();
152
153 if (o.use_xforwardedfor) {
154 // grab the X-Forwarded-For IP if available
155 p2 = h.getXForwardedForIP();
156 if (p2.length() > 0) {
157 ippath = p1 + "-" + p2;
158 } else {
159 ippath = p1;
160 }
161 } else {
162 ippath = p1;
163 }
164
165 DEBUG_auth("IPPath is ", ippath);
166
167 // change '.' to '-'
168 ippath.swapChar('.', '-');
169 DEBUG_auth("IPPath is ", ippath);
170 if (getdnstxt(ippath)) {
171 string = userst.user;
172 is_real_user = true;
173 authrec.user_name = string;
174 authrec.filter_group = userst.group;
175 authrec.user_source = "dnsa";
176 return E2AUTH_OK_GOT_GROUP;
177 } else {
178 // redirect code
179 if (redirect_to_auth) { // used to force log-in
180 // dnsauth plug-in must be last
181 // check if this is request to authurl or in authexception lists
182 if (h.url().startsWith(authprefix) || inAuthByPassLists(h)) {
183 string = "::auth::";
184 userst.user = string;
185 userst.group = 0;
186 return E2AUTH_OK_NOPERSIST;
187 } else {
188 string = authurl + "=" + h.URLEncode();
189 return E2AUTH_REDIRECT;
190 }
191 } else {
192 return E2AUTH_NOMATCH; // used for log-in on demand
193 // needs dnsauth plug-in to be first
194 }
195 }
196 }
197
198 int dnsauthinstance::determineGroup(std::string &user, int &fg, ListContainer &uglc)
199 {
200 fg = userst.group;
201 DEBUG_auth("Matched user", user, " to group ", fg, " in cached DNS record");
202 return E2AUTH_OK;
203 }
204
205 bool dnsauthinstance::getdnstxt(String &ippath)
206 {
207 // get info from DNS
208 union {
209 HEADER hdr;
210 u_char buf[NS_PACKETSZ];
211 } response;
212 int responseLen;
213
214 ns_msg handle; /* handle for response message */
215 responseLen = res_querydomain(ippath.c_str(), basedomain.c_str(), ns_c_in, ns_t_txt, (u_char *)&response, sizeof(response));
216 if (responseLen < 0) {
217 DEBUG_auth("DNS query returned error ", dns_error(h_errno));
218 return false;
219 }
220 if (ns_initparse(response.buf, responseLen, &handle) < 0) {
221 DEBUG_auth("ns_initparse returned error ", strerror(errno));
222 return false;
223 }
224
225 //int rrnum; /* resource record number */
226 ns_rr rr; /* expanded resource record */
227 //u_char *cp;
228 //char ans[MAXDNAME];
229
230 int i = ns_msg_count(handle, ns_s_an);
231 if (i > 0) {
232 if (ns_parserr(&handle, ns_s_an, 0, &rr)) {
233 DEBUG_auth("ns_paserr returned error ", strerror(errno));
234 return false;
235 } else {
236 if (ns_rr_type(rr) == ns_t_txt) {
237 DEBUG_auth("ns_rr_rdlen returned ", ns_rr_rdlen(rr));
238 u_char *k = (u_char *)ns_rr_rdata(rr);
239 char p[400];
240 unsigned int j = 0;
241 for (unsigned int j1 = 1; j1 < ns_rr_rdlen(rr); j1++) {
242 p[j++] = k[j1];
243 }
244 p[j] = '\0';
245 DEBUG_auth("ns_rr_data returned ", p);
246 String dnstxt(p);
247 userst.user = dnstxt.before(",");
248 userst.group = (dnstxt.after(",")).toInteger() - 1;
249 return true;
250 }
251 }
252 }
253 return true;
254 }
255
256 String dnsauthinstance::dns_error(int herror)
257 {
258
259 String s;
260
261 switch (herror) {
262 case HOST_NOT_FOUND:
263 s = "HOST_NOT_FOUND";
264 break;
265 case TRY_AGAIN:
266 s = "TRY_AGAIN - DNS server failure";
267 break;
268 case NO_DATA:
269 s = "NO_DATA - unexpected DNS error";
270 break;
271 default:
272 String S2(herror);
273 s = "DNS - Unexpected error number " + S2;
274 break;
275 }
276 return s;
277 }
278
279 bool dnsauthinstance::inAuthByPassLists(HTTPHeader &h)
280 {
281 String url = h.url();
282 String urld = h.decode(url);
283 //FOptionContainer* foc = o.currentLists()->fg[0];
284 url.removePTP();
285 if (url.contains("/")) {
286 url = url.before("/");
287 }
288
289 // Find other way to do this using Storyboarding
290
291 // bool is_ip = (*foc).isIPHostname(url);
292 // bool is_ssl = h.requestType() == "CONNECT";
293
294 // if ((*foc).inAuthExceptionSiteList(urld, true, is_ip, is_ssl)) {
295 // // exceptioncat = (*o.lm.l[(*o.fg[filtergroup]).exception_site_list]).lastcategory.toCharArray();
296 // return true;
297 //} else if ((*foc).inAuthExceptionURLList(urld, true, is_ip, is_ssl)) {
298 // // exceptioncat = (*o.lm.l[(*o.fg[filtergroup]).exception_url_list]).lastcategory.toCharArray();
299 // return true;
300 //}
301 return false;
302 }