1 /* $OpenBSD: ldapclient.c,v 1.34 2015/11/17 02:16:52 deraadt 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 *); 60 61 /* 62 * dummy wrapper to provide aldap_init with its fd's. 63 */ 64 struct aldap * 65 client_aldap_open(struct ypldap_addr *addr) 66 { 67 int fd = -1; 68 struct ypldap_addr *p; 69 70 for (p = addr; p != NULL; p = p->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 for (h = idm->idm_addr; h != NULL; h = h->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, *p; 128 129 if (idm->idm_addr == NULL) 130 return (-1); 131 132 for (h = idm->idm_addr; h != NULL; h = p) { 133 p = h->next; 134 free(h); 135 } 136 137 idm->idm_addr = NULL; 138 139 return (0); 140 } 141 142 void 143 client_sig_handler(int sig, short event, void *p) 144 { 145 switch (sig) { 146 case SIGINT: 147 case SIGTERM: 148 client_shutdown(); 149 break; 150 default: 151 fatalx("unexpected signal"); 152 } 153 } 154 155 void 156 client_dispatch_dns(int fd, short events, void *p) 157 { 158 struct imsg imsg; 159 u_int16_t dlen; 160 u_char *data; 161 struct ypldap_addr *h; 162 int n, wait_cnt = 0; 163 struct idm *idm; 164 int shut = 0; 165 166 struct env *env = p; 167 struct imsgev *iev = env->sc_iev_dns; 168 struct imsgbuf *ibuf = &iev->ibuf; 169 170 if ((events & (EV_READ | EV_WRITE)) == 0) 171 fatalx("unknown event"); 172 173 if (events & EV_READ) { 174 if ((n = imsg_read(ibuf)) == -1) 175 fatal("imsg_read error"); 176 if (n == 0) 177 shut = 1; 178 } 179 if (events & EV_WRITE) { 180 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 181 fatal("msgbuf_write"); 182 if (n == 0) 183 shut = 1; 184 goto done; 185 } 186 187 for (;;) { 188 if ((n = imsg_get(ibuf, &imsg)) == -1) 189 fatal("client_dispatch_dns: imsg_get error"); 190 if (n == 0) 191 break; 192 193 switch (imsg.hdr.type) { 194 case IMSG_HOST_DNS: 195 TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) 196 if (idm->idm_id == imsg.hdr.peerid) 197 break; 198 if (idm == NULL) { 199 log_warnx("IMSG_HOST_DNS with invalid peerID"); 200 break; 201 } 202 if (idm->idm_addr != NULL) { 203 log_warnx("IMSG_HOST_DNS but addr != NULL!"); 204 break; 205 } 206 207 dlen = imsg.hdr.len - IMSG_HEADER_SIZE; 208 if (dlen == 0) { /* no data -> temp error */ 209 idm->idm_state = STATE_DNS_TEMPFAIL; 210 break; 211 } 212 213 data = (u_char *)imsg.data; 214 while (dlen >= sizeof(struct sockaddr_storage)) { 215 if ((h = calloc(1, sizeof(struct ypldap_addr))) == 216 NULL) 217 fatal(NULL); 218 memcpy(&h->ss, data, sizeof(h->ss)); 219 220 if (idm->idm_addr == NULL) 221 h->next = NULL; 222 else 223 h->next = idm->idm_addr; 224 225 idm->idm_addr = h; 226 227 data += sizeof(h->ss); 228 dlen -= sizeof(h->ss); 229 } 230 if (dlen != 0) 231 fatalx("IMSG_HOST_DNS: dlen != 0"); 232 233 client_addr_init(idm); 234 235 break; 236 default: 237 break; 238 } 239 imsg_free(&imsg); 240 } 241 242 TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) { 243 if (client_try_idm(env, idm) == -1) 244 idm->idm_state = STATE_LDAP_FAIL; 245 246 if (idm->idm_state < STATE_LDAP_DONE) 247 wait_cnt++; 248 } 249 if (wait_cnt == 0) 250 imsg_compose_event(env->sc_iev, IMSG_END_UPDATE, 0, 0, -1, 251 NULL, 0); 252 253 done: 254 if (!shut) 255 imsg_event_add(iev); 256 else { 257 /* this pipe is dead, so remove the event handler */ 258 event_del(&iev->ev); 259 event_loopexit(NULL); 260 } 261 } 262 263 void 264 client_dispatch_parent(int fd, short events, void *p) 265 { 266 int n; 267 int shut = 0; 268 struct imsg imsg; 269 struct env *env = p; 270 struct imsgev *iev = env->sc_iev; 271 struct imsgbuf *ibuf = &iev->ibuf; 272 273 if ((events & (EV_READ | EV_WRITE)) == 0) 274 fatalx("unknown event"); 275 276 if (events & EV_READ) { 277 if ((n = imsg_read(ibuf)) == -1) 278 fatal("imsg_read error"); 279 if (n == 0) 280 shut = 1; 281 } 282 if (events & EV_WRITE) { 283 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN) 284 fatal("msgbuf_write"); 285 if (n == 0) 286 shut = 1; 287 goto done; 288 } 289 290 for (;;) { 291 if ((n = imsg_get(ibuf, &imsg)) == -1) 292 fatal("client_dispatch_parent: imsg_get error"); 293 if (n == 0) 294 break; 295 296 switch (imsg.hdr.type) { 297 case IMSG_CONF_START: { 298 struct env params; 299 300 if (env->sc_flags & F_CONFIGURING) { 301 log_warnx("configuration already in progress"); 302 break; 303 } 304 memcpy(¶ms, imsg.data, sizeof(params)); 305 log_debug("configuration starting"); 306 env->sc_flags |= F_CONFIGURING; 307 purge_config(env); 308 memcpy(&env->sc_conf_tv, ¶ms.sc_conf_tv, 309 sizeof(env->sc_conf_tv)); 310 env->sc_flags |= params.sc_flags; 311 break; 312 } 313 case IMSG_CONF_IDM: { 314 struct idm *idm; 315 316 if (!(env->sc_flags & F_CONFIGURING)) 317 break; 318 if ((idm = calloc(1, sizeof(*idm))) == NULL) 319 fatal(NULL); 320 memcpy(idm, imsg.data, sizeof(*idm)); 321 idm->idm_env = env; 322 TAILQ_INSERT_TAIL(&env->sc_idms, idm, idm_entry); 323 break; 324 } 325 case IMSG_CONF_END: 326 env->sc_flags &= ~F_CONFIGURING; 327 log_debug("applying configuration"); 328 client_configure(env); 329 break; 330 default: 331 log_debug("client_dispatch_parent: unexpect imsg %d", 332 imsg.hdr.type); 333 334 break; 335 } 336 imsg_free(&imsg); 337 } 338 339 done: 340 if (!shut) 341 imsg_event_add(iev); 342 else { 343 /* this pipe is dead, so remove the event handler */ 344 event_del(&iev->ev); 345 event_loopexit(NULL); 346 } 347 } 348 349 void 350 client_shutdown(void) 351 { 352 log_info("ldap client exiting"); 353 _exit(0); 354 } 355 356 pid_t 357 ldapclient(int pipe_main2client[2]) 358 { 359 pid_t pid, dns_pid; 360 int pipe_dns[2]; 361 struct passwd *pw; 362 struct event ev_sigint; 363 struct event ev_sigterm; 364 struct env env; 365 366 switch (pid = fork()) { 367 case -1: 368 fatal("cannot fork"); 369 break; 370 case 0: 371 break; 372 default: 373 return (pid); 374 } 375 376 bzero(&env, sizeof(env)); 377 TAILQ_INIT(&env.sc_idms); 378 379 if ((pw = getpwnam(YPLDAP_USER)) == NULL) 380 fatal("getpwnam"); 381 382 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_dns) == -1) 383 fatal("socketpair"); 384 dns_pid = ypldap_dns(pipe_dns, pw); 385 close(pipe_dns[1]); 386 387 #ifndef DEBUG 388 if (chroot(pw->pw_dir) == -1) 389 fatal("chroot"); 390 if (chdir("/") == -1) 391 fatal("chdir"); 392 #else 393 #warning disabling chrooting in DEBUG mode 394 #endif 395 setproctitle("ldap client"); 396 ypldap_process = PROC_CLIENT; 397 398 #ifndef DEBUG 399 if (setgroups(1, &pw->pw_gid) || 400 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) || 401 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) 402 fatal("cannot drop privileges"); 403 #else 404 #warning disabling privilege revocation in DEBUG mode 405 #endif 406 407 if (pledge("stdio inet", NULL) == -1) 408 fatal("pledge"); 409 410 event_init(); 411 signal(SIGPIPE, SIG_IGN); 412 signal_set(&ev_sigint, SIGINT, client_sig_handler, NULL); 413 signal_set(&ev_sigterm, SIGTERM, client_sig_handler, NULL); 414 signal_add(&ev_sigint, NULL); 415 signal_add(&ev_sigterm, NULL); 416 417 close(pipe_main2client[0]); 418 if ((env.sc_iev = calloc(1, sizeof(*env.sc_iev))) == NULL) 419 fatal(NULL); 420 if ((env.sc_iev_dns = calloc(1, sizeof(*env.sc_iev_dns))) == NULL) 421 fatal(NULL); 422 423 env.sc_iev->events = EV_READ; 424 env.sc_iev->data = &env; 425 imsg_init(&env.sc_iev->ibuf, pipe_main2client[1]); 426 env.sc_iev->handler = client_dispatch_parent; 427 event_set(&env.sc_iev->ev, env.sc_iev->ibuf.fd, env.sc_iev->events, 428 env.sc_iev->handler, &env); 429 event_add(&env.sc_iev->ev, NULL); 430 431 env.sc_iev_dns->events = EV_READ; 432 env.sc_iev_dns->data = &env; 433 imsg_init(&env.sc_iev_dns->ibuf, pipe_dns[0]); 434 env.sc_iev_dns->handler = client_dispatch_dns; 435 event_set(&env.sc_iev_dns->ev, env.sc_iev_dns->ibuf.fd, 436 env.sc_iev_dns->events, env.sc_iev_dns->handler, &env); 437 event_add(&env.sc_iev_dns->ev, NULL); 438 439 event_dispatch(); 440 client_shutdown(); 441 442 return (0); 443 444 } 445 446 int 447 client_build_req(struct idm *idm, struct idm_req *ir, struct aldap_message *m, 448 int min_attr, int max_attr) 449 { 450 char **ldap_attrs; 451 int i, k; 452 453 bzero(ir, sizeof(*ir)); 454 for (i = min_attr; i < max_attr; i++) { 455 if (idm->idm_flags & F_FIXED_ATTR(i)) { 456 if (strlcat(ir->ir_line, idm->idm_attrs[i], 457 sizeof(ir->ir_line)) >= sizeof(ir->ir_line)) 458 /* 459 * entry yields a line > 1024, trash it. 460 */ 461 return (-1); 462 463 if (i == ATTR_UID) { 464 ir->ir_key.ik_uid = strtonum( 465 idm->idm_attrs[i], 0, 466 UID_MAX, NULL); 467 } else if (i == ATTR_GR_GID) { 468 ir->ir_key.ik_gid = strtonum( 469 idm->idm_attrs[i], 0, 470 GID_MAX, NULL); 471 } 472 } else if (idm->idm_list & F_LIST(i)) { 473 aldap_match_attr(m, idm->idm_attrs[i], &ldap_attrs); 474 for (k = 0; k >= 0 && ldap_attrs && ldap_attrs[k] != NULL; k++) { 475 /* XXX: Fail when attributes have illegal characters e.g. ',' */ 476 if (strlcat(ir->ir_line, ldap_attrs[k], 477 sizeof(ir->ir_line)) >= sizeof(ir->ir_line)) 478 continue; 479 if (ldap_attrs[k+1] != NULL) 480 if (strlcat(ir->ir_line, ",", 481 sizeof(ir->ir_line)) 482 >= sizeof(ir->ir_line)) { 483 aldap_free_attr(ldap_attrs); 484 return (-1); 485 } 486 } 487 aldap_free_attr(ldap_attrs); 488 } else { 489 if (aldap_match_attr(m, idm->idm_attrs[i], &ldap_attrs) == -1) 490 return (-1); 491 if (ldap_attrs[0] == NULL) 492 return (-1); 493 if (strlcat(ir->ir_line, ldap_attrs[0], 494 sizeof(ir->ir_line)) >= sizeof(ir->ir_line)) { 495 aldap_free_attr(ldap_attrs); 496 return (-1); 497 } 498 if (i == ATTR_UID) { 499 ir->ir_key.ik_uid = strtonum( 500 ldap_attrs[0], 0, UID_MAX, NULL); 501 } else if (i == ATTR_GR_GID) { 502 ir->ir_key.ik_uid = strtonum( 503 ldap_attrs[0], 0, GID_MAX, NULL); 504 } 505 aldap_free_attr(ldap_attrs); 506 } 507 508 if (i + 1 != max_attr) 509 if (strlcat(ir->ir_line, ":", 510 sizeof(ir->ir_line)) >= sizeof(ir->ir_line)) 511 return (-1); 512 } 513 514 return (0); 515 } 516 517 int 518 client_search_idm(struct env *env, struct idm *idm, struct aldap *al, 519 char **attrs, char *filter, int min_attr, int max_attr, 520 enum imsg_type type) 521 { 522 struct idm_req ir; 523 struct aldap_message *m; 524 struct aldap_page_control *pg = NULL; 525 const char *errstr; 526 char *dn; 527 528 dn = idm->idm_basedn; 529 if (type == IMSG_GRP_ENTRY && idm->idm_groupdn[0] != '\0') 530 dn = idm->idm_groupdn; 531 532 do { 533 if (aldap_search(al, dn, LDAP_SCOPE_SUBTREE, 534 filter, attrs, 0, 0, 0, pg) == -1) { 535 aldap_get_errno(al, &errstr); 536 log_debug("%s", errstr); 537 return (-1); 538 } 539 540 if (pg != NULL) { 541 aldap_freepage(pg); 542 pg = NULL; 543 } 544 545 while ((m = aldap_parse(al)) != NULL) { 546 if (al->msgid != m->msgid) { 547 goto fail; 548 } 549 550 if (m->message_type == LDAP_RES_SEARCH_RESULT) { 551 if (m->page != NULL && m->page->cookie_len != 0) 552 pg = m->page; 553 else 554 pg = NULL; 555 556 aldap_freemsg(m); 557 break; 558 } 559 560 if (m->message_type != LDAP_RES_SEARCH_ENTRY) { 561 goto fail; 562 } 563 564 if (client_build_req(idm, &ir, m, min_attr, max_attr) == 0) 565 imsg_compose_event(env->sc_iev, type, 0, 0, -1, 566 &ir, sizeof(ir)); 567 568 aldap_freemsg(m); 569 } 570 } while (pg != NULL); 571 572 return (0); 573 574 fail: 575 aldap_freemsg(m); 576 if (pg != NULL) { 577 aldap_freepage(pg); 578 } 579 580 return (-1); 581 } 582 583 int 584 client_try_idm(struct env *env, struct idm *idm) 585 { 586 const char *where; 587 char *attrs[ATTR_MAX+1]; 588 int i, j; 589 struct aldap_message *m; 590 struct aldap *al; 591 592 where = "connect"; 593 if ((al = client_aldap_open(idm->idm_addr)) == NULL) 594 return (-1); 595 596 if (idm->idm_flags & F_NEEDAUTH) { 597 where = "binding"; 598 if (aldap_bind(al, idm->idm_binddn, idm->idm_bindcred) == -1) 599 goto bad; 600 601 where = "parsing"; 602 if ((m = aldap_parse(al)) == NULL) 603 goto bad; 604 where = "verifying msgid"; 605 if (al->msgid != m->msgid) { 606 aldap_freemsg(m); 607 goto bad; 608 } 609 aldap_freemsg(m); 610 } 611 612 bzero(attrs, sizeof(attrs)); 613 for (i = 0, j = 0; i < ATTR_MAX; i++) { 614 if (idm->idm_flags & F_FIXED_ATTR(i)) 615 continue; 616 attrs[j++] = idm->idm_attrs[i]; 617 } 618 attrs[j] = NULL; 619 620 /* 621 * build password line. 622 */ 623 where = "search"; 624 log_debug("searching password entries"); 625 if (client_search_idm(env, idm, al, attrs, 626 idm->idm_filters[FILTER_USER], 0, ATTR_MAX, IMSG_PW_ENTRY) == -1) 627 goto bad; 628 629 bzero(attrs, sizeof(attrs)); 630 for (i = ATTR_GR_MIN, j = 0; i < ATTR_GR_MAX; i++) { 631 if (idm->idm_flags & F_FIXED_ATTR(i)) 632 continue; 633 attrs[j++] = idm->idm_attrs[i]; 634 } 635 attrs[j] = NULL; 636 637 /* 638 * build group line. 639 */ 640 where = "search"; 641 log_debug("searching group entries"); 642 if (client_search_idm(env, idm, al, attrs, 643 idm->idm_filters[FILTER_GROUP], ATTR_GR_MIN, ATTR_GR_MAX, 644 IMSG_GRP_ENTRY) == -1) 645 goto bad; 646 647 aldap_close(al); 648 649 idm->idm_state = STATE_LDAP_DONE; 650 651 return (0); 652 bad: 653 aldap_close(al); 654 log_debug("directory %s errored out in %s", idm->idm_name, where); 655 return (-1); 656 } 657 658 void 659 client_periodic_update(int fd, short event, void *p) 660 { 661 struct env *env = p; 662 663 struct idm *idm; 664 int fail_cnt = 0; 665 666 /* If LDAP isn't finished, notify the master process to trash the 667 * update. */ 668 TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) { 669 if (idm->idm_state < STATE_LDAP_DONE) 670 fail_cnt++; 671 672 idm->idm_state = STATE_NONE; 673 674 client_addr_free(idm); 675 } 676 if (fail_cnt > 0) { 677 log_debug("trash the update"); 678 imsg_compose_event(env->sc_iev, IMSG_TRASH_UPDATE, 0, 0, -1, 679 NULL, 0); 680 } 681 682 client_configure(env); 683 } 684 685 void 686 client_configure(struct env *env) 687 { 688 struct timeval tv; 689 struct idm *idm; 690 u_int16_t dlen; 691 692 log_debug("connecting to directories"); 693 694 imsg_compose_event(env->sc_iev, IMSG_START_UPDATE, 0, 0, -1, NULL, 0); 695 696 /* Start the DNS lookups */ 697 TAILQ_FOREACH(idm, &env->sc_idms, idm_entry) { 698 dlen = strlen(idm->idm_name) + 1; 699 imsg_compose_event(env->sc_iev_dns, IMSG_HOST_DNS, idm->idm_id, 700 0, -1, idm->idm_name, dlen); 701 } 702 703 tv.tv_sec = env->sc_conf_tv.tv_sec; 704 tv.tv_usec = env->sc_conf_tv.tv_usec; 705 evtimer_set(&env->sc_conf_ev, client_periodic_update, env); 706 evtimer_add(&env->sc_conf_ev, &tv); 707 } 708