1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 1999-2003 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <stdlib.h> 30*0Sstevel@tonic-gate #include <libintl.h> 31*0Sstevel@tonic-gate #include <stdio.h> 32*0Sstevel@tonic-gate #include <errno.h> 33*0Sstevel@tonic-gate #include <strings.h> 34*0Sstevel@tonic-gate #include "ns_sldap.h" 35*0Sstevel@tonic-gate #include "ns_internal.h" 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate /* 38*0Sstevel@tonic-gate * getldaplaliasbyname() retrieves the aliases information from the LDAP server. 39*0Sstevel@tonic-gate * This is requires that the LDAP naming information (ie. LDAP_CLIENT_CACHE 40*0Sstevel@tonic-gate * file) is configured properly on the client machine. 41*0Sstevel@tonic-gate * 42*0Sstevel@tonic-gate * Return value: 43*0Sstevel@tonic-gate * 0 = success; 44*0Sstevel@tonic-gate * 1 = alias not found; 45*0Sstevel@tonic-gate * -1 = other failure. Contents in answer are undefined. 46*0Sstevel@tonic-gate */ 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate #define ALIAS_FILTER "(&(objectclass=mailgroup)(|(cn=%s)(mail=%s)))" 49*0Sstevel@tonic-gate #define ALIAS_FILTER_SSD "(&(%%s)(|(cn=%s)(mail=%s)))" 50*0Sstevel@tonic-gate #define MAIL_CN "cn" 51*0Sstevel@tonic-gate #define MAIL_ATTRIBUTE "mail" 52*0Sstevel@tonic-gate #define MAIL_MEMBER "mgrpRFC822MailMember" 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate /* 55*0Sstevel@tonic-gate * This is a generic filter call back function for 56*0Sstevel@tonic-gate * merging the filter from service search descriptor with 57*0Sstevel@tonic-gate * an existing search filter. This routine expects userdata 58*0Sstevel@tonic-gate * contain a format string with a single %s in it, and will 59*0Sstevel@tonic-gate * use the format string with sprintf() to insert the SSD filter. 60*0Sstevel@tonic-gate * 61*0Sstevel@tonic-gate * This routine is passed to the __ns_ldap_list() API as the 62*0Sstevel@tonic-gate * filter call back together with filter and userdata. For example, 63*0Sstevel@tonic-gate * "(&(objectclass=mailgroup)(|(cn=abc)(mail=abc)))" as filter 64*0Sstevel@tonic-gate * and "(&(%s)(|(cn=abc)(mail=abc)))" as userdata. 65*0Sstevel@tonic-gate * This routine will then be called by __ns_ldap_list() to output 66*0Sstevel@tonic-gate * "(&(dept=sds)(|(cn=abc)(mail=abc)))" as the real search 67*0Sstevel@tonic-gate * filter, if the input SSD contains a filter "dpet=sds". 68*0Sstevel@tonic-gate */ 69*0Sstevel@tonic-gate int 70*0Sstevel@tonic-gate __s_api_merge_SSD_filter(const ns_ldap_search_desc_t *desc, 71*0Sstevel@tonic-gate char **realfilter, 72*0Sstevel@tonic-gate const void *userdata) 73*0Sstevel@tonic-gate { 74*0Sstevel@tonic-gate int len; 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate /* sanity check */ 77*0Sstevel@tonic-gate if (realfilter == NULL) 78*0Sstevel@tonic-gate return (NS_LDAP_INVALID_PARAM); 79*0Sstevel@tonic-gate *realfilter = NULL; 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate if (desc == NULL || desc->filter == NULL || 82*0Sstevel@tonic-gate userdata == NULL) 83*0Sstevel@tonic-gate return (NS_LDAP_INVALID_PARAM); 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate len = strlen(userdata) + strlen(desc->filter) + 1; 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate *realfilter = (char *)malloc(len); 88*0Sstevel@tonic-gate if (*realfilter == NULL) 89*0Sstevel@tonic-gate return (NS_LDAP_MEMORY); 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate (void) sprintf(*realfilter, (char *)userdata, 92*0Sstevel@tonic-gate desc->filter); 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate return (NS_LDAP_SUCCESS); 95*0Sstevel@tonic-gate } 96*0Sstevel@tonic-gate int 97*0Sstevel@tonic-gate __getldapaliasbyname(char *alias, char *answer, size_t ans_len) 98*0Sstevel@tonic-gate { 99*0Sstevel@tonic-gate char *service = "aliases"; 100*0Sstevel@tonic-gate char filter[BUFSIZE]; 101*0Sstevel@tonic-gate char userdata[BUFSIZE]; 102*0Sstevel@tonic-gate char *attribute[2]; 103*0Sstevel@tonic-gate ns_ldap_result_t *result = NULL; 104*0Sstevel@tonic-gate ns_ldap_error_t *errorp = NULL; 105*0Sstevel@tonic-gate int rc, i, j, len, comma; 106*0Sstevel@tonic-gate ns_ldap_entry_t *entry = NULL; 107*0Sstevel@tonic-gate char **attr_value = NULL; 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate if (!alias || !*alias || !answer || ans_len == 0) { 110*0Sstevel@tonic-gate errno = EINVAL; 111*0Sstevel@tonic-gate return (-1); 112*0Sstevel@tonic-gate } 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate answer[0] = '\0'; 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate /* get the aliases */ 117*0Sstevel@tonic-gate if (snprintf(filter, sizeof (filter), ALIAS_FILTER, alias, alias) < 0) { 118*0Sstevel@tonic-gate errno = EINVAL; 119*0Sstevel@tonic-gate return (-1); 120*0Sstevel@tonic-gate } 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate /* get the userdata for __ns_ldap_list filter call back */ 123*0Sstevel@tonic-gate if (snprintf(userdata, sizeof (userdata), ALIAS_FILTER_SSD, 124*0Sstevel@tonic-gate alias, alias) < 0) { 125*0Sstevel@tonic-gate errno = EINVAL; 126*0Sstevel@tonic-gate return (-1); 127*0Sstevel@tonic-gate } 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate attribute[0] = MAIL_MEMBER; 130*0Sstevel@tonic-gate attribute[1] = NULL; 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate /* should we do hardlookup */ 133*0Sstevel@tonic-gate rc = __ns_ldap_list(service, (const char *)filter, 134*0Sstevel@tonic-gate __s_api_merge_SSD_filter, 135*0Sstevel@tonic-gate (const char **)attribute, NULL, 0, &result, 136*0Sstevel@tonic-gate &errorp, NULL, userdata); 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate if (rc == NS_LDAP_NOTFOUND) { 139*0Sstevel@tonic-gate errno = ENOENT; 140*0Sstevel@tonic-gate return (1); 141*0Sstevel@tonic-gate } else if (rc != NS_LDAP_SUCCESS) { 142*0Sstevel@tonic-gate #ifdef DEBUG 143*0Sstevel@tonic-gate char *p; 144*0Sstevel@tonic-gate (void) __ns_ldap_err2str(rc, &p); 145*0Sstevel@tonic-gate if (errorp) { 146*0Sstevel@tonic-gate if (errorp->message) 147*0Sstevel@tonic-gate (void) fprintf(stderr, "%s (%s)\n", p, 148*0Sstevel@tonic-gate errorp->message); 149*0Sstevel@tonic-gate } else 150*0Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", p); 151*0Sstevel@tonic-gate #endif /* DEBUG */ 152*0Sstevel@tonic-gate (void) __ns_ldap_freeError(&errorp); 153*0Sstevel@tonic-gate return (-1); 154*0Sstevel@tonic-gate } 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate /* build the return value */ 157*0Sstevel@tonic-gate answer[0] = '\0'; 158*0Sstevel@tonic-gate len = 0; 159*0Sstevel@tonic-gate comma = 0; 160*0Sstevel@tonic-gate entry = result->entry; 161*0Sstevel@tonic-gate for (i = 0; i < result->entries_count; i++) { 162*0Sstevel@tonic-gate attr_value = __ns_ldap_getAttr(entry, MAIL_MEMBER); 163*0Sstevel@tonic-gate if (attr_value == NULL) { 164*0Sstevel@tonic-gate errno = ENOENT; 165*0Sstevel@tonic-gate return (-1); 166*0Sstevel@tonic-gate } 167*0Sstevel@tonic-gate for (j = 0; attr_value[j]; j++) { 168*0Sstevel@tonic-gate char *tmp, *newhead; 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate tmp = attr_value[j]; 171*0Sstevel@tonic-gate while (*tmp == ' ' || *tmp == '\t' && *tmp != '\0') 172*0Sstevel@tonic-gate tmp++; 173*0Sstevel@tonic-gate newhead = tmp; 174*0Sstevel@tonic-gate while (*tmp != '\0') tmp++; 175*0Sstevel@tonic-gate while (*tmp == ' ' || *tmp == '\t' || *tmp == '\0' && 176*0Sstevel@tonic-gate tmp != newhead) { 177*0Sstevel@tonic-gate *tmp-- = '\0'; 178*0Sstevel@tonic-gate } 179*0Sstevel@tonic-gate len = len + comma + strlen(newhead); 180*0Sstevel@tonic-gate if ((len + 1) > ans_len) { 181*0Sstevel@tonic-gate (void) __ns_ldap_freeResult(&result); 182*0Sstevel@tonic-gate errno = EOVERFLOW; 183*0Sstevel@tonic-gate return (-1); 184*0Sstevel@tonic-gate } 185*0Sstevel@tonic-gate if (comma) 186*0Sstevel@tonic-gate (void) strcat(answer, ","); 187*0Sstevel@tonic-gate else 188*0Sstevel@tonic-gate comma = 1; 189*0Sstevel@tonic-gate (void) strcat(answer, newhead); 190*0Sstevel@tonic-gate } 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate (void) __ns_ldap_freeResult(&result); 194*0Sstevel@tonic-gate errno = 0; 195*0Sstevel@tonic-gate return (0); 196*0Sstevel@tonic-gate } 197