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