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