cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher/*
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher SSSD
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher Authors:
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher Stephen Gallagher <sgallagh@redhat.com>
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher Copyright (C) 2012 Red Hat
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher This program is free software; you can redistribute it and/or modify
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher it under the terms of the GNU General Public License as published by
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher the Free Software Foundation; either version 3 of the License, or
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher (at your option) any later version.
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher This program is distributed in the hope that it will be useful,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher but WITHOUT ANY WARRANTY; without even the implied warranty of
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher GNU General Public License for more details.
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher You should have received a copy of the GNU General Public License
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher along with this program. If not, see <http://www.gnu.org/licenses/>.
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher*/
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher#include <nss.h>
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher#include <netdb.h>
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher#include <errno.h>
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher#include <sys/types.h>
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher#include <unistd.h>
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher#include <stdlib.h>
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher#include <stdint.h>
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher#include <stdio.h>
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher#include <string.h>
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher#include "sss_cli.h"
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagherstatic struct sss_nss_getservent_data {
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher size_t len;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher size_t ptr;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher uint8_t *data;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher} sss_nss_getservent_data;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagherstatic void sss_nss_getservent_data_clean(void) {
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher if (sss_nss_getservent_data.data != NULL) {
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher free(sss_nss_getservent_data.data);
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_getservent_data.data = NULL;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher }
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_getservent_data.len = 0;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_getservent_data.ptr = 0;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher}
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher/* GETSERVBYNAME Request
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher *
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * 0-X: Sequence of two, zero-terminated strings (name, protocol).
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * Protocol may be zero-length to imply "any"
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher *
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * GETSERVBYPORT Request:
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * 0-3: 16-bit port number in network byte order
8126906ab783494204dc853c814b0882d2a76523Stephen Gallagher * 4-15: Reserved/padding
8126906ab783494204dc853c814b0882d2a76523Stephen Gallagher * 16-X: Zero-terminated string (protocol)
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * Protocol may be zero-length to imply "any"
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher *
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * Replies:
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * 0-3: 32-bit unsigned number of results
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * 4-7: 32-bit unsigned (reserved/padding)
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * 7-X: Result data (blocks equal to number of results)
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher *
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * Result data:
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * 0-3: 32-bit unsigned port number in network byte order
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * 4-7: 32-bit unsigned number of aliases
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * 8-X: sequence of zero-terminated strings
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * (name, protocol, zero or more aliases)
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher */
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagherstruct sss_nss_svc_rep {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher struct servent *result;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher char *buffer;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher size_t buflen;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher};
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher#define SVC_METADATA_COUNT 8
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagherstatic errno_t
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallaghersss_nss_getsvc_readrep(struct sss_nss_svc_rep *sr,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher uint8_t *buf, size_t *len)
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher{
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher errno_t ret;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher uint32_t c;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher uint32_t num_aliases;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher size_t i, l, slen, dlen, pad, ptaliases, alen;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher char *sbuf;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher /* Buffer must contain two 32-bit integers,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * at least one character and null-terminator
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * for the name, and at least a null-
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher * terminator for the protocol.
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher */
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (*len < 11) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher /* not enough space for data, bad packet */
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher return EBADMSG;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher /* Get the port */
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher SAFEALIGN_COPY_UINT32(&c, buf, NULL);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher sr->result->s_port = (uint16_t)c;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher /* Get the number of aliases */
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher SAFEALIGN_COPY_UINT32(&num_aliases, buf + sizeof(uint32_t), NULL);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher sbuf = (char *)&buf[2 * sizeof(uint32_t)];
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher slen = *len - (2 * sizeof(uint32_t));
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher dlen = sr->buflen;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher /* Copy in the name */
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher i = 0;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher sr->result->s_name = &(sr->buffer[i]);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher ret = sss_readrep_copy_string(sbuf, &i,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher &slen, &dlen,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher &sr->result->s_name,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher NULL);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (ret != EOK) return ret;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher /* Copy in the protocol */
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher sr->result->s_proto = &(sr->buffer[i]);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher ret = sss_readrep_copy_string(sbuf, &i,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher &slen, &dlen,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher &sr->result->s_proto,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher NULL);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (ret != EOK) return ret;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
8bf65dbab8703697c85b033beb5c189fce17b036Michal Zidek /* Make sure sr->buffer[i+pad] is aligned to sizeof(char *) */
8bf65dbab8703697c85b033beb5c189fce17b036Michal Zidek pad = PADDING_SIZE(i, char *);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher /* Copy in the aliases */
684405758a96bd9fb47480d44f2e1a2f038f8defMichal Zidek sr->result->s_aliases = DISCARD_ALIGN(&(sr->buffer[i+pad]), char **);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher ptaliases = (sizeof(char *) * (num_aliases + 1)) + pad;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (ptaliases > dlen) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher return ERANGE; /* not ENOMEM, ERANGE is what glibc looks for */
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher dlen -= ptaliases;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher ptaliases += i;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher sr->result->s_aliases[num_aliases] = NULL; /* terminate array */
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher for (l = 0; l < num_aliases; l++) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher sr->result->s_aliases[l] = &(sr->buffer[ptaliases]);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher ret = sss_readrep_copy_string(sbuf, &i,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher &slen, &dlen,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher &sr->result->s_aliases[l],
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher &alen);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (ret != EOK) return ret;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher ptaliases += alen + 1;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher *len = slen - i;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher return EOK;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher}
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagherenum nss_status
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher_nss_sss_getservbyname_r(const char *name,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher const char *protocol,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher struct servent *result,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher char *buffer, size_t buflen,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher int *errnop)
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher{
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher struct sss_cli_req_data rd;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher struct sss_nss_svc_rep svcrep;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher size_t name_len;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher size_t proto_len = 0;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher uint8_t *repbuf;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher uint8_t *data;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher size_t replen, len;
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek uint32_t num_results;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher enum nss_status nret;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher int ret;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher /* Caught once glibc passing in buffer == 0x0 */
c096972ff25f14a390a621851f4935901b908be8Carlos O'Donell if (!buffer || !buflen) {
c096972ff25f14a390a621851f4935901b908be8Carlos O'Donell *errnop = ERANGE;
c096972ff25f14a390a621851f4935901b908be8Carlos O'Donell return NSS_STATUS_TRYAGAIN;
c096972ff25f14a390a621851f4935901b908be8Carlos O'Donell }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher ret = sss_strnlen(name, SSS_NAME_MAX, &name_len);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (ret != 0) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher *errnop = EINVAL;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher return NSS_STATUS_NOTFOUND;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (protocol) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher ret = sss_strnlen(protocol, SSS_NAME_MAX, &proto_len);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (ret != 0) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher *errnop = EINVAL;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher return NSS_STATUS_NOTFOUND;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher rd.len = name_len + proto_len + 2;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher data = malloc(sizeof(uint8_t)*rd.len);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (data == NULL) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher nret = NSS_STATUS_TRYAGAIN;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher goto out;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher memcpy(data, name, name_len + 1);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (protocol) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher memcpy(data + name_len + 1, protocol, proto_len + 1);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher } else {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher /* No protocol specified, pass empty string */
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher data[name_len + 1] = '\0';
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher rd.data = data;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher sss_nss_lock();
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher nret = sss_nss_make_request(SSS_NSS_GETSERVBYNAME, &rd,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher &repbuf, &replen, errnop);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher free(data);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (nret != NSS_STATUS_SUCCESS) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher goto out;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher svcrep.result = result;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher svcrep.buffer = buffer;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher svcrep.buflen = buflen;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek /* Get number of results from repbuf. */
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek SAFEALIGN_COPY_UINT32(&num_results, repbuf, NULL);
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher /* no results if not found */
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek if (num_results == 0) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher free(repbuf);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher nret = NSS_STATUS_NOTFOUND;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher goto out;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher /* only 1 result is accepted for this function */
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek if (num_results != 1) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher *errnop = EBADMSG;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher free(repbuf);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher nret = NSS_STATUS_TRYAGAIN;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher goto out;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher len = replen - SVC_METADATA_COUNT;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher ret = sss_nss_getsvc_readrep(&svcrep,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher repbuf + SVC_METADATA_COUNT,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher &len);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher free(repbuf);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (ret) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher *errnop = ret;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher nret = NSS_STATUS_TRYAGAIN;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher goto out;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher nret = NSS_STATUS_SUCCESS;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagherout:
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher sss_nss_unlock();
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher return nret;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher}
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagherenum nss_status
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher_nss_sss_getservbyport_r(int port, const char *protocol,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher struct servent *result,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher char *buffer, size_t buflen,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher int *errnop)
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher{
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher struct sss_cli_req_data rd;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher struct sss_nss_svc_rep svcrep;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher size_t proto_len = 0;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher uint8_t *repbuf;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher uint8_t *data;
188f9e1e646b0bed530913ca76bbcdf0f342cc66Jakub Hrozek size_t p = 0;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher size_t replen, len;
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek uint32_t num_results;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher enum nss_status nret;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher int ret;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher /* Caught once glibc passing in buffer == 0x0 */
c096972ff25f14a390a621851f4935901b908be8Carlos O'Donell if (!buffer || !buflen) {
c096972ff25f14a390a621851f4935901b908be8Carlos O'Donell *errnop = ERANGE;
c096972ff25f14a390a621851f4935901b908be8Carlos O'Donell return NSS_STATUS_TRYAGAIN;
c096972ff25f14a390a621851f4935901b908be8Carlos O'Donell }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (protocol) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher ret = sss_strnlen(protocol, SSS_NAME_MAX, &proto_len);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (ret != 0) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher *errnop = EINVAL;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher return NSS_STATUS_NOTFOUND;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
b481712148b36362e09d7611be1f36be6b962ca0Jakub Hrozek rd.len = sizeof(uint32_t)*2 + proto_len + 1;
188f9e1e646b0bed530913ca76bbcdf0f342cc66Jakub Hrozek data = malloc(sizeof(uint8_t)*rd.len);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (data == NULL) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher nret = NSS_STATUS_TRYAGAIN;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher goto out;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
188f9e1e646b0bed530913ca76bbcdf0f342cc66Jakub Hrozek SAFEALIGN_SET_UINT16(data, port, &p);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher /* Padding */
188f9e1e646b0bed530913ca76bbcdf0f342cc66Jakub Hrozek SAFEALIGN_SET_UINT16(data + p, 0, &p);
8126906ab783494204dc853c814b0882d2a76523Stephen Gallagher SAFEALIGN_SET_UINT32(data + p, 0, &p);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (protocol) {
188f9e1e646b0bed530913ca76bbcdf0f342cc66Jakub Hrozek memcpy(data + p, protocol, proto_len + 1);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher } else {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher /* No protocol specified, pass empty string */
188f9e1e646b0bed530913ca76bbcdf0f342cc66Jakub Hrozek data[p] = '\0';
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher rd.data = data;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher sss_nss_lock();
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher nret = sss_nss_make_request(SSS_NSS_GETSERVBYPORT, &rd,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher &repbuf, &replen, errnop);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher free(data);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (nret != NSS_STATUS_SUCCESS) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher goto out;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher svcrep.result = result;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher svcrep.buffer = buffer;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher svcrep.buflen = buflen;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek /* Get number of results from repbuf. */
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek SAFEALIGN_COPY_UINT32(&num_results, repbuf, NULL);
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher /* no results if not found */
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek if (num_results == 0) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher free(repbuf);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher nret = NSS_STATUS_NOTFOUND;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher goto out;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher /* only 1 result is accepted for this function */
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek if (num_results != 1) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher *errnop = EBADMSG;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher free(repbuf);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher nret = NSS_STATUS_TRYAGAIN;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher goto out;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher len = replen - SVC_METADATA_COUNT;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher ret = sss_nss_getsvc_readrep(&svcrep,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher repbuf + SVC_METADATA_COUNT,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher &len);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher free(repbuf);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher if (ret) {
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher *errnop = ret;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher nret = NSS_STATUS_TRYAGAIN;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher goto out;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher }
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher nret = NSS_STATUS_SUCCESS;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagherout:
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher sss_nss_unlock();
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher return nret;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher}
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagherenum nss_status
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher_nss_sss_setservent(void)
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher{
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher enum nss_status nret;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher int errnop;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_lock();
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher /* make sure we do not have leftovers, and release memory */
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_getservent_data_clean();
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher nret = sss_nss_make_request(SSS_NSS_SETSERVENT,
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher NULL, NULL, NULL, &errnop);
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher if (nret != NSS_STATUS_SUCCESS) {
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher errno = errnop;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher }
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_unlock();
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher return nret;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher}
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagherstatic enum nss_status internal_getservent_r(struct servent *result,
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher char *buffer, size_t buflen,
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher int *errnop);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagherenum nss_status
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher_nss_sss_getservent_r(struct servent *result,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher char *buffer, size_t buflen,
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher int *errnop)
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher{
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher enum nss_status nret;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_lock();
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher nret = internal_getservent_r(result, buffer, buflen, errnop);
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_unlock();
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher return nret;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher}
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagherstatic enum nss_status internal_getservent_r(struct servent *result,
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher char *buffer, size_t buflen,
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher int *errnop)
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher{
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher struct sss_cli_req_data rd;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher struct sss_nss_svc_rep pwrep;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher uint8_t *repbuf;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher size_t replen;
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek uint32_t num_results;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher enum nss_status nret;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher uint32_t num_entries;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher int ret;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher /* Caught once glibc passing in buffer == 0x0 */
c096972ff25f14a390a621851f4935901b908be8Carlos O'Donell if (!buffer || !buflen) {
c096972ff25f14a390a621851f4935901b908be8Carlos O'Donell *errnop = ERANGE;
c096972ff25f14a390a621851f4935901b908be8Carlos O'Donell return NSS_STATUS_TRYAGAIN;
c096972ff25f14a390a621851f4935901b908be8Carlos O'Donell }
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher /* if there are leftovers return the next one */
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher if (sss_nss_getservent_data.data != NULL &&
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_getservent_data.ptr < sss_nss_getservent_data.len) {
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher repbuf = sss_nss_getservent_data.data + sss_nss_getservent_data.ptr;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher replen = sss_nss_getservent_data.len - sss_nss_getservent_data.ptr;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher pwrep.result = result;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher pwrep.buffer = buffer;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher pwrep.buflen = buflen;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher ret = sss_nss_getsvc_readrep(&pwrep, repbuf, &replen);
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher if (ret) {
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher *errnop = ret;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher return NSS_STATUS_TRYAGAIN;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher }
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher /* advance buffer pointer */
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_getservent_data.ptr = sss_nss_getservent_data.len - replen;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher return NSS_STATUS_SUCCESS;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher }
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher /* release memory if any */
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_getservent_data_clean();
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher /* retrieve no more than SSS_NSS_MAX_ENTRIES at a time */
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher num_entries = SSS_NSS_MAX_ENTRIES;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher rd.len = sizeof(uint32_t);
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher rd.data = &num_entries;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher nret = sss_nss_make_request(SSS_NSS_GETSERVENT, &rd,
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher &repbuf, &replen, errnop);
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher if (nret != NSS_STATUS_SUCCESS) {
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher return nret;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher }
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek /* Get number of results from repbuf */
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek SAFEALIGN_COPY_UINT32(&num_results, repbuf, NULL);
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher /* no results if not found */
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek if ((num_results == 0) || (replen - SVC_METADATA_COUNT == 0)) {
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher free(repbuf);
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher return NSS_STATUS_NOTFOUND;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher }
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_getservent_data.data = repbuf;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_getservent_data.len = replen;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher /* skip metadata fields */
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_getservent_data.ptr = SVC_METADATA_COUNT;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher /* call again ourselves, this will return the first result */
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher return internal_getservent_r(result, buffer, buflen, errnop);
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher}
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagherenum nss_status
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher_nss_sss_endservent(void)
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher{
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher enum nss_status nret;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher int errnop;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_lock();
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher /* make sure we do not have leftovers, and release memory */
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_getservent_data_clean();
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher nret = sss_nss_make_request(SSS_NSS_ENDSERVENT,
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher NULL, NULL, NULL, &errnop);
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher if (nret != NSS_STATUS_SUCCESS) {
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher errno = errnop;
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher }
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher sss_nss_unlock();
cff6bb5f8ee424b0e3aff74247e103e44075b16aStephen Gallagher return nret;
cc7ee57f3adada83ac657e69636ca995d5b6948eStephen Gallagher}