#include <netdb.h>
#include "files_common.h"
#include <sys/types.h>
#include <netinet/in.h>
#include <inttypes.h>
#include <strings.h>
#include <ctype.h>
#include <stdlib.h>
static int
check_name(nss_XbyY_args_t *argp, const char *line, int linelen)
{
const char *limit, *linep, *keyp;
int name_match = 0;
linep = line;
limit = line + linelen;
keyp = argp->key.serv.serv.name;
while (*keyp && linep < limit && !isspace(*linep) && *keyp == *linep) {
keyp++;
linep++;
}
if (*keyp == '\0' && linep < limit && isspace(*linep)) {
if (argp->key.serv.proto == NULL)
return (1);
else
name_match = 1;
}
while (linep < limit && !isspace(*linep))
linep++;
while (linep < limit && isspace(*linep))
linep++;
while (linep < limit && !isspace(*linep) && *linep != '/')
linep++;
if (linep == limit || *linep != '/')
return (0);
linep++;
if ((keyp = argp->key.serv.proto) == NULL) {
while (linep < limit && !isspace(*linep))
linep++;
} else {
while (*keyp && linep < limit && !isspace(*linep) &&
*keyp == *linep) {
keyp++;
linep++;
}
if (*keyp || (linep < limit && !isspace(*linep)))
return (0);
if (name_match)
return (1);
}
while (linep < limit) {
while (linep < limit && isspace(*linep))
linep++;
keyp = argp->key.serv.serv.name;
while (*keyp && linep < limit && !isspace(*linep) &&
*keyp == *linep) {
keyp++;
linep++;
}
if (*keyp == '\0' && (linep == limit || isspace(*linep)))
return (1);
while (linep < limit && !isspace(*linep))
linep++;
}
return (0);
}
static nss_status_t
getbyname(be, a)
files_backend_ptr_t be;
void *a;
{
nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
return (_nss_files_XY_all(be, argp, 1,
argp->key.serv.serv.name, check_name));
}
static int
check_port(nss_XbyY_args_t *argp, const char *line, int linelen)
{
const char *limit, *linep, *keyp, *numstart;
int numlen, s_port;
char numbuf[12], *numend;
linep = line;
limit = line + linelen;
while (linep < limit && !isspace(*linep))
linep++;
while (linep < limit && isspace(*linep))
linep++;
numstart = linep;
while (linep < limit && !isspace(*linep) && *linep != '/')
linep++;
if (linep == limit || *linep != '/')
return (0);
numlen = linep - numstart;
if (numlen == 0 || numlen >= sizeof (numbuf))
return (0);
(void) memcpy(numbuf, numstart, numlen);
numbuf[numlen] = '\0';
s_port = htons((int)strtol(numbuf, &numend, 10));
if (*numend != '\0')
return (0);
if (s_port == argp->key.serv.serv.port) {
if ((keyp = argp->key.serv.proto) == NULL)
return (1);
} else
return (0);
linep++;
while (*keyp && linep < limit && !isspace(*linep) && *keyp == *linep) {
keyp++;
linep++;
}
return (*keyp == '\0' && (linep == limit || isspace(*linep)));
}
static nss_status_t
getbyport(be, a)
files_backend_ptr_t be;
void *a;
{
nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
char portstr[12];
(void) snprintf(portstr, 12, "%d", ntohs(argp->key.serv.serv.port));
return (_nss_files_XY_all(be, argp, 1, portstr, check_port));
}
static files_backend_op_t serv_ops[] = {
_nss_files_destr,
_nss_files_endent,
_nss_files_setent,
_nss_files_getent_netdb,
getbyname,
getbyport
};
nss_backend_t *
_nss_files_services_constr(dummy1, dummy2, dummy3)
const char *dummy1, *dummy2, *dummy3;
{
return (_nss_files_constr(serv_ops,
sizeof (serv_ops) / sizeof (serv_ops[0]),
_PATH_SERVICES,
NSS_LINELEN_SERVICES,
NULL));
}