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 <sys/types.h> 30*0Sstevel@tonic-gate #include <stdio.h> 31*0Sstevel@tonic-gate #include <string.h> 32*0Sstevel@tonic-gate #include <stdlib.h> 33*0Sstevel@tonic-gate #include <nss_dbdefs.h> 34*0Sstevel@tonic-gate #include <prof_attr.h> 35*0Sstevel@tonic-gate #include <getxby_door.h> 36*0Sstevel@tonic-gate #include <sys/mman.h> 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate /* Externs from libnsl */ 40*0Sstevel@tonic-gate extern profstr_t *_getprofnam(const char *, profstr_t *, char *, int, int *); 41*0Sstevel@tonic-gate extern profstr_t *_getprofattr(profstr_t *, char *, int, int *); 42*0Sstevel@tonic-gate extern void _setprofattr(void); 43*0Sstevel@tonic-gate extern void _endprofattr(void); 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate static profattr_t *profstr2attr(profstr_t *); 46*0Sstevel@tonic-gate static profstr_t *process_getprof(profstr_t *, char *, int, nsc_data_t *); 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate profattr_t * 50*0Sstevel@tonic-gate getprofattr() 51*0Sstevel@tonic-gate { 52*0Sstevel@tonic-gate int err = 0; 53*0Sstevel@tonic-gate char buf[NSS_BUFLEN_PROFATTR]; 54*0Sstevel@tonic-gate profstr_t prof; 55*0Sstevel@tonic-gate profstr_t *tmp; 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate tmp = _getprofattr(&prof, buf, NSS_BUFLEN_PROFATTR, &err); 58*0Sstevel@tonic-gate return (profstr2attr(tmp)); 59*0Sstevel@tonic-gate } 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate profattr_t * 63*0Sstevel@tonic-gate getprofnam(const char *name) 64*0Sstevel@tonic-gate { 65*0Sstevel@tonic-gate int err = 0; 66*0Sstevel@tonic-gate int ndata = 0; 67*0Sstevel@tonic-gate int adata = 0; 68*0Sstevel@tonic-gate char buf[NSS_BUFLEN_PROFATTR]; 69*0Sstevel@tonic-gate profstr_t prof; 70*0Sstevel@tonic-gate union { 71*0Sstevel@tonic-gate nsc_data_t s_d; 72*0Sstevel@tonic-gate char s_b[1024]; 73*0Sstevel@tonic-gate } space; 74*0Sstevel@tonic-gate nsc_data_t *sptr = (nsc_data_t *)NULL; 75*0Sstevel@tonic-gate profstr_t *resptr = (profstr_t *)NULL; 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate (void) memset(&prof, 0, sizeof (profstr_t)); 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate #ifdef PIC 80*0Sstevel@tonic-gate (void) memset(&space, 0, sizeof (space)); 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate if ((name == NULL) || 83*0Sstevel@tonic-gate (strlen(name) >= (sizeof (space) - sizeof (nsc_data_t)))) { 84*0Sstevel@tonic-gate errno = ERANGE; 85*0Sstevel@tonic-gate return ((profattr_t *)NULL); 86*0Sstevel@tonic-gate } 87*0Sstevel@tonic-gate ndata = sizeof (space); 88*0Sstevel@tonic-gate adata = strlen(name) + sizeof (nsc_call_t) + 1; 89*0Sstevel@tonic-gate space.s_d.nsc_call.nsc_callnumber = GETPROFNAM; 90*0Sstevel@tonic-gate (void) strcpy(space.s_d.nsc_call.nsc_u.name, name); 91*0Sstevel@tonic-gate sptr = &space.s_d; 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate switch (_nsc_trydoorcall(&sptr, &ndata, &adata)) { 94*0Sstevel@tonic-gate case SUCCESS: /* positive cache hit */ 95*0Sstevel@tonic-gate break; 96*0Sstevel@tonic-gate case NOTFOUND: /* negative cache hit */ 97*0Sstevel@tonic-gate return ((profattr_t *)NULL); 98*0Sstevel@tonic-gate default: 99*0Sstevel@tonic-gate (void) memset(&prof, 0, sizeof (profattr_t)); 100*0Sstevel@tonic-gate resptr = _getprofnam(name, &prof, buf, 101*0Sstevel@tonic-gate NSS_BUFLEN_PROFATTR, &err); 102*0Sstevel@tonic-gate return (profstr2attr(resptr)); 103*0Sstevel@tonic-gate } 104*0Sstevel@tonic-gate resptr = process_getprof(&prof, buf, NSS_BUFLEN_PROFATTR, sptr); 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate /* 107*0Sstevel@tonic-gate * check if doors reallocated the memory underneath us 108*0Sstevel@tonic-gate * if they did munmap it or suffer a memory leak 109*0Sstevel@tonic-gate */ 110*0Sstevel@tonic-gate if (sptr != &space.s_d) 111*0Sstevel@tonic-gate (void) munmap((void *)sptr, ndata); 112*0Sstevel@tonic-gate #else /* !PIC */ 113*0Sstevel@tonic-gate resptr = _getprofnam(name, &prof, buf, NSS_BUFLEN_PROFATTR, &err); 114*0Sstevel@tonic-gate #endif /* PIC */ 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate return (profstr2attr(resptr)); 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate } 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate void 122*0Sstevel@tonic-gate setprofattr() 123*0Sstevel@tonic-gate { 124*0Sstevel@tonic-gate _setprofattr(); 125*0Sstevel@tonic-gate } 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gate void 129*0Sstevel@tonic-gate endprofattr() 130*0Sstevel@tonic-gate { 131*0Sstevel@tonic-gate _endprofattr(); 132*0Sstevel@tonic-gate } 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate void 136*0Sstevel@tonic-gate free_profattr(profattr_t *prof) 137*0Sstevel@tonic-gate { 138*0Sstevel@tonic-gate if (prof) { 139*0Sstevel@tonic-gate free(prof->name); 140*0Sstevel@tonic-gate free(prof->res1); 141*0Sstevel@tonic-gate free(prof->res2); 142*0Sstevel@tonic-gate free(prof->desc); 143*0Sstevel@tonic-gate _kva_free(prof->attr); 144*0Sstevel@tonic-gate free(prof); 145*0Sstevel@tonic-gate } 146*0Sstevel@tonic-gate } 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate static profattr_t * 150*0Sstevel@tonic-gate profstr2attr(profstr_t *prof) 151*0Sstevel@tonic-gate { 152*0Sstevel@tonic-gate profattr_t *newprof; 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate if (prof == NULL) 155*0Sstevel@tonic-gate return ((profattr_t *)NULL); 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate if ((newprof = (profattr_t *)malloc(sizeof (profattr_t))) == NULL) 158*0Sstevel@tonic-gate return ((profattr_t *)NULL); 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate newprof->name = _do_unescape(prof->name); 161*0Sstevel@tonic-gate newprof->res1 = _do_unescape(prof->res1); 162*0Sstevel@tonic-gate newprof->res2 = _do_unescape(prof->res2); 163*0Sstevel@tonic-gate newprof->desc = _do_unescape(prof->desc); 164*0Sstevel@tonic-gate newprof->attr = _str2kva(prof->attr, KV_ASSIGN, KV_DELIMITER); 165*0Sstevel@tonic-gate return (newprof); 166*0Sstevel@tonic-gate } 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate static profstr_t * 170*0Sstevel@tonic-gate process_getprof( 171*0Sstevel@tonic-gate profstr_t *result, 172*0Sstevel@tonic-gate char *buffer, 173*0Sstevel@tonic-gate int buflen, 174*0Sstevel@tonic-gate nsc_data_t *sptr) 175*0Sstevel@tonic-gate { 176*0Sstevel@tonic-gate char *fixed; 177*0Sstevel@tonic-gate #ifdef _LP64 178*0Sstevel@tonic-gate profstr_t prof64; 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate fixed = (char *)(((uintptr_t)buffer + 7) & ~7); 181*0Sstevel@tonic-gate #else 182*0Sstevel@tonic-gate fixed = (char *)(((uintptr_t)buffer + 3) & ~3); 183*0Sstevel@tonic-gate #endif 184*0Sstevel@tonic-gate buflen -= fixed - buffer; 185*0Sstevel@tonic-gate buffer = fixed; 186*0Sstevel@tonic-gate 187*0Sstevel@tonic-gate if (sptr->nsc_ret.nsc_return_code != SUCCESS) 188*0Sstevel@tonic-gate return ((profstr_t *)NULL); 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate #ifdef _LP64 191*0Sstevel@tonic-gate if (sptr->nsc_ret.nsc_bufferbytesused - (int)sizeof (profstr32_t) 192*0Sstevel@tonic-gate > buflen) 193*0Sstevel@tonic-gate #else 194*0Sstevel@tonic-gate if (sptr->nsc_ret.nsc_bufferbytesused - (int)sizeof (profstr_t) 195*0Sstevel@tonic-gate > buflen) 196*0Sstevel@tonic-gate #endif 197*0Sstevel@tonic-gate { 198*0Sstevel@tonic-gate errno = ERANGE; 199*0Sstevel@tonic-gate return ((profstr_t *)NULL); 200*0Sstevel@tonic-gate } 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate #ifdef _LP64 203*0Sstevel@tonic-gate (void) memcpy(buffer, (sptr->nsc_ret.nsc_u.buff + sizeof (profstr32_t)), 204*0Sstevel@tonic-gate (sptr->nsc_ret.nsc_bufferbytesused - sizeof (profstr32_t))); 205*0Sstevel@tonic-gate prof64.name = (char *)(sptr->nsc_ret.nsc_u.prof.name + 206*0Sstevel@tonic-gate (uintptr_t)buffer); 207*0Sstevel@tonic-gate prof64.res1 = (char *)(sptr->nsc_ret.nsc_u.prof.res1 + 208*0Sstevel@tonic-gate (uintptr_t)buffer); 209*0Sstevel@tonic-gate prof64.res2 = (char *)(sptr->nsc_ret.nsc_u.prof.res2 + 210*0Sstevel@tonic-gate (uintptr_t)buffer); 211*0Sstevel@tonic-gate prof64.desc = (char *)(sptr->nsc_ret.nsc_u.prof.desc + 212*0Sstevel@tonic-gate (uintptr_t)buffer); 213*0Sstevel@tonic-gate prof64.attr = (char *)(sptr->nsc_ret.nsc_u.prof.attr + 214*0Sstevel@tonic-gate (uintptr_t)buffer); 215*0Sstevel@tonic-gate *result = prof64; 216*0Sstevel@tonic-gate #else 217*0Sstevel@tonic-gate sptr->nsc_ret.nsc_u.prof.name += (uintptr_t)buffer; 218*0Sstevel@tonic-gate sptr->nsc_ret.nsc_u.prof.res1 += (uintptr_t)buffer; 219*0Sstevel@tonic-gate sptr->nsc_ret.nsc_u.prof.res2 += (uintptr_t)buffer; 220*0Sstevel@tonic-gate sptr->nsc_ret.nsc_u.prof.desc += (uintptr_t)buffer; 221*0Sstevel@tonic-gate sptr->nsc_ret.nsc_u.prof.attr += (uintptr_t)buffer; 222*0Sstevel@tonic-gate *result = sptr->nsc_ret.nsc_u.prof; 223*0Sstevel@tonic-gate (void) memcpy(buffer, (sptr->nsc_ret.nsc_u.buff + sizeof (profstr_t)), 224*0Sstevel@tonic-gate (sptr->nsc_ret.nsc_bufferbytesused - sizeof (profstr_t))); 225*0Sstevel@tonic-gate #endif 226*0Sstevel@tonic-gate return (result); 227*0Sstevel@tonic-gate } 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate /* 231*0Sstevel@tonic-gate * Given a profile name, gets the list of profiles found from 232*0Sstevel@tonic-gate * the whole hierarchy, using the given profile as root 233*0Sstevel@tonic-gate */ 234*0Sstevel@tonic-gate void 235*0Sstevel@tonic-gate getproflist(const char *profileName, char **profArray, int *profcnt) 236*0Sstevel@tonic-gate { 237*0Sstevel@tonic-gate profattr_t *profattr; 238*0Sstevel@tonic-gate char *subprofiles, *lasts, *profname; 239*0Sstevel@tonic-gate int i; 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate /* Check if this is a duplicate */ 242*0Sstevel@tonic-gate for (i = 0; i < *profcnt; i++) { 243*0Sstevel@tonic-gate if (strcmp(profileName, profArray[i]) == 0) { 244*0Sstevel@tonic-gate /* It's a duplicate, don't need to do anything */ 245*0Sstevel@tonic-gate return; 246*0Sstevel@tonic-gate } 247*0Sstevel@tonic-gate } 248*0Sstevel@tonic-gate 249*0Sstevel@tonic-gate profArray[*profcnt] = strdup(profileName); 250*0Sstevel@tonic-gate *profcnt = *profcnt + 1; 251*0Sstevel@tonic-gate 252*0Sstevel@tonic-gate profattr = getprofnam(profileName); 253*0Sstevel@tonic-gate if (profattr == NULL) { 254*0Sstevel@tonic-gate return; 255*0Sstevel@tonic-gate } 256*0Sstevel@tonic-gate 257*0Sstevel@tonic-gate if (profattr->attr == NULL) { 258*0Sstevel@tonic-gate free_profattr(profattr); 259*0Sstevel@tonic-gate return; 260*0Sstevel@tonic-gate } 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate subprofiles = kva_match(profattr->attr, PROFATTR_PROFS_KW); 263*0Sstevel@tonic-gate if (subprofiles == NULL) { 264*0Sstevel@tonic-gate free_profattr(profattr); 265*0Sstevel@tonic-gate return; 266*0Sstevel@tonic-gate } 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gate /* get execattr from each subprofiles */ 269*0Sstevel@tonic-gate for (profname = (char *)strtok_r(subprofiles, ",", &lasts); 270*0Sstevel@tonic-gate profname != NULL; 271*0Sstevel@tonic-gate profname = (char *)strtok_r(NULL, ",", &lasts)) { 272*0Sstevel@tonic-gate getproflist(profname, profArray, profcnt); 273*0Sstevel@tonic-gate } 274*0Sstevel@tonic-gate free_profattr(profattr); 275*0Sstevel@tonic-gate } 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate void 278*0Sstevel@tonic-gate free_proflist(char **profArray, int profcnt) 279*0Sstevel@tonic-gate { 280*0Sstevel@tonic-gate int i; 281*0Sstevel@tonic-gate for (i = 0; i < profcnt; i++) { 282*0Sstevel@tonic-gate free(profArray[i]); 283*0Sstevel@tonic-gate } 284*0Sstevel@tonic-gate } 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate #ifdef DEBUG 288*0Sstevel@tonic-gate void 289*0Sstevel@tonic-gate print_profattr(profattr_t *prof) 290*0Sstevel@tonic-gate { 291*0Sstevel@tonic-gate extern void print_kva(kva_t *); 292*0Sstevel@tonic-gate char *empty = "empty"; 293*0Sstevel@tonic-gate 294*0Sstevel@tonic-gate if (prof == NULL) { 295*0Sstevel@tonic-gate printf("NULL\n"); 296*0Sstevel@tonic-gate return; 297*0Sstevel@tonic-gate } 298*0Sstevel@tonic-gate 299*0Sstevel@tonic-gate printf("name=%s\n", prof->name ? prof->name : empty); 300*0Sstevel@tonic-gate printf("res1=%s\n", prof->res1 ? prof->res1 : empty); 301*0Sstevel@tonic-gate printf("res2=%s\n", prof->res2 ? prof->res2 : empty); 302*0Sstevel@tonic-gate printf("desc=%s\n", prof->desc ? prof->desc : empty); 303*0Sstevel@tonic-gate printf("attr=\n"); 304*0Sstevel@tonic-gate print_kva(prof->attr); 305*0Sstevel@tonic-gate fflush(stdout); 306*0Sstevel@tonic-gate } 307*0Sstevel@tonic-gate #endif /* DEBUG */ 308