Debian Codegraph @

src/authplugins/ip.cpp

raw
1 // IP (range, subnet) auth plugin
2
3 // For all support, instructions and copyright go to:
4 // http://e2guardian.org/
5 // Released under the GPL v2, with the OpenSSL exception described in the README file.
6
7 // INCLUDES
8 #ifdef HAVE_CONFIG_H
9 #include "e2config.h"
10 #endif
11
12 #include "../Auth.hpp"
13 #include "../RegExp.hpp"
14 #include "../OptionContainer.hpp"
15 #include "../Logger.hpp"
16
17 #include <algorithm>
18 #include <unistd.h>
19 #include <iostream>
20 #include <fstream>
21
22 // GLOBALS
23
24 extern OptionContainer o;
25
26 // DECLARATIONS
27
28 // structs linking subnets and IP ranges to filter groups
29 //struct subnetstruct
30 // uint32_t maskedaddr;
31 //uint32_t mask;
32 //int group;
33 //};
34
35 //struct rangestruct {
36 // uint32_t startaddr;
37 // uint32_t endaddr;
38 // int group;
39 //};
40
41 // class for linking IPs to filter groups, complete with comparison operators
42 // allowing standard C++ sort to work
43 #ifdef NODEF
44 class ip
45 {
46 public:
47 ip(uint32_t a, int g)
48 {
49 addr = a;
50 group = g;
51 };
52 uint32_t addr;
53 int group;
54 int operator<(const ip &a) const
55 {
56 return addr < a.addr;
57 };
58 int operator<(const uint32_t &a) const
59 {
60 return addr < a;
61 };
62 int operator==(const uint32_t &a) const
63 {
64 return a == addr;
65 };
66 };
67 #endif
68
69 // class name is relevant!
70 class ipinstance : public AuthPlugin
71 {
72 public:
73 // keep credentials for the whole of a connection - IP isn't going to change.
74 // not quite true - what about downstream proxy with x-forwarded-for?
75 ipinstance(ConfigVar &definition)
76 : AuthPlugin(definition)
77 {
78 if (!o.use_xforwardedfor)
79 is_connection_based = true;
80 client_ip_based = true;
81 };
82
83 int identify(Socket &peercon, Socket &proxycon, HTTPHeader &h, std::string &string, bool &is_real_user, auth_rec &authrec,NaughtyFilter &cm);
84 //int determineGroup(std::string &user, int &fg, ListContainer &uglc);
85
86 int init(void *args);
87 int quit();
88
89 private:
90 //std::vector<ip> iplist;
91 //std::list<subnetstruct> ipsubnetlist;
92 std::vector<rangestruct> iprangelist;
93
94 int readIPMelangeList(const char *filename);
95 int searchList(int a, int s, const uint32_t &ip);
96 int inList(const uint32_t &ip);
97 int inSubnet(const uint32_t &ip);
98 int inRange(const uint32_t &ip);
99 };
100
101 // IMPLEMENTATION
102
103 // class factory code *MUST* be included in every plugin
104
105 AuthPlugin *ipcreate(ConfigVar &definition)
106 {
107 return new ipinstance(definition);
108 }
109
110 // end of Class factory
111
112 //
113 //
114 // Standard plugin funcs
115 //
116 //
117
118 // plugin quit - clear IP, subnet & range lists
119 int ipinstance::quit()
120 {
121 //iplist.clear();
122 //ipsubnetlist.clear();
123 //iprangelist.clear();
124 return 0;
125 }
126
127 // plugin init - read in ip melange list
128 int ipinstance::init(void *args)
129 {
130 AuthPlugin::init(args);
131 StoryBoardOptions::SB_entry_map sen;
132 sen.entry_function = cv["story_function"];
133 if (sen.entry_function.length() > 0) {
134 sen.entry_id = ENT_STORYA_AUTH_IP;
135 story_entry = sen.entry_id;
136 o.story.auth_entry_dq.push_back(sen);
137 return 0;
138 } else {
139 E2LOGGER_error("No story_function defined in IP auth plugin config");
140 return -1;
141 }
142 }
143
144 // IP-based 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 ipinstance::identify(Socket &peercon, Socket &proxycon, HTTPHeader &h, std::string &string, bool &is_real_user, auth_rec &authrec,NaughtyFilter &cm)
148 {
149 // we don't get usernames out of this plugin, just a filter group
150 // for now, use the IP as the username
151 bool use_xforwardedfor;
152 use_xforwardedfor = false;
153 if (o.use_xforwardedfor == 1) {
154 if (o.net.xforwardedfor_filter_ip.size() > 0) {
155 const char *ip = peercon.getPeerIP().c_str();
156 for (unsigned int i = 0; i < o.net.xforwardedfor_filter_ip.size(); i++) {
157 if (strcmp(ip, o.net.xforwardedfor_filter_ip[i].c_str()) == 0) {
158 use_xforwardedfor = true;
159 break;
160 }
161 }
162 } else {
163 use_xforwardedfor = true;
164 }
165 }
166 if (use_xforwardedfor == 1) {
167 // grab the X-Forwarded-For IP if available
168 string = h.getXForwardedForIP();
169 // or try the client IP from the header
170 if (string.length() == 0)
171 string = h.getClientIP();
172 // otherwise, grab the IP directly from the client connection
173 if (string.length() == 0)
174 string = peercon.getPeerIP();
175 } else {
176 string = h.getClientIP();
177 // otherwise, grab the IP directly from the client connection
178 if (string.length() == 0)
179 string = peercon.getPeerIP();
180 }
181 authrec.user_name = string;
182 authrec.user_source = "ip";;
183 is_real_user = false;
184 return E2AUTH_OK;
185 }
186
187 #ifdef NODEF
188 int ipinstance::determineGroup(std::string &user, int &rfg, ListContainer &uglc)
189 {
190 struct in_addr sin;
191 inet_aton(user.c_str(), &sin);
192 uint32_t addr = ntohl(sin.s_addr);
193 int fg;
194 // check straight IPs, subnets, and ranges
195 // fg = inList(addr);
196 if (fg >= 0) {
197 rfg = fg;
198 DEBUG_auth("Matched IP ", user, " to straight IP list");
199 return E2AUTH_OK;
200 }
201 // fg = inSubnet(addr);
202 if (fg >= 0) {
203 rfg = fg;
204 DEBUG_auth("Matched IP ", user, " to subnet");
205 return E2AUTH_OK;
206 }
207 // fg = inRange(addr);
208 if (fg >= 0) {
209 rfg = fg;
210 DEBUG_auth("Matched IP ", user, " to range");
211 return E2AUTH_OK;
212 }
213 DEBUG_auth("Matched IP ", user, " to nothing");
214 return E2AUTH_NOMATCH;
215 }
216
217 #endif
218
219