getnetgrent.c revision 2
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 */
58N/A/*
2N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
32N/A */
32N/A
127N/A
127N/A#include <syslog.h>
145N/A#include "ldap_common.h"
127N/A
127N/A/* netgroup attributes filters */
127N/A#define _N_TRIPLE "nisnetgrouptriple"
127N/A#define _N_MEMBER "membernisnetgroup"
2N/A
145N/A#define PRINT_VAL(a) (((a).argc == 0) || ((a).argv == NULL) || \
26N/A ((a).argv[0] == NULL)) ? "*" : (a).argv[0]
38N/A#define ISNULL(a) (a == NULL ? "<NULL>" : a)
38N/A#define MAX_DOMAIN_LEN 1024
29N/A#define MAX_TRIPLE_LEN (MAXHOSTNAMELEN + LOGNAME_MAX + \
93N/A MAX_DOMAIN_LEN + 5)
29N/A
29N/A#define _F_SETMEMBER "(&(objectClass=nisNetGroup)(cn=%s))"
26N/A#define _F_SETMEMBER_SSD "(&(%%s)(cn=%s))"
26N/A
29N/A#define N_HASH 257
26N/A#define COMMA ','
2N/A
38N/Astatic const char *netgrent_attrs[] = {
2N/A _N_TRIPLE,
6N/A _N_MEMBER,
2N/A (char *)NULL
58N/A};
2N/A
145N/Atypedef struct netgroup_name {
145N/A char *name;
34N/A struct netgroup_name *next;
34N/A struct netgroup_name *next_hash;
34N/A} netgroup_name_t;
49N/A
145N/Atypedef struct {
2N/A netgroup_name_t *hash_list[N_HASH];
34N/A netgroup_name_t *to_do;
34N/A netgroup_name_t *done;
94N/A} netgroup_table_t;
94N/A
156N/Atypedef struct {
156N/A ns_ldap_result_t *results;
166N/A ns_ldap_entry_t *entry;
166N/A char **attrs;
34N/A char *netgroup;
59N/A netgroup_table_t tab;
72N/A} getnetgrent_cookie_t;
72N/A
72N/Atypedef struct {
59N/A struct nss_innetgr_args *ia;
2N/A const char *ssd_filter;
85N/A const char *netgrname;
61N/A const char *membername;
61N/A netgroup_table_t tab;
61N/A} innetgr_cookie_t;
61N/A
61N/Atypedef unsigned int hash_t;
61N/A
61N/Astatic hash_t
61N/Aget_hash(const char *s)
61N/A{
152N/A unsigned int sum = 0;
74N/A unsigned int i;
61N/A
85N/A for (i = 0; s[i] != '\0'; i++)
61N/A sum += ((unsigned char *)s)[i];
61N/A
61N/A return ((sum + i) % N_HASH);
61N/A}
61N/A
61N/A/*
61N/A * Adds a name to the netgroup table
61N/A *
152N/A * Returns
152N/A * 0 if successfully added or already present
61N/A * -1 if memory allocation error or NULL netgroup_table_t
61N/A * from caller.
43N/A */
43N/A
120N/Astatic int
64N/Aadd_netgroup_name(const char *name, netgroup_table_t *tab)
64N/A{
64N/A hash_t h;
43N/A netgroup_name_t *ng;
43N/A netgroup_name_t *ng_new;
120N/A
64N/A if (tab == NULL) {
64N/A /*
64N/A * Should never happen. But if it does,
64N/A * that's an error condition.
64N/A */
64N/A return (-1);
64N/A }
64N/A if (name == NULL || *name == '\0') {
64N/A /* no name to add means success */
64N/A return (0);
64N/A }
64N/A
43N/A h = get_hash(name);
16N/A ng = tab->hash_list[h];
16N/A
16N/A while (ng != NULL) {
34N/A if (strcmp(name, ng->name) == 0)
34N/A break;
16N/A ng = ng->next_hash;
6N/A }
12N/A
12N/A if (ng == NULL) {
12N/A ng_new = (netgroup_name_t *)
12N/A calloc(1, sizeof (netgroup_name_t));
12N/A if (ng_new == NULL)
12N/A return (-1);
12N/A ng_new->name = strdup(name);
12N/A if (ng_new->name == NULL) {
12N/A free(ng_new);
12N/A return (-1);
12N/A }
61N/A ng_new->next_hash = tab->hash_list[h];
61N/A tab->hash_list[h] = ng_new;
61N/A ng_new->next = tab->to_do;
61N/A tab->to_do = ng_new;
61N/A }
12N/A return (0);
12N/A}
61N/A
61N/Astatic netgroup_name_t *
12N/Aget_next_netgroup(netgroup_table_t *tab)
2N/A{
99N/A netgroup_name_t *ng;
99N/A
99N/A if (tab == NULL)
99N/A return (NULL);
40N/A
40N/A ng = tab->to_do;
40N/A if (ng != NULL) {
40N/A tab->to_do = ng->next;
43N/A ng->next = tab->done;
40N/A tab->done = ng;
127N/A }
2N/A return (ng);
99N/A}
99N/A
2N/Astatic void
99N/Afree_netgroup_table(netgroup_table_t *tab)
99N/A{
2N/A netgroup_name_t *ng, *next;
151N/A
151N/A if (tab == NULL)
2N/A return;
151N/A
151N/A for (ng = tab->to_do; ng != NULL; ng = next) {
2N/A if (ng->name != NULL)
2N/A free(ng->name);
2N/A next = ng->next;
2N/A free(ng);
61N/A }
61N/A
61N/A for (ng = tab->done; ng != NULL; ng = next) {
61N/A if (ng->name != NULL)
61N/A free(ng->name);
16N/A next = ng->next;
16N/A free(ng);
148N/A }
148N/A (void) memset(tab, 0, sizeof (*tab));
148N/A}
148N/A
2N/A/*
47N/A * domain comparing routine
2N/A * n1: See if n1 is n2 or an ancestor of it
2N/A * n2: (in string terms, n1 is a suffix of n2)
2N/A * Returns ZERO for success, -1 for failure.
148N/A */
2N/Astatic int
146N/Adomcmp(const char *n1, const char *n2)
146N/A{
146N/A#define PASS 0
146N/A#define FAIL -1
146N/A
116N/A size_t l1, l2;
116N/A
127N/A if ((n1 == NULL) || (n2 == NULL))
127N/A return (FAIL);
2N/A
2N/A l1 = strlen(n1);
82N/A l2 = strlen(n2);
99N/A
99N/A /* Turn a blind eye to the presence or absence of trailing periods */
2N/A if (l1 != 0 && n1[l1 - 1] == '.') {
105N/A --l1;
2N/A }
70N/A if (l2 != 0 && n2[l2 - 1] == '.') {
2N/A --l2;
160N/A }
160N/A if (l1 > l2) { /* Can't be a suffix */
160N/A return (FAIL);
160N/A } else if (l1 == 0) { /* Trivially a suffix; */
165N/A /* (do we want this case?) */
2N/A return (PASS);
2N/A }
2N/A /* So 0 < l1 <= l2 */
7N/A if (l1 < l2 && n2[l2 - l1 - 1] != '.') {
142N/A return (FAIL);
43N/A }
43N/A if (strncasecmp(n1, &n2[l2 - l1], l1) == 0) {
16N/A return (PASS);
43N/A } else {
138N/A return (FAIL);
61N/A }
61N/A}
61N/A
61N/Astatic int
61N/Asplit_triple(char *triple, char **hostname, char **username, char **domain)
99N/A{
99N/A int i, syntax_err;
99N/A char *splittriple[3];
99N/A char *p = triple;
99N/A
99N/A#ifdef DEBUG
99N/A (void) fprintf(stdout, "\n[getnetgrent.c: split_triple]\n");
99N/A#endif /* DEBUG */
99N/A
61N/A if (triple == NULL)
99N/A return (-1);
99N/A
99N/A p++;
99N/A syntax_err = 0;
99N/A for (i = 0; i < 3; i++) {
61N/A char *start;
99N/A char *limit;
99N/A const char *terminators = ",) \t";
99N/A
99N/A if (i == 2) {
61N/A /* Don't allow comma */
61N/A terminators++;
61N/A }
61N/A while (isspace(*p)) {
99N/A p++;
99N/A }
99N/A start = p;
99N/A limit = strpbrk(start, terminators);
99N/A if (limit == 0) {
99N/A syntax_err++;
99N/A break;
99N/A }
99N/A p = limit;
99N/A while (isspace(*p)) {
99N/A p++;
99N/A }
99N/A if (*p == terminators[0]) {
99N/A /*
99N/A * Successfully parsed this name and
99N/A * the separator after it (comma or
99N/A * right paren); leave p ready for
99N/A * next parse.
102N/A */
108N/A p++;
102N/A if (start == limit) {
61N/A /* Wildcard */
99N/A splittriple[i] = NULL;
99N/A } else {
99N/A *limit = '\0';
99N/A splittriple[i] = start;
99N/A }
99N/A } else {
99N/A syntax_err++;
99N/A break;
99N/A }
99N/A }
99N/A
99N/A if (syntax_err != 0)
99N/A return (-1);
99N/A
99N/A *hostname = splittriple[0];
99N/A *username = splittriple[1];
99N/A *domain = splittriple[2];
99N/A
99N/A return (0);
165N/A}
165N/A
165N/A/*
165N/A * Test membership in triple
165N/A * return 0 = no match
165N/A * return 1 = match
165N/A */
99N/A
102N/Astatic int
102N/Amatch_triple_entry(struct nss_innetgr_args *ia, const ns_ldap_entry_t *entry)
102N/A{
102N/A int ndomains;
102N/A char **pdomains;
99N/A int nhost;
99N/A char **phost;
99N/A int nusers;
61N/A char **pusers;
61N/A char **attr;
61N/A char triple[MAX_TRIPLE_LEN];
61N/A char *tuser, *thost, *tdomain;
99N/A int i;
99N/A char *current, *limit;
102N/A int pulen, phlen;
99N/A char *pusers0, *phost0;
99N/A
99N/A nhost = ia->arg[NSS_NETGR_MACHINE].argc;
99N/A phost = (char **)ia->arg[NSS_NETGR_MACHINE].argv;
99N/A if (phost == NULL || *phost == NULL) {
99N/A nhost = 0;
102N/A } else {
99N/A phost0 = phost[0];
109N/A phlen = strlen(phost0);
109N/A#ifdef DEBUG
109N/A syslog(LOG_DEBUG, "nss_ldap: match_triple_entry: "
109N/A "entering with host: %s", phost0 ? phost0 : "");
109N/A#endif
109N/A }
109N/A nusers = ia->arg[NSS_NETGR_USER].argc;
109N/A pusers = (char **)ia->arg[NSS_NETGR_USER].argv;
109N/A if (pusers == NULL || *pusers == NULL) {
109N/A nusers = 0;
109N/A } else {
109N/A pusers0 = pusers[0];
99N/A pulen = strlen(pusers0);
99N/A#ifdef DEBUG
99N/A syslog(LOG_DEBUG, "nss_ldap: match_triple_entry: "
99N/A "entering with user: %s", pusers0 ? pusers0 : "");
99N/A#endif
99N/A }
99N/A ndomains = ia->arg[NSS_NETGR_DOMAIN].argc;
99N/A pdomains = (char **)ia->arg[NSS_NETGR_DOMAIN].argv;
99N/A if (pdomains == NULL || *pdomains == NULL)
99N/A ndomains = 0;
109N/A#ifdef DEBUG
109N/A else
61N/A syslog(LOG_DEBUG, "nss_ldap: match_triple_entry: "
99N/A "entering with domain: %s", pdomains[0] ? pdomains[0] : "");
99N/A#endif
99N/A
99N/A attr = __ns_ldap_getAttr(entry, _N_TRIPLE);
99N/A if (attr == NULL || *attr == NULL)
99N/A return (0);
99N/A
99N/A#ifdef DEBUG
99N/A syslog(LOG_DEBUG, "nss_ldap: match_triple_entry: "
99N/A "(nusers: %d, nhost:%d, ndomains: %d)",
99N/A nusers, nhost, ndomains);
61N/A#endif
99N/A
99N/A /* Special cases for speedup */
99N/A if (nusers == 1 && nhost == 0 && ndomains == 0) {
99N/A /* Special case for finding a single user in a netgroup */
99N/A for (; *attr; attr++) {
99N/A /* jump to first comma and check next character */
99N/A current = *attr;
99N/A#ifdef DEBUG
99N/A syslog(LOG_DEBUG, "nss_ldap: match_triple_entry: "
99N/A "current is: %s", current);
99N/A#endif
99N/A if ((current = strchr(current, COMMA)) == NULL)
99N/A continue;
99N/A current++;
99N/A
99N/A /* skip whitespaces */
99N/A while (isspace(*current))
99N/A current++;
61N/A
99N/A /* if user part is null, then treat as wildcard */
99N/A if (*current == COMMA)
99N/A return (1);
99N/A
99N/A /* compare first character */
99N/A if (*pusers0 != *current)
99N/A continue;
99N/A
99N/A /* limit username to COMMA */
99N/A if ((limit = strchr(current, COMMA)) == NULL)
99N/A continue;
99N/A *limit = '\0';
99N/A
99N/A /* remove blanks before COMMA */
99N/A if ((limit = strpbrk(current, " \t")) != NULL)
99N/A *limit = '\0';
99N/A
99N/A /* compare size of username */
99N/A if (pulen != strlen(current)) {
99N/A continue;
99N/A }
99N/A
170N/A /* do actual compare */
170N/A if (strncmp(pusers0, current, pulen) == 0) {
170N/A return (1);
99N/A } else {
99N/A continue;
61N/A }
99N/A }
99N/A } else if (nusers == 0 && nhost == 1 && ndomains == 0) {
99N/A /* Special case for finding a single host in a netgroup */
99N/A for (; *attr; attr++) {
99N/A
99N/A /* jump to first character and check */
99N/A current = *attr;
99N/A#ifdef DEBUG
99N/A syslog(LOG_DEBUG, "nss_ldap: match_triple_entry: "
99N/A "current is: %s", current);
99N/A#endif
99N/A current++;
61N/A
99N/A /* skip whitespaces */
99N/A while (isspace(*current))
99N/A current++;
99N/A
99N/A /* if host part is null, then treat as wildcard */
99N/A if (*current == COMMA)
99N/A return (1);
99N/A
61N/A /* limit hostname to COMMA */
99N/A if ((limit = strchr(current, COMMA)) == NULL)
99N/A continue;
99N/A *limit = '\0';
99N/A
99N/A /* remove blanks before COMMA */
99N/A if ((limit = strpbrk(current, " \t")) != NULL)
102N/A *limit = '\0';
170N/A
64N/A /* compare size of hostname */
62N/A if (phlen != strlen(current)) {
64N/A continue;
64N/A }
82N/A
64N/A /* do actual compare */
82N/A if (strncasecmp(phost0, current, phlen) == 0) {
64N/A return (1);
64N/A } else {
62N/A continue;
62N/A }
62N/A }
62N/A } else {
for (; *attr; attr++) {
if (strlcpy(triple, *attr,
sizeof (triple)) >= sizeof (triple))
continue;
#ifdef DEBUG
syslog(LOG_DEBUG, "nss_ldap: match_triple_entry: "
"triple is: %s", triple);
#endif
if (split_triple(triple, &thost, &tuser, &tdomain) != 0)
continue;
if (thost != NULL && *thost != '\0' && nhost != 0) {
for (i = 0; i < nhost; i++)
if (strcasecmp(thost, phost[i]) == 0)
break;
if (i == nhost)
continue;
}
if (tuser != NULL && *tuser != '\0' && nusers != 0) {
for (i = 0; i < nusers; i++)
if (strcmp(tuser, pusers[i]) == 0)
break;
if (i == nusers)
continue;
}
if (tdomain != NULL && *tdomain != '\0' &&
ndomains != 0) {
for (i = 0; i < ndomains; i++)
if (domcmp(tdomain, pdomains[i]) == 0)
break;
if (i == ndomains)
continue;
}
return (1);
}
}
return (0);
}
static int
match_triple(struct nss_innetgr_args *ia, ns_ldap_result_t *result)
{
ns_ldap_entry_t *entry;
for (entry = result->entry; entry != NULL; entry = entry->next)
if (match_triple_entry(ia, entry) == 1)
return (1);
return (0);
}
static int
add_netgroup_member_entry(ns_ldap_entry_t *entry, netgroup_table_t *tab)
{
char **attrs;
char **a;
attrs = __ns_ldap_getAttr(entry, _N_MEMBER);
if (attrs == NULL || *attrs == NULL)
return (0);
for (a = attrs; *a != NULL; a++) {}
do {
a--;
if (add_netgroup_name(*a, tab) != 0)
return (-1);
} while (a > attrs);
return (0);
}
static int
add_netgroup_member(ns_ldap_result_t *result, netgroup_table_t *tab)
{
ns_ldap_entry_t *entry;
int ret = 0;
for (entry = result->entry; entry != NULL; entry = entry->next) {
ret = add_netgroup_member_entry(entry, tab);
if (ret != 0)
break;
}
return (ret);
}
/*
* top_down_search checks only checks the netgroup specified in netgrname
*/
static nss_status_t
top_down_search(struct nss_innetgr_args *ia, char *netgrname)
{
char searchfilter[SEARCHFILTERLEN];
char name[SEARCHFILTERLEN];
char userdata[SEARCHFILTERLEN];
ns_ldap_result_t *result = NULL;
ns_ldap_error_t *error = NULL;
int rc;
nss_status_t status = NSS_NOTFOUND;
nss_status_t status1;
netgroup_table_t tab;
netgroup_name_t *ng;
int ret;
(void) memset(&tab, 0, sizeof (tab));
if (add_netgroup_name(netgrname, &tab) != 0)
return ((nss_status_t)NSS_NOTFOUND);
while ((ng = get_next_netgroup(&tab)) != NULL) {
#ifdef DEBUG
syslog(LOG_DEBUG, "nss_ldap: top_down_search: netgroup loop "
"(ng->name: %s)", ng->name ? ng->name : "null !");
#endif
if (_ldap_filter_name(name, ng->name, sizeof (name)) != 0)
break;
ret = snprintf(searchfilter, sizeof (searchfilter),
_F_SETMEMBER, name);
if (ret >= sizeof (searchfilter) || ret < 0)
break;
ret = snprintf(userdata, sizeof (userdata), _F_SETMEMBER_SSD,
name);
if (ret >= sizeof (userdata) || ret < 0)
break;
/* searching for current netgroup name entry */
rc = __ns_ldap_list(_NETGROUP, searchfilter,
_merge_SSD_filter, netgrent_attrs, NULL, 0, &result,
&error, NULL, userdata);
if (error != NULL) {
status1 = switch_err(rc, error);
if (status1 == NSS_TRYAGAIN) {
(void) __ns_ldap_freeError(&error);
free_netgroup_table(&tab);
return (status1);
}
}
(void) __ns_ldap_freeError(&error);
if (rc == NS_LDAP_SUCCESS) {
if (match_triple(ia, result) == 1) {
/* We found a match */
ia->status = NSS_NETGR_FOUND;
status = NSS_SUCCESS;
#ifdef DEBUG
syslog(LOG_DEBUG, "nss_ldap: top_down_search: "
"found match");
#endif
break;
}
/*
* No match found. Check for membernisnetgroup
* in result and if yes, start again with those.
*/
rc = add_netgroup_member(result, &tab);
if (rc != 0)
break;
} else if (rc != NS_LDAP_NOTFOUND) {
break;
}
(void) __ns_ldap_freeResult(&result);
}
(void) __ns_ldap_freeResult(&result);
free_netgroup_table(&tab);
return (status);
}
/*
* __netgr_in checks only checks the netgroup specified in ngroup
*/
static nss_status_t
__netgr_in(void *a, char *netgrname)
{
struct nss_innetgr_args *ia = (struct nss_innetgr_args *)a;
nss_status_t status = NSS_NOTFOUND;
#ifdef DEBUG
(void) fprintf(stdout, "\n[getnetgrent.c: netgr_in]\n");
(void) fprintf(stdout, "\tmachine: argc[%d]='%s' user: "
"argc[%d]='%s',\n\tdomain:argc[%d]='%s' "
"netgroup: argc[%d]='%s'\n",
NSS_NETGR_MACHINE,
PRINT_VAL(ia->arg[NSS_NETGR_MACHINE]),
NSS_NETGR_USER,
PRINT_VAL(ia->arg[NSS_NETGR_USER]),
NSS_NETGR_DOMAIN,
PRINT_VAL(ia->arg[NSS_NETGR_DOMAIN]),
NSS_NETGR_N,
PRINT_VAL(ia->arg[NSS_NETGR_N]));
(void) fprintf(stdout, "\tgroups='%s'\n", netgrname);
#endif /* DEBUG */
ia->status = NSS_NETGR_NO;
if (netgrname == NULL)
return (status);
return (top_down_search(ia, netgrname));
}
/*ARGSUSED0*/
static nss_status_t
netgr_in(ldap_backend_ptr be, void *a)
{
struct nss_innetgr_args *ia = (struct nss_innetgr_args *)a;
int i;
nss_status_t rc = (nss_status_t)NSS_NOTFOUND;
ia->status = NSS_NETGR_NO;
for (i = 0; i < ia->groups.argc; i++) {
rc = __netgr_in(a, ia->groups.argv[i]);
if (ia->status == NSS_NETGR_FOUND)
return (NSS_SUCCESS);
}
return (rc);
}
/*
*
*/
static nss_status_t
getnetgr_ldap_setent(ldap_backend_ptr be, void *a)
{
const char *netgroup = (const char *) a;
getnetgrent_cookie_t *cookie;
#ifdef DEBUG
(void) fprintf(stdout, "\n[getnetgrent.c: getnetgr_ldap_setent]\n");
#endif /* DEBUG */
cookie = (getnetgrent_cookie_t *)be->netgroup_cookie;
if (cookie != NULL && cookie->netgroup != NULL) {
/* is this another set on the same netgroup */
if (strcmp(cookie->netgroup, netgroup) == 0)
return ((nss_status_t)NSS_SUCCESS);
}
return (NSS_NOTFOUND);
}
static void
free_getnetgrent_cookie(getnetgrent_cookie_t **cookie)
{
getnetgrent_cookie_t *p = *cookie;
#ifdef DEBUG
(void) fprintf(stdout, "\n[getnetgrent.c: free_getnetgrent_cookie]\n");
#endif /* DEBUG */
if (p == NULL)
return;
(void) __ns_ldap_freeResult(&p->results);
free_netgroup_table(&p->tab);
free(p->netgroup);
free(p);
*cookie = NULL;
}
/*ARGSUSED1*/
static nss_status_t
getnetgr_ldap_endent(ldap_backend_ptr be, void *a)
{
#ifdef DEBUG
(void) fprintf(stdout, "\n[getnetgrent.c: getnetgr_ldap_endent]\n");
#endif /* DEBUG */
free_getnetgrent_cookie((getnetgrent_cookie_t **)&be->netgroup_cookie);
return ((nss_status_t)NSS_NOTFOUND);
}
/*ARGSUSED1*/
static nss_status_t
getnetgr_ldap_destr(ldap_backend_ptr be, void *a)
{
#ifdef DEBUG
(void) fprintf(stdout, "\n[getnetgrent.c: getnetgr_ldap_destr]\n");
#endif /* DEBUG */
free_getnetgrent_cookie((getnetgrent_cookie_t **)&be->netgroup_cookie);
free(be);
return ((nss_status_t)NSS_NOTFOUND);
}
static nss_status_t
getnetgr_ldap_getent(ldap_backend_ptr be, void *a)
{
struct nss_getnetgrent_args *args;
getnetgrent_cookie_t *p;
char searchfilter[SEARCHFILTERLEN];
char userdata[SEARCHFILTERLEN];
char name[SEARCHFILTERLEN];
int rc;
ns_ldap_result_t *result = NULL;
ns_ldap_error_t *error = NULL;
char **attrs;
char *hostname, *username, *domain;
char *buffer;
nss_status_t status = NSS_SUCCESS;
netgroup_name_t *ng;
int ret;
#ifdef DEBUG
(void) fprintf(stdout, "\n[getnetgrent.c: getnetgr_ldap_getent]\n");
#endif /* DEBUG */
args = (struct nss_getnetgrent_args *)a;
args->status = NSS_NETGR_NO;
p = (getnetgrent_cookie_t *)be->netgroup_cookie;
if (p == NULL)
return ((nss_status_t)NSS_SUCCESS);
for (;;) {
/*
* Search through each netgroup consecutively: only search
* next netgroup when results from previous netgroup are
* processed.
* Needed for nested netgroup (memberNisNetgroup attributes).
*/
if (p->results == NULL) {
if ((ng = get_next_netgroup(&p->tab)) != NULL) {
if (_ldap_filter_name(name, ng->name,
sizeof (name)) != 0)
break;
ret = snprintf(searchfilter,
sizeof (searchfilter),
_F_SETMEMBER, name);
if (ret >= sizeof (searchfilter) || ret < 0)
break;
#ifdef DEBUG
syslog(LOG_DEBUG, "nss_ldap: "
"getnetgr_ldap_getent: "
"netgroup name: %s", name);
#endif
ret = snprintf(userdata, sizeof (userdata),
_F_SETMEMBER_SSD, name);
if (ret >= sizeof (userdata) || ret < 0)
break;
result = NULL;
rc = __ns_ldap_list(_NETGROUP, searchfilter,
_merge_SSD_filter, netgrent_attrs, NULL,
0, &result, &error, NULL, userdata);
(void) __ns_ldap_freeError(&error);
if (rc == NS_LDAP_SUCCESS && result != NULL) {
p->results = result;
} else {
#ifdef DEBUG
syslog(LOG_DEBUG, "nss_ldap: "
"getnetgr_ldap_getent: "
"__ns_ldap_list() returned %d "
"(result: 0x%x)", rc, result);
#endif
/*
* Will exit when no more netgroup
* to search and no more p->results
* to process.
*/
(void) __ns_ldap_freeResult(&result);
}
} else { /* no more netgroup to process */
/*
* If no more results to process, and since
* there's no more netgroup to process either,
* then it's time to break and exit the for
* loop.
*/
#ifdef DEBUG
syslog(LOG_DEBUG, "nss_ldap: "
"getnetgr_ldap_getent: no more netgroup "
"to process, p->results: 0x%x",
p->results);
#endif
if (p->results == NULL)
break;
}
}
if (p->results == NULL)
continue;
if (p->entry == NULL)
p->entry = p->results->entry;
if (p->entry == NULL)
continue;
if (p->attrs == NULL) {
attrs = __ns_ldap_getAttr(p->entry, _N_TRIPLE);
if (attrs != NULL && *attrs != NULL)
p->attrs = attrs;
}
if (p->attrs != NULL) {
attrs = p->attrs;
buffer = args->buffer;
if (strlcpy(buffer, *attrs, args->buflen) >=
args->buflen) {
status = NSS_STR_PARSE_ERANGE;
break;
}
rc = split_triple(buffer, &hostname, &username,
&domain);
attrs++;
if (attrs != NULL && *attrs != NULL)
p->attrs = attrs;
else
p->attrs = NULL;
if (rc == 0) {
args->retp[NSS_NETGR_MACHINE] = hostname;
args->retp[NSS_NETGR_USER] = username;
args->retp[NSS_NETGR_DOMAIN] = domain;
args->status = NSS_NETGR_FOUND;
#ifdef DEBUG
syslog(LOG_DEBUG, "nss_ldap: "
"getnetgr_ldap_getent: found triple "
"(%s, %s, %s), 0x%x to process",
hostname ? hostname : "",
username ? username : "",
domain ? domain : "",
p->attrs);
#endif
if (p->attrs != NULL)
break;
}
}
if (p->attrs == NULL) {
rc = add_netgroup_member_entry(p->entry, &p->tab);
if (rc != 0) {
args->status = NSS_NETGR_NO;
break;
}
p->entry = p->entry->next;
if (p->entry == NULL)
(void) __ns_ldap_freeResult(&p->results);
if (args->status == NSS_NETGR_FOUND)
break;
}
}
return (status);
}
static ldap_backend_op_t getnetgroup_ops[] = {
getnetgr_ldap_destr,
getnetgr_ldap_endent,
getnetgr_ldap_setent,
getnetgr_ldap_getent,
};
/*
*
*/
static nss_status_t
netgr_set(ldap_backend_ptr be, void *a)
{
struct nss_setnetgrent_args *args =
(struct nss_setnetgrent_args *)a;
ldap_backend_ptr get_be;
getnetgrent_cookie_t *p;
#ifdef DEBUG
(void) fprintf(stdout, "\n[getnetgrent.c: netgr_set]\n");
(void) fprintf(stdout,
"\targs->netgroup: %s\n", ISNULL(args->netgroup));
#endif /* DEBUG */
if (args->netgroup == NULL)
return ((nss_status_t)NSS_NOTFOUND);
free_getnetgrent_cookie((getnetgrent_cookie_t **)&be->netgroup_cookie);
p = (getnetgrent_cookie_t *)calloc(1, sizeof (getnetgrent_cookie_t));
if (p == NULL)
return ((nss_status_t)NSS_NOTFOUND);
p->netgroup = strdup(args->netgroup);
if (p->netgroup == NULL) {
free(p);
return ((nss_status_t)NSS_NOTFOUND);
}
if (add_netgroup_name(args->netgroup, &p->tab) == -1) {
free_getnetgrent_cookie(&p);
return ((nss_status_t)NSS_NOTFOUND);
}
/* now allocate and return iteration backend structure */
if ((get_be = (ldap_backend_ptr)malloc(sizeof (*get_be))) == NULL)
return (NSS_UNAVAIL);
get_be->ops = getnetgroup_ops;
get_be->nops = sizeof (getnetgroup_ops) / sizeof (getnetgroup_ops[0]);
get_be->tablename = NULL;
get_be->attrs = netgrent_attrs;
get_be->result = NULL;
get_be->ldapobj2str = NULL;
get_be->setcalled = 1;
get_be->filter = NULL;
get_be->toglue = NULL;
get_be->enumcookie = NULL;
get_be->netgroup_cookie = p;
args->iterator = (nss_backend_t *)get_be;
(void) __ns_ldap_freeResult(&be->result);
return (NSS_SUCCESS);
}
/*ARGSUSED1*/
static nss_status_t
netgr_ldap_destr(ldap_backend_ptr be, void *a)
{
#ifdef DEBUG
(void) fprintf(stdout, "\n[getnetgrent.c: netgr_ldap_destr]\n");
#endif /* DEBUG */
(void) _clean_ldap_backend(be);
return ((nss_status_t)NSS_NOTFOUND);
}
static ldap_backend_op_t netgroup_ops[] = {
netgr_ldap_destr,
0,
0,
0,
netgr_in, /* innetgr() */
netgr_set /* setnetgrent() */
};
/*
* _nss_ldap_netgroup_constr is where life begins. This function calls the
* generic ldap constructor function to define and build the abstract data
* types required to support ldap operations.
*/
/*ARGSUSED0*/
nss_backend_t *
_nss_ldap_netgroup_constr(const char *dummy1, const char *dummy2,
const char *dummy3)
{
#ifdef DEBUG
(void) fprintf(stdout,
"\n[getnetgrent.c: _nss_ldap_netgroup_constr]\n");
#endif /* DEBUG */
return ((nss_backend_t *)_nss_ldap_constr(netgroup_ops,
sizeof (netgroup_ops)/sizeof (netgroup_ops[0]), _NETGROUP,
netgrent_attrs, NULL));
}