Esempio n. 1
0
static enum nss_status
internal_nis_getservent_r (struct servent *serv, char *buffer,
			   size_t buflen, int *errnop, intern_t *data)
{
  struct parser_data *pdata = (void *) buffer;
  int parse_res;
  char *p;

  if (data->start == NULL)
    internal_nis_setservent (data);

  /* Get the next entry until we found a correct one. */
  do
    {
      if (data->next == NULL)
	return NSS_STATUS_NOTFOUND;

      p = strncpy (buffer, data->next->val, buflen);
      while (isspace (*p))
        ++p;

      parse_res = _nss_files_parse_servent (p, serv, pdata, buflen, errnop);
      if (parse_res == -1)
        return NSS_STATUS_TRYAGAIN;
      data->next = data->next->next;
    }
  while (!parse_res);

  return NSS_STATUS_SUCCESS;
}
Esempio n. 2
0
enum nss_status
_nss_nis_setservent (int stayopen)
{
  enum nss_status status;

  __libc_lock_lock (lock);

  status = internal_nis_setservent (&intern);

  __libc_lock_unlock (lock);

  return status;
}
Esempio n. 3
0
enum nss_status
_nss_nis_getservbyname_r (const char *name, const char *protocol,
			  struct servent *serv, char *buffer, size_t buflen,
			  int *errnop)
{
  intern_t data = { NULL, NULL };
  enum nss_status status;
  int found;

  if (name == NULL)
    {
      *errnop = EINVAL;
      return NSS_STATUS_UNAVAIL;
    }

  /* If the protocol is given, we could try if our NIS server knows
     about services.byservicename map. If yes, we only need one query */
  if (protocol != NULL)
    {
      char key[strlen (name) + strlen (protocol) + 2];
      char *cp, *domain, *result;
      size_t keylen, len;
      int int_len;

      /* If this fails, the other solution will also fail. */
      if (yp_get_default_domain (&domain))
	return NSS_STATUS_UNAVAIL;

      /* key is: "name/protocol" */
      cp = stpcpy (key, name);
      *cp++ = '/';
      stpcpy (cp, protocol);
      keylen = strlen (key);
      status = yperr2nss (yp_match (domain, "services.byservicename", key,
				    keylen, &result, &int_len));
      len = int_len;

      /* If we found the key, it's ok and parse the result. If not,
	 fall through and parse the complete table. */
      if (status == NSS_STATUS_SUCCESS)
	{
	  struct parser_data *pdata = (void *) buffer;
	  int parse_res;
	  char *p;

	  if ((size_t) (len + 1) > buflen)
	    {
	      free (result);
	      *errnop = ERANGE;
	      return NSS_STATUS_TRYAGAIN;
	    }

	  p = strncpy (buffer, result, len);
	  buffer[len] = '\0';
	  while (isspace (*p))
	    ++p;
	  free (result);
	  parse_res = _nss_files_parse_servent (p, serv, pdata,
						buflen, errnop);
	  if (parse_res < 0)
	    {
	      if (parse_res == -1)
		return NSS_STATUS_TRYAGAIN;
	      else
		{
		  *errnop = ENOENT;
		  return NSS_STATUS_NOTFOUND;
		}
	    }
	  else
	    return NSS_STATUS_SUCCESS;
	}
    }

  status = internal_nis_setservent (&data);
  if (status != NSS_STATUS_SUCCESS)
    return status;

  found = 0;
  while (!found &&
         ((status = internal_nis_getservent_r (serv, buffer, buflen, errnop,
					       &data)) == NSS_STATUS_SUCCESS))
    {
      if (protocol == NULL || strcmp (serv->s_proto, protocol) == 0)
	{
	  char **cp;

	  if (strcmp (serv->s_name, name) == 0)
	    found = 1;
	  else
	    for (cp = serv->s_aliases; *cp; cp++)
	      if (strcmp (name, *cp) == 0)
		found = 1;
	}
    }

  internal_nis_endservent (&data);

  if (!found && status == NSS_STATUS_SUCCESS)
    {
      *errnop = ENOENT;
      return NSS_STATUS_NOTFOUND;
    }
  else
    return status;
}
Esempio n. 4
0
enum nss_status
_nss_nis_getservbyport_r (int port, const char *protocol,
			  struct servent *serv, char *buffer,
			  size_t buflen, int *errnop)
{
  intern_t data = { NULL, NULL };
  enum nss_status status;
  int found;

  /* If the protocol is given, we only need one query */
  if (protocol != NULL)
    {
      char key[100 + strlen (protocol) + 2];
      char *domain, *result;
      size_t keylen, len;
      int int_len;

      /* If this fails, the other solution will also fail. */
      if (yp_get_default_domain (&domain))
	return NSS_STATUS_UNAVAIL;

      /* key is: "port/protocol" */
      keylen = snprintf (key, sizeof (key), "%d/%s", port, protocol);
      status = yperr2nss (yp_match (domain, "services.byname", key,
				    keylen, &result, &int_len));
      len = int_len;

      /* If we found the key, it's ok and parse the result. If not,
	 fall through and parse the complete table. */
      if (status == NSS_STATUS_SUCCESS)
	{
	  struct parser_data *pdata = (void *) buffer;
	  int parse_res;
	  char *p;

	  if ((size_t) (len + 1) > buflen)
	    {
	      free (result);
	      *errnop = ERANGE;
	      return NSS_STATUS_TRYAGAIN;
	    }

	  p = strncpy (buffer, result, len);
	  buffer[len] = '\0';
	  while (isspace (*p))
	    ++p;
	  free (result);
	  parse_res = _nss_files_parse_servent (p, serv, pdata,
						buflen, errnop);
	  if (parse_res < 0)
	    {
	      if (parse_res == -1)
		return NSS_STATUS_TRYAGAIN;
	      else
		{
		  *errnop = ENOENT;
		  return NSS_STATUS_NOTFOUND;
		}
	    }
	  else
	    return NSS_STATUS_SUCCESS;
	}
    }

  status = internal_nis_setservent (&data);
  if (status != NSS_STATUS_SUCCESS)
    return status;

  found = 0;
  while (!found &&
         ((status = internal_nis_getservent_r (serv, buffer, buflen, errnop,
					       &data)) == NSS_STATUS_SUCCESS))
    if (serv->s_port == port &&
	(protocol == NULL || strcmp (serv->s_proto, protocol) == 0))
      found = 1;

  internal_nis_endservent (&data);

  if (!found && status == NSS_STATUS_SUCCESS)
    {
      *errnop = ENOENT;
      return NSS_STATUS_NOTFOUND;
    }
  else
    return status;
}
Esempio n. 5
0
static enum nss_status
internal_nis_getservent_r (struct servent *serv, char *buffer,
			   size_t buflen, int *errnop)
{
  struct parser_data *pdata = (void *) buffer;
  int parse_res;
  char *p;

  if (intern.start == NULL)
    internal_nis_setservent ();

  if (intern.next == NULL)
    /* Not one entry in the map.  */
    return NSS_STATUS_NOTFOUND;

  /* Get the next entry until we found a correct one.  */
  do
    {
      struct response_t *bucket = intern.next;

      if (__glibc_unlikely (intern.offset >= bucket->size))
	{
	  if (bucket->next == NULL)
	    return NSS_STATUS_NOTFOUND;

	  /* We look at all the content in the current bucket.  Go on
	     to the next.  */
	  bucket = intern.next = bucket->next;
	  intern.offset = 0;
	}

      for (p = &bucket->mem[intern.offset]; isspace (*p); ++p)
        ++intern.offset;

      size_t len = strlen (p) + 1;
      if (__glibc_unlikely (len > buflen))
	{
	  *errnop = ERANGE;
	  return NSS_STATUS_TRYAGAIN;
	}

      /* We unfortunately have to copy the data in the user-provided
	 buffer because that buffer might be around for a very long
	 time and the servent structure must remain valid.  If we would
	 rely on the BUCKET memory the next 'setservent' or 'endservent'
	 call would destroy it.

	 The important thing is that it is a single NUL-terminated
	 string.  This is what the parsing routine expects.  */
      p = memcpy (buffer, &bucket->mem[intern.offset], len);

      parse_res = _nss_files_parse_servent (p, serv, pdata, buflen, errnop);
      if (__glibc_unlikely (parse_res == -1))
        return NSS_STATUS_TRYAGAIN;

      intern.offset += len;
    }
  while (!parse_res);

  return NSS_STATUS_SUCCESS;
}