| 1 |
/* |
| 2 |
* Copyright (c) 2005 Atheme Development Group |
| 3 |
* Rights to this code are documented in doc/LICENSE. |
| 4 |
* |
| 5 |
* This file contains the main() routine. |
| 6 |
* |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "atheme.h" |
| 10 |
#include "uplink.h" |
| 11 |
|
| 12 |
DECLARE_MODULE_V1 |
| 13 |
( |
| 14 |
"saslserv/main", false, _modinit, _moddeinit, |
| 15 |
PACKAGE_STRING, |
| 16 |
VENDOR_STRING |
| 17 |
); |
| 18 |
|
| 19 |
mowgli_list_t sessions; |
| 20 |
static mowgli_list_t sasl_mechanisms; |
| 21 |
static char mechlist_string[400]; |
| 22 |
static bool hide_server_names; |
| 23 |
|
| 24 |
static sasl_session_t *find_session(const char *uid); |
| 25 |
static sasl_session_t *make_session(const char *uid, server_t *server); |
| 26 |
static void reset_session(sasl_session_t *p); |
| 27 |
static void destroy_session(sasl_session_t *p); |
| 28 |
static void reset_or_destroy_session(sasl_session_t *p); |
| 29 |
static void sasl_logcommand(sasl_session_t *p, myuser_t *login, int level, const char *fmt, ...); |
| 30 |
static void sasl_input(sasl_message_t *smsg); |
| 31 |
static void sasl_packet(sasl_session_t *p, char *buf, int len); |
| 32 |
static void sasl_write(char *target, char *data, int length); |
| 33 |
static bool may_impersonate(myuser_t *source_mu, myuser_t *target_mu); |
| 34 |
static myuser_t *login_user(sasl_session_t *p); |
| 35 |
static void sasl_newuser(hook_user_nick_t *data); |
| 36 |
static void sasl_server_eob(server_t *s); |
| 37 |
static void delete_stale(void *vptr); |
| 38 |
static void sasl_mech_register(sasl_mechanism_t *mech); |
| 39 |
static void sasl_mech_unregister(sasl_mechanism_t *mech); |
| 40 |
static void mechlist_build_string(char *ptr, size_t buflen); |
| 41 |
static void mechlist_do_rebuild(); |
| 42 |
static const char *sasl_get_source_name(sourceinfo_t *si); |
| 43 |
|
| 44 |
sasl_mech_register_func_t sasl_mech_register_funcs = { &sasl_mech_register, &sasl_mech_unregister }; |
| 45 |
|
| 46 |
/* main services client routine */ |
| 47 |
static void saslserv(sourceinfo_t *si, int parc, char *parv[]) |
| 48 |
{ |
| 49 |
char *cmd; |
| 50 |
char *text; |
| 51 |
char orig[BUFSIZE]; |
| 52 |
|
| 53 |
/* this should never happen */ |
| 54 |
if (parv[0][0] == '&') |
| 55 |
{ |
| 56 |
slog(LG_ERROR, "services(): got parv with local channel: %s", parv[0]); |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
/* make a copy of the original for debugging */ |
| 61 |
mowgli_strlcpy(orig, parv[parc - 1], BUFSIZE); |
| 62 |
|
| 63 |
/* lets go through this to get the command */ |
| 64 |
cmd = strtok(parv[parc - 1], " "); |
| 65 |
text = strtok(NULL, ""); |
| 66 |
|
| 67 |
if (!cmd) |
| 68 |
return; |
| 69 |
if (*orig == '\001') |
| 70 |
{ |
| 71 |
handle_ctcp_common(si, cmd, text); |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
command_fail(si, fault_noprivs, "This service exists to identify " |
| 76 |
"connecting clients to the network. It has no " |
| 77 |
"public interface."); |
| 78 |
} |
| 79 |
|
| 80 |
service_t *saslsvs = NULL; |
| 81 |
mowgli_eventloop_timer_t *delete_stale_timer = NULL; |
| 82 |
|
| 83 |
static void sasl_mech_register(sasl_mechanism_t *mech) |
| 84 |
{ |
| 85 |
mowgli_node_t *node; |
| 86 |
|
| 87 |
slog(LG_DEBUG, "sasl_mech_register(): registering %s", mech->name); |
| 88 |
|
| 89 |
node = mowgli_node_create(); |
| 90 |
mowgli_node_add(mech, node, &sasl_mechanisms); |
| 91 |
|
| 92 |
mechlist_do_rebuild(); |
| 93 |
} |
| 94 |
|
| 95 |
static void sasl_mech_unregister(sasl_mechanism_t *mech) |
| 96 |
{ |
| 97 |
mowgli_node_t *n, *tn; |
| 98 |
sasl_session_t *session; |
| 99 |
|
| 100 |
slog(LG_DEBUG, "sasl_mech_unregister(): unregistering %s", mech->name); |
| 101 |
|
| 102 |
MOWGLI_ITER_FOREACH_SAFE(n, tn, sessions.head) |
| 103 |
{ |
| 104 |
session = n->data; |
| 105 |
if (session->mechptr == mech) |
| 106 |
{ |
| 107 |
slog(LG_DEBUG, "sasl_mech_unregister(): destroying session %s", session->uid); |
| 108 |
destroy_session(session); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
MOWGLI_ITER_FOREACH_SAFE(n, tn, sasl_mechanisms.head) |
| 113 |
{ |
| 114 |
if (n->data == mech) |
| 115 |
{ |
| 116 |
mowgli_node_delete(n, &sasl_mechanisms); |
| 117 |
mowgli_node_free(n); |
| 118 |
|
| 119 |
mechlist_do_rebuild(); |
| 120 |
break; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
void _modinit(module_t *m) |
| 126 |
{ |
| 127 |
hook_add_event("sasl_input"); |
| 128 |
hook_add_sasl_input(sasl_input); |
| 129 |
hook_add_event("user_add"); |
| 130 |
hook_add_user_add(sasl_newuser); |
| 131 |
hook_add_event("server_eob"); |
| 132 |
hook_add_server_eob(sasl_server_eob); |
| 133 |
hook_add_event("sasl_may_impersonate"); |
| 134 |
hook_add_event("user_can_login"); |
| 135 |
|
| 136 |
delete_stale_timer = mowgli_timer_add(base_eventloop, "sasl_delete_stale", delete_stale, NULL, 30); |
| 137 |
|
| 138 |
saslsvs = service_add("saslserv", saslserv); |
| 139 |
add_bool_conf_item("HIDE_SERVER_NAMES", &saslsvs->conf_table, 0, &hide_server_names, false); |
| 140 |
authservice_loaded++; |
| 141 |
} |
| 142 |
|
| 143 |
void _moddeinit(module_unload_intent_t intent) |
| 144 |
{ |
| 145 |
mowgli_node_t *n, *tn; |
| 146 |
|
| 147 |
hook_del_sasl_input(sasl_input); |
| 148 |
hook_del_user_add(sasl_newuser); |
| 149 |
hook_del_server_eob(sasl_server_eob); |
| 150 |
|
| 151 |
mowgli_timer_destroy(base_eventloop, delete_stale_timer); |
| 152 |
|
| 153 |
del_conf_item("HIDE_SERVER_NAMES", &saslsvs->conf_table); |
| 154 |
|
| 155 |
if (saslsvs != NULL) |
| 156 |
service_delete(saslsvs); |
| 157 |
|
| 158 |
authservice_loaded--; |
| 159 |
|
| 160 |
if (sessions.head != NULL) |
| 161 |
slog(LG_DEBUG, "saslserv/main: shutting down with a non-empty session list, a mech did not unregister itself!"); |
| 162 |
|
| 163 |
MOWGLI_ITER_FOREACH_SAFE(n, tn, sessions.head) |
| 164 |
{ |
| 165 |
destroy_session(n->data); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/* |
| 170 |
* Begin SASL-specific code |
| 171 |
*/ |
| 172 |
|
| 173 |
/* find an existing session by uid */ |
| 174 |
static sasl_session_t *find_session(const char *uid) |
| 175 |
{ |
| 176 |
sasl_session_t *p; |
| 177 |
mowgli_node_t *n; |
| 178 |
|
| 179 |
if (uid == NULL) |
| 180 |
return NULL; |
| 181 |
|
| 182 |
MOWGLI_ITER_FOREACH(n, sessions.head) |
| 183 |
{ |
| 184 |
p = n->data; |
| 185 |
if(p->uid != NULL && !strcmp(p->uid, uid)) |
| 186 |
return p; |
| 187 |
} |
| 188 |
|
| 189 |
return NULL; |
| 190 |
} |
| 191 |
|
| 192 |
/* create a new session if it does not already exist */ |
| 193 |
static sasl_session_t *make_session(const char *uid, server_t *server) |
| 194 |
{ |
| 195 |
sasl_session_t *p = find_session(uid); |
| 196 |
mowgli_node_t *n; |
| 197 |
|
| 198 |
if(p) |
| 199 |
return p; |
| 200 |
|
| 201 |
p = malloc(sizeof(sasl_session_t)); |
| 202 |
memset(p, 0, sizeof(sasl_session_t)); |
| 203 |
p->uid = strdup(uid); |
| 204 |
p->server = server; |
| 205 |
n = mowgli_node_create(); |
| 206 |
mowgli_node_add(p, n, &sessions); |
| 207 |
|
| 208 |
return p; |
| 209 |
} |
| 210 |
|
| 211 |
/* reset a session */ |
| 212 |
static void reset_session(sasl_session_t *p) |
| 213 |
{ |
| 214 |
if (p->mechptr && p->mechptr->mech_finish) |
| 215 |
p->mechptr->mech_finish(p); |
| 216 |
|
| 217 |
p->mechptr = NULL; |
| 218 |
} |
| 219 |
|
| 220 |
/* free a session and all its contents */ |
| 221 |
static void destroy_session(sasl_session_t *p) |
| 222 |
{ |
| 223 |
mowgli_node_t *n, *tn; |
| 224 |
myuser_t *mu; |
| 225 |
|
| 226 |
if (p->flags & ASASL_NEED_LOG && p->username != NULL) |
| 227 |
{ |
| 228 |
mu = myuser_find_by_nick(p->username); |
| 229 |
if (mu != NULL && !(ircd->flags & IRCD_SASL_USE_PUID)) |
| 230 |
sasl_logcommand(p, mu, CMDLOG_LOGIN, "LOGIN (session timed out)"); |
| 231 |
} |
| 232 |
|
| 233 |
MOWGLI_ITER_FOREACH_SAFE(n, tn, sessions.head) |
| 234 |
{ |
| 235 |
if(n->data == p) |
| 236 |
{ |
| 237 |
mowgli_node_delete(n, &sessions); |
| 238 |
mowgli_node_free(n); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
reset_session(p); |
| 243 |
free(p->uid); |
| 244 |
free(p->buf); |
| 245 |
free(p->username); |
| 246 |
free(p->certfp); |
| 247 |
free(p->authzid); |
| 248 |
free(p->pendingeid); |
| 249 |
free(p->host); |
| 250 |
free(p->ip); |
| 251 |
free(p); |
| 252 |
} |
| 253 |
|
| 254 |
/* reset or destroy a session */ |
| 255 |
static void reset_or_destroy_session(sasl_session_t *p) |
| 256 |
{ |
| 257 |
if (p->pendingeid) |
| 258 |
reset_session(p); |
| 259 |
else |
| 260 |
destroy_session(p); |
| 261 |
} |
| 262 |
|
| 263 |
typedef struct { |
| 264 |
sourceinfo_t parent; |
| 265 |
sasl_session_t *sess; |
| 266 |
} sasl_sourceinfo_t; |
| 267 |
|
| 268 |
static void sasl_sourceinfo_delete(sasl_sourceinfo_t *ssi) |
| 269 |
{ |
| 270 |
return_if_fail(ssi != NULL); |
| 271 |
|
| 272 |
free(ssi); |
| 273 |
} |
| 274 |
|
| 275 |
static struct sourceinfo_vtable sasl_vtable = { |
| 276 |
.description = "SASL", |
| 277 |
.get_source_name = sasl_get_source_name, |
| 278 |
.get_source_mask = sasl_get_source_name |
| 279 |
}; |
| 280 |
|
| 281 |
static sourceinfo_t *sasl_sourceinfo_create(sasl_session_t *p) |
| 282 |
{ |
| 283 |
sasl_sourceinfo_t *ssi; |
| 284 |
|
| 285 |
ssi = smalloc(sizeof(sasl_sourceinfo_t)); |
| 286 |
object_init(object(ssi), "<sasl sourceinfo>", (destructor_t) sasl_sourceinfo_delete); |
| 287 |
|
| 288 |
ssi->parent.s = p->server; |
| 289 |
ssi->parent.connection = curr_uplink->conn; |
| 290 |
if (p->host) |
| 291 |
ssi->parent.sourcedesc = p->host; |
| 292 |
ssi->parent.service = saslsvs; |
| 293 |
ssi->parent.v = &sasl_vtable; |
| 294 |
ssi->parent.force_language = language_find("en"); |
| 295 |
ssi->sess = p; |
| 296 |
|
| 297 |
return &ssi->parent; |
| 298 |
} |
| 299 |
|
| 300 |
/* interpret an AUTHENTICATE message */ |
| 301 |
static void sasl_input(sasl_message_t *smsg) |
| 302 |
{ |
| 303 |
sasl_session_t *p = make_session(smsg->uid, smsg->server); |
| 304 |
int len = strlen(smsg->buf); |
| 305 |
char *tmpbuf; |
| 306 |
int tmplen; |
| 307 |
|
| 308 |
switch(smsg->mode) |
| 309 |
{ |
| 310 |
case 'H': |
| 311 |
/* (H)ost information */ |
| 312 |
p->host = sstrdup(smsg->buf); |
| 313 |
p->ip = sstrdup(smsg->ext); |
| 314 |
return; |
| 315 |
|
| 316 |
case 'S': |
| 317 |
/* (S)tart authentication */ |
| 318 |
if(smsg->mode == 'S' && smsg->ext != NULL && !strcmp(smsg->buf, "EXTERNAL")) |
| 319 |
{ |
| 320 |
free(p->certfp); |
| 321 |
p->certfp = sstrdup(smsg->ext); |
| 322 |
} |
| 323 |
/* fallthrough to 'C' */ |
| 324 |
|
| 325 |
case 'C': |
| 326 |
/* (C)lient data */ |
| 327 |
if(p->buf == NULL) |
| 328 |
{ |
| 329 |
p->buf = (char *)malloc(len + 1); |
| 330 |
p->p = p->buf; |
| 331 |
p->len = len; |
| 332 |
} |
| 333 |
else |
| 334 |
{ |
| 335 |
if(p->len + len + 1 > 8192) /* This is a little much... */ |
| 336 |
{ |
| 337 |
sasl_sts(p->uid, 'D', "F"); |
| 338 |
reset_or_destroy_session(p); |
| 339 |
return; |
| 340 |
} |
| 341 |
|
| 342 |
p->buf = (char *)realloc(p->buf, p->len + len + 1); |
| 343 |
p->p = p->buf + p->len; |
| 344 |
p->len += len; |
| 345 |
} |
| 346 |
|
| 347 |
memcpy(p->p, smsg->buf, len); |
| 348 |
|
| 349 |
/* Messages not exactly 400 bytes are the end of a packet. */ |
| 350 |
if(len < 400) |
| 351 |
{ |
| 352 |
p->buf[p->len] = '\0'; |
| 353 |
tmpbuf = p->buf; |
| 354 |
tmplen = p->len; |
| 355 |
p->buf = p->p = NULL; |
| 356 |
p->len = 0; |
| 357 |
sasl_packet(p, tmpbuf, tmplen); |
| 358 |
free(tmpbuf); |
| 359 |
} |
| 360 |
return; |
| 361 |
|
| 362 |
case 'D': |
| 363 |
/* (D)one -- when we receive it, means client abort */ |
| 364 |
reset_or_destroy_session(p); |
| 365 |
return; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
/* find a mechanism by name */ |
| 370 |
static sasl_mechanism_t *find_mechanism(char *name) |
| 371 |
{ |
| 372 |
mowgli_node_t *n; |
| 373 |
sasl_mechanism_t *mptr; |
| 374 |
|
| 375 |
MOWGLI_ITER_FOREACH(n, sasl_mechanisms.head) |
| 376 |
{ |
| 377 |
mptr = n->data; |
| 378 |
if(!strcmp(mptr->name, name)) |
| 379 |
return mptr; |
| 380 |
} |
| 381 |
|
| 382 |
slog(LG_DEBUG, "find_mechanism(): cannot find mechanism `%s'!", name); |
| 383 |
|
| 384 |
return NULL; |
| 385 |
} |
| 386 |
|
| 387 |
static void sasl_server_eob(server_t *s) |
| 388 |
{ |
| 389 |
/* new server online, push mechlist to make sure it's using the current one */ |
| 390 |
sasl_mechlist_sts(mechlist_string); |
| 391 |
} |
| 392 |
|
| 393 |
static void mechlist_do_rebuild() |
| 394 |
{ |
| 395 |
mechlist_build_string(mechlist_string, sizeof(mechlist_string)); |
| 396 |
|
| 397 |
/* push mechanism list to the network */ |
| 398 |
if (me.connected) |
| 399 |
sasl_mechlist_sts(mechlist_string); |
| 400 |
} |
| 401 |
|
| 402 |
static void mechlist_build_string(char *ptr, size_t buflen) |
| 403 |
{ |
| 404 |
int l = 0; |
| 405 |
mowgli_node_t *n; |
| 406 |
|
| 407 |
MOWGLI_ITER_FOREACH(n, sasl_mechanisms.head) |
| 408 |
{ |
| 409 |
sasl_mechanism_t *mptr = n->data; |
| 410 |
if(l + strlen(mptr->name) > buflen) |
| 411 |
break; |
| 412 |
strcpy(ptr, mptr->name); |
| 413 |
ptr += strlen(mptr->name); |
| 414 |
*ptr++ = ','; |
| 415 |
l += strlen(mptr->name) + 1; |
| 416 |
} |
| 417 |
|
| 418 |
if(l) |
| 419 |
ptr--; |
| 420 |
*ptr = '\0'; |
| 421 |
} |
| 422 |
|
| 423 |
/* given an entire sasl message, advance session by passing data to mechanism |
| 424 |
* and feeding returned data back to client. |
| 425 |
*/ |
| 426 |
static void sasl_packet(sasl_session_t *p, char *buf, int len) |
| 427 |
{ |
| 428 |
int rc; |
| 429 |
size_t tlen = 0; |
| 430 |
char *cloak, *out = NULL; |
| 431 |
char temp[BUFSIZE]; |
| 432 |
char mech[61]; |
| 433 |
size_t out_len = 0; |
| 434 |
metadata_t *md; |
| 435 |
|
| 436 |
/* First piece of data in a session is the name of |
| 437 |
* the SASL mechanism that will be used. |
| 438 |
*/ |
| 439 |
if(!p->mechptr) |
| 440 |
{ |
| 441 |
if(len > 60) |
| 442 |
{ |
| 443 |
sasl_sts(p->uid, 'D', "F"); |
| 444 |
reset_or_destroy_session(p); |
| 445 |
return; |
| 446 |
} |
| 447 |
|
| 448 |
memcpy(mech, buf, len); |
| 449 |
mech[len] = '\0'; |
| 450 |
|
| 451 |
if(!(p->mechptr = find_mechanism(mech))) |
| 452 |
{ |
| 453 |
sasl_sts(p->uid, 'M', mechlist_string); |
| 454 |
|
| 455 |
sasl_sts(p->uid, 'D', "F"); |
| 456 |
reset_or_destroy_session(p); |
| 457 |
return; |
| 458 |
} |
| 459 |
|
| 460 |
rc = p->mechptr->mech_start(p, &out, &out_len); |
| 461 |
}else{ |
| 462 |
if(len == 1 && *buf == '+') |
| 463 |
rc = p->mechptr->mech_step(p, (char []) { '\0' }, 0, |
| 464 |
&out, &out_len); |
| 465 |
else if ((tlen = base64_decode(buf, temp, BUFSIZE)) && |
| 466 |
tlen != (size_t)-1) |
| 467 |
rc = p->mechptr->mech_step(p, temp, tlen, &out, &out_len); |
| 468 |
else |
| 469 |
rc = ASASL_FAIL; |
| 470 |
} |
| 471 |
|
| 472 |
/* Some progress has been made, reset timeout. */ |
| 473 |
p->flags &= ~ASASL_MARKED_FOR_DELETION; |
| 474 |
|
| 475 |
if(rc == ASASL_DONE) |
| 476 |
{ |
| 477 |
myuser_t *mu = login_user(p); |
| 478 |
if(mu) |
| 479 |
{ |
| 480 |
if ((md = metadata_find(mu, "private:usercloak"))) |
| 481 |
cloak = md->value; |
| 482 |
else |
| 483 |
cloak = "*"; |
| 484 |
|
| 485 |
if (!(mu->flags & MU_WAITAUTH)) |
| 486 |
svslogin_sts(p->uid, "*", "*", cloak, mu); |
| 487 |
sasl_sts(p->uid, 'D', "S"); |
| 488 |
if (p->pendingeid) |
| 489 |
free(p->pendingeid); |
| 490 |
p->pendingeid = strdup(entity(mu)->id); |
| 491 |
/* Will destroy session on introduction of user to net. */ |
| 492 |
} |
| 493 |
else |
| 494 |
{ |
| 495 |
sasl_sts(p->uid, 'D', "F"); |
| 496 |
reset_or_destroy_session(p); |
| 497 |
} |
| 498 |
return; |
| 499 |
} |
| 500 |
else if(rc == ASASL_MORE) |
| 501 |
{ |
| 502 |
if(out_len) |
| 503 |
{ |
| 504 |
if(base64_encode(out, out_len, temp, BUFSIZE)) |
| 505 |
{ |
| 506 |
sasl_write(p->uid, temp, strlen(temp)); |
| 507 |
free(out); |
| 508 |
return; |
| 509 |
} |
| 510 |
} |
| 511 |
else |
| 512 |
{ |
| 513 |
sasl_sts(p->uid, 'C', "+"); |
| 514 |
free(out); |
| 515 |
return; |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
/* If we reach this, they failed SASL auth, so if they were trying |
| 520 |
* to identify as a specific user, bad_password them. |
| 521 |
*/ |
| 522 |
if (p->username) |
| 523 |
{ |
| 524 |
myuser_t *mu = myuser_find_by_nick(p->username); |
| 525 |
if (mu) |
| 526 |
{ |
| 527 |
sourceinfo_t *si = sasl_sourceinfo_create(p); |
| 528 |
bad_password(si, mu); |
| 529 |
object_unref(si); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
free(out); |
| 534 |
sasl_sts(p->uid, 'D', "F"); |
| 535 |
reset_or_destroy_session(p); |
| 536 |
} |
| 537 |
|
| 538 |
/* output an arbitrary amount of data to the SASL client */ |
| 539 |
static void sasl_write(char *target, char *data, int length) |
| 540 |
{ |
| 541 |
char out[401]; |
| 542 |
int last = 400, rem = length; |
| 543 |
|
| 544 |
while(rem) |
| 545 |
{ |
| 546 |
int nbytes = rem > 400 ? 400 : rem; |
| 547 |
memcpy(out, data, nbytes); |
| 548 |
out[nbytes] = '\0'; |
| 549 |
sasl_sts(target, 'C', out); |
| 550 |
|
| 551 |
data += nbytes; |
| 552 |
rem -= nbytes; |
| 553 |
last = nbytes; |
| 554 |
} |
| 555 |
|
| 556 |
/* The end of a packet is indicated by a string not of length 400. |
| 557 |
* If last piece is exactly 400 in size, send an empty string to |
| 558 |
* finish the transaction. |
| 559 |
* Also if there is no data at all. |
| 560 |
*/ |
| 561 |
if(last == 400) |
| 562 |
sasl_sts(target, 'C', "+"); |
| 563 |
} |
| 564 |
|
| 565 |
static void sasl_logcommand(sasl_session_t *p, myuser_t *mu, int level, const char *fmt, ...) |
| 566 |
{ |
| 567 |
va_list args; |
| 568 |
char lbuf[BUFSIZE]; |
| 569 |
|
| 570 |
va_start(args, fmt); |
| 571 |
vsnprintf(lbuf, BUFSIZE, fmt, args); |
| 572 |
slog(level, "%s %s:%s %s", service_get_log_target(saslsvs), mu ? entity(mu)->name : "", p->uid, lbuf); |
| 573 |
va_end(args); |
| 574 |
} |
| 575 |
|
| 576 |
static bool may_impersonate(myuser_t *source_mu, myuser_t *target_mu) |
| 577 |
{ |
| 578 |
hook_sasl_may_impersonate_t req; |
| 579 |
char priv[512] = PRIV_IMPERSONATE_ANY; |
| 580 |
char *classname; |
| 581 |
|
| 582 |
/* Allow same (although this function won't get called in that case anyway) */ |
| 583 |
if(source_mu == target_mu) |
| 584 |
return true; |
| 585 |
|
| 586 |
/* Check for wildcard priv */ |
| 587 |
if(has_priv_myuser(source_mu, priv)) |
| 588 |
return true; |
| 589 |
|
| 590 |
/* Check for target-operclass specific priv */ |
| 591 |
classname = (target_mu->soper && target_mu->soper->classname) |
| 592 |
? target_mu->soper->classname : "user"; |
| 593 |
|
| 594 |
snprintf(priv, sizeof(priv), PRIV_IMPERSONATE_CLASS_FMT, classname); |
| 595 |
|
| 596 |
if(has_priv_myuser(source_mu, priv)) |
| 597 |
return true; |
| 598 |
|
| 599 |
/* Check for target-entity specific priv */ |
| 600 |
snprintf(priv, sizeof(priv), PRIV_IMPERSONATE_ENTITY_FMT, entity(target_mu)->name); |
| 601 |
|
| 602 |
if(has_priv_myuser(source_mu, priv)) |
| 603 |
return true; |
| 604 |
|
| 605 |
/* Allow modules to check too */ |
| 606 |
req.source_mu = source_mu; |
| 607 |
req.target_mu = target_mu; |
| 608 |
req.allowed = false; |
| 609 |
|
| 610 |
hook_call_sasl_may_impersonate(&req); |
| 611 |
|
| 612 |
return req.allowed; |
| 613 |
} |
| 614 |
|
| 615 |
/* authenticated, now double check that their account is ok for login */ |
| 616 |
static myuser_t *login_user(sasl_session_t *p) |
| 617 |
{ |
| 618 |
myuser_t *source_mu, *target_mu; |
| 619 |
hook_user_login_check_t req; |
| 620 |
|
| 621 |
/* source_mu is the user whose credentials we verified ("authentication id") */ |
| 622 |
/* target_mu is the user who will be ultimately logged in ("authorization id") */ |
| 623 |
|
| 624 |
source_mu = myuser_find_by_nick(p->username); |
| 625 |
if(source_mu == NULL) |
| 626 |
return NULL; |
| 627 |
|
| 628 |
req.si = sasl_sourceinfo_create(p); |
| 629 |
req.mu = source_mu; |
| 630 |
req.allowed = true; |
| 631 |
hook_call_user_can_login(&req); |
| 632 |
object_unref(req.si); |
| 633 |
if (!req.allowed) |
| 634 |
{ |
| 635 |
sasl_logcommand(p, source_mu, CMDLOG_LOGIN, "failed LOGIN to \2%s\2 (denied by hook)", entity(source_mu)->name); |
| 636 |
return NULL; |
| 637 |
} |
| 638 |
|
| 639 |
if(p->authzid && *p->authzid) |
| 640 |
{ |
| 641 |
target_mu = myuser_find_by_nick(p->authzid); |
| 642 |
if(target_mu == NULL) |
| 643 |
return NULL; |
| 644 |
} |
| 645 |
else |
| 646 |
{ |
| 647 |
target_mu = source_mu; |
| 648 |
if(p->authzid != NULL) |
| 649 |
free(p->authzid); |
| 650 |
p->authzid = sstrdup(p->username); |
| 651 |
} |
| 652 |
|
| 653 |
if(metadata_find(source_mu, "private:freeze:freezer")) |
| 654 |
{ |
| 655 |
sasl_logcommand(p, source_mu, CMDLOG_LOGIN, "failed LOGIN to \2%s\2 (frozen)", entity(source_mu)->name); |
| 656 |
return NULL; |
| 657 |
} |
| 658 |
|
| 659 |
if(target_mu != source_mu) |
| 660 |
{ |
| 661 |
if(!may_impersonate(source_mu, target_mu)) |
| 662 |
{ |
| 663 |
sasl_logcommand(p, source_mu, CMDLOG_LOGIN, "denied IMPERSONATE by \2%s\2 to \2%s\2", entity(source_mu)->name, entity(target_mu)->name); |
| 664 |
return NULL; |
| 665 |
} |
| 666 |
|
| 667 |
sasl_logcommand(p, source_mu, CMDLOG_LOGIN, "allowed IMPERSONATE by \2%s\2 to \2%s\2", entity(source_mu)->name, entity(target_mu)->name); |
| 668 |
|
| 669 |
req.si = sasl_sourceinfo_create(p); |
| 670 |
req.mu = target_mu; |
| 671 |
req.allowed = true; |
| 672 |
hook_call_user_can_login(&req); |
| 673 |
object_unref(req.si); |
| 674 |
if (!req.allowed) |
| 675 |
{ |
| 676 |
sasl_logcommand(p, source_mu, CMDLOG_LOGIN, "failed LOGIN to \2%s\2 (denied by hook)", entity(target_mu)->name); |
| 677 |
return NULL; |
| 678 |
} |
| 679 |
|
| 680 |
if(metadata_find(target_mu, "private:freeze:freezer")) |
| 681 |
{ |
| 682 |
sasl_logcommand(p, target_mu, CMDLOG_LOGIN, "failed LOGIN to \2%s\2 (frozen)", entity(target_mu)->name); |
| 683 |
return NULL; |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
if(MOWGLI_LIST_LENGTH(&target_mu->logins) >= me.maxlogins) |
| 688 |
{ |
| 689 |
sasl_logcommand(p, target_mu, CMDLOG_LOGIN, "failed LOGIN to \2%s\2 (too many logins)", entity(target_mu)->name); |
| 690 |
return NULL; |
| 691 |
} |
| 692 |
|
| 693 |
/* Log it with the full n!u@h later */ |
| 694 |
p->flags |= ASASL_NEED_LOG; |
| 695 |
|
| 696 |
/* We just did SASL authentication for a user. With IRCds which do not |
| 697 |
* have unique UIDs for users, we will likely be expecting the login |
| 698 |
* data to be bursted. As a result, we should give the core a heads' |
| 699 |
* up that this is going to happen so that hooks will be properly |
| 700 |
* fired... |
| 701 |
*/ |
| 702 |
if(ircd->flags & IRCD_SASL_USE_PUID) |
| 703 |
{ |
| 704 |
target_mu->flags &= ~MU_NOBURSTLOGIN; |
| 705 |
target_mu->flags |= MU_PENDINGLOGIN; |
| 706 |
} |
| 707 |
|
| 708 |
return target_mu; |
| 709 |
} |
| 710 |
|
| 711 |
/* clean up after a user who is finally on the net */ |
| 712 |
static void sasl_newuser(hook_user_nick_t *data) |
| 713 |
{ |
| 714 |
user_t *u = data->u; |
| 715 |
sasl_mechanism_t *mptr; |
| 716 |
sasl_session_t *p; |
| 717 |
myuser_t *mu; |
| 718 |
|
| 719 |
/* If the user has been killed, don't do anything. */ |
| 720 |
if (!u) |
| 721 |
return; |
| 722 |
|
| 723 |
p = find_session(u->uid); |
| 724 |
|
| 725 |
/* Not concerned unless it's a SASL login. */ |
| 726 |
if(p == NULL) |
| 727 |
return; |
| 728 |
|
| 729 |
/* We will log it ourselves, if needed */ |
| 730 |
p->flags &= ~ASASL_NEED_LOG; |
| 731 |
|
| 732 |
/* Find the account */ |
| 733 |
mu = ((p->pendingeid != NULL) ? myuser_find_uid(p->pendingeid) : NULL); |
| 734 |
if (mu == NULL) |
| 735 |
{ |
| 736 |
if (p->pendingeid != NULL) |
| 737 |
notice(saslsvs->nick, u->nick, "Account %s dropped, login cancelled", |
| 738 |
p->authzid ? p->authzid : "??"); |
| 739 |
|
| 740 |
destroy_session(p); |
| 741 |
/* We'll remove their ircd login in handle_burstlogin() */ |
| 742 |
return; |
| 743 |
} |
| 744 |
|
| 745 |
mptr = p->mechptr; |
| 746 |
|
| 747 |
destroy_session(p); |
| 748 |
|
| 749 |
myuser_login(saslsvs, u, mu, false); |
| 750 |
|
| 751 |
logcommand_user(saslsvs, u, CMDLOG_LOGIN, "LOGIN (%s)", mptr->name); |
| 752 |
} |
| 753 |
|
| 754 |
/* This function is run approximately once every 30 seconds. |
| 755 |
* It looks for flagged sessions, and deletes them, while |
| 756 |
* flagging all the others. This way stale sessions are deleted |
| 757 |
* after no more than 60 seconds. |
| 758 |
*/ |
| 759 |
static void delete_stale(void *vptr) |
| 760 |
{ |
| 761 |
sasl_session_t *p; |
| 762 |
mowgli_node_t *n, *tn; |
| 763 |
|
| 764 |
MOWGLI_ITER_FOREACH_SAFE(n, tn, sessions.head) |
| 765 |
{ |
| 766 |
p = n->data; |
| 767 |
if(p->flags & ASASL_MARKED_FOR_DELETION) |
| 768 |
{ |
| 769 |
mowgli_node_delete(n, &sessions); |
| 770 |
destroy_session(p); |
| 771 |
mowgli_node_free(n); |
| 772 |
} else |
| 773 |
p->flags |= ASASL_MARKED_FOR_DELETION; |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
static const char *sasl_get_source_name(sourceinfo_t *si) |
| 778 |
{ |
| 779 |
static char result[HOSTLEN+NICKLEN+10]; |
| 780 |
char description[BUFSIZE]; |
| 781 |
sasl_sourceinfo_t *ssi = (sasl_sourceinfo_t *) si; |
| 782 |
|
| 783 |
if (ssi->sess->server && !hide_server_names) |
| 784 |
snprintf(description, BUFSIZE, "Unknown user on %s (via SASL)", ssi->sess->server->name); |
| 785 |
else |
| 786 |
mowgli_strlcpy(description, "Unknown user (via SASL)", sizeof description); |
| 787 |
|
| 788 |
/* we can reasonably assume that si->v is non-null as this is part of the SASL vtable */ |
| 789 |
if (si->sourcedesc) |
| 790 |
snprintf(result, sizeof result, "<%s:%s>%s", description, si->sourcedesc, si->smu ? entity(si->smu)->name : ""); |
| 791 |
else |
| 792 |
snprintf(result, sizeof result, "<%s>%s", description, si->smu ? entity(si->smu)->name : ""); |
| 793 |
|
| 794 |
return result; |
| 795 |
} |
| 796 |
|
| 797 |
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs |
| 798 |
* vim:ts=8 |
| 799 |
* vim:sw=8 |
| 800 |
* vim:noexpandtab |
| 801 |
*/ |