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