f128b7b865062da662127712935dcc58bd022384Stephen Gallagher/*
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher SSSD
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher nss_netgroup.c
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher Authors:
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher Stephen Gallagher <sgallagh@redhat.com>
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher Copyright (C) 2010 Red Hat
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher This program is free software; you can redistribute it and/or modify
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher it under the terms of the GNU General Public License as published by
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher the Free Software Foundation; either version 3 of the License, or
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher (at your option) any later version.
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher This program is distributed in the hope that it will be useful,
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher but WITHOUT ANY WARRANTY; without even the implied warranty of
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher GNU General Public License for more details.
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher You should have received a copy of the GNU General Public License
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher along with this program. If not, see <http://www.gnu.org/licenses/>.
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher*/
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher#include <nss.h>
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher#include <errno.h>
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher#include <sys/types.h>
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher#include <unistd.h>
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher#include <stdlib.h>
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher#include <stdint.h>
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher#include <stdio.h>
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher#include <string.h>
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher#include "sss_cli.h"
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher#include "nss_compat.h"
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose#define CLEAR_NETGRENT_DATA(netgrent) do { \
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose free(netgrent->data); \
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose netgrent->data = NULL; \
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose netgrent->idx.position = 0; \
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose netgrent->data_size = 0; \
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose} while (0);
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher/*
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher * Replies:
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher *
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose * 0-3: 32bit unsigned number of results N
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher * 4-7: 32bit unsigned (reserved/padding)
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher * For each result:
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose * 8-11: 32bit unsigned type of result
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose * 12-X: \0 terminated string representing a tuple
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose * (host, user, domain)
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose * or a netgroup, depending on the type indicator
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose * ... repeated N times
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher */
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher#define NETGR_METADATA_COUNT 2 * sizeof(uint32_t)
f128b7b865062da662127712935dcc58bd022384Stephen Gallagherstruct sss_nss_netgr_rep {
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher struct __netgrent *result;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher char *buffer;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher size_t buflen;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher};
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagherstatic int sss_nss_getnetgr_readrep(struct sss_nss_netgr_rep *pr,
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher uint8_t *buf, size_t *len)
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher{
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher errno_t ret;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher char *sbuf;
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher char *temp;
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher size_t i, slen, dlen, size;
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose uint32_t type;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose if (*len < 6) {
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher /* Not enough space for data, bad packet */
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher return EBADMSG;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher }
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose sbuf = (char *)(buf + sizeof(uint32_t));
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose slen = *len - sizeof(uint32_t);
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher dlen = pr->buflen;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher i = 0;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose SAFEALIGN_COPY_UINT32(&type, buf, NULL);
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose switch (type) {
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose case SSS_NETGR_REP_TRIPLE:
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose pr->result->type = triple_val;
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose /* Host value */
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher temp = &(pr->buffer[i]);
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher ret = sss_readrep_copy_string(sbuf, &i,
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher &slen, &dlen,
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher &temp,
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher &size);
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher if (ret != EOK) return ret;
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose /* libc expects NULL instead of empty string */
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher if (size == 0) {
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose pr->result->val.triple.host = NULL;
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher } else {
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher pr->result->val.triple.host = temp;
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose }
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose /* User value */
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher temp = &(pr->buffer[i]);
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher ret = sss_readrep_copy_string(sbuf, &i,
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher &slen, &dlen,
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher &temp,
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher &size);
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher if (ret != EOK) return ret;
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose /* libc expects NULL instead of empty string */
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher if (size == 0) {
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose pr->result->val.triple.user = NULL;
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher } else {
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher pr->result->val.triple.user = temp;
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose }
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose /* Domain value */
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher temp = &(pr->buffer[i]);
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher ret = sss_readrep_copy_string(sbuf, &i,
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher &slen, &dlen,
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher &temp,
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher &size);
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher if (ret != EOK) return ret;
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose /* libc expects NULL instead of empty string */
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher if (size == 0) {
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose pr->result->val.triple.domain = NULL;
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher } else {
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher pr->result->val.triple.domain = temp;
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose }
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose break;
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose case SSS_NETGR_REP_GROUP:
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose pr->result->type = group_val;
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher temp = &(pr->buffer[i]);
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher ret = sss_readrep_copy_string(sbuf, &i,
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher &slen, &dlen,
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher &temp,
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher NULL);
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher if (ret != EOK) return ret;
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher pr->result->val.group = temp;
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose break;
fd3714d0cf068f3c782c1fff32105fc51cc97a0eStephen Gallagher
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose default:
36fc83f3f64bb16db7bef3e1cebe829424edacd1Sumit Bose return EBADMSG;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher }
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher *len = slen -i;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher return 0;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher}
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagherenum nss_status _nss_sss_setnetgrent(const char *netgroup,
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher struct __netgrent *result)
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher{
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher uint8_t *repbuf = NULL;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher size_t replen;
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek uint32_t num_results;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher enum nss_status nret;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher struct sss_cli_req_data rd;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher int errnop;
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose char *name;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher size_t name_len;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher errno_t ret;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher if (!netgroup) return NSS_STATUS_NOTFOUND;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce sss_nss_lock();
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher /* make sure we do not have leftovers, and release memory */
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose CLEAR_NETGRENT_DATA(result);
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
22c7230dc0c8d41a189eb758be78991d183de1f7Stephen Gallagher ret = sss_strnlen(netgroup, SSS_NAME_MAX, &name_len);
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce if (ret != 0) {
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce nret = NSS_STATUS_NOTFOUND;
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce goto out;
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce }
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose name = malloc(sizeof(char)*name_len + 1);
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose if (name == NULL) {
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce nret = NSS_STATUS_TRYAGAIN;
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce goto out;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher }
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose strncpy(name, netgroup, name_len + 1);
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose rd.data = name;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher rd.len = name_len + 1;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher nret = sss_nss_make_request(SSS_NSS_SETNETGRENT, &rd,
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher &repbuf, &replen, &errnop);
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose free(name);
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher if (nret != NSS_STATUS_SUCCESS) {
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher errno = errnop;
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce goto out;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher }
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek /* Get number of results from repbuf */
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek SAFEALIGN_COPY_UINT32(&num_results, repbuf, NULL);
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher /* no results if not found */
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek if ((num_results == 0) || (replen < NETGR_METADATA_COUNT)) {
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher free(repbuf);
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce nret = NSS_STATUS_NOTFOUND;
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce goto out;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher }
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose free(repbuf);
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce nret = NSS_STATUS_SUCCESS;
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorceout:
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce sss_nss_unlock();
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce return nret;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher}
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorcestatic enum nss_status internal_getnetgrent_r(struct __netgrent *result,
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce char *buffer, size_t buflen,
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce int *errnop)
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher{
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher struct sss_cli_req_data rd;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher struct sss_nss_netgr_rep netgrrep;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher uint8_t *repbuf;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher size_t replen;
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek uint32_t num_results;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher enum nss_status nret;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher uint32_t num_entries;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher int ret;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen 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 }
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher /* If we're already processing result data, continue to
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher * return it.
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher */
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose if (result->data != NULL &&
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose result->idx.position < result->data_size) {
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose repbuf = (uint8_t *) result->data + result->idx.position;
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose replen = result->data_size - result->idx.position;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher netgrrep.result = result;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher netgrrep.buffer = buffer;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher netgrrep.buflen = buflen;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher ret = sss_nss_getnetgr_readrep(&netgrrep, repbuf, &replen);
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher if (ret != 0) {
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher *errnop = ret;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher return NSS_STATUS_TRYAGAIN;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher }
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose result->idx.position = result->data_size - replen;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher return NSS_STATUS_SUCCESS;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher }
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher /* Release memory, if any */
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose CLEAR_NETGRENT_DATA(result);
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher /* retrieve no more than SSS_NSS_MAX_ENTRIES at a time */
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher num_entries = SSS_NSS_MAX_ENTRIES;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher rd.len = sizeof(uint32_t);
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher rd.data = &num_entries;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher nret = sss_nss_make_request(SSS_NSS_GETNETGRENT, &rd,
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher &repbuf, &replen, errnop);
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher if (nret != NSS_STATUS_SUCCESS) {
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher return nret;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher }
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek /* Get number of results from repbuf. */
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek SAFEALIGN_COPY_UINT32(&num_results, repbuf, NULL);
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher /* no results if not found */
a171d77f40aa92e240e91aa4bafe5a392a98b5a2Michal Zidek if ((num_results == 0) || (replen <= NETGR_METADATA_COUNT)) {
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher free(repbuf);
c640ae818270b1e8d57190516587d06c007d3938Sumit Bose return NSS_STATUS_RETURN;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher }
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose result->data = (char *) repbuf;
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose result->data_size = replen;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher /* skip metadata fields */
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose result->idx.position = NETGR_METADATA_COUNT;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher /* call again ourselves, this will return the first result */
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce return internal_getnetgrent_r(result, buffer, buflen, errnop);
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce}
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorceenum nss_status _nss_sss_getnetgrent_r(struct __netgrent *result,
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce char *buffer, size_t buflen,
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce int *errnop)
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce{
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce enum nss_status nret;
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce sss_nss_lock();
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce nret = internal_getnetgrent_r(result, buffer, buflen, errnop);
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce sss_nss_unlock();
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce return nret;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher}
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Boseenum nss_status _nss_sss_endnetgrent(struct __netgrent *result)
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher{
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher enum nss_status nret;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher int errnop;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce sss_nss_lock();
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher /* make sure we do not have leftovers, and release memory */
60dceaee2014822717a3f22e1b65b228e20cc5f0Sumit Bose CLEAR_NETGRENT_DATA(result);
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher nret = sss_nss_make_request(SSS_NSS_ENDNETGRENT,
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher NULL, NULL, NULL, &errnop);
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher if (nret != NSS_STATUS_SUCCESS) {
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher errno = errnop;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher }
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce sss_nss_unlock();
c89589fa349f38214c9cb8d9389c0fd557e5dca2Simo Sorce return nret;
f128b7b865062da662127712935dcc58bd022384Stephen Gallagher}