17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*cb5caa98Sdjl * Common Development and Distribution License (the "License"). 6*cb5caa98Sdjl * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*cb5caa98Sdjl * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <rpc/rpcent.h> 297c478bd9Sstevel@tonic-gate #include "ns_internal.h" 307c478bd9Sstevel@tonic-gate #include "ldap_common.h" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate /* rpc attributes filters */ 337c478bd9Sstevel@tonic-gate #define _R_NAME "cn" 347c478bd9Sstevel@tonic-gate #define _R_NUMBER "oncrpcnumber" 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate #define _F_GETRPCBYNAME "(&(objectClass=oncRpc)(cn=%s))" 387c478bd9Sstevel@tonic-gate #define _F_GETRPCBYNAME_SSD "(&(%%s)(cn=%s))" 397c478bd9Sstevel@tonic-gate #define _F_GETRPCBYNUMBER "(&(objectClass=oncRpc)(oncRpcNumber=%d))" 407c478bd9Sstevel@tonic-gate #define _F_GETRPCBYNUMBER_SSD "(&(%%s)(oncRpcNumber=%d))" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate static const char *rpc_attrs[] = { 437c478bd9Sstevel@tonic-gate _R_NAME, 447c478bd9Sstevel@tonic-gate _R_NUMBER, 457c478bd9Sstevel@tonic-gate (char *)NULL 467c478bd9Sstevel@tonic-gate }; 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate /* 49*cb5caa98Sdjl * _nss_ldap_rpc2str is the data marshaling method for the rpc getXbyY 507c478bd9Sstevel@tonic-gate * (e.g., getbyname(), getbynumber(), getrpcent()) backend processes. 517c478bd9Sstevel@tonic-gate * This method is called after a successful ldap search has been performed. 52*cb5caa98Sdjl * This method will parse the ldap search values into the file format. 53*cb5caa98Sdjl * e.g. 54*cb5caa98Sdjl * 55*cb5caa98Sdjl * nfs_acl 100227 56*cb5caa98Sdjl * snmp 100122 na.snmp snmp-cmc snmp-synoptics snmp-unisys snmp-utk 577c478bd9Sstevel@tonic-gate */ 587c478bd9Sstevel@tonic-gate static int 59*cb5caa98Sdjl _nss_ldap_rpc2str(ldap_backend_ptr be, nss_XbyY_args_t *argp) 607c478bd9Sstevel@tonic-gate { 61*cb5caa98Sdjl uint_t i; 627c478bd9Sstevel@tonic-gate int nss_result; 63*cb5caa98Sdjl int buflen = 0, len; 64*cb5caa98Sdjl char *cname = NULL; 65*cb5caa98Sdjl char *buffer = NULL; 667c478bd9Sstevel@tonic-gate ns_ldap_result_t *result = be->result; 67*cb5caa98Sdjl ns_ldap_attr_t *names; 68*cb5caa98Sdjl char **rpcnumber; 697c478bd9Sstevel@tonic-gate 70*cb5caa98Sdjl if (result == NULL) 71*cb5caa98Sdjl return (NSS_STR_PARSE_PARSE); 72*cb5caa98Sdjl nss_result = NSS_STR_PARSE_SUCCESS; 737c478bd9Sstevel@tonic-gate (void) memset(argp->buf.buffer, 0, buflen); 747c478bd9Sstevel@tonic-gate 75*cb5caa98Sdjl buflen = argp->buf.buflen; 76*cb5caa98Sdjl if (argp->buf.result != NULL) { 77*cb5caa98Sdjl if ((be->buffer = calloc(1, buflen)) == NULL) { 78*cb5caa98Sdjl nss_result = NSS_STR_PARSE_PARSE; 79*cb5caa98Sdjl goto result_rpc2str; 807c478bd9Sstevel@tonic-gate } 81*cb5caa98Sdjl buffer = be->buffer; 82*cb5caa98Sdjl } else 83*cb5caa98Sdjl buffer = argp->buf.buffer; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate 86*cb5caa98Sdjl names = __ns_ldap_getAttrStruct(result->entry, _R_NAME); 87*cb5caa98Sdjl if (names == NULL || names->attrvalue == NULL) { 88*cb5caa98Sdjl nss_result = NSS_STR_PARSE_PARSE; 89*cb5caa98Sdjl goto result_rpc2str; 90*cb5caa98Sdjl } 91*cb5caa98Sdjl /* Get the canonical rpc name */ 92*cb5caa98Sdjl cname = __s_api_get_canonical_name(result->entry, names, 1); 93*cb5caa98Sdjl if (cname == NULL || (len = strlen(cname)) < 1) { 94*cb5caa98Sdjl nss_result = NSS_STR_PARSE_PARSE; 95*cb5caa98Sdjl goto result_rpc2str; 96*cb5caa98Sdjl } 97*cb5caa98Sdjl rpcnumber = __ns_ldap_getAttr(result->entry, _R_NUMBER); 98*cb5caa98Sdjl if (rpcnumber == NULL || rpcnumber[0] == NULL || 99*cb5caa98Sdjl (len = strlen(rpcnumber[0])) < 1) { 100*cb5caa98Sdjl nss_result = NSS_STR_PARSE_PARSE; 101*cb5caa98Sdjl goto result_rpc2str; 102*cb5caa98Sdjl } 103*cb5caa98Sdjl len = snprintf(buffer, buflen, "%s %s", cname, rpcnumber[0]); 104*cb5caa98Sdjl TEST_AND_ADJUST(len, buffer, buflen, result_rpc2str); 105*cb5caa98Sdjl /* Append aliases */ 106*cb5caa98Sdjl for (i = 0; i < names->value_count; i++) { 107*cb5caa98Sdjl if (names->attrvalue[i] == NULL) { 108*cb5caa98Sdjl nss_result = NSS_STR_PARSE_PARSE; 109*cb5caa98Sdjl goto result_rpc2str; 110*cb5caa98Sdjl } 111*cb5caa98Sdjl /* Skip the canonical name */ 112*cb5caa98Sdjl if (strcasecmp(names->attrvalue[i], cname) != 0) { 113*cb5caa98Sdjl len = snprintf(buffer, buflen, " %s", 114*cb5caa98Sdjl names->attrvalue[i]); 115*cb5caa98Sdjl TEST_AND_ADJUST(len, buffer, buflen, result_rpc2str); 116*cb5caa98Sdjl } 117*cb5caa98Sdjl } 118*cb5caa98Sdjl 119*cb5caa98Sdjl /* The front end marshaller doesn't need to copy trailing nulls */ 120*cb5caa98Sdjl if (argp->buf.result != NULL) 121*cb5caa98Sdjl be->buflen = strlen(be->buffer); 122*cb5caa98Sdjl 123*cb5caa98Sdjl result_rpc2str: 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate (void) __ns_ldap_freeResult(&be->result); 126*cb5caa98Sdjl return (nss_result); 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate /* 1307c478bd9Sstevel@tonic-gate * getbyname gets struct rpcent values by rpc name. This function 1317c478bd9Sstevel@tonic-gate * constructs an ldap search filter using the rpc name invocation 1327c478bd9Sstevel@tonic-gate * parameter and the getrpcbyname search filter defined. Once the 1337c478bd9Sstevel@tonic-gate * filter is constructed, we search for a matching entry and marshal 1347c478bd9Sstevel@tonic-gate * the data results into *rpc = (struct rpcent *)argp->buf.result. 1357c478bd9Sstevel@tonic-gate * The function _nss_ldap_rpc2ent performs the data marshaling. 1367c478bd9Sstevel@tonic-gate */ 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate static nss_status_t 1397c478bd9Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a) 1407c478bd9Sstevel@tonic-gate { 1417c478bd9Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 1427c478bd9Sstevel@tonic-gate char searchfilter[SEARCHFILTERLEN]; 1437c478bd9Sstevel@tonic-gate char userdata[SEARCHFILTERLEN]; 1447c478bd9Sstevel@tonic-gate char name[SEARCHFILTERLEN]; 1457c478bd9Sstevel@tonic-gate int ret; 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0) 1487c478bd9Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate ret = snprintf(searchfilter, sizeof (searchfilter), _F_GETRPCBYNAME, 1517c478bd9Sstevel@tonic-gate name); 1527c478bd9Sstevel@tonic-gate if (ret >= sizeof (searchfilter) || ret < 0) 1537c478bd9Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate ret = snprintf(userdata, sizeof (userdata), _F_GETRPCBYNAME_SSD, name); 1567c478bd9Sstevel@tonic-gate if (ret >= sizeof (userdata) || ret < 0) 1577c478bd9Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate return ((nss_status_t)_nss_ldap_lookup(be, argp, _RPC, searchfilter, 1607c478bd9Sstevel@tonic-gate NULL, _merge_SSD_filter, userdata)); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate /* 1657c478bd9Sstevel@tonic-gate * getbynumber gets struct rpcent values by rpc number. This function 1667c478bd9Sstevel@tonic-gate * constructs an ldap search filter using the rpc number invocation 1677c478bd9Sstevel@tonic-gate * parameter and the getrpcbynumber search filter defined. Once the 1687c478bd9Sstevel@tonic-gate * filter is constructed, we search for a matching entry and marshal 1697c478bd9Sstevel@tonic-gate * the data results into *rpc = (struct rpcent *)argp->buf.result. 1707c478bd9Sstevel@tonic-gate * The function _nss_ldap_rpc2ent performs the data marshaling. 1717c478bd9Sstevel@tonic-gate */ 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate static nss_status_t 1747c478bd9Sstevel@tonic-gate getbynumber(ldap_backend_ptr be, void *a) 1757c478bd9Sstevel@tonic-gate { 1767c478bd9Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 1777c478bd9Sstevel@tonic-gate char searchfilter[SEARCHFILTERLEN]; 1787c478bd9Sstevel@tonic-gate char userdata[SEARCHFILTERLEN]; 1797c478bd9Sstevel@tonic-gate int ret; 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate ret = snprintf(searchfilter, sizeof (searchfilter), 1827c478bd9Sstevel@tonic-gate _F_GETRPCBYNUMBER, argp->key.number); 1837c478bd9Sstevel@tonic-gate if (ret >= sizeof (searchfilter) || ret < 0) 1847c478bd9Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate ret = snprintf(userdata, sizeof (userdata), 1877c478bd9Sstevel@tonic-gate _F_GETRPCBYNUMBER_SSD, argp->key.number); 1887c478bd9Sstevel@tonic-gate if (ret >= sizeof (userdata) || ret < 0) 1897c478bd9Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate return ((nss_status_t)_nss_ldap_lookup(be, argp, _RPC, searchfilter, 1927c478bd9Sstevel@tonic-gate NULL, _merge_SSD_filter, userdata)); 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate static ldap_backend_op_t rpc_ops[] = { 1977c478bd9Sstevel@tonic-gate _nss_ldap_destr, 1987c478bd9Sstevel@tonic-gate _nss_ldap_endent, 1997c478bd9Sstevel@tonic-gate _nss_ldap_setent, 2007c478bd9Sstevel@tonic-gate _nss_ldap_getent, 2017c478bd9Sstevel@tonic-gate getbyname, 2027c478bd9Sstevel@tonic-gate getbynumber 2037c478bd9Sstevel@tonic-gate }; 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate /* 2077c478bd9Sstevel@tonic-gate * _nss_ldap_rpc_constr is where life begins. This function calls the generic 2087c478bd9Sstevel@tonic-gate * ldap constructor function to define and build the abstract data types 2097c478bd9Sstevel@tonic-gate * required to support ldap operations. 2107c478bd9Sstevel@tonic-gate */ 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate /*ARGSUSED0*/ 2137c478bd9Sstevel@tonic-gate nss_backend_t * 2147c478bd9Sstevel@tonic-gate _nss_ldap_rpc_constr(const char *dummy1, const char *dummy2, 2157c478bd9Sstevel@tonic-gate const char *dummy3) 2167c478bd9Sstevel@tonic-gate { 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate return ((nss_backend_t *)_nss_ldap_constr(rpc_ops, 2197c478bd9Sstevel@tonic-gate sizeof (rpc_ops)/sizeof (rpc_ops[0]), 220*cb5caa98Sdjl _RPC, rpc_attrs, _nss_ldap_rpc2str)); 2217c478bd9Sstevel@tonic-gate } 222