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 <sys/mman.h> 31*7c478bd9Sstevel@tonic-gate #include <stdio.h> 32*7c478bd9Sstevel@tonic-gate #include <string.h> 33*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 34*7c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h> 35*7c478bd9Sstevel@tonic-gate #include <user_attr.h> 36*7c478bd9Sstevel@tonic-gate #include <getxby_door.h> 37*7c478bd9Sstevel@tonic-gate #include <pwd.h> 38*7c478bd9Sstevel@tonic-gate 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate /* Externs from libnsl */ 41*7c478bd9Sstevel@tonic-gate extern userstr_t *_getusernam(const char *, userstr_t *, char *, int, int *); 42*7c478bd9Sstevel@tonic-gate extern userstr_t *_getuserattr(userstr_t *, char *, int, int *); 43*7c478bd9Sstevel@tonic-gate extern userstr_t *_fgetuserattr(FILE *, userstr_t *, char *, int); 44*7c478bd9Sstevel@tonic-gate extern void _setuserattr(void); 45*7c478bd9Sstevel@tonic-gate extern void _enduserattr(void); 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate static userattr_t *userstr2attr(userstr_t *); 49*7c478bd9Sstevel@tonic-gate static userstr_t *process_getuser(userstr_t *, char *, int, nsc_data_t *); 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate userattr_t * 53*7c478bd9Sstevel@tonic-gate getuserattr() 54*7c478bd9Sstevel@tonic-gate { 55*7c478bd9Sstevel@tonic-gate int err = 0; 56*7c478bd9Sstevel@tonic-gate char buf[NSS_BUFLEN_USERATTR]; 57*7c478bd9Sstevel@tonic-gate userstr_t user; 58*7c478bd9Sstevel@tonic-gate userstr_t *tmp; 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate (void) memset(&user, 0, sizeof (userattr_t)); 61*7c478bd9Sstevel@tonic-gate tmp = _getuserattr(&user, buf, NSS_BUFLEN_USERATTR, &err); 62*7c478bd9Sstevel@tonic-gate return (userstr2attr(tmp)); 63*7c478bd9Sstevel@tonic-gate } 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate userattr_t * 67*7c478bd9Sstevel@tonic-gate fgetuserattr(FILE *f) 68*7c478bd9Sstevel@tonic-gate { 69*7c478bd9Sstevel@tonic-gate char buf[NSS_BUFLEN_USERATTR]; 70*7c478bd9Sstevel@tonic-gate userstr_t user; 71*7c478bd9Sstevel@tonic-gate userstr_t *tmp; 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate (void) memset(&user, 0, sizeof (userattr_t)); 74*7c478bd9Sstevel@tonic-gate tmp = _fgetuserattr(f, &user, buf, NSS_BUFLEN_USERATTR); 75*7c478bd9Sstevel@tonic-gate return (userstr2attr(tmp)); 76*7c478bd9Sstevel@tonic-gate } 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate userattr_t * 80*7c478bd9Sstevel@tonic-gate getusernam(const char *name) 81*7c478bd9Sstevel@tonic-gate { 82*7c478bd9Sstevel@tonic-gate int err = 0; 83*7c478bd9Sstevel@tonic-gate int ndata; 84*7c478bd9Sstevel@tonic-gate int adata; 85*7c478bd9Sstevel@tonic-gate char buf[NSS_BUFLEN_USERATTR]; 86*7c478bd9Sstevel@tonic-gate userstr_t user; 87*7c478bd9Sstevel@tonic-gate union { 88*7c478bd9Sstevel@tonic-gate nsc_data_t s_d; 89*7c478bd9Sstevel@tonic-gate char s_b[1024]; 90*7c478bd9Sstevel@tonic-gate } space; 91*7c478bd9Sstevel@tonic-gate nsc_data_t *sptr; 92*7c478bd9Sstevel@tonic-gate userstr_t *resptr = (userstr_t *)NULL; 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate #ifdef PIC 95*7c478bd9Sstevel@tonic-gate if ((name == NULL) || 96*7c478bd9Sstevel@tonic-gate (strlen(name) >= (sizeof (space) - sizeof (nsc_data_t)))) { 97*7c478bd9Sstevel@tonic-gate errno = ERANGE; 98*7c478bd9Sstevel@tonic-gate return ((userattr_t *)NULL); 99*7c478bd9Sstevel@tonic-gate } 100*7c478bd9Sstevel@tonic-gate ndata = sizeof (space); 101*7c478bd9Sstevel@tonic-gate adata = strlen(name) + sizeof (nsc_call_t) + 1; 102*7c478bd9Sstevel@tonic-gate space.s_d.nsc_call.nsc_callnumber = GETUSERNAM; 103*7c478bd9Sstevel@tonic-gate (void) strcpy(space.s_d.nsc_call.nsc_u.name, name); 104*7c478bd9Sstevel@tonic-gate sptr = &space.s_d; 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate switch (_nsc_trydoorcall(&sptr, &ndata, &adata)) { 107*7c478bd9Sstevel@tonic-gate case SUCCESS: /* positive cache hit */ 108*7c478bd9Sstevel@tonic-gate break; 109*7c478bd9Sstevel@tonic-gate case NOTFOUND: /* negative cache hit */ 110*7c478bd9Sstevel@tonic-gate return ((userattr_t *)NULL); 111*7c478bd9Sstevel@tonic-gate default: 112*7c478bd9Sstevel@tonic-gate (void) memset(&user, 0, sizeof (userattr_t)); 113*7c478bd9Sstevel@tonic-gate resptr = _getusernam(name, &user, buf, 114*7c478bd9Sstevel@tonic-gate NSS_BUFLEN_USERATTR, &err); 115*7c478bd9Sstevel@tonic-gate return (userstr2attr(resptr)); 116*7c478bd9Sstevel@tonic-gate } 117*7c478bd9Sstevel@tonic-gate resptr = process_getuser(&user, buf, NSS_BUFLEN_USERATTR, sptr); 118*7c478bd9Sstevel@tonic-gate 119*7c478bd9Sstevel@tonic-gate /* 120*7c478bd9Sstevel@tonic-gate * check if doors reallocated the memory underneath us 121*7c478bd9Sstevel@tonic-gate * if they did munmap it or suffer a memory leak 122*7c478bd9Sstevel@tonic-gate */ 123*7c478bd9Sstevel@tonic-gate if (sptr != &space.s_d) 124*7c478bd9Sstevel@tonic-gate (void) munmap((void *)sptr, ndata); 125*7c478bd9Sstevel@tonic-gate #else /* !PIC */ 126*7c478bd9Sstevel@tonic-gate resptr = _getusernam(name, &user, buf, NSS_BUFLEN_USERATTR, &err); 127*7c478bd9Sstevel@tonic-gate #endif /* PIC */ 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate return (userstr2attr(resptr)); 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate } 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate userattr_t * 135*7c478bd9Sstevel@tonic-gate getuseruid(uid_t u) 136*7c478bd9Sstevel@tonic-gate { 137*7c478bd9Sstevel@tonic-gate struct passwd pwd; 138*7c478bd9Sstevel@tonic-gate char buf[NSS_BUFLEN_PASSWD]; 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate if (getpwuid_r(u, &pwd, buf, NSS_BUFLEN_PASSWD) == NULL) 141*7c478bd9Sstevel@tonic-gate return ((userattr_t *)NULL); 142*7c478bd9Sstevel@tonic-gate return (getusernam(pwd.pw_name)); 143*7c478bd9Sstevel@tonic-gate } 144*7c478bd9Sstevel@tonic-gate 145*7c478bd9Sstevel@tonic-gate 146*7c478bd9Sstevel@tonic-gate void 147*7c478bd9Sstevel@tonic-gate setuserattr() 148*7c478bd9Sstevel@tonic-gate { 149*7c478bd9Sstevel@tonic-gate _setuserattr(); 150*7c478bd9Sstevel@tonic-gate } 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate 153*7c478bd9Sstevel@tonic-gate void 154*7c478bd9Sstevel@tonic-gate enduserattr() 155*7c478bd9Sstevel@tonic-gate { 156*7c478bd9Sstevel@tonic-gate _enduserattr(); 157*7c478bd9Sstevel@tonic-gate } 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gate 160*7c478bd9Sstevel@tonic-gate void 161*7c478bd9Sstevel@tonic-gate free_userattr(userattr_t *user) 162*7c478bd9Sstevel@tonic-gate { 163*7c478bd9Sstevel@tonic-gate if (user) { 164*7c478bd9Sstevel@tonic-gate free(user->name); 165*7c478bd9Sstevel@tonic-gate free(user->qualifier); 166*7c478bd9Sstevel@tonic-gate free(user->res1); 167*7c478bd9Sstevel@tonic-gate free(user->res2); 168*7c478bd9Sstevel@tonic-gate _kva_free(user->attr); 169*7c478bd9Sstevel@tonic-gate free(user); 170*7c478bd9Sstevel@tonic-gate } 171*7c478bd9Sstevel@tonic-gate } 172*7c478bd9Sstevel@tonic-gate 173*7c478bd9Sstevel@tonic-gate 174*7c478bd9Sstevel@tonic-gate static userattr_t * 175*7c478bd9Sstevel@tonic-gate userstr2attr(userstr_t *user) 176*7c478bd9Sstevel@tonic-gate { 177*7c478bd9Sstevel@tonic-gate userattr_t *newuser; 178*7c478bd9Sstevel@tonic-gate 179*7c478bd9Sstevel@tonic-gate if (user == NULL) 180*7c478bd9Sstevel@tonic-gate return ((userattr_t *)NULL); 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gate if ((newuser = (userattr_t *)malloc(sizeof (userattr_t))) == NULL) 183*7c478bd9Sstevel@tonic-gate return ((userattr_t *)NULL); 184*7c478bd9Sstevel@tonic-gate 185*7c478bd9Sstevel@tonic-gate newuser->name = _do_unescape(user->name); 186*7c478bd9Sstevel@tonic-gate newuser->qualifier = _do_unescape(user->qualifier); 187*7c478bd9Sstevel@tonic-gate newuser->res1 = _do_unescape(user->res1); 188*7c478bd9Sstevel@tonic-gate newuser->res2 = _do_unescape(user->res2); 189*7c478bd9Sstevel@tonic-gate newuser->attr = _str2kva(user->attr, KV_ASSIGN, KV_DELIMITER); 190*7c478bd9Sstevel@tonic-gate return (newuser); 191*7c478bd9Sstevel@tonic-gate } 192*7c478bd9Sstevel@tonic-gate 193*7c478bd9Sstevel@tonic-gate 194*7c478bd9Sstevel@tonic-gate static userstr_t * 195*7c478bd9Sstevel@tonic-gate process_getuser( 196*7c478bd9Sstevel@tonic-gate userstr_t *result, 197*7c478bd9Sstevel@tonic-gate char *buffer, 198*7c478bd9Sstevel@tonic-gate int buflen, 199*7c478bd9Sstevel@tonic-gate nsc_data_t *sptr) 200*7c478bd9Sstevel@tonic-gate { 201*7c478bd9Sstevel@tonic-gate char *fixed; 202*7c478bd9Sstevel@tonic-gate #ifdef _LP64 203*7c478bd9Sstevel@tonic-gate userstr_t user64; 204*7c478bd9Sstevel@tonic-gate 205*7c478bd9Sstevel@tonic-gate fixed = (char *)(((uintptr_t)buffer + 7) & ~7); 206*7c478bd9Sstevel@tonic-gate #else 207*7c478bd9Sstevel@tonic-gate fixed = (char *)(((uintptr_t)buffer + 3) & ~3); 208*7c478bd9Sstevel@tonic-gate #endif 209*7c478bd9Sstevel@tonic-gate buflen -= fixed - buffer; 210*7c478bd9Sstevel@tonic-gate buffer = fixed; 211*7c478bd9Sstevel@tonic-gate 212*7c478bd9Sstevel@tonic-gate if (sptr->nsc_ret.nsc_return_code != SUCCESS) 213*7c478bd9Sstevel@tonic-gate return ((userstr_t *)NULL); 214*7c478bd9Sstevel@tonic-gate 215*7c478bd9Sstevel@tonic-gate #ifdef _LP64 216*7c478bd9Sstevel@tonic-gate if (sptr->nsc_ret.nsc_bufferbytesused - (int)sizeof (userstr32_t) 217*7c478bd9Sstevel@tonic-gate > buflen) 218*7c478bd9Sstevel@tonic-gate #else 219*7c478bd9Sstevel@tonic-gate if (sptr->nsc_ret.nsc_bufferbytesused - (int)sizeof (userstr_t) 220*7c478bd9Sstevel@tonic-gate > buflen) 221*7c478bd9Sstevel@tonic-gate #endif 222*7c478bd9Sstevel@tonic-gate { 223*7c478bd9Sstevel@tonic-gate errno = ERANGE; 224*7c478bd9Sstevel@tonic-gate return ((userstr_t *)NULL); 225*7c478bd9Sstevel@tonic-gate } 226*7c478bd9Sstevel@tonic-gate 227*7c478bd9Sstevel@tonic-gate #ifdef _LP64 228*7c478bd9Sstevel@tonic-gate (void) memcpy(buffer, (sptr->nsc_ret.nsc_u.buff + sizeof (userstr32_t)), 229*7c478bd9Sstevel@tonic-gate (sptr->nsc_ret.nsc_bufferbytesused - sizeof (userstr32_t))); 230*7c478bd9Sstevel@tonic-gate user64.name = (char *)(sptr->nsc_ret.nsc_u.user.name + 231*7c478bd9Sstevel@tonic-gate (uintptr_t)buffer); 232*7c478bd9Sstevel@tonic-gate user64.qualifier = (char *)(sptr->nsc_ret.nsc_u.user.qualifier + 233*7c478bd9Sstevel@tonic-gate (uintptr_t)buffer); 234*7c478bd9Sstevel@tonic-gate user64.res1 = (char *)(sptr->nsc_ret.nsc_u.user.res1 + 235*7c478bd9Sstevel@tonic-gate (uintptr_t)buffer); 236*7c478bd9Sstevel@tonic-gate user64.res2 = (char *)(sptr->nsc_ret.nsc_u.user.res2 + 237*7c478bd9Sstevel@tonic-gate (uintptr_t)buffer); 238*7c478bd9Sstevel@tonic-gate user64.attr = (char *)(sptr->nsc_ret.nsc_u.user.attr + 239*7c478bd9Sstevel@tonic-gate (uintptr_t)buffer); 240*7c478bd9Sstevel@tonic-gate *result = user64; 241*7c478bd9Sstevel@tonic-gate #else 242*7c478bd9Sstevel@tonic-gate sptr->nsc_ret.nsc_u.user.name += (uintptr_t)buffer; 243*7c478bd9Sstevel@tonic-gate sptr->nsc_ret.nsc_u.user.qualifier += (uintptr_t)buffer; 244*7c478bd9Sstevel@tonic-gate sptr->nsc_ret.nsc_u.user.res1 += (uintptr_t)buffer; 245*7c478bd9Sstevel@tonic-gate sptr->nsc_ret.nsc_u.user.res2 += (uintptr_t)buffer; 246*7c478bd9Sstevel@tonic-gate sptr->nsc_ret.nsc_u.user.attr += (uintptr_t)buffer; 247*7c478bd9Sstevel@tonic-gate *result = sptr->nsc_ret.nsc_u.user; 248*7c478bd9Sstevel@tonic-gate (void) memcpy(buffer, (sptr->nsc_ret.nsc_u.buff + sizeof (userstr_t)), 249*7c478bd9Sstevel@tonic-gate (sptr->nsc_ret.nsc_bufferbytesused - sizeof (userstr_t))); 250*7c478bd9Sstevel@tonic-gate #endif 251*7c478bd9Sstevel@tonic-gate return (result); 252*7c478bd9Sstevel@tonic-gate } 253*7c478bd9Sstevel@tonic-gate 254*7c478bd9Sstevel@tonic-gate 255*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 256*7c478bd9Sstevel@tonic-gate void 257*7c478bd9Sstevel@tonic-gate print_userattr(userattr_t *user) 258*7c478bd9Sstevel@tonic-gate { 259*7c478bd9Sstevel@tonic-gate extern void print_kva(kva_t *); 260*7c478bd9Sstevel@tonic-gate char *empty = "empty"; 261*7c478bd9Sstevel@tonic-gate 262*7c478bd9Sstevel@tonic-gate if (user == NULL) { 263*7c478bd9Sstevel@tonic-gate printf("NULL\n"); 264*7c478bd9Sstevel@tonic-gate return; 265*7c478bd9Sstevel@tonic-gate } 266*7c478bd9Sstevel@tonic-gate 267*7c478bd9Sstevel@tonic-gate printf("name=%s\n", user->name ? user->name : empty); 268*7c478bd9Sstevel@tonic-gate printf("qualifier=%s\n", user->qualifier ? user->qualifier : empty); 269*7c478bd9Sstevel@tonic-gate printf("res1=%s\n", user->res1 ? user->res1 : empty); 270*7c478bd9Sstevel@tonic-gate printf("res2=%s\n", user->res2 ? user->res2 : empty); 271*7c478bd9Sstevel@tonic-gate printf("attr=\n"); 272*7c478bd9Sstevel@tonic-gate print_kva(user->attr); 273*7c478bd9Sstevel@tonic-gate fflush(stdout); 274*7c478bd9Sstevel@tonic-gate } 275*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 276