nss_mc_initgr.c revision 39b31427e2d11ca318df11fd48db33a7cc610aa7
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * System Security Services Daemon. NSS client interface
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * Lukas Slebodnik <lslebodn@redhat.com>
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * Copyright (C) 2015 Red Hat
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * This program is free software; you can redistribute it and/or modify
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * it under the terms of the GNU Lesser General Public License as
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * published by the Free Software Foundation; either version 2.1 of the
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * License, or (at your option) any later version.
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * This program is distributed in the hope that it will be useful,
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * but WITHOUT ANY WARRANTY; without even the implied warranty of
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * GNU Lesser General Public License for more details.
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * You should have received a copy of the GNU Lesser General Public License
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * along with this program. If not, see <http://www.gnu.org/licenses/>.
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik/* INITGROUPs database NSS interface using mmap cache */
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnikstruct sss_cli_mc_ctx initgr_mc_ctx = { UNINITIALIZED, -1, 0, NULL, 0, NULL, 0,
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnikstatic errno_t sss_nss_mc_parse_result(struct sss_mc_rec *rec,
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik /* additional checks before filling result*/
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik /* entry is now invalid */
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik data = (struct sss_mc_initgr_data *)rec->data;
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik /* check we have enough space in the buffer */
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik newgroups = (gid_t *)realloc((*groups), newsize * sizeof(**groups));
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik for (i = 0; i < max_ret; i++) {
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik SAFEALIGN_COPY_UINT32(&(*groups)[*start], data->gids + i, NULL);
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnikerrno_t sss_nss_mc_initgroups_dyn(const char *name, size_t name_len,
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik gid_t group, long int *start, long int *size,
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik ret = sss_nss_mc_get_ctx("initgroups", &initgr_mc_ctx);
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik /* Get max address of data table. */
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik max_addr = initgr_mc_ctx.data_table + initgr_mc_ctx.dt_size;
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik /* hashes are calculated including the NULL terminator */
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik hash = sss_nss_mc_hash(&initgr_mc_ctx, name, name_len + 1);
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik /* If slot is not within the bounds of mmaped region and
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * it's value is not MC_INVALID_VAL, then the cache is
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * probbably corrupted. */
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik while (MC_SLOT_WITHIN_BOUNDS(slot, initgr_mc_ctx.dt_size)) {
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik /* free record from previous iteration */
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik ret = sss_nss_mc_get_record(&initgr_mc_ctx, slot, &rec);
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik /* check record matches what we are searching for */
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik /* if name hash does not match we can skip this immediately */
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik slot = sss_nss_mc_next_slot_with_hash(rec, hash);
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik data = (struct sss_mc_initgr_data *)rec->data;
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik /* Integrity check
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * - array with gids must be within data_table
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik * - string must be within data_table */
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik || (uint8_t *)data + data->name + name_len > max_addr) {
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik slot = sss_nss_mc_next_slot_with_hash(rec, hash);
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik if (!MC_SLOT_WITHIN_BOUNDS(slot, initgr_mc_ctx.dt_size)) {
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik ret = sss_nss_mc_parse_result(rec, start, size, groups, limit);