#include "files_common.h"
#include <ctype.h>
#include <rpcsvc/ypclnt.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
#include <sys/sysmacros.h>
#pragma weak __nss_files_netgr_debug
#pragma weak __nss_files_netgr_error
extern void __nss_files_netgr_debug(const char *, ...);
extern void __nss_files_netgr_error(const char *, ...);
static uint_t
hash_netgrname(nss_XbyY_args_t *argp, int keyhash, const char *line,
int linelen)
{
const char *name;
uint_t namelen, i;
uint_t hash = 0;
if (keyhash) {
name = argp->key.name;
namelen = strlen(name);
} else {
name = line;
namelen = 0;
while (linelen-- && !isspace(*line)) {
line++;
namelen++;
}
}
for (i = 0; i < namelen; i++)
hash = hash * 15 + name[i];
return (hash);
}
static files_hash_func hash_netgr[1] = { hash_netgrname };
static files_hash_t hashinfo = {
DEFAULTMUTEX,
sizeof (struct nss_netgrent),
NSS_LINELEN_NETGROUP,
1,
hash_netgr
};
static int
check_netgrname(nss_XbyY_args_t *argp, const char *line, int linelen)
{
const char *linep, *limit;
const char *keyp = argp->key.name;
linep = line;
limit = line + linelen;
if (linelen == 0 || *line == '+' || *line == '-')
return (0);
while (*keyp && linep < limit && *keyp == *linep) {
keyp++;
linep++;
}
return (linep < limit && *keyp == '\0' && isspace(*linep));
}
static nss_status_t
getbyname(files_backend_ptr_t be, void *a)
{
return (_nss_files_XY_hash(be, a, 1, &hashinfo, 0, check_netgrname));
}
static int
str2netgr(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
{
const char sep[] = " \t\n";
struct nss_netgrent *netgr = ent;
const char *p;
p = instr;
while (isspace(*p))
p++;
if (*p == '\0')
return (NSS_STR_PARSE_PARSE);
p = strpbrk(p, sep);
if (p == NULL)
return (NSS_STR_PARSE_PARSE);
while (isspace(*p))
p++;
if (*p == '\0')
return (NSS_STR_PARSE_PARSE);
netgr->netgr_members = strdup(p);
if (netgr->netgr_members == NULL)
return (NSS_STR_PARSE_PARSE);
return (NSS_STR_PARSE_SUCCESS);
}
static nss_status_t
netgr_get_members(struct files_backend *be,
const char *name, char **valp)
{
struct nss_netgrent netgr;
nss_XbyY_args_t args;
nss_status_t result;
if (name == (const char *)NULL)
return (NSS_ERROR);
(void) memset(&netgr, '\0', sizeof (netgr));
(void) memset(&args, '\0', sizeof (args));
args.buf.result = &netgr;
args.str2ent = str2netgr;
args.key.name = name;
result = getbyname(be, &args);
if (result == NSS_SUCCESS) {
*valp = netgr.netgr_members;
if (*valp == NULL)
result = NSS_UNAVAIL;
}
return (result);
}
struct files_getnetgr_be;
typedef nss_status_t (*files_getnetgr_op_t)(
struct files_getnetgr_be *, void *);
struct files_getnetgr_be {
files_getnetgr_op_t *ops;
nss_dbop_t n_ops;
char *netgroup;
struct grouplist *all_members;
struct grouplist *next_member;
};
struct grouplist {
char *triple[NSS_NETGR_N];
struct grouplist *gl_nxt;
};
static nss_status_t
getnetgr_set(struct files_getnetgr_be *be, void *a)
{
const char *netgroup = (const char *) a;
if (be->netgroup != NULL &&
strcmp(be->netgroup, netgroup) == 0) {
be->next_member = be->all_members;
return (NSS_SUCCESS);
}
return (NSS_NOTFOUND);
}
static nss_status_t
getnetgr_get(struct files_getnetgr_be *be, void *a)
{
struct nss_getnetgrent_args *args = (struct nss_getnetgrent_args *)a;
struct grouplist *mem;
if ((mem = be->next_member) == 0) {
args->status = NSS_NETGR_NO;
} else {
char *buffer = args->buffer;
int buflen = args->buflen;
enum nss_netgr_argn i;
args->status = NSS_NETGR_FOUND;
for (i = 0; i < NSS_NETGR_N; i++) {
const char *str;
ssize_t len;
if ((str = mem->triple[i]) == 0) {
args->retp[i] = NULL;
} else if ((len = strlen(str) + 1) <= buflen) {
args->retp[i] = buffer;
(void) memcpy(buffer, str, len);
buffer += len;
buflen -= len;
} else {
args->status = NSS_NETGR_NOMEM;
break;
}
}
be->next_member = mem->gl_nxt;
}
return (NSS_SUCCESS);
}
static nss_status_t
getnetgr_end(struct files_getnetgr_be *be, void *dummy)
{
struct grouplist *gl;
struct grouplist *next;
for (gl = be->all_members; gl != NULL; gl = next) {
enum nss_netgr_argn i;
next = gl->gl_nxt;
for (i = NSS_NETGR_MACHINE; i < NSS_NETGR_N; i++) {
free(gl->triple[i]);
}
free(gl);
}
be->all_members = NULL;
be->next_member = NULL;
free(be->netgroup);
be->netgroup = NULL;
return (NSS_SUCCESS);
}
static nss_status_t
getnetgr_destr(struct files_getnetgr_be *be, void *dummy)
{
if (be != NULL) {
(void) getnetgr_end(be, NULL);
free(be);
}
return (NSS_SUCCESS);
}
static files_getnetgr_op_t getnetgr_ops[] = {
getnetgr_destr,
getnetgr_end,
getnetgr_set,
getnetgr_get,
};
struct netgrnam {
struct netgrnam *hash_chain;
struct netgrnam *expand_next;
char name[1];
};
#define HASHMOD 113
struct netgrtab {
struct netgrnam *expand_first;
struct netgrnam **expand_lastp;
struct netgrnam *hash_heads[HASHMOD];
};
static void
ngt_init(struct netgrtab *ngt)
{
(void) memset((void *)ngt, '\0', sizeof (*ngt));
ngt->expand_lastp = &ngt->expand_first;
}
static void
ngt_insert(struct netgrtab *ngt, const char *name, size_t namelen)
{
unsigned hashval;
size_t i;
struct netgrnam *cur;
struct netgrnam **head;
if (__nss_files_netgr_debug != NULL) {
__nss_files_netgr_debug(
"ngt_insert: ngt=%p names=%s", ngt, name);
}
for (hashval = 0, i = 0; i < namelen; i++) {
hashval = (hashval << 2) + hashval +
((const unsigned char *)name)[i];
}
head = &ngt->hash_heads[hashval % HASHMOD];
for (cur = *head; cur != 0; cur = cur->hash_chain) {
if (strncmp(cur->name, name, namelen) == 0 &&
cur->name[namelen] == 0) {
return;
}
}
cur = malloc(offsetof(struct netgrnam, name) + namelen + 1);
if (cur == NULL) {
return;
}
(void) memcpy(cur->name, name, namelen);
cur->name[namelen] = '\0';
cur->hash_chain = *head;
*head = cur;
cur->expand_next = NULL;
*ngt->expand_lastp = cur;
ngt->expand_lastp = &cur->expand_next;
}
static const char *
ngt_next(struct netgrtab *ngt)
{
struct netgrnam *first;
if ((first = ngt->expand_first) == NULL) {
return (NULL);
}
if ((ngt->expand_first = first->expand_next) == NULL) {
ngt->expand_lastp = &ngt->expand_first;
}
return (first->name);
}
static void
ngt_destroy(struct netgrtab *ngt)
{
struct netgrnam *cur;
struct netgrnam *next;
int i;
for (i = 0; i < HASHMOD; i++) {
for (cur = ngt->hash_heads[i]; cur != NULL; ) {
next = cur->hash_chain;
free(cur);
cur = next;
}
}
}
typedef const char *ccp;
static nss_status_t
top_down(struct files_backend *be, const char **groups, int ngroups,
int (*func)(ccp triple[3], void *iter_args, nss_status_t *return_val),
void *iter_args)
{
struct netgrtab *ngt;
const char *group;
int nfound;
int done;
nss_status_t result;
if ((ngt = malloc(sizeof (*ngt))) == NULL) {
return (NSS_UNAVAIL);
}
ngt_init(ngt);
while (ngroups > 0) {
ngt_insert(ngt, *groups, strlen(*groups));
groups++;
ngroups--;
}
done = 0;
nfound = 0;
while (!done && (group = ngt_next(ngt)) != NULL) {
char *val = NULL;
char *p;
result = netgr_get_members(be, group, &val);
if (result != NSS_SUCCESS) {
if (result == NSS_NOTFOUND) {
if (__nss_files_netgr_error != NULL)
__nss_files_netgr_error(
"files netgroup lookup: %s doesn't exist",
group);
} else {
if (__nss_files_netgr_error != NULL)
__nss_files_netgr_error(
"files netgroup lookup: getbyname returned [%s]",
strerror(errno));
done = 1;
}
continue;
}
if (__nss_files_netgr_debug != NULL) {
__nss_files_netgr_debug(
"ngt_top: ngt=%p grp=%s members=\"%s\"",
ngt, group, val);
}
nfound++;
if ((p = strpbrk(val, "#\n")) != NULL) {
*p = '\0';
}
p = val;
for (;;) {
ccp triple[NSS_NETGR_N];
int syntax_err;
enum nss_netgr_argn i;
while (isspace(*p))
p++;
if (*p == '\0') {
break;
}
if (*p != '(') {
char *start = p;
p = strpbrk(start, " \t");
if (p == 0) {
p = start + strlen(start);
}
ngt_insert(ngt, start, (size_t)(p - start));
continue;
}
p++;
syntax_err = 0;
for (i = NSS_NETGR_MACHINE; i < NSS_NETGR_N; i++) {
char *start;
char *limit;
const char *terminators = ",) \t";
if (i == NSS_NETGR_DOMAIN) {
terminators++;
}
while (isspace(*p))
p++;
start = p;
limit = strpbrk(start, terminators);
if (limit == 0) {
syntax_err++;
break;
}
p = limit;
while (isspace(*p))
p++;
if (*p == terminators[0]) {
p++;
if (start == limit) {
triple[i] = 0;
} else {
*limit = '\0';
triple[i] = start;
}
} else {
syntax_err++;
break;
}
}
if (syntax_err) {
break;
} else if ((*func)(triple, iter_args, &result) == 0) {
done = 1;
break;
}
}
free(val);
val = NULL;
}
ngt_destroy(ngt);
free(ngt);
if (done) {
return (result);
} else if (nfound > 0) {
return (NSS_SUCCESS);
} else {
return (NSS_NOTFOUND);
}
}
static int
save_triple(ccp trippp[NSS_NETGR_N], void *headp_arg,
nss_status_t *return_val)
{
struct grouplist **headp = headp_arg;
struct grouplist *gl;
enum nss_netgr_argn i;
if (__nss_files_netgr_debug != NULL) {
__nss_files_netgr_debug(
"save_tripple: h=%s u=%s d=%s",
trippp[0] ? trippp[0] : "*",
trippp[1] ? trippp[1] : "*",
trippp[2] ? trippp[2] : "*");
}
if ((gl = malloc(sizeof (*gl))) == NULL) {
*return_val = NSS_UNAVAIL;
return (0);
}
for (i = NSS_NETGR_MACHINE; i < NSS_NETGR_N; i++) {
if (trippp[i] == NULL) {
gl->triple[i] = NULL;
} else if ((gl->triple[i] = strdup(trippp[i])) == NULL) {
enum nss_netgr_argn j;
for (j = NSS_NETGR_MACHINE; j < i; j++) {
free(gl->triple[j]);
}
free(gl);
*return_val = NSS_UNAVAIL;
return (0);
}
}
gl->gl_nxt = *headp;
*headp = gl;
return (1);
}
static nss_status_t
netgr_set(struct files_backend *be, void *a)
{
struct nss_setnetgrent_args *args = (struct nss_setnetgrent_args *)a;
struct files_getnetgr_be *get_be;
nss_status_t res;
get_be = malloc(sizeof (*get_be));
if (get_be == NULL) {
return (NSS_UNAVAIL);
}
get_be->all_members = NULL;
res = top_down(be, &args->netgroup, 1, save_triple,
&get_be->all_members);
if (res == NSS_SUCCESS) {
get_be->ops = getnetgr_ops;
get_be->n_ops = ARRAY_SIZE(getnetgr_ops);
get_be->netgroup = strdup(args->netgroup);
if (get_be->netgroup == NULL) {
args->iterator = NULL;
free(get_be);
return (NSS_UNAVAIL);
}
get_be->next_member = get_be->all_members;
args->iterator = (nss_backend_t *)get_be;
} else {
args->iterator = NULL;
free(get_be);
}
return (res);
}
static int
match_triple(ccp triple[NSS_NETGR_N], void *ia_arg, nss_status_t *return_val)
{
struct nss_innetgr_args *ia = ia_arg;
enum nss_netgr_argn i;
if (__nss_files_netgr_debug != NULL) {
__nss_files_netgr_debug(
"match_triple: h=%s u=%s d=%s",
triple[0] ? triple[0] : "*",
triple[1] ? triple[1] : "*",
triple[2] ? triple[2] : "*");
}
for (i = NSS_NETGR_MACHINE; i < NSS_NETGR_N; i++) {
int (*cmpf)(const char *, const char *);
char **argv;
uint_t n;
const char *name = triple[i];
int argc = ia->arg[i].argc;
if (argc == 0 || name == NULL) {
continue;
}
argv = ia->arg[i].argv;
cmpf = (i == NSS_NETGR_MACHINE) ? strcasecmp : strcmp;
for (n = 0; n < argc; n++) {
if ((*cmpf)(argv[n], name) == 0) {
break;
}
}
if (n >= argc) {
return (1);
}
}
if (__nss_files_netgr_debug != NULL)
__nss_files_netgr_debug("match_triple: found");
ia->status = NSS_NETGR_FOUND;
*return_val = NSS_SUCCESS;
return (0);
}
static nss_status_t
netgr_in(struct files_backend *be, void *a)
{
struct nss_innetgr_args *ia = (struct nss_innetgr_args *)a;
nss_status_t res;
ia->status = NSS_NETGR_NO;
ia->status = NSS_NETGR_NO;
res = top_down(be, (const char **)ia->groups.argv, ia->groups.argc,
match_triple, ia);
return (res);
}
static nss_status_t
netgr_destr(struct files_backend *be, void *dummy)
{
free(be);
return (NSS_SUCCESS);
}
static files_backend_op_t netgroup_ops[] = {
netgr_destr,
NULL,
NULL,
NULL,
netgr_in,
netgr_set,
getbyname,
};
nss_backend_t *
_nss_files_netgroup_constr(const char *dummy1, const char *dummy2,
const char *dummy3)
{
nss_backend_t *be;
be = _nss_files_constr(netgroup_ops,
ARRAY_SIZE(netgroup_ops),
"/etc/netgroup",
NSS_LINELEN_NETGROUP,
&hashinfo);
return (be);
}