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