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