10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*2830Sdjl * Common Development and Distribution License (the "License"). 6*2830Sdjl * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 220Sstevel@tonic-gate * getgrent.c 230Sstevel@tonic-gate * 24*2830Sdjl * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 250Sstevel@tonic-gate * Use is subject to license terms. 260Sstevel@tonic-gate * 270Sstevel@tonic-gate * lib/nsswitch/compat/getgrent.c -- name-service-switch backend for getgrnam() 280Sstevel@tonic-gate * et al that does 4.x compatibility. It looks in /etc/group; if it finds 290Sstevel@tonic-gate * group entries there that begin with "+" or "-", it consults other 300Sstevel@tonic-gate * services. By default it uses NIS (YP), but the user can override this 310Sstevel@tonic-gate * with a "group_compat" entry in /etc/nsswitch.conf, e.g. 320Sstevel@tonic-gate * group_compat: nisplus 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * This code tries to produce the same results as the 4.x code, even when 350Sstevel@tonic-gate * the latter seems ill thought-out. Bug-compatible, in other words. 360Sstevel@tonic-gate * Though we do try to be more reasonable about the format of "+" and "-" 370Sstevel@tonic-gate * entries here, i.e. you don't have to pad them with spurious colons and 380Sstevel@tonic-gate * bogus uid/gid values. 390Sstevel@tonic-gate * 400Sstevel@tonic-gate * Caveats: 410Sstevel@tonic-gate * - More than one source may be specified, with the usual switch semantics, 420Sstevel@tonic-gate * but having multiple sources here is definitely odd. 430Sstevel@tonic-gate * - People who recursively specify "compat" deserve what they get. 440Sstevel@tonic-gate */ 450Sstevel@tonic-gate 460Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 470Sstevel@tonic-gate 480Sstevel@tonic-gate #include <grp.h> 490Sstevel@tonic-gate #include <stdlib.h> 500Sstevel@tonic-gate #include <unistd.h> /* for GF_PATH */ 510Sstevel@tonic-gate #include <strings.h> 520Sstevel@tonic-gate #include "compat_common.h" 530Sstevel@tonic-gate 540Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root); 550Sstevel@tonic-gate 560Sstevel@tonic-gate static void 570Sstevel@tonic-gate _nss_initf_group_compat(p) 580Sstevel@tonic-gate nss_db_params_t *p; 590Sstevel@tonic-gate { 600Sstevel@tonic-gate p->name = NSS_DBNAM_GROUP; 610Sstevel@tonic-gate p->config_name = NSS_DBNAM_GROUP_COMPAT; 620Sstevel@tonic-gate p->default_config = NSS_DEFCONF_GROUP_COMPAT; 630Sstevel@tonic-gate } 640Sstevel@tonic-gate 650Sstevel@tonic-gate static const char * 660Sstevel@tonic-gate get_grname(argp) 670Sstevel@tonic-gate nss_XbyY_args_t *argp; 680Sstevel@tonic-gate { 690Sstevel@tonic-gate struct group *g = (struct group *)argp->returnval; 700Sstevel@tonic-gate 710Sstevel@tonic-gate return (g->gr_name); 720Sstevel@tonic-gate } 730Sstevel@tonic-gate 740Sstevel@tonic-gate static int 750Sstevel@tonic-gate check_grname(argp) 760Sstevel@tonic-gate nss_XbyY_args_t *argp; 770Sstevel@tonic-gate { 780Sstevel@tonic-gate struct group *g = (struct group *)argp->returnval; 790Sstevel@tonic-gate 800Sstevel@tonic-gate return (strcmp(g->gr_name, argp->key.name) == 0); 810Sstevel@tonic-gate } 820Sstevel@tonic-gate 830Sstevel@tonic-gate static nss_status_t 840Sstevel@tonic-gate getbyname(be, a) 850Sstevel@tonic-gate compat_backend_ptr_t be; 860Sstevel@tonic-gate void *a; 870Sstevel@tonic-gate { 880Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 890Sstevel@tonic-gate 900Sstevel@tonic-gate return (_nss_compat_XY_all(be, argp, check_grname, 910Sstevel@tonic-gate NSS_DBOP_GROUP_BYNAME)); 920Sstevel@tonic-gate } 930Sstevel@tonic-gate 940Sstevel@tonic-gate static int 950Sstevel@tonic-gate check_grgid(argp) 960Sstevel@tonic-gate nss_XbyY_args_t *argp; 970Sstevel@tonic-gate { 980Sstevel@tonic-gate struct group *g = (struct group *)argp->returnval; 990Sstevel@tonic-gate 1000Sstevel@tonic-gate return (g->gr_gid == argp->key.gid); 1010Sstevel@tonic-gate } 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate static nss_status_t 1040Sstevel@tonic-gate getbygid(be, a) 1050Sstevel@tonic-gate compat_backend_ptr_t be; 1060Sstevel@tonic-gate void *a; 1070Sstevel@tonic-gate { 1080Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate return (_nss_compat_XY_all(be, argp, check_grgid, 1110Sstevel@tonic-gate NSS_DBOP_GROUP_BYGID)); 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate static nss_status_t 1150Sstevel@tonic-gate getbymember(be, a) 1160Sstevel@tonic-gate compat_backend_ptr_t be; 1170Sstevel@tonic-gate void *a; 1180Sstevel@tonic-gate { 1190Sstevel@tonic-gate struct nss_groupsbymem *argp = (struct nss_groupsbymem *)a; 1200Sstevel@tonic-gate int numgids = argp->numgids; 1210Sstevel@tonic-gate int maxgids = argp->maxgids; 1220Sstevel@tonic-gate gid_t *gid_array = argp->gid_array; 1230Sstevel@tonic-gate struct nss_XbyY_args grargs; 1240Sstevel@tonic-gate struct group *g; 1250Sstevel@tonic-gate nss_XbyY_buf_t *gb = NULL, *b = NULL; 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate /* 1280Sstevel@tonic-gate * Generic implementation: enumerate using getent(), then check each 1290Sstevel@tonic-gate * group returned by getent() to see whether it contains the user. 1300Sstevel@tonic-gate * There are much faster ways, but at least this one gets the right 1310Sstevel@tonic-gate * answer. 1320Sstevel@tonic-gate */ 1330Sstevel@tonic-gate if (numgids >= maxgids) { 1340Sstevel@tonic-gate /* full gid_array; nobody should have bothered to call us */ 1350Sstevel@tonic-gate return (NSS_SUCCESS); 1360Sstevel@tonic-gate } 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate b = NSS_XbyY_ALLOC(&gb, sizeof (struct group), NSS_BUFLEN_GROUP); 1390Sstevel@tonic-gate if (b == 0) 1400Sstevel@tonic-gate return (NSS_UNAVAIL); 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate NSS_XbyY_INIT(&grargs, gb->result, gb->buffer, gb->buflen, 1430Sstevel@tonic-gate argp->str2ent); 1440Sstevel@tonic-gate g = (struct group *)gb->result; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate (void) _nss_compat_setent(be, 0); 1470Sstevel@tonic-gate while (_nss_compat_getent(be, &grargs) == NSS_SUCCESS) { 1480Sstevel@tonic-gate char **mem; 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate if (grargs.returnval == 0) { 1510Sstevel@tonic-gate continue; 1520Sstevel@tonic-gate } 1530Sstevel@tonic-gate for (mem = g->gr_mem; *mem != 0; mem++) { 1540Sstevel@tonic-gate if (strcmp(*mem, argp->username) == 0) { 1550Sstevel@tonic-gate int gid = g->gr_gid; 1560Sstevel@tonic-gate int i; 1570Sstevel@tonic-gate for (i = 0; i < numgids; i++) { 1580Sstevel@tonic-gate if (gid == gid_array[i]) { 1590Sstevel@tonic-gate break; 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate } 1620Sstevel@tonic-gate if (i == numgids) { 1630Sstevel@tonic-gate gid_array[numgids++] = gid; 1640Sstevel@tonic-gate argp->numgids = numgids; 1650Sstevel@tonic-gate if (numgids >= maxgids) { 1660Sstevel@tonic-gate /* filled the gid_array */ 1670Sstevel@tonic-gate (void) _nss_compat_endent(be, 1680Sstevel@tonic-gate 0); 1690Sstevel@tonic-gate NSS_XbyY_FREE(&gb); 1700Sstevel@tonic-gate return (NSS_SUCCESS); 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate /* Done with this group, try next */ 1730Sstevel@tonic-gate break; 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate } 1780Sstevel@tonic-gate (void) _nss_compat_endent(be, 0); 1790Sstevel@tonic-gate NSS_XbyY_FREE(&gb); 1800Sstevel@tonic-gate return (NSS_NOTFOUND); /* Really means "gid_array not full yet" */ 1810Sstevel@tonic-gate } 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate /*ARGSUSED*/ 1840Sstevel@tonic-gate static int 1850Sstevel@tonic-gate merge_grents(be, argp, fields) 1860Sstevel@tonic-gate compat_backend_ptr_t be; 1870Sstevel@tonic-gate nss_XbyY_args_t *argp; 1880Sstevel@tonic-gate const char **fields; 1890Sstevel@tonic-gate { 1900Sstevel@tonic-gate struct group *g = (struct group *)argp->buf.result; 1910Sstevel@tonic-gate char *buf; 1920Sstevel@tonic-gate char *s; 1930Sstevel@tonic-gate int parsestat; 194*2830Sdjl int dlen; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate /* 1970Sstevel@tonic-gate * We're allowed to override the passwd (has anyone ever actually used 1980Sstevel@tonic-gate * the passwd in a group entry?) and the membership list, but not 1990Sstevel@tonic-gate * the groupname or the gid. 2000Sstevel@tonic-gate * That's what the SunOS 4.x code did; who are we to question it... 2010Sstevel@tonic-gate * 2020Sstevel@tonic-gate * Efficiency is heartlessly abandoned in the quest for simplicity. 2030Sstevel@tonic-gate */ 204*2830Sdjl if (fields[1] == 0 && fields[3] == 0 && 205*2830Sdjl be->return_string_data != 1) { 2060Sstevel@tonic-gate /* No legal overrides, leave *argp unscathed */ 2070Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate if ((buf = malloc(NSS_LINELEN_GROUP)) == 0) { 2100Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 2110Sstevel@tonic-gate /* Really "out of memory", but PARSE_PARSE will have to do */ 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate s = buf; 2140Sstevel@tonic-gate (void) snprintf(s, NSS_LINELEN_GROUP, "%s:%s:%d:", 2150Sstevel@tonic-gate g->gr_name, 2160Sstevel@tonic-gate fields[1] != 0 ? fields[1] : g->gr_passwd, 2170Sstevel@tonic-gate g->gr_gid); 2180Sstevel@tonic-gate s += strlen(s); 2190Sstevel@tonic-gate if (fields[3] != 0) { 220*2830Sdjl (void) strcpy(s, fields[3]); 2210Sstevel@tonic-gate s += strlen(s); 2220Sstevel@tonic-gate } else { 2230Sstevel@tonic-gate char **memp; 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate for (memp = g->gr_mem; *memp != 0; memp++) { 2260Sstevel@tonic-gate size_t len = strlen(*memp); 2270Sstevel@tonic-gate if (s + len + 1 <= buf + NSS_LINELEN_GROUP) { 2280Sstevel@tonic-gate if (memp != g->gr_mem) { 2290Sstevel@tonic-gate *s++ = ','; 2300Sstevel@tonic-gate } 2310Sstevel@tonic-gate (void) memcpy(s, *memp, len); 2320Sstevel@tonic-gate s += len; 2330Sstevel@tonic-gate } else { 2340Sstevel@tonic-gate free(buf); 2350Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 2360Sstevel@tonic-gate } 2370Sstevel@tonic-gate } 2380Sstevel@tonic-gate } 239*2830Sdjl 240*2830Sdjl dlen = s - buf; 241*2830Sdjl 242*2830Sdjl /* 243*2830Sdjl * if asked, return the data in /etc file format 244*2830Sdjl */ 245*2830Sdjl if (be->return_string_data == 1) { 246*2830Sdjl /* reset the result ptr to the original value */ 247*2830Sdjl argp->buf.result = NULL; 248*2830Sdjl 249*2830Sdjl if (dlen > argp->buf.buflen) { 250*2830Sdjl parsestat = NSS_STR_PARSE_ERANGE; 251*2830Sdjl } else { 252*2830Sdjl (void) strncpy(argp->buf.buffer, buf, dlen); 253*2830Sdjl argp->returnval = argp->buf.buffer; 254*2830Sdjl argp->returnlen = dlen; 255*2830Sdjl parsestat = NSS_SUCCESS; 256*2830Sdjl } 257*2830Sdjl } else { 258*2830Sdjl parsestat = (*argp->str2ent)(buf, dlen, 2590Sstevel@tonic-gate argp->buf.result, 2600Sstevel@tonic-gate argp->buf.buffer, 2610Sstevel@tonic-gate argp->buf.buflen); 262*2830Sdjl } 263*2830Sdjl 2640Sstevel@tonic-gate free(buf); 2650Sstevel@tonic-gate return (parsestat); 2660Sstevel@tonic-gate } 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate static compat_backend_op_t group_ops[] = { 2690Sstevel@tonic-gate _nss_compat_destr, 2700Sstevel@tonic-gate _nss_compat_endent, 2710Sstevel@tonic-gate _nss_compat_setent, 2720Sstevel@tonic-gate _nss_compat_getent, 2730Sstevel@tonic-gate getbyname, 2740Sstevel@tonic-gate getbygid, 2750Sstevel@tonic-gate getbymember 2760Sstevel@tonic-gate }; 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate /*ARGSUSED*/ 2790Sstevel@tonic-gate nss_backend_t * 2800Sstevel@tonic-gate _nss_compat_group_constr(dummy1, dummy2, dummy3) 2810Sstevel@tonic-gate const char *dummy1, *dummy2, *dummy3; 2820Sstevel@tonic-gate { 2830Sstevel@tonic-gate return (_nss_compat_constr(group_ops, 2840Sstevel@tonic-gate sizeof (group_ops) / sizeof (group_ops[0]), 2850Sstevel@tonic-gate GF_PATH, 2860Sstevel@tonic-gate NSS_LINELEN_GROUP, 2870Sstevel@tonic-gate &db_root, 2880Sstevel@tonic-gate _nss_initf_group_compat, 2890Sstevel@tonic-gate 0, 2900Sstevel@tonic-gate get_grname, 2910Sstevel@tonic-gate merge_grents)); 2920Sstevel@tonic-gate } 293