1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <stdio.h> 30*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 31*7c478bd9Sstevel@tonic-gate #include <strings.h> 32*7c478bd9Sstevel@tonic-gate #include <string.h> 33*7c478bd9Sstevel@tonic-gate #include <unistd.h> 34*7c478bd9Sstevel@tonic-gate #include <pthread.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/socket.h> 37*7c478bd9Sstevel@tonic-gate #include <netinet/in.h> 38*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate #include "ldap_util.h" 41*7c478bd9Sstevel@tonic-gate #include "ldap_glob.h" 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate static time_t msgtime[MSG_LASTMSG] = {0}; 44*7c478bd9Sstevel@tonic-gate static time_t msgtimeout = 3600; 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate static pthread_key_t tsdKey; 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate /* 49*7c478bd9Sstevel@tonic-gate * Log a message to the appropriate place. 50*7c478bd9Sstevel@tonic-gate */ 51*7c478bd9Sstevel@tonic-gate void 52*7c478bd9Sstevel@tonic-gate logmsg(int msgtype, int priority, char *fmt, ...) { 53*7c478bd9Sstevel@tonic-gate va_list ap; 54*7c478bd9Sstevel@tonic-gate struct timeval tp; 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate /* 57*7c478bd9Sstevel@tonic-gate * Only log LOG_INFO priority if 'verbose' is on, or if 58*7c478bd9Sstevel@tonic-gate * msgtype is MSG_ALWAYS. 59*7c478bd9Sstevel@tonic-gate */ 60*7c478bd9Sstevel@tonic-gate if (priority == LOG_INFO && !verbose && msgtype != MSG_ALWAYS) 61*7c478bd9Sstevel@tonic-gate return; 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate /* Make sure we don't log the same message too often */ 64*7c478bd9Sstevel@tonic-gate if (msgtype != MSG_NOTIMECHECK && msgtype != MSG_ALWAYS && 65*7c478bd9Sstevel@tonic-gate msgtype > 0 && msgtype < MSG_LASTMSG && 66*7c478bd9Sstevel@tonic-gate gettimeofday(&tp, 0) != -1) { 67*7c478bd9Sstevel@tonic-gate if (tp.tv_sec - msgtime[msgtype] < msgtimeout) 68*7c478bd9Sstevel@tonic-gate return; 69*7c478bd9Sstevel@tonic-gate msgtime[msgtype] = tp.tv_sec; 70*7c478bd9Sstevel@tonic-gate } 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate va_start(ap, fmt); 73*7c478bd9Sstevel@tonic-gate if (cons == 0) { 74*7c478bd9Sstevel@tonic-gate vsyslog(priority, fmt, ap); 75*7c478bd9Sstevel@tonic-gate } else { 76*7c478bd9Sstevel@tonic-gate int flen = slen(fmt); 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate vfprintf(cons, fmt, ap); 79*7c478bd9Sstevel@tonic-gate /* 80*7c478bd9Sstevel@tonic-gate * If the last character in 'fmt' wasn't a '\n', write one 81*7c478bd9Sstevel@tonic-gate * to the console. 82*7c478bd9Sstevel@tonic-gate */ 83*7c478bd9Sstevel@tonic-gate if (flen > 0 && fmt[flen-1] != '\n') 84*7c478bd9Sstevel@tonic-gate fprintf(cons, "\n"); 85*7c478bd9Sstevel@tonic-gate } 86*7c478bd9Sstevel@tonic-gate va_end(ap); 87*7c478bd9Sstevel@tonic-gate } 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate void 90*7c478bd9Sstevel@tonic-gate __destroyTsdKey(void *arg) { 91*7c478bd9Sstevel@tonic-gate __nis_deferred_error_t *defErr = arg; 92*7c478bd9Sstevel@tonic-gate 93*7c478bd9Sstevel@tonic-gate if (defErr != 0) { 94*7c478bd9Sstevel@tonic-gate sfree(defErr->message); 95*7c478bd9Sstevel@tonic-gate free(defErr); 96*7c478bd9Sstevel@tonic-gate } 97*7c478bd9Sstevel@tonic-gate } 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate static void 100*7c478bd9Sstevel@tonic-gate __initTsdKey(void) 101*7c478bd9Sstevel@tonic-gate { 102*7c478bd9Sstevel@tonic-gate (void) pthread_key_create(&tsdKey, __destroyTsdKey); 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate #pragma init(__initTsdKey) 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate void 107*7c478bd9Sstevel@tonic-gate reportError(int error, char *fmt, ...) { 108*7c478bd9Sstevel@tonic-gate __nis_deferred_error_t *defErr = pthread_getspecific(tsdKey); 109*7c478bd9Sstevel@tonic-gate int doStore = (defErr == 0); 110*7c478bd9Sstevel@tonic-gate char *myself = "reportError"; 111*7c478bd9Sstevel@tonic-gate va_list ap; 112*7c478bd9Sstevel@tonic-gate __nis_buffer_t b = {0, 0}; 113*7c478bd9Sstevel@tonic-gate 114*7c478bd9Sstevel@tonic-gate if (defErr == 0 && (defErr = am(myself, sizeof (*defErr))) == 0) 115*7c478bd9Sstevel@tonic-gate return; 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate va_start(ap, fmt); 118*7c478bd9Sstevel@tonic-gate b.len = vp2buf(myself, &b.buf, b.len, fmt, ap); 119*7c478bd9Sstevel@tonic-gate va_end(ap); 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate if (b.len > 0) { 122*7c478bd9Sstevel@tonic-gate defErr->error = error; 123*7c478bd9Sstevel@tonic-gate defErr->message = b.buf; 124*7c478bd9Sstevel@tonic-gate if (doStore) { 125*7c478bd9Sstevel@tonic-gate int ret = pthread_setspecific(tsdKey, defErr); 126*7c478bd9Sstevel@tonic-gate if (ret != 0) { 127*7c478bd9Sstevel@tonic-gate logmsg(MSG_TSDERR, LOG_ERR, 128*7c478bd9Sstevel@tonic-gate "%s: pthread_setspecific() => %d", 129*7c478bd9Sstevel@tonic-gate myself, ret); 130*7c478bd9Sstevel@tonic-gate sfree(b.buf); 131*7c478bd9Sstevel@tonic-gate free(defErr); 132*7c478bd9Sstevel@tonic-gate } 133*7c478bd9Sstevel@tonic-gate } 134*7c478bd9Sstevel@tonic-gate } 135*7c478bd9Sstevel@tonic-gate } 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate int 138*7c478bd9Sstevel@tonic-gate getError(char **message) { 139*7c478bd9Sstevel@tonic-gate __nis_deferred_error_t *defErr = pthread_getspecific(tsdKey); 140*7c478bd9Sstevel@tonic-gate char *myself = "getError"; 141*7c478bd9Sstevel@tonic-gate 142*7c478bd9Sstevel@tonic-gate if (defErr == 0) { 143*7c478bd9Sstevel@tonic-gate if (message != 0) 144*7c478bd9Sstevel@tonic-gate *message = sdup(myself, T, "no TSD"); 145*7c478bd9Sstevel@tonic-gate return (NPL_TSDERR); 146*7c478bd9Sstevel@tonic-gate } 147*7c478bd9Sstevel@tonic-gate 148*7c478bd9Sstevel@tonic-gate if (message != 0) 149*7c478bd9Sstevel@tonic-gate *message = sdup(myself, T, defErr->message); 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gate return (defErr->error); 152*7c478bd9Sstevel@tonic-gate } 153*7c478bd9Sstevel@tonic-gate 154*7c478bd9Sstevel@tonic-gate void 155*7c478bd9Sstevel@tonic-gate clearError(void) { 156*7c478bd9Sstevel@tonic-gate __nis_deferred_error_t *defErr = pthread_getspecific(tsdKey); 157*7c478bd9Sstevel@tonic-gate 158*7c478bd9Sstevel@tonic-gate if (defErr != 0) { 159*7c478bd9Sstevel@tonic-gate sfree(defErr->message); 160*7c478bd9Sstevel@tonic-gate defErr->message = 0; 161*7c478bd9Sstevel@tonic-gate defErr->error = NPL_NOERROR; 162*7c478bd9Sstevel@tonic-gate } 163*7c478bd9Sstevel@tonic-gate } 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate void 166*7c478bd9Sstevel@tonic-gate logError(int priority) { 167*7c478bd9Sstevel@tonic-gate __nis_deferred_error_t *defErr = pthread_getspecific(tsdKey); 168*7c478bd9Sstevel@tonic-gate int msgtype; 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate if (defErr != 0) { 171*7c478bd9Sstevel@tonic-gate switch (defErr->error) { 172*7c478bd9Sstevel@tonic-gate case NPL_NOERROR: 173*7c478bd9Sstevel@tonic-gate msgtype = MSG_LASTMSG; 174*7c478bd9Sstevel@tonic-gate break; 175*7c478bd9Sstevel@tonic-gate case NPL_NOMEM: 176*7c478bd9Sstevel@tonic-gate msgtype = MSG_NOMEM; 177*7c478bd9Sstevel@tonic-gate break; 178*7c478bd9Sstevel@tonic-gate case NPL_TSDERR: 179*7c478bd9Sstevel@tonic-gate msgtype = MSG_TSDERR; 180*7c478bd9Sstevel@tonic-gate break; 181*7c478bd9Sstevel@tonic-gate case NPL_BERENCODE: 182*7c478bd9Sstevel@tonic-gate case NPL_BERDECODE: 183*7c478bd9Sstevel@tonic-gate msgtype = MSG_BER; 184*7c478bd9Sstevel@tonic-gate break; 185*7c478bd9Sstevel@tonic-gate default: 186*7c478bd9Sstevel@tonic-gate msgtype = MSG_LASTMSG; 187*7c478bd9Sstevel@tonic-gate break; 188*7c478bd9Sstevel@tonic-gate } 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate if (msgtype != MSG_LASTMSG) { 191*7c478bd9Sstevel@tonic-gate logmsg(msgtype, priority, defErr->message); 192*7c478bd9Sstevel@tonic-gate } 193*7c478bd9Sstevel@tonic-gate } 194*7c478bd9Sstevel@tonic-gate } 195*7c478bd9Sstevel@tonic-gate 196*7c478bd9Sstevel@tonic-gate /* 197*7c478bd9Sstevel@tonic-gate * Allocate zero-initialized memory of the specified 'size'. If the 198*7c478bd9Sstevel@tonic-gate * allocation fails, log a message and return NULL. Allocation of 199*7c478bd9Sstevel@tonic-gate * zero bytes is legal, and returns a NULL pointer. 200*7c478bd9Sstevel@tonic-gate */ 201*7c478bd9Sstevel@tonic-gate void * 202*7c478bd9Sstevel@tonic-gate am(char *msg, int size) { 203*7c478bd9Sstevel@tonic-gate void *p; 204*7c478bd9Sstevel@tonic-gate 205*7c478bd9Sstevel@tonic-gate if (size > 0) { 206*7c478bd9Sstevel@tonic-gate p = calloc(1, size); 207*7c478bd9Sstevel@tonic-gate if (p == 0) { 208*7c478bd9Sstevel@tonic-gate if (msg == 0) 209*7c478bd9Sstevel@tonic-gate msg = "<unknown>"; 210*7c478bd9Sstevel@tonic-gate logmsg(MSG_NOMEM, LOG_ERR, "%s: calloc(%d) => NULL\n", 211*7c478bd9Sstevel@tonic-gate msg, size); 212*7c478bd9Sstevel@tonic-gate return (0); 213*7c478bd9Sstevel@tonic-gate } 214*7c478bd9Sstevel@tonic-gate } else if (size == 0) { 215*7c478bd9Sstevel@tonic-gate p = 0; 216*7c478bd9Sstevel@tonic-gate } else { 217*7c478bd9Sstevel@tonic-gate if (msg == 0) 218*7c478bd9Sstevel@tonic-gate msg = "<unknown>"; 219*7c478bd9Sstevel@tonic-gate logmsg(MSG_MEMPARAM, LOG_INFO, "%s: size (%d) < 0\n", size); 220*7c478bd9Sstevel@tonic-gate exit(-1); 221*7c478bd9Sstevel@tonic-gate } 222*7c478bd9Sstevel@tonic-gate return (p); 223*7c478bd9Sstevel@tonic-gate } 224*7c478bd9Sstevel@tonic-gate 225*7c478bd9Sstevel@tonic-gate /* 226*7c478bd9Sstevel@tonic-gate * Return the length of a string, just like strlen(), but don't croak 227*7c478bd9Sstevel@tonic-gate * on a NULL pointer. 228*7c478bd9Sstevel@tonic-gate */ 229*7c478bd9Sstevel@tonic-gate int 230*7c478bd9Sstevel@tonic-gate slen(char *str) { 231*7c478bd9Sstevel@tonic-gate return ((str != 0) ? strlen(str) : 0); 232*7c478bd9Sstevel@tonic-gate } 233*7c478bd9Sstevel@tonic-gate 234*7c478bd9Sstevel@tonic-gate /* 235*7c478bd9Sstevel@tonic-gate * If allocate==0, return 'str'; othewise, duplicate the string just 236*7c478bd9Sstevel@tonic-gate * like strdup(), but don't die if 'str' is a NULL pointer. 237*7c478bd9Sstevel@tonic-gate */ 238*7c478bd9Sstevel@tonic-gate char * 239*7c478bd9Sstevel@tonic-gate sdup(char *msg, int allocate, char *str) { 240*7c478bd9Sstevel@tonic-gate char *s; 241*7c478bd9Sstevel@tonic-gate 242*7c478bd9Sstevel@tonic-gate if (!allocate) 243*7c478bd9Sstevel@tonic-gate return (str); 244*7c478bd9Sstevel@tonic-gate 245*7c478bd9Sstevel@tonic-gate if (str == 0) { 246*7c478bd9Sstevel@tonic-gate s = strdup(""); 247*7c478bd9Sstevel@tonic-gate } else { 248*7c478bd9Sstevel@tonic-gate s = strdup(str); 249*7c478bd9Sstevel@tonic-gate } 250*7c478bd9Sstevel@tonic-gate if (s == 0) { 251*7c478bd9Sstevel@tonic-gate logmsg(MSG_NOMEM, LOG_ERR, "%s: strdup(%d bytes) => NULL\n", 252*7c478bd9Sstevel@tonic-gate (msg != 0) ? msg : "<unknown>", slen(str)+1); 253*7c478bd9Sstevel@tonic-gate } 254*7c478bd9Sstevel@tonic-gate return (s); 255*7c478bd9Sstevel@tonic-gate } 256*7c478bd9Sstevel@tonic-gate 257*7c478bd9Sstevel@tonic-gate /* 258*7c478bd9Sstevel@tonic-gate * Concatenate strings like strcat(), but don't expire if passed a 259*7c478bd9Sstevel@tonic-gate * NULL pointer or two. If deallocate!=0, free() the input strings. 260*7c478bd9Sstevel@tonic-gate */ 261*7c478bd9Sstevel@tonic-gate char * 262*7c478bd9Sstevel@tonic-gate scat(char *msg, int deallocate, char *s1, char *s2) { 263*7c478bd9Sstevel@tonic-gate char *n; 264*7c478bd9Sstevel@tonic-gate int l1 = 0, l2 = 0; 265*7c478bd9Sstevel@tonic-gate 266*7c478bd9Sstevel@tonic-gate if (s1 == 0) { 267*7c478bd9Sstevel@tonic-gate n = sdup(msg, T, s2); 268*7c478bd9Sstevel@tonic-gate if (deallocate) 269*7c478bd9Sstevel@tonic-gate sfree(s2); 270*7c478bd9Sstevel@tonic-gate return (n); 271*7c478bd9Sstevel@tonic-gate } else if (s2 == 0) { 272*7c478bd9Sstevel@tonic-gate n = sdup(msg, T, s1); 273*7c478bd9Sstevel@tonic-gate if (deallocate) 274*7c478bd9Sstevel@tonic-gate free(s1); 275*7c478bd9Sstevel@tonic-gate return (n); 276*7c478bd9Sstevel@tonic-gate } 277*7c478bd9Sstevel@tonic-gate 278*7c478bd9Sstevel@tonic-gate l1 = strlen(s1); 279*7c478bd9Sstevel@tonic-gate l2 = strlen(s2); 280*7c478bd9Sstevel@tonic-gate 281*7c478bd9Sstevel@tonic-gate n = malloc(l1+l2+1); 282*7c478bd9Sstevel@tonic-gate if (n != 0) { 283*7c478bd9Sstevel@tonic-gate memcpy(n, s1, l1); 284*7c478bd9Sstevel@tonic-gate memcpy(&n[l1], s2, l2); 285*7c478bd9Sstevel@tonic-gate n[l1+l2] = '\0'; 286*7c478bd9Sstevel@tonic-gate } else { 287*7c478bd9Sstevel@tonic-gate logmsg(MSG_NOMEM, LOG_ERR, "%s: malloc(%d) => NULL\n", 288*7c478bd9Sstevel@tonic-gate (msg != 0) ? msg : "<unknown>", l1+l2+1); 289*7c478bd9Sstevel@tonic-gate } 290*7c478bd9Sstevel@tonic-gate 291*7c478bd9Sstevel@tonic-gate if (deallocate) { 292*7c478bd9Sstevel@tonic-gate free(s1); 293*7c478bd9Sstevel@tonic-gate free(s2); 294*7c478bd9Sstevel@tonic-gate } 295*7c478bd9Sstevel@tonic-gate 296*7c478bd9Sstevel@tonic-gate return (n); 297*7c478bd9Sstevel@tonic-gate } 298*7c478bd9Sstevel@tonic-gate 299*7c478bd9Sstevel@tonic-gate /* For debugging */ 300*7c478bd9Sstevel@tonic-gate static void *PTR = 0; 301*7c478bd9Sstevel@tonic-gate 302*7c478bd9Sstevel@tonic-gate /* 303*7c478bd9Sstevel@tonic-gate * Counters for memory errors. Note that we don't protect access, 304*7c478bd9Sstevel@tonic-gate * so the values aren't entirely reliable in an MT application. 305*7c478bd9Sstevel@tonic-gate */ 306*7c478bd9Sstevel@tonic-gate ulong_t numMisaligned = 0; 307*7c478bd9Sstevel@tonic-gate ulong_t numNotActive = 0; 308*7c478bd9Sstevel@tonic-gate 309*7c478bd9Sstevel@tonic-gate /* free() the input, but don't pass away if it's NULL */ 310*7c478bd9Sstevel@tonic-gate void 311*7c478bd9Sstevel@tonic-gate sfree(void *ptr) { 312*7c478bd9Sstevel@tonic-gate 313*7c478bd9Sstevel@tonic-gate /* NULL pointer OK */ 314*7c478bd9Sstevel@tonic-gate if (ptr == 0) 315*7c478bd9Sstevel@tonic-gate return; 316*7c478bd9Sstevel@tonic-gate 317*7c478bd9Sstevel@tonic-gate /* 318*7c478bd9Sstevel@tonic-gate * For use in the debugger, when we need to detect free of a 319*7c478bd9Sstevel@tonic-gate * certain address. 320*7c478bd9Sstevel@tonic-gate */ 321*7c478bd9Sstevel@tonic-gate if (ptr == PTR) 322*7c478bd9Sstevel@tonic-gate abort(); 323*7c478bd9Sstevel@tonic-gate 324*7c478bd9Sstevel@tonic-gate /* 325*7c478bd9Sstevel@tonic-gate * All addresses returned by malloc() and friends are "suitably 326*7c478bd9Sstevel@tonic-gate * aligned for any use", so they should fall on eight-byte boundaries. 327*7c478bd9Sstevel@tonic-gate */ 328*7c478bd9Sstevel@tonic-gate if (((unsigned long)ptr % 8) != 0) { 329*7c478bd9Sstevel@tonic-gate numMisaligned++; 330*7c478bd9Sstevel@tonic-gate return; 331*7c478bd9Sstevel@tonic-gate } 332*7c478bd9Sstevel@tonic-gate 333*7c478bd9Sstevel@tonic-gate #ifdef NISDB_LDAP_DEBUG 334*7c478bd9Sstevel@tonic-gate /* 335*7c478bd9Sstevel@tonic-gate * Malloc:ed memory should have the length (four bytes), starting 336*7c478bd9Sstevel@tonic-gate * eight bytes before the block, and with the least-significant 337*7c478bd9Sstevel@tonic-gate * bit set. 338*7c478bd9Sstevel@tonic-gate */ 339*7c478bd9Sstevel@tonic-gate if ((((uint_t *)ptr)[-2] & 0x1) == 0) { 340*7c478bd9Sstevel@tonic-gate numNotActive++; 341*7c478bd9Sstevel@tonic-gate return; 342*7c478bd9Sstevel@tonic-gate } 343*7c478bd9Sstevel@tonic-gate #endif /* NISDB_LDAP_DEBUG */ 344*7c478bd9Sstevel@tonic-gate 345*7c478bd9Sstevel@tonic-gate /* Finally, we believe it's OK to free() the pointer */ 346*7c478bd9Sstevel@tonic-gate free(ptr); 347*7c478bd9Sstevel@tonic-gate } 348*7c478bd9Sstevel@tonic-gate 349*7c478bd9Sstevel@tonic-gate /* 350*7c478bd9Sstevel@tonic-gate * If a __nis_single_value_t represents a string, the length count may or may 351*7c478bd9Sstevel@tonic-gate * not include a concluding NUL. Hence this function, which returns the last 352*7c478bd9Sstevel@tonic-gate * non-NUL character of the value. 353*7c478bd9Sstevel@tonic-gate */ 354*7c478bd9Sstevel@tonic-gate char 355*7c478bd9Sstevel@tonic-gate lastChar(__nis_single_value_t *v) { 356*7c478bd9Sstevel@tonic-gate char *s; 357*7c478bd9Sstevel@tonic-gate 358*7c478bd9Sstevel@tonic-gate if (v == 0 || v->value == 0 || v->length < 2) 359*7c478bd9Sstevel@tonic-gate return ('\0'); 360*7c478bd9Sstevel@tonic-gate 361*7c478bd9Sstevel@tonic-gate s = v->value; 362*7c478bd9Sstevel@tonic-gate if (s[v->length - 1] != '\0') 363*7c478bd9Sstevel@tonic-gate return (s[v->length - 1]); 364*7c478bd9Sstevel@tonic-gate else 365*7c478bd9Sstevel@tonic-gate return (s[v->length - 2]); 366*7c478bd9Sstevel@tonic-gate } 367*7c478bd9Sstevel@tonic-gate 368*7c478bd9Sstevel@tonic-gate void * 369*7c478bd9Sstevel@tonic-gate appendString2SingleVal(char *str, __nis_single_value_t *v, int *newLen) { 370*7c478bd9Sstevel@tonic-gate void *s; 371*7c478bd9Sstevel@tonic-gate int l, nl; 372*7c478bd9Sstevel@tonic-gate char *myself = "appendString2SingleVal"; 373*7c478bd9Sstevel@tonic-gate 374*7c478bd9Sstevel@tonic-gate if (v == 0 || v->length < 0) 375*7c478bd9Sstevel@tonic-gate return (0); 376*7c478bd9Sstevel@tonic-gate 377*7c478bd9Sstevel@tonic-gate /* 378*7c478bd9Sstevel@tonic-gate * If 'str' is NULL or empty, just return NULL so that the caller 379*7c478bd9Sstevel@tonic-gate * does nothing. 380*7c478bd9Sstevel@tonic-gate */ 381*7c478bd9Sstevel@tonic-gate l = slen(str); 382*7c478bd9Sstevel@tonic-gate if (l <= 0) 383*7c478bd9Sstevel@tonic-gate return (0); 384*7c478bd9Sstevel@tonic-gate 385*7c478bd9Sstevel@tonic-gate s = am(myself, (nl = l + v->length) + 1); 386*7c478bd9Sstevel@tonic-gate if (s == 0) { 387*7c478bd9Sstevel@tonic-gate /* Caller does nothing; let's hope for the best... */ 388*7c478bd9Sstevel@tonic-gate return (0); 389*7c478bd9Sstevel@tonic-gate } 390*7c478bd9Sstevel@tonic-gate 391*7c478bd9Sstevel@tonic-gate if (v->value != 0) 392*7c478bd9Sstevel@tonic-gate memcpy(s, v->value, v->length); 393*7c478bd9Sstevel@tonic-gate 394*7c478bd9Sstevel@tonic-gate memcpy(&(((char *)s)[v->length]), str, l); 395*7c478bd9Sstevel@tonic-gate 396*7c478bd9Sstevel@tonic-gate if (newLen != 0) 397*7c478bd9Sstevel@tonic-gate *newLen = nl; 398*7c478bd9Sstevel@tonic-gate 399*7c478bd9Sstevel@tonic-gate return (s); 400*7c478bd9Sstevel@tonic-gate } 401*7c478bd9Sstevel@tonic-gate 402*7c478bd9Sstevel@tonic-gate 403*7c478bd9Sstevel@tonic-gate /* 404*7c478bd9Sstevel@tonic-gate * Do the equivalent of a strcmp() between a string and a string-valued 405*7c478bd9Sstevel@tonic-gate * __nis_single_value_t. 406*7c478bd9Sstevel@tonic-gate */ 407*7c478bd9Sstevel@tonic-gate int 408*7c478bd9Sstevel@tonic-gate scmp(char *s, __nis_single_value_t *v) { 409*7c478bd9Sstevel@tonic-gate 410*7c478bd9Sstevel@tonic-gate if (s == 0) 411*7c478bd9Sstevel@tonic-gate return (1); 412*7c478bd9Sstevel@tonic-gate else if (v == 0 || v->value == 0 || v->length <= 0) 413*7c478bd9Sstevel@tonic-gate return (-1); 414*7c478bd9Sstevel@tonic-gate 415*7c478bd9Sstevel@tonic-gate return (strncmp(s, v->value, v->length)); 416*7c478bd9Sstevel@tonic-gate } 417*7c478bd9Sstevel@tonic-gate 418*7c478bd9Sstevel@tonic-gate /* 419*7c478bd9Sstevel@tonic-gate * Do the equivalent of a strcasecmp() between a string and a string-valued 420*7c478bd9Sstevel@tonic-gate * __nis_single_value_t. 421*7c478bd9Sstevel@tonic-gate */ 422*7c478bd9Sstevel@tonic-gate int 423*7c478bd9Sstevel@tonic-gate scasecmp(char *s, __nis_single_value_t *v) { 424*7c478bd9Sstevel@tonic-gate 425*7c478bd9Sstevel@tonic-gate if (s == 0) 426*7c478bd9Sstevel@tonic-gate return (1); 427*7c478bd9Sstevel@tonic-gate else if (v == 0 || v->value == 0 || v->length <= 0) 428*7c478bd9Sstevel@tonic-gate return (-1); 429*7c478bd9Sstevel@tonic-gate 430*7c478bd9Sstevel@tonic-gate return (strncasecmp(s, v->value, v->length)); 431*7c478bd9Sstevel@tonic-gate } 432*7c478bd9Sstevel@tonic-gate 433*7c478bd9Sstevel@tonic-gate #define STDBUFSIZE 81 434*7c478bd9Sstevel@tonic-gate 435*7c478bd9Sstevel@tonic-gate /* 436*7c478bd9Sstevel@tonic-gate * vsprintf the 'fmt' and 'ap' to a buffer, then concatenate the 437*7c478bd9Sstevel@tonic-gate * result to '*buf'. 438*7c478bd9Sstevel@tonic-gate */ 439*7c478bd9Sstevel@tonic-gate int 440*7c478bd9Sstevel@tonic-gate vp2buf(char *msg, char **buf, int buflen, char *fmt, va_list ap) { 441*7c478bd9Sstevel@tonic-gate char *newbuf = am(msg, STDBUFSIZE); 442*7c478bd9Sstevel@tonic-gate int size = 0; 443*7c478bd9Sstevel@tonic-gate 444*7c478bd9Sstevel@tonic-gate if (newbuf == 0) 445*7c478bd9Sstevel@tonic-gate return (0); 446*7c478bd9Sstevel@tonic-gate 447*7c478bd9Sstevel@tonic-gate if (buf == 0 || buflen < 0 || fmt == 0) { 448*7c478bd9Sstevel@tonic-gate free(newbuf); 449*7c478bd9Sstevel@tonic-gate return (0); 450*7c478bd9Sstevel@tonic-gate } 451*7c478bd9Sstevel@tonic-gate 452*7c478bd9Sstevel@tonic-gate /* Find out how large the new buffer needs to be */ 453*7c478bd9Sstevel@tonic-gate size = vsnprintf(newbuf, STDBUFSIZE, fmt, ap); 454*7c478bd9Sstevel@tonic-gate 455*7c478bd9Sstevel@tonic-gate if (size > STDBUFSIZE) { 456*7c478bd9Sstevel@tonic-gate free(newbuf); 457*7c478bd9Sstevel@tonic-gate newbuf = am(msg, size+1); 458*7c478bd9Sstevel@tonic-gate if (newbuf == 0) 459*7c478bd9Sstevel@tonic-gate return (0); 460*7c478bd9Sstevel@tonic-gate size = vsnprintf(newbuf, size+1, fmt, ap); 461*7c478bd9Sstevel@tonic-gate } 462*7c478bd9Sstevel@tonic-gate 463*7c478bd9Sstevel@tonic-gate *buf = scat(msg, T, *buf, newbuf); 464*7c478bd9Sstevel@tonic-gate /* Don't count the NUL. This enables us to concatenate correctly */ 465*7c478bd9Sstevel@tonic-gate buflen += size; 466*7c478bd9Sstevel@tonic-gate 467*7c478bd9Sstevel@tonic-gate return (buflen); 468*7c478bd9Sstevel@tonic-gate } 469*7c478bd9Sstevel@tonic-gate 470*7c478bd9Sstevel@tonic-gate /* Generic print buffer */ 471*7c478bd9Sstevel@tonic-gate __nis_buffer_t pb = {0, 0}; 472*7c478bd9Sstevel@tonic-gate 473*7c478bd9Sstevel@tonic-gate /* sprintf to the generic __nis_buffer_t */ 474*7c478bd9Sstevel@tonic-gate void 475*7c478bd9Sstevel@tonic-gate p2buf(char *msg, char *fmt, ...) { 476*7c478bd9Sstevel@tonic-gate va_list ap; 477*7c478bd9Sstevel@tonic-gate 478*7c478bd9Sstevel@tonic-gate va_start(ap, fmt); 479*7c478bd9Sstevel@tonic-gate pb.len = vp2buf(msg, &pb.buf, pb.len, fmt, ap); 480*7c478bd9Sstevel@tonic-gate va_end(ap); 481*7c478bd9Sstevel@tonic-gate } 482*7c478bd9Sstevel@tonic-gate 483*7c478bd9Sstevel@tonic-gate /* sprintf to the specified __nis_buffer_t */ 484*7c478bd9Sstevel@tonic-gate void 485*7c478bd9Sstevel@tonic-gate bp2buf(char *msg, __nis_buffer_t *b, char *fmt, ...) { 486*7c478bd9Sstevel@tonic-gate va_list ap; 487*7c478bd9Sstevel@tonic-gate 488*7c478bd9Sstevel@tonic-gate va_start(ap, fmt); 489*7c478bd9Sstevel@tonic-gate b->len = vp2buf(msg, &b->buf, b->len, fmt, ap); 490*7c478bd9Sstevel@tonic-gate va_end(ap); 491*7c478bd9Sstevel@tonic-gate } 492*7c478bd9Sstevel@tonic-gate 493*7c478bd9Sstevel@tonic-gate /* Copy 'buf' to the specified __nis_buffer_t */ 494*7c478bd9Sstevel@tonic-gate void 495*7c478bd9Sstevel@tonic-gate bc2buf(char *msg, void *buf, int len, __nis_buffer_t *b) { 496*7c478bd9Sstevel@tonic-gate void *new; 497*7c478bd9Sstevel@tonic-gate 498*7c478bd9Sstevel@tonic-gate /* 499*7c478bd9Sstevel@tonic-gate * Make buffer one byte larger than the lenghts indicate. This 500*7c478bd9Sstevel@tonic-gate * gives us room to append a NUL, so that we can mix string and 501*7c478bd9Sstevel@tonic-gate * non-string copies into the buffer, and still end up with 502*7c478bd9Sstevel@tonic-gate * something that can be sent to printf(), strcat(), etc. 503*7c478bd9Sstevel@tonic-gate */ 504*7c478bd9Sstevel@tonic-gate new = realloc(b->buf, b->len+len+1); 505*7c478bd9Sstevel@tonic-gate if (new != 0) { 506*7c478bd9Sstevel@tonic-gate b->buf = new; 507*7c478bd9Sstevel@tonic-gate memcpy(&(b->buf[b->len]), buf, len); 508*7c478bd9Sstevel@tonic-gate b->len += len; 509*7c478bd9Sstevel@tonic-gate /* Put a NUL at the end, just in case we printf() */ 510*7c478bd9Sstevel@tonic-gate if (b->len > 0 && b->buf[b->len-1] != '\0') 511*7c478bd9Sstevel@tonic-gate b->buf[b->len] = '\0'; 512*7c478bd9Sstevel@tonic-gate } else { 513*7c478bd9Sstevel@tonic-gate logmsg(MSG_NOMEM, LOG_ERR, "%s: realloc(%d) => NULL\n", 514*7c478bd9Sstevel@tonic-gate (msg != 0) ? msg : "<unknown", b->len+len); 515*7c478bd9Sstevel@tonic-gate } 516*7c478bd9Sstevel@tonic-gate } 517*7c478bd9Sstevel@tonic-gate 518*7c478bd9Sstevel@tonic-gate /* Like bc2buf(), but remove any trailing NUL bytes */ 519*7c478bd9Sstevel@tonic-gate void 520*7c478bd9Sstevel@tonic-gate sbc2buf(char *msg, void *buf, int len, __nis_buffer_t *b) { 521*7c478bd9Sstevel@tonic-gate if (buf == 0 || len <= 0 || b == 0) 522*7c478bd9Sstevel@tonic-gate return; 523*7c478bd9Sstevel@tonic-gate /* Snip off trailing NULs */ 524*7c478bd9Sstevel@tonic-gate while (len > 0 && ((char *)buf)[len-1] == '\0') 525*7c478bd9Sstevel@tonic-gate len--; 526*7c478bd9Sstevel@tonic-gate if (len <= 0) 527*7c478bd9Sstevel@tonic-gate return; 528*7c478bd9Sstevel@tonic-gate bc2buf(msg, buf, len, b); 529*7c478bd9Sstevel@tonic-gate } 530*7c478bd9Sstevel@tonic-gate 531*7c478bd9Sstevel@tonic-gate /* Copy 'buf' to the generic __nis_buffer_t */ 532*7c478bd9Sstevel@tonic-gate void 533*7c478bd9Sstevel@tonic-gate c2buf(char *msg, void *buf, int len) { 534*7c478bd9Sstevel@tonic-gate bc2buf(msg, buf, len, &pb); 535*7c478bd9Sstevel@tonic-gate } 536*7c478bd9Sstevel@tonic-gate 537*7c478bd9Sstevel@tonic-gate /* Like c2buf(), but remove trailing NUL bytes */ 538*7c478bd9Sstevel@tonic-gate void 539*7c478bd9Sstevel@tonic-gate sc2buf(char *msg, void *buf, int len) { 540*7c478bd9Sstevel@tonic-gate sbc2buf(msg, buf, len, &pb); 541*7c478bd9Sstevel@tonic-gate } 542*7c478bd9Sstevel@tonic-gate 543*7c478bd9Sstevel@tonic-gate /* How many times we try write(2) if it fails */ 544*7c478bd9Sstevel@tonic-gate #define MAXTRY 10 545*7c478bd9Sstevel@tonic-gate 546*7c478bd9Sstevel@tonic-gate /* Output the generic __nis_buffer_t to stdout */ 547*7c478bd9Sstevel@tonic-gate void 548*7c478bd9Sstevel@tonic-gate printbuf(void) { 549*7c478bd9Sstevel@tonic-gate int maxtry = MAXTRY, len = pb.len; 550*7c478bd9Sstevel@tonic-gate 551*7c478bd9Sstevel@tonic-gate if (pb.buf != 0) { 552*7c478bd9Sstevel@tonic-gate int tmp; 553*7c478bd9Sstevel@tonic-gate 554*7c478bd9Sstevel@tonic-gate while (len > 0 && maxtry > 0) { 555*7c478bd9Sstevel@tonic-gate tmp = write(1, pb.buf, len); 556*7c478bd9Sstevel@tonic-gate if (tmp < 0) 557*7c478bd9Sstevel@tonic-gate break; 558*7c478bd9Sstevel@tonic-gate len -= tmp; 559*7c478bd9Sstevel@tonic-gate if (tmp > 0) 560*7c478bd9Sstevel@tonic-gate maxtry = MAXTRY; 561*7c478bd9Sstevel@tonic-gate else 562*7c478bd9Sstevel@tonic-gate maxtry--; 563*7c478bd9Sstevel@tonic-gate } 564*7c478bd9Sstevel@tonic-gate free(pb.buf); 565*7c478bd9Sstevel@tonic-gate pb.buf = 0; 566*7c478bd9Sstevel@tonic-gate } 567*7c478bd9Sstevel@tonic-gate pb.len = 0; 568*7c478bd9Sstevel@tonic-gate } 569*7c478bd9Sstevel@tonic-gate 570*7c478bd9Sstevel@tonic-gate void * 571*7c478bd9Sstevel@tonic-gate extendArray(void *array, int newsize) { 572*7c478bd9Sstevel@tonic-gate void *new = realloc(array, newsize); 573*7c478bd9Sstevel@tonic-gate if (new == 0) 574*7c478bd9Sstevel@tonic-gate sfree(array); 575*7c478bd9Sstevel@tonic-gate return (new); 576*7c478bd9Sstevel@tonic-gate } 577*7c478bd9Sstevel@tonic-gate 578*7c478bd9Sstevel@tonic-gate /* 579*7c478bd9Sstevel@tonic-gate * Determine if the given string is an IP address (IPv4 or IPv6). 580*7c478bd9Sstevel@tonic-gate * If so, it converts it to the format as required by rfc2307bis 581*7c478bd9Sstevel@tonic-gate * and *newaddr will point to the new Address. 582*7c478bd9Sstevel@tonic-gate * 583*7c478bd9Sstevel@tonic-gate * Returns -2 : error 584*7c478bd9Sstevel@tonic-gate * -1 : not an IP address 585*7c478bd9Sstevel@tonic-gate * 0 : IP address not supported by rfc2307bis 586*7c478bd9Sstevel@tonic-gate * AF_INET : IPv4 587*7c478bd9Sstevel@tonic-gate * AF_INET6 : IPv6 588*7c478bd9Sstevel@tonic-gate */ 589*7c478bd9Sstevel@tonic-gate int 590*7c478bd9Sstevel@tonic-gate checkIPaddress(char *addr, int len, char **newaddr) { 591*7c478bd9Sstevel@tonic-gate ipaddr_t addr_ipv4; 592*7c478bd9Sstevel@tonic-gate in6_addr_t addr_ipv6; 593*7c478bd9Sstevel@tonic-gate char *buffer; 594*7c478bd9Sstevel@tonic-gate int s, e; 595*7c478bd9Sstevel@tonic-gate char *myself = "checkIPaddress"; 596*7c478bd9Sstevel@tonic-gate 597*7c478bd9Sstevel@tonic-gate /* skip leading whitespaces */ 598*7c478bd9Sstevel@tonic-gate for (s = 0; (s < len) && (addr[s] == ' ' || addr[s] == '\t'); s++); 599*7c478bd9Sstevel@tonic-gate if (s >= len) 600*7c478bd9Sstevel@tonic-gate return (-1); 601*7c478bd9Sstevel@tonic-gate 602*7c478bd9Sstevel@tonic-gate /* skip trailing whitespaces */ 603*7c478bd9Sstevel@tonic-gate for (e = len - 1; (e > s) && (addr[e] == ' ' || addr[e] == '\t'); e--); 604*7c478bd9Sstevel@tonic-gate if (s == e) 605*7c478bd9Sstevel@tonic-gate return (-1); 606*7c478bd9Sstevel@tonic-gate 607*7c478bd9Sstevel@tonic-gate /* adjust len */ 608*7c478bd9Sstevel@tonic-gate len = e - s + 1; 609*7c478bd9Sstevel@tonic-gate 610*7c478bd9Sstevel@tonic-gate if ((buffer = am(myself, len + 1)) == 0) 611*7c478bd9Sstevel@tonic-gate return (-2); 612*7c478bd9Sstevel@tonic-gate (void) memcpy(buffer, addr + s, len); 613*7c478bd9Sstevel@tonic-gate 614*7c478bd9Sstevel@tonic-gate if (inet_pton(AF_INET6, buffer, &addr_ipv6) == 1) { 615*7c478bd9Sstevel@tonic-gate sfree(buffer); 616*7c478bd9Sstevel@tonic-gate /* 617*7c478bd9Sstevel@tonic-gate * IPv4-compatible IPv6 address and IPv4-mapped 618*7c478bd9Sstevel@tonic-gate * IPv6 addresses not allowed by rfc2307bis 619*7c478bd9Sstevel@tonic-gate */ 620*7c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_V4COMPAT(&addr_ipv6)) 621*7c478bd9Sstevel@tonic-gate return (0); 622*7c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(&addr_ipv6)) 623*7c478bd9Sstevel@tonic-gate return (0); 624*7c478bd9Sstevel@tonic-gate if (newaddr == 0) 625*7c478bd9Sstevel@tonic-gate return (AF_INET6); 626*7c478bd9Sstevel@tonic-gate if ((*newaddr = am(myself, INET6_ADDRSTRLEN)) == 0) 627*7c478bd9Sstevel@tonic-gate return (-2); 628*7c478bd9Sstevel@tonic-gate if (inet_ntop(AF_INET6, &addr_ipv6, *newaddr, INET6_ADDRSTRLEN)) 629*7c478bd9Sstevel@tonic-gate return (AF_INET6); 630*7c478bd9Sstevel@tonic-gate sfree(*newaddr); 631*7c478bd9Sstevel@tonic-gate return (-2); 632*7c478bd9Sstevel@tonic-gate } 633*7c478bd9Sstevel@tonic-gate 634*7c478bd9Sstevel@tonic-gate if (inet_pton(AF_INET, buffer, &addr_ipv4) == 1) { 635*7c478bd9Sstevel@tonic-gate sfree(buffer); 636*7c478bd9Sstevel@tonic-gate if (newaddr == 0) 637*7c478bd9Sstevel@tonic-gate return (AF_INET); 638*7c478bd9Sstevel@tonic-gate if ((*newaddr = am(myself, INET_ADDRSTRLEN)) == 0) 639*7c478bd9Sstevel@tonic-gate return (-2); 640*7c478bd9Sstevel@tonic-gate if (inet_ntop(AF_INET, &addr_ipv4, *newaddr, INET_ADDRSTRLEN)) 641*7c478bd9Sstevel@tonic-gate return (AF_INET); 642*7c478bd9Sstevel@tonic-gate sfree(*newaddr); 643*7c478bd9Sstevel@tonic-gate return (-2); 644*7c478bd9Sstevel@tonic-gate } 645*7c478bd9Sstevel@tonic-gate 646*7c478bd9Sstevel@tonic-gate sfree(buffer); 647*7c478bd9Sstevel@tonic-gate return (-1); 648*7c478bd9Sstevel@tonic-gate } 649*7c478bd9Sstevel@tonic-gate 650*7c478bd9Sstevel@tonic-gate int 651*7c478bd9Sstevel@tonic-gate sstrncmp(const char *s1, const char *s2, int n) { 652*7c478bd9Sstevel@tonic-gate if (s1 == 0 && s2 == 0) 653*7c478bd9Sstevel@tonic-gate return (0); 654*7c478bd9Sstevel@tonic-gate 655*7c478bd9Sstevel@tonic-gate if (s1 == 0) 656*7c478bd9Sstevel@tonic-gate return (1); 657*7c478bd9Sstevel@tonic-gate 658*7c478bd9Sstevel@tonic-gate if (s2 == 0) 659*7c478bd9Sstevel@tonic-gate return (-1); 660*7c478bd9Sstevel@tonic-gate 661*7c478bd9Sstevel@tonic-gate return (strncmp(s1, s2, n)); 662*7c478bd9Sstevel@tonic-gate } 663*7c478bd9Sstevel@tonic-gate 664*7c478bd9Sstevel@tonic-gate /* 665*7c478bd9Sstevel@tonic-gate * Does the following: 666*7c478bd9Sstevel@tonic-gate * - Trims leading and trailing whitespaces 667*7c478bd9Sstevel@tonic-gate * - Collapses two or more whitespaces into one space 668*7c478bd9Sstevel@tonic-gate * - Converts all whitespaces into spaces 669*7c478bd9Sstevel@tonic-gate * - At entrance, *len contains length of str 670*7c478bd9Sstevel@tonic-gate * - At exit, *len will contain length of the return string 671*7c478bd9Sstevel@tonic-gate * - In case of mem alloc failure, *len should be ignored 672*7c478bd9Sstevel@tonic-gate */ 673*7c478bd9Sstevel@tonic-gate char * 674*7c478bd9Sstevel@tonic-gate trimWhiteSpaces(char *str, int *len, int deallocate) { 675*7c478bd9Sstevel@tonic-gate char *ostr; 676*7c478bd9Sstevel@tonic-gate int olen = 0; 677*7c478bd9Sstevel@tonic-gate int first = 1, i; 678*7c478bd9Sstevel@tonic-gate char *myself = "trimWhiteSpaces"; 679*7c478bd9Sstevel@tonic-gate 680*7c478bd9Sstevel@tonic-gate if ((ostr = am(myself, *len + 1)) == 0) { 681*7c478bd9Sstevel@tonic-gate if (deallocate) 682*7c478bd9Sstevel@tonic-gate sfree(str); 683*7c478bd9Sstevel@tonic-gate *len = 0; 684*7c478bd9Sstevel@tonic-gate return (0); 685*7c478bd9Sstevel@tonic-gate } 686*7c478bd9Sstevel@tonic-gate 687*7c478bd9Sstevel@tonic-gate /* Skip leading whitespaces */ 688*7c478bd9Sstevel@tonic-gate for (i = 0; i < *len && (str[i] == ' ' || str[i] == '\t'); i++); 689*7c478bd9Sstevel@tonic-gate 690*7c478bd9Sstevel@tonic-gate /* Collapse multiple whitespaces into one */ 691*7c478bd9Sstevel@tonic-gate for (; i < *len; i++) { 692*7c478bd9Sstevel@tonic-gate if (str[i] == ' ' || str[i] == '\t') { 693*7c478bd9Sstevel@tonic-gate if (first) { 694*7c478bd9Sstevel@tonic-gate first = 0; 695*7c478bd9Sstevel@tonic-gate ostr[olen++] = ' '; 696*7c478bd9Sstevel@tonic-gate } 697*7c478bd9Sstevel@tonic-gate continue; 698*7c478bd9Sstevel@tonic-gate } 699*7c478bd9Sstevel@tonic-gate first = 1; 700*7c478bd9Sstevel@tonic-gate ostr[olen++] = str[i]; 701*7c478bd9Sstevel@tonic-gate } 702*7c478bd9Sstevel@tonic-gate 703*7c478bd9Sstevel@tonic-gate /* Handle the trailing whitespace if any */ 704*7c478bd9Sstevel@tonic-gate if (olen && ostr[olen - 1] == ' ') { 705*7c478bd9Sstevel@tonic-gate olen--; 706*7c478bd9Sstevel@tonic-gate ostr[olen] = 0; 707*7c478bd9Sstevel@tonic-gate } 708*7c478bd9Sstevel@tonic-gate 709*7c478bd9Sstevel@tonic-gate if (deallocate) 710*7c478bd9Sstevel@tonic-gate sfree(str); 711*7c478bd9Sstevel@tonic-gate 712*7c478bd9Sstevel@tonic-gate *len = olen; 713*7c478bd9Sstevel@tonic-gate return (ostr); 714*7c478bd9Sstevel@tonic-gate } 715*7c478bd9Sstevel@tonic-gate 716*7c478bd9Sstevel@tonic-gate /* 717*7c478bd9Sstevel@tonic-gate * Escapes special characters in DN using the list from RFC 2253 718*7c478bd9Sstevel@tonic-gate */ 719*7c478bd9Sstevel@tonic-gate int 720*7c478bd9Sstevel@tonic-gate escapeSpecialChars(__nis_value_t *val) { 721*7c478bd9Sstevel@tonic-gate int i, j, k, count; 722*7c478bd9Sstevel@tonic-gate char *newval, *s; 723*7c478bd9Sstevel@tonic-gate char *myself = "escapeSpecialChars"; 724*7c478bd9Sstevel@tonic-gate 725*7c478bd9Sstevel@tonic-gate /* Assume val is always non NULL */ 726*7c478bd9Sstevel@tonic-gate 727*7c478bd9Sstevel@tonic-gate for (i = 0; i < val->numVals; i++) { 728*7c478bd9Sstevel@tonic-gate /* 729*7c478bd9Sstevel@tonic-gate * Count the special characters in value to determine 730*7c478bd9Sstevel@tonic-gate * the length for the new value 731*7c478bd9Sstevel@tonic-gate */ 732*7c478bd9Sstevel@tonic-gate s = val->val[i].value; 733*7c478bd9Sstevel@tonic-gate for (j = 0, count = 0; j < val->val[i].length; j++, s++) { 734*7c478bd9Sstevel@tonic-gate if (*s == '#' || *s == ',' || *s == '+' || *s == '"' || 735*7c478bd9Sstevel@tonic-gate *s == '\\' || *s == '<' || *s == '>' || *s == ';') 736*7c478bd9Sstevel@tonic-gate count++; 737*7c478bd9Sstevel@tonic-gate } 738*7c478bd9Sstevel@tonic-gate if (count == 0) 739*7c478bd9Sstevel@tonic-gate continue; 740*7c478bd9Sstevel@tonic-gate 741*7c478bd9Sstevel@tonic-gate if ((newval = am(myself, val->val[i].length + count + 1)) == 0) 742*7c478bd9Sstevel@tonic-gate return (-1); 743*7c478bd9Sstevel@tonic-gate 744*7c478bd9Sstevel@tonic-gate /* Escape the special characters using '\\' */ 745*7c478bd9Sstevel@tonic-gate s = val->val[i].value; 746*7c478bd9Sstevel@tonic-gate for (j = 0, k = 0; j < val->val[i].length; j++, k++, s++) { 747*7c478bd9Sstevel@tonic-gate if (*s == '#' || *s == ',' || *s == '+' || *s == '"' || 748*7c478bd9Sstevel@tonic-gate *s == '\\' || *s == '<' || *s == '>' || *s == ';') 749*7c478bd9Sstevel@tonic-gate newval[k++] = '\\'; 750*7c478bd9Sstevel@tonic-gate newval[k] = *s; 751*7c478bd9Sstevel@tonic-gate } 752*7c478bd9Sstevel@tonic-gate 753*7c478bd9Sstevel@tonic-gate sfree(val->val[i].value); 754*7c478bd9Sstevel@tonic-gate val->val[i].value = newval; 755*7c478bd9Sstevel@tonic-gate val->val[i].length += count; 756*7c478bd9Sstevel@tonic-gate } 757*7c478bd9Sstevel@tonic-gate 758*7c478bd9Sstevel@tonic-gate return (1); 759*7c478bd9Sstevel@tonic-gate } 760*7c478bd9Sstevel@tonic-gate 761*7c478bd9Sstevel@tonic-gate /* 762*7c478bd9Sstevel@tonic-gate * Remove escape characters from DN returned by LDAP server 763*7c478bd9Sstevel@tonic-gate */ 764*7c478bd9Sstevel@tonic-gate void 765*7c478bd9Sstevel@tonic-gate removeEscapeChars(__nis_value_t *val) { 766*7c478bd9Sstevel@tonic-gate int i; 767*7c478bd9Sstevel@tonic-gate char *s, *d, *end; 768*7c478bd9Sstevel@tonic-gate 769*7c478bd9Sstevel@tonic-gate 770*7c478bd9Sstevel@tonic-gate for (i = 0; i < val->numVals; i++) { 771*7c478bd9Sstevel@tonic-gate s = val->val[i].value; 772*7c478bd9Sstevel@tonic-gate end = s + val->val[i].length; 773*7c478bd9Sstevel@tonic-gate 774*7c478bd9Sstevel@tonic-gate /* 775*7c478bd9Sstevel@tonic-gate * This function is called frequently and for most entries 776*7c478bd9Sstevel@tonic-gate * there will be no escapes. Process rapidly up to first escape. 777*7c478bd9Sstevel@tonic-gate */ 778*7c478bd9Sstevel@tonic-gate for (d = s; s < end; s++, d++) { 779*7c478bd9Sstevel@tonic-gate if (*s == '\\') 780*7c478bd9Sstevel@tonic-gate break; 781*7c478bd9Sstevel@tonic-gate } 782*7c478bd9Sstevel@tonic-gate 783*7c478bd9Sstevel@tonic-gate /* 784*7c478bd9Sstevel@tonic-gate * Reached the end, in which case will not go into loop, 785*7c478bd9Sstevel@tonic-gate * or found an escape and now have to start moving data. 786*7c478bd9Sstevel@tonic-gate */ 787*7c478bd9Sstevel@tonic-gate for (; s < end; s++) { 788*7c478bd9Sstevel@tonic-gate if (*s == '\\') { 789*7c478bd9Sstevel@tonic-gate val->val[i].length--; 790*7c478bd9Sstevel@tonic-gate /* 791*7c478bd9Sstevel@tonic-gate * Next character gets coppied without being 792*7c478bd9Sstevel@tonic-gate * checked 793*7c478bd9Sstevel@tonic-gate */ 794*7c478bd9Sstevel@tonic-gate s++; 795*7c478bd9Sstevel@tonic-gate if (s >= end) 796*7c478bd9Sstevel@tonic-gate break; 797*7c478bd9Sstevel@tonic-gate } 798*7c478bd9Sstevel@tonic-gate 799*7c478bd9Sstevel@tonic-gate *d = *s; 800*7c478bd9Sstevel@tonic-gate d++; 801*7c478bd9Sstevel@tonic-gate } 802*7c478bd9Sstevel@tonic-gate } 803*7c478bd9Sstevel@tonic-gate } 804