2000-01-23 01:40:58 +03:00
|
|
|
/* $NetBSD: getnetgrent.c,v 1.28 2000/01/22 22:40:58 mycroft Exp $ */
|
1995-02-25 11:50:56 +03:00
|
|
|
|
1994-05-17 17:30:51 +04:00
|
|
|
/*
|
1994-12-04 21:12:12 +03:00
|
|
|
* Copyright (c) 1994 Christos Zoulas
|
|
|
|
* All rights reserved.
|
1994-05-17 17:30:51 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
1994-12-04 21:12:12 +03:00
|
|
|
* This product includes software developed by Christos Zoulas.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
1994-05-17 17:30:51 +04:00
|
|
|
*
|
1994-12-04 21:12:12 +03:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
1994-05-17 17:30:51 +04:00
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-07-13 23:12:05 +04:00
|
|
|
#include <sys/cdefs.h>
|
1994-05-17 17:30:51 +04:00
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
2000-01-23 01:40:58 +03:00
|
|
|
__RCSID("$NetBSD: getnetgrent.c,v 1.28 2000/01/22 22:40:58 mycroft Exp $");
|
1994-05-17 17:30:51 +04:00
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
1997-07-13 23:12:05 +04:00
|
|
|
#include "namespace.h"
|
1996-04-27 22:59:05 +04:00
|
|
|
#include <sys/types.h>
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <db.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
1996-04-27 22:59:05 +04:00
|
|
|
#define _NETGROUP_PRIVATE
|
1994-12-04 21:12:12 +03:00
|
|
|
#include <netgroup.h>
|
1999-01-16 10:47:18 +03:00
|
|
|
#include <nsswitch.h>
|
1999-09-16 15:44:54 +04:00
|
|
|
#include <stdio.h>
|
1994-10-19 06:13:22 +03:00
|
|
|
#include <stdlib.h>
|
1999-09-16 15:44:54 +04:00
|
|
|
#include <string.h>
|
1997-01-17 10:26:18 +03:00
|
|
|
#include <stringlist.h>
|
1999-09-16 15:44:54 +04:00
|
|
|
|
1996-12-20 23:46:19 +03:00
|
|
|
#ifdef YP
|
1999-01-16 10:47:18 +03:00
|
|
|
#include <rpc/rpc.h>
|
1996-12-20 23:46:19 +03:00
|
|
|
#include <rpcsvc/ypclnt.h>
|
1999-01-16 10:47:18 +03:00
|
|
|
#include <rpcsvc/yp_prot.h>
|
1996-12-20 23:46:19 +03:00
|
|
|
#endif
|
1999-04-18 06:04:04 +04:00
|
|
|
|
|
|
|
#ifdef __STDC__
|
|
|
|
#include <stdarg.h>
|
|
|
|
#else
|
|
|
|
#include <varargs.h>
|
|
|
|
#endif
|
1999-01-16 10:47:18 +03:00
|
|
|
|
1997-07-21 18:06:24 +04:00
|
|
|
#ifdef __weak_alias
|
2000-01-23 01:19:07 +03:00
|
|
|
__weak_alias(endnetgrent,_endnetgrent)
|
|
|
|
__weak_alias(getnetgrent,_getnetgrent)
|
|
|
|
__weak_alias(innetgr,_innetgr)
|
|
|
|
__weak_alias(setnetgrent,_setnetgrent)
|
1997-07-21 18:06:24 +04:00
|
|
|
#endif
|
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
#define _NG_STAR(s) (((s) == NULL || *(s) == '\0') ? _ngstar : s)
|
1996-04-27 22:59:05 +04:00
|
|
|
#define _NG_EMPTY(s) ((s) == NULL ? "" : s)
|
1994-12-04 21:12:12 +03:00
|
|
|
#define _NG_ISSPACE(p) (isspace((unsigned char) (p)) || (p) == '\n')
|
1994-05-17 17:30:51 +04:00
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
static const char _ngstar[] = "*";
|
1994-12-12 22:05:31 +03:00
|
|
|
static struct netgroup *_nghead = (struct netgroup *)NULL;
|
|
|
|
static struct netgroup *_nglist = (struct netgroup *)NULL;
|
1994-12-04 21:12:12 +03:00
|
|
|
static DB *_ng_db;
|
1994-05-17 17:30:51 +04:00
|
|
|
|
1999-11-28 07:30:15 +03:00
|
|
|
static int getstring __P((char **, int, __aconst char **));
|
1994-12-04 21:12:12 +03:00
|
|
|
static struct netgroup *getnetgroup __P((char **));
|
1999-01-16 10:47:18 +03:00
|
|
|
static int lookup __P((char *, char **, int));
|
1999-11-28 07:30:15 +03:00
|
|
|
static int addgroup __P((StringList *, char *));
|
1994-12-04 21:12:12 +03:00
|
|
|
static int in_check __P((const char *, const char *,
|
|
|
|
const char *, struct netgroup *));
|
1999-01-16 10:47:18 +03:00
|
|
|
static int in_find __P((StringList *, char *, const char *,
|
1994-12-04 21:12:12 +03:00
|
|
|
const char *, const char *));
|
1999-01-16 10:47:18 +03:00
|
|
|
static char *in_lookup1 __P((const char *, const char *, int));
|
1994-12-04 21:12:12 +03:00
|
|
|
static int in_lookup __P((const char *, const char *,
|
1999-01-16 10:47:18 +03:00
|
|
|
const char *, int));
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1999-01-22 01:21:13 +03:00
|
|
|
static const ns_src default_files_nis[] = {
|
1999-01-22 06:25:31 +03:00
|
|
|
{ NSSRC_FILES, NS_SUCCESS | NS_NOTFOUND },
|
1999-01-22 01:21:13 +03:00
|
|
|
#ifdef YP
|
|
|
|
{ NSSRC_NIS, NS_SUCCESS },
|
|
|
|
#endif
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
/*
|
|
|
|
* getstring(): Get a string delimited by the character, skipping leading and
|
|
|
|
* trailing blanks and advancing the pointer
|
|
|
|
*/
|
1994-12-12 22:05:31 +03:00
|
|
|
static int
|
|
|
|
getstring(pp, del, str)
|
1994-12-04 21:12:12 +03:00
|
|
|
char **pp;
|
|
|
|
int del;
|
1998-07-27 13:47:44 +04:00
|
|
|
char __aconst **str;
|
1994-12-04 21:12:12 +03:00
|
|
|
{
|
1998-02-26 06:13:18 +03:00
|
|
|
size_t len;
|
1994-12-04 21:12:12 +03:00
|
|
|
char *sp, *ep, *dp;
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(pp != NULL);
|
|
|
|
_DIAGASSERT(str != NULL);
|
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
/* skip leading blanks */
|
|
|
|
for (sp = *pp; *sp && _NG_ISSPACE(*sp); sp++)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* accumulate till delimiter or space */
|
|
|
|
for (ep = sp; *ep && *ep != del && !_NG_ISSPACE(*ep); ep++)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* hunt for the delimiter */
|
|
|
|
for (dp = ep; *dp && *dp != del && _NG_ISSPACE(*dp); dp++)
|
|
|
|
continue;
|
|
|
|
|
1994-12-12 22:05:31 +03:00
|
|
|
if (*dp != del) {
|
|
|
|
*str = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
1994-12-04 21:12:12 +03:00
|
|
|
|
|
|
|
*pp = ++dp;
|
|
|
|
|
1998-02-26 06:13:18 +03:00
|
|
|
len = (ep - sp) + 1;
|
|
|
|
if (len > 1) {
|
|
|
|
dp = malloc(len);
|
1994-12-12 22:05:31 +03:00
|
|
|
if (dp == NULL)
|
1999-11-28 07:30:15 +03:00
|
|
|
return 0;
|
1998-02-26 06:13:18 +03:00
|
|
|
memcpy(dp, sp, len);
|
|
|
|
dp[len - 1] = '\0';
|
1994-12-12 22:05:31 +03:00
|
|
|
} else
|
|
|
|
dp = NULL;
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1994-12-12 22:05:31 +03:00
|
|
|
*str = dp;
|
|
|
|
return 1;
|
1994-12-04 21:12:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* getnetgroup(): Parse a netgroup, and advance the pointer
|
|
|
|
*/
|
|
|
|
static struct netgroup *
|
|
|
|
getnetgroup(pp)
|
|
|
|
char **pp;
|
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
struct netgroup *ng;
|
|
|
|
|
|
|
|
_DIAGASSERT(pp != NULL);
|
|
|
|
_DIAGASSERT(*pp != NULL);
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
ng = malloc(sizeof(struct netgroup));
|
1994-12-04 21:12:12 +03:00
|
|
|
if (ng == NULL)
|
1999-11-28 07:30:15 +03:00
|
|
|
return NULL;
|
1994-12-04 21:12:12 +03:00
|
|
|
|
|
|
|
(*pp)++; /* skip '(' */
|
1994-12-12 22:05:31 +03:00
|
|
|
if (!getstring(pp, ',', &ng->ng_host))
|
1994-12-04 21:12:12 +03:00
|
|
|
goto badhost;
|
|
|
|
|
1994-12-12 22:05:31 +03:00
|
|
|
if (!getstring(pp, ',', &ng->ng_user))
|
1994-12-04 21:12:12 +03:00
|
|
|
goto baduser;
|
|
|
|
|
1994-12-12 22:05:31 +03:00
|
|
|
if (!getstring(pp, ')', &ng->ng_domain))
|
1994-12-04 21:12:12 +03:00
|
|
|
goto baddomain;
|
|
|
|
|
|
|
|
#ifdef DEBUG_NG
|
1996-04-27 22:59:05 +04:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
(void) fprintf(stderr, "netgroup %s\n",
|
|
|
|
_ng_print(buf, sizeof(buf), ng));
|
|
|
|
}
|
1994-12-04 21:12:12 +03:00
|
|
|
#endif
|
|
|
|
return ng;
|
|
|
|
|
|
|
|
baddomain:
|
1994-12-12 22:05:31 +03:00
|
|
|
if (ng->ng_user)
|
1998-07-26 23:34:10 +04:00
|
|
|
free((char *)ng->ng_user);
|
1994-12-04 21:12:12 +03:00
|
|
|
baduser:
|
1994-12-12 22:05:31 +03:00
|
|
|
if (ng->ng_host)
|
1998-07-26 23:34:10 +04:00
|
|
|
free((char *)ng->ng_host);
|
1994-12-04 21:12:12 +03:00
|
|
|
badhost:
|
|
|
|
free(ng);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
static int _local_lookup __P((void *, void *, va_list));
|
|
|
|
|
1999-01-18 23:36:32 +03:00
|
|
|
/*ARGSUSED*/
|
1994-12-04 21:12:12 +03:00
|
|
|
static int
|
1999-01-16 10:47:18 +03:00
|
|
|
_local_lookup(rv, cb_data, ap)
|
|
|
|
void *rv;
|
|
|
|
void *cb_data;
|
|
|
|
va_list ap;
|
1994-12-04 21:12:12 +03:00
|
|
|
{
|
1999-01-16 10:47:18 +03:00
|
|
|
char *name = va_arg(ap, char *);
|
|
|
|
char **line = va_arg(ap, char **);
|
|
|
|
int bywhat = va_arg(ap, int);
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
DBT key, data;
|
|
|
|
size_t len;
|
|
|
|
char *ks;
|
|
|
|
int r;
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
if (_ng_db == NULL)
|
|
|
|
return NS_UNAVAIL;
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
len = strlen(name) + 2;
|
|
|
|
ks = malloc(len);
|
|
|
|
if (ks == NULL)
|
1999-11-28 07:30:15 +03:00
|
|
|
return NS_UNAVAIL;
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
ks[0] = bywhat;
|
|
|
|
memcpy(&ks[1], name, len - 1);
|
|
|
|
|
|
|
|
key.data = (u_char *) ks;
|
|
|
|
key.size = len;
|
|
|
|
|
|
|
|
r = (_ng_db->get) (_ng_db, &key, &data, 0);
|
|
|
|
free(ks);
|
|
|
|
switch (r) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
return NS_NOTFOUND;
|
|
|
|
case -1:
|
1999-01-22 06:25:31 +03:00
|
|
|
/* XXX: call endnetgrent() here ? */
|
1999-01-16 10:47:18 +03:00
|
|
|
return NS_UNAVAIL;
|
|
|
|
}
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
*line = strdup(data.data);
|
|
|
|
if (*line == NULL)
|
|
|
|
return NS_UNAVAIL;
|
|
|
|
return NS_SUCCESS;
|
|
|
|
}
|
1994-12-04 21:12:12 +03:00
|
|
|
|
|
|
|
#ifdef YP
|
1999-01-16 10:47:18 +03:00
|
|
|
static int _nis_lookup __P((void *, void *, va_list));
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1999-01-18 23:36:32 +03:00
|
|
|
/*ARGSUSED*/
|
1999-01-16 10:47:18 +03:00
|
|
|
static int
|
|
|
|
_nis_lookup(rv, cb_data, ap)
|
|
|
|
void *rv;
|
|
|
|
void *cb_data;
|
|
|
|
va_list ap;
|
|
|
|
{
|
|
|
|
char *name = va_arg(ap, char *);
|
|
|
|
char **line = va_arg(ap, char **);
|
|
|
|
int bywhat = va_arg(ap, int);
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
static char *__ypdomain;
|
|
|
|
int i;
|
|
|
|
char *map = NULL;
|
1994-12-12 22:05:31 +03:00
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
if(__ypdomain == NULL) {
|
|
|
|
switch (yp_get_default_domain(&__ypdomain)) {
|
|
|
|
case 0:
|
1994-12-12 22:05:31 +03:00
|
|
|
break;
|
1999-01-16 10:47:18 +03:00
|
|
|
case YPERR_RESRC:
|
|
|
|
return NS_TRYAGAIN;
|
|
|
|
default:
|
|
|
|
return NS_UNAVAIL;
|
1994-12-12 22:05:31 +03:00
|
|
|
}
|
1999-01-16 10:47:18 +03:00
|
|
|
}
|
1994-12-12 22:05:31 +03:00
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
switch (bywhat) {
|
|
|
|
case _NG_KEYBYNAME:
|
|
|
|
map = "netgroup";
|
|
|
|
break;
|
1994-12-12 22:05:31 +03:00
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
case _NG_KEYBYUSER:
|
|
|
|
map = "netgroup.byuser";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case _NG_KEYBYHOST:
|
|
|
|
map = "netgroup.byhost";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
abort();
|
1994-12-12 22:05:31 +03:00
|
|
|
}
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
*line = NULL;
|
|
|
|
switch (yp_match(__ypdomain, map, name, (int)strlen(name), line, &i)) {
|
|
|
|
case 0:
|
|
|
|
return NS_SUCCESS;
|
|
|
|
case YPERR_KEY:
|
|
|
|
if (*line)
|
|
|
|
free(*line);
|
|
|
|
return NS_NOTFOUND;
|
|
|
|
default:
|
|
|
|
if (*line)
|
|
|
|
free(*line);
|
|
|
|
return NS_UNAVAIL;
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
1994-05-17 17:30:51 +04:00
|
|
|
}
|
1999-01-16 10:47:18 +03:00
|
|
|
#endif
|
1994-05-17 17:30:51 +04:00
|
|
|
|
1999-11-28 07:30:15 +03:00
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
/*
|
|
|
|
* lookup(): Find the given key in the database or yp, and return its value
|
|
|
|
* in *line; returns 1 if key was found, 0 otherwise
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
lookup(name, line, bywhat)
|
|
|
|
char *name;
|
|
|
|
char **line;
|
|
|
|
int bywhat;
|
|
|
|
{
|
|
|
|
int r;
|
1999-01-19 11:30:46 +03:00
|
|
|
static const ns_dtab dtab[] = {
|
1999-01-19 11:07:58 +03:00
|
|
|
NS_FILES_CB(_local_lookup, NULL)
|
1999-01-22 01:21:13 +03:00
|
|
|
NS_NIS_CB(_nis_lookup, NULL)
|
1999-01-19 11:07:58 +03:00
|
|
|
{ 0 }
|
1999-01-16 10:47:18 +03:00
|
|
|
};
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(name != NULL);
|
|
|
|
_DIAGASSERT(line != NULL);
|
|
|
|
|
1999-01-22 01:21:13 +03:00
|
|
|
r = nsdispatch(NULL, dtab, NSDB_NETGROUP, "lookup", default_files_nis,
|
1999-01-19 11:07:58 +03:00
|
|
|
name, line, bywhat);
|
1999-01-16 10:47:18 +03:00
|
|
|
return (r == NS_SUCCESS) ? 1 : 0;
|
|
|
|
}
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1994-05-17 17:30:51 +04:00
|
|
|
/*
|
1994-12-04 21:12:12 +03:00
|
|
|
* _ng_parse(): Parse a line and return: _NG_ERROR: Syntax Error _NG_NONE:
|
|
|
|
* line was empty or a comment _NG_GROUP: line had a netgroup definition,
|
|
|
|
* returned in ng _NG_NAME: line had a netgroup name, returned in name
|
|
|
|
*
|
|
|
|
* Public since used by netgroup_mkdb
|
1994-05-17 17:30:51 +04:00
|
|
|
*/
|
|
|
|
int
|
1994-12-04 21:12:12 +03:00
|
|
|
_ng_parse(p, name, ng)
|
|
|
|
char **p;
|
|
|
|
char **name;
|
|
|
|
struct netgroup **ng;
|
1994-05-17 17:30:51 +04:00
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
_DIAGASSERT(p != NULL);
|
|
|
|
_DIAGASSERT(*p != NULL);
|
|
|
|
_DIAGASSERT(name != NULL);
|
|
|
|
_DIAGASSERT(ng != NULL);
|
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
while (**p) {
|
|
|
|
if (**p == '#')
|
|
|
|
/* comment */
|
|
|
|
return _NG_NONE;
|
|
|
|
|
|
|
|
while (**p && _NG_ISSPACE(**p))
|
|
|
|
/* skipblank */
|
|
|
|
(*p)++;
|
|
|
|
|
|
|
|
if (**p == '(') {
|
1999-11-28 07:30:15 +03:00
|
|
|
if ((*ng = getnetgroup(p)) == NULL)
|
1994-12-04 21:12:12 +03:00
|
|
|
return _NG_ERROR;
|
|
|
|
return _NG_GROUP;
|
|
|
|
} else {
|
1999-01-16 10:47:18 +03:00
|
|
|
char *np;
|
|
|
|
size_t i;
|
1994-05-17 17:30:51 +04:00
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
for (np = *p; **p && !_NG_ISSPACE(**p); (*p)++)
|
|
|
|
continue;
|
|
|
|
if (np != *p) {
|
|
|
|
i = (*p - np) + 1;
|
|
|
|
*name = malloc(i);
|
|
|
|
if (*name == NULL)
|
1999-11-28 07:30:15 +03:00
|
|
|
return _NG_ERROR;
|
1994-12-04 21:12:12 +03:00
|
|
|
memcpy(*name, np, i);
|
|
|
|
(*name)[i - 1] = '\0';
|
|
|
|
return _NG_NAME;
|
|
|
|
}
|
|
|
|
}
|
1994-05-17 17:30:51 +04:00
|
|
|
}
|
1994-12-04 21:12:12 +03:00
|
|
|
return _NG_NONE;
|
1994-05-17 17:30:51 +04:00
|
|
|
}
|
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1994-05-17 17:30:51 +04:00
|
|
|
/*
|
1999-11-28 07:30:15 +03:00
|
|
|
* addgroup(): Recursively add all the members of the netgroup to this group.
|
|
|
|
* returns 0 upon failure, nonzero upon success.
|
|
|
|
* grp is not a valid pointer after return (either free(3)ed or allocated
|
|
|
|
* to a stringlist). in either case, it shouldn't be used again.
|
1994-05-17 17:30:51 +04:00
|
|
|
*/
|
1999-11-28 07:30:15 +03:00
|
|
|
static int
|
1999-01-16 10:47:18 +03:00
|
|
|
addgroup(sl, grp)
|
1997-01-17 10:26:18 +03:00
|
|
|
StringList *sl;
|
|
|
|
char *grp;
|
1994-05-17 17:30:51 +04:00
|
|
|
{
|
1994-12-04 21:12:12 +03:00
|
|
|
char *line, *p;
|
|
|
|
struct netgroup *ng;
|
|
|
|
char *name;
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(sl != NULL);
|
|
|
|
_DIAGASSERT(grp != NULL);
|
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
#ifdef DEBUG_NG
|
|
|
|
(void) fprintf(stderr, "addgroup(%s)\n", grp);
|
|
|
|
#endif
|
|
|
|
/* check for cycles */
|
1997-01-17 10:26:18 +03:00
|
|
|
if (sl_find(sl, grp) != NULL) {
|
1994-12-12 22:05:31 +03:00
|
|
|
free(grp);
|
1997-07-13 23:12:05 +04:00
|
|
|
warnx("netgroup: Cycle in group `%s'", grp);
|
1999-11-28 07:30:15 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (sl_add(sl, grp) == -1) {
|
|
|
|
free(grp);
|
|
|
|
return 0;
|
1994-05-17 17:30:51 +04:00
|
|
|
}
|
1994-12-04 21:12:12 +03:00
|
|
|
|
|
|
|
/* Lookup this netgroup */
|
1999-01-16 10:47:18 +03:00
|
|
|
line = NULL;
|
|
|
|
if (!lookup(grp, &line, _NG_KEYBYNAME)) {
|
|
|
|
if (line != NULL)
|
|
|
|
free(line);
|
1999-11-28 07:30:15 +03:00
|
|
|
return 0;
|
1999-01-16 10:47:18 +03:00
|
|
|
}
|
1994-12-04 21:12:12 +03:00
|
|
|
|
|
|
|
p = line;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
switch (_ng_parse(&p, &name, &ng)) {
|
|
|
|
case _NG_NONE:
|
|
|
|
/* Done with the line */
|
|
|
|
free(line);
|
1999-11-28 07:30:15 +03:00
|
|
|
return 1;
|
1994-12-04 21:12:12 +03:00
|
|
|
|
|
|
|
case _NG_GROUP:
|
|
|
|
/* new netgroup */
|
|
|
|
/* add to the list */
|
|
|
|
ng->ng_next = _nglist;
|
|
|
|
_nglist = ng;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case _NG_NAME:
|
|
|
|
/* netgroup name */
|
1999-11-28 07:30:15 +03:00
|
|
|
if (!addgroup(sl, name))
|
|
|
|
return 0;
|
1994-12-04 21:12:12 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case _NG_ERROR:
|
1999-11-28 07:30:15 +03:00
|
|
|
return 0;
|
1994-12-04 21:12:12 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
1994-05-17 17:30:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1994-05-17 17:30:51 +04:00
|
|
|
/*
|
1994-12-04 21:12:12 +03:00
|
|
|
* in_check(): Compare the spec with the netgroup
|
1994-05-17 17:30:51 +04:00
|
|
|
*/
|
1994-12-04 21:12:12 +03:00
|
|
|
static int
|
|
|
|
in_check(host, user, domain, ng)
|
|
|
|
const char *host;
|
|
|
|
const char *user;
|
|
|
|
const char *domain;
|
|
|
|
struct netgroup *ng;
|
1994-05-17 17:30:51 +04:00
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
/* host may be NULL */
|
|
|
|
/* user may be NULL */
|
|
|
|
/* domain may be NULL */
|
|
|
|
_DIAGASSERT(ng != NULL);
|
|
|
|
|
1994-12-12 22:05:31 +03:00
|
|
|
if ((host != NULL) && (ng->ng_host != NULL)
|
1994-12-04 21:12:12 +03:00
|
|
|
&& strcmp(ng->ng_host, host) != 0)
|
|
|
|
return 0;
|
|
|
|
|
1994-12-12 22:05:31 +03:00
|
|
|
if ((user != NULL) && (ng->ng_user != NULL)
|
1994-12-04 21:12:12 +03:00
|
|
|
&& strcmp(ng->ng_user, user) != 0)
|
|
|
|
return 0;
|
|
|
|
|
1994-12-12 22:05:31 +03:00
|
|
|
if ((domain != NULL) && (ng->ng_domain != NULL)
|
1994-12-04 21:12:12 +03:00
|
|
|
&& strcmp(ng->ng_domain, domain) != 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
1994-05-17 17:30:51 +04:00
|
|
|
}
|
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1994-05-17 17:30:51 +04:00
|
|
|
/*
|
1999-11-28 07:30:15 +03:00
|
|
|
* in_find(): Find a match for the host, user, domain spec.
|
|
|
|
* grp is not a valid pointer after return (either free(3)ed or allocated
|
|
|
|
* to a stringlist). in either case, it shouldn't be used again.
|
1994-05-17 17:30:51 +04:00
|
|
|
*/
|
|
|
|
static int
|
1999-01-16 10:47:18 +03:00
|
|
|
in_find(sl, grp, host, user, domain)
|
1997-01-17 10:26:18 +03:00
|
|
|
StringList *sl;
|
|
|
|
char *grp;
|
|
|
|
const char *host;
|
|
|
|
const char *user;
|
|
|
|
const char *domain;
|
1994-05-17 17:30:51 +04:00
|
|
|
{
|
1994-12-04 21:12:12 +03:00
|
|
|
char *line, *p;
|
|
|
|
int i;
|
|
|
|
struct netgroup *ng;
|
|
|
|
char *name;
|
1994-05-17 17:30:51 +04:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(sl != NULL);
|
|
|
|
_DIAGASSERT(grp != NULL);
|
|
|
|
/* host may be NULL */
|
|
|
|
/* user may be NULL */
|
|
|
|
/* domain may be NULL */
|
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
#ifdef DEBUG_NG
|
|
|
|
(void) fprintf(stderr, "in_find(%s)\n", grp);
|
|
|
|
#endif
|
|
|
|
/* check for cycles */
|
1997-01-17 10:26:18 +03:00
|
|
|
if (sl_find(sl, grp) != NULL) {
|
1994-12-12 22:05:31 +03:00
|
|
|
free(grp);
|
1997-07-13 23:12:05 +04:00
|
|
|
warnx("netgroup: Cycle in group `%s'", grp);
|
1994-12-04 21:12:12 +03:00
|
|
|
return 0;
|
1994-05-17 17:30:51 +04:00
|
|
|
}
|
1999-11-28 07:30:15 +03:00
|
|
|
if (sl_add(sl, grp) == -1) {
|
|
|
|
free(grp);
|
|
|
|
return 0;
|
|
|
|
}
|
1994-12-04 21:12:12 +03:00
|
|
|
|
|
|
|
/* Lookup this netgroup */
|
1999-01-16 10:47:18 +03:00
|
|
|
line = NULL;
|
|
|
|
if (!lookup(grp, &line, _NG_KEYBYNAME)) {
|
|
|
|
if (line)
|
|
|
|
free(line);
|
1994-12-04 21:12:12 +03:00
|
|
|
return 0;
|
1999-01-16 10:47:18 +03:00
|
|
|
}
|
1994-12-04 21:12:12 +03:00
|
|
|
|
|
|
|
p = line;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
switch (_ng_parse(&p, &name, &ng)) {
|
|
|
|
case _NG_NONE:
|
|
|
|
/* Done with the line */
|
|
|
|
free(line);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case _NG_GROUP:
|
|
|
|
/* new netgroup */
|
|
|
|
i = in_check(host, user, domain, ng);
|
1994-12-12 22:05:31 +03:00
|
|
|
if (ng->ng_host != NULL)
|
1998-07-26 23:34:10 +04:00
|
|
|
free((char *)ng->ng_host);
|
1994-12-12 22:05:31 +03:00
|
|
|
if (ng->ng_user != NULL)
|
1998-07-26 23:34:10 +04:00
|
|
|
free((char *)ng->ng_user);
|
1994-12-12 22:05:31 +03:00
|
|
|
if (ng->ng_domain != NULL)
|
1998-07-26 23:34:10 +04:00
|
|
|
free((char *)ng->ng_domain);
|
1994-12-04 21:12:12 +03:00
|
|
|
free(ng);
|
|
|
|
if (i) {
|
|
|
|
free(line);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case _NG_NAME:
|
|
|
|
/* netgroup name */
|
1999-01-16 10:47:18 +03:00
|
|
|
if (in_find(sl, name, host, user, domain)) {
|
1994-12-04 21:12:12 +03:00
|
|
|
free(line);
|
|
|
|
return 1;
|
1994-05-17 17:30:51 +04:00
|
|
|
}
|
1994-09-19 08:32:45 +04:00
|
|
|
break;
|
1994-12-04 21:12:12 +03:00
|
|
|
|
|
|
|
case _NG_ERROR:
|
|
|
|
free(line);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
1994-05-17 17:30:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
/*
|
|
|
|
* _ng_makekey(): Make a key from the two names given. The key is of the form
|
|
|
|
* <name1>.<name2> Names strings are replaced with * if they are empty;
|
1999-11-28 07:30:15 +03:00
|
|
|
* Returns NULL if there's a problem.
|
1994-12-04 21:12:12 +03:00
|
|
|
*/
|
|
|
|
char *
|
|
|
|
_ng_makekey(s1, s2, len)
|
|
|
|
const char *s1, *s2;
|
|
|
|
size_t len;
|
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
char *buf;
|
|
|
|
|
|
|
|
/* s1 may be NULL */
|
|
|
|
/* s2 may be NULL */
|
|
|
|
|
|
|
|
buf = malloc(len);
|
1999-11-28 07:30:15 +03:00
|
|
|
if (buf != NULL)
|
|
|
|
(void) snprintf(buf, len, "%s.%s", _NG_STAR(s1), _NG_STAR(s2));
|
1994-12-04 21:12:12 +03:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
1996-04-27 22:59:05 +04:00
|
|
|
void
|
|
|
|
_ng_print(buf, len, ng)
|
|
|
|
char *buf;
|
|
|
|
size_t len;
|
|
|
|
const struct netgroup *ng;
|
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(buf != NULL);
|
|
|
|
_DIAGASSERT(ng != NULL);
|
|
|
|
|
1996-04-27 22:59:05 +04:00
|
|
|
(void) snprintf(buf, len, "(%s,%s,%s)", _NG_EMPTY(ng->ng_host),
|
|
|
|
_NG_EMPTY(ng->ng_user), _NG_EMPTY(ng->ng_domain));
|
|
|
|
}
|
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1994-05-17 17:30:51 +04:00
|
|
|
/*
|
1994-12-04 21:12:12 +03:00
|
|
|
* in_lookup1(): Fast lookup for a key in the appropriate map
|
1994-05-17 17:30:51 +04:00
|
|
|
*/
|
1994-12-04 21:12:12 +03:00
|
|
|
static char *
|
1999-01-16 10:47:18 +03:00
|
|
|
in_lookup1(key, domain, map)
|
1994-12-04 21:12:12 +03:00
|
|
|
const char *key;
|
|
|
|
const char *domain;
|
|
|
|
int map;
|
1994-05-17 17:30:51 +04:00
|
|
|
{
|
1994-12-04 21:12:12 +03:00
|
|
|
char *line;
|
|
|
|
size_t len;
|
|
|
|
char *ptr;
|
|
|
|
int res;
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
/* key may be NULL */
|
|
|
|
/* domain may be NULL */
|
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
len = (key ? strlen(key) : 1) + (domain ? strlen(domain) : 1) + 2;
|
|
|
|
ptr = _ng_makekey(key, domain, len);
|
1999-11-28 07:30:15 +03:00
|
|
|
if (ptr == NULL)
|
|
|
|
return NULL;
|
1999-01-16 10:47:18 +03:00
|
|
|
res = lookup(ptr, &line, map);
|
1994-12-04 21:12:12 +03:00
|
|
|
free(ptr);
|
|
|
|
return res ? line : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* in_lookup(): Fast lookup for a key in the appropriate map
|
|
|
|
*/
|
|
|
|
static int
|
1999-01-16 10:47:18 +03:00
|
|
|
in_lookup(group, key, domain, map)
|
1994-12-04 21:12:12 +03:00
|
|
|
const char *group;
|
|
|
|
const char *key;
|
|
|
|
const char *domain;
|
|
|
|
int map;
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char *ptr, *line;
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(group != NULL);
|
|
|
|
/* key may be NULL */
|
|
|
|
/* domain may be NULL */
|
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
if (domain != NULL) {
|
|
|
|
/* Domain specified; look in "group.domain" and "*.domain" */
|
1999-01-16 10:47:18 +03:00
|
|
|
if ((line = in_lookup1(key, domain, map)) == NULL)
|
|
|
|
line = in_lookup1(NULL, domain, map);
|
1999-11-28 07:30:15 +03:00
|
|
|
} else
|
1994-12-04 21:12:12 +03:00
|
|
|
line = NULL;
|
|
|
|
|
|
|
|
if (line == NULL) {
|
|
|
|
/*
|
|
|
|
* domain not specified or domain lookup failed; look in
|
|
|
|
* "group.*" and "*.*"
|
|
|
|
*/
|
1999-01-16 10:47:18 +03:00
|
|
|
if (((line = in_lookup1(key, NULL, map)) == NULL) &&
|
|
|
|
((line = in_lookup1(NULL, NULL, map)) == NULL))
|
1994-12-04 21:12:12 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(group);
|
|
|
|
|
|
|
|
for (ptr = line; (ptr = strstr(ptr, group)) != NULL;)
|
|
|
|
/* Make sure we did not find a substring */
|
|
|
|
if ((ptr != line && ptr[-1] != ',') ||
|
|
|
|
(ptr[len] != '\0' && strchr("\n\t ,", ptr[len]) == NULL))
|
|
|
|
ptr++;
|
|
|
|
else {
|
|
|
|
free(line);
|
|
|
|
return 1;
|
1994-05-17 17:30:51 +04:00
|
|
|
}
|
1994-12-04 21:12:12 +03:00
|
|
|
|
|
|
|
free(line);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
endnetgrent()
|
|
|
|
{
|
|
|
|
for (_nglist = _nghead; _nglist != NULL; _nglist = _nghead) {
|
|
|
|
_nghead = _nglist->ng_next;
|
1994-12-12 22:05:31 +03:00
|
|
|
if (_nglist->ng_host != NULL)
|
1998-07-26 23:34:10 +04:00
|
|
|
free((char *)_nglist->ng_host);
|
1994-12-12 22:05:31 +03:00
|
|
|
if (_nglist->ng_user != NULL)
|
1998-07-26 23:34:10 +04:00
|
|
|
free((char *)_nglist->ng_user);
|
1994-12-12 22:05:31 +03:00
|
|
|
if (_nglist->ng_domain != NULL)
|
1998-07-26 23:34:10 +04:00
|
|
|
free((char *)_nglist->ng_domain);
|
1994-12-04 21:12:12 +03:00
|
|
|
free(_nglist);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_ng_db) {
|
|
|
|
(void) (_ng_db->close) (_ng_db);
|
|
|
|
_ng_db = NULL;
|
1994-05-17 17:30:51 +04:00
|
|
|
}
|
1994-12-04 21:12:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
setnetgrent(ng)
|
|
|
|
const char *ng;
|
|
|
|
{
|
1999-11-28 07:30:15 +03:00
|
|
|
StringList *sl;
|
1999-01-16 10:47:18 +03:00
|
|
|
char *ng_copy;
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(ng != NULL);
|
|
|
|
|
1999-11-28 07:30:15 +03:00
|
|
|
sl = sl_init();
|
|
|
|
if (sl == NULL)
|
|
|
|
return;
|
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
/* Cleanup any previous storage */
|
|
|
|
if (_nghead != NULL)
|
|
|
|
endnetgrent();
|
|
|
|
|
|
|
|
if (_ng_db == NULL)
|
|
|
|
_ng_db = dbopen(_PATH_NETGROUP_DB, O_RDONLY, 0, DB_HASH, NULL);
|
|
|
|
|
|
|
|
ng_copy = strdup(ng);
|
1999-11-28 07:30:15 +03:00
|
|
|
if (ng_copy != NULL)
|
|
|
|
addgroup(sl, ng_copy);
|
1994-12-04 21:12:12 +03:00
|
|
|
_nghead = _nglist;
|
1997-01-17 10:26:18 +03:00
|
|
|
sl_free(sl, 1);
|
1994-12-04 21:12:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
getnetgrent(host, user, domain)
|
|
|
|
const char **host;
|
|
|
|
const char **user;
|
|
|
|
const char **domain;
|
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(host != NULL);
|
|
|
|
_DIAGASSERT(user != NULL);
|
|
|
|
_DIAGASSERT(domain != NULL);
|
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
if (_nglist == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*host = _nglist->ng_host;
|
|
|
|
*user = _nglist->ng_user;
|
|
|
|
*domain = _nglist->ng_domain;
|
|
|
|
|
|
|
|
_nglist = _nglist->ng_next;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
innetgr(grp, host, user, domain)
|
|
|
|
const char *grp, *host, *user, *domain;
|
|
|
|
{
|
|
|
|
int found;
|
1997-01-17 10:26:18 +03:00
|
|
|
StringList *sl;
|
1994-12-04 21:12:12 +03:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(grp != NULL);
|
|
|
|
/* host may be NULL */
|
|
|
|
/* user may be NULL */
|
|
|
|
/* domain may be NULL */
|
|
|
|
|
1994-12-04 21:12:12 +03:00
|
|
|
if (_ng_db == NULL)
|
|
|
|
_ng_db = dbopen(_PATH_NETGROUP_DB, O_RDONLY, 0, DB_HASH, NULL);
|
|
|
|
|
|
|
|
/* Try the fast lookup first */
|
|
|
|
if (host != NULL && user == NULL) {
|
1999-01-16 10:47:18 +03:00
|
|
|
if (in_lookup(grp, host, domain, _NG_KEYBYHOST))
|
1994-12-04 21:12:12 +03:00
|
|
|
return 1;
|
|
|
|
} else if (host == NULL && user != NULL) {
|
1999-01-16 10:47:18 +03:00
|
|
|
if (in_lookup(grp, user, domain, _NG_KEYBYUSER))
|
1994-12-04 21:12:12 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* If a domainname is given, we would have found a match */
|
|
|
|
if (domain != NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Too bad need the slow recursive way */
|
1997-01-17 10:26:18 +03:00
|
|
|
sl = sl_init();
|
1999-11-28 07:30:15 +03:00
|
|
|
if (sl == NULL)
|
|
|
|
return 0;
|
1999-01-16 10:47:18 +03:00
|
|
|
found = in_find(sl, strdup(grp), host, user, domain);
|
1997-01-17 10:26:18 +03:00
|
|
|
sl_free(sl, 1);
|
1994-12-04 21:12:12 +03:00
|
|
|
|
|
|
|
return found;
|
1994-05-17 17:30:51 +04:00
|
|
|
}
|