Blame hesiod/nss_hesiod/hesiod-proto.c

Packit 6c4009
/* Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <hesiod.h>
Packit 6c4009
#include <netdb.h>
Packit 6c4009
#include <netinet/in.h>
Packit 6c4009
#include <nss.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
/* Declare a parser for Hesiod protocol entries.  Although the format
Packit 6c4009
   of the entries is identical to those in /etc/protocols, here is no
Packit 6c4009
   predefined parser for us to use.  */
Packit 6c4009
Packit 6c4009
#define ENTNAME protoent
Packit 6c4009
Packit 6c4009
struct protoent_data {};
Packit 6c4009
Packit 6c4009
#define TRAILING_LIST_MEMBER		p_aliases
Packit 6c4009
#define TRAILING_LIST_SEPARATOR_P	isspace
Packit 6c4009
#include <nss/nss_files/files-parse.c>
Packit 6c4009
LINE_PARSER
Packit 6c4009
("#",
Packit 6c4009
 STRING_FIELD (result->p_name, isspace, 1);
Packit 6c4009
 INT_FIELD (result->p_proto, isspace, 1, 10,);
Packit 6c4009
 )
Packit 6c4009
Packit 6c4009
enum nss_status
Packit 6c4009
_nss_hesiod_setprotoent (int stayopen)
Packit 6c4009
{
Packit 6c4009
  return NSS_STATUS_SUCCESS;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
enum nss_status
Packit 6c4009
_nss_hesiod_endprotoent (void)
Packit 6c4009
{
Packit 6c4009
  return NSS_STATUS_SUCCESS;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static enum nss_status
Packit 6c4009
lookup (const char *name, const char *type, struct protoent *proto,
Packit 6c4009
	char *buffer, size_t buflen, int *errnop)
Packit 6c4009
{
Packit 6c4009
  struct parser_data *data = (void *) buffer;
Packit 6c4009
  size_t linebuflen;
Packit 6c4009
  void *context;
Packit 6c4009
  char **list, **item;
Packit 6c4009
  int parse_res;
Packit 6c4009
  int found;
Packit 6c4009
  int olderr = errno;
Packit 6c4009
Packit 6c4009
  if (hesiod_init (&context) < 0)
Packit 6c4009
    return NSS_STATUS_UNAVAIL;
Packit 6c4009
Packit 6c4009
  list = hesiod_resolve (context, name, type);
Packit 6c4009
  if (list == NULL)
Packit 6c4009
    {
Packit 6c4009
      int err = errno;
Packit 6c4009
      hesiod_end (context);
Packit 6c4009
      __set_errno (olderr);
Packit 6c4009
      return err == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  linebuflen = buffer + buflen - data->linebuffer;
Packit 6c4009
Packit 6c4009
  item = list;
Packit 6c4009
  found = 0;
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      size_t len = strlen (*item) + 1;
Packit 6c4009
Packit 6c4009
      if (linebuflen < len)
Packit 6c4009
	{
Packit 6c4009
	  hesiod_free_list (context, list);
Packit 6c4009
	  hesiod_end (context);
Packit 6c4009
	  *errnop = ERANGE;
Packit 6c4009
	  return NSS_STATUS_TRYAGAIN;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      memcpy (data->linebuffer, *item, len);
Packit 6c4009
Packit 6c4009
      parse_res = parse_line (buffer, proto, data, buflen, errnop);
Packit 6c4009
      if (parse_res == -1)
Packit 6c4009
	{
Packit 6c4009
	  hesiod_free_list (context, list);
Packit 6c4009
	  hesiod_end (context);
Packit 6c4009
	  return NSS_STATUS_TRYAGAIN;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (parse_res > 0)
Packit 6c4009
	found = 1;
Packit 6c4009
Packit 6c4009
      ++item;
Packit 6c4009
    }
Packit 6c4009
  while (*item != NULL && !found);
Packit 6c4009
Packit 6c4009
  hesiod_free_list (context, list);
Packit 6c4009
  hesiod_end (context);
Packit 6c4009
Packit 6c4009
  if (found == 0)
Packit 6c4009
    {
Packit 6c4009
      __set_errno (olderr);
Packit 6c4009
      return NSS_STATUS_NOTFOUND;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return NSS_STATUS_SUCCESS;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
enum nss_status
Packit 6c4009
_nss_hesiod_getprotobyname_r (const char *name, struct protoent *proto,
Packit 6c4009
			      char *buffer, size_t buflen, int *errnop)
Packit 6c4009
{
Packit 6c4009
  return lookup (name, "protocol", proto, buffer, buflen, errnop);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
enum nss_status
Packit 6c4009
_nss_hesiod_getprotobynumber_r (const int protocol, struct protoent *proto,
Packit 6c4009
				char *buffer, size_t buflen, int *errnop)
Packit 6c4009
{
Packit 6c4009
  char protostr[21];
Packit 6c4009
Packit 6c4009
  snprintf (protostr, sizeof protostr, "%d", protocol);
Packit 6c4009
Packit 6c4009
  return lookup (protostr, "protonum", proto, buffer, buflen, errnop);
Packit 6c4009
}