#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <malloc.h>
#include <stdlib.h>
#include <deflt.h>
#include <limits.h>
#include "ldap_parse.h"
#include "ldap_glob.h"
#include "ldap_attr.h"
#include "ldap_util.h"
#include "ldap_map.h"
#include "ldap_ruleval.h"
#include "nis_parse_ldap_conf.h"
int yp2ldap = 0;
__nis_table_mapping_t *ldapMappingSeq = 0;
int
parseConfig(char **ldapCLA, char *ldapConfFile) {
int ret;
ldapDBTableMapping.initTtlLo = (3600-1800);
ldapDBTableMapping.initTtlHi = (3600+1800);
ldapDBTableMapping.ttl = 3600;
ldapDBTableMapping.enumExpire = 0;
ldapDBTableMapping.fromLDAP = FALSE;
ldapDBTableMapping.toLDAP = FALSE;
ldapDBTableMapping.expire = 0;
ret = parse_ldap_migration((const char **)ldapCLA, ldapConfFile);
return (ret);
}
int
linked2hash(__nis_table_mapping_t *tlist) {
__nis_hash_table_mt dbids;
__nis_table_mapping_t *t, *told, *x, **seqNext;
__nis_object_dn_t *o, *to;
char *myself = "linked2hash";
#ifdef NISDB_LDAP_DEBUG
char *selectDbid = getenv("NISLDAPSELECTDBID");
char **sdi, *s;
int i, nsdi;
#endif
if (tlist == 0)
return (0);
{
int len = slen(proxyInfo.default_nis_domain);
if (len > 0 && proxyInfo.default_nis_domain[len-1] != '.') {
char *domain = am(myself, len+2);
(void) memcpy(domain, proxyInfo.default_nis_domain,
len);
domain[len] = '.';
domain[len+1] = '\0';
sfree(proxyInfo.default_nis_domain);
proxyInfo.default_nis_domain = domain;
}
}
#ifdef NISDB_LDAP_DEBUG
for (nsdi = 0, s = selectDbid; s != 0 && *s != '\0'; s++) {
if (*s != ' ') {
nsdi++;
while (*s != ' ' && *s != '\0')
s++;
if (*s == '\0')
break;
}
}
if (nsdi > 0) {
sdi = am(myself, nsdi * sizeof (sdi[0]));
if (sdi == 0)
logmsg(MSG_NOTIMECHECK, LOG_WARNING,
"%s: Memory alloc failure for dbId selection",
myself);
else {
for (i = 0, s = selectDbid; *s != '\0'; s++) {
if (*s != ' ') {
sdi[i++] = selectDbid;
while (*s != ' ' && *s != '\0')
s++;
if (*s != '\0') {
*s = '\0';
s++;
} else
break;
selectDbid = s;
}
}
}
}
#endif
__nis_init_hash_table(&dbids, 0);
seqNext = &ldapMappingSeq;
for (t = tlist; t != 0; t = told) {
int len;
#ifdef NISDB_LDAP_DEBUG
if (nsdi > 0) {
for (i = 0; i < nsdi; i++) {
if (strcmp(sdi[i], t->dbId) == 0)
break;
}
if (i >= nsdi) {
told = t->next;
if (tlist != t)
t->next = tlist;
else
t->next = 0;
tlist = t;
continue;
}
}
#endif
told = t->next;
t->next = 0;
if (t->item.name == 0)
t->item.name = t->dbId;
len = slen(t->objName);
while (len > 0 && t->objName[0] == '.') {
(void) memmove(t->objName, &t->objName[1], len);
len -= 1;
}
if (yp2ldap) {
t->objPath = internal_table_name(t->objName,
t->objPath);
if (!t->objPath) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"%s: Failed to obtain internal table name for \"%s\"",
myself, t->objName);
return (-1);
}
} else {
t->objPath = am(myself, len + MAXPATHLEN + 1);
if (t->objPath == 0)
return (-1);
if (internal_table_name(t->objName,
t->objPath) == 0) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"%s: Failed to obtain internal table name for \"%s\"",
myself, t->objName);
return (-1);
}
}
if (!yp2ldap) {
if (setColumnsDuringConfig && setColumnNames(t)) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"%s: Unable to find column names for \"%s\"",
myself, NIL(t->objName));
return (-1);
}
}
for (o = to = t->objectDN; o != 0; o = o->next) {
__nis_table_mapping_t *p;
if (o == to) {
x = t;
*seqNext = t;
t->seqNext = 0;
seqNext = (__nis_table_mapping_t **)&t->seqNext;
} else {
x = am(myself, sizeof (*x));
if (x == 0) {
return (-1);
}
memcpy(x, t, sizeof (*x));
x->objectDN = o;
x->next = 0;
}
if (x->objectDN->write.scope != LDAP_SCOPE_UNKNOWN) {
if (x->objectDN->write.base == 0 &&
x->objectDN->read.base != 0) {
x->objectDN->write.base =
sdup(myself, T,
x->objectDN->read.base);
if (x->objectDN->write.base == 0)
return (-1);
}
if (x->objectDN->write.attrs == 0 &&
x->objectDN->read.attrs != 0) {
x->objectDN->write.attrs =
sdup(myself, T,
x->objectDN->read.attrs);
if (x->objectDN->write.attrs == 0)
return (-1);
}
}
if (o != to) {
for (p = t; p->next != 0; p = p->next);
p->next = x;
}
}
if (t->objectDN != 0 && !__nis_insert_item_mt(t, &dbids, 0)) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"%s: Error inserting mapping for \"%s\" on hash list",
myself, NIL(t->objName));
#ifdef NISDB_LDAP_DEBUG
abort();
#endif
return (-1);
}
}
return (dbids2objs(&dbids, &ldapMappingList));
}
int
dbids2objs(__nis_hash_table_mt *dbids, __nis_hash_table_mt *objs) {
__nis_table_mapping_t *t, *o;
char *myself = "dbids2objs";
while ((t = __nis_pop_item_mt(dbids)) != 0) {
o = __nis_find_item_mt(t->objPath, objs, -1, 0);
if (o != 0) {
__nis_table_mapping_t *p = o;
while (p->next != 0) {
p = p->next;
}
p->next = t;
if (!__nis_release_item(o, objs, -1)) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"%s: __nis_release_item error",
myself);
return (-1);
}
} else {
t->item.name = t->objPath;
if (!__nis_insert_item_mt(t, objs, 0)) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"%s: __nis_insert_item error",
myself);
return (-1);
}
}
}
return (0);
}
char *
internal_table_name(nis_name name, char *res)
{
char *s, *t;
int i, j;
if (yp2ldap) {
if (name == NULL)
return (NULL);
res = s_strndup(name, strlen(name));
if (res == NULL)
return (NULL);
return (res);
}
if (res == NULL)
return (NULL);
s = relative_name(name);
if (s) {
return (NULL);
}
t = strrchr(res, '/');
if (t)
t++;
if (t[0] == '"') {
if (t[1] != '"') {
j = strlen(t);
for (i = 0; i < j; i++)
t[i] = t[i+1];
t[j-2] = '\0';
}
}
__make_legal(res);
return (res);
}
void
__make_legal(char *s)
{
while (*s) {
if (isupper(*s))
*s = tolower(*s);
s++;
}
}
nis_name
relative_name(s)
char *s;
{
char *d;
char *buf;
int dl, sl;
name_pos p;
if (s == NULL)
return (NULL);
d = __nis_rpc_domain();
if (d == NULL)
return (NULL);
dl = strlen(d);
buf = strdup(s);
if (buf == NULL)
return (NULL);
strcpy(buf, s);
sl = strlen(buf);
if (dl == 1) {
buf[sl-1] = '\0';
return (buf);
}
p = nis_dir_cmp(buf, d);
if ((p == HIGHER_NAME) || (p == NOT_SEQUENTIAL) || (p == SAME_NAME)) {
free(buf);
return (NULL);
}
buf[(sl - dl) - 1] = '\0';
if (buf[0] == '\0') {
free((void *)buf);
return (NULL);
}
return (buf);
}
char *
internalTableName(char *name) {
char *buf, *res;
char *myself = "internalTableName";
buf = (char *)am(myself, MAXPATHLEN + NIS_MAXNAMELEN + 1);
if (buf == 0)
return (0);
if (name == 0) {
(void) memcpy(buf, ROOTDIRFILE, slen(ROOTDIRFILE));
return (buf);
}
res = internal_table_name(name, buf);
if (res != buf) {
sfree(buf);
buf = 0;
}
return (buf);
}
__nis_table_mapping_t *
getObjMapping(char *name, char *intNameArg, int asObj,
int *doRead, int *doWrite) {
__nis_table_mapping_t *t, *x;
char *intName;
int freeIntName = 0, rd, wr;
if (doRead != 0)
*doRead = 0;
if (doWrite != 0)
*doWrite = 0;
if (intNameArg == 0) {
if (name == 0)
return (0);
intName = internalTableName(name);
if (intName == 0)
return (0);
freeIntName = 1;
} else {
intName = intNameArg;
}
t = __nis_find_item_mt(intName, &ldapMappingList, 0, 0);
if (t == 0) {
if (freeIntName)
sfree(intName);
return (0);
}
rd = wr = 0;
for (x = t; x != 0; x = x->next) {
if (asObj && x->objType == NIS_TABLE_OBJ &&
x->numColumns > 0)
continue;
if (x->objectDN->read.scope != LDAP_SCOPE_UNKNOWN)
rd++;
if (x->objectDN->write.scope != LDAP_SCOPE_UNKNOWN)
wr++;
}
if (doRead != 0)
*doRead = (rd > 0) ? 1 : 0;
if (doWrite != 0)
*doWrite = (wr > 0) ? 1 : 0;
if (freeIntName)
sfree(intName);
return (x);
}