#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <rpc/rpc.h>
#include <xfn/xfn.h>
#include "automount.h"
#include "ns_fnutils.h"
static int
get_contexts(const FN_composite_name_t *, FN_ctx_t **ctxp,
FN_ctx_t **prefix_ctxp, FN_ctx_t *init_ctx, FN_status_t *);
static int
split_cname(const FN_composite_name_t *name, FN_composite_name_t **last,
FN_composite_name_t **lead);
static int
list_ctx_and_or_nns(FN_ctx_t *ctx, FN_ctx_t *prefix_ctx, struct dir_entry **,
FN_status_t *);
static bool_t
need_nns_only(FN_ctx_t *ctx, FN_ctx_t *prefix_ctx, FN_status_t *);
static bool_t
need_ctx_and_nns(FN_ctx_t *, FN_status_t *);
static int
list_ctx(FN_ctx_t *, struct dir_entry **, FN_status_t *);
static int
list_ctx_aux(FN_ctx_t *, FN_compound_name_t *name, const FN_attrset_t *syntax,
struct dir_entry **, FN_status_t *);
static int
add_name_to_dirlist(const FN_compound_name_t *, struct dir_entry **);
static bool_t
slash_hierarchy(const FN_attrset_t *syntax);
static bool_t
non_slash_hierarchy(const FN_attrset_t *syntax);
static bool_t
syntax_attrs_equal(const FN_attrset_t *, const FN_attrset_t *);
static const FN_attrvalue_t *
get_attrval(const FN_attrset_t *, const FN_identifier_t *attr_id);
static FN_ctx_t *
lookup_ctx(FN_ctx_t *, const FN_composite_name_t *, bool_t log, FN_status_t *);
void
getmapkeys_fn(const char *map, struct dir_entry **entries_p, int *error,
int *cache_time, uid_t uid)
{
FN_composite_name_t *name;
FN_status_t *status;
FN_ctx_t *init_ctx;
FN_ctx_t *ctx;
FN_ctx_t *prefix_ctx;
struct dir_entry *p;
*cache_time = RDDIR_CACHE_TIME;
if ((init_fn() != 0) || (status = fn_status_create()) == NULL) {
log_mem_failure();
*error = -1;
return;
}
init_ctx = _fn_ctx_handle_from_initial_with_uid(uid, 0, status);
if (init_ctx == NULL) {
logstat(status, "", "No initial context");
fn_status_destroy(status);
return;
}
if (strcmp(map, FNPREFIX) == 0) {
*cache_time = 0;
*error = list_ctx(init_ctx, entries_p, status);
} else if (strcmp(map, FNPREFIX "/_dns") == 0) {
*cache_time = 1000000;
*error = 0;
} else {
if (strcmp(map, FNPREFIX "/...") == 0) {
name = new_cname("_x500");
} else {
name = new_cname(map + FNPREFIXLEN + 1);
}
if (name == NULL) {
*error = -1;
} else if (fn_composite_name_count(name) == 1) {
ctx = lookup_ctx(init_ctx, name, TRUE, status);
if (ctx != NULL) {
*error = list_ctx_and_or_nns(ctx, init_ctx,
entries_p, status);
fn_ctx_handle_destroy(ctx);
} else {
*error = -1;
}
} else {
*error = get_contexts(name, &ctx, &prefix_ctx,
init_ctx, status);
if (*error == 0) {
*error = list_ctx_and_or_nns(ctx, prefix_ctx,
entries_p, status);
fn_ctx_handle_destroy(ctx);
fn_ctx_handle_destroy(prefix_ctx);
}
}
fn_composite_name_destroy(name);
}
fn_status_destroy(status);
fn_ctx_handle_destroy(init_ctx);
if (*error == 0) {
for (p = *entries_p; p != NULL; p = p->next)
btree_enter(entries_p, p);
}
}
static int
get_contexts(const FN_composite_name_t *name, FN_ctx_t **ctxp,
FN_ctx_t **prefix_ctxp, FN_ctx_t *init_ctx, FN_status_t *status)
{
FN_composite_name_t *prefix = NULL;
FN_composite_name_t *suffix = NULL;
FN_ctx_t *nns_ctx;
if (split_cname(name, &suffix, &prefix) != 0) {
return (-1);
}
*ctxp = NULL;
*prefix_ctxp = lookup_ctx(init_ctx, prefix, TRUE, status);
fn_composite_name_destroy(prefix);
if (*prefix_ctxp != NULL) {
nns_ctx = lookup_ctx(*prefix_ctxp, slash_cname, FALSE, status);
if (nns_ctx != NULL) {
*ctxp = lookup_ctx(nns_ctx, suffix, FALSE, status);
if (*ctxp != NULL) {
fn_ctx_handle_destroy(*prefix_ctxp);
*prefix_ctxp = nns_ctx;
} else {
fn_ctx_handle_destroy(nns_ctx);
}
}
if (*ctxp == NULL) {
*ctxp =
lookup_ctx(*prefix_ctxp, suffix, FALSE, status);
}
}
fn_composite_name_destroy(suffix);
return (*ctxp != NULL ? 0 : -1);
}
static int
split_cname(const FN_composite_name_t *name, FN_composite_name_t **last,
FN_composite_name_t **lead)
{
void *iter;
(void) fn_composite_name_last(name, &iter);
*last = fn_composite_name_suffix(name, iter);
*lead = fn_composite_name_prefix(name, iter);
if (*last == NULL || *lead == NULL) {
log_mem_failure();
fn_composite_name_destroy(*last);
fn_composite_name_destroy(*lead);
return (-1);
}
return (0);
}
static int
list_ctx_and_or_nns(FN_ctx_t *ctx, FN_ctx_t *prefix_ctx,
struct dir_entry **entries_p, FN_status_t *status)
{
FN_ctx_t *nns_ctx;
int rc;
if (!need_nns_only(ctx, prefix_ctx, status)) {
if (list_ctx(ctx, entries_p, status) != 0) {
return (-1);
}
if (!need_ctx_and_nns(ctx, status)) {
return (0);
}
}
nns_ctx = lookup_ctx(ctx, slash_cname, FALSE, status);
if (nns_ctx == NULL) {
return (0);
}
rc = list_ctx(nns_ctx, entries_p, status);
fn_ctx_handle_destroy(nns_ctx);
return (rc);
}
static bool_t
need_nns_only(FN_ctx_t *ctx, FN_ctx_t *prefix_ctx, FN_status_t *status)
{
FN_attrset_t *syn;
FN_attrset_t *prefix_syn;
bool_t retval;
syn = fn_ctx_get_syntax_attrs(ctx, empty_cname, status);
if (syn == NULL || !non_slash_hierarchy(syn)) {
fn_attrset_destroy(syn);
return (FALSE);
}
prefix_syn = fn_ctx_get_syntax_attrs(prefix_ctx, empty_cname, status);
retval = (prefix_syn == NULL) || syntax_attrs_equal(syn, prefix_syn);
fn_attrset_destroy(syn);
fn_attrset_destroy(prefix_syn);
return (retval);
}
static bool_t
need_ctx_and_nns(FN_ctx_t *ctx, FN_status_t *status)
{
FN_attrset_t *syn;
bool_t retval;
syn = fn_ctx_get_syntax_attrs(ctx, empty_cname, status);
if (syn == NULL) {
return (FALSE);
}
retval = slash_hierarchy(syn);
fn_attrset_destroy(syn);
return (retval);
}
static int
list_ctx(FN_ctx_t *ctx, struct dir_entry **entries_p, FN_status_t *status)
{
FN_attrset_t *syntax;
FN_compound_name_t *name;
int retval;
syntax = fn_ctx_get_syntax_attrs(ctx, empty_cname, status);
if (syntax == NULL) {
logstat(status, "", "bad syntax attributes");
return (-1);
}
name =
fn_compound_name_from_syntax_attrs(syntax, empty_string, status);
if (name == NULL) {
logstat(status, "", "could not create compound name");
fn_attrset_destroy(syntax);
return (-1);
}
if (!non_slash_hierarchy(syntax)) {
fn_attrset_destroy(syntax);
syntax = NULL;
}
retval = list_ctx_aux(ctx, name, syntax, entries_p, status);
fn_attrset_destroy(syntax);
fn_compound_name_destroy(name);
return (retval);
}
static int
list_ctx_aux(FN_ctx_t *ctx, FN_compound_name_t *name,
const FN_attrset_t *syntax, struct dir_entry **entries_p,
FN_status_t *status)
{
FN_bindinglist_t *bindings;
FN_string_t *child;
FN_ref_t *ref;
unsigned int stat;
int rc = 0;
void *iter;
bindings = fn_ctx_list_bindings(ctx, empty_cname, status);
if (bindings == NULL) {
return (0);
}
while ((child = fn_bindinglist_next(bindings, &ref, status)) != NULL) {
if (fn_compound_name_append_comp(name, child, &stat) == 0) {
rc = -1;
break;
}
if (add_name_to_dirlist(name, entries_p) != 0) {
rc = -1;
break;
}
if (syntax != NULL) {
ctx = fn_ctx_handle_from_ref(ref, XFN2(0) status);
if (ctx != NULL) {
rc = list_ctx_aux(ctx, name, syntax, entries_p,
status);
fn_ctx_handle_destroy(ctx);
if (rc != 0) {
break;
}
}
}
fn_ref_destroy(ref);
fn_string_destroy(child);
(void) fn_compound_name_last(name, &iter);
(void) fn_compound_name_next(name, &iter);
(void) fn_compound_name_delete_comp(name, &iter);
}
fn_string_destroy(child);
fn_bindinglist_destroy(bindings XFN1(status));
return (rc);
}
static int
add_name_to_dirlist(const FN_compound_name_t *name,
struct dir_entry **entries_p)
{
FN_string_t *string;
char *str;
unsigned int stat;
struct dir_entry *entry;
string = fn_string_from_compound_name(name);
if (string == NULL) {
log_mem_failure();
return (-1);
}
str = (char *)fn_string_str(string, &stat);
if (str != NULL) {
str = auto_rddir_strdup(str);
}
fn_string_destroy(string);
if (str == NULL) {
log_mem_failure();
return (-1);
}
entry = (struct dir_entry *)
auto_rddir_malloc(sizeof (*entry));
if (entry == NULL) {
log_mem_failure();
free(str);
return (-1);
}
(void) memset((char *)entry, 0, sizeof (*entry));
entry->name = str;
entry->next = *entries_p;
*entries_p = entry;
return (0);
}
static const FN_identifier_t syntax_direction = {
FN_ID_STRING,
sizeof ("fn_std_syntax_direction") - 1,
"fn_std_syntax_direction"
};
static const FN_identifier_t syntax_separator = {
FN_ID_STRING,
sizeof ("fn_std_syntax_separator") - 1,
"fn_std_syntax_separator"
};
static bool_t
slash_hierarchy(const FN_attrset_t *syntax)
{
const FN_attrvalue_t *dir = get_attrval(syntax, &syntax_direction);
const FN_attrvalue_t *sep = get_attrval(syntax, &syntax_separator);
return (dir != NULL &&
memcmp("flat", dir->contents, dir->length) != 0 &&
sep != NULL &&
memcmp("/", sep->contents, sep->length) == 0);
}
static bool_t
non_slash_hierarchy(const FN_attrset_t *syntax)
{
const FN_attrvalue_t *dir = get_attrval(syntax, &syntax_direction);
const FN_attrvalue_t *sep = get_attrval(syntax, &syntax_separator);
return (dir != NULL &&
memcmp("flat", dir->contents, dir->length) != 0 &&
sep != NULL &&
memcmp("/", sep->contents, sep->length) != 0);
}
static bool_t
syntax_attrs_equal(const FN_attrset_t *syn1, const FN_attrset_t *syn2)
{
const FN_attribute_t *attr;
const FN_attrvalue_t *val1;
const FN_attrvalue_t *val2;
void *iter1;
void *iter2;
if (fn_attrset_count(syn1) != fn_attrset_count(syn2)) {
return (FALSE);
}
for (attr = fn_attrset_first(syn1, &iter1);
attr != NULL;
attr = fn_attrset_next(syn1, &iter1)) {
val1 = fn_attribute_first(attr, &iter2);
val2 = get_attrval(syn2, fn_attribute_identifier(attr));
if ((val1 == NULL && val2 != NULL) ||
(val1 != NULL && val2 == NULL)) {
return (FALSE);
}
if (val1 != NULL && val2 != NULL) {
if (val1->length != val2->length ||
memcmp(val1->contents, val2->contents,
val1->length) != 0) {
return (FALSE);
}
}
}
return (TRUE);
}
static const FN_attrvalue_t *
get_attrval(const FN_attrset_t *attrs, const FN_identifier_t *attr_id)
{
const FN_attribute_t *attr;
void *iter;
attr = fn_attrset_get(attrs, attr_id);
if (attr != NULL) {
return (fn_attribute_first(attr, &iter));
} else {
return (NULL);
}
}
static FN_ctx_t *
lookup_ctx(FN_ctx_t *ctx, const FN_composite_name_t *name, bool_t log,
FN_status_t *status)
{
FN_ref_t *ref;
char *msg;
ref = fn_ctx_lookup(ctx, name, status);
if (ref == NULL) {
ctx = NULL;
msg = "lookup failed";
} else {
ctx = fn_ctx_handle_from_ref(ref, XFN2(0) status);
fn_ref_destroy(ref);
if (ctx == NULL) {
msg = "could not construct context handle";
}
}
if (ctx == NULL && verbose && (log || transient(status))) {
logstat(status, "", msg);
}
return (ctx);
}