|
Packit |
6bd9ab |
/*
|
|
Packit |
6bd9ab |
protocol.c - protocol name and number lookup routines
|
|
Packit |
6bd9ab |
Parts of this file were part of the nss_ldap library (as ldap-proto.c)
|
|
Packit |
6bd9ab |
which has been forked into the nss-pam-ldapd library.
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
Copyright (C) 1997-2005 Luke Howard
|
|
Packit |
6bd9ab |
Copyright (C) 2006 West Consulting
|
|
Packit |
6bd9ab |
Copyright (C) 2006-2014 Arthur de Jong
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
This library is free software; you can redistribute it and/or
|
|
Packit |
6bd9ab |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6bd9ab |
License as published by the Free Software Foundation; either
|
|
Packit |
6bd9ab |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
This library is distributed in the hope that it will be useful,
|
|
Packit |
6bd9ab |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6bd9ab |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6bd9ab |
Lesser General Public License for more details.
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6bd9ab |
License along with this library; if not, write to the Free Software
|
|
Packit |
6bd9ab |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
Packit |
6bd9ab |
02110-1301 USA
|
|
Packit |
6bd9ab |
*/
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
#include "config.h"
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
#include <stdio.h>
|
|
Packit |
6bd9ab |
#include <stdlib.h>
|
|
Packit |
6bd9ab |
#include <string.h>
|
|
Packit |
6bd9ab |
#ifdef HAVE_STDINT_H
|
|
Packit |
6bd9ab |
#include <stdint.h>
|
|
Packit |
6bd9ab |
#endif /* HAVE_STDINT_H */
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
#include "common.h"
|
|
Packit |
6bd9ab |
#include "log.h"
|
|
Packit |
6bd9ab |
#include "myldap.h"
|
|
Packit |
6bd9ab |
#include "cfg.h"
|
|
Packit |
6bd9ab |
#include "attmap.h"
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
/* ( nisSchema.2.4 NAME 'ipProtocol' SUP top STRUCTURAL
|
|
Packit |
6bd9ab |
* DESC 'Abstraction of an IP protocol. Maps a protocol number
|
|
Packit |
6bd9ab |
* to one or more names. The distinguished value of the cn
|
|
Packit |
6bd9ab |
* attribute denotes the protocol's canonical name'
|
|
Packit |
6bd9ab |
* MUST ( cn $ ipProtocolNumber )
|
|
Packit |
6bd9ab |
* MAY description )
|
|
Packit |
6bd9ab |
*/
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
/* the search base for searches */
|
|
Packit |
6bd9ab |
const char *protocol_bases[NSS_LDAP_CONFIG_MAX_BASES] = { NULL };
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
/* the search scope for searches */
|
|
Packit |
6bd9ab |
int protocol_scope = LDAP_SCOPE_DEFAULT;
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
/* the basic search filter for searches */
|
|
Packit |
6bd9ab |
const char *protocol_filter = "(objectClass=ipProtocol)";
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
/* the attributes used in searches */
|
|
Packit |
6bd9ab |
const char *attmap_protocol_cn = "cn";
|
|
Packit |
6bd9ab |
const char *attmap_protocol_ipProtocolNumber = "ipProtocolNumber";
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
/* the attribute list to request with searches */
|
|
Packit |
6bd9ab |
static const char *protocol_attrs[3];
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
static int mkfilter_protocol_byname(const char *name,
|
|
Packit |
6bd9ab |
char *buffer, size_t buflen)
|
|
Packit |
6bd9ab |
{
|
|
Packit |
6bd9ab |
char safename[BUFLEN_SAFENAME];
|
|
Packit |
6bd9ab |
/* escape attribute */
|
|
Packit |
6bd9ab |
if (myldap_escape(name, safename, sizeof(safename)))
|
|
Packit |
6bd9ab |
{
|
|
Packit |
6bd9ab |
log_log(LOG_ERR, "mkfilter_protocol_byname(): safename buffer too small");
|
|
Packit |
6bd9ab |
return -1;
|
|
Packit |
6bd9ab |
}
|
|
Packit |
6bd9ab |
/* build filter */
|
|
Packit |
6bd9ab |
return mysnprintf(buffer, buflen, "(&%s(%s=%s))",
|
|
Packit |
6bd9ab |
protocol_filter, attmap_protocol_cn, safename);
|
|
Packit |
6bd9ab |
}
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
/* create a search filter for searching a protocol entry
|
|
Packit |
6bd9ab |
by uid, return -1 on errors */
|
|
Packit |
6bd9ab |
static int mkfilter_protocol_bynumber(int protocol,
|
|
Packit |
6bd9ab |
char *buffer, size_t buflen)
|
|
Packit |
6bd9ab |
{
|
|
Packit |
6bd9ab |
return mysnprintf(buffer, buflen, "(&%s(%s=%d))",
|
|
Packit |
6bd9ab |
protocol_filter, attmap_protocol_ipProtocolNumber, protocol);
|
|
Packit |
6bd9ab |
}
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
void protocol_init(void)
|
|
Packit |
6bd9ab |
{
|
|
Packit |
6bd9ab |
int i;
|
|
Packit |
6bd9ab |
/* set up search bases */
|
|
Packit |
6bd9ab |
if (protocol_bases[0] == NULL)
|
|
Packit |
6bd9ab |
for (i = 0; i < NSS_LDAP_CONFIG_MAX_BASES; i++)
|
|
Packit |
6bd9ab |
protocol_bases[i] = nslcd_cfg->bases[i];
|
|
Packit |
6bd9ab |
/* set up scope */
|
|
Packit |
6bd9ab |
if (protocol_scope == LDAP_SCOPE_DEFAULT)
|
|
Packit |
6bd9ab |
protocol_scope = nslcd_cfg->scope;
|
|
Packit |
6bd9ab |
/* set up attribute list */
|
|
Packit |
6bd9ab |
protocol_attrs[0] = attmap_protocol_cn;
|
|
Packit |
6bd9ab |
protocol_attrs[1] = attmap_protocol_ipProtocolNumber;
|
|
Packit |
6bd9ab |
protocol_attrs[2] = NULL;
|
|
Packit |
6bd9ab |
}
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
static int write_protocol(TFILE *fp, MYLDAP_ENTRY *entry, const char *reqname)
|
|
Packit |
6bd9ab |
{
|
|
Packit |
6bd9ab |
int32_t tmpint32, tmp2int32, tmp3int32;
|
|
Packit |
6bd9ab |
const char *name;
|
|
Packit |
6bd9ab |
const char **aliases;
|
|
Packit |
6bd9ab |
const char **protos;
|
|
Packit |
6bd9ab |
char *tmp;
|
|
Packit |
6bd9ab |
long proto;
|
|
Packit |
6bd9ab |
int i;
|
|
Packit |
6bd9ab |
/* get the most canonical name */
|
|
Packit |
6bd9ab |
name = myldap_get_rdn_value(entry, attmap_protocol_cn);
|
|
Packit |
6bd9ab |
/* get the other names for the protocol */
|
|
Packit |
6bd9ab |
aliases = myldap_get_values(entry, attmap_protocol_cn);
|
|
Packit |
6bd9ab |
if ((aliases == NULL) || (aliases[0] == NULL))
|
|
Packit |
6bd9ab |
{
|
|
Packit |
6bd9ab |
log_log(LOG_WARNING, "%s: %s: missing",
|
|
Packit |
6bd9ab |
myldap_get_dn(entry), attmap_protocol_cn);
|
|
Packit |
6bd9ab |
return 0;
|
|
Packit |
6bd9ab |
}
|
|
Packit |
6bd9ab |
/* if the protocol name is not yet found, get the first entry */
|
|
Packit |
6bd9ab |
if (name == NULL)
|
|
Packit |
6bd9ab |
name = aliases[0];
|
|
Packit |
6bd9ab |
/* check case of returned protocol entry */
|
|
Packit |
6bd9ab |
if ((reqname != NULL) && (STR_CMP(reqname, name) != 0))
|
|
Packit |
6bd9ab |
{
|
|
Packit |
6bd9ab |
for (i = 0; (aliases[i] != NULL) && (STR_CMP(reqname, aliases[i]) != 0); i++)
|
|
Packit |
6bd9ab |
/* nothing */ ;
|
|
Packit |
6bd9ab |
if (aliases[i] == NULL)
|
|
Packit |
6bd9ab |
return 0; /* neither the name nor any of the aliases matched */
|
|
Packit |
6bd9ab |
}
|
|
Packit |
6bd9ab |
/* get the protocol number */
|
|
Packit |
6bd9ab |
protos = myldap_get_values(entry, attmap_protocol_ipProtocolNumber);
|
|
Packit |
6bd9ab |
if ((protos == NULL) || (protos[0] == NULL))
|
|
Packit |
6bd9ab |
{
|
|
Packit |
6bd9ab |
log_log(LOG_WARNING, "%s: %s: missing",
|
|
Packit |
6bd9ab |
myldap_get_dn(entry), attmap_protocol_ipProtocolNumber);
|
|
Packit |
6bd9ab |
return 0;
|
|
Packit |
6bd9ab |
}
|
|
Packit |
6bd9ab |
else if (protos[1] != NULL)
|
|
Packit |
6bd9ab |
{
|
|
Packit |
6bd9ab |
log_log(LOG_WARNING, "%s: %s: multiple values",
|
|
Packit |
6bd9ab |
myldap_get_dn(entry), attmap_protocol_ipProtocolNumber);
|
|
Packit |
6bd9ab |
}
|
|
Packit |
6bd9ab |
errno = 0;
|
|
Packit |
6bd9ab |
proto = strtol(protos[0], &tmp, 10);
|
|
Packit |
6bd9ab |
if ((*(protos[0]) == '\0') || (*tmp != '\0'))
|
|
Packit |
6bd9ab |
{
|
|
Packit |
6bd9ab |
log_log(LOG_WARNING, "%s: %s: non-numeric",
|
|
Packit |
6bd9ab |
myldap_get_dn(entry), attmap_protocol_ipProtocolNumber);
|
|
Packit |
6bd9ab |
return 0;
|
|
Packit |
6bd9ab |
}
|
|
Packit |
6bd9ab |
else if ((errno != 0) || (proto < 0) || (proto > (long)UINT8_MAX))
|
|
Packit |
6bd9ab |
{
|
|
Packit |
6bd9ab |
log_log(LOG_WARNING, "%s: %s: out of range",
|
|
Packit |
6bd9ab |
myldap_get_dn(entry), attmap_protocol_ipProtocolNumber);
|
|
Packit |
6bd9ab |
return 0;
|
|
Packit |
6bd9ab |
}
|
|
Packit |
6bd9ab |
/* write entry */
|
|
Packit |
6bd9ab |
WRITE_INT32(fp, NSLCD_RESULT_BEGIN);
|
|
Packit |
6bd9ab |
WRITE_STRING(fp, name);
|
|
Packit |
6bd9ab |
WRITE_STRINGLIST_EXCEPT(fp, aliases, name);
|
|
Packit |
6bd9ab |
/* proto number is actually an 8-bit value but we write 32 bits anyway */
|
|
Packit |
6bd9ab |
WRITE_INT32(fp, proto);
|
|
Packit |
6bd9ab |
return 0;
|
|
Packit |
6bd9ab |
}
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
NSLCD_HANDLE(
|
|
Packit |
6bd9ab |
protocol, byname, NSLCD_ACTION_PROTOCOL_BYNAME,
|
|
Packit |
6bd9ab |
char name[BUFLEN_NAME];
|
|
Packit |
6bd9ab |
char filter[BUFLEN_FILTER];
|
|
Packit |
6bd9ab |
READ_STRING(fp, name);
|
|
Packit |
6bd9ab |
log_setrequest("protocol=\"%s\"", name);,
|
|
Packit |
6bd9ab |
mkfilter_protocol_byname(name, filter, sizeof(filter)),
|
|
Packit |
6bd9ab |
write_protocol(fp, entry, name)
|
|
Packit |
6bd9ab |
)
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
NSLCD_HANDLE(
|
|
Packit |
6bd9ab |
protocol, bynumber, NSLCD_ACTION_PROTOCOL_BYNUMBER,
|
|
Packit |
6bd9ab |
int protocol;
|
|
Packit |
6bd9ab |
char filter[BUFLEN_FILTER];
|
|
Packit |
6bd9ab |
READ_INT32(fp, protocol);
|
|
Packit |
6bd9ab |
log_setrequest("protocol=%lu", (unsigned long int)protocol);,
|
|
Packit |
6bd9ab |
mkfilter_protocol_bynumber(protocol, filter, sizeof(filter)),
|
|
Packit |
6bd9ab |
write_protocol(fp, entry, NULL)
|
|
Packit |
6bd9ab |
)
|
|
Packit |
6bd9ab |
|
|
Packit |
6bd9ab |
NSLCD_HANDLE(
|
|
Packit |
6bd9ab |
protocol, all, NSLCD_ACTION_PROTOCOL_ALL,
|
|
Packit |
6bd9ab |
const char *filter;
|
|
Packit |
6bd9ab |
log_setrequest("protocol(all)");,
|
|
Packit |
6bd9ab |
(filter = protocol_filter, 0),
|
|
Packit |
6bd9ab |
write_protocol(fp, entry, NULL)
|
|
Packit |
6bd9ab |
)
|