#include <sys/types.h>
#include <sys/limits.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <grp.h>
#include <pwd.h>
#include <errno.h>
#include <rpc/rpc.h>
#include <rpcsvc/yp.h>
#include <rpcsvc/ypclnt.h>
#ifdef YP
#define _PATH_NETID "/etc/netid"
#define MAXLINELENGTH 1024
static int _parse_netid(char*, uid_t, gid_t*, int*, int);
static int _read_netid(const char *, uid_t, gid_t*, int*, int);
static int
_parse_netid(char *netid, uid_t uid, gid_t *groups, int *ngroups,
int maxgroups)
{
const char *errstr = NULL;
char *start, *p;
uid_t tuid;
gid_t gid;
int i;
p = strchr(netid, ':');
if (!p)
return (0);
*p++ = '\0';
tuid = (uid_t)strtonum(netid, 0, UID_MAX, &errstr);
if (errstr || tuid != uid)
return (0);
while (p && *p) {
start = p;
p = strchr(start, ',');
if (p)
*p++ = '\0';
gid = (gid_t)strtonum(start, 0, GID_MAX, &errstr);
if (errstr)
continue;
for (i = 0; i < maxgroups && i < *ngroups; i++)
if (groups[i] == gid)
break;
if (i == *ngroups) {
if (*ngroups >= maxgroups)
(*ngroups)++;
else
groups[(*ngroups)++] = gid;
}
}
return (1);
}
static int
_read_netid(const char *key, uid_t uid, gid_t *groups, int *ngroups,
int maxgroups)
{
FILE *fp;
char line[MAXLINELENGTH], *p;
int found = 0, fd;
fd = __pledge_open(_PATH_NETID, O_RDONLY|O_CLOEXEC);
if (fd == -1)
return (0);
fp = fdopen(fd, "r");
if (!fp) {
close(fd);
return (0);
}
while (!found && fgets(line, sizeof(line), fp)) {
p = strchr(line, '\n');
if (p)
*p = '\0';
else {
int ch;
while ((ch = getc_unlocked(fp)) != '\n' && ch != EOF)
;
continue;
}
p = strchr(line, ' ');
if (!p)
continue;
*p++ = '\0';
if (strcmp(line, key))
continue;
found = _parse_netid(p, uid, groups, ngroups, maxgroups);
}
(void)fclose(fp);
return (found);
}
#endif
int
getgrouplist(const char *uname, gid_t agroup, gid_t *groups, int *grpcnt)
{
int i, ngroups = 0, maxgroups = *grpcnt, bail;
int needyp = 0, foundyp = 0;
int *skipyp = &foundyp;
extern struct group *_getgrent_yp(int *);
struct group *grp;
if (ngroups >= maxgroups)
ngroups++;
else
groups[ngroups++] = agroup;
setgrent();
while ((grp = _getgrent_yp(skipyp)) || foundyp) {
if (foundyp) {
if (foundyp > 0)
needyp = 1;
else
skipyp = NULL;
foundyp = 0;
continue;
}
if (grp->gr_gid == agroup)
continue;
for (bail = 0, i = 0; bail == 0 && i < maxgroups &&
i < ngroups; i++) {
if (groups[i] == grp->gr_gid)
bail = 1;
}
if (bail)
continue;
for (i = 0; grp->gr_mem[i]; i++) {
if (!strcmp(grp->gr_mem[i], uname)) {
if (ngroups >= maxgroups)
ngroups++;
else
groups[ngroups++] = grp->gr_gid;
break;
}
}
}
#ifdef YP
if (skipyp && needyp) {
char buf[MAXLINELENGTH], *ypdata = NULL, *key;
static char *__ypdomain;
struct passwd pwstore;
int ypdatalen;
if (getpwnam_r(uname, &pwstore, buf, sizeof buf, NULL) ||
(!__ypdomain && yp_get_default_domain(&__ypdomain)))
goto out;
i = asprintf(&key, "unix.%u@%s", pwstore.pw_uid, __ypdomain);
if (i == -1)
goto out;
if (_read_netid(key, pwstore.pw_uid, groups, &ngroups,
maxgroups)) {
free(key);
goto out;
}
if (!yp_match(__ypdomain, "netid.byname", key,
(int)strlen(key), &ypdata, &ypdatalen))
_parse_netid(ypdata, pwstore.pw_uid, groups, &ngroups,
maxgroups);
free(key);
free(ypdata);
}
#endif
out:
endgrent();
*grpcnt = ngroups;
return (ngroups > maxgroups ? -1 : 0);
}
DEF_WEAK(getgrouplist);