#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <pwd.h>
#include <rpcsvc/ypclnt.h>
#include "util.h"
#include "table.h"
#include "getgroup.h"
#define MAXDOMAINLEN 256
#define MAXGROUPLEN 1024
typedef struct stringnode *stringlist;
struct stringnode {
char *str;
stringlist next;
};
typedef struct stringnode stringnode;
typedef struct groupentrynode *groupentrylist;
struct groupentrynode {
char *name;
stringlist groups;
groupentrylist next;
};
typedef struct groupentrynode groupentrynode;
stringtable ngtable;
static groupentrylist grouptable[TABLESIZE];
static char *nextgroup(void);
static void storegroup(char *group, struct grouplist *glist, int byuser);
static void enter(char *name, char *group);
static void appendgroup(groupentrylist grlist, char *group);
static groupentrylist newentry(char *name, char *group);
static void loadtable(FILE *nf);
static void dumptable(void);
int
main(int argc, char *argv[])
{
char *group;
struct grouplist *glist;
int byuser;
loadtable(stdin);
if (argc == 2 && argv[1][0] == '-' &&
(argv[1][1] == 'u' || argv[1][1] == 'h')) {
byuser = (argv[1][1] == 'u');
} else {
(void) fprintf(stderr,
"usage: %s -h (by host), %s -u (by user)\n",
argv[0], argv[0]);
exit(1);
}
while (group = nextgroup()) {
glist = my_getgroup(group);
storegroup(group, glist, byuser);
}
dumptable();
return (0);
}
static char *
nextgroup(void)
{
static int index = -1;
static tablelist cur = NULL;
char *group;
while (cur == NULL) {
if (++index == TABLESIZE) {
return (NULL);
}
cur = ngtable[index];
}
group = cur->key;
cur = cur->next;
return (group);
}
static void
dumptable(void)
{
int i;
groupentrylist entry;
stringlist groups;
for (i = 0; i < TABLESIZE; i++) {
if (entry = grouptable[i]) {
while (entry) {
fputs(entry->name, stdout);
putc('\t', stdout);
for (groups = entry->groups; groups;
groups = groups->next) {
fputs(groups->str, stdout);
if (groups->next) {
putc(',', stdout);
}
}
putc('\n', stdout);
entry = entry->next;
}
}
}
}
static void
storegroup(char *group, struct grouplist *glist, int byuser)
{
char *name;
char *domain;
char *key;
static char *universal = "*";
for (; glist; glist = glist->gl_nxt) {
name = byuser ? glist->gl_name : glist->gl_machine;
if (!name) {
name = universal;
} else if (!isalnum(*name) && *name != '_') {
continue;
}
domain = glist->gl_domain;
if (!domain) {
domain = universal;
}
key = malloc((strlen(name) + strlen(domain) + 2));
(void) sprintf(key, "%s.%s", name, domain);
enter(key, group);
}
}
static groupentrylist
newentry(char *name, char *group)
{
groupentrylist new;
new = MALLOC(groupentrynode);
STRCPY(new->name, name);
new->groups = MALLOC(stringnode);
new->groups->str = group;
new->groups->next = NULL;
new->next = NULL;
return (new);
}
static void
appendgroup(groupentrylist grlist, char *group)
{
stringlist cur, prev;
prev = NULL;
for (cur = grlist->groups; cur; prev = cur, cur = cur->next) {
if (strcmp(group, cur->str) == 0) {
return;
}
}
if (prev == NULL)
return;
prev->next = MALLOC(stringnode);
cur = prev->next;
cur->str = group;
cur->next = NULL;
}
static void
enter(char *name, char *group)
{
int key;
groupentrylist gel;
groupentrylist gelprev = NULL;
key = tablekey(name);
if (grouptable[key] == NULL) {
grouptable[key] = newentry(name, group);
} else {
gel = grouptable[key];
while (gel && strcmp(gel->name, name)) {
gelprev = gel;
gel = gel->next;
}
if (gel) {
appendgroup(gel, group);
} else {
gelprev->next = newentry(name, group);
}
}
}
static void
loadtable(FILE *nf)
{
char buf[MAXGROUPLEN];
char *p;
char *group;
char *line;
while (getaline(buf, MAXGROUPLEN, nf)) {
for (p = buf; *p && isspace((int)*p); p++)
;
for (; *p && *p != '#' && *p != ' ' && *p != '\t'; p++)
;
if (*p == EOS || *p == '#')
continue;
*p++ = EOS;
while (*p == ' ' || *p == '\t') {
p++;
}
if (*p == EOS || *p == '#')
continue;
STRCPY(group, buf);
STRCPY(line, p);
store(ngtable, group, line);
}
}