| 1 |
// AuthPlugin class - interface for plugins for retrieving client usernames |
| 2 |
// and filter group membership |
| 3 |
|
| 4 |
// For all support, instructions and copyright go to: |
| 5 |
// http://e2guardian.org/ |
| 6 |
// Released under the GPL v2, with the OpenSSL exception described in the README file. |
| 7 |
|
| 8 |
// INCLUDES |
| 9 |
|
| 10 |
#ifdef HAVE_CONFIG_H |
| 11 |
#include "e2config.h" |
| 12 |
#endif |
| 13 |
#include "Auth.hpp" |
| 14 |
#include "OptionContainer.hpp" |
| 15 |
#include "LOptionContainer.hpp" |
| 16 |
#include "Logger.hpp" |
| 17 |
|
| 18 |
#include <iostream> |
| 19 |
|
| 20 |
// GLOBALS |
| 21 |
|
| 22 |
extern OptionContainer o; |
| 23 |
|
| 24 |
extern authcreate_t identcreate; |
| 25 |
extern authcreate_t ipcreate; |
| 26 |
extern authcreate_t portcreate; |
| 27 |
extern authcreate_t headercreate; |
| 28 |
extern authcreate_t PF_basic_create; |
| 29 |
extern authcreate_t bearer_basic_create; |
| 30 |
|
| 31 |
#ifdef PRT_DNSAUTH |
| 32 |
extern authcreate_t dnsauthcreate; |
| 33 |
#endif |
| 34 |
|
| 35 |
|
| 36 |
// IMPLEMENTATION |
| 37 |
|
| 38 |
AuthPlugin::AuthPlugin(ConfigVar &definition) |
| 39 |
: is_connection_based(false) |
| 40 |
{ |
| 41 |
cv = definition; |
| 42 |
pluginName = cv["plugname"]; |
| 43 |
} |
| 44 |
|
| 45 |
int AuthPlugin::init(void *args) |
| 46 |
{ |
| 47 |
read_def_fg(); |
| 48 |
read_ports(); |
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
int AuthPlugin::quit() |
| 53 |
{ |
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
String AuthPlugin::getPluginName() |
| 58 |
{ |
| 59 |
return pluginName; |
| 60 |
} |
| 61 |
|
| 62 |
// determine what filter group the given username is in |
| 63 |
// return -1 when user not found |
| 64 |
int AuthPlugin::determineGroup(std::string &user, int &fg, StoryBoard & story, NaughtyFilter &cm ) |
| 65 |
{ |
| 66 |
if (user.length() < 1 || user == "-") { |
| 67 |
return E2AUTH_NOMATCH; |
| 68 |
} |
| 69 |
String u(user); |
| 70 |
String lastcategory; |
| 71 |
u.toLower(); // since the filtergroupslist is read in in lowercase, we should do this. |
| 72 |
user = u.toCharArray(); // also pass back to ConnectionHandler, so appears lowercase in logs |
| 73 |
// String ue(u); |
| 74 |
// ue += "="; |
| 75 |
|
| 76 |
//char *i = ldl->filter_groups_list.findStartsWithPartial(ue.toCharArray(), lastcategory); |
| 77 |
// char *i = uglc.findStartsWithPartial(ue.toCharArray(), lastcategory); |
| 78 |
cm.user = user; |
| 79 |
if (!story.runFunctEntry(story_entry,cm)) { |
| 80 |
int t = get_default(!cm.request_header->isProxyRequest); |
| 81 |
if (t > 0) { |
| 82 |
fg = --t; |
| 83 |
cm.authrec->group_source = "pdef"; |
| 84 |
return E2AUTH_OK; |
| 85 |
} |
| 86 |
DEBUG_auth("User not in filter groups list for: ", pluginName); |
| 87 |
return E2AUTH_NOGROUP; |
| 88 |
} |
| 89 |
|
| 90 |
DEBUG_auth("Group found for: ", user, " in ", pluginName); |
| 91 |
fg = cm.filtergroup; |
| 92 |
return E2AUTH_OK; |
| 93 |
} |
| 94 |
|
| 95 |
// take in a configuration file, find the AuthPlugin class associated with the plugname variable, and return an instance |
| 96 |
AuthPlugin *auth_plugin_load(const char *pluginConfigPath) |
| 97 |
{ |
| 98 |
ConfigVar cv; |
| 99 |
|
| 100 |
if (cv.readVar(pluginConfigPath, "=") > 0) { |
| 101 |
E2LOGGER_error("Unable to load plugin config: ", pluginConfigPath); |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
|
| 105 |
String plugname(cv["plugname"]); |
| 106 |
if (plugname.length() < 1) { |
| 107 |
E2LOGGER_error("Unable read plugin config plugname variable: ", pluginConfigPath); |
| 108 |
return NULL; |
| 109 |
} |
| 110 |
|
| 111 |
DEBUG_auth("plugname :", plugname); |
| 112 |
|
| 113 |
|
| 114 |
if (plugname == "ident") { |
| 115 |
DEBUG_auth("Enabling ident server auth plugin"); |
| 116 |
return identcreate(cv); |
| 117 |
} |
| 118 |
|
| 119 |
if (plugname == "ip") { |
| 120 |
DEBUG_auth("Enabling IP-based auth plugin"); |
| 121 |
return ipcreate(cv); |
| 122 |
} |
| 123 |
|
| 124 |
if (plugname == "port") { |
| 125 |
DEBUG_auth("Enabling port-based auth plugin"); |
| 126 |
return portcreate(cv); |
| 127 |
} |
| 128 |
|
| 129 |
if (plugname == "proxy-header") { |
| 130 |
DEBUG_auth("Enabling proxy-header auth plugin"); |
| 131 |
return headercreate(cv); |
| 132 |
} |
| 133 |
|
| 134 |
if (plugname == "pf-basic") { |
| 135 |
DEBUG_auth("Enabling proxy-header auth plugin"); |
| 136 |
return PF_basic_create(cv); |
| 137 |
} |
| 138 |
if (plugname == "bearer-basic") { |
| 139 |
DEBUG_auth("Enabling bearer-basic auth plugin"); |
| 140 |
return bearer_basic_create(cv); |
| 141 |
} |
| 142 |
|
| 143 |
#ifdef PRT_DNSAUTH |
| 144 |
if (plugname == "dnsauth") { |
| 145 |
DEBUG_auth("Enabling DNS-based auth plugin"); |
| 146 |
return dnsauthcreate(cv); |
| 147 |
} |
| 148 |
#endif |
| 149 |
|
| 150 |
|
| 151 |
E2LOGGER_error("Unable to load plugin: ", pluginConfigPath); |
| 152 |
return NULL; |
| 153 |
} |
| 154 |
|
| 155 |
int AuthPlugin::get_default(bool is_transparent) { |
| 156 |
if (is_transparent && tran_default_fg > 0) { |
| 157 |
// syslog(LOG_ERR, "%spa default set as %d", thread_id.c_str(), tran_default_fg); |
| 158 |
return tran_default_fg; |
| 159 |
} |
| 160 |
else if (default_fg > 0) { |
| 161 |
//syslog(LOG_ERR, "%spa default set as %d", thread_id.c_str(), default_fg); |
| 162 |
return default_fg; |
| 163 |
} |
| 164 |
return 0; |
| 165 |
} |
| 166 |
|
| 167 |
void AuthPlugin::read_def_fg() { |
| 168 |
String t = cv["defaultfiltergroup"]; |
| 169 |
int i = t.toInteger(); |
| 170 |
if(i > 0 && i <= o.filter.filter_groups) { |
| 171 |
default_fg = i; |
| 172 |
} |
| 173 |
t = cv["defaulttransparentfiltergroup"]; |
| 174 |
i = t.toInteger(); |
| 175 |
if(i > 0 && i <= o.filter.filter_groups) { |
| 176 |
tran_default_fg = i; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
void AuthPlugin::read_ports() { |
| 181 |
String t = cv["ports"]; |
| 182 |
// DEBUG_auth("ports = ",t); |
| 183 |
if (t.empty()) return; |
| 184 |
while (t.contains(",")) { |
| 185 |
String p = t.before(","); |
| 186 |
int pn = p.toInteger(); |
| 187 |
portlist.push_back(pn); |
| 188 |
t = t.after(","); |
| 189 |
} |
| 190 |
int pn = t.toInteger(); |
| 191 |
portlist.push_back(pn); |
| 192 |
//DEBUG_auth("portlist pushto = ",pn); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
bool AuthPlugin::port_matched(int &port) { |
| 197 |
if (portlist.empty()) { |
| 198 |
DEBUG_auth("`Port list empty - true"); |
| 199 |
return true; |
| 200 |
} |
| 201 |
for (auto rec : portlist) { |
| 202 |
DEBUG_auth(" port is ", port, " list is ", rec); |
| 203 |
if (rec == port) { |
| 204 |
return true; |
| 205 |
} |
| 206 |
} |
| 207 |
return false; |
| 208 |
|
| 209 |
} |
| 210 |
|