1 /* $OpenBSD: ldapclient.c,v 1.36 2016/04/10 09:59:21 jmatthew Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Alexander Schrijver <aschrijver@openbsd.org> 5 * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/types.h> 21 #include <sys/queue.h> 22 #include <sys/socket.h> 23 #include <sys/tree.h> 24 25 #include <netinet/in.h> 26 #include <arpa/inet.h> 27 28 #include <netdb.h> 29 #include <errno.h> 30 #include <err.h> 31 #include <signal.h> 32 #include <event.h> 33 #include <fcntl.h> 34 #include <unistd.h> 35 #include <pwd.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <limits.h> 40 41 #include "aldap.h" 42 #include "ypldap.h" 43 44 void client_sig_handler(int, short, void *); 45 void client_dispatch_dns(int, short, void *); 46 void client_dispatch_parent(int, short, void *); 47 void client_shutdown(void); 48 void client_connect(int, short, void *); 49 void client_configure(struct env *); 50 void client_periodic_update(int, short, void *); 51 int client_build_req(struct idm *, struct idm_req *, struct aldap_message *, 52 int, int); 53 int client_search_idm(struct env *, struct idm *, struct aldap *, 54 char **, char *, int, int, enum imsg_type); 55 int client_try_idm(struct env *, struct idm *); 56 int client_addr_init(struct idm *); 57 int client_addr_free(struct idm *); 58 59 struct aldap *client_aldap_open(struct ypldap_addr_list *); 60 61 /* 62 * dummy wrapper to provide aldap_init with its fd's. 63 */ 64 struct aldap * 65 client_aldap_open(struct ypldap_addr_list *addr) 66 { 67 int fd = -1; 68 struct ypldap_addr *p; 69 70 TAILQ_FOREACH(p, addr, next) { 71 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; 72 struct sockaddr *sa = (struct sockaddr *)&p->ss; 73 74 if (getnameinfo(sa, SA_LEN(sa), hbuf, sizeof(hbuf), sbuf, 75 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) 76 errx(1, "could not get numeric hostname"); 77 78 if ((fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) 79 return NULL; 80 81 if (connect(fd, sa, SA_LEN(sa)) == 0) 82 break; 83 84 warn("connect to %s port %s (%s) failed", hbuf, sbuf, "tcp"); 85 close(fd); 86 } 87 88 if (fd == -1) 89 return NULL; 90 91 return aldap_init(fd); 92 } 93 94 int 95 client_addr_init(struct idm *idm) 96 { 97 struct sockaddr_in *sa_in; 98 struct sockaddr_in6 *sa_in6; 99 struct ypldap_addr *h; 100 101 TAILQ_FOREACH(h, &idm->idm_addr, next) { 102 switch (h->ss.ss_family) { 103 case AF_INET: 104 sa_in = (struct sockaddr_in *)&h->ss; 105 if (ntohs(sa_in->sin_port) == 0) 106 sa_in->sin_port = htons(LDAP_PORT); 107 idm->idm_state = STATE_DNS_DONE; 108 break; 109 case AF_INET6: 110 sa_in6 = (struct sockaddr_in6 *)&h->ss; 111 if (ntohs(sa_in6->sin6_port) == 0) 112 sa_in6->sin6_port = htons(LDAP_PORT); 113 idm->idm_state = STATE_DNS_DONE; 114 break; 115 default: 116 fatalx("king bula sez: wrong AF in client_addr_init"); 117 /* not reached */ 118 } 119 } 120 121 return (0); 122 } 123 124 int 125 client_addr_free(struct idm *idm) 126 { 127 struct ypldap_addr *h; 128 129 while (!TAILQ_EMPTY(&idm->idm_addr)) { 130 h = TAILQ_FIRST(&idm->idm_addr); 131 TAILQ_REMOVE(&idm->idm_addr, h, next); 132 free(h); 133 } 134 135 return (0); 136 } 137 138 void 139 client_sig_handler(int sig, short event, void *p) 140 { 141 switch (sig) { 142 case SIGINT: 143 case SIGTERM: 144 client_shutdown(); 145 break; 146 default: 147 fatalx("unexpected signal"); 148 } 149 } 150 151 void 152 client_dispatch_dns(int fd, short events, void *p) 153 { 154 struct imsg imsg; 155 u_int16_t dlen; 156 u_char *data; 157 struct ypldap_addr *h; 158 int n, wait_cnt = 0; 159 struct idm *idm; 160 int shut = 0; 161 162 struct env *env = p; 163 struct imsgev *iev = env->sc_iev_dns; 164 struct imsgbuf *ibuf = &iev->ibuf; 165 166 if ((events & (EV_READ | EV_WRITE)) == 0) 167 fatalx("unknown event"); 168 169 if (events & EV_READ) { 170 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) 171 fatal("imsg_read error"); 172 if (n == 0) 173 shut = 1; 174 } 175 if (events & EV_WRITE) { 176 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 177 fatal("msgbuf_write"); 178 if (n == 0) 179 shut = 1; 180 goto done; 181 } 182 183 for (;;) { 184 if ((n = imsg_get(ibuf, &imsg)) == -1) 185 fatal("client_dispatch_dns: imsg_get error"); 186 if (n == 0) 187 break; 188 189 switch (imsg.hdr.type) { 190 case IMSG_HOST_DNS: 191 TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) 192 if (idm->idm_id == imsg.hdr.peerid) 193 break; 194 if (idm == NULL) { 195 log_warnx("IMSG_HOST_DNS with invalid peerID"); 196 break; 197 } 198 if (!TAILQ_EMPTY(&idm->idm_addr)) { 199 log_warnx("IMSG_HOST_DNS but addrs set!"); 200 break; 201 } 202 203 dlen = imsg.hdr.len - IMSG_HEADER_SIZE; 204 if (dlen == 0) { /* no data -> temp error */ 205 idm->idm_state = STATE_DNS_TEMPFAIL; 206 break; 207 } 208 209 data = (u_char *)imsg.data; 210 while (dlen >= sizeof(struct sockaddr_storage)) { 211 if ((h = calloc(1, sizeof(*h))) == NULL) 212 fatal(NULL); 213 memcpy(&h->ss, data, sizeof(h->ss)); 214 TAILQ_INSERT_HEAD(&idm->idm_addr, h, next); 215 216 data += sizeof(h->ss); 217 dlen -= sizeof(h->ss); 218 } 219 if (dlen != 0) 220 fatalx("IMSG_HOST_DNS: dlen != 0"); 221 222 client_addr_init(idm); 223 224 break; 225 default: 226 break; 227 } 228 imsg_free(&imsg); 229 } 230 231 TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) { 232 if (client_try_idm(env, idm) == -1) 233 idm->idm_state = STATE_LDAP_FAIL; 234 235 if (idm->idm_state < STATE_LDAP_DONE) 236 wait_cnt++; 237 } 238 if (wait_cnt == 0) 239 imsg_compose_event(env->sc_iev, IMSG_END_UPDATE, 0, 0, -1, 240 NULL, 0); 241 242 done: 243 if (!shut) 244 imsg_event_add(iev); 245 else { 246 /* this pipe is dead, so remove the event handler */ 247 event_del(&iev->ev); 248 event_loopexit(NULL); 249 } 250 } 251 252 void 253 client_dispatch_parent(int fd, short events, void *p) 254 { 255 int n; 256 int shut = 0; 257 struct imsg imsg; 258 struct env *env = p; 259 struct imsgev *iev = env->sc_iev; 260 struct imsgbuf *ibuf = &iev->ibuf; 261 262 if ((events & (EV_READ | EV_WRITE)) == 0) 263 fatalx("unknown event"); 264 265 if (events & EV_READ) { 266 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN) 267 fatal("imsg_read error"); 268 if (n == 0) 269 shut = 1; 270 } 271 if (events & EV_WRITE) { 272 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 273 fatal("msgbuf_write"); 274 if (n == 0) 275 shut = 1; 276 goto done; 277 } 278 279 for (;;) { 280 if ((n = imsg_get(ibuf, &imsg)) == -1) 281 fatal("client_dispatch_parent: imsg_get error"); 282 if (n == 0) 283 break; 284 285 switch (imsg.hdr.type) { 286 case IMSG_CONF_START: { 287 struct env params; 288 289 if (env->sc_flags & F_CONFIGURING) { 290 log_warnx("configuration already in progress"); 291 break; 292 } 293 memcpy(¶ms, imsg.data, sizeof(params)); 294 log_debug("configuration starting"); 295 env->sc_flags |= F_CONFIGURING; 296 purge_config(env); 297 memcpy(&env->sc_conf_tv, ¶ms.sc_conf_tv, 298 sizeof(env->sc_conf_tv)); 299 env->sc_flags |= params.sc_flags; 300 break; 301 } 302 case IMSG_CONF_IDM: { 303 struct idm *idm; 304 305 if (!(env->sc_flags & F_CONFIGURING)) 306 break; 307 if ((idm = calloc(1, sizeof(*idm))) == NULL) 308 fatal(NULL); 309 memcpy(idm, imsg.data, sizeof(*idm)); 310 idm->idm_env = env; 311 TAILQ_INSERT_TAIL(&env->sc_idms, idm, idm_entry); 312 break; 313 } 314 case IMSG_CONF_END: 315 env->sc_flags &= ~F_CONFIGURING; 316 log_debug("applying configuration"); 317 client_configure(env); 318 break; 319 default: 320 log_debug("client_dispatch_parent: unexpect imsg %d", 321 imsg.hdr.type); 322 323 break; 324 } 325 imsg_free(&imsg); 326 } 327 328 done: 329 if (!shut) 330 imsg_event_add(iev); 331 else { 332 /* this pipe is dead, so remove the event handler */ 333 event_del(&iev->ev); 334 event_loopexit(NULL); 335 } 336 } 337 338 void 339 client_shutdown(void) 340 { 341 log_info("ldap client exiting"); 342 _exit(0); 343 } 344 345 pid_t 346 ldapclient(int pipe_main2client[2]) 347 { 348 pid_t pid, dns_pid; 349 int pipe_dns[2]; 350 struct passwd *pw; 351 struct event ev_sigint; 352 struct event ev_sigterm; 353 struct env env; 354 355 switch (pid = fork()) { 356 case -1: 357 fatal("cannot fork"); 358 break; 359 case 0: 360 break; 361 default: 362 return (pid); 363 } 364 365 bzero(&env, sizeof(env)); 366 TAILQ_INIT(&env.sc_idms); 367 368 if ((pw = getpwnam(YPLDAP_USER)) == NULL) 369 fatal("getpwnam"); 370 371 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_dns) == -1) 372 fatal("socketpair"); 373 dns_pid = ypldap_dns(pipe_dns, pw); 374 close(pipe_dns[1]); 375 376 #ifndef DEBUG 377 if (chroot(pw->pw_dir) == -1) 378 fatal("chroot"); 379 if (chdir("/") == -1) 380 fatal("chdir"); 381 #else 382 #warning disabling chrooting in DEBUG mode 383 #endif 384 setproctitle("ldap client"); 385 ypldap_process = PROC_CLIENT; 386 387 #ifndef DEBUG 388 if (setgroups(1, &pw->pw_gid) || 389 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) || 390 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) 391 fatal("cannot drop privileges"); 392 #else 393 #warning disabling privilege revocation in DEBUG mode 394 #endif 395 396 if (pledge("stdio inet", NULL) == -1) 397 fatal("pledge"); 398 399 event_init(); 400 signal(SIGPIPE, SIG_IGN); 401 signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL); 402 signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL); 403 signal_add(&ev_sigint, NULL); 404 signal_add(&ev_sigterm, NULL); 405 406 close(pipe_main2client[0]); 407 if ((env.sc_iev = calloc(1, sizeof(*env.sc_iev))) == NULL) 408 fatal(NULL); 409 if ((env.sc_iev_dns = calloc(1, sizeof(*env.sc_iev_dns))) == NULL) 410 fatal(NULL); 411 412 env.sc_iev->events = EV_READ; 413 env.sc_iev->data = &env; 414 imsg_init(&env.sc_iev->ibuf, pipe_main2client[1]); 415 env.sc_iev->handler = client_dispatch_parent; 416 event_set(&env.sc_iev->ev, env.sc_iev->ibuf.fd, env.sc_iev->events, 417 env.sc_iev->handler, &env); 418 event_add(&env.sc_iev->ev, NULL); 419 420 env.sc_iev_dns->events = EV_READ; 421 env.sc_iev_dns->data = &env; 422 imsg_init(&env.sc_iev_dns->ibuf, pipe_dns[0]); 423 env.sc_iev_dns->handler = client_dispatch_dns; 424 event_set(&env.sc_iev_dns->ev, env.sc_iev_dns->ibuf.fd, 425 env.sc_iev_dns->events, env.sc_iev_dns->handler, &env); 426 event_add(&env.sc_iev_dns->ev, NULL); 427 428 event_dispatch(); 429 client_shutdown(); 430 431 return (0); 432 433 } 434 435 int 436 client_build_req(struct idm *idm, struct idm_req *ir, struct aldap_message *m, 437 int min_attr, int max_attr) 438 { 439 char **ldap_attrs; 440 int i, k; 441 442 bzero(ir, sizeof(*ir)); 443 for (i = min_attr; i < max_attr; i++) { 444 if (idm->idm_flags & F_FIXED_ATTR(i)) { 445 if (strlcat(ir->ir_line, idm->idm_attrs[i], 446 sizeof(ir->ir_line)) >= sizeof(ir->ir_line)) 447 /* 448 * entry yields a line > 1024, trash it. 449 */ 450 return (-1); 451 452 if (i == ATTR_UID) { 453 ir->ir_key.ik_uid = strtonum( 454 idm->idm_attrs[i], 0, 455 UID_MAX, NULL); 456 } else if (i == ATTR_GR_GID) { 457 ir->ir_key.ik_gid = strtonum( 458 idm->idm_attrs[i], 0, 459 GID_MAX, NULL); 460 } 461 } else if (idm->idm_list & F_LIST(i)) { 462 aldap_match_attr(m, idm->idm_attrs[i], &ldap_attrs); 463 for (k = 0; k >= 0 && ldap_attrs && ldap_attrs[k] != NULL; k++) { 464 /* XXX: Fail when attributes have illegal characters e.g. ',' */ 465 if (strlcat(ir->ir_line, ldap_attrs[k], 466 sizeof(ir->ir_line)) >= sizeof(ir->ir_line)) 467 continue; 468 if (ldap_attrs[k+1] != NULL) 469 if (strlcat(ir->ir_line, ",", 470 sizeof(ir->ir_line)) 471 >= sizeof(ir->ir_line)) { 472 aldap_free_attr(ldap_attrs); 473 return (-1); 474 } 475 } 476 aldap_free_attr(ldap_attrs); 477 } else { 478 if (aldap_match_attr(m, idm->idm_attrs[i], &ldap_attrs) == -1) 479 return (-1); 480 if (ldap_attrs[0] == NULL) 481 return (-1); 482 if (strlcat(ir->ir_line, ldap_attrs[0], 483 sizeof(ir->ir_line)) >= sizeof(ir->ir_line)) { 484 aldap_free_attr(ldap_attrs); 485 return (-1); 486 } 487 if (i == ATTR_UID) { 488 ir->ir_key.ik_uid = strtonum( 489 ldap_attrs[0], 0, UID_MAX, NULL); 490 } else if (i == ATTR_GR_GID) { 491 ir->ir_key.ik_uid = strtonum( 492 ldap_attrs[0], 0, GID_MAX, NULL); 493 } 494 aldap_free_attr(ldap_attrs); 495 } 496 497 if (i + 1 != max_attr) 498 if (strlcat(ir->ir_line, ":", 499 sizeof(ir->ir_line)) >= sizeof(ir->ir_line)) 500 return (-1); 501 } 502 503 return (0); 504 } 505 506 int 507 client_search_idm(struct env *env, struct idm *idm, struct aldap *al, 508 char **attrs, char *filter, int min_attr, int max_attr, 509 enum imsg_type type) 510 { 511 struct idm_req ir; 512 struct aldap_message *m; 513 struct aldap_page_control *pg = NULL; 514 const char *errstr; 515 char *dn; 516 517 dn = idm->idm_basedn; 518 if (type == IMSG_GRP_ENTRY && idm->idm_groupdn[0] != '\0') 519 dn = idm->idm_groupdn; 520 521 do { 522 if (aldap_search(al, dn, LDAP_SCOPE_SUBTREE, 523 filter, attrs, 0, 0, 0, pg) == -1) { 524 aldap_get_errno(al, &errstr); 525 log_debug("%s", errstr); 526 return (-1); 527 } 528 529 if (pg != NULL) { 530 aldap_freepage(pg); 531 pg = NULL; 532 } 533 534 while ((m = aldap_parse(al)) != NULL) { 535 if (al->msgid != m->msgid) { 536 goto fail; 537 } 538 539 if (m->message_type == LDAP_RES_SEARCH_RESULT) { 540 if (m->page != NULL && m->page->cookie_len != 0) 541 pg = m->page; 542 else 543 pg = NULL; 544 545 aldap_freemsg(m); 546 break; 547 } 548 549 if (m->message_type != LDAP_RES_SEARCH_ENTRY) { 550 goto fail; 551 } 552 553 if (client_build_req(idm, &ir, m, min_attr, max_attr) == 0) 554 imsg_compose_event(env->sc_iev, type, 0, 0, -1, 555 &ir, sizeof(ir)); 556 557 aldap_freemsg(m); 558 } 559 } while (pg != NULL); 560 561 return (0); 562 563 fail: 564 aldap_freemsg(m); 565 if (pg != NULL) { 566 aldap_freepage(pg); 567 } 568 569 return (-1); 570 } 571 572 int 573 client_try_idm(struct env *env, struct idm *idm) 574 { 575 const char *where; 576 char *attrs[ATTR_MAX+1]; 577 int i, j; 578 struct aldap_message *m; 579 struct aldap *al; 580 581 where = "connect"; 582 if ((al = client_aldap_open(&idm->idm_addr)) == NULL) 583 return (-1); 584 585 if (idm->idm_flags & F_NEEDAUTH) { 586 where = "binding"; 587 if (aldap_bind(al, idm->idm_binddn, idm->idm_bindcred) == -1) 588 goto bad; 589 590 where = "parsing"; 591 if ((m = aldap_parse(al)) == NULL) 592 goto bad; 593 where = "verifying msgid"; 594 if (al->msgid != m->msgid) { 595 aldap_freemsg(m); 596 goto bad; 597 } 598 aldap_freemsg(m); 599 } 600 601 bzero(attrs, sizeof(attrs)); 602 for (i = 0, j = 0; i < ATTR_MAX; i++) { 603 if (idm->idm_flags & F_FIXED_ATTR(i)) 604 continue; 605 attrs[j++] = idm->idm_attrs[i]; 606 } 607 attrs[j] = NULL; 608 609 /* 610 * build password line. 611 */ 612 where = "search"; 613 log_debug("searching password entries"); 614 if (client_search_idm(env, idm, al, attrs, 615 idm->idm_filters[FILTER_USER], 0, ATTR_MAX, IMSG_PW_ENTRY) == -1) 616 goto bad; 617 618 bzero(attrs, sizeof(attrs)); 619 for (i = ATTR_GR_MIN, j = 0; i < ATTR_GR_MAX; i++) { 620 if (idm->idm_flags & F_FIXED_ATTR(i)) 621 continue; 622 attrs[j++] = idm->idm_attrs[i]; 623 } 624 attrs[j] = NULL; 625 626 /* 627 * build group line. 628 */ 629 where = "search"; 630 log_debug("searching group entries"); 631 if (client_search_idm(env, idm, al, attrs, 632 idm->idm_filters[FILTER_GROUP], ATTR_GR_MIN, ATTR_GR_MAX, 633 IMSG_GRP_ENTRY) == -1) 634 goto bad; 635 636 aldap_close(al); 637 638 idm->idm_state = STATE_LDAP_DONE; 639 640 return (0); 641 bad: 642 aldap_close(al); 643 log_debug("directory %s errored out in %s", idm->idm_name, where); 644 return (-1); 645 } 646 647 void 648 client_periodic_update(int fd, short event, void *p) 649 { 650 struct env *env = p; 651 652 struct idm *idm; 653 int fail_cnt = 0; 654 655 /* If LDAP isn't finished, notify the master process to trash the 656 * update. */ 657 TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) { 658 if (idm->idm_state < STATE_LDAP_DONE) 659 fail_cnt++; 660 661 idm->idm_state = STATE_NONE; 662 663 client_addr_free(idm); 664 } 665 if (fail_cnt > 0) { 666 log_debug("trash the update"); 667 imsg_compose_event(env->sc_iev, IMSG_TRASH_UPDATE, 0, 0, -1, 668 NULL, 0); 669 } 670 671 client_configure(env); 672 } 673 674 void 675 client_configure(struct env *env) 676 { 677 struct timeval tv; 678 struct idm *idm; 679 u_int16_t dlen; 680 681 log_debug("connecting to directories"); 682 683 imsg_compose_event(env->sc_iev, IMSG_START_UPDATE, 0, 0, -1, NULL, 0); 684 685 /* Start the DNS lookups */ 686 TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) { 687 dlen = strlen(idm->idm_name) + 1; 688 imsg_compose_event(env->sc_iev_dns, IMSG_HOST_DNS, idm->idm_id, 689 0, -1, idm->idm_name, dlen); 690 } 691 692 tv.tv_sec = env->sc_conf_tv.tv_sec; 693 tv.tv_usec = env->sc_conf_tv.tv_usec; 694 evtimer_set(&env->sc_conf_ev, client_periodic_update, env); 695 evtimer_add(&env->sc_conf_ev, &tv); 696 } 697