#include "mt.h"
#include <stdlib.h>
#include <sys/types.h>
#include <nss_dbdefs.h>
#include <string.h>
#include <prof_attr.h>
extern char *_strtok_escape(char *, char *, char **);
static int profattr_stayopen;
static DEFINE_NSS_DB_ROOT(db_root);
static DEFINE_NSS_GETENT(context);
void
_nss_initf_profattr(nss_db_params_t *p)
{
p->name = NSS_DBNAM_PROFATTR;
p->default_config = NSS_DEFCONF_PROFATTR;
}
int
str2profattr(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
{
char *last = NULL;
char *sep = KV_TOKEN_DELIMIT;
profstr_t *prof = (profstr_t *)ent;
if (lenstr >= buflen)
return (NSS_STR_PARSE_ERANGE);
if (instr != buffer)
(void) strncpy(buffer, instr, buflen);
if (buffer[lenstr] == '\n')
buffer[lenstr] = '\0';
if (ent == (void *)NULL)
return (NSS_STR_PARSE_SUCCESS);
prof->name = _strtok_escape(buffer, sep, &last);
prof->res1 = _strtok_escape(NULL, sep, &last);
prof->res2 = _strtok_escape(NULL, sep, &last);
prof->desc = _strtok_escape(NULL, sep, &last);
prof->attr = _strtok_escape(NULL, sep, &last);
return (0);
}
void
_setprofattr(void)
{
profattr_stayopen = 0;
nss_setent(&db_root, _nss_initf_profattr, &context);
}
void
_endprofattr(void)
{
profattr_stayopen = 0;
nss_endent(&db_root, _nss_initf_profattr, &context);
nss_delete(&db_root);
}
profstr_t *
_getprofattr(profstr_t *result, char *buffer, int buflen, int *h_errnop)
{
nss_XbyY_args_t arg;
nss_status_t res;
NSS_XbyY_INIT(&arg, result, buffer, buflen, str2profattr);
res = nss_getent(&db_root, _nss_initf_profattr, &context, &arg);
arg.status = res;
*h_errnop = arg.h_errno;
return ((profstr_t *)NSS_XbyY_FINI(&arg));
}
profstr_t *
_getprofnam(const char *name, profstr_t *result, char *buffer, int buflen,
int *errnop)
{
nss_XbyY_args_t arg;
nss_status_t res;
NSS_XbyY_INIT(&arg, result, buffer, buflen, str2profattr);
arg.key.name = name;
arg.stayopen = profattr_stayopen;
res = nss_search(&db_root, _nss_initf_profattr,
NSS_DBOP_PROFATTR_BYNAME, &arg);
arg.status = res;
*errnop = arg.h_errno;
return ((profstr_t *)NSS_XbyY_FINI(&arg));
}