/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* nis/getnetgrent.c -- "nis" backend for nsswitch "netgroup" database
*
* The API for netgroups differs sufficiently from that for the average
* getXXXbyYYY function that we use very few of the support routines in
* nis_common.h.
*
* The implementation of setnetgrent()/getnetgrent() here follows the
* the 4.x code, inasmuch as the setnetgrent() routine does all the work
* of traversing the netgroup graph and building a (potentially large)
* list in memory, and getnetgrent() just steps down the list.
*
* An alternative, and probably better, implementation would lazy-eval
* the netgroup graph in response to getnetgrent() calls (though
* setnetgrent() should still check for the top-level netgroup name
* and return NSS_SUCCESS / NSS_NOTFOUND).
*/
#include "nis_common.h"
#include <ctype.h>
#include <malloc.h>
#include <string.h>
#ifdef DEBUG
#endif /* DEBUG */
/*
* The nss_backend_t for a getnetgrent() sequence; we actually give the
* netgroup frontend a pointer to one of these structures in response to
* a (successful) setnetgrent() call on the nis_netgr_be backend
* described further down in this file.
*/
struct nis_getnetgr_be;
struct nis_getnetgr_be {
/*
* State for set/get/endnetgrent()
*/
char *netgroup;
};
};
static nss_status_t
struct nis_getnetgr_be *be;
void *a;
{
const char *netgroup = (const char *) a;
/* We already have the member-list; regurgitate it */
return (NSS_SUCCESS);
}
return (NSS_NOTFOUND);
}
static nss_status_t
struct nis_getnetgr_be *be;
void *a;
{
} else {
enum nss_netgr_argn i;
for (i = 0; i < NSS_NETGR_N; i++) {
const char *str;
} else {
break;
}
}
}
return (NSS_SUCCESS); /* Yup, even for end-of-list, i.e. */
/* do NOT advance to next backend. */
}
/*ARGSUSED*/
static nss_status_t
struct nis_getnetgr_be *be;
void *dummy;
{
enum nss_netgr_argn i;
for (i = NSS_NETGR_MACHINE; i < NSS_NETGR_N; i++) {
}
}
}
be->all_members = 0;
be->next_member = 0;
}
return (NSS_SUCCESS);
}
/*ARGSUSED*/
static nss_status_t
struct nis_getnetgr_be *be;
void *dummy;
{
if (be != 0) {
(void) getnetgr_end(be, (void *)0);
}
return (NSS_SUCCESS);
}
getnetgr_get, /* getnetgrent_r() */
};
/*
* The nss_backend_t for innetgr() and setnetgrent().
*/
struct nis_netgr_be;
struct nis_netgr_be {
};
/*
* Code to do top-down search in the graph defined by the 'netgroup' YP map
*/
/*
* ===> This code is now used for setnetgrent(), not just innetgr().
*
* If the easy way doesn't pan out, recursively search the 'netgroup' map.
* In order to do this, we:
*
* - remember all the netgroup names we've seen during this search,
* whether or not we've expanded them yet (we want fast insertion
* with duplicate-detection, so use yet another chained hash table),
*
* - keep a list of all the netgroups we haven't expanded yet (we just
* want fast insertion and pop-first, so a linked list will do fine).
* If we insert at the head, we get a depth-first search; insertion
* at the tail gives breadth-first (?), which seems preferable (?).
*
* A netgrnam struct contains pointers for both the hash-table and the list.
* It also contains the netgroup name; note that we embed the name at the
* end of the structure rather than holding a pointer to yet another
* malloc()ed region.
*
* pointers for the expansion list.
*
* Most of this code is common to at least the NIS backend; it
* should be generalized and, presumably, moved into the frontend.
* ==> Not any longer...
*/
struct netgrnam {
};
struct netgrtab {
};
static void
{
}
static void
/* ==> ? Should return 'failed' (out-of-memory) status ? */
const char *name;
{
unsigned hashval;
size_t i;
((const unsigned char *)name)[i];
}
return; /* Already in table, do nothing */
}
}
/* Create new netgrnam struct */
if (cur == 0) {
return; /* Out of memory, too bad */
}
/* Insert in hash table */
/* Insert in expansion list (insert at end for breadth-first search */
cur->expand_next = 0;
}
static const char *
{
return (0);
}
}
}
static void
{
int i;
for (i = 0; i < HASHMOD; i++) {
}
}
/* Don't bother zeroing pointers; must do init if we want to reuse */
}
typedef const char *ccp;
static nss_status_t
void *iter_args)
{
/* netgrtab goes on the heap, not the stack, because it's large and */
/* stacks may not be all that big in multi-threaded programs. */
const char *group;
int nfound;
int done;
return (NSS_UNAVAIL);
}
while (ngroups > 0) {
groups++;
ngroups--;
}
done = 0; /* Set to 1 to indicate that we cut the iteration */
/* short (and 'result' holds the return value) */
nfound = 0; /* Number of successful netgroup yp_match calls */
char *val;
int vallen;
char *p;
int yperr;
if (result != NSS_SUCCESS) {
/*LINTED E_NOP_IF_STMT*/
if (result == NSS_NOTFOUND) {
;
#ifdef DEBUG
"NIS netgroup lookup: %s doesn't exist",
group);
#endif /* DEBUG */
} else {
#ifdef DEBUG
"NIS netgroup lookup: yp_match returned [%s]",
#endif /* DEBUG */
}
/* Don't need to clean up anything */
continue;
}
nfound++;
*p = '\0';
}
p = val;
/* Parse val into triples and recursive netgroup references */
/*CONSTCOND*/
while (1) {
int syntax_err;
enum nss_netgr_argn i;
while (isspace(*p)) {
p++;
}
if (*p == '\0') {
/* Finished processing this particular val */
break;
}
if (*p != '(') {
/* Doesn't look like the start of a triple, */
/* so assume it's a recursive netgroup. */
char *start = p;
if (p == 0) {
/* Point p at the final '\0' */
}
continue;
}
/* Main case: a (machine, user, domain) triple */
p++;
syntax_err = 0;
for (i = NSS_NETGR_MACHINE; i < NSS_NETGR_N; i++) {
char *start;
char *limit;
if (i == NSS_NETGR_DOMAIN) {
/* Don't allow comma */
terminators++;
}
while (isspace(*p)) {
p++;
}
start = p;
if (limit == 0) {
syntax_err++;
break;
}
p = limit;
while (isspace(*p)) {
p++;
}
if (*p == terminators[0]) {
/*
* Successfully parsed this name and
* the separator after it (comma or
* right paren); leave p ready for
* next parse.
*/
p++;
/* Wildcard */
triple[i] = 0;
} else {
*limit = '\0';
}
} else {
syntax_err++;
break;
}
}
if (syntax_err) {
/*
* ===> log it;
* ===> try skipping past next ')'; failing that, abandon the line;
*/
break; /* Abandon this line */
/* Return result, good or bad */
done = 1;
break;
}
}
/* End of inner loop over val[] */
}
/* End of outer loop (!done && ngt_next(ngt) != 0) */
if (done) {
return (result);
} else if (nfound > 0) {
/* ==== ? Should only do this if all the top-level groups */
/* exist in YP? */
return (NSS_SUCCESS);
} else {
return (NSS_NOTFOUND);
}
}
/*
* Code for setnetgrent()
*/
/*
* Iterator function for setnetgrent(): copy triple, add to be->all_members
*/
static int
{
enum nss_netgr_argn i;
/* Out of memory */
return (0);
}
for (i = NSS_NETGR_MACHINE; i < NSS_NETGR_N; i++) {
if (trippp[i] == 0) {
/* Wildcard */
/* Out of memory. Free any we've allocated */
enum nss_netgr_argn j;
for (j = NSS_NETGR_MACHINE; j < i; j++) {
}
}
return (0);
}
}
return (1); /* Tell top_down() to keep iterating */
}
static nss_status_t
struct nis_netgr_be *be;
void *a;
{
if (get_be == 0) {
return (NSS_UNAVAIL);
}
get_be->all_members = 0;
&get_be->all_members);
if (res == NSS_SUCCESS) {
sizeof (getnetgr_ops[0]);
} else {
}
return (res);
}
/*
* Code for innetgr()
*/
/*
* Iterator function for innetgr(): Check whether triple matches args
*/
static int
{
enum nss_netgr_argn i;
for (i = NSS_NETGR_MACHINE; i < NSS_NETGR_N; i++) {
int (*cmpf)(const char *, const char *);
char **argv;
int n;
/* Wildcarded on one side or t'other */
continue;
}
for (n = 0; n < argc; n++) {
break;
}
}
if (n >= argc) {
/* Match failed, tell top_down() to keep looking */
return (1);
}
}
/* Matched on all three, so quit looking and declare victory */
return (0);
}
/*
* inlist() -- return 1 if at least one item from the "what" list
* is in the comma-separated, newline-terminated "list"
*/
static int
char *list;
{
char *p;
while (*list != 0) {
list++;
!isspace(*p); /* nothing */)
p++;
if (p != list) {
if (*p != 0)
*p++ = 0;
return (1);
}
list = p;
}
}
return (0);
}
/*
* Generate a key for a netgroup.byXXXX NIS map
*/
static void
char *key;
const char *name;
const char *domain;
{
;
;
}
static int
char *key;
const char *name; /* Convert this to lowercase */
const char *domain; /* But not this */
{
int found_uc = 0;
char c;
while (c = *name++) {
if (isupper(c)) {
++found_uc;
c = tolower(c);
}
*key++ = c;
}
*key++ = '.';
;
return (found_uc);
}
/*
* easy_way() -- try to use netgroup.byuser and netgroup.byhost maps to
* get answers more efficiently than by recursive search.
*
* If more than one name (username or hostname) is specified, this approach
* becomes less attractive; at some point it's probably cheaper to do the
* recursive search. We don't know what the threshold is (among other things
* it may depend on the site-specific struucture of netgroup information),
* so here's a guesstimate.
*/
static int
struct nis_netgr_be *be;
struct nss_innetgr_args *ia;
struct nss_innetgr_1arg *argp;
const char *map;
int try_lc;
{
int yperr;
char *val;
int vallen;
char *key;
int i;
/* Our caller guaranteed that nname >= 1 */
while (nname > 1) {
if (nname > NNAME_THRESHOLD) {
return (0); /* May be cheaper to use 'netgroup' */
}
return (1);
}
++pname;
--nname;
/* Fall through and do the last one inline */
}
return (0); /* Or maybe (1) and NSS_UNAVAIL */
}
switch (i) {
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
try_lc = 0; /* Sleazy but effective */
continue; /* i.e. quit looping */
}
break;
case 5:
break;
}
if (*statusp == NSS_SUCCESS) {
return (1);
} else {
}
} else {
#ifdef DEBUG
"innetgr: yp_match(%s,%s) failed: %s",
#endif /* DEBUG */
return (0);
}
}
}
/* =====> is this (an authoritative "no") always the right thing to do? */
/* Answer: yes, except for hostnames that aren't all lowercase */
return (1); /* Silly, innit? */
}
static nss_status_t
struct nis_netgr_be *be;
void *a;
{
/* Can we use netgroup.byhost or netgroup.byuser to speed things up? */
/* ====> diddle this to try fast path for domains.argc == 0 too */
"netgroup.byuser", 0, &res)) {
return (res);
}
return (res);
}
}
}
/* Nope, try the slow way */
match_triple, ia);
return (res);
}
/*
* (Almost) boilerplate for a switch backend
*/
/*ARGSUSED*/
static nss_status_t
struct nis_netgr_be *be;
void *dummy;
{
if (be != 0) {
}
return (NSS_SUCCESS);
}
0, /* No setent; setnetgrent() is really a getXbyY() */
0, /* No getent in the normal sense */
netgr_in, /* innetgr() */
netgr_set, /* setnetgrent() */
};
/*ARGSUSED*/
{
const char *domain;
if ((domain = _nss_nis_domain()) == 0 ||
return (0);
}
return ((nss_backend_t *)be);
}