1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
|
/* Copyright (C) 2002-2005 Luke Howard.
This file is part of the nss_ldap library.
Contributed by Luke Howard, <lukeh@padl.com>, 2002.
The nss_ldap library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The nss_ldap library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the nss_ldap library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
/*
* Shim to support AIX loadable authentication modules
*/
#include "config.h"
static char rcsId[] =
"$Id: aix_authmeth.c,v 2.33 2009/11/06 10:15:26 lukeh Exp $";
#ifdef HAVE_USERSEC_H
#include <stdlib.h>
#include <string.h>
#include <usersec.h>
#ifdef HAVE_LBER_H
#include <lber.h>
#endif
#ifdef HAVE_LDAP_H
#include <ldap.h>
#endif
#include "ldap-nss.h"
#include "util.h"
#define TABLE_KEY_ALL "ALL"
#define TABLE_USER "user"
#define TABLE_GROUP "group"
#define S_LDAPDN "ldapdn"
static struct irs_gr *uess_gr_be = NULL;
static struct irs_pw *uess_pw_be = NULL;
extern void *gr_pvtinit (void); /* irs-grp.c */
extern void *pw_pvtinit (void); /* irs-pwd.c */
/* from ldap-grp.c */
extern char *_nss_ldap_getgrset (char *user);
/* search arguments for getentry method */
typedef struct ldap_uess_args
{
/* argument block */
const char *lua_key;
const char *lua_table;
char **lua_attributes;
attrval_t *lua_results;
int lua_size;
/* private */
ldap_map_selector_t lua_map;
size_t lua__bufsiz;
size_t lua__buflen;
char *lua__buffer;
const char *lua_naming_attribute;
}
ldap_uess_args_t;
static NSS_STATUS uess_get_char (LDAPMessage * e, ldap_uess_args_t * arg, int index);
static NSS_STATUS uess_get_char_ex (LDAPMessage * e, ldap_uess_args_t * arg, int index, const char *attribute);
static NSS_STATUS uess_get_int (LDAPMessage * e, ldap_uess_args_t * arg, int index);
static NSS_STATUS uess_get_pgrp (LDAPMessage * e, ldap_uess_args_t * arg, int index);
static NSS_STATUS uess_get_groupsids (LDAPMessage * e, ldap_uess_args_t * arg, int index);
static NSS_STATUS uess_get_gecos (LDAPMessage * e, ldap_uess_args_t * arg, int index);
static NSS_STATUS uess_get_pwd (LDAPMessage * e, ldap_uess_args_t * arg, int index);
static NSS_STATUS uess_get_dn (LDAPMessage * e, ldap_uess_args_t * arg, int index);
/* dispatch table for retrieving UESS attribute from an LDAP entry */
struct ldap_uess_fn
{
const char *luf_attribute;
NSS_STATUS (*luf_translator) (LDAPMessage * e,
ldap_uess_args_t *, int);
}
ldap_uess_fn_t;
static struct ldap_uess_fn __uess_fns[] = {
{S_GECOS, uess_get_gecos},
{S_GROUPSIDS, uess_get_groupsids},
{S_HOME, uess_get_char},
{S_ID, uess_get_int},
{S_PWD, uess_get_pwd},
{S_SHELL, uess_get_char},
{S_PGRP, uess_get_pgrp},
{SEC_PASSWD, uess_get_char},
{SEC_LASTUP, uess_get_int},
{S_MAXAGE, uess_get_int},
{S_MINAGE, uess_get_int},
{S_MAXEXPIRED, uess_get_int},
{S_PWDWARNTIME, uess_get_int},
/* add additional attributes we know about here */
{S_LDAPDN, uess_get_dn},
{NULL, NULL}
};
#define GR_PVTINIT() do { \
if (uess_gr_be == NULL) { \
uess_gr_be = (struct irs_gr *) gr_pvtinit (); \
if (uess_gr_be == NULL) \
return NULL; \
} \
} while (0)
#define PW_PVTINIT() do { \
if (uess_pw_be == NULL) { \
uess_pw_be = (struct irs_pw *) pw_pvtinit (); \
if (uess_pw_be == NULL) \
return NULL; \
} \
} while (0)
static void *
_nss_ldap_uess_open (const char *name, const char *domain,
const int mode, char *options)
{
/* Currently we do not use the above parameters */
GR_PVTINIT();
PW_PVTINIT();
return NULL;
}
static void
_nss_ldap_uess_close (void *token)
{
if (uess_gr_be != NULL)
{
(uess_gr_be->close) (uess_gr_be);
uess_gr_be = NULL;
}
if (uess_pw_be != NULL)
{
(uess_pw_be->close) (uess_pw_be);
uess_pw_be = NULL;
}
}
static struct group *
_nss_ldap_getgrgid (gid_t gid)
{
GR_PVTINIT ();
return (uess_gr_be->bygid) (uess_gr_be, gid);
}
static struct group *
_nss_ldap_getgrnam (const char *name)
{
GR_PVTINIT ();
return (uess_gr_be->byname) (uess_gr_be, name);
}
static struct passwd *
_nss_ldap_getpwuid (uid_t uid)
{
PW_PVTINIT ();
return (uess_pw_be->byuid) (uess_pw_be, uid);
}
static struct passwd *
_nss_ldap_getpwnam (const char *name)
{
PW_PVTINIT ();
return (uess_pw_be->byname) (uess_pw_be, name);
}
static struct group *
_nss_ldap_getgracct (void *id, int type)
{
GR_PVTINIT ();
if (type == SEC_INT)
return (uess_gr_be->bygid) (uess_gr_be, *(gid_t *) id);
else
return (uess_gr_be->byname) (uess_gr_be, (char *) id);
}
static int
_nss_ldap_authenticate (char *user, char *response, int *reenter,
char **message)
{
NSS_STATUS stat;
int rc;
debug ("==> _nss_ldap_authenticate");
*reenter = FALSE;
*message = NULL;
stat = _nss_ldap_proxy_bind (user, response);
switch (stat)
{
case NSS_TRYAGAIN:
rc = AUTH_FAILURE;
break;
case NSS_NOTFOUND:
rc = AUTH_NOTFOUND;
break;
case NSS_SUCCESS:
rc = AUTH_SUCCESS;
break;
default:
case NSS_UNAVAIL:
rc = AUTH_UNAVAIL;
break;
}
debug ("<== _nss_ldap_authenticate");
return rc;
}
/*
* Support this for when proxy authentication is disabled.
* There may be some re-entrancy issues here; not sure
* if we are supposed to return allocated memory or not,
* this is not documented. I am assuming not in line with
* the other APIs.
*/
static char *
_nss_ldap_getpasswd (char *user)
{
struct passwd *pw;
static char pwdbuf[32];
char *p = NULL;
debug ("==> _nss_ldap_getpasswd");
pw = _nss_ldap_getpwnam (user);
if (pw != NULL)
{
if (strlen (pw->pw_passwd) > sizeof (pwdbuf) - 1)
{
errno = ERANGE;
}
else
{
strcpy (pwdbuf, pw->pw_passwd);
p = pwdbuf;
}
}
else
{
errno = ENOENT; /* user does not exist */
}
debug ("<== _nss_ldap_getpasswd");
return p;
}
/*
* Convert a UESS table string to an nss_ldap map type
*/
static ldap_map_selector_t
table2map (const char *table)
{
if (strcmp (table, TABLE_USER) == 0)
return LM_PASSWD;
else if (strcmp (table, TABLE_GROUP) == 0)
return LM_GROUP;
return LM_NONE;
}
/*
* Convert a UESS key to an nss_ldap internal search query
*/
static ldap_args_t *
key2filter (char *key, ldap_map_selector_t map,
ldap_args_t * a, const char **filter)
{
if (strcmp (key, TABLE_KEY_ALL) == 0)
{
if (map == LM_PASSWD)
*filter = _nss_ldap_filt_getpwent;
else
*filter = _nss_ldap_filt_getgrent;
return NULL; /* indicates enumeration */
}
LA_INIT (*a);
LA_TYPE (*a) = LA_TYPE_STRING;
LA_STRING (*a) = key;
if (map == LM_PASSWD)
*filter = _nss_ldap_filt_getpwnam;
else
*filter = _nss_ldap_filt_getgrnam;
return a;
}
/*
* Map a UESS attribute to an LDAP attribute
*/
static const char *
uess2ldapattr (ldap_map_selector_t map, const char *attribute)
{
if (strcmp (attribute, "username") == 0)
return ATM (LM_PASSWD, uid);
else if (strcmp (attribute, "groupname") == 0)
return ATM (LM_GROUP, cn);
else if (strcmp (attribute, S_ID) == 0)
{
if (map == LM_PASSWD)
return ATM (LM_PASSWD, uidNumber);
else
return ATM (LM_GROUP, gidNumber);
}
else if (strcmp (attribute, S_PWD) == 0)
return ATM (LM_PASSWD, userPassword);
else if (strcmp (attribute, S_HOME) == 0)
return ATM (LM_PASSWD, homeDirectory);
else if (strcmp (attribute, S_SHELL) == 0)
return ATM (LM_PASSWD, loginShell);
else if (strcmp (attribute, S_GECOS) == 0)
return ATM (LM_PASSWD, gecos);
else if (strcmp (attribute, SEC_PASSWD) == 0)
return ATM (LM_SHADOW, userPassword);
else if (strcmp (attribute, SEC_LASTUP) == 0)
return ATM (LM_SHADOW, shadowLastChange);
else if (strcmp (attribute, S_MAXAGE) == 0)
return ATM (LM_SHADOW, shadowMax);
else if (strcmp (attribute, S_MINAGE) == 0)
return ATM (LM_SHADOW, shadowMin);
else if (strcmp (attribute, S_MAXEXPIRED) == 0)
return ATM (LM_SHADOW, shadowExpire);
else if (strcmp (attribute, S_PWDWARNTIME) == 0)
return ATM (LM_SHADOW, shadowWarning);
else if (strcmp (attribute, S_PGRP) == 0)
return ATM (LM_GROUP, cn);
else if (strcmp (attribute, S_USERS) == 0)
return ATM (LM_GROUP, memberUid);
return NULL;
}
/*
* Get primary group name for a user
*/
static NSS_STATUS
uess_get_pgrp (LDAPMessage * e, ldap_uess_args_t * lua, int i)
{
char **vals;
LDAPMessage *res;
const char *attrs[2];
NSS_STATUS stat;
ldap_args_t a;
vals = _nss_ldap_get_values (e, ATM (LM_PASSWD, gidNumber));
if (vals == NULL)
return NSS_NOTFOUND;
LA_INIT (a);
LA_TYPE (a) = LA_TYPE_NUMBER;
stat = _nss_ldap_parse_long (vals[0], 0, &(LA_NUMBER(a)));
if (stat != NSS_SUCCESS)
{
ldap_value_free (vals);
return stat;
}
attrs[0] = ATM (LM_GROUP, cn);
attrs[1] = NULL;
stat = _nss_ldap_search_s (&a, _nss_ldap_filt_getgrgid, LM_GROUP,
attrs, 1, &res);
if (stat != NSS_SUCCESS)
{
ldap_value_free (vals);
return NSS_NOTFOUND;
}
ldap_value_free (vals);
e = _nss_ldap_first_entry (res);
if (e == NULL)
{
ldap_msgfree (res);
return NSS_NOTFOUND;
}
stat = uess_get_char_ex (e, lua, i, attrs[0]);
ldap_msgfree (res);
return stat;
}
/*
* Get groups to which a user belongs
*/
static NSS_STATUS
uess_get_groupsids (LDAPMessage * e, ldap_uess_args_t * lua, int i)
{
char *p, *q;
size_t len;
p = _nss_ldap_getgrset ((char *) lua->lua_key);
if (p == NULL)
return NSS_NOTFOUND;
len = strlen (p);
q = malloc (len + 2);
if (q == NULL)
{
errno = ENOMEM;
return NSS_NOTFOUND;
}
memcpy (q, p, len + 1);
q[len + 1] = '\0';
free (p);
p = NULL;
for (p = q; *p != '\0'; p++)
{
if (*p == ',')
*p++ = '\0';
}
lua->lua_results[i].attr_un.au_char = q;
return NSS_SUCCESS;
}
/*
* Get a mapped UESS string attribute
*/
static NSS_STATUS
uess_get_char (LDAPMessage * e, ldap_uess_args_t * lua, int i)
{
const char *attribute;
attribute = uess2ldapattr (lua->lua_map, lua->lua_attributes[i]);
if (attribute == NULL)
return NSS_NOTFOUND;
return uess_get_char_ex (e, lua, i, attribute);
}
/*
* Get a specific LDAP attribute
*/
static NSS_STATUS
uess_get_char_ex (LDAPMessage * e,
ldap_uess_args_t * lua, int i, const char *attribute)
{
char **vals;
attrval_t *av = &lua->lua_results[i];
vals = _nss_ldap_get_values (e, attribute);
if (vals == NULL)
return NSS_NOTFOUND;
if (vals[0] == NULL)
{
ldap_value_free (vals);
return NSS_NOTFOUND;
}
av->attr_un.au_char = strdup (vals[0]);
if (av->attr_un.au_char == NULL)
{
ldap_value_free (vals);
return NSS_TRYAGAIN;
}
ldap_value_free (vals);
return NSS_SUCCESS;
}
/*
* Get an encoded crypt password
*/
static NSS_STATUS
uess_get_pwd (LDAPMessage * e, ldap_uess_args_t * lua, int i)
{
char **vals;
attrval_t *av = &lua->lua_results[i];
const char *pwd;
const char *attribute;
attribute = uess2ldapattr (lua->lua_map, lua->lua_attributes[i]);
if (attribute == NULL)
return NSS_NOTFOUND;
vals = _nss_ldap_get_values (e, attribute);
pwd = _nss_ldap_locate_userpassword (vals);
av->attr_un.au_char = strdup (pwd);
if (vals != NULL)
ldap_value_free (vals);
return (av->attr_un.au_char == NULL) ? NSS_TRYAGAIN : NSS_SUCCESS;
}
/*
* Get a UESS integer attribute
*/
static NSS_STATUS
uess_get_int (LDAPMessage * e, ldap_uess_args_t * lua, int i)
{
const char *attribute;
char **vals;
attrval_t *av = &lua->lua_results[i];
attribute = uess2ldapattr (lua->lua_map, lua->lua_attributes[i]);
if (attribute == NULL)
return NSS_NOTFOUND;
vals = _nss_ldap_get_values (e, attribute);
if (vals == NULL)
return NSS_NOTFOUND;
if (vals[0] == NULL)
{
ldap_value_free (vals);
return NSS_NOTFOUND;
}
stat = _nss_ldap_parse_int(vals[0], 0, &av->attr_un.au_int);
ldap_value_free (vals);
return stat;
}
/*
* Get the GECOS/cn attribute
*/
static NSS_STATUS
uess_get_gecos (LDAPMessage * e, ldap_uess_args_t * lua, int i)
{
NSS_STATUS stat;
stat = uess_get_char (e, lua, i);
if (stat == NSS_NOTFOUND)
{
stat = uess_get_char_ex (e, lua, i, ATM (LM_PASSWD, cn));
}
return stat;
}
/*
* Get the DN
*/
static NSS_STATUS
uess_get_dn (LDAPMessage * e, ldap_uess_args_t * lua, int i)
{
lua->lua_results[i].attr_un.au_char = _nss_ldap_get_dn (e);
if (lua->lua_results[i].attr_un.au_char == NULL)
return NSS_NOTFOUND;
return NSS_SUCCESS;
}
static NSS_STATUS
do_parse_uess_getentry (LDAPMessage * e,
ldap_state_t * pvt, void *result,
char *buffer, size_t buflen)
{
ldap_uess_args_t *lua = (ldap_uess_args_t *) result;
int i;
char **vals;
size_t len;
NSS_STATUS stat;
/* If a buffer is supplied, then we are enumerating. */
if (lua->lua__buffer != NULL)
{
attrval_t *av = lua->lua_results;
vals = _nss_ldap_get_values (e, lua->lua_naming_attribute);
if (vals == NULL)
return NSS_NOTFOUND;
if (vals[0] == NULL)
{
ldap_value_free (vals);
return NSS_NOTFOUND;
}
len = strlen (vals[0]) + 1; /* for string terminator */
if (lua->lua__buflen < len + 1) /* for list terminator */
{
size_t grow = len + 1;
size_t offset = (lua->lua__buffer - av->attr_un.au_char);
grow += NSS_BUFSIZ - 1;
grow -= (grow % NSS_BUFSIZ);
av->attr_un.au_char =
realloc (lua->lua__buffer, lua->lua__bufsiz + grow);
if (av->attr_un.au_char == NULL)
{
ldap_value_free (vals);
return NSS_TRYAGAIN;
}
/* reset buffer pointer in case realloc() returned a new region */
lua->lua__buffer = &av->attr_un.au_char[offset];
lua->lua__buflen += grow;
lua->lua__bufsiz += grow;
}
memcpy (lua->lua__buffer, vals[0], len);
lua->lua__buflen -= len;
lua->lua__buffer += len;
ldap_value_free (vals);
lua->lua__buffer[0] = '\0'; /* ensure _list_ is always terminated */
if (av->attr_flag != 0)
av->attr_flag = 0;
return NSS_NOTFOUND; /* trick caller into calling us again */
}
else
{
for (i = 0; i < lua->lua_size; i++)
{
int j;
attrval_t *av = &lua->lua_results[i];
av->attr_flag = -1;
av->attr_un.au_char = NULL;
for (j = 0; __uess_fns[j].luf_attribute != NULL; j++)
{
if (strcmp (__uess_fns[j].luf_attribute, lua->lua_attributes[i])
== 0)
{
stat = (__uess_fns[j].luf_translator) (e, lua, i);
switch (stat)
{
case NSS_SUCCESS:
av->attr_flag = 0;
break;
case NSS_TRYAGAIN:
return NSS_TRYAGAIN;
break;
default:
break;
}
}
}
}
}
return NSS_SUCCESS;
}
static int
_nss_ldap_getentry (char *key, char *table, char *attributes[],
attrval_t results[], int size)
{
NSS_STATUS stat;
ent_context_t *ctx = NULL;
ldap_args_t a, *ap;
const char *filter;
int erange = 0;
ldap_uess_args_t lua;
const char *namingAttributes[2];
debug ("==> _nss_ldap_getentry (key=%s table=%s attributes[0]=%s size=%d)",
(key != NULL) ? key : "(null)",
(table != NULL) ? table : "(null)",
(size >= 1) ? attributes[0] : "(null)",
size);
lua.lua_key = key;
lua.lua_table = table;
lua.lua_attributes = attributes;
lua.lua_results = results;
lua.lua_size = size;
lua.lua_naming_attribute = NULL;
lua.lua_map = table2map (table);
if (lua.lua_map == LM_NONE)
{
errno = ENOSYS;
debug ("<== _nss_ldap_getentry (no such map)");
return -1;
}
lua.lua__buffer = NULL;
lua.lua__bufsiz = 0;
lua.lua__buflen = 0;
ap = key2filter (key, lua.lua_map, &a, &filter);
if (ap == NULL) /* enumeration */
{
const char **attrs;
if (size != 1)
{
errno = EINVAL;
debug ("<== _nss_ldap_getentry (size != 1)");
return -1;
}
debug (":== _nss_ldap_getentry filter=%s attribute=%s",
filter, lua.lua_attributes[0]);
lua.lua__bufsiz = NSS_BUFSIZ;
lua.lua__buflen = lua.lua__bufsiz;
lua.lua__buffer = results[0].attr_un.au_char = malloc (lua.lua__bufsiz);
if (lua.lua__buffer == NULL)
{
errno = ENOMEM;
debug ("<== _nss_ldap_getentry (no memory)");
return -1;
}
results[0].attr_flag = -1;
/* just request the naming attributes */
attrs = _nss_ldap_get_attributes (lua.lua_map);
if (attrs == NULL || attrs[0] == NULL)
{
errno = ENOENT;
debug ("<== _nss_ldap_getentry (could not read schema)");
return -1;
}
lua.lua_naming_attribute = attrs[0];
namingAttributes[0] = lua.lua_naming_attribute;
namingAttributes[1] = NULL;
}
else
{
/* Check at least one attribute is mapped before searching */
int i, found = 0;
for (i = 0; i < size; i++)
{
if (uess2ldapattr (lua.lua_map, lua.lua_attributes[i]) != NULL)
{
found++;
break;
}
}
if (!found)
{
errno = ENOENT;
debug ("<== _nss_ldap_getentry (no mappable attribute requested)");
return -1;
}
}
_nss_ldap_enter ();
if (_nss_ldap_ent_context_init_locked (&ctx) == NULL)
{
_nss_ldap_leave ();
if (results[0].attr_un.au_char != NULL)
free (results[0].attr_un.au_char);
errno = ENOMEM;
debug ("<== _nss_ldap_getentry (ent_context_init failed)");
return -1;
}
stat = _nss_ldap_getent_ex (ap, &ctx, (void *) &lua, NULL, 0,
&erange, filter, lua.lua_map,
(ap == NULL) ? namingAttributes : NULL,
do_parse_uess_getentry);
_nss_ldap_ent_context_release (&ctx);
_nss_ldap_leave ();
/*
* Whilst enumerating, we have the parser always return
* NSS_NOTFOUND so that it will be called for each entry.
*
* Although this is probably bogus overloading of the
* _nss_ldap_getent_ex() API, it does allow us to share
* the same code for matches and enumerations. However,
* for the enumeration case we need to treat NSS_NOTFOUND
* as a success code; hence, we use the attr_flag to
* indicate failure.
*/
if (ap == NULL)
{
if (stat == NSS_NOTFOUND && results[0].attr_flag == 0)
stat = NSS_SUCCESS;
}
if (stat != NSS_SUCCESS)
{
if (stat == NSS_TRYAGAIN)
errno = ERANGE;
else
errno = ENOENT;
debug ("<== _nss_ldap_getentry (failed with stat=%d)", stat);
return -1;
}
debug ("<== _nss_ldap_getentry (success)");
return AUTH_SUCCESS;
}
/*
*
*/
static NSS_STATUS
uess_get_pwuid(const char *user, uid_t *uid)
{
char **vals;
LDAPMessage *res, *e;
const char *attrs[2];
NSS_STATUS stat;
ldap_args_t a;
LA_INIT (a);
LA_TYPE (a) = LA_TYPE_STRING;
LA_STRING (a) = user;
attrs[0] = ATM (LM_PASSWD, uidNumber);
attrs[1] = NULL;
stat = _nss_ldap_search_s (&a, _nss_ldap_filt_getpwuid, LM_PASSWD,
attrs, 1, &res);
if (stat != NSS_SUCCESS)
return stat;
e = _nss_ldap_first_entry (res);
if (e == NULL)
{
ldap_msgfree (res);
return NSS_NOTFOUND;
}
vals = _nss_ldap_get_values (e, attrs[0]);
if (vals == NULL)
{
ldap_msgfree (res);
return NSS_NOTFOUND;
}
if (vals[0] == NULL || (vals[0])[0] == '\0')
{
ldap_value_free (vals);
ldap_msgfree (res);
return NSS_NOTFOUND;
}
stat = _nss_ldap_parse_uid_t (vals[0], 0, uid);
ldap_value_free (vals);
ldap_msgfree (res);
return stat;
}
/*
* Get membership for a group
*/
static int
_nss_ldap_getgrusers (char *group, void *result, int type, int *size)
{
struct group *gr;
struct irs_gr *be;
char **memp;
size_t i;
be = (struct irs_gr *) gr_pvtinit ();
if (be == NULL)
{
errno = ENOSYS;
return -1;
}
gr = (be->byname) (be, group);
if (gr == NULL)
{
(be->close) (be);
errno = ENOENT;
return -1;
}
if (gr->gr_mem == NULL)
{
(be->close) (be);
*size = 0;
return 0;
}
for (i = 0; gr->gr_mem[i] != NULL; i++)
;
if (i > *size)
{
(be->close) (be);
*size = i;
errno = ERANGE;
return -1;
}
_nss_ldap_enter ();
for (i = 0, memp = gr->gr_mem; *memp != NULL; memp++)
{
if (type == SEC_INT)
{
if (uess_get_pwuid(*memp, &(((uid_t *)result)[i])) != NSS_SUCCESS)
continue;
}
else
{
((char **)result)[i] = strdup(*memp);
if (((char **)result)[i] == NULL)
{
_nss_ldap_leave ();
(be->close) (be);
errno = ENOMEM;
return -1;
}
}
i++;
}
_nss_ldap_leave ();
*size = i;
(be->close) (be);
return AUTH_SUCCESS;
}
#if 0
/*
* Additional attributes supported
*/
static attrlist_t **
_nss_ldap_attrlist(void)
{
attrlist_t **a;
a = malloc(2 * sizeof(attrlist_t *) + sizeof(attrlist_t));
if (a == NULL)
{
errno = ENOMEM;
return NULL;
}
a[0] = (attrlist_t *)(a + 2);
a[0]->al_name = strdup(S_LDAPDN);
a[0]->al_flags = AL_USERATTR;
a[0]->al_type = SEC_CHAR;
a[1] = NULL;
return a;
}
#endif /* notdef */
#if 0
/* not implemented yet */
static int
_nss_ldap_normalize (char *longname, char *shortname)
{
}
#endif
int
nss_ldap_initialize (struct secmethod_table *meths)
{
memset (meths, 0, sizeof (*meths));
/* Initialize schema */
(void) _nss_ldap_init();
/* Identification methods */
meths->method_getpwnam = _nss_ldap_getpwnam;
meths->method_getpwuid = _nss_ldap_getpwuid;
meths->method_getgrnam = _nss_ldap_getgrnam;
meths->method_getgrgid = _nss_ldap_getgrgid;
meths->method_getgrset = _nss_ldap_getgrset;
meths->method_getentry = _nss_ldap_getentry;
/* meths->method_attrlist = _nss_ldap_attrlist; */
meths->method_getgrusers = _nss_ldap_getgrusers;
/* meths->method_normalize = _nss_ldap_normalize; */
meths->method_getgracct = _nss_ldap_getgracct;
meths->method_getpasswd = _nss_ldap_getpasswd;
/* Support methods */
meths->method_open = _nss_ldap_uess_open;
meths->method_close = _nss_ldap_uess_close;
/* Authentication methods */
meths->method_authenticate = _nss_ldap_authenticate;
return AUTH_SUCCESS;
}
#endif /* HAVE_USERSEC_H */
|