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 52830Sdjl * Common Development and Distribution License (the "License"). 62830Sdjl * 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*8040SBaban.Kenkre@Sun.COM * Copyright 2008 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 #include <grp.h> 470Sstevel@tonic-gate #include <stdlib.h> 480Sstevel@tonic-gate #include <unistd.h> /* for GF_PATH */ 490Sstevel@tonic-gate #include <strings.h> 500Sstevel@tonic-gate #include "compat_common.h" 510Sstevel@tonic-gate 520Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root); 530Sstevel@tonic-gate 540Sstevel@tonic-gate static void 550Sstevel@tonic-gate _nss_initf_group_compat(p) 560Sstevel@tonic-gate nss_db_params_t *p; 570Sstevel@tonic-gate { 580Sstevel@tonic-gate p->name = NSS_DBNAM_GROUP; 590Sstevel@tonic-gate p->config_name = NSS_DBNAM_GROUP_COMPAT; 600Sstevel@tonic-gate p->default_config = NSS_DEFCONF_GROUP_COMPAT; 610Sstevel@tonic-gate } 620Sstevel@tonic-gate 63*8040SBaban.Kenkre@Sun.COM /* 64*8040SBaban.Kenkre@Sun.COM * Validates group entry replacing gid > MAXUID by GID_NOBODY. 65*8040SBaban.Kenkre@Sun.COM */ 66*8040SBaban.Kenkre@Sun.COM int 67*8040SBaban.Kenkre@Sun.COM validate_group_ids(char *line, int *linelenp, int buflen, int extra_chars) 68*8040SBaban.Kenkre@Sun.COM { 69*8040SBaban.Kenkre@Sun.COM char *linep, *limit, *gidp; 70*8040SBaban.Kenkre@Sun.COM ulong_t gid; 71*8040SBaban.Kenkre@Sun.COM int oldgidlen, idlen; 72*8040SBaban.Kenkre@Sun.COM int linelen = *linelenp, newlinelen; 73*8040SBaban.Kenkre@Sun.COM 74*8040SBaban.Kenkre@Sun.COM if (linelen == 0 || *line == '+' || *line == '-') 75*8040SBaban.Kenkre@Sun.COM return (NSS_STR_PARSE_SUCCESS); 76*8040SBaban.Kenkre@Sun.COM 77*8040SBaban.Kenkre@Sun.COM linep = line; 78*8040SBaban.Kenkre@Sun.COM limit = line + linelen; 79*8040SBaban.Kenkre@Sun.COM 80*8040SBaban.Kenkre@Sun.COM while (linep < limit && *linep++ != ':') /* skip groupname */ 81*8040SBaban.Kenkre@Sun.COM continue; 82*8040SBaban.Kenkre@Sun.COM while (linep < limit && *linep++ != ':') /* skip password */ 83*8040SBaban.Kenkre@Sun.COM continue; 84*8040SBaban.Kenkre@Sun.COM if (linep == limit) 85*8040SBaban.Kenkre@Sun.COM return (NSS_STR_PARSE_PARSE); 86*8040SBaban.Kenkre@Sun.COM 87*8040SBaban.Kenkre@Sun.COM gidp = linep; 88*8040SBaban.Kenkre@Sun.COM gid = strtoul(gidp, (char **)&linep, 10); /* grab gid */ 89*8040SBaban.Kenkre@Sun.COM oldgidlen = linep - gidp; 90*8040SBaban.Kenkre@Sun.COM if (linep >= limit || oldgidlen == 0) 91*8040SBaban.Kenkre@Sun.COM return (NSS_STR_PARSE_PARSE); 92*8040SBaban.Kenkre@Sun.COM 93*8040SBaban.Kenkre@Sun.COM if (gid <= MAXUID) 94*8040SBaban.Kenkre@Sun.COM return (NSS_STR_PARSE_SUCCESS); 95*8040SBaban.Kenkre@Sun.COM 96*8040SBaban.Kenkre@Sun.COM idlen = snprintf(NULL, 0, "%u", GID_NOBODY); 97*8040SBaban.Kenkre@Sun.COM newlinelen = linelen + idlen - oldgidlen; 98*8040SBaban.Kenkre@Sun.COM if (newlinelen + extra_chars > buflen) 99*8040SBaban.Kenkre@Sun.COM return (NSS_STR_PARSE_ERANGE); 100*8040SBaban.Kenkre@Sun.COM 101*8040SBaban.Kenkre@Sun.COM (void) bcopy(linep, gidp + idlen, limit - linep + extra_chars); 102*8040SBaban.Kenkre@Sun.COM (void) snprintf(gidp, idlen + 1, "%u", GID_NOBODY); 103*8040SBaban.Kenkre@Sun.COM *(gidp + idlen) = ':'; 104*8040SBaban.Kenkre@Sun.COM *linelenp = newlinelen; 105*8040SBaban.Kenkre@Sun.COM return (NSS_STR_PARSE_SUCCESS); 106*8040SBaban.Kenkre@Sun.COM } 107*8040SBaban.Kenkre@Sun.COM 1080Sstevel@tonic-gate static const char * 1090Sstevel@tonic-gate get_grname(argp) 1100Sstevel@tonic-gate nss_XbyY_args_t *argp; 1110Sstevel@tonic-gate { 1120Sstevel@tonic-gate struct group *g = (struct group *)argp->returnval; 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate return (g->gr_name); 1150Sstevel@tonic-gate } 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate static int 1180Sstevel@tonic-gate check_grname(argp) 1190Sstevel@tonic-gate nss_XbyY_args_t *argp; 1200Sstevel@tonic-gate { 1210Sstevel@tonic-gate struct group *g = (struct group *)argp->returnval; 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate return (strcmp(g->gr_name, argp->key.name) == 0); 1240Sstevel@tonic-gate } 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate static nss_status_t 1270Sstevel@tonic-gate getbyname(be, a) 1280Sstevel@tonic-gate compat_backend_ptr_t be; 1290Sstevel@tonic-gate void *a; 1300Sstevel@tonic-gate { 1310Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate return (_nss_compat_XY_all(be, argp, check_grname, 1340Sstevel@tonic-gate NSS_DBOP_GROUP_BYNAME)); 1350Sstevel@tonic-gate } 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate static int 1380Sstevel@tonic-gate check_grgid(argp) 1390Sstevel@tonic-gate nss_XbyY_args_t *argp; 1400Sstevel@tonic-gate { 1410Sstevel@tonic-gate struct group *g = (struct group *)argp->returnval; 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate return (g->gr_gid == argp->key.gid); 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate static nss_status_t 1470Sstevel@tonic-gate getbygid(be, a) 1480Sstevel@tonic-gate compat_backend_ptr_t be; 1490Sstevel@tonic-gate void *a; 1500Sstevel@tonic-gate { 1510Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 1520Sstevel@tonic-gate 153*8040SBaban.Kenkre@Sun.COM if (argp->key.gid > MAXUID) 154*8040SBaban.Kenkre@Sun.COM return (NSS_NOTFOUND); 1550Sstevel@tonic-gate return (_nss_compat_XY_all(be, argp, check_grgid, 1560Sstevel@tonic-gate NSS_DBOP_GROUP_BYGID)); 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate static nss_status_t 1600Sstevel@tonic-gate getbymember(be, a) 1610Sstevel@tonic-gate compat_backend_ptr_t be; 1620Sstevel@tonic-gate void *a; 1630Sstevel@tonic-gate { 1640Sstevel@tonic-gate struct nss_groupsbymem *argp = (struct nss_groupsbymem *)a; 1650Sstevel@tonic-gate int numgids = argp->numgids; 1660Sstevel@tonic-gate int maxgids = argp->maxgids; 1670Sstevel@tonic-gate gid_t *gid_array = argp->gid_array; 1680Sstevel@tonic-gate struct nss_XbyY_args grargs; 1690Sstevel@tonic-gate struct group *g; 1700Sstevel@tonic-gate nss_XbyY_buf_t *gb = NULL, *b = NULL; 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate /* 1730Sstevel@tonic-gate * Generic implementation: enumerate using getent(), then check each 1740Sstevel@tonic-gate * group returned by getent() to see whether it contains the user. 1750Sstevel@tonic-gate * There are much faster ways, but at least this one gets the right 1760Sstevel@tonic-gate * answer. 1770Sstevel@tonic-gate */ 1780Sstevel@tonic-gate if (numgids >= maxgids) { 1790Sstevel@tonic-gate /* full gid_array; nobody should have bothered to call us */ 1800Sstevel@tonic-gate return (NSS_SUCCESS); 1810Sstevel@tonic-gate } 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate b = NSS_XbyY_ALLOC(&gb, sizeof (struct group), NSS_BUFLEN_GROUP); 1840Sstevel@tonic-gate if (b == 0) 1850Sstevel@tonic-gate return (NSS_UNAVAIL); 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate NSS_XbyY_INIT(&grargs, gb->result, gb->buffer, gb->buflen, 1880Sstevel@tonic-gate argp->str2ent); 1890Sstevel@tonic-gate g = (struct group *)gb->result; 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate (void) _nss_compat_setent(be, 0); 1920Sstevel@tonic-gate while (_nss_compat_getent(be, &grargs) == NSS_SUCCESS) { 1930Sstevel@tonic-gate char **mem; 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate if (grargs.returnval == 0) { 1960Sstevel@tonic-gate continue; 1970Sstevel@tonic-gate } 1980Sstevel@tonic-gate for (mem = g->gr_mem; *mem != 0; mem++) { 1990Sstevel@tonic-gate if (strcmp(*mem, argp->username) == 0) { 2000Sstevel@tonic-gate int gid = g->gr_gid; 2010Sstevel@tonic-gate int i; 2020Sstevel@tonic-gate for (i = 0; i < numgids; i++) { 2030Sstevel@tonic-gate if (gid == gid_array[i]) { 2040Sstevel@tonic-gate break; 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate if (i == numgids) { 2080Sstevel@tonic-gate gid_array[numgids++] = gid; 2090Sstevel@tonic-gate argp->numgids = numgids; 2100Sstevel@tonic-gate if (numgids >= maxgids) { 2110Sstevel@tonic-gate /* filled the gid_array */ 2120Sstevel@tonic-gate (void) _nss_compat_endent(be, 2130Sstevel@tonic-gate 0); 2140Sstevel@tonic-gate NSS_XbyY_FREE(&gb); 2150Sstevel@tonic-gate return (NSS_SUCCESS); 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate /* Done with this group, try next */ 2180Sstevel@tonic-gate break; 2190Sstevel@tonic-gate } 2200Sstevel@tonic-gate } 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate } 2230Sstevel@tonic-gate (void) _nss_compat_endent(be, 0); 2240Sstevel@tonic-gate NSS_XbyY_FREE(&gb); 2250Sstevel@tonic-gate return (NSS_NOTFOUND); /* Really means "gid_array not full yet" */ 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate /*ARGSUSED*/ 2290Sstevel@tonic-gate static int 2300Sstevel@tonic-gate merge_grents(be, argp, fields) 2310Sstevel@tonic-gate compat_backend_ptr_t be; 2320Sstevel@tonic-gate nss_XbyY_args_t *argp; 2330Sstevel@tonic-gate const char **fields; 2340Sstevel@tonic-gate { 2350Sstevel@tonic-gate struct group *g = (struct group *)argp->buf.result; 2360Sstevel@tonic-gate char *buf; 2370Sstevel@tonic-gate char *s; 2380Sstevel@tonic-gate int parsestat; 2392830Sdjl int dlen; 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate /* 2420Sstevel@tonic-gate * We're allowed to override the passwd (has anyone ever actually used 2430Sstevel@tonic-gate * the passwd in a group entry?) and the membership list, but not 2440Sstevel@tonic-gate * the groupname or the gid. 2450Sstevel@tonic-gate * That's what the SunOS 4.x code did; who are we to question it... 2460Sstevel@tonic-gate * 2470Sstevel@tonic-gate * Efficiency is heartlessly abandoned in the quest for simplicity. 2480Sstevel@tonic-gate */ 2492830Sdjl if (fields[1] == 0 && fields[3] == 0 && 2502830Sdjl be->return_string_data != 1) { 2510Sstevel@tonic-gate /* No legal overrides, leave *argp unscathed */ 2520Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 2530Sstevel@tonic-gate } 2540Sstevel@tonic-gate if ((buf = malloc(NSS_LINELEN_GROUP)) == 0) { 2550Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 2560Sstevel@tonic-gate /* Really "out of memory", but PARSE_PARSE will have to do */ 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate s = buf; 259*8040SBaban.Kenkre@Sun.COM (void) snprintf(s, NSS_LINELEN_GROUP, "%s:%s:%u:", 2600Sstevel@tonic-gate g->gr_name, 2610Sstevel@tonic-gate fields[1] != 0 ? fields[1] : g->gr_passwd, 2620Sstevel@tonic-gate g->gr_gid); 2630Sstevel@tonic-gate s += strlen(s); 2640Sstevel@tonic-gate if (fields[3] != 0) { 2652830Sdjl (void) strcpy(s, fields[3]); 2660Sstevel@tonic-gate s += strlen(s); 2670Sstevel@tonic-gate } else { 2680Sstevel@tonic-gate char **memp; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate for (memp = g->gr_mem; *memp != 0; memp++) { 2710Sstevel@tonic-gate size_t len = strlen(*memp); 2720Sstevel@tonic-gate if (s + len + 1 <= buf + NSS_LINELEN_GROUP) { 2730Sstevel@tonic-gate if (memp != g->gr_mem) { 2740Sstevel@tonic-gate *s++ = ','; 2750Sstevel@tonic-gate } 2760Sstevel@tonic-gate (void) memcpy(s, *memp, len); 2770Sstevel@tonic-gate s += len; 2780Sstevel@tonic-gate } else { 2790Sstevel@tonic-gate free(buf); 2800Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 2810Sstevel@tonic-gate } 2820Sstevel@tonic-gate } 2830Sstevel@tonic-gate } 2842830Sdjl 2852830Sdjl dlen = s - buf; 2862830Sdjl 2872830Sdjl /* 2882830Sdjl * if asked, return the data in /etc file format 2892830Sdjl */ 2902830Sdjl if (be->return_string_data == 1) { 2912830Sdjl /* reset the result ptr to the original value */ 2922830Sdjl argp->buf.result = NULL; 2932830Sdjl 2942830Sdjl if (dlen > argp->buf.buflen) { 2952830Sdjl parsestat = NSS_STR_PARSE_ERANGE; 2962830Sdjl } else { 2972830Sdjl (void) strncpy(argp->buf.buffer, buf, dlen); 2982830Sdjl argp->returnval = argp->buf.buffer; 2992830Sdjl argp->returnlen = dlen; 3002830Sdjl parsestat = NSS_SUCCESS; 3012830Sdjl } 3022830Sdjl } else { 3032830Sdjl parsestat = (*argp->str2ent)(buf, dlen, 3040Sstevel@tonic-gate argp->buf.result, 3050Sstevel@tonic-gate argp->buf.buffer, 3060Sstevel@tonic-gate argp->buf.buflen); 3072830Sdjl } 3082830Sdjl 3090Sstevel@tonic-gate free(buf); 3100Sstevel@tonic-gate return (parsestat); 3110Sstevel@tonic-gate } 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate static compat_backend_op_t group_ops[] = { 3140Sstevel@tonic-gate _nss_compat_destr, 3150Sstevel@tonic-gate _nss_compat_endent, 3160Sstevel@tonic-gate _nss_compat_setent, 3170Sstevel@tonic-gate _nss_compat_getent, 3180Sstevel@tonic-gate getbyname, 3190Sstevel@tonic-gate getbygid, 3200Sstevel@tonic-gate getbymember 3210Sstevel@tonic-gate }; 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate /*ARGSUSED*/ 3240Sstevel@tonic-gate nss_backend_t * 3250Sstevel@tonic-gate _nss_compat_group_constr(dummy1, dummy2, dummy3) 3260Sstevel@tonic-gate const char *dummy1, *dummy2, *dummy3; 3270Sstevel@tonic-gate { 3280Sstevel@tonic-gate return (_nss_compat_constr(group_ops, 3290Sstevel@tonic-gate sizeof (group_ops) / sizeof (group_ops[0]), 3300Sstevel@tonic-gate GF_PATH, 3310Sstevel@tonic-gate NSS_LINELEN_GROUP, 3320Sstevel@tonic-gate &db_root, 3330Sstevel@tonic-gate _nss_initf_group_compat, 3340Sstevel@tonic-gate 0, 3350Sstevel@tonic-gate get_grname, 3360Sstevel@tonic-gate merge_grents)); 3370Sstevel@tonic-gate } 338