| 1 |
/* |
| 2 |
* Copyright (C) 2018 Atheme Development Group (https://atheme.github.io/) |
| 3 |
* Rights to this code are documented in doc/LICENSE. |
| 4 |
* |
| 5 |
* Restrict authentication methods by name or by host. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "atheme-compat.h" |
| 9 |
|
| 10 |
#if (CURRENT_ABI_REVISION >= 730000) |
| 11 |
|
| 12 |
struct blacklist_entry |
| 13 |
{ |
| 14 |
mowgli_node_t node; |
| 15 |
char * data; |
| 16 |
}; |
| 17 |
|
| 18 |
static mowgli_list_t restricted_hosts; |
| 19 |
static mowgli_list_t permitted_mechanisms; |
| 20 |
|
| 21 |
static struct service *saslsvs = NULL; |
| 22 |
|
| 23 |
static void |
| 24 |
blacklist_clear_list(mowgli_list_t *const restrict list) |
| 25 |
{ |
| 26 |
mowgli_node_t *n, *tn; |
| 27 |
|
| 28 |
MOWGLI_ITER_FOREACH_SAFE(n, tn, list->head) |
| 29 |
{ |
| 30 |
struct blacklist_entry *const entry = n->data; |
| 31 |
|
| 32 |
(void) mowgli_node_delete(n, list); |
| 33 |
(void) sfree(entry->data); |
| 34 |
(void) sfree(entry); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
static void |
| 39 |
blacklist_process_configentry(mowgli_config_file_entry_t *const restrict ce, mowgli_list_t *const restrict list, |
| 40 |
const char *const restrict name) |
| 41 |
{ |
| 42 |
if (! ce->entries) |
| 43 |
return; |
| 44 |
|
| 45 |
(void) blacklist_clear_list(list); |
| 46 |
|
| 47 |
mowgli_config_file_entry_t *subce; |
| 48 |
|
| 49 |
MOWGLI_ITER_FOREACH(subce, ce->entries) |
| 50 |
{ |
| 51 |
if (! subce->entries) |
| 52 |
{ |
| 53 |
struct blacklist_entry *const entry = smalloc(sizeof *entry); |
| 54 |
|
| 55 |
entry->data = sstrdup(subce->varname); |
| 56 |
|
| 57 |
(void) mowgli_node_add(entry, &entry->node, list); |
| 58 |
} |
| 59 |
else |
| 60 |
(void) conf_report_warning(ce, "Invalid saslserv::%s entry", name); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
static int |
| 65 |
c_restricted_hosts(mowgli_config_file_entry_t *const restrict ce) |
| 66 |
{ |
| 67 |
(void) blacklist_process_configentry(ce, &restricted_hosts, "restricted_hosts"); |
| 68 |
|
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
static int |
| 73 |
c_permitted_mechanisms(mowgli_config_file_entry_t *const restrict ce) |
| 74 |
{ |
| 75 |
(void) blacklist_process_configentry(ce, &permitted_mechanisms, "permitted_mechanisms"); |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
static bool |
| 81 |
is_restricted_host(char const *const restrict host) |
| 82 |
{ |
| 83 |
if (! host || ! *host) |
| 84 |
return false; |
| 85 |
|
| 86 |
mowgli_node_t *n; |
| 87 |
|
| 88 |
MOWGLI_ITER_FOREACH(n, restricted_hosts.head) |
| 89 |
{ |
| 90 |
struct blacklist_entry *const entry = n->data; |
| 91 |
|
| 92 |
if (match(entry->data, host) == 0 || match_ips(entry->data, host) == 0) |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
static bool |
| 100 |
is_permitted_mechanism(char const *const restrict mech) |
| 101 |
{ |
| 102 |
if (! mech || ! *mech) |
| 103 |
return false; |
| 104 |
|
| 105 |
mowgli_node_t *n; |
| 106 |
|
| 107 |
MOWGLI_ITER_FOREACH(n, permitted_mechanisms.head) |
| 108 |
{ |
| 109 |
struct blacklist_entry *const entry = n->data; |
| 110 |
|
| 111 |
if (strcmp(entry->data, mech) == 0) |
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
static void |
| 119 |
blacklist_can_login(hook_user_login_check_t *const restrict c) |
| 120 |
{ |
| 121 |
if (! c->si) |
| 122 |
return; |
| 123 |
|
| 124 |
if (c->si->service == saslsvs) |
| 125 |
{ |
| 126 |
const struct sasl_sourceinfo *const ssi = (struct sasl_sourceinfo *) c->si; |
| 127 |
|
| 128 |
if (! ssi->sess || ! ssi->sess->mechptr || ! (ssi->sess->host || ssi->sess->ip)) |
| 129 |
return; |
| 130 |
|
| 131 |
if (is_restricted_host(ssi->sess->host) || is_restricted_host(ssi->sess->ip)) |
| 132 |
{ |
| 133 |
char const *const log_target = service_get_log_target(saslsvs); |
| 134 |
|
| 135 |
if (! is_permitted_mechanism(ssi->sess->mechptr->name)) |
| 136 |
{ |
| 137 |
(void) slog(CMDLOG_LOGIN, "%s %s:%s failed LOGIN to \2%s\2 ('%s' not allowed)", |
| 138 |
log_target, entity(c->mu)->name, ssi->sess->uid, entity(c->mu)->name, |
| 139 |
ssi->sess->mechptr->name); |
| 140 |
|
| 141 |
c->allowed = false; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
else if (c->si->su) |
| 146 |
{ |
| 147 |
if (is_restricted_host(c->si->su->host) || is_restricted_host(c->si->su->chost) || |
| 148 |
is_restricted_host(c->si->su->vhost) || is_restricted_host(c->si->su->ip)) |
| 149 |
{ |
| 150 |
(void) logcommand(c->si, CMDLOG_LOGIN, "failed IDENTIFY to \2%s\2 (restricted address)", |
| 151 |
entity(c->mu)->name); |
| 152 |
|
| 153 |
c->allowed = false; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
static void |
| 159 |
blacklist_can_register(hook_user_register_check_t *const restrict c) |
| 160 |
{ |
| 161 |
if (is_restricted_host(c->si->su->host) || is_restricted_host(c->si->su->chost) || |
| 162 |
is_restricted_host(c->si->su->vhost) || is_restricted_host(c->si->su->ip)) |
| 163 |
{ |
| 164 |
(void) logcommand(c->si, CMDLOG_LOGIN, "denied REGISTER of \2%s\2 (restricted address)", c->account); |
| 165 |
|
| 166 |
c->approved++; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
static void |
| 171 |
mod_init(module_t *const restrict m) |
| 172 |
{ |
| 173 |
// We do this to depend on saslserv/main, so that if it is reloaded, we are too |
| 174 |
const struct sasl_core_functions *sasl_core_functions; |
| 175 |
MODULE_TRY_REQUEST_SYMBOL(m, sasl_core_functions, "saslserv/main", "sasl_core_functions"); |
| 176 |
|
| 177 |
if (! (saslsvs = service_find("saslserv"))) |
| 178 |
{ |
| 179 |
(void) slog(LG_ERROR, "%s: could not find SASLServ (BUG!)", m->name); |
| 180 |
|
| 181 |
m->mflags |= MODFLAG_FAIL; |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
(void) hook_add_event("user_can_login"); |
| 186 |
(void) hook_add_user_can_login(&blacklist_can_login); |
| 187 |
|
| 188 |
(void) hook_add_event("user_can_register"); |
| 189 |
(void) hook_add_user_can_register(&blacklist_can_register); |
| 190 |
|
| 191 |
(void) add_conf_item("RESTRICTED_HOSTS", &saslsvs->conf_table, &c_restricted_hosts); |
| 192 |
(void) add_conf_item("PERMITTED_MECHANISMS", &saslsvs->conf_table, &c_permitted_mechanisms); |
| 193 |
} |
| 194 |
|
| 195 |
static void |
| 196 |
mod_deinit(const module_unload_intent_t ATHEME_VATTR_UNUSED intent) |
| 197 |
{ |
| 198 |
(void) del_conf_item("RESTRICTED_HOSTS", &saslsvs->conf_table); |
| 199 |
(void) del_conf_item("PERMITTED_MECHANISMS", &saslsvs->conf_table); |
| 200 |
|
| 201 |
(void) hook_del_user_can_login(&blacklist_can_login); |
| 202 |
(void) hook_del_user_can_register(&blacklist_can_register); |
| 203 |
|
| 204 |
(void) blacklist_clear_list(&restricted_hosts); |
| 205 |
(void) blacklist_clear_list(&permitted_mechanisms); |
| 206 |
} |
| 207 |
|
| 208 |
#else /* (CURRENT_ABI_REVISION >= 730000) */ |
| 209 |
|
| 210 |
static void |
| 211 |
mod_init(module_t *const restrict m) |
| 212 |
{ |
| 213 |
(void) slog(LG_ERROR, "%s: this module only works with Atheme v7.3.0 or above", m->name); |
| 214 |
|
| 215 |
m->mflags |= MODFLAG_FAIL; |
| 216 |
} |
| 217 |
|
| 218 |
static void |
| 219 |
mod_deinit(const module_unload_intent_t ATHEME_VATTR_UNUSED intent) |
| 220 |
{ |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
#endif /* (CURRENT_ABI_REVISION < 730000) */ |
| 225 |
|
| 226 |
VENDOR_DECLARE_MODULE_V1("contrib/sasl_blacklist", MODULE_UNLOAD_CAPABILITY_OK, CONTRIB_VENDOR_FREENODE) |