5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose/*
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose SSSD
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose Extended NSS Responder Interface
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose Authors:
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose Sumit Bose <sbose@redhat.com>
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose Copyright (C) 2017 Red Hat
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose This program is free software; you can redistribute it and/or modify
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose it under the terms of the GNU General Public License as published by
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose the Free Software Foundation; either version 3 of the License, or
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose (at your option) any later version.
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose This program is distributed in the hope that it will be useful,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose but WITHOUT ANY WARRANTY; without even the implied warranty of
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose GNU General Public License for more details.
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose You should have received a copy of the GNU General Public License
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose along with this program. If not, see <http://www.gnu.org/licenses/>.
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose*/
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose#include <stdlib.h>
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose#include <errno.h>
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose#include <sys/param.h> /* for MIN() */
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose#include "sss_client/sss_cli.h"
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose#include "sss_client/nss_mc.h"
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose#include "sss_client/nss_common.h"
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose#include "sss_client/idmap/sss_nss_idmap.h"
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose#include "sss_client/idmap/sss_nss_idmap_private.h"
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose#ifndef discard_const
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose#define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose#endif
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bosestruct sss_nss_initgr_rep {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose gid_t *groups;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose long int *ngroups;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose long int *start;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose};
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bosestruct nss_input {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose union {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose const char *name;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose uid_t uid;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose gid_t gid;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose } input;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose struct sss_cli_req_data rd;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose enum sss_cli_command cmd;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose union {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose struct sss_nss_pw_rep pwrep;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose struct sss_nss_gr_rep grrep;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose struct sss_nss_initgr_rep initgrrep;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose } result;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose};
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Boseerrno_t sss_nss_mc_get(struct nss_input *inp)
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose{
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose switch(inp->cmd) {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose case SSS_NSS_GETPWNAM:
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose case SSS_NSS_GETPWNAM_EX:
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose return sss_nss_mc_getpwnam(inp->input.name, strlen(inp->input.name),
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp->result.pwrep.result,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp->result.pwrep.buffer,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp->result.pwrep.buflen);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose break;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose case SSS_NSS_GETPWUID:
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose case SSS_NSS_GETPWUID_EX:
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose return sss_nss_mc_getpwuid(inp->input.uid,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp->result.pwrep.result,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp->result.pwrep.buffer,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp->result.pwrep.buflen);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose break;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose case SSS_NSS_GETGRNAM:
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose case SSS_NSS_GETGRNAM_EX:
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose return sss_nss_mc_getgrnam(inp->input.name, strlen(inp->input.name),
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp->result.grrep.result,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp->result.grrep.buffer,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp->result.grrep.buflen);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose break;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose case SSS_NSS_GETGRGID:
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose case SSS_NSS_GETGRGID_EX:
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose return sss_nss_mc_getgrgid(inp->input.gid,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp->result.grrep.result,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp->result.grrep.buffer,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp->result.grrep.buflen);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose break;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose case SSS_NSS_INITGR:
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose case SSS_NSS_INITGR_EX:
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose return sss_nss_mc_initgroups_dyn(inp->input.name,
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose strlen(inp->input.name),
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose -1 /* currently ignored */,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp->result.initgrrep.start,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp->result.initgrrep.ngroups,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose &(inp->result.initgrrep.groups),
46a4c265629d9b725c41f22849741ce7342bdd85Sumit Bose /* no limit so that needed size can
46a4c265629d9b725c41f22849741ce7342bdd85Sumit Bose * be returned properly */
46a4c265629d9b725c41f22849741ce7342bdd85Sumit Bose -1);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose break;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose default:
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose return EINVAL;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose}
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bosestatic int check_flags(struct nss_input *inp, uint32_t flags,
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose bool *skip_mc, bool *skip_data)
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose{
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose bool no_data = false;
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose /* SSS_NSS_EX_FLAG_NO_CACHE and SSS_NSS_EX_FLAG_INVALIDATE_CACHE are
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose * mutually exclusive */
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose if ((flags & SSS_NSS_EX_FLAG_NO_CACHE) != 0
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose && (flags & SSS_NSS_EX_FLAG_INVALIDATE_CACHE) != 0) {
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose return EINVAL;
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose }
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose *skip_mc = false;
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose if ((flags & SSS_NSS_EX_FLAG_NO_CACHE) != 0
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose || (flags & SSS_NSS_EX_FLAG_INVALIDATE_CACHE) != 0) {
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose *skip_mc = true;
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose }
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose switch(inp->cmd) {
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose case SSS_NSS_GETPWNAM:
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose case SSS_NSS_GETPWNAM_EX:
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose case SSS_NSS_GETPWUID:
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose case SSS_NSS_GETPWUID_EX:
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose if (inp->result.pwrep.buffer == NULL
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose || inp->result.pwrep.buflen == 0) {
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose no_data = true;
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose }
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose break;
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose case SSS_NSS_GETGRNAM:
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose case SSS_NSS_GETGRNAM_EX:
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose case SSS_NSS_GETGRGID:
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose case SSS_NSS_GETGRGID_EX:
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose if (inp->result.grrep.buffer == NULL
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose || inp->result.grrep.buflen == 0) {
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose no_data = true;
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose }
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose break;
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose case SSS_NSS_INITGR:
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose case SSS_NSS_INITGR_EX:
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose if (inp->result.initgrrep.ngroups == 0
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose || inp->result.initgrrep.groups == NULL) {
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose return EINVAL;
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose }
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose break;
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose default:
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose return EINVAL;
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose }
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose *skip_data = false;
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose /* Allow empty buffer with SSS_NSS_EX_FLAG_INVALIDATE_CACHE */
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose if (no_data) {
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose if ((flags & SSS_NSS_EX_FLAG_INVALIDATE_CACHE) != 0) {
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose *skip_data = true;
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose } else {
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose return ERANGE;
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose }
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose }
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose return 0;
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose}
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Boseint sss_get_ex(struct nss_input *inp, uint32_t flags, unsigned int timeout)
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose{
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose uint8_t *repbuf = NULL;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose size_t replen;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose size_t len;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose uint32_t num_results;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose int ret;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose int time_left;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose int errnop;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose size_t c;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose gid_t *new_groups;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose size_t idx;
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose bool skip_mc = false;
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose bool skip_data = false;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose ret = check_flags(inp, flags, &skip_mc, &skip_data);
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose if (ret != 0) {
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose return ret;
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose }
55f7d8034d783c01789d76a2b9ffc901045e8af8Sumit Bose
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose if (!skip_mc && !skip_data) {
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose ret = sss_nss_mc_get(inp);
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose switch (ret) {
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose case 0:
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose return 0;
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose case ERANGE:
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose return ERANGE;
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose case ENOENT:
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose /* fall through, we need to actively ask the parent
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose * if no entry is found */
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose break;
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose default:
080e1bfb72ed0e8d96e390d83ad35eaba79bd450René Genz /* if using the mmapped cache failed,
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose * fall back to socket based comms */
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose break;
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
3e32cb2ad36a9dd2654c7f63469dc595f1bb8593Sumit Bose ret = sss_nss_timedlock(timeout, &time_left);
3e32cb2ad36a9dd2654c7f63469dc595f1bb8593Sumit Bose if (ret != 0) {
3e32cb2ad36a9dd2654c7f63469dc595f1bb8593Sumit Bose return ret;
3e32cb2ad36a9dd2654c7f63469dc595f1bb8593Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose if (!skip_mc && !skip_data) {
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose /* previous thread might already initialize entry in mmap cache */
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose ret = sss_nss_mc_get(inp);
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose switch (ret) {
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose case 0:
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose ret = 0;
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose goto out;
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose case ERANGE:
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose ret = ERANGE;
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose goto out;
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose case ENOENT:
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose /* fall through, we need to actively ask the parent
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose * if no entry is found */
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose break;
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose default:
080e1bfb72ed0e8d96e390d83ad35eaba79bd450René Genz /* if using the mmapped cache failed,
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose * fall back to socket based comms */
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose break;
ac6b267ff3df6d0417062a128ec16b184ea2c1b7Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = sss_nss_make_request_timeout(inp->cmd, &inp->rd, time_left,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose &repbuf, &replen, &errnop);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose if (ret != NSS_STATUS_SUCCESS) {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = errnop != 0 ? errnop : EIO;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose goto out;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose /* Get number of results from repbuf. */
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose SAFEALIGN_COPY_UINT32(&num_results, repbuf, NULL);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose /* no results if not found */
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose if (num_results == 0) {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = ENOENT;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose goto out;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose if (skip_data) {
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose /* No data requested, just return the return code */
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose ret = 0;
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose goto out;
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose }
859bddc2bf51dc426a3dc56bd9f365e9c5722b65Sumit Bose
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose if (inp->cmd == SSS_NSS_INITGR || inp->cmd == SSS_NSS_INITGR_EX) {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose if ((*(inp->result.initgrrep.ngroups) - *(inp->result.initgrrep.start))
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose < num_results) {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose new_groups = realloc(inp->result.initgrrep.groups,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose (num_results + *(inp->result.initgrrep.start))
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose * sizeof(gid_t));
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose if (new_groups == NULL) {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = ENOMEM;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose goto out;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp->result.initgrrep.groups = new_groups;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose *(inp->result.initgrrep.ngroups) = num_results
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose + *(inp->result.initgrrep.start);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose idx = 2 * sizeof(uint32_t);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose for (c = 0; c < num_results; c++) {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose SAFEALIGN_COPY_UINT32(
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose &(inp->result.initgrrep.groups[*(inp->result.initgrrep.start)]),
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose repbuf + idx, &idx);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose *(inp->result.initgrrep.start) += 1;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = 0;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose goto out;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose /* only 1 result is accepted for this function */
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose if (num_results != 1) {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = EBADMSG;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose goto out;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose len = replen - 8;
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose switch(inp->cmd) {
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose case SSS_NSS_GETPWNAM:
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose case SSS_NSS_GETPWUID:
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose case SSS_NSS_GETPWNAM_EX:
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose case SSS_NSS_GETPWUID_EX:
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = sss_nss_getpw_readrep(&(inp->result.pwrep), repbuf+8, &len);
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose break;
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose case SSS_NSS_GETGRNAM:
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose case SSS_NSS_GETGRGID:
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose case SSS_NSS_GETGRNAM_EX:
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose case SSS_NSS_GETGRGID_EX:
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = sss_nss_getgr_readrep(&(inp->result.grrep), repbuf+8, &len);
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose break;
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose default:
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = EINVAL;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose if (ret != 0) {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose goto out;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose if (len == 0) {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose /* no extra data */
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = 0;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose goto out;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Boseout:
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose free(repbuf);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose sss_nss_unlock();
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose return ret;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose}
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bosestatic int make_name_flag_req_data(const char *name, uint32_t flags,
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose struct sss_cli_req_data *rd)
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose{
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose size_t len;
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose size_t name_len;
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose uint8_t *data;
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose int ret;
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose if (name == NULL) {
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose return EINVAL;
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose }
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose ret = sss_strnlen(name, SSS_NAME_MAX, &name_len);
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose if (ret != 0) {
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose return ret;
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose }
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose name_len++;
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose len = name_len + sizeof(uint32_t);
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose data = malloc(len);
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose if (data == NULL) {
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose return ENOMEM;
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose }
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose memcpy(data, name, name_len);
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose SAFEALIGN_COPY_UINT32(data + name_len, &flags, NULL);
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose rd->len = len;
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose rd->data = data;
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose return 0;
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose}
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Boseint sss_nss_getpwnam_timeout(const char *name, struct passwd *pwd,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose char *buffer, size_t buflen,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose struct passwd **result,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose uint32_t flags, unsigned int timeout)
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose{
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose int ret;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose struct nss_input inp = {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .input.name = name,
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose .cmd = SSS_NSS_GETPWNAM_EX,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .result.pwrep.result = pwd,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .result.pwrep.buffer = buffer,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .result.pwrep.buflen = buflen};
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose ret = make_name_flag_req_data(name, flags, &inp.rd);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose if (ret != 0) {
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose return ret;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = sss_get_ex(&inp, flags, timeout);
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose free(discard_const(inp.rd.data));
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose if (result != NULL) {
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose if (ret == 0) {
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose *result = inp.result.pwrep.result;
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose } else {
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose *result = NULL;
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose return ret;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose}
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Boseint sss_nss_getpwuid_timeout(uid_t uid, struct passwd *pwd,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose char *buffer, size_t buflen,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose struct passwd **result,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose uint32_t flags, unsigned int timeout)
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose{
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose int ret;
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose uint32_t req_data[2];
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose struct nss_input inp = {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .input.uid = uid,
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose .cmd = SSS_NSS_GETPWUID_EX,
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose .rd.len = 2 * sizeof(uint32_t),
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose .rd.data = &req_data,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .result.pwrep.result = pwd,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .result.pwrep.buffer = buffer,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .result.pwrep.buflen = buflen};
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose SAFEALIGN_COPY_UINT32(&req_data[0], &uid, NULL);
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose SAFEALIGN_COPY_UINT32(&req_data[1], &flags, NULL);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = sss_get_ex(&inp, flags, timeout);
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose if (result != NULL) {
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose if (ret == 0) {
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose *result = inp.result.pwrep.result;
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose } else {
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose *result = NULL;
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose return ret;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose}
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Boseint sss_nss_getgrnam_timeout(const char *name, struct group *grp,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose char *buffer, size_t buflen, struct group **result,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose uint32_t flags, unsigned int timeout)
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose{
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose int ret;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose struct nss_input inp = {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .input.name = name,
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose .cmd = SSS_NSS_GETGRNAM_EX,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .result.grrep.result = grp,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .result.grrep.buffer = buffer,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .result.grrep.buflen = buflen};
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose ret = make_name_flag_req_data(name, flags, &inp.rd);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose if (ret != 0) {
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose return ret;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = sss_get_ex(&inp, flags, timeout);
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose free(discard_const(inp.rd.data));
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose if (result != NULL) {
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose if (ret == 0) {
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose *result = inp.result.grrep.result;
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose } else {
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose *result = NULL;
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose return ret;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose}
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Boseint sss_nss_getgrgid_timeout(gid_t gid, struct group *grp,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose char *buffer, size_t buflen, struct group **result,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose uint32_t flags, unsigned int timeout)
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose{
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose int ret;
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose uint32_t req_data[2];
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose struct nss_input inp = {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .input.gid = gid,
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose .cmd = SSS_NSS_GETGRGID_EX,
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose .rd.len = 2 * sizeof(uint32_t),
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose .rd.data = &req_data,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .result.grrep.result = grp,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .result.grrep.buffer = buffer,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .result.grrep.buflen = buflen};
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose SAFEALIGN_COPY_UINT32(&req_data[0], &gid, NULL);
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose SAFEALIGN_COPY_UINT32(&req_data[1], &flags, NULL);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = sss_get_ex(&inp, flags, timeout);
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose if (result != NULL) {
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose if (ret == 0) {
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose *result = inp.result.grrep.result;
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose } else {
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose *result = NULL;
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
bba068c535d23eebff61f592bddb3a6438446d6fSumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose return ret;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose}
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Boseint sss_nss_getgrouplist_timeout(const char *name, gid_t group,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose gid_t *groups, int *ngroups,
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose uint32_t flags, unsigned int timeout)
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose{
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose int ret;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose long int new_ngroups;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose long int start = 1;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose struct nss_input inp = {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose .input.name = name,
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose .cmd = SSS_NSS_INITGR_EX};
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
cf93f7c2f2031078bbbff095dae01eb4f8deff85Sumit Bose ret = make_name_flag_req_data(name, flags, &inp.rd);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose if (ret != 0) {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose return ret;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose new_ngroups = MAX(1, *ngroups);
2c4dc7a4d98c439c69625f12ba4c3c8253f4cc5bSumit Bose inp.result.initgrrep.groups = malloc(new_ngroups * sizeof(gid_t));
2c4dc7a4d98c439c69625f12ba4c3c8253f4cc5bSumit Bose if (inp.result.initgrrep.groups == NULL) {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose free(discard_const(inp.rd.data));
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose return ENOMEM;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
2c4dc7a4d98c439c69625f12ba4c3c8253f4cc5bSumit Bose inp.result.initgrrep.groups[0] = group;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp.result.initgrrep.ngroups = &new_ngroups;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose inp.result.initgrrep.start = &start;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
2c4dc7a4d98c439c69625f12ba4c3c8253f4cc5bSumit Bose /* inp.result.initgrrep.groups, inp.result.initgrrep.ngroups and
2c4dc7a4d98c439c69625f12ba4c3c8253f4cc5bSumit Bose * inp.result.initgrrep.start might be modified by sss_get_ex() */
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = sss_get_ex(&inp, flags, timeout);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose free(discard_const(inp.rd.data));
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose if (ret != 0) {
2c4dc7a4d98c439c69625f12ba4c3c8253f4cc5bSumit Bose free(inp.result.initgrrep.groups);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose return ret;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
2c4dc7a4d98c439c69625f12ba4c3c8253f4cc5bSumit Bose memcpy(groups, inp.result.initgrrep.groups,
2c4dc7a4d98c439c69625f12ba4c3c8253f4cc5bSumit Bose MIN(*ngroups, start) * sizeof(gid_t));
2c4dc7a4d98c439c69625f12ba4c3c8253f4cc5bSumit Bose free(inp.result.initgrrep.groups);
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose if (start > *ngroups) {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = ERANGE;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose } else {
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose ret = 0;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose }
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose *ngroups = start;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose return ret;
5e6622722e84d594298a8324f3685a1bda2b5868Sumit Bose}