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 2003 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 /* 30*7c478bd9Sstevel@tonic-gate * Routines to handle getgr* calls in nscd 31*7c478bd9Sstevel@tonic-gate */ 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #include <assert.h> 34*7c478bd9Sstevel@tonic-gate #include <errno.h> 35*7c478bd9Sstevel@tonic-gate #include <memory.h> 36*7c478bd9Sstevel@tonic-gate #include <signal.h> 37*7c478bd9Sstevel@tonic-gate #include <stdio.h> 38*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 39*7c478bd9Sstevel@tonic-gate #include <string.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/door.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 42*7c478bd9Sstevel@tonic-gate #include <sys/time.h> 43*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 44*7c478bd9Sstevel@tonic-gate #include <sys/wait.h> 45*7c478bd9Sstevel@tonic-gate #include <thread.h> 46*7c478bd9Sstevel@tonic-gate #include <unistd.h> 47*7c478bd9Sstevel@tonic-gate #include <ucred.h> 48*7c478bd9Sstevel@tonic-gate #include <nss_common.h> 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate #include <getxby_door.h> 51*7c478bd9Sstevel@tonic-gate #include "server_door.h" 52*7c478bd9Sstevel@tonic-gate #include "nscd.h" 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate static hash_t *uid_hash; 55*7c478bd9Sstevel@tonic-gate static hash_t *nam_hash; 56*7c478bd9Sstevel@tonic-gate static mutex_t group_lock = DEFAULTMUTEX; 57*7c478bd9Sstevel@tonic-gate static waiter_t group_wait; 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate static void getgr_gidkeepalive(int keep, int interval); 60*7c478bd9Sstevel@tonic-gate static void getgr_namekeepalive(int keep, int interval); 61*7c478bd9Sstevel@tonic-gate static int update_gr_bucket(nsc_bucket_t **old, nsc_bucket_t *new, 62*7c478bd9Sstevel@tonic-gate int callnumber); 63*7c478bd9Sstevel@tonic-gate static nsc_bucket_t *fixbuffer(nsc_return_t *in, int maxlen); 64*7c478bd9Sstevel@tonic-gate static void do_findgids(nsc_bucket_t *ptr, int *table, int gid); 65*7c478bd9Sstevel@tonic-gate static void do_findgnams(nsc_bucket_t *ptr, int *table, char *gnam); 66*7c478bd9Sstevel@tonic-gate static void do_invalidate(nsc_bucket_t **ptr, int callnumber); 67*7c478bd9Sstevel@tonic-gate static void getgr_invalidate_unlocked(void); 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate void 71*7c478bd9Sstevel@tonic-gate getgr_init(void) 72*7c478bd9Sstevel@tonic-gate { 73*7c478bd9Sstevel@tonic-gate uid_hash = make_ihash(current_admin.group.nsc_suggestedsize); 74*7c478bd9Sstevel@tonic-gate nam_hash = make_hash(current_admin.group.nsc_suggestedsize); 75*7c478bd9Sstevel@tonic-gate 76*7c478bd9Sstevel@tonic-gate } 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate static void 79*7c478bd9Sstevel@tonic-gate do_invalidate(nsc_bucket_t **ptr, int callnumber) 80*7c478bd9Sstevel@tonic-gate { 81*7c478bd9Sstevel@tonic-gate if (*ptr != NULL && *ptr != (nsc_bucket_t *)-1) { 82*7c478bd9Sstevel@tonic-gate /* leave pending calls alone */ 83*7c478bd9Sstevel@tonic-gate update_gr_bucket(ptr, NULL, callnumber); 84*7c478bd9Sstevel@tonic-gate } 85*7c478bd9Sstevel@tonic-gate } 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate static void 88*7c478bd9Sstevel@tonic-gate do_findgids(nsc_bucket_t *ptr, int *table, int gid) 89*7c478bd9Sstevel@tonic-gate { 90*7c478bd9Sstevel@tonic-gate 91*7c478bd9Sstevel@tonic-gate /* 92*7c478bd9Sstevel@tonic-gate * be careful with ptr - it may be -1 or NULL. 93*7c478bd9Sstevel@tonic-gate */ 94*7c478bd9Sstevel@tonic-gate if (ptr != NULL && ptr != (nsc_bucket_t *)-1) { 95*7c478bd9Sstevel@tonic-gate insertn(table, ptr->nsc_hits, gid); 96*7c478bd9Sstevel@tonic-gate } 97*7c478bd9Sstevel@tonic-gate } 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate static void 100*7c478bd9Sstevel@tonic-gate do_findgnams(nsc_bucket_t *ptr, int *table, char *gnam) 101*7c478bd9Sstevel@tonic-gate { 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate /* 104*7c478bd9Sstevel@tonic-gate * be careful with ptr - it may be -1 or NULL. 105*7c478bd9Sstevel@tonic-gate */ 106*7c478bd9Sstevel@tonic-gate 107*7c478bd9Sstevel@tonic-gate if (ptr != NULL && ptr != (nsc_bucket_t *)-1) { 108*7c478bd9Sstevel@tonic-gate char *tmp = (char *)insertn(table, ptr->nsc_hits, 109*7c478bd9Sstevel@tonic-gate (int)strdup(gnam)); 110*7c478bd9Sstevel@tonic-gate if (tmp != (char *)-1) 111*7c478bd9Sstevel@tonic-gate free(tmp); 112*7c478bd9Sstevel@tonic-gate } 113*7c478bd9Sstevel@tonic-gate } 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate void 116*7c478bd9Sstevel@tonic-gate getgr_revalidate(void) 117*7c478bd9Sstevel@tonic-gate { 118*7c478bd9Sstevel@tonic-gate for (;;) { 119*7c478bd9Sstevel@tonic-gate int slp; 120*7c478bd9Sstevel@tonic-gate int interval; 121*7c478bd9Sstevel@tonic-gate int count; 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate slp = current_admin.group.nsc_pos_ttl; 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate if (slp < 60) { 126*7c478bd9Sstevel@tonic-gate slp = 60; 127*7c478bd9Sstevel@tonic-gate } 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate if ((count = current_admin.group.nsc_keephot) != 0) { 130*7c478bd9Sstevel@tonic-gate interval = (slp / 2)/count; 131*7c478bd9Sstevel@tonic-gate if (interval == 0) interval = 1; 132*7c478bd9Sstevel@tonic-gate sleep(slp * 2 / 3); 133*7c478bd9Sstevel@tonic-gate getgr_gidkeepalive(count, interval); 134*7c478bd9Sstevel@tonic-gate getgr_namekeepalive(count, interval); 135*7c478bd9Sstevel@tonic-gate } else { 136*7c478bd9Sstevel@tonic-gate sleep(slp); 137*7c478bd9Sstevel@tonic-gate } 138*7c478bd9Sstevel@tonic-gate } 139*7c478bd9Sstevel@tonic-gate } 140*7c478bd9Sstevel@tonic-gate 141*7c478bd9Sstevel@tonic-gate static void 142*7c478bd9Sstevel@tonic-gate getgr_gidkeepalive(int keep, int interval) 143*7c478bd9Sstevel@tonic-gate { 144*7c478bd9Sstevel@tonic-gate int *table; 145*7c478bd9Sstevel@tonic-gate nsc_data_t ping; 146*7c478bd9Sstevel@tonic-gate int i; 147*7c478bd9Sstevel@tonic-gate 148*7c478bd9Sstevel@tonic-gate if (!keep) 149*7c478bd9Sstevel@tonic-gate return; 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gate table = maken(keep); 152*7c478bd9Sstevel@tonic-gate mutex_lock(&group_lock); 153*7c478bd9Sstevel@tonic-gate operate_hash(uid_hash, do_findgids, (char *)table); 154*7c478bd9Sstevel@tonic-gate mutex_unlock(&group_lock); 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate for (i = 1; i <= keep; i++) { 157*7c478bd9Sstevel@tonic-gate ping.nsc_call.nsc_callnumber = GETGRGID; 158*7c478bd9Sstevel@tonic-gate if ((ping.nsc_call.nsc_u.gid = table[keep + 1 + i]) == -1) 159*7c478bd9Sstevel@tonic-gate continue; /* unused slot in table */ 160*7c478bd9Sstevel@tonic-gate launch_update(&ping.nsc_call); 161*7c478bd9Sstevel@tonic-gate sleep(interval); 162*7c478bd9Sstevel@tonic-gate } 163*7c478bd9Sstevel@tonic-gate free(table); 164*7c478bd9Sstevel@tonic-gate } 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gate static void 167*7c478bd9Sstevel@tonic-gate getgr_namekeepalive(int keep, int interval) 168*7c478bd9Sstevel@tonic-gate { 169*7c478bd9Sstevel@tonic-gate int *table; 170*7c478bd9Sstevel@tonic-gate union { 171*7c478bd9Sstevel@tonic-gate nsc_data_t ping; 172*7c478bd9Sstevel@tonic-gate char space[sizeof (nsc_data_t) + NSCDMAXNAMELEN]; 173*7c478bd9Sstevel@tonic-gate } u; 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate int i; 176*7c478bd9Sstevel@tonic-gate 177*7c478bd9Sstevel@tonic-gate if (!keep) 178*7c478bd9Sstevel@tonic-gate return; 179*7c478bd9Sstevel@tonic-gate 180*7c478bd9Sstevel@tonic-gate table = maken(keep); 181*7c478bd9Sstevel@tonic-gate mutex_lock(&group_lock); 182*7c478bd9Sstevel@tonic-gate operate_hash(nam_hash, do_findgnams, (char *)table); 183*7c478bd9Sstevel@tonic-gate mutex_unlock(&group_lock); 184*7c478bd9Sstevel@tonic-gate 185*7c478bd9Sstevel@tonic-gate for (i = 1; i <= keep; i++) { 186*7c478bd9Sstevel@tonic-gate char *tmp; 187*7c478bd9Sstevel@tonic-gate u.ping.nsc_call.nsc_callnumber = GETGRNAM; 188*7c478bd9Sstevel@tonic-gate 189*7c478bd9Sstevel@tonic-gate if ((tmp = (char *)table[keep + 1 + i]) == (char *)-1) 190*7c478bd9Sstevel@tonic-gate continue; /* unused slot in table */ 191*7c478bd9Sstevel@tonic-gate 192*7c478bd9Sstevel@tonic-gate strcpy(u.ping.nsc_call.nsc_u.name, tmp); 193*7c478bd9Sstevel@tonic-gate 194*7c478bd9Sstevel@tonic-gate launch_update(&u.ping.nsc_call); 195*7c478bd9Sstevel@tonic-gate sleep(interval); 196*7c478bd9Sstevel@tonic-gate } 197*7c478bd9Sstevel@tonic-gate 198*7c478bd9Sstevel@tonic-gate for (i = 1; i <= keep; i++) { 199*7c478bd9Sstevel@tonic-gate char *tmp; 200*7c478bd9Sstevel@tonic-gate if ((tmp = (char *)table[keep + 1 + i]) != (char *)-1) 201*7c478bd9Sstevel@tonic-gate free(tmp); 202*7c478bd9Sstevel@tonic-gate } 203*7c478bd9Sstevel@tonic-gate 204*7c478bd9Sstevel@tonic-gate free(table); 205*7c478bd9Sstevel@tonic-gate } 206*7c478bd9Sstevel@tonic-gate 207*7c478bd9Sstevel@tonic-gate 208*7c478bd9Sstevel@tonic-gate /* 209*7c478bd9Sstevel@tonic-gate * This routine marks all entries as invalid 210*7c478bd9Sstevel@tonic-gate * 211*7c478bd9Sstevel@tonic-gate */ 212*7c478bd9Sstevel@tonic-gate 213*7c478bd9Sstevel@tonic-gate void 214*7c478bd9Sstevel@tonic-gate getgr_invalidate(void) 215*7c478bd9Sstevel@tonic-gate { 216*7c478bd9Sstevel@tonic-gate mutex_lock(&group_lock); 217*7c478bd9Sstevel@tonic-gate getgr_invalidate_unlocked(); 218*7c478bd9Sstevel@tonic-gate mutex_unlock(&group_lock); 219*7c478bd9Sstevel@tonic-gate } 220*7c478bd9Sstevel@tonic-gate 221*7c478bd9Sstevel@tonic-gate static void 222*7c478bd9Sstevel@tonic-gate getgr_invalidate_unlocked(void) 223*7c478bd9Sstevel@tonic-gate { 224*7c478bd9Sstevel@tonic-gate operate_hash_addr(nam_hash, do_invalidate, (char *)GETGRNAM); 225*7c478bd9Sstevel@tonic-gate operate_hash_addr(uid_hash, do_invalidate, (char *)GETGRGID); 226*7c478bd9Sstevel@tonic-gate current_admin.group.nsc_invalidate_count++; 227*7c478bd9Sstevel@tonic-gate } 228*7c478bd9Sstevel@tonic-gate 229*7c478bd9Sstevel@tonic-gate void 230*7c478bd9Sstevel@tonic-gate getgr_lookup(nsc_return_t *out, int maxsize, nsc_call_t *in, time_t now) 231*7c478bd9Sstevel@tonic-gate { 232*7c478bd9Sstevel@tonic-gate int out_of_date; 233*7c478bd9Sstevel@tonic-gate nsc_bucket_t *retb; 234*7c478bd9Sstevel@tonic-gate char **bucket; 235*7c478bd9Sstevel@tonic-gate 236*7c478bd9Sstevel@tonic-gate static time_t lastmod; 237*7c478bd9Sstevel@tonic-gate 238*7c478bd9Sstevel@tonic-gate int bufferspace = maxsize - sizeof (nsc_return_t); 239*7c478bd9Sstevel@tonic-gate 240*7c478bd9Sstevel@tonic-gate if (current_admin.group.nsc_enabled == 0) { 241*7c478bd9Sstevel@tonic-gate out->nsc_return_code = NOSERVER; 242*7c478bd9Sstevel@tonic-gate out->nsc_bufferbytesused = sizeof (*out); 243*7c478bd9Sstevel@tonic-gate return; 244*7c478bd9Sstevel@tonic-gate } 245*7c478bd9Sstevel@tonic-gate 246*7c478bd9Sstevel@tonic-gate mutex_lock(&group_lock); 247*7c478bd9Sstevel@tonic-gate 248*7c478bd9Sstevel@tonic-gate if (current_admin.group.nsc_check_files) { 249*7c478bd9Sstevel@tonic-gate struct stat buf; 250*7c478bd9Sstevel@tonic-gate 251*7c478bd9Sstevel@tonic-gate if (stat("/etc/group", &buf) < 0) { 252*7c478bd9Sstevel@tonic-gate /*EMPTY*/; 253*7c478bd9Sstevel@tonic-gate } else if (lastmod == 0) { 254*7c478bd9Sstevel@tonic-gate lastmod = buf.st_mtime; 255*7c478bd9Sstevel@tonic-gate } else if (lastmod < buf.st_mtime) { 256*7c478bd9Sstevel@tonic-gate getgr_invalidate_unlocked(); 257*7c478bd9Sstevel@tonic-gate lastmod = buf.st_mtime; 258*7c478bd9Sstevel@tonic-gate } 259*7c478bd9Sstevel@tonic-gate } 260*7c478bd9Sstevel@tonic-gate 261*7c478bd9Sstevel@tonic-gate if (current_admin.debug_level >= DBG_ALL) { 262*7c478bd9Sstevel@tonic-gate if (MASKUPDATEBIT(in->nsc_callnumber) == GETGRGID) { 263*7c478bd9Sstevel@tonic-gate logit("getgr_lookup: looking for gid %d\n", 264*7c478bd9Sstevel@tonic-gate in->nsc_u.gid); 265*7c478bd9Sstevel@tonic-gate } else { 266*7c478bd9Sstevel@tonic-gate logit("getgr_lookup: looking for name %s\n", 267*7c478bd9Sstevel@tonic-gate in->nsc_u.name); 268*7c478bd9Sstevel@tonic-gate } 269*7c478bd9Sstevel@tonic-gate } 270*7c478bd9Sstevel@tonic-gate 271*7c478bd9Sstevel@tonic-gate for (;;) { 272*7c478bd9Sstevel@tonic-gate if (MASKUPDATEBIT(in->nsc_callnumber) == GETGRGID) { 273*7c478bd9Sstevel@tonic-gate bucket = get_hash(uid_hash, (char *)in->nsc_u.gid); 274*7c478bd9Sstevel@tonic-gate } else { 275*7c478bd9Sstevel@tonic-gate if (strlen(in->nsc_u.name) > NSCDMAXNAMELEN) { 276*7c478bd9Sstevel@tonic-gate ucred_t *uc = NULL; 277*7c478bd9Sstevel@tonic-gate 278*7c478bd9Sstevel@tonic-gate if (door_ucred(&uc) != 0) { 279*7c478bd9Sstevel@tonic-gate logit("getgr_lookup: Name too long, " 280*7c478bd9Sstevel@tonic-gate "but no user credential: %s\n", 281*7c478bd9Sstevel@tonic-gate strerror(errno)); 282*7c478bd9Sstevel@tonic-gate } else { 283*7c478bd9Sstevel@tonic-gate logit("getgr_lookup: Name too long " 284*7c478bd9Sstevel@tonic-gate "from pid %d uid %d\n", 285*7c478bd9Sstevel@tonic-gate ucred_getpid(uc), 286*7c478bd9Sstevel@tonic-gate ucred_getruid(uc)); 287*7c478bd9Sstevel@tonic-gate ucred_free(uc); 288*7c478bd9Sstevel@tonic-gate } 289*7c478bd9Sstevel@tonic-gate 290*7c478bd9Sstevel@tonic-gate out->nsc_errno = NSS_NOTFOUND; 291*7c478bd9Sstevel@tonic-gate out->nsc_return_code = NOTFOUND; 292*7c478bd9Sstevel@tonic-gate out->nsc_bufferbytesused = sizeof (*out); 293*7c478bd9Sstevel@tonic-gate goto getout; 294*7c478bd9Sstevel@tonic-gate } 295*7c478bd9Sstevel@tonic-gate bucket = get_hash(nam_hash, in->nsc_u.name); 296*7c478bd9Sstevel@tonic-gate } 297*7c478bd9Sstevel@tonic-gate 298*7c478bd9Sstevel@tonic-gate if (*bucket == (char *)-1) { /* pending lookup */ 299*7c478bd9Sstevel@tonic-gate if (get_clearance(in->nsc_callnumber) != 0) { 300*7c478bd9Sstevel@tonic-gate /* 301*7c478bd9Sstevel@tonic-gate * no threads available 302*7c478bd9Sstevel@tonic-gate * cannot process now 303*7c478bd9Sstevel@tonic-gate */ 304*7c478bd9Sstevel@tonic-gate out->nsc_return_code = NOSERVER; 305*7c478bd9Sstevel@tonic-gate out->nsc_bufferbytesused = sizeof (*out); 306*7c478bd9Sstevel@tonic-gate current_admin.group.nsc_throttle_count++; 307*7c478bd9Sstevel@tonic-gate goto getout; 308*7c478bd9Sstevel@tonic-gate } 309*7c478bd9Sstevel@tonic-gate nscd_wait(&group_wait, &group_lock, bucket); 310*7c478bd9Sstevel@tonic-gate release_clearance(in->nsc_callnumber); 311*7c478bd9Sstevel@tonic-gate continue; /* go back and relookup hash bucket */ 312*7c478bd9Sstevel@tonic-gate } 313*7c478bd9Sstevel@tonic-gate break; 314*7c478bd9Sstevel@tonic-gate } 315*7c478bd9Sstevel@tonic-gate 316*7c478bd9Sstevel@tonic-gate /* 317*7c478bd9Sstevel@tonic-gate * check for no name_service mode 318*7c478bd9Sstevel@tonic-gate */ 319*7c478bd9Sstevel@tonic-gate 320*7c478bd9Sstevel@tonic-gate if (*bucket == NULL && current_admin.avoid_nameservice) { 321*7c478bd9Sstevel@tonic-gate out->nsc_return_code = NOTFOUND; 322*7c478bd9Sstevel@tonic-gate out->nsc_bufferbytesused = sizeof (*out); 323*7c478bd9Sstevel@tonic-gate } else if ((*bucket == NULL) || /* New entry in name service */ 324*7c478bd9Sstevel@tonic-gate (in->nsc_callnumber & UPDATEBIT) || /* needs updating */ 325*7c478bd9Sstevel@tonic-gate (out_of_date = (!current_admin.avoid_nameservice && 326*7c478bd9Sstevel@tonic-gate (current_admin.group.nsc_old_data_ok == 0) && 327*7c478bd9Sstevel@tonic-gate (((nsc_bucket_t *)*bucket)->nsc_timestamp < now)))) { 328*7c478bd9Sstevel@tonic-gate /* time has expired */ 329*7c478bd9Sstevel@tonic-gate int saved_errno; 330*7c478bd9Sstevel@tonic-gate int saved_hits = 0; 331*7c478bd9Sstevel@tonic-gate struct group *p; 332*7c478bd9Sstevel@tonic-gate 333*7c478bd9Sstevel@tonic-gate if (get_clearance(in->nsc_callnumber) != 0) { 334*7c478bd9Sstevel@tonic-gate /* no threads available */ 335*7c478bd9Sstevel@tonic-gate out->nsc_return_code = NOSERVER; 336*7c478bd9Sstevel@tonic-gate /* cannot process now */ 337*7c478bd9Sstevel@tonic-gate out->nsc_bufferbytesused = sizeof (*out); 338*7c478bd9Sstevel@tonic-gate current_admin.group.nsc_throttle_count++; 339*7c478bd9Sstevel@tonic-gate goto getout; 340*7c478bd9Sstevel@tonic-gate } 341*7c478bd9Sstevel@tonic-gate 342*7c478bd9Sstevel@tonic-gate if (*bucket != NULL) { 343*7c478bd9Sstevel@tonic-gate saved_hits = ((nsc_bucket_t *)*bucket)->nsc_hits; 344*7c478bd9Sstevel@tonic-gate } 345*7c478bd9Sstevel@tonic-gate 346*7c478bd9Sstevel@tonic-gate /* 347*7c478bd9Sstevel@tonic-gate * block any threads accessing this bucket if data is 348*7c478bd9Sstevel@tonic-gate * non-existent out of date 349*7c478bd9Sstevel@tonic-gate */ 350*7c478bd9Sstevel@tonic-gate 351*7c478bd9Sstevel@tonic-gate if (*bucket == NULL || out_of_date) { 352*7c478bd9Sstevel@tonic-gate update_gr_bucket((nsc_bucket_t **)bucket, 353*7c478bd9Sstevel@tonic-gate (nsc_bucket_t *)-1, 354*7c478bd9Sstevel@tonic-gate in->nsc_callnumber); 355*7c478bd9Sstevel@tonic-gate } else { 356*7c478bd9Sstevel@tonic-gate /* 357*7c478bd9Sstevel@tonic-gate * if still not -1 bucket we are doing update... 358*7c478bd9Sstevel@tonic-gate * mark to prevent pileups of threads if the name 359*7c478bd9Sstevel@tonic-gate * service is hanging.... 360*7c478bd9Sstevel@tonic-gate */ 361*7c478bd9Sstevel@tonic-gate ((nsc_bucket_t *)(*bucket))->nsc_status |= 362*7c478bd9Sstevel@tonic-gate ST_UPDATE_PENDING; 363*7c478bd9Sstevel@tonic-gate /* cleared by deletion of old data */ 364*7c478bd9Sstevel@tonic-gate } 365*7c478bd9Sstevel@tonic-gate mutex_unlock(&group_lock); 366*7c478bd9Sstevel@tonic-gate 367*7c478bd9Sstevel@tonic-gate if (MASKUPDATEBIT(in->nsc_callnumber) == GETGRGID) { 368*7c478bd9Sstevel@tonic-gate p = _uncached_getgrgid_r(in->nsc_u.gid, &out->nsc_u.grp, 369*7c478bd9Sstevel@tonic-gate out->nsc_u.buff + sizeof (struct group), 370*7c478bd9Sstevel@tonic-gate bufferspace); 371*7c478bd9Sstevel@tonic-gate saved_errno = errno; 372*7c478bd9Sstevel@tonic-gate } else { 373*7c478bd9Sstevel@tonic-gate p = _uncached_getgrnam_r(in->nsc_u.name, 374*7c478bd9Sstevel@tonic-gate &out->nsc_u.grp, 375*7c478bd9Sstevel@tonic-gate out->nsc_u.buff + sizeof (struct group), 376*7c478bd9Sstevel@tonic-gate bufferspace); 377*7c478bd9Sstevel@tonic-gate saved_errno = errno; 378*7c478bd9Sstevel@tonic-gate } 379*7c478bd9Sstevel@tonic-gate 380*7c478bd9Sstevel@tonic-gate mutex_lock(&group_lock); 381*7c478bd9Sstevel@tonic-gate 382*7c478bd9Sstevel@tonic-gate release_clearance(in->nsc_callnumber); 383*7c478bd9Sstevel@tonic-gate 384*7c478bd9Sstevel@tonic-gate if (p == NULL) { /* data not found */ 385*7c478bd9Sstevel@tonic-gate if (current_admin.debug_level >= DBG_CANT_FIND) { 386*7c478bd9Sstevel@tonic-gate if (MASKUPDATEBIT(in->nsc_callnumber) == 387*7c478bd9Sstevel@tonic-gate GETGRGID) { 388*7c478bd9Sstevel@tonic-gate logit("getgr_lookup: nscd COULDN'T FIND gid %d\n", 389*7c478bd9Sstevel@tonic-gate in->nsc_u.gid); 390*7c478bd9Sstevel@tonic-gate } else { 391*7c478bd9Sstevel@tonic-gate logit("getgr_lookup: nscd COULDN'T FIND group name %s\n", 392*7c478bd9Sstevel@tonic-gate in->nsc_u.name); 393*7c478bd9Sstevel@tonic-gate } 394*7c478bd9Sstevel@tonic-gate } 395*7c478bd9Sstevel@tonic-gate 396*7c478bd9Sstevel@tonic-gate 397*7c478bd9Sstevel@tonic-gate if (!(UPDATEBIT & in->nsc_callnumber)) 398*7c478bd9Sstevel@tonic-gate current_admin.group.nsc_neg_cache_misses++; 399*7c478bd9Sstevel@tonic-gate 400*7c478bd9Sstevel@tonic-gate retb = (nsc_bucket_t *)malloc(sizeof (nsc_bucket_t)); 401*7c478bd9Sstevel@tonic-gate 402*7c478bd9Sstevel@tonic-gate retb->nsc_refcount = 1; 403*7c478bd9Sstevel@tonic-gate retb->nsc_data.nsc_bufferbytesused = 404*7c478bd9Sstevel@tonic-gate sizeof (nsc_return_t); 405*7c478bd9Sstevel@tonic-gate retb->nsc_data.nsc_return_code = NOTFOUND; 406*7c478bd9Sstevel@tonic-gate retb->nsc_data.nsc_errno = saved_errno; 407*7c478bd9Sstevel@tonic-gate memcpy(out, &retb->nsc_data, 408*7c478bd9Sstevel@tonic-gate retb->nsc_data.nsc_bufferbytesused); 409*7c478bd9Sstevel@tonic-gate update_gr_bucket((nsc_bucket_t **)bucket, 410*7c478bd9Sstevel@tonic-gate retb, 411*7c478bd9Sstevel@tonic-gate in->nsc_callnumber); 412*7c478bd9Sstevel@tonic-gate goto getout; 413*7c478bd9Sstevel@tonic-gate } else { 414*7c478bd9Sstevel@tonic-gate if (current_admin.debug_level >= DBG_ALL) { 415*7c478bd9Sstevel@tonic-gate if (MASKUPDATEBIT(in->nsc_callnumber) == 416*7c478bd9Sstevel@tonic-gate GETGRGID) { 417*7c478bd9Sstevel@tonic-gate logit("getgr_lookup: nscd FOUND gid %d\n", 418*7c478bd9Sstevel@tonic-gate in->nsc_u.gid); 419*7c478bd9Sstevel@tonic-gate } else { 420*7c478bd9Sstevel@tonic-gate logit("getgr_lookup: nscd FOUND group name %s\n", 421*7c478bd9Sstevel@tonic-gate in->nsc_u.name); 422*7c478bd9Sstevel@tonic-gate } 423*7c478bd9Sstevel@tonic-gate } 424*7c478bd9Sstevel@tonic-gate if (!(UPDATEBIT & in->nsc_callnumber)) 425*7c478bd9Sstevel@tonic-gate current_admin.group.nsc_pos_cache_misses++; 426*7c478bd9Sstevel@tonic-gate 427*7c478bd9Sstevel@tonic-gate retb = fixbuffer(out, bufferspace); 428*7c478bd9Sstevel@tonic-gate update_gr_bucket((nsc_bucket_t **)bucket, 429*7c478bd9Sstevel@tonic-gate retb, 430*7c478bd9Sstevel@tonic-gate in->nsc_callnumber); 431*7c478bd9Sstevel@tonic-gate if (saved_hits) 432*7c478bd9Sstevel@tonic-gate retb->nsc_hits = saved_hits; 433*7c478bd9Sstevel@tonic-gate } 434*7c478bd9Sstevel@tonic-gate } else { /* found entry in cache */ 435*7c478bd9Sstevel@tonic-gate retb = (nsc_bucket_t *)*bucket; 436*7c478bd9Sstevel@tonic-gate 437*7c478bd9Sstevel@tonic-gate retb->nsc_hits++; 438*7c478bd9Sstevel@tonic-gate 439*7c478bd9Sstevel@tonic-gate memcpy(out, &(retb->nsc_data), 440*7c478bd9Sstevel@tonic-gate retb->nsc_data.nsc_bufferbytesused); 441*7c478bd9Sstevel@tonic-gate 442*7c478bd9Sstevel@tonic-gate if (out->nsc_return_code == SUCCESS) { 443*7c478bd9Sstevel@tonic-gate if (!(UPDATEBIT & in->nsc_callnumber)) 444*7c478bd9Sstevel@tonic-gate current_admin.group.nsc_pos_cache_hits++; 445*7c478bd9Sstevel@tonic-gate if (current_admin.debug_level >= DBG_ALL) { 446*7c478bd9Sstevel@tonic-gate if (MASKUPDATEBIT(in->nsc_callnumber) == 447*7c478bd9Sstevel@tonic-gate GETGRGID) { 448*7c478bd9Sstevel@tonic-gate logit("getgr_lookup: found gid %d in cache\n", 449*7c478bd9Sstevel@tonic-gate in->nsc_u.gid); 450*7c478bd9Sstevel@tonic-gate } else { 451*7c478bd9Sstevel@tonic-gate logit("getgr_lookup: found name %s in cache\n", 452*7c478bd9Sstevel@tonic-gate in->nsc_u.name); 453*7c478bd9Sstevel@tonic-gate } 454*7c478bd9Sstevel@tonic-gate } 455*7c478bd9Sstevel@tonic-gate } else { 456*7c478bd9Sstevel@tonic-gate if (!(UPDATEBIT & in->nsc_callnumber)) 457*7c478bd9Sstevel@tonic-gate current_admin.group.nsc_neg_cache_hits++; 458*7c478bd9Sstevel@tonic-gate if (current_admin.debug_level >= DBG_ALL) { 459*7c478bd9Sstevel@tonic-gate if (MASKUPDATEBIT(in->nsc_callnumber) == 460*7c478bd9Sstevel@tonic-gate GETGRGID) { 461*7c478bd9Sstevel@tonic-gate logit("getgr_lookup: %d marked as NOT FOUND in cache.\n", 462*7c478bd9Sstevel@tonic-gate in->nsc_u.gid); 463*7c478bd9Sstevel@tonic-gate } else { 464*7c478bd9Sstevel@tonic-gate logit("getgr_lookup: %s marked as NOT FOUND in cache.\n", 465*7c478bd9Sstevel@tonic-gate in->nsc_u.name); 466*7c478bd9Sstevel@tonic-gate } 467*7c478bd9Sstevel@tonic-gate } 468*7c478bd9Sstevel@tonic-gate } 469*7c478bd9Sstevel@tonic-gate 470*7c478bd9Sstevel@tonic-gate if ((retb->nsc_timestamp < now) && 471*7c478bd9Sstevel@tonic-gate !(in->nsc_callnumber & UPDATEBIT) && 472*7c478bd9Sstevel@tonic-gate !(retb->nsc_status & ST_UPDATE_PENDING)) { 473*7c478bd9Sstevel@tonic-gate logit("launch update since time = %d\n", retb->nsc_timestamp); 474*7c478bd9Sstevel@tonic-gate retb->nsc_status |= ST_UPDATE_PENDING; 475*7c478bd9Sstevel@tonic-gate /* cleared by deletion of old data */ 476*7c478bd9Sstevel@tonic-gate launch_update(in); 477*7c478bd9Sstevel@tonic-gate } 478*7c478bd9Sstevel@tonic-gate } 479*7c478bd9Sstevel@tonic-gate 480*7c478bd9Sstevel@tonic-gate getout: 481*7c478bd9Sstevel@tonic-gate 482*7c478bd9Sstevel@tonic-gate mutex_unlock(&group_lock); 483*7c478bd9Sstevel@tonic-gate } 484*7c478bd9Sstevel@tonic-gate 485*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 486*7c478bd9Sstevel@tonic-gate static int 487*7c478bd9Sstevel@tonic-gate update_gr_bucket(nsc_bucket_t **old, nsc_bucket_t *new, int callnumber) 488*7c478bd9Sstevel@tonic-gate { 489*7c478bd9Sstevel@tonic-gate if (*old != NULL && *old != (nsc_bucket_t *)-1) { /* old data exists */ 490*7c478bd9Sstevel@tonic-gate free(*old); 491*7c478bd9Sstevel@tonic-gate current_admin.group.nsc_entries--; 492*7c478bd9Sstevel@tonic-gate } 493*7c478bd9Sstevel@tonic-gate 494*7c478bd9Sstevel@tonic-gate /* 495*7c478bd9Sstevel@tonic-gate * we can do this before reseting *old since we're holding the lock 496*7c478bd9Sstevel@tonic-gate */ 497*7c478bd9Sstevel@tonic-gate 498*7c478bd9Sstevel@tonic-gate else if (*old == (nsc_bucket_t *)-1) { 499*7c478bd9Sstevel@tonic-gate nscd_signal(&group_wait, (char **)old); 500*7c478bd9Sstevel@tonic-gate } 501*7c478bd9Sstevel@tonic-gate 502*7c478bd9Sstevel@tonic-gate 503*7c478bd9Sstevel@tonic-gate *old = new; 504*7c478bd9Sstevel@tonic-gate 505*7c478bd9Sstevel@tonic-gate if ((new != NULL) && (new != (nsc_bucket_t *)-1)) { 506*7c478bd9Sstevel@tonic-gate /* real data, not just update pending or invalidate */ 507*7c478bd9Sstevel@tonic-gate 508*7c478bd9Sstevel@tonic-gate new->nsc_hits = 1; 509*7c478bd9Sstevel@tonic-gate new->nsc_status = 0; 510*7c478bd9Sstevel@tonic-gate new->nsc_refcount = 1; 511*7c478bd9Sstevel@tonic-gate current_admin.group.nsc_entries++; 512*7c478bd9Sstevel@tonic-gate 513*7c478bd9Sstevel@tonic-gate if (new->nsc_data.nsc_return_code == SUCCESS) { 514*7c478bd9Sstevel@tonic-gate new->nsc_timestamp = time(NULL) + 515*7c478bd9Sstevel@tonic-gate current_admin.group.nsc_pos_ttl; 516*7c478bd9Sstevel@tonic-gate } else { 517*7c478bd9Sstevel@tonic-gate new->nsc_timestamp = time(NULL) + 518*7c478bd9Sstevel@tonic-gate current_admin.group.nsc_neg_ttl; 519*7c478bd9Sstevel@tonic-gate } 520*7c478bd9Sstevel@tonic-gate } 521*7c478bd9Sstevel@tonic-gate return (0); 522*7c478bd9Sstevel@tonic-gate } 523*7c478bd9Sstevel@tonic-gate 524*7c478bd9Sstevel@tonic-gate 525*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 526*7c478bd9Sstevel@tonic-gate static nsc_bucket_t * 527*7c478bd9Sstevel@tonic-gate fixbuffer(nsc_return_t *in, int maxlen) 528*7c478bd9Sstevel@tonic-gate { 529*7c478bd9Sstevel@tonic-gate int group_members; 530*7c478bd9Sstevel@tonic-gate int i; 531*7c478bd9Sstevel@tonic-gate nsc_bucket_t *retb; 532*7c478bd9Sstevel@tonic-gate nsc_return_t *out; 533*7c478bd9Sstevel@tonic-gate char *dest; 534*7c478bd9Sstevel@tonic-gate int offset; 535*7c478bd9Sstevel@tonic-gate int strs; 536*7c478bd9Sstevel@tonic-gate char **members; 537*7c478bd9Sstevel@tonic-gate int pwlen; 538*7c478bd9Sstevel@tonic-gate 539*7c478bd9Sstevel@tonic-gate /* 540*7c478bd9Sstevel@tonic-gate * find out the size of the data block we're going to need 541*7c478bd9Sstevel@tonic-gate */ 542*7c478bd9Sstevel@tonic-gate 543*7c478bd9Sstevel@tonic-gate strs = 0; 544*7c478bd9Sstevel@tonic-gate strs += 1 + strlen(in->nsc_u.grp.gr_name); 545*7c478bd9Sstevel@tonic-gate pwlen = strlen(in->nsc_u.grp.gr_passwd); 546*7c478bd9Sstevel@tonic-gate if (pwlen < 4) 547*7c478bd9Sstevel@tonic-gate pwlen = 4; 548*7c478bd9Sstevel@tonic-gate strs += 1 + pwlen; 549*7c478bd9Sstevel@tonic-gate 550*7c478bd9Sstevel@tonic-gate group_members = 0; 551*7c478bd9Sstevel@tonic-gate while (in->nsc_u.grp.gr_mem[group_members]) { 552*7c478bd9Sstevel@tonic-gate strs += 1 + strlen(in->nsc_u.grp.gr_mem[group_members]); 553*7c478bd9Sstevel@tonic-gate group_members++; 554*7c478bd9Sstevel@tonic-gate } 555*7c478bd9Sstevel@tonic-gate 556*7c478bd9Sstevel@tonic-gate strs += (group_members+1) * sizeof (char *); 557*7c478bd9Sstevel@tonic-gate 558*7c478bd9Sstevel@tonic-gate /* 559*7c478bd9Sstevel@tonic-gate * allocate it and copy it in 560*7c478bd9Sstevel@tonic-gate * code doesn't assume packing order in original buffer 561*7c478bd9Sstevel@tonic-gate */ 562*7c478bd9Sstevel@tonic-gate 563*7c478bd9Sstevel@tonic-gate if ((retb = (nsc_bucket_t *)malloc(sizeof (*retb) + strs)) == NULL) { 564*7c478bd9Sstevel@tonic-gate return (NULL); 565*7c478bd9Sstevel@tonic-gate } 566*7c478bd9Sstevel@tonic-gate 567*7c478bd9Sstevel@tonic-gate out = &(retb->nsc_data); 568*7c478bd9Sstevel@tonic-gate out->nsc_bufferbytesused = strs + ((int)&out->nsc_u.grp - (int)out) + 569*7c478bd9Sstevel@tonic-gate sizeof (struct group); 570*7c478bd9Sstevel@tonic-gate out->nsc_return_code = SUCCESS; 571*7c478bd9Sstevel@tonic-gate out->nsc_errno = 0; 572*7c478bd9Sstevel@tonic-gate 573*7c478bd9Sstevel@tonic-gate 574*7c478bd9Sstevel@tonic-gate out->nsc_u.grp.gr_gid = in->nsc_u.grp.gr_gid; 575*7c478bd9Sstevel@tonic-gate 576*7c478bd9Sstevel@tonic-gate dest = retb->nsc_data.nsc_u.buff + sizeof (struct group); 577*7c478bd9Sstevel@tonic-gate offset = (int)dest; 578*7c478bd9Sstevel@tonic-gate 579*7c478bd9Sstevel@tonic-gate members = (char **)dest; 580*7c478bd9Sstevel@tonic-gate out->nsc_u.grp.gr_mem = (char **)(dest - offset); 581*7c478bd9Sstevel@tonic-gate dest += (group_members+1) * sizeof (char *); 582*7c478bd9Sstevel@tonic-gate 583*7c478bd9Sstevel@tonic-gate 584*7c478bd9Sstevel@tonic-gate strcpy(dest, in->nsc_u.grp.gr_name); 585*7c478bd9Sstevel@tonic-gate strs = 1 + strlen(in->nsc_u.grp.gr_name); 586*7c478bd9Sstevel@tonic-gate out->nsc_u.grp.gr_name = dest - offset; 587*7c478bd9Sstevel@tonic-gate dest += strs; 588*7c478bd9Sstevel@tonic-gate 589*7c478bd9Sstevel@tonic-gate strcpy(dest, in->nsc_u.grp.gr_passwd); 590*7c478bd9Sstevel@tonic-gate strs = 1 + pwlen; 591*7c478bd9Sstevel@tonic-gate out->nsc_u.grp.gr_passwd = dest - offset; 592*7c478bd9Sstevel@tonic-gate dest += strs; 593*7c478bd9Sstevel@tonic-gate 594*7c478bd9Sstevel@tonic-gate for (i = 0; i < group_members; i++) { 595*7c478bd9Sstevel@tonic-gate members[i] = dest - offset; 596*7c478bd9Sstevel@tonic-gate strcpy(dest, in->nsc_u.grp.gr_mem[i]); 597*7c478bd9Sstevel@tonic-gate strs = 1 + strlen(in->nsc_u.grp.gr_mem[i]); 598*7c478bd9Sstevel@tonic-gate dest += strs; 599*7c478bd9Sstevel@tonic-gate } 600*7c478bd9Sstevel@tonic-gate members[i] = NULL; /* null terminate list */ 601*7c478bd9Sstevel@tonic-gate memcpy(in, out, out->nsc_bufferbytesused); 602*7c478bd9Sstevel@tonic-gate 603*7c478bd9Sstevel@tonic-gate return (retb); 604*7c478bd9Sstevel@tonic-gate } 605*7c478bd9Sstevel@tonic-gate 606*7c478bd9Sstevel@tonic-gate void 607*7c478bd9Sstevel@tonic-gate getgr_uid_reaper() 608*7c478bd9Sstevel@tonic-gate { 609*7c478bd9Sstevel@tonic-gate nsc_reaper("gr_uid", uid_hash, ¤t_admin.group, &group_lock); 610*7c478bd9Sstevel@tonic-gate } 611*7c478bd9Sstevel@tonic-gate 612*7c478bd9Sstevel@tonic-gate void 613*7c478bd9Sstevel@tonic-gate getgr_nam_reaper() 614*7c478bd9Sstevel@tonic-gate { 615*7c478bd9Sstevel@tonic-gate nsc_reaper("gr_nam", nam_hash, ¤t_admin.group, &group_lock); 616*7c478bd9Sstevel@tonic-gate } 617