#include <ctype.h>
#include <netdb.h>
#include "ns_internal.h"
#include "ldap_common.h"
#define _S_NAME "cn"
#define _S_PORT "ipserviceport"
#define _S_PROTOCOL "ipserviceprotocol"
#define _F_GETSERVBYNAME "(&(objectClass=ipService)(cn=%s))"
#define _F_GETSERVBYNAME_SSD "(&(%%s)(cn=%s))"
#define _F_GETSERVBYNAMEPROTO \
"(&(objectClass=ipService)(cn=%s)(ipServiceProtocol=%s))"
#define _F_GETSERVBYNAMEPROTO_SSD \
"(&(%%s)(cn=%s)(ipServiceProtocol=%s))"
#define _F_GETSERVBYPORT "(&(objectClass=ipService)(ipServicePort=%ld))"
#define _F_GETSERVBYPORT_SSD "(&(%%s)(ipServicePort=%ld))"
#define _F_GETSERVBYPORTPROTO \
"(&(objectClass=ipService)(ipServicePort=%ld)(ipServiceProtocol=%s))"
#define _F_GETSERVBYPORTPROTO_SSD \
"(&(%%s)(ipServicePort=%ld)(ipServiceProtocol=%s))"
typedef struct _nss_services_cookie {
int index;
char *cname;
ns_ldap_result_t *result;
} _nss_services_cookie_t;
static const char *services_attrs[] = {
_S_NAME,
_S_PORT,
_S_PROTOCOL,
(char *)NULL
};
void
_nss_services_cookie_free(void **ckP) {
_nss_services_cookie_t **cookieP = (_nss_services_cookie_t **)ckP;
if (cookieP && *cookieP) {
if ((*cookieP)->result)
(void) __ns_ldap_freeResult(&(*cookieP)->result);
free(*cookieP);
*cookieP = NULL;
}
}
static _nss_services_cookie_t *
_nss_services_cookie_new(ns_ldap_result_t *result, int index, char *cname) {
_nss_services_cookie_t *cookie;
if ((cookie = calloc(1, sizeof (*cookie))) == NULL)
return (NULL);
cookie->result = result;
cookie->index = index;
cookie->cname = cname;
return (cookie);
}
static int
_nss_ldap_services2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
{
uint_t i, k;
int nss_result;
int buflen = 0, len;
char **ipport, *cname = NULL, *protoval = NULL;
char *buffer = NULL;
ns_ldap_result_t *result;
ns_ldap_attr_t *names = NULL, *protocol = NULL;
_nss_services_cookie_t *cookie = (_nss_services_cookie_t *)
be->services_cookie;
if (cookie) {
result = cookie->result;
cname = cookie->cname;
} else {
result = be->result;
}
if (result == NULL) {
nss_result = NSS_STR_PARSE_PARSE;
goto result_srvs2str;
}
buflen = argp->buf.buflen;
if (argp->buf.result != NULL) {
if ((be->buffer = calloc(1, buflen)) == NULL) {
nss_result = NSS_STR_PARSE_PARSE;
goto result_srvs2str;
}
buffer = be->buffer;
} else
buffer = argp->buf.buffer;
nss_result = NSS_STR_PARSE_SUCCESS;
(void) memset(argp->buf.buffer, 0, buflen);
names = __ns_ldap_getAttrStruct(result->entry, _S_NAME);
if (names == NULL || names->attrvalue == NULL) {
nss_result = NSS_STR_PARSE_PARSE;
goto result_srvs2str;
}
if (cname == NULL) {
cname = __s_api_get_canonical_name(result->entry, names, 1);
if (cname == NULL || (len = strlen(cname)) < 1) {
nss_result = NSS_STR_PARSE_PARSE;
goto result_srvs2str;
}
}
ipport = __ns_ldap_getAttr(result->entry, _S_PORT);
if (ipport == NULL || ipport[0] == NULL ||
(len = strlen(cname)) < 1) {
nss_result = NSS_STR_PARSE_PARSE;
goto result_srvs2str;
}
len = snprintf(buffer, buflen, "%s %s/", cname, ipport[0]);
TEST_AND_ADJUST(len, buffer, buflen, result_srvs2str);
protocol = __ns_ldap_getAttrStruct(result->entry, _S_PROTOCOL);
if (protocol == NULL || protocol->attrvalue == NULL) {
nss_result = NSS_STR_PARSE_PARSE;
goto result_srvs2str;
}
if (cookie) {
protoval = protocol->attrvalue[cookie->index++];
} else if (protocol->value_count > 1 && be->setcalled == 0 &&
argp->key.serv.proto) {
for (k = 0; k < protocol->value_count; k++) {
if (protocol->attrvalue[k] == NULL) {
nss_result = NSS_STR_PARSE_PARSE;
goto result_srvs2str;
}
if (strcmp(protocol->attrvalue[k],
argp->key.serv.proto) == 0) {
protoval = protocol->attrvalue[k];
break;
}
}
} else {
protoval = protocol->attrvalue[0];
}
if (protoval == NULL || (len = strlen(protoval)) < 1) {
nss_result = NSS_STR_PARSE_PARSE;
goto result_srvs2str;
}
len = snprintf(buffer, buflen, "%s", protoval);
TEST_AND_ADJUST(len, buffer, buflen, result_srvs2str);
for (i = 0; i < names->value_count; i++) {
if (names->attrvalue[i] == NULL) {
nss_result = NSS_STR_PARSE_PARSE;
goto result_srvs2str;
}
if (strcmp(cname, names->attrvalue[i]) != 0) {
len = snprintf(buffer, buflen, " %s",
names->attrvalue[i]);
TEST_AND_ADJUST(len, buffer, buflen, result_srvs2str);
}
}
if (be->enumcookie != NULL && cookie == NULL &&
protocol->value_count > 1) {
be->services_cookie =
(void *)_nss_services_cookie_new(be->result, 1, cname);
if (be->services_cookie == NULL) {
nss_result = NSS_STR_PARSE_PARSE;
goto result_srvs2str;
}
be->result = NULL;
}
if (argp->buf.result != NULL)
be->buflen = strlen(be->buffer);
result_srvs2str:
if (cookie) {
if (nss_result != NSS_STR_PARSE_SUCCESS ||
cookie->index >= protocol->value_count) {
_nss_services_cookie_free(
(void **)&be->services_cookie);
}
} else {
(void) __ns_ldap_freeResult(&be->result);
}
return (nss_result);
}
static nss_status_t
getbyname(ldap_backend_ptr be, void *a)
{
nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
const char *proto = argp->key.serv.proto;
char searchfilter[SEARCHFILTERLEN];
char userdata[SEARCHFILTERLEN];
char name[SEARCHFILTERLEN];
char protocol[SEARCHFILTERLEN];
int ret;
if (_ldap_filter_name(name, argp->key.serv.serv.name, sizeof (name))
!= 0)
return ((nss_status_t)NSS_NOTFOUND);
if (proto == NULL) {
ret = snprintf(searchfilter, sizeof (searchfilter),
_F_GETSERVBYNAME, name);
if (ret >= sizeof (searchfilter) || ret < 0)
return ((nss_status_t)NSS_NOTFOUND);
ret = snprintf(userdata, sizeof (userdata),
_F_GETSERVBYNAME_SSD, name);
if (ret >= sizeof (userdata) || ret < 0)
return ((nss_status_t)NSS_NOTFOUND);
} else {
if (_ldap_filter_name(protocol, proto, sizeof (protocol)) != 0)
return ((nss_status_t)NSS_NOTFOUND);
ret = snprintf(searchfilter, sizeof (searchfilter),
_F_GETSERVBYNAMEPROTO, name, protocol);
if (ret >= sizeof (searchfilter) || ret < 0)
return ((nss_status_t)NSS_NOTFOUND);
ret = snprintf(userdata, sizeof (userdata),
_F_GETSERVBYNAMEPROTO_SSD, name, protocol);
if (ret >= sizeof (userdata) || ret < 0)
return ((nss_status_t)NSS_NOTFOUND);
}
return ((nss_status_t)_nss_ldap_lookup(be, argp,
_SERVICES, searchfilter, NULL,
_merge_SSD_filter, userdata));
}
static nss_status_t
getbyport(ldap_backend_ptr be, void *a)
{
nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
const char *proto = argp->key.serv.proto;
char portstr[12];
char searchfilter[SEARCHFILTERLEN];
char userdata[SEARCHFILTERLEN];
char protocol[SEARCHFILTERLEN];
int ret;
ret = snprintf(portstr, sizeof (portstr), " %d",
ntohs((ushort_t)argp->key.serv.serv.port));
if (ret >= sizeof (portstr) || ret < 0)
return ((nss_status_t)NSS_NOTFOUND);
if (proto == NULL) {
ret = snprintf(searchfilter, sizeof (searchfilter),
_F_GETSERVBYPORT, strtol(portstr, (char **)NULL, 10));
if (ret >= sizeof (searchfilter) || ret < 0)
return ((nss_status_t)NSS_NOTFOUND);
ret = snprintf(userdata, sizeof (userdata),
_F_GETSERVBYPORT_SSD, strtol(portstr, (char **)NULL, 10));
if (ret >= sizeof (userdata) || ret < 0)
return ((nss_status_t)NSS_NOTFOUND);
} else {
if (_ldap_filter_name(protocol, proto, sizeof (protocol)) != 0)
return ((nss_status_t)NSS_NOTFOUND);
ret = snprintf(searchfilter, sizeof (searchfilter),
_F_GETSERVBYPORTPROTO,
strtol(portstr, (char **)NULL, 10), protocol);
if (ret >= sizeof (searchfilter) || ret < 0)
return ((nss_status_t)NSS_NOTFOUND);
ret = snprintf(userdata, sizeof (userdata),
_F_GETSERVBYPORTPROTO_SSD,
strtol(portstr, (char **)NULL, 10), protocol);
if (ret >= sizeof (userdata) || ret < 0)
return ((nss_status_t)NSS_NOTFOUND);
}
return ((nss_status_t)_nss_ldap_lookup(be, argp,
_SERVICES, searchfilter, NULL,
_merge_SSD_filter, userdata));
}
static ldap_backend_op_t serv_ops[] = {
_nss_ldap_destr,
_nss_ldap_endent,
_nss_ldap_setent,
_nss_ldap_getent,
getbyname,
getbyport
};
nss_backend_t *
_nss_ldap_services_constr(const char *dummy1, const char *dummy2,
const char *dummy3)
{
return ((nss_backend_t *)_nss_ldap_constr(serv_ops,
sizeof (serv_ops)/sizeof (serv_ops[0]), _SERVICES,
services_attrs, _nss_ldap_services2str));
}