10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52830Sdjl * Common Development and Distribution License (the "License"). 62830Sdjl * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*3386Smichen * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <grp.h> 290Sstevel@tonic-gate #include "ldap_common.h" 300Sstevel@tonic-gate 310Sstevel@tonic-gate /* String which may need to be removed from beginning of group password */ 320Sstevel@tonic-gate #define _CRYPT "{CRYPT}" 330Sstevel@tonic-gate #define _NO_PASSWD_VAL "" 340Sstevel@tonic-gate 350Sstevel@tonic-gate /* Group attributes filters */ 360Sstevel@tonic-gate #define _G_NAME "cn" 370Sstevel@tonic-gate #define _G_GID "gidnumber" 380Sstevel@tonic-gate #define _G_PASSWD "userpassword" 390Sstevel@tonic-gate #define _G_MEM "memberuid" 400Sstevel@tonic-gate 410Sstevel@tonic-gate #define _F_GETGRNAM "(&(objectClass=posixGroup)(cn=%s))" 420Sstevel@tonic-gate #define _F_GETGRNAM_SSD "(&(%%s)(cn=%s))" 430Sstevel@tonic-gate #define _F_GETGRGID "(&(objectClass=posixGroup)(gidNumber=%ld))" 440Sstevel@tonic-gate #define _F_GETGRGID_SSD "(&(%%s)(gidNumber=%ld))" 450Sstevel@tonic-gate #define _F_GETGRMEM "(&(objectClass=posixGroup)(memberUid=%s))" 460Sstevel@tonic-gate #define _F_GETGRMEM_SSD "(&(%%s)(memberUid=%s))" 470Sstevel@tonic-gate 480Sstevel@tonic-gate static const char *gr_attrs[] = { 490Sstevel@tonic-gate _G_NAME, 500Sstevel@tonic-gate _G_GID, 510Sstevel@tonic-gate _G_PASSWD, 520Sstevel@tonic-gate _G_MEM, 530Sstevel@tonic-gate (char *)NULL 540Sstevel@tonic-gate }; 550Sstevel@tonic-gate 560Sstevel@tonic-gate 570Sstevel@tonic-gate /* 582830Sdjl * _nss_ldap_group2str is the data marshaling method for the group getXbyY 590Sstevel@tonic-gate * (e.g., getgrnam(), getgrgid(), getgrent()) backend processes. This method 600Sstevel@tonic-gate * is called after a successful ldap search has been performed. This method 612830Sdjl * will parse the ldap search values into the file format. 622830Sdjl * e.g. 632830Sdjl * 642830Sdjl * adm::4:root,adm,daemon 652830Sdjl * 660Sstevel@tonic-gate */ 670Sstevel@tonic-gate 680Sstevel@tonic-gate static int 692830Sdjl _nss_ldap_group2str(ldap_backend_ptr be, nss_XbyY_args_t *argp) 700Sstevel@tonic-gate { 712830Sdjl int i; 720Sstevel@tonic-gate int nss_result; 732830Sdjl int buflen = 0, len; 742830Sdjl int firstime = 1; 752830Sdjl char *buffer = NULL; 760Sstevel@tonic-gate ns_ldap_result_t *result = be->result; 772830Sdjl char **gname, **passwd, **gid, *password; 782830Sdjl ns_ldap_attr_t *members; 792830Sdjl 802830Sdjl 812830Sdjl if (result == NULL) 822830Sdjl return (NSS_STR_PARSE_PARSE); 832830Sdjl buflen = argp->buf.buflen; 842830Sdjl 852830Sdjl if (argp->buf.result != NULL) { 862830Sdjl if ((be->buffer = calloc(1, buflen)) == NULL) { 872830Sdjl nss_result = NSS_STR_PARSE_PARSE; 882830Sdjl goto result_grp2str; 892830Sdjl } 902830Sdjl buffer = be->buffer; 912830Sdjl } else 922830Sdjl buffer = argp->buf.buffer; 932830Sdjl 942830Sdjl nss_result = NSS_STR_PARSE_SUCCESS; 952830Sdjl (void) memset(buffer, 0, buflen); 960Sstevel@tonic-gate 972830Sdjl gname = __ns_ldap_getAttr(result->entry, _G_NAME); 982830Sdjl if (gname == NULL || gname[0] == NULL || (strlen(gname[0]) < 1)) { 992830Sdjl nss_result = NSS_STR_PARSE_PARSE; 1002830Sdjl goto result_grp2str; 1010Sstevel@tonic-gate } 1022830Sdjl passwd = __ns_ldap_getAttr(result->entry, _G_PASSWD); 1032830Sdjl if (passwd == NULL || passwd[0] == NULL || (strlen(passwd[0]) == 0)) { 1042830Sdjl /* group password could be NULL, replace it with "" */ 1052830Sdjl password = _NO_PASSWD_VAL; 1062830Sdjl } else { 1072830Sdjl /* 1082830Sdjl * Preen "{crypt}" if necessary. 1092830Sdjl * If the password does not include the {crypt} prefix 1102830Sdjl * then the password may be plain text. And thus 1112830Sdjl * perhaps crypt(3c) should be used to encrypt it. 1122830Sdjl * Currently the password is copied verbatim. 1132830Sdjl */ 1142830Sdjl if (strncasecmp(passwd[0], _CRYPT, strlen(_CRYPT)) == 0) 1152830Sdjl password = passwd[0] + strlen(_CRYPT); 1162830Sdjl else 1172830Sdjl password = passwd[0]; 1182830Sdjl } 1192830Sdjl gid = __ns_ldap_getAttr(result->entry, _G_GID); 1202830Sdjl if (gid == NULL || gid[0] == NULL || (strlen(gid[0]) < 1)) { 1212830Sdjl nss_result = NSS_STR_PARSE_PARSE; 1222830Sdjl goto result_grp2str; 1232830Sdjl } 1242830Sdjl len = snprintf(buffer, buflen, "%s:%s:%s:", 1252830Sdjl gname[0], password, gid[0]); 1262830Sdjl TEST_AND_ADJUST(len, buffer, buflen, result_grp2str); 1270Sstevel@tonic-gate 1282830Sdjl members = __ns_ldap_getAttrStruct(result->entry, _G_MEM); 1292830Sdjl if (members == NULL || members->attrvalue == NULL) { 130*3386Smichen /* no member is fine, skip processing the member list */ 131*3386Smichen goto nomember; 1320Sstevel@tonic-gate } 1330Sstevel@tonic-gate 1342830Sdjl for (i = 0; i < members->value_count; i++) { 1352830Sdjl if (members->attrvalue[i] == NULL) { 1362830Sdjl nss_result = NSS_STR_PARSE_PARSE; 1372830Sdjl goto result_grp2str; 1380Sstevel@tonic-gate } 1392830Sdjl if (firstime) { 1402830Sdjl len = snprintf(buffer, buflen, "%s", 1412830Sdjl members->attrvalue[i]); 1422830Sdjl TEST_AND_ADJUST(len, buffer, buflen, result_grp2str); 1432830Sdjl firstime = 0; 1442830Sdjl } else { 1452830Sdjl len = snprintf(buffer, buflen, ",%s", 1462830Sdjl members->attrvalue[i]); 1472830Sdjl TEST_AND_ADJUST(len, buffer, buflen, result_grp2str); 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate } 150*3386Smichen nomember: 1512830Sdjl /* The front end marshaller doesn't need the trailing nulls */ 1522830Sdjl if (argp->buf.result != NULL) 1532830Sdjl be->buflen = strlen(be->buffer); 1542830Sdjl result_grp2str: 1550Sstevel@tonic-gate (void) __ns_ldap_freeResult(&be->result); 1562830Sdjl return (nss_result); 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate /* 1600Sstevel@tonic-gate * getbynam gets a group entry by name. This function constructs an ldap 1610Sstevel@tonic-gate * search filter using the name invocation parameter and the getgrnam search 1620Sstevel@tonic-gate * filter defined. Once the filter is constructed, we searche for a matching 1630Sstevel@tonic-gate * entry and marshal the data results into struct group for the frontend 1640Sstevel@tonic-gate * process. The function _nss_ldap_group2ent performs the data marshaling. 1650Sstevel@tonic-gate */ 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate static nss_status_t 1680Sstevel@tonic-gate getbynam(ldap_backend_ptr be, void *a) 1690Sstevel@tonic-gate { 1700Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 1710Sstevel@tonic-gate char searchfilter[SEARCHFILTERLEN]; 1720Sstevel@tonic-gate char userdata[SEARCHFILTERLEN]; 1730Sstevel@tonic-gate char groupname[SEARCHFILTERLEN]; 1740Sstevel@tonic-gate int ret; 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate if (_ldap_filter_name(groupname, argp->key.name, sizeof (groupname)) 1770Sstevel@tonic-gate != 0) 1780Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate ret = snprintf(searchfilter, sizeof (searchfilter), 1810Sstevel@tonic-gate _F_GETGRNAM, groupname); 1820Sstevel@tonic-gate if (ret >= sizeof (searchfilter) || ret < 0) 1830Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate ret = snprintf(userdata, sizeof (userdata), _F_GETGRNAM_SSD, groupname); 1860Sstevel@tonic-gate if (ret >= sizeof (userdata) || ret < 0) 1870Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate return ((nss_status_t)_nss_ldap_lookup(be, argp, 1900Sstevel@tonic-gate _GROUP, searchfilter, NULL, 1910Sstevel@tonic-gate _merge_SSD_filter, userdata)); 1920Sstevel@tonic-gate } 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate /* 1960Sstevel@tonic-gate * getbygid gets a group entry by number. This function constructs an ldap 1970Sstevel@tonic-gate * search filter using the name invocation parameter and the getgrgid search 1980Sstevel@tonic-gate * filter defined. Once the filter is constructed, we searche for a matching 1990Sstevel@tonic-gate * entry and marshal the data results into struct group for the frontend 2000Sstevel@tonic-gate * process. The function _nss_ldap_group2ent performs the data marshaling. 2010Sstevel@tonic-gate */ 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate static nss_status_t 2040Sstevel@tonic-gate getbygid(ldap_backend_ptr be, void *a) 2050Sstevel@tonic-gate { 2060Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 2070Sstevel@tonic-gate char searchfilter[SEARCHFILTERLEN]; 2080Sstevel@tonic-gate char userdata[SEARCHFILTERLEN]; 2090Sstevel@tonic-gate int ret; 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate ret = snprintf(searchfilter, sizeof (searchfilter), 2120Sstevel@tonic-gate _F_GETGRGID, (long)argp->key.uid); 2130Sstevel@tonic-gate if (ret >= sizeof (searchfilter) || ret < 0) 2140Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate ret = snprintf(userdata, sizeof (userdata), 2170Sstevel@tonic-gate _F_GETGRGID_SSD, (long)argp->key.uid); 2180Sstevel@tonic-gate if (ret >= sizeof (userdata) || ret < 0) 2190Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate return ((nss_status_t)_nss_ldap_lookup(be, argp, 2220Sstevel@tonic-gate _GROUP, searchfilter, NULL, 2230Sstevel@tonic-gate _merge_SSD_filter, userdata)); 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate /* 2290Sstevel@tonic-gate * getbymember returns all groups a user is defined in. This function 2300Sstevel@tonic-gate * uses different architectural procedures than the other group backend 2310Sstevel@tonic-gate * system calls because it's a private interface. This function constructs 2320Sstevel@tonic-gate * an ldap search filter using the name invocation parameter. Once the 2330Sstevel@tonic-gate * filter is constructed, we search for all matching groups counting 2340Sstevel@tonic-gate * and storing each group name, gid, etc. Data marshaling is used for 2350Sstevel@tonic-gate * group processing. The function _nss_ldap_group2ent() performs the 2360Sstevel@tonic-gate * data marshaling. 2370Sstevel@tonic-gate * 2380Sstevel@tonic-gate * (const char *)argp->username; (size_t)strlen(argp->username); 2390Sstevel@tonic-gate * (gid_t)argp->gid_array; (int)argp->maxgids; 2400Sstevel@tonic-gate * (int)argp->numgids; 2410Sstevel@tonic-gate */ 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate static nss_status_t 2440Sstevel@tonic-gate getbymember(ldap_backend_ptr be, void *a) 2450Sstevel@tonic-gate { 2460Sstevel@tonic-gate int i, j, k; 2470Sstevel@tonic-gate int gcnt = (int)0; 2480Sstevel@tonic-gate char **groupvalue, **membervalue; 2490Sstevel@tonic-gate nss_status_t lstat; 2500Sstevel@tonic-gate nss_XbyY_args_t argb; 2510Sstevel@tonic-gate static nss_XbyY_buf_t *gb; 2520Sstevel@tonic-gate struct nss_groupsbymem *argp = (struct nss_groupsbymem *)a; 2530Sstevel@tonic-gate char searchfilter[SEARCHFILTERLEN]; 2540Sstevel@tonic-gate char userdata[SEARCHFILTERLEN]; 2550Sstevel@tonic-gate char name[SEARCHFILTERLEN]; 2560Sstevel@tonic-gate ns_ldap_result_t *result; 2570Sstevel@tonic-gate ns_ldap_entry_t *curEntry; 2580Sstevel@tonic-gate char *username; 2590Sstevel@tonic-gate gid_t gid; 2600Sstevel@tonic-gate int ret; 2610Sstevel@tonic-gate 2622830Sdjl /* LINTED E_EXPR_NULL_EFFECT */ 2630Sstevel@tonic-gate NSS_XbyY_ALLOC(&gb, sizeof (struct group), NSS_BUFLEN_GROUP); 2640Sstevel@tonic-gate NSS_XbyY_INIT(&argb, gb->result, gb->buffer, gb->buflen, 0); 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate if (strcmp(argp->username, "") == 0 || 2670Sstevel@tonic-gate strcmp(argp->username, "root") == 0) 2680Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate if (_ldap_filter_name(name, argp->username, sizeof (name)) != 0) 2710Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate ret = snprintf(searchfilter, sizeof (searchfilter), _F_GETGRMEM, name); 2740Sstevel@tonic-gate if (ret >= sizeof (searchfilter) || ret < 0) 2750Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate ret = snprintf(userdata, sizeof (userdata), _F_GETGRMEM_SSD, name); 2780Sstevel@tonic-gate if (ret >= sizeof (userdata) || ret < 0) 2790Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate gcnt = (int)argp->numgids; 2820Sstevel@tonic-gate lstat = (nss_status_t)_nss_ldap_nocb_lookup(be, &argb, 2830Sstevel@tonic-gate _GROUP, searchfilter, NULL, 2840Sstevel@tonic-gate _merge_SSD_filter, userdata); 2850Sstevel@tonic-gate if (lstat != (nss_status_t)NS_LDAP_SUCCESS) 2860Sstevel@tonic-gate return ((nss_status_t)lstat); 2870Sstevel@tonic-gate if (be->result == NULL) 2880Sstevel@tonic-gate return (NSS_NOTFOUND); 2890Sstevel@tonic-gate username = (char *)argp->username; 2900Sstevel@tonic-gate result = (ns_ldap_result_t *)be->result; 2910Sstevel@tonic-gate curEntry = (ns_ldap_entry_t *)result->entry; 2920Sstevel@tonic-gate for (i = 0; i < result->entries_count; i++) { 2930Sstevel@tonic-gate membervalue = __ns_ldap_getAttr(curEntry, "memberUid"); 2940Sstevel@tonic-gate if (membervalue) { 2950Sstevel@tonic-gate for (j = 0; membervalue[j]; j++) { 2960Sstevel@tonic-gate if (strcmp(membervalue[j], username) == NULL) { 2970Sstevel@tonic-gate groupvalue = __ns_ldap_getAttr(curEntry, 2980Sstevel@tonic-gate "gidnumber"); 2990Sstevel@tonic-gate gid = (gid_t)strtol(groupvalue[0], 3000Sstevel@tonic-gate (char **)NULL, 10); 3010Sstevel@tonic-gate if (argp->numgids < argp->maxgids) { 3020Sstevel@tonic-gate for (k = 0; k < argp->numgids; 3030Sstevel@tonic-gate k++) { 3040Sstevel@tonic-gate if (argp->gid_array[k] == gid) 3050Sstevel@tonic-gate /* already exists */ 3060Sstevel@tonic-gate break; 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate if (k == argp->numgids) 3090Sstevel@tonic-gate argp->gid_array[argp->numgids++] 3100Sstevel@tonic-gate = gid; 3110Sstevel@tonic-gate } 3120Sstevel@tonic-gate break; 3130Sstevel@tonic-gate } 3140Sstevel@tonic-gate } 3150Sstevel@tonic-gate } 3160Sstevel@tonic-gate curEntry = curEntry->next; 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate 3192830Sdjl (void) __ns_ldap_freeResult((ns_ldap_result_t **)&be->result); 3200Sstevel@tonic-gate NSS_XbyY_FREE(&gb); 3210Sstevel@tonic-gate if (gcnt == argp->numgids) 3220Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate return ((nss_status_t)NSS_SUCCESS); 3250Sstevel@tonic-gate } 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate static ldap_backend_op_t gr_ops[] = { 3280Sstevel@tonic-gate _nss_ldap_destr, 3290Sstevel@tonic-gate _nss_ldap_endent, 3300Sstevel@tonic-gate _nss_ldap_setent, 3310Sstevel@tonic-gate _nss_ldap_getent, 3320Sstevel@tonic-gate getbynam, 3330Sstevel@tonic-gate getbygid, 3340Sstevel@tonic-gate getbymember 3350Sstevel@tonic-gate }; 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate /*ARGSUSED0*/ 3390Sstevel@tonic-gate nss_backend_t * 3400Sstevel@tonic-gate _nss_ldap_group_constr(const char *dummy1, const char *dummy2, 3410Sstevel@tonic-gate const char *dummy3) 3420Sstevel@tonic-gate { 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate return ((nss_backend_t *)_nss_ldap_constr(gr_ops, 3450Sstevel@tonic-gate sizeof (gr_ops)/sizeof (gr_ops[0]), _GROUP, gr_attrs, 3462830Sdjl _nss_ldap_group2str)); 3470Sstevel@tonic-gate } 348