2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 1991, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A *
2N/A * files/getgrent.c -- "files" backend for nsswitch "group" database
2N/A */
2N/A
2N/A#include <grp.h>
2N/A#include <unistd.h> /* for GF_PATH */
2N/A#include <stdlib.h> /* for GF_PATH */
2N/A#include "files_common.h"
2N/A#include <strings.h>
2N/A
2N/Astatic files_hash_func hash_gr[2] = { hash_name, hash_ugid };
2N/A
2N/Astatic files_hash_t hashinfo = {
2N/A DEFAULTMUTEX,
2N/A sizeof (struct group),
2N/A NSS_BUFLEN_GROUP,
2N/A 2,
2N/A hash_gr
2N/A};
2N/A
2N/Astatic int
2N/Acheck_grname(nss_XbyY_args_t *argp, const char *line, int linelen)
2N/A{
2N/A const char *linep, *limit;
2N/A const char *keyp = argp->key.name;
2N/A
2N/A linep = line;
2N/A limit = line + linelen;
2N/A
2N/A /* +/- entries valid for compat source only */
2N/A if (linelen == 0 || *line == '+' || *line == '-')
2N/A return (0);
2N/A while (*keyp && linep < limit && *keyp == *linep) {
2N/A keyp++;
2N/A linep++;
2N/A }
2N/A return (linep < limit && *keyp == '\0' && *linep == ':');
2N/A}
2N/A
2N/Astatic nss_status_t
2N/Agetbyname(be, a)
2N/A files_backend_ptr_t be;
2N/A void *a;
2N/A{
2N/A return (_nss_files_XY_hash(be, a, 0, &hashinfo, 0, check_grname));
2N/A}
2N/A
2N/Astatic int
2N/Acheck_grgid(nss_XbyY_args_t *argp, const char *line, int linelen)
2N/A{
2N/A const char *linep, *limit, *end;
2N/A ulong_t gr_gid;
2N/A
2N/A linep = line;
2N/A limit = line + linelen;
2N/A
2N/A /* +/- entries valid for compat source only */
2N/A if (linelen == 0 || *line == '+' || *line == '-')
2N/A return (0);
2N/A
2N/A while (linep < limit && *linep++ != ':') /* skip groupname */
2N/A continue;
2N/A while (linep < limit && *linep++ != ':') /* skip password */
2N/A continue;
2N/A if (linep == limit)
2N/A return (0);
2N/A
2N/A /* gid */
2N/A end = linep;
2N/A gr_gid = strtoul(linep, (char **)&end, 10);
2N/A
2N/A /* check if gid is empty or overflows */
2N/A if (linep == end || gr_gid > UINT32_MAX)
2N/A return (0);
2N/A
2N/A return ((gid_t)gr_gid == argp->key.gid);
2N/A}
2N/A
2N/Astatic nss_status_t
2N/Agetbygid(be, a)
2N/A files_backend_ptr_t be;
2N/A void *a;
2N/A{
2N/A nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
2N/A
2N/A if (argp->key.gid > MAXUID)
2N/A return (NSS_NOTFOUND);
2N/A return (_nss_files_XY_hash(be, argp, 0, &hashinfo, 1, check_grgid));
2N/A}
2N/A
2N/A/*
2N/A * Validates group entry replacing gid > MAXUID by GID_NOBODY.
2N/A */
2N/Aint
2N/Avalidate_group_ids(char *line, int *linelenp, int buflen, int extra_chars,
2N/A files_XY_check_func check)
2N/A{
2N/A char *linep, *limit, *gidp;
2N/A ulong_t gid;
2N/A int oldgidlen, idlen;
2N/A int linelen = *linelenp, newlinelen;
2N/A
2N/A /*
2N/A * getbygid() rejects searching by ephemeral gid therefore
2N/A * no need to validate because the matched entry won't have
2N/A * an ephemeral gid.
2N/A */
2N/A if (check != NULL && check == check_grgid)
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A
2N/A /* +/- entries valid for compat source only */
2N/A if (linelen == 0 || *line == '+' || *line == '-')
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A
2N/A linep = line;
2N/A limit = line + linelen;
2N/A
2N/A while (linep < limit && *linep++ != ':') /* skip groupname */
2N/A continue;
2N/A while (linep < limit && *linep++ != ':') /* skip password */
2N/A continue;
2N/A if (linep == limit)
2N/A return (NSS_STR_PARSE_PARSE);
2N/A
2N/A gidp = linep;
2N/A gid = strtoul(gidp, (char **)&linep, 10); /* grab gid */
2N/A oldgidlen = linep - gidp;
2N/A if (linep >= limit || oldgidlen == 0)
2N/A return (NSS_STR_PARSE_PARSE);
2N/A
2N/A if (gid <= MAXUID)
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A
2N/A idlen = snprintf(NULL, 0, "%u", GID_NOBODY);
2N/A newlinelen = linelen + idlen - oldgidlen;
2N/A if (newlinelen + extra_chars > buflen)
2N/A return (NSS_STR_PARSE_ERANGE);
2N/A
2N/A (void) bcopy(linep, gidp + idlen, limit - linep + extra_chars);
2N/A (void) snprintf(gidp, idlen + 1, "%u", GID_NOBODY);
2N/A *(gidp + idlen) = ':';
2N/A *linelenp = newlinelen;
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A}
2N/A
2N/Astatic nss_status_t
2N/Agetbymember(be, a)
2N/A files_backend_ptr_t be;
2N/A void *a;
2N/A{
2N/A struct nss_groupsbymem *argp = (struct nss_groupsbymem *)a;
2N/A
2N/A return (_nss_files_do_all(be, argp, argp->username,
2N/A (files_do_all_func_t)argp->process_cstr));
2N/A}
2N/A
2N/Astatic files_backend_op_t group_ops[] = {
2N/A _nss_files_destr,
2N/A _nss_files_endent,
2N/A _nss_files_setent,
2N/A _nss_files_getent_rigid,
2N/A getbyname,
2N/A getbygid,
2N/A getbymember
2N/A};
2N/A
2N/A/*ARGSUSED*/
2N/Anss_backend_t *
2N/A_nss_files_group_constr(dummy1, dummy2, dummy3)
2N/A const char *dummy1, *dummy2, *dummy3;
2N/A{
2N/A return (_nss_files_constr(group_ops,
2N/A sizeof (group_ops) / sizeof (group_ops[0]),
2N/A GF_PATH,
2N/A NSS_LINELEN_GROUP,
2N/A &hashinfo, FC_FLAG_GROUP));
2N/A}