a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose/*
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose SSSD
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose ID-mapping library
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose Authors:
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose Sumit Bose <sbose@redhat.com>
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose Copyright (C) 2012 Red Hat
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose This program is free software; you can redistribute it and/or modify
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose it under the terms of the GNU General Public License as published by
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose the Free Software Foundation; either version 3 of the License, or
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose (at your option) any later version.
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose This program is distributed in the hope that it will be useful,
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose but WITHOUT ANY WARRANTY; without even the implied warranty of
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose GNU General Public License for more details.
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose You should have received a copy of the GNU General Public License
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose along with this program. If not, see <http://www.gnu.org/licenses/>.
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose*/
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose#include <string.h>
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose#include <stdio.h>
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose#include <errno.h>
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl#include <inttypes.h>
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose#include "lib/idmap/sss_idmap.h"
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose#include "lib/idmap/sss_idmap_private.h"
3996e391054a1c02ab62e1541ae21a8204bd5d0aAmitKumar#include "shared/murmurhash3.h"
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose#define SID_FMT "%s-%d"
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose#define SID_STR_MAX_LEN 1024
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl/* Hold all parameters for unix<->sid mapping relevant for
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl * given slice. */
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichlstruct idmap_range_params {
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl uint32_t min_id;
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl uint32_t max_id;
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl char *range_id;
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl uint32_t first_rid;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_range_params *next;
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl};
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bosestruct idmap_domain_info {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose char *name;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose char *sid;
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl struct idmap_range_params range_params;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose struct idmap_domain_info *next;
c63c8a63ab062a9c4397278a29b12bd32c4f3895Sumit Bose bool external_mapping;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_range_params *helpers;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl bool auto_add_ranges;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl bool helpers_owner;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl idmap_store_cb cb;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl void *pvt;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose};
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bosestatic void *default_alloc(size_t size, void *pvt)
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose{
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return malloc(size);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose}
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bosestatic void default_free(void *ptr, void *pvt)
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose{
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose free(ptr);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose}
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bosestatic char *idmap_strdup(struct sss_idmap_ctx *ctx, const char *str)
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose{
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose char *new = NULL;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose size_t len;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose CHECK_IDMAP_CTX(ctx, NULL);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose len = strlen(str) + 1;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose new = ctx->alloc_func(len, ctx->alloc_pvt);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose if (new == NULL) {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return NULL;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose memcpy(new, str, len);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return new;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose}
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichlstatic bool ranges_eq(const struct idmap_range_params *a,
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl const struct idmap_range_params *b)
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl{
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl if (a == NULL || b == NULL) {
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl return false;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl }
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl if (a->first_rid == b->first_rid
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl && a->min_id == b->min_id
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl && a->max_id == b->max_id) {
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl return true;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl }
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl return false;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl}
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichlstatic enum idmap_error_code
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichlconstruct_range(struct sss_idmap_ctx *ctx,
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl const struct idmap_range_params *src,
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl char *id,
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl struct idmap_range_params **_dst)
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl{
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl struct idmap_range_params *dst;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl if (src == NULL || id == NULL || _dst == NULL) {
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl return IDMAP_ERROR;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl }
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl dst = ctx->alloc_func(sizeof(struct idmap_range_params), ctx->alloc_pvt);
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl if (dst == NULL) {
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl return IDMAP_OUT_OF_MEMORY;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl }
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl dst->min_id = src->min_id;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl dst->max_id = src->max_id;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl dst->first_rid = src->first_rid;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl dst->next = NULL;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl dst->range_id = id;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl *_dst = dst;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl return IDMAP_SUCCESS;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl}
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichlstatic bool id_is_in_range(uint32_t id,
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl struct idmap_range_params *rp,
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose uint32_t *rid)
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose{
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl if (id == 0 || rp == NULL) {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return false;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl if (id >= rp->min_id && id <= rp->max_id) {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose if (rid != NULL) {
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl *rid = rp->first_rid + (id - rp->min_id);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return true;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return false;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose}
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Boseconst char *idmap_error_string(enum idmap_error_code err)
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose{
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose switch (err) {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose case IDMAP_SUCCESS:
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return "IDMAP operation successful";
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose break;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose case IDMAP_NOT_IMPLEMENTED:
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return "IDMAP Function is not yet implemented";
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose break;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose case IDMAP_ERROR:
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return "IDMAP general error";
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose break;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose case IDMAP_OUT_OF_MEMORY:
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return "IDMAP operation ran out of memory";
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose break;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose case IDMAP_NO_DOMAIN:
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return "IDMAP domain not found";
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose break;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose case IDMAP_CONTEXT_INVALID:
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return "IDMAP context is invalid";
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose break;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose case IDMAP_SID_INVALID:
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return "IDMAP SID is invalid";
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose break;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose case IDMAP_SID_UNKNOWN:
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return "IDMAP SID not found";
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose break;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose case IDMAP_NO_RANGE:
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return "IDMAP range not found";
c377d4d604f1e7b35c484711f1084b7a761772b6Sumit Bose break;
c377d4d604f1e7b35c484711f1084b7a761772b6Sumit Bose case IDMAP_BUILTIN_SID:
c377d4d604f1e7b35c484711f1084b7a761772b6Sumit Bose return "IDMAP SID from BUILTIN domain";
c377d4d604f1e7b35c484711f1084b7a761772b6Sumit Bose break;
c377d4d604f1e7b35c484711f1084b7a761772b6Sumit Bose case IDMAP_OUT_OF_SLICES:
c377d4d604f1e7b35c484711f1084b7a761772b6Sumit Bose return "IDMAP not more free slices";
c377d4d604f1e7b35c484711f1084b7a761772b6Sumit Bose break;
c377d4d604f1e7b35c484711f1084b7a761772b6Sumit Bose case IDMAP_COLLISION:
c377d4d604f1e7b35c484711f1084b7a761772b6Sumit Bose return "IDMAP new range collides with existing one";
c377d4d604f1e7b35c484711f1084b7a761772b6Sumit Bose break;
c377d4d604f1e7b35c484711f1084b7a761772b6Sumit Bose case IDMAP_EXTERNAL:
c377d4d604f1e7b35c484711f1084b7a761772b6Sumit Bose return "IDMAP ID managed externally";
c377d4d604f1e7b35c484711f1084b7a761772b6Sumit Bose break;
c377d4d604f1e7b35c484711f1084b7a761772b6Sumit Bose case IDMAP_NAME_UNKNOWN:
c377d4d604f1e7b35c484711f1084b7a761772b6Sumit Bose return "IDMAP domain with the given name not found";
c377d4d604f1e7b35c484711f1084b7a761772b6Sumit Bose break;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose default:
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return "IDMAP unknown error code";
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose}
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bosebool is_domain_sid(const char *sid)
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose{
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose const char *p;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose long long a;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose char *endptr;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose size_t c;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose if (sid == NULL || strncmp(sid, DOM_SID_PREFIX, DOM_SID_PREFIX_LEN) != 0) {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return false;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose p = sid + DOM_SID_PREFIX_LEN;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose c = 0;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose do {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose errno = 0;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose a = strtoull(p, &endptr, 10);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose if (errno != 0 || a > UINT32_MAX) {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return false;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose if (*endptr == '-') {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose p = endptr + 1;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose } else if (*endptr != '\0') {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return false;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose c++;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose } while(c < 3 && *endptr != '\0');
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose if (c != 3 || *endptr != '\0') {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return false;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return true;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose}
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Boseenum idmap_error_code sss_idmap_init(idmap_alloc_func *alloc_func,
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose void *alloc_pvt,
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose idmap_free_func *free_func,
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose struct sss_idmap_ctx **_ctx)
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose{
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose struct sss_idmap_ctx *ctx;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose if (alloc_func == NULL) {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose alloc_func = default_alloc;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose ctx = alloc_func(sizeof(struct sss_idmap_ctx), alloc_pvt);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose if (ctx == NULL) {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return IDMAP_OUT_OF_MEMORY;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose memset(ctx, 0, sizeof(struct sss_idmap_ctx));
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose ctx->alloc_func = alloc_func;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose ctx->alloc_pvt = alloc_pvt;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose ctx->free_func = (free_func == NULL) ? default_free : free_func;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek /* Set default values. */
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek ctx->idmap_opts.autorid_mode = SSS_IDMAP_DEFAULT_AUTORID;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek ctx->idmap_opts.idmap_lower = SSS_IDMAP_DEFAULT_LOWER;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek ctx->idmap_opts.idmap_upper = SSS_IDMAP_DEFAULT_UPPER;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek ctx->idmap_opts.rangesize = SSS_IDMAP_DEFAULT_RANGESIZE;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ctx->idmap_opts.extra_slice_init = SSS_IDMAP_DEFAULT_EXTRA_SLICE_INIT;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose *_ctx = ctx;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return IDMAP_SUCCESS;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose}
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlstatic void free_helpers(struct sss_idmap_ctx *ctx,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_range_params *helpers,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl bool helpers_owner)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl{
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_range_params *it = helpers;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_range_params *tmp;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (helpers_owner == false) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl while (it != NULL) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl tmp = it->next;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ctx->free_func(it->range_id, ctx->alloc_pvt);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ctx->free_func(it, ctx->alloc_pvt);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl it = tmp;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl}
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichlstatic struct idmap_range_params*
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichlget_helper_by_id(struct idmap_range_params *helpers, const char *id)
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl{
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl struct idmap_range_params *it;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl for (it = helpers; it != NULL; it = it->next) {
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl if (strcmp(it->range_id, id) == 0) {
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl return it;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl }
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl }
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl return NULL;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl}
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl
cff4a89e38078337d74aa558f0e628d5756b3bbaSumit Bosestatic void sss_idmap_free_domain(struct sss_idmap_ctx *ctx,
cff4a89e38078337d74aa558f0e628d5756b3bbaSumit Bose struct idmap_domain_info *dom)
cff4a89e38078337d74aa558f0e628d5756b3bbaSumit Bose{
cff4a89e38078337d74aa558f0e628d5756b3bbaSumit Bose if (ctx == NULL || dom == NULL) {
cff4a89e38078337d74aa558f0e628d5756b3bbaSumit Bose return;
cff4a89e38078337d74aa558f0e628d5756b3bbaSumit Bose }
cff4a89e38078337d74aa558f0e628d5756b3bbaSumit Bose
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl ctx->free_func(dom->range_params.range_id, ctx->alloc_pvt);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl free_helpers(ctx, dom->helpers, dom->helpers_owner);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
cff4a89e38078337d74aa558f0e628d5756b3bbaSumit Bose ctx->free_func(dom->name, ctx->alloc_pvt);
cff4a89e38078337d74aa558f0e628d5756b3bbaSumit Bose ctx->free_func(dom->sid, ctx->alloc_pvt);
cff4a89e38078337d74aa558f0e628d5756b3bbaSumit Bose ctx->free_func(dom, ctx->alloc_pvt);
cff4a89e38078337d74aa558f0e628d5756b3bbaSumit Bose}
cff4a89e38078337d74aa558f0e628d5756b3bbaSumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Boseenum idmap_error_code sss_idmap_free(struct sss_idmap_ctx *ctx)
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose{
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose struct idmap_domain_info *dom;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose struct idmap_domain_info *next;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose next = ctx->idmap_domain_info;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose while (next) {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose dom = next;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose next = dom->next;
cff4a89e38078337d74aa558f0e628d5756b3bbaSumit Bose sss_idmap_free_domain(ctx, dom);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose ctx->free_func(ctx, ctx->alloc_pvt);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return IDMAP_SUCCESS;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose}
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezinastatic enum idmap_error_code sss_idmap_free_ptr(struct sss_idmap_ctx *ctx,
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina void *ptr)
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina{
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina if (ptr != NULL) {
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina ctx->free_func(ptr, ctx->alloc_pvt);
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina }
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina return IDMAP_SUCCESS;
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina}
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezinaenum idmap_error_code sss_idmap_free_sid(struct sss_idmap_ctx *ctx,
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina char *sid)
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina{
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina return sss_idmap_free_ptr(ctx, sid);
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina}
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezinaenum idmap_error_code sss_idmap_free_dom_sid(struct sss_idmap_ctx *ctx,
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina struct sss_dom_sid *dom_sid)
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina{
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina return sss_idmap_free_ptr(ctx, dom_sid);
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina}
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezinaenum idmap_error_code sss_idmap_free_smb_sid(struct sss_idmap_ctx *ctx,
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina struct dom_sid *smb_sid)
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina{
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina return sss_idmap_free_ptr(ctx, smb_sid);
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina}
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezinaenum idmap_error_code sss_idmap_free_bin_sid(struct sss_idmap_ctx *ctx,
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina uint8_t *bin_sid)
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina{
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina return sss_idmap_free_ptr(ctx, bin_sid);
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina}
5c94d34d890c75ec179a32d2cda4fa060d2b5f0cPavel Bƙezina
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlstatic bool check_overlap(struct idmap_range_params *range,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl id_t min, id_t max)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl{
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return ((range->min_id <= min && range->max_id >= max)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl || (range->min_id >= min && range->min_id <= max)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl || (range->max_id >= min && range->max_id <= max));
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl}
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlstatic bool check_dom_overlap(struct idmap_range_params *prim_range,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl /* struct idmap_range_params *sec_ranges, */
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl id_t min,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl id_t max)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl{
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return check_overlap(prim_range, min, max);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl}
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidekenum idmap_error_code sss_idmap_calculate_range(struct sss_idmap_ctx *ctx,
bda0a8ee67c4222ede597fd57456b74e33116653Pavel Reichl const char *range_id,
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek id_t *slice_num,
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek struct sss_idmap_range *_range)
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek{
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek id_t max_slices;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek id_t orig_slice;
11fd679eb2edabd139c9016e2b7d2085ca3aac5dOndrej Kos id_t new_slice = 0;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek id_t min;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek id_t max;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek id_t idmap_lower;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek id_t idmap_upper;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek id_t rangesize;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek bool autorid_mode;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek uint32_t hash_val;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek struct idmap_domain_info *dom;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek idmap_lower = ctx->idmap_opts.idmap_lower;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek idmap_upper = ctx->idmap_opts.idmap_upper;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek rangesize = ctx->idmap_opts.rangesize;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek autorid_mode = ctx->idmap_opts.autorid_mode;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek max_slices = (idmap_upper - idmap_lower) / rangesize;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek if (slice_num && *slice_num != -1) {
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek /* The slice is being set explicitly.
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek * This may happen at system startup when we're loading
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek * previously-determined slices. In the future, we may also
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek * permit configuration to select the slice for a domain
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek * explicitly.
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek */
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek new_slice = *slice_num;
0526dde7f3d4089617c0f4a6a85f83e9d266c9f1Marlena Marlenowska min = (rangesize * new_slice) + idmap_lower;
0526dde7f3d4089617c0f4a6a85f83e9d266c9f1Marlena Marlenowska max = min + rangesize - 1;
0526dde7f3d4089617c0f4a6a85f83e9d266c9f1Marlena Marlenowska for (dom = ctx->idmap_domain_info; dom != NULL; dom = dom->next) {
0526dde7f3d4089617c0f4a6a85f83e9d266c9f1Marlena Marlenowska if (check_dom_overlap(&dom->range_params,min, max)) {
0526dde7f3d4089617c0f4a6a85f83e9d266c9f1Marlena Marlenowska /* This range overlaps one already registered
0526dde7f3d4089617c0f4a6a85f83e9d266c9f1Marlena Marlenowska * Fail, because the slice was manually configured
0526dde7f3d4089617c0f4a6a85f83e9d266c9f1Marlena Marlenowska */
0526dde7f3d4089617c0f4a6a85f83e9d266c9f1Marlena Marlenowska return IDMAP_COLLISION;
0526dde7f3d4089617c0f4a6a85f83e9d266c9f1Marlena Marlenowska }
0526dde7f3d4089617c0f4a6a85f83e9d266c9f1Marlena Marlenowska }
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek } else {
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek /* If slice is -1, we're being asked to pick a new slice */
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek if (autorid_mode) {
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek /* In autorid compatibility mode, always start at 0 and find the
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek * first free value.
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek */
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek orig_slice = 0;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek } else {
bda0a8ee67c4222ede597fd57456b74e33116653Pavel Reichl /* Hash the range identifier string */
bda0a8ee67c4222ede597fd57456b74e33116653Pavel Reichl hash_val = murmurhash3(range_id, strlen(range_id), 0xdeadbeef);
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek /* Now get take the modulus of the hash val and the max_slices
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek * to determine its optimal position in the range.
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek */
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek new_slice = hash_val % max_slices;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek orig_slice = new_slice;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek }
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek min = (rangesize * new_slice) + idmap_lower;
7db89d44b5582a0cb0a61a7aa42a2fac7ca9408fPavel Reichl max = min + rangesize - 1;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek /* Verify that this slice is not already in use */
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek do {
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek for (dom = ctx->idmap_domain_info; dom != NULL; dom = dom->next) {
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (check_dom_overlap(&dom->range_params,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl min, max)) {
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek /* This range overlaps one already registered
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek * We'll try the next available slot
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek */
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek new_slice++;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek if (new_slice >= max_slices) {
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek /* loop around to the beginning if necessary */
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek new_slice = 0;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek }
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek min = (rangesize * new_slice) + idmap_lower;
7db89d44b5582a0cb0a61a7aa42a2fac7ca9408fPavel Reichl max = min + rangesize - 1;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek break;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek }
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek }
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek /* Keep trying until dom is NULL (meaning we got to the end
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek * without matching) or we have run out of slices and gotten
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek * back to the first one we tried.
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek */
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek } while (dom && new_slice != orig_slice);
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek if (dom) {
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek /* We looped all the way through and found no empty slots */
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek return IDMAP_OUT_OF_SLICES;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek }
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek }
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek _range->min = (rangesize * new_slice) + idmap_lower;
7db89d44b5582a0cb0a61a7aa42a2fac7ca9408fPavel Reichl _range->max = _range->min + rangesize - 1;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek if (slice_num) {
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek *slice_num = new_slice;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek }
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek return IDMAP_SUCCESS;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek}
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Boseenum idmap_error_code sss_idmap_check_collision_ex(const char *o_name,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose const char *o_sid,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose struct sss_idmap_range *o_range,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose uint32_t o_first_rid,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose const char *o_range_id,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose bool o_external_mapping,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose const char *n_name,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose const char *n_sid,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose struct sss_idmap_range *n_range,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose uint32_t n_first_rid,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose const char *n_range_id,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose bool n_external_mapping)
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose{
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose bool names_equal;
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose bool sids_equal;
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose /* TODO: if both ranges have the same ID check if an update is
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose * needed. */
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose /* Check if ID ranges overlap.
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose * ID ranges with external mapping may overlap. */
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose if ((!n_external_mapping && !o_external_mapping)
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose && ((n_range->min >= o_range->min
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose && n_range->min <= o_range->max)
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose || (n_range->max >= o_range->min
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose && n_range->max <= o_range->max))) {
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose return IDMAP_COLLISION;
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose }
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose names_equal = (strcasecmp(n_name, o_name) == 0);
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose sids_equal = ((n_sid == NULL && o_sid == NULL)
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose || (n_sid != NULL && o_sid != NULL
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose && strcasecmp(n_sid, o_sid) == 0));
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose /* check if domain name and SID are consistent */
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose if ((names_equal && !sids_equal) || (!names_equal && sids_equal)) {
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose return IDMAP_COLLISION;
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose }
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose /* check if external_mapping is consistent */
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose if (names_equal && sids_equal
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose && n_external_mapping != o_external_mapping) {
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose return IDMAP_COLLISION;
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose }
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose /* check if RID ranges overlap */
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose if (names_equal && sids_equal
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose && n_external_mapping == false
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose && n_first_rid >= o_first_rid
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose && n_first_rid <= o_first_rid + (o_range->max - o_range->min)) {
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose return IDMAP_COLLISION;
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose }
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose return IDMAP_SUCCESS;
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose}
c63c8a63ab062a9c4397278a29b12bd32c4f3895Sumit Bose
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Boseenum idmap_error_code sss_idmap_check_collision(struct sss_idmap_ctx *ctx,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose char *n_name, char *n_sid,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose struct sss_idmap_range *n_range,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose uint32_t n_first_rid,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose char *n_range_id,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose bool n_external_mapping)
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose{
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose struct idmap_domain_info *dom;
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose enum idmap_error_code err;
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl struct sss_idmap_range range;
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose for (dom = ctx->idmap_domain_info; dom != NULL; dom = dom->next) {
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl range.min = dom->range_params.min_id;
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl range.max = dom->range_params.max_id;
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl err = sss_idmap_check_collision_ex(dom->name, dom->sid,
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl &range,
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl dom->range_params.first_rid,
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl dom->range_params.range_id,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose dom->external_mapping,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose n_name, n_sid, n_range, n_first_rid,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose n_range_id, n_external_mapping);
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose if (err != IDMAP_SUCCESS) {
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose return err;
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose }
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose }
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose return IDMAP_SUCCESS;
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose}
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichlstatic enum
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichlidmap_error_code dom_check_collision(struct idmap_domain_info *dom_list,
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl struct idmap_domain_info *new_dom)
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose{
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose struct idmap_domain_info *dom;
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose enum idmap_error_code err;
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl struct sss_idmap_range range;
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl struct sss_idmap_range new_dom_range = { new_dom->range_params.min_id,
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl new_dom->range_params.max_id };
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose for (dom = dom_list; dom != NULL; dom = dom->next) {
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl range.min = dom->range_params.min_id;
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl range.max = dom->range_params.max_id;
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl err = sss_idmap_check_collision_ex(dom->name, dom->sid,
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl &range,
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl dom->range_params.first_rid,
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl dom->range_params.range_id,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose dom->external_mapping,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose new_dom->name, new_dom->sid,
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl &new_dom_range,
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl new_dom->range_params.first_rid,
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl new_dom->range_params.range_id,
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose new_dom->external_mapping);
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose if (err != IDMAP_SUCCESS) {
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose return err;
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose }
ff6e24f4474cca6226cd44c47ba2ec6ba6cf9a16Sumit Bose }
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose return IDMAP_SUCCESS;
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose}
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlstatic char*
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlgenerate_sec_slice_name(struct sss_idmap_ctx *ctx,
bda0a8ee67c4222ede597fd57456b74e33116653Pavel Reichl const char *domain_sid, uint32_t rid)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl{
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl const char *SEC_SLICE_NAME_FMT = "%s-%"PRIu32;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl char *slice_name;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl int len, len2;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
bda0a8ee67c4222ede597fd57456b74e33116653Pavel Reichl len = snprintf(NULL, 0, SEC_SLICE_NAME_FMT, domain_sid, rid);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (len <= 0) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return NULL;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl slice_name = ctx->alloc_func(len + 1, ctx->alloc_pvt);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (slice_name == NULL) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return NULL;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
bda0a8ee67c4222ede597fd57456b74e33116653Pavel Reichl len2 = snprintf(slice_name, len + 1, SEC_SLICE_NAME_FMT, domain_sid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl rid);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (len != len2) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ctx->free_func(slice_name, ctx->alloc_pvt);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return NULL;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return slice_name;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl}
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlstatic enum idmap_error_code
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlgenerate_slice(struct sss_idmap_ctx *ctx, char *slice_name, uint32_t first_rid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_range_params **_slice)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl{
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_range_params *slice;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct sss_idmap_range tmp_range;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl enum idmap_error_code err;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl slice = ctx->alloc_func(sizeof(struct idmap_range_params), ctx->alloc_pvt);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (slice == NULL) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_OUT_OF_MEMORY;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl slice->next = NULL;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl err = sss_idmap_calculate_range(ctx, slice_name, NULL, &tmp_range);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (err != IDMAP_SUCCESS) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ctx->free_func(slice, ctx->alloc_pvt);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return err;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl slice->min_id = tmp_range.min;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl slice->max_id = tmp_range.max;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl slice->range_id = slice_name;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl slice->first_rid = first_rid;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl *_slice = slice;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_SUCCESS;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl}
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlstatic enum idmap_error_code
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlget_helpers(struct sss_idmap_ctx *ctx,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl const char *domain_sid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl uint32_t first_rid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_range_params **_sec_slices)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl{
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_range_params *prev = NULL;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_range_params *sec_slices = NULL;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl static enum idmap_error_code err;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_range_params *slice;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl char *secondary_name;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl for (int i = 0; i < ctx->idmap_opts.extra_slice_init; i++) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl secondary_name = generate_sec_slice_name(ctx, domain_sid, first_rid);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (secondary_name == NULL) {
5554a2a679f72f19f266d660a5681e3b0c657379Pavel Reichl err = IDMAP_OUT_OF_MEMORY;
5554a2a679f72f19f266d660a5681e3b0c657379Pavel Reichl goto fail;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl err = generate_slice(ctx, secondary_name, first_rid, &slice);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (err != IDMAP_SUCCESS) {
5554a2a679f72f19f266d660a5681e3b0c657379Pavel Reichl goto fail;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl first_rid += ctx->idmap_opts.rangesize;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (prev != NULL) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl prev->next = slice;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (sec_slices == NULL) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl sec_slices = slice;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl prev = slice;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl *_sec_slices = sec_slices;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_SUCCESS;
5554a2a679f72f19f266d660a5681e3b0c657379Pavel Reichl
5554a2a679f72f19f266d660a5681e3b0c657379Pavel Reichlfail:
5554a2a679f72f19f266d660a5681e3b0c657379Pavel Reichl ctx->free_func(secondary_name, ctx->alloc_pvt);
5554a2a679f72f19f266d660a5681e3b0c657379Pavel Reichl
5554a2a679f72f19f266d660a5681e3b0c657379Pavel Reichl /* Free already generated helpers. */
5554a2a679f72f19f266d660a5681e3b0c657379Pavel Reichl free_helpers(ctx, sec_slices, true);
5554a2a679f72f19f266d660a5681e3b0c657379Pavel Reichl
5554a2a679f72f19f266d660a5681e3b0c657379Pavel Reichl return err;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl}
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Boseenum idmap_error_code sss_idmap_add_domain_ex(struct sss_idmap_ctx *ctx,
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose const char *domain_name,
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose const char *domain_sid,
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose struct sss_idmap_range *range,
9ef0d43b961c05f1aae2ec21eed4142ae3221bc3Sumit Bose const char *range_id,
c63c8a63ab062a9c4397278a29b12bd32c4f3895Sumit Bose uint32_t rid,
c63c8a63ab062a9c4397278a29b12bd32c4f3895Sumit Bose bool external_mapping)
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose{
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose struct idmap_domain_info *dom = NULL;
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose enum idmap_error_code err;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose if (domain_name == NULL) {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return IDMAP_NO_DOMAIN;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose if (range == NULL) {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return IDMAP_NO_RANGE;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose /* For algorithmic mapping a valid domain SID is required, for external
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose * mapping it may be NULL, but if set it should be valid. */
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose if ((!external_mapping && !is_domain_sid(domain_sid))
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose || (external_mapping
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose && domain_sid != NULL
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose && !is_domain_sid(domain_sid))) {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return IDMAP_SID_INVALID;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose dom = ctx->alloc_func(sizeof(struct idmap_domain_info), ctx->alloc_pvt);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose if (dom == NULL) {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return IDMAP_OUT_OF_MEMORY;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose memset(dom, 0, sizeof(struct idmap_domain_info));
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose dom->name = idmap_strdup(ctx, domain_name);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose if (dom->name == NULL) {
222f2484ea37c8bd434184ccd38160fb58c8087fSumit Bose err = IDMAP_OUT_OF_MEMORY;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose goto fail;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose if (domain_sid != NULL) {
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose dom->sid = idmap_strdup(ctx, domain_sid);
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose if (dom->sid == NULL) {
222f2484ea37c8bd434184ccd38160fb58c8087fSumit Bose err = IDMAP_OUT_OF_MEMORY;
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose goto fail;
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl dom->range_params.min_id = range->min;
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl dom->range_params.max_id = range->max;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
9ef0d43b961c05f1aae2ec21eed4142ae3221bc3Sumit Bose if (range_id != NULL) {
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl dom->range_params.range_id = idmap_strdup(ctx, range_id);
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl if (dom->range_params.range_id == NULL) {
222f2484ea37c8bd434184ccd38160fb58c8087fSumit Bose err = IDMAP_OUT_OF_MEMORY;
9ef0d43b961c05f1aae2ec21eed4142ae3221bc3Sumit Bose goto fail;
9ef0d43b961c05f1aae2ec21eed4142ae3221bc3Sumit Bose }
9ef0d43b961c05f1aae2ec21eed4142ae3221bc3Sumit Bose }
9ef0d43b961c05f1aae2ec21eed4142ae3221bc3Sumit Bose
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl dom->range_params.first_rid = rid;
c63c8a63ab062a9c4397278a29b12bd32c4f3895Sumit Bose dom->external_mapping = external_mapping;
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose err = dom_check_collision(ctx->idmap_domain_info, dom);
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose if (err != IDMAP_SUCCESS) {
222f2484ea37c8bd434184ccd38160fb58c8087fSumit Bose goto fail;
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose }
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose dom->next = ctx->idmap_domain_info;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose ctx->idmap_domain_info = dom;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return IDMAP_SUCCESS;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bosefail:
222f2484ea37c8bd434184ccd38160fb58c8087fSumit Bose sss_idmap_free_domain(ctx, dom);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
222f2484ea37c8bd434184ccd38160fb58c8087fSumit Bose return err;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose}
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlenum idmap_error_code
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlsss_idmap_add_auto_domain_ex(struct sss_idmap_ctx *ctx,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl const char *domain_name,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl const char *domain_sid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct sss_idmap_range *range,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl const char *range_id,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl uint32_t rid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl bool external_mapping,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl idmap_store_cb cb,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl void *pvt)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl{
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl enum idmap_error_code err;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl err = sss_idmap_add_domain_ex(ctx, domain_name, domain_sid, range,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl range_id, rid, external_mapping);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (err != IDMAP_SUCCESS) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return err;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (external_mapping) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl /* There's no point in generating secondary ranges if external_mapping
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl is enabled. */
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ctx->idmap_domain_info->auto_add_ranges = false;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_SUCCESS;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if ((range->max - range->min + 1) != ctx->idmap_opts.rangesize) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl /* Range of primary slice is not equal to the value of
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ldap_idmap_range_size option. */
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_ERROR;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl /* No additional secondary ranges should be added if no sec ranges are
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl predeclared. */
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (ctx->idmap_opts.extra_slice_init == 0) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ctx->idmap_domain_info->auto_add_ranges = false;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_SUCCESS;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl /* Add size of primary slice for first_rid of secondary slices. */
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl rid += ctx->idmap_opts.rangesize;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl err = get_helpers(ctx, domain_sid, rid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl &ctx->idmap_domain_info->helpers);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (err == IDMAP_SUCCESS) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ctx->idmap_domain_info->auto_add_ranges = true;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ctx->idmap_domain_info->helpers_owner = true;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl } else {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl /* Running out of slices for secondary mapping is a non-fatal
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl * problem. */
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (err == IDMAP_OUT_OF_SLICES) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl err = IDMAP_SUCCESS;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ctx->idmap_domain_info->auto_add_ranges = false;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ctx->idmap_domain_info->cb = cb;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ctx->idmap_domain_info->pvt = pvt;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return err;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl}
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Boseenum idmap_error_code sss_idmap_add_domain(struct sss_idmap_ctx *ctx,
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose const char *domain_name,
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose const char *domain_sid,
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose struct sss_idmap_range *range)
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose{
9ef0d43b961c05f1aae2ec21eed4142ae3221bc3Sumit Bose return sss_idmap_add_domain_ex(ctx, domain_name, domain_sid, range, NULL,
c63c8a63ab062a9c4397278a29b12bd32c4f3895Sumit Bose 0, false);
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose}
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose
d6f283302268520c1506fb3da4f2a22f5a741be5Michal Zidekstatic bool sss_idmap_sid_is_builtin(const char *sid)
d6f283302268520c1506fb3da4f2a22f5a741be5Michal Zidek{
d6f283302268520c1506fb3da4f2a22f5a741be5Michal Zidek if (strncmp(sid, "S-1-5-32-", 9) == 0) {
d6f283302268520c1506fb3da4f2a22f5a741be5Michal Zidek return true;
d6f283302268520c1506fb3da4f2a22f5a741be5Michal Zidek }
d6f283302268520c1506fb3da4f2a22f5a741be5Michal Zidek
d6f283302268520c1506fb3da4f2a22f5a741be5Michal Zidek return false;
d6f283302268520c1506fb3da4f2a22f5a741be5Michal Zidek}
d6f283302268520c1506fb3da4f2a22f5a741be5Michal Zidek
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlstatic bool parse_rid(const char *sid, size_t dom_prefix_len, long long *_rid)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl{
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl long long rid;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl char *endptr;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl errno = 0;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl /* Use suffix of sid - part after domain and following '-' */
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl rid = strtoull(sid + dom_prefix_len + 1, &endptr, 10);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (errno != 0 || rid > UINT32_MAX || *endptr != '\0') {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return false;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl *_rid = rid;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return true;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl}
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlstatic bool is_sid_from_dom(const char *dom_sid, const char *sid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl size_t *_dom_sid_len)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl{
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl size_t dom_sid_len;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (dom_sid == NULL) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return false;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl dom_sid_len = strlen(dom_sid);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl *_dom_sid_len = dom_sid_len;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (strlen(sid) < dom_sid_len || sid[dom_sid_len] != '-') {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return false;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return strncmp(sid, dom_sid, dom_sid_len) == 0;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl}
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlstatic bool comp_id(struct idmap_range_params *range_params, long long rid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl uint32_t *_id)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl{
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl uint32_t id;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (rid >= range_params->first_rid
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl && ((UINT32_MAX - range_params->min_id) >
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl (rid - range_params->first_rid))) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl id = range_params->min_id + (rid - range_params->first_rid);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (id <= range_params->max_id) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl *_id = id;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return true;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return false;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl}
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlstatic enum idmap_error_code
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlget_range(struct sss_idmap_ctx *ctx,
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl struct idmap_range_params *helpers,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl const char *dom_sid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl long long rid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_range_params **_range)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl{
b9941359b3181c42f415530d5ccad0f4664d85faLukas Slebodnik char *secondary_name = NULL;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl enum idmap_error_code err;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl int first_rid;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_range_params *range;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl struct idmap_range_params *helper;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl first_rid = (rid / ctx->idmap_opts.rangesize) * ctx->idmap_opts.rangesize;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl secondary_name = generate_sec_slice_name(ctx, dom_sid, first_rid);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (secondary_name == NULL) {
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl err = IDMAP_OUT_OF_MEMORY;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl goto error;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl helper = get_helper_by_id(helpers, secondary_name);
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl if (helper != NULL) {
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl /* Utilize helper's range. */
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl err = construct_range(ctx, helper, secondary_name, &range);
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl } else {
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl /* Have to generate a whole new range. */
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl err = generate_slice(ctx, secondary_name, first_rid, &range);
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl }
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl if (err != IDMAP_SUCCESS) {
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl goto error;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl *_range = range;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_SUCCESS;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichlerror:
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl ctx->free_func(secondary_name, ctx->alloc_pvt);
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl return err;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl}
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlstatic enum idmap_error_code
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlspawn_dom(struct sss_idmap_ctx *ctx,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_domain_info *parent,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_range_params *range)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl{
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct sss_idmap_range tmp;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl static enum idmap_error_code err;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_domain_info *it;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl tmp.min = range->min_id;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl tmp.max = range->max_id;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl err = sss_idmap_add_domain_ex(ctx,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl parent->name,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl parent->sid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl &tmp, range->range_id,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl range->first_rid, false);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (err != IDMAP_SUCCESS) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return err;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl it = ctx->idmap_domain_info;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl while (it != NULL) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl /* Find the newly added domain. */
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl if (ranges_eq(&it->range_params, range)) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl /* Share helpers. */
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl it->helpers = parent->helpers;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl it->auto_add_ranges = parent->auto_add_ranges;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl /* Share call back for storing domains */
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl it->cb = parent->cb;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl it->pvt = parent->pvt;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl break;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl it = it->next;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (it == NULL) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl /* Failed to find just added domain. */
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_ERROR;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl /* Store mapping for newly created domain. */
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (it->cb != NULL) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl err = it->cb(it->name,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl it->sid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl it->range_params.range_id,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl it->range_params.min_id,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl it->range_params.max_id,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl it->range_params.first_rid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl it->pvt);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (err != IDMAP_SUCCESS) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return err;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_SUCCESS;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl}
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlstatic enum idmap_error_code
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichladd_dom_for_sid(struct sss_idmap_ctx *ctx,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_domain_info *matched_dom,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl const char *sid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl uint32_t *_id)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl{
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl enum idmap_error_code err;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl long long rid;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_range_params *range = NULL;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (parse_rid(sid, strlen(matched_dom->sid), &rid) == false) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl err = IDMAP_SID_INVALID;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl goto done;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl err = get_range(ctx, matched_dom->helpers, matched_dom->sid, rid, &range);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (err != IDMAP_SUCCESS) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl goto done;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl err = spawn_dom(ctx, matched_dom, range);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (err != IDMAP_SUCCESS) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl goto done;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (!comp_id(range, rid, _id)) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl err = IDMAP_ERROR;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl goto done;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl err = IDMAP_SUCCESS;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichldone:
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (range != NULL) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ctx->free_func(range->range_id, ctx->alloc_pvt);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ctx->free_func(range, ctx->alloc_pvt);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return err;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl}
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Boseenum idmap_error_code sss_idmap_sid_to_unix(struct sss_idmap_ctx *ctx,
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose const char *sid,
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose uint32_t *_id)
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose{
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose struct idmap_domain_info *idmap_domain_info;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl struct idmap_domain_info *matched_dom = NULL;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose size_t dom_len;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose long long rid;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
95a08a0c02281b28bd1914e0727b40ae25b4e16aSumit Bose if (sid == NULL || _id == NULL) {
bd0baea783295bd22aeabdd7f4ce3aa31eb6144dMichal Zidek return IDMAP_ERROR;
bd0baea783295bd22aeabdd7f4ce3aa31eb6144dMichal Zidek }
bd0baea783295bd22aeabdd7f4ce3aa31eb6144dMichal Zidek
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose idmap_domain_info = ctx->idmap_domain_info;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
bd0baea783295bd22aeabdd7f4ce3aa31eb6144dMichal Zidek if (sss_idmap_sid_is_builtin(sid)) {
d6f283302268520c1506fb3da4f2a22f5a741be5Michal Zidek return IDMAP_BUILTIN_SID;
d6f283302268520c1506fb3da4f2a22f5a741be5Michal Zidek }
d6f283302268520c1506fb3da4f2a22f5a741be5Michal Zidek
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl /* Try primary slices */
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose while (idmap_domain_info != NULL) {
c63c8a63ab062a9c4397278a29b12bd32c4f3895Sumit Bose
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (is_sid_from_dom(idmap_domain_info->sid, sid, &dom_len)) {
c63c8a63ab062a9c4397278a29b12bd32c4f3895Sumit Bose
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (idmap_domain_info->external_mapping == true) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_EXTERNAL;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (parse_rid(sid, dom_len, &rid) == false) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_SID_INVALID;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (comp_id(&idmap_domain_info->range_params, rid, _id)) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_SUCCESS;
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl matched_dom = idmap_domain_info;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose idmap_domain_info = idmap_domain_info->next;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (matched_dom != NULL && matched_dom->auto_add_ranges) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return add_dom_for_sid(ctx, matched_dom, sid, _id);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return matched_dom ? IDMAP_NO_RANGE : IDMAP_NO_DOMAIN;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose}
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Boseenum idmap_error_code sss_idmap_check_sid_unix(struct sss_idmap_ctx *ctx,
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose const char *sid,
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose uint32_t id)
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose{
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose struct idmap_domain_info *idmap_domain_info;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose size_t dom_len;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose bool no_range = false;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose if (sid == NULL) {
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose return IDMAP_ERROR;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose }
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose if (ctx->idmap_domain_info == NULL) {
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose return IDMAP_NO_DOMAIN;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose }
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose idmap_domain_info = ctx->idmap_domain_info;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose if (sss_idmap_sid_is_builtin(sid)) {
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose return IDMAP_BUILTIN_SID;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose }
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose while (idmap_domain_info != NULL) {
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose if (idmap_domain_info->sid != NULL) {
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose dom_len = strlen(idmap_domain_info->sid);
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose if (strlen(sid) > dom_len && sid[dom_len] == '-'
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose && strncmp(sid, idmap_domain_info->sid, dom_len) == 0) {
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl if (id >= idmap_domain_info->range_params.min_id
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl && id <= idmap_domain_info->range_params.max_id) {
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose return IDMAP_SUCCESS;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose }
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose no_range = true;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose }
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose }
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose idmap_domain_info = idmap_domain_info->next;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose }
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose return no_range ? IDMAP_NO_RANGE : IDMAP_SID_UNKNOWN;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose}
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlstatic enum idmap_error_code generate_sid(struct sss_idmap_ctx *ctx,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl const char *dom_sid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl uint32_t rid,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl char **_sid)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl{
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl char *sid;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl int len;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl int ret;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl len = snprintf(NULL, 0, SID_FMT, dom_sid, rid);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (len <= 0 || len > SID_STR_MAX_LEN) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_ERROR;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl sid = ctx->alloc_func(len + 1, ctx->alloc_pvt);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (sid == NULL) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_OUT_OF_MEMORY;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ret = snprintf(sid, len + 1, SID_FMT, dom_sid, rid);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (ret != len) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ctx->free_func(sid, ctx->alloc_pvt);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_ERROR;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl *_sid = sid;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_SUCCESS;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl}
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Boseenum idmap_error_code sss_idmap_unix_to_sid(struct sss_idmap_ctx *ctx,
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose uint32_t id,
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose char **_sid)
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose{
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose struct idmap_domain_info *idmap_domain_info;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose uint32_t rid;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl enum idmap_error_code err;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose idmap_domain_info = ctx->idmap_domain_info;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose while (idmap_domain_info != NULL) {
c3cdd6a644a870531092e4378cbcd1a428ff514cPavel Reichl if (id_is_in_range(id, &idmap_domain_info->range_params, &rid)) {
c63c8a63ab062a9c4397278a29b12bd32c4f3895Sumit Bose
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose if (idmap_domain_info->external_mapping == true
47b326af8e662b215139d55e6d52f2b6066fc6dfSumit Bose || idmap_domain_info->sid == NULL) {
c63c8a63ab062a9c4397278a29b12bd32c4f3895Sumit Bose return IDMAP_EXTERNAL;
c63c8a63ab062a9c4397278a29b12bd32c4f3895Sumit Bose }
c63c8a63ab062a9c4397278a29b12bd32c4f3895Sumit Bose
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return generate_sid(ctx, idmap_domain_info->sid, rid, _sid);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl idmap_domain_info = idmap_domain_info->next;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl /* Check secondary ranges. */
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl idmap_domain_info = ctx->idmap_domain_info;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl while (idmap_domain_info != NULL) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl for (struct idmap_range_params *it = idmap_domain_info->helpers;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl it != NULL;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl it = it->next) {
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl if (idmap_domain_info->helpers_owner == false) {
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl /* Checking helpers on owner is sufficient. */
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl continue;
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl }
012d334cec221d8abf86dffbbaf9649ec0a4b585Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (id_is_in_range(id, it, &rid)) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (idmap_domain_info->external_mapping == true
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl || idmap_domain_info->sid == NULL) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_EXTERNAL;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl err = spawn_dom(ctx, idmap_domain_info, it);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl if (err != IDMAP_SUCCESS) {
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return err;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return generate_sid(ctx, idmap_domain_info->sid, rid, _sid);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose idmap_domain_info = idmap_domain_info->next;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose }
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose return IDMAP_NO_DOMAIN;
a6098862048d4bb469130b9ff21be3020d6f2c54Sumit Bose}
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Boseenum idmap_error_code sss_idmap_dom_sid_to_unix(struct sss_idmap_ctx *ctx,
c51a204a40b8f85f7f525edb3e24520916d8b9c7Sumit Bose struct sss_dom_sid *dom_sid,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose uint32_t *id)
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose{
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose enum idmap_error_code err;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose char *sid;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose err = sss_idmap_dom_sid_to_sid(ctx, dom_sid, &sid);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose if (err != IDMAP_SUCCESS) {
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose goto done;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose }
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose err = sss_idmap_sid_to_unix(ctx, sid, id);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bosedone:
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose ctx->free_func(sid, ctx->alloc_pvt);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose return err;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose}
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Boseenum idmap_error_code sss_idmap_bin_sid_to_unix(struct sss_idmap_ctx *ctx,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose uint8_t *bin_sid,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose size_t length,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose uint32_t *id)
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose{
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose enum idmap_error_code err;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose char *sid;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose err = sss_idmap_bin_sid_to_sid(ctx, bin_sid, length, &sid);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose if (err != IDMAP_SUCCESS) {
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose goto done;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose }
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose err = sss_idmap_sid_to_unix(ctx, sid, id);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bosedone:
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose ctx->free_func(sid, ctx->alloc_pvt);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose return err;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose}
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Boseenum idmap_error_code sss_idmap_smb_sid_to_unix(struct sss_idmap_ctx *ctx,
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose struct dom_sid *smb_sid,
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose uint32_t *id)
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose{
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose enum idmap_error_code err;
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose char *sid;
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose err = sss_idmap_smb_sid_to_sid(ctx, smb_sid, &sid);
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose if (err != IDMAP_SUCCESS) {
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose goto done;
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose }
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose err = sss_idmap_sid_to_unix(ctx, sid, id);
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bosedone:
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose ctx->free_func(sid, ctx->alloc_pvt);
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose return err;
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose}
6f504738cad1ee9daa1bd6eec721caceef65f21dSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Boseenum idmap_error_code sss_idmap_check_dom_sid_to_unix(struct sss_idmap_ctx *ctx,
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose struct sss_dom_sid *dom_sid,
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose uint32_t id)
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose{
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose enum idmap_error_code err;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose char *sid;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose err = sss_idmap_dom_sid_to_sid(ctx, dom_sid, &sid);
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose if (err != IDMAP_SUCCESS) {
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose goto done;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose }
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose err = sss_idmap_check_sid_unix(ctx, sid, id);
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bosedone:
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose ctx->free_func(sid, ctx->alloc_pvt);
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose return err;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose}
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Boseenum idmap_error_code sss_idmap_check_bin_sid_unix(struct sss_idmap_ctx *ctx,
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose uint8_t *bin_sid,
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose size_t length,
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose uint32_t id)
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose{
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose enum idmap_error_code err;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose char *sid;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose err = sss_idmap_bin_sid_to_sid(ctx, bin_sid, length, &sid);
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose if (err != IDMAP_SUCCESS) {
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose goto done;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose }
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose err = sss_idmap_check_sid_unix(ctx, sid, id);
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bosedone:
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose ctx->free_func(sid, ctx->alloc_pvt);
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose return err;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose}
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Boseenum idmap_error_code sss_idmap_check_smb_sid_unix(struct sss_idmap_ctx *ctx,
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose struct dom_sid *smb_sid,
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose uint32_t id)
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose{
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose enum idmap_error_code err;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose char *sid;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose err = sss_idmap_smb_sid_to_sid(ctx, smb_sid, &sid);
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose if (err != IDMAP_SUCCESS) {
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose goto done;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose }
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose err = sss_idmap_check_sid_unix(ctx, sid, id);
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bosedone:
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose ctx->free_func(sid, ctx->alloc_pvt);
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose return err;
9869c20a4db6ce7e285a9d7ae7007718a6de207eSumit Bose}
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Boseenum idmap_error_code sss_idmap_unix_to_dom_sid(struct sss_idmap_ctx *ctx,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose uint32_t id,
c51a204a40b8f85f7f525edb3e24520916d8b9c7Sumit Bose struct sss_dom_sid **_dom_sid)
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose{
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose enum idmap_error_code err;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose char *sid = NULL;
c51a204a40b8f85f7f525edb3e24520916d8b9c7Sumit Bose struct sss_dom_sid *dom_sid = NULL;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose err = sss_idmap_unix_to_sid(ctx, id, &sid);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose if (err != IDMAP_SUCCESS) {
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose goto done;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose }
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose err = sss_idmap_sid_to_dom_sid(ctx, sid, &dom_sid);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose if (err != IDMAP_SUCCESS) {
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose goto done;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose }
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose *_dom_sid = dom_sid;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose err = IDMAP_SUCCESS;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bosedone:
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose ctx->free_func(sid, ctx->alloc_pvt);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose if (err != IDMAP_SUCCESS) {
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose ctx->free_func(dom_sid, ctx->alloc_pvt);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose }
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose return err;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose}
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Boseenum idmap_error_code sss_idmap_unix_to_bin_sid(struct sss_idmap_ctx *ctx,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose uint32_t id,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose uint8_t **_bin_sid,
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose size_t *_length)
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose{
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose enum idmap_error_code err;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose char *sid = NULL;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose uint8_t *bin_sid = NULL;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose size_t length;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose err = sss_idmap_unix_to_sid(ctx, id, &sid);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose if (err != IDMAP_SUCCESS) {
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose goto done;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose }
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose err = sss_idmap_sid_to_bin_sid(ctx, sid, &bin_sid, &length);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose if (err != IDMAP_SUCCESS) {
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose goto done;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose }
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose *_bin_sid = bin_sid;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose *_length = length;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose err = IDMAP_SUCCESS;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bosedone:
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose ctx->free_func(sid, ctx->alloc_pvt);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose if (err != IDMAP_SUCCESS) {
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose ctx->free_func(bin_sid, ctx->alloc_pvt);
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose }
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose return err;
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose
b6dfbf81c61d4431aaa81687ec53e892f8b71edbSumit Bose}
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidekenum idmap_error_code
46222e5191473f9a46aec581273eb2eef22e23beMichal Zideksss_idmap_ctx_set_autorid(struct sss_idmap_ctx *ctx, bool use_autorid)
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek{
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek ctx->idmap_opts.autorid_mode = use_autorid;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek return IDMAP_SUCCESS;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek}
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidekenum idmap_error_code
46222e5191473f9a46aec581273eb2eef22e23beMichal Zideksss_idmap_ctx_set_lower(struct sss_idmap_ctx *ctx, id_t lower)
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek{
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek ctx->idmap_opts.idmap_lower = lower;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek return IDMAP_SUCCESS;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek}
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidekenum idmap_error_code
46222e5191473f9a46aec581273eb2eef22e23beMichal Zideksss_idmap_ctx_set_upper(struct sss_idmap_ctx *ctx, id_t upper)
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek{
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek ctx->idmap_opts.idmap_upper = upper;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek return IDMAP_SUCCESS;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek}
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidekenum idmap_error_code
46222e5191473f9a46aec581273eb2eef22e23beMichal Zideksss_idmap_ctx_set_rangesize(struct sss_idmap_ctx *ctx, id_t rangesize)
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek{
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek ctx->idmap_opts.rangesize = rangesize;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek return IDMAP_SUCCESS;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek}
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlenum idmap_error_code
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichlsss_idmap_ctx_set_extra_slice_init(struct sss_idmap_ctx *ctx,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl int extra_slice_init)
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl{
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl ctx->idmap_opts.extra_slice_init = extra_slice_init;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl return IDMAP_SUCCESS;
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl}
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidekenum idmap_error_code
46222e5191473f9a46aec581273eb2eef22e23beMichal Zideksss_idmap_ctx_get_autorid(struct sss_idmap_ctx *ctx, bool *_autorid)
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek{
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek *_autorid = ctx->idmap_opts.autorid_mode;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek return IDMAP_SUCCESS;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek}
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidekenum idmap_error_code
46222e5191473f9a46aec581273eb2eef22e23beMichal Zideksss_idmap_ctx_get_lower(struct sss_idmap_ctx *ctx, id_t *_lower)
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek{
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek *_lower = ctx->idmap_opts.idmap_lower;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek return IDMAP_SUCCESS;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek}
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidekenum idmap_error_code
46222e5191473f9a46aec581273eb2eef22e23beMichal Zideksss_idmap_ctx_get_upper(struct sss_idmap_ctx *ctx, id_t *_upper)
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek{
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek *_upper = ctx->idmap_opts.idmap_upper;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek return IDMAP_SUCCESS;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek}
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidekenum idmap_error_code
46222e5191473f9a46aec581273eb2eef22e23beMichal Zideksss_idmap_ctx_get_rangesize(struct sss_idmap_ctx *ctx, id_t *_rangesize)
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek{
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek *_rangesize = ctx->idmap_opts.rangesize;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek return IDMAP_SUCCESS;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek}
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Boseenum idmap_error_code
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bosesss_idmap_domain_has_algorithmic_mapping(struct sss_idmap_ctx *ctx,
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose const char *dom_sid,
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose bool *has_algorithmic_mapping)
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose{
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose struct idmap_domain_info *idmap_domain_info;
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose size_t len;
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose size_t dom_sid_len;
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose if (dom_sid == NULL) {
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose return IDMAP_SID_INVALID;
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose }
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose if (ctx->idmap_domain_info == NULL) {
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose return IDMAP_NO_DOMAIN;
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose }
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose idmap_domain_info = ctx->idmap_domain_info;
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose while (idmap_domain_info != NULL) {
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose if (idmap_domain_info->sid != NULL) {
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose len = strlen(idmap_domain_info->sid);
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose dom_sid_len = strlen(dom_sid);
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose if (((dom_sid_len > len && dom_sid[len] == '-')
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose || dom_sid_len == len)
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose && strncmp(dom_sid, idmap_domain_info->sid, len) == 0) {
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose *has_algorithmic_mapping = !idmap_domain_info->external_mapping;
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose return IDMAP_SUCCESS;
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose }
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose }
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose idmap_domain_info = idmap_domain_info->next;
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose }
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose return IDMAP_SID_UNKNOWN;
7f02ba09b9481f59c309fd09a88089857e7fe79fSumit Bose}
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Boseenum idmap_error_code
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bosesss_idmap_domain_by_name_has_algorithmic_mapping(struct sss_idmap_ctx *ctx,
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose const char *dom_name,
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose bool *has_algorithmic_mapping)
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose{
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose struct idmap_domain_info *idmap_domain_info;
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose if (dom_name == NULL) {
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose return IDMAP_ERROR;
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose }
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose CHECK_IDMAP_CTX(ctx, IDMAP_CONTEXT_INVALID);
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose if (ctx->idmap_domain_info == NULL) {
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose return IDMAP_NO_DOMAIN;
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose }
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose idmap_domain_info = ctx->idmap_domain_info;
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose while (idmap_domain_info != NULL) {
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose if (idmap_domain_info->name != NULL
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose && strcmp(dom_name, idmap_domain_info->name) == 0) {
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose *has_algorithmic_mapping = !idmap_domain_info->external_mapping;
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose return IDMAP_SUCCESS;
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose }
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose idmap_domain_info = idmap_domain_info->next;
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose }
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose return IDMAP_NAME_UNKNOWN;
3cbbfb4b05d0eb0a0809704e83589d0075e117a0Sumit Bose}