17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5cb5caa98Sdjl * Common Development and Distribution License (the "License"). 6cb5caa98Sdjl * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 227c478bd9Sstevel@tonic-gate * getgrent.c 237c478bd9Sstevel@tonic-gate * 24*36e852a1SRaja Andra * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate * Use is subject to license terms. 267c478bd9Sstevel@tonic-gate * 277c478bd9Sstevel@tonic-gate * lib/nsswitch/compat/getgrent.c -- name-service-switch backend for getgrnam() 287c478bd9Sstevel@tonic-gate * et al that does 4.x compatibility. It looks in /etc/group; if it finds 297c478bd9Sstevel@tonic-gate * group entries there that begin with "+" or "-", it consults other 307c478bd9Sstevel@tonic-gate * services. By default it uses NIS (YP), but the user can override this 317c478bd9Sstevel@tonic-gate * with a "group_compat" entry in /etc/nsswitch.conf, e.g. 32*36e852a1SRaja Andra * group_compat: ldap 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * This code tries to produce the same results as the 4.x code, even when 357c478bd9Sstevel@tonic-gate * the latter seems ill thought-out. Bug-compatible, in other words. 367c478bd9Sstevel@tonic-gate * Though we do try to be more reasonable about the format of "+" and "-" 377c478bd9Sstevel@tonic-gate * entries here, i.e. you don't have to pad them with spurious colons and 387c478bd9Sstevel@tonic-gate * bogus uid/gid values. 397c478bd9Sstevel@tonic-gate * 407c478bd9Sstevel@tonic-gate * Caveats: 417c478bd9Sstevel@tonic-gate * - More than one source may be specified, with the usual switch semantics, 427c478bd9Sstevel@tonic-gate * but having multiple sources here is definitely odd. 437c478bd9Sstevel@tonic-gate * - People who recursively specify "compat" deserve what they get. 447c478bd9Sstevel@tonic-gate */ 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate #include <grp.h> 477c478bd9Sstevel@tonic-gate #include <stdlib.h> 487c478bd9Sstevel@tonic-gate #include <unistd.h> /* for GF_PATH */ 497c478bd9Sstevel@tonic-gate #include <strings.h> 507c478bd9Sstevel@tonic-gate #include "compat_common.h" 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root); 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate static void 557c478bd9Sstevel@tonic-gate _nss_initf_group_compat(p) 567c478bd9Sstevel@tonic-gate nss_db_params_t *p; 577c478bd9Sstevel@tonic-gate { 587c478bd9Sstevel@tonic-gate p->name = NSS_DBNAM_GROUP; 597c478bd9Sstevel@tonic-gate p->config_name = NSS_DBNAM_GROUP_COMPAT; 607c478bd9Sstevel@tonic-gate p->default_config = NSS_DEFCONF_GROUP_COMPAT; 617c478bd9Sstevel@tonic-gate } 627c478bd9Sstevel@tonic-gate 632b4a7802SBaban Kenkre /* 642b4a7802SBaban Kenkre * Validates group entry replacing gid > MAXUID by GID_NOBODY. 652b4a7802SBaban Kenkre */ 662b4a7802SBaban Kenkre int 672b4a7802SBaban Kenkre validate_group_ids(char *line, int *linelenp, int buflen, int extra_chars) 682b4a7802SBaban Kenkre { 692b4a7802SBaban Kenkre char *linep, *limit, *gidp; 702b4a7802SBaban Kenkre ulong_t gid; 712b4a7802SBaban Kenkre int oldgidlen, idlen; 722b4a7802SBaban Kenkre int linelen = *linelenp, newlinelen; 732b4a7802SBaban Kenkre 742b4a7802SBaban Kenkre if (linelen == 0 || *line == '+' || *line == '-') 752b4a7802SBaban Kenkre return (NSS_STR_PARSE_SUCCESS); 762b4a7802SBaban Kenkre 772b4a7802SBaban Kenkre linep = line; 782b4a7802SBaban Kenkre limit = line + linelen; 792b4a7802SBaban Kenkre 802b4a7802SBaban Kenkre while (linep < limit && *linep++ != ':') /* skip groupname */ 812b4a7802SBaban Kenkre continue; 822b4a7802SBaban Kenkre while (linep < limit && *linep++ != ':') /* skip password */ 832b4a7802SBaban Kenkre continue; 842b4a7802SBaban Kenkre if (linep == limit) 852b4a7802SBaban Kenkre return (NSS_STR_PARSE_PARSE); 862b4a7802SBaban Kenkre 872b4a7802SBaban Kenkre gidp = linep; 882b4a7802SBaban Kenkre gid = strtoul(gidp, (char **)&linep, 10); /* grab gid */ 892b4a7802SBaban Kenkre oldgidlen = linep - gidp; 902b4a7802SBaban Kenkre if (linep >= limit || oldgidlen == 0) 912b4a7802SBaban Kenkre return (NSS_STR_PARSE_PARSE); 922b4a7802SBaban Kenkre 932b4a7802SBaban Kenkre if (gid <= MAXUID) 942b4a7802SBaban Kenkre return (NSS_STR_PARSE_SUCCESS); 952b4a7802SBaban Kenkre 962b4a7802SBaban Kenkre idlen = snprintf(NULL, 0, "%u", GID_NOBODY); 972b4a7802SBaban Kenkre newlinelen = linelen + idlen - oldgidlen; 982b4a7802SBaban Kenkre if (newlinelen + extra_chars > buflen) 992b4a7802SBaban Kenkre return (NSS_STR_PARSE_ERANGE); 1002b4a7802SBaban Kenkre 1012b4a7802SBaban Kenkre (void) bcopy(linep, gidp + idlen, limit - linep + extra_chars); 1022b4a7802SBaban Kenkre (void) snprintf(gidp, idlen + 1, "%u", GID_NOBODY); 1032b4a7802SBaban Kenkre *(gidp + idlen) = ':'; 1042b4a7802SBaban Kenkre *linelenp = newlinelen; 1052b4a7802SBaban Kenkre return (NSS_STR_PARSE_SUCCESS); 1062b4a7802SBaban Kenkre } 1072b4a7802SBaban Kenkre 1087c478bd9Sstevel@tonic-gate static const char * 1097c478bd9Sstevel@tonic-gate get_grname(argp) 1107c478bd9Sstevel@tonic-gate nss_XbyY_args_t *argp; 1117c478bd9Sstevel@tonic-gate { 1127c478bd9Sstevel@tonic-gate struct group *g = (struct group *)argp->returnval; 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate return (g->gr_name); 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate static int 1187c478bd9Sstevel@tonic-gate check_grname(argp) 1197c478bd9Sstevel@tonic-gate nss_XbyY_args_t *argp; 1207c478bd9Sstevel@tonic-gate { 1217c478bd9Sstevel@tonic-gate struct group *g = (struct group *)argp->returnval; 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate return (strcmp(g->gr_name, argp->key.name) == 0); 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate static nss_status_t 1277c478bd9Sstevel@tonic-gate getbyname(be, a) 1287c478bd9Sstevel@tonic-gate compat_backend_ptr_t be; 1297c478bd9Sstevel@tonic-gate void *a; 1307c478bd9Sstevel@tonic-gate { 1317c478bd9Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate return (_nss_compat_XY_all(be, argp, check_grname, 1347c478bd9Sstevel@tonic-gate NSS_DBOP_GROUP_BYNAME)); 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate static int 1387c478bd9Sstevel@tonic-gate check_grgid(argp) 1397c478bd9Sstevel@tonic-gate nss_XbyY_args_t *argp; 1407c478bd9Sstevel@tonic-gate { 1417c478bd9Sstevel@tonic-gate struct group *g = (struct group *)argp->returnval; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate return (g->gr_gid == argp->key.gid); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate static nss_status_t 1477c478bd9Sstevel@tonic-gate getbygid(be, a) 1487c478bd9Sstevel@tonic-gate compat_backend_ptr_t be; 1497c478bd9Sstevel@tonic-gate void *a; 1507c478bd9Sstevel@tonic-gate { 1517c478bd9Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 1527c478bd9Sstevel@tonic-gate 1532b4a7802SBaban Kenkre if (argp->key.gid > MAXUID) 1542b4a7802SBaban Kenkre return (NSS_NOTFOUND); 1557c478bd9Sstevel@tonic-gate return (_nss_compat_XY_all(be, argp, check_grgid, 1567c478bd9Sstevel@tonic-gate NSS_DBOP_GROUP_BYGID)); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate static nss_status_t 1607c478bd9Sstevel@tonic-gate getbymember(be, a) 1617c478bd9Sstevel@tonic-gate compat_backend_ptr_t be; 1627c478bd9Sstevel@tonic-gate void *a; 1637c478bd9Sstevel@tonic-gate { 1647c478bd9Sstevel@tonic-gate struct nss_groupsbymem *argp = (struct nss_groupsbymem *)a; 1657c478bd9Sstevel@tonic-gate int numgids = argp->numgids; 1667c478bd9Sstevel@tonic-gate int maxgids = argp->maxgids; 1677c478bd9Sstevel@tonic-gate gid_t *gid_array = argp->gid_array; 1687c478bd9Sstevel@tonic-gate struct nss_XbyY_args grargs; 1697c478bd9Sstevel@tonic-gate struct group *g; 1707c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *gb = NULL, *b = NULL; 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate /* 1737c478bd9Sstevel@tonic-gate * Generic implementation: enumerate using getent(), then check each 1747c478bd9Sstevel@tonic-gate * group returned by getent() to see whether it contains the user. 1757c478bd9Sstevel@tonic-gate * There are much faster ways, but at least this one gets the right 1767c478bd9Sstevel@tonic-gate * answer. 1777c478bd9Sstevel@tonic-gate */ 1787c478bd9Sstevel@tonic-gate if (numgids >= maxgids) { 1797c478bd9Sstevel@tonic-gate /* full gid_array; nobody should have bothered to call us */ 1807c478bd9Sstevel@tonic-gate return (NSS_SUCCESS); 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate b = NSS_XbyY_ALLOC(&gb, sizeof (struct group), NSS_BUFLEN_GROUP); 1847c478bd9Sstevel@tonic-gate if (b == 0) 1857c478bd9Sstevel@tonic-gate return (NSS_UNAVAIL); 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate NSS_XbyY_INIT(&grargs, gb->result, gb->buffer, gb->buflen, 1887c478bd9Sstevel@tonic-gate argp->str2ent); 1897c478bd9Sstevel@tonic-gate g = (struct group *)gb->result; 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate (void) _nss_compat_setent(be, 0); 1927c478bd9Sstevel@tonic-gate while (_nss_compat_getent(be, &grargs) == NSS_SUCCESS) { 1937c478bd9Sstevel@tonic-gate char **mem; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate if (grargs.returnval == 0) { 1967c478bd9Sstevel@tonic-gate continue; 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate for (mem = g->gr_mem; *mem != 0; mem++) { 1997c478bd9Sstevel@tonic-gate if (strcmp(*mem, argp->username) == 0) { 2007c478bd9Sstevel@tonic-gate int gid = g->gr_gid; 2017c478bd9Sstevel@tonic-gate int i; 2027c478bd9Sstevel@tonic-gate for (i = 0; i < numgids; i++) { 2037c478bd9Sstevel@tonic-gate if (gid == gid_array[i]) { 2047c478bd9Sstevel@tonic-gate break; 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate if (i == numgids) { 2087c478bd9Sstevel@tonic-gate gid_array[numgids++] = gid; 2097c478bd9Sstevel@tonic-gate argp->numgids = numgids; 2107c478bd9Sstevel@tonic-gate if (numgids >= maxgids) { 2117c478bd9Sstevel@tonic-gate /* filled the gid_array */ 2127c478bd9Sstevel@tonic-gate (void) _nss_compat_endent(be, 2137c478bd9Sstevel@tonic-gate 0); 2147c478bd9Sstevel@tonic-gate NSS_XbyY_FREE(&gb); 2157c478bd9Sstevel@tonic-gate return (NSS_SUCCESS); 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate /* Done with this group, try next */ 2187c478bd9Sstevel@tonic-gate break; 2197c478bd9Sstevel@tonic-gate } 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate } 2237c478bd9Sstevel@tonic-gate (void) _nss_compat_endent(be, 0); 2247c478bd9Sstevel@tonic-gate NSS_XbyY_FREE(&gb); 2257c478bd9Sstevel@tonic-gate return (NSS_NOTFOUND); /* Really means "gid_array not full yet" */ 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2297c478bd9Sstevel@tonic-gate static int 2307c478bd9Sstevel@tonic-gate merge_grents(be, argp, fields) 2317c478bd9Sstevel@tonic-gate compat_backend_ptr_t be; 2327c478bd9Sstevel@tonic-gate nss_XbyY_args_t *argp; 2337c478bd9Sstevel@tonic-gate const char **fields; 2347c478bd9Sstevel@tonic-gate { 2357c478bd9Sstevel@tonic-gate struct group *g = (struct group *)argp->buf.result; 2367c478bd9Sstevel@tonic-gate char *buf; 2377c478bd9Sstevel@tonic-gate char *s; 2387c478bd9Sstevel@tonic-gate int parsestat; 239cb5caa98Sdjl int dlen; 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate /* 2427c478bd9Sstevel@tonic-gate * We're allowed to override the passwd (has anyone ever actually used 2437c478bd9Sstevel@tonic-gate * the passwd in a group entry?) and the membership list, but not 2447c478bd9Sstevel@tonic-gate * the groupname or the gid. 2457c478bd9Sstevel@tonic-gate * That's what the SunOS 4.x code did; who are we to question it... 2467c478bd9Sstevel@tonic-gate * 2477c478bd9Sstevel@tonic-gate * Efficiency is heartlessly abandoned in the quest for simplicity. 2487c478bd9Sstevel@tonic-gate */ 249cb5caa98Sdjl if (fields[1] == 0 && fields[3] == 0 && 250cb5caa98Sdjl be->return_string_data != 1) { 2517c478bd9Sstevel@tonic-gate /* No legal overrides, leave *argp unscathed */ 2527c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 2537c478bd9Sstevel@tonic-gate } 2547c478bd9Sstevel@tonic-gate if ((buf = malloc(NSS_LINELEN_GROUP)) == 0) { 2557c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 2567c478bd9Sstevel@tonic-gate /* Really "out of memory", but PARSE_PARSE will have to do */ 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate s = buf; 2592b4a7802SBaban Kenkre (void) snprintf(s, NSS_LINELEN_GROUP, "%s:%s:%u:", 2607c478bd9Sstevel@tonic-gate g->gr_name, 2617c478bd9Sstevel@tonic-gate fields[1] != 0 ? fields[1] : g->gr_passwd, 2627c478bd9Sstevel@tonic-gate g->gr_gid); 2637c478bd9Sstevel@tonic-gate s += strlen(s); 2647c478bd9Sstevel@tonic-gate if (fields[3] != 0) { 265cb5caa98Sdjl (void) strcpy(s, fields[3]); 2667c478bd9Sstevel@tonic-gate s += strlen(s); 2677c478bd9Sstevel@tonic-gate } else { 2687c478bd9Sstevel@tonic-gate char **memp; 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate for (memp = g->gr_mem; *memp != 0; memp++) { 2717c478bd9Sstevel@tonic-gate size_t len = strlen(*memp); 2727c478bd9Sstevel@tonic-gate if (s + len + 1 <= buf + NSS_LINELEN_GROUP) { 2737c478bd9Sstevel@tonic-gate if (memp != g->gr_mem) { 2747c478bd9Sstevel@tonic-gate *s++ = ','; 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate (void) memcpy(s, *memp, len); 2777c478bd9Sstevel@tonic-gate s += len; 2787c478bd9Sstevel@tonic-gate } else { 2797c478bd9Sstevel@tonic-gate free(buf); 2807c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate } 284cb5caa98Sdjl 285cb5caa98Sdjl dlen = s - buf; 286cb5caa98Sdjl 287cb5caa98Sdjl /* 288cb5caa98Sdjl * if asked, return the data in /etc file format 289cb5caa98Sdjl */ 290cb5caa98Sdjl if (be->return_string_data == 1) { 291cb5caa98Sdjl /* reset the result ptr to the original value */ 292cb5caa98Sdjl argp->buf.result = NULL; 293cb5caa98Sdjl 294cb5caa98Sdjl if (dlen > argp->buf.buflen) { 295cb5caa98Sdjl parsestat = NSS_STR_PARSE_ERANGE; 296cb5caa98Sdjl } else { 297cb5caa98Sdjl (void) strncpy(argp->buf.buffer, buf, dlen); 298cb5caa98Sdjl argp->returnval = argp->buf.buffer; 299cb5caa98Sdjl argp->returnlen = dlen; 300cb5caa98Sdjl parsestat = NSS_SUCCESS; 301cb5caa98Sdjl } 302cb5caa98Sdjl } else { 303cb5caa98Sdjl parsestat = (*argp->str2ent)(buf, dlen, 3047c478bd9Sstevel@tonic-gate argp->buf.result, 3057c478bd9Sstevel@tonic-gate argp->buf.buffer, 3067c478bd9Sstevel@tonic-gate argp->buf.buflen); 307cb5caa98Sdjl } 308cb5caa98Sdjl 3097c478bd9Sstevel@tonic-gate free(buf); 3107c478bd9Sstevel@tonic-gate return (parsestat); 3117c478bd9Sstevel@tonic-gate } 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate static compat_backend_op_t group_ops[] = { 3147c478bd9Sstevel@tonic-gate _nss_compat_destr, 3157c478bd9Sstevel@tonic-gate _nss_compat_endent, 3167c478bd9Sstevel@tonic-gate _nss_compat_setent, 3177c478bd9Sstevel@tonic-gate _nss_compat_getent, 3187c478bd9Sstevel@tonic-gate getbyname, 3197c478bd9Sstevel@tonic-gate getbygid, 3207c478bd9Sstevel@tonic-gate getbymember 3217c478bd9Sstevel@tonic-gate }; 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3247c478bd9Sstevel@tonic-gate nss_backend_t * 3257c478bd9Sstevel@tonic-gate _nss_compat_group_constr(dummy1, dummy2, dummy3) 3267c478bd9Sstevel@tonic-gate const char *dummy1, *dummy2, *dummy3; 3277c478bd9Sstevel@tonic-gate { 3287c478bd9Sstevel@tonic-gate return (_nss_compat_constr(group_ops, 3297c478bd9Sstevel@tonic-gate sizeof (group_ops) / sizeof (group_ops[0]), 3307c478bd9Sstevel@tonic-gate GF_PATH, 3317c478bd9Sstevel@tonic-gate NSS_LINELEN_GROUP, 3327c478bd9Sstevel@tonic-gate &db_root, 3337c478bd9Sstevel@tonic-gate _nss_initf_group_compat, 3347c478bd9Sstevel@tonic-gate 0, 3357c478bd9Sstevel@tonic-gate get_grname, 3367c478bd9Sstevel@tonic-gate merge_grents)); 3377c478bd9Sstevel@tonic-gate } 338