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,
225dc6914cdc8920b02a129b98ece1ed97b99c03Lukas Slebodnik const size_t data_offset = offsetof(struct sss_mc_initgr_data, gids);
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik ret = sss_nss_mc_get_ctx("initgroups", &initgr_mc_ctx);
ba847347cade817ee927397d82c952b51b0dcb2bLukas Slebodnik /* Get max size of data table. */
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. */
ba847347cade817ee927397d82c952b51b0dcb2bLukas Slebodnik while (MC_SLOT_WITHIN_BOUNDS(slot, data_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
225dc6914cdc8920b02a129b98ece1ed97b99c03Lukas Slebodnik * - name_len cannot be longer than all strings or data
ba847347cade817ee927397d82c952b51b0dcb2bLukas Slebodnik * - all data must be within copy of record
ba847347cade817ee927397d82c952b51b0dcb2bLukas Slebodnik * - size of record must be lower that data table size
ba847347cade817ee927397d82c952b51b0dcb2bLukas Slebodnik * - data->strs cannot point outside strings */
ba847347cade817ee927397d82c952b51b0dcb2bLukas Slebodnik || (data->strs + name_len) > (data_offset + data->data_len)) {
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik slot = sss_nss_mc_next_slot_with_hash(rec, hash);
ba847347cade817ee927397d82c952b51b0dcb2bLukas Slebodnik if (!MC_SLOT_WITHIN_BOUNDS(slot, data_size)) {
88e68607e474ab2ce46c562753ef2e988516d1e9Lukas Slebodnik ret = sss_nss_mc_parse_result(rec, start, size, groups, limit);