libgpo: apply some const.
[Samba.git] / source3 / winbindd / idmap_util.c
blob196b4adf24756351d5b8dd7911a184cc6d0fa289
1 /*
2 Unix SMB/CIFS implementation.
3 ID Mapping
4 Copyright (C) Simo Sorce 2003
5 Copyright (C) Jeremy Allison 2006
6 Copyright (C) Michael Adam 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.*/
21 #include "includes.h"
22 #include "winbindd.h"
23 #include "winbindd_proto.h"
24 #include "idmap.h"
25 #include "idmap_cache.h"
26 #include "../libcli/security/security.h"
27 #include "secrets.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_IDMAP
32 /**
33 * check whether a given unix id is inside the filter range of an idmap domain
35 bool idmap_unix_id_is_in_range(uint32_t id, struct idmap_domain *dom)
37 if ((dom->low_id && (id < dom->low_id)) ||
38 (dom->high_id && (id > dom->high_id)))
40 return false;
43 return true;
46 /**
47 * Helper for unixids_to_sids: find entry by id in mapping array,
48 * search up to IDMAP_AD_MAX_IDS entries
50 struct id_map *idmap_find_map_by_id(struct id_map **maps, enum id_type type,
51 uint32_t id)
53 int i;
55 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
56 if (maps[i] == NULL) { /* end of the run */
57 return NULL;
59 if ((maps[i]->xid.type == type) && (maps[i]->xid.id == id)) {
60 return maps[i];
64 return NULL;
67 /**
68 * Helper for sids_to_unix_ids: find entry by SID in mapping array,
69 * search up to IDMAP_AD_MAX_IDS entries
71 struct id_map *idmap_find_map_by_sid(struct id_map **maps, struct dom_sid *sid)
73 int i;
75 for (i = 0; i < IDMAP_LDAP_MAX_IDS; i++) {
76 if (maps[i] == NULL) { /* end of the run */
77 return NULL;
79 if (dom_sid_equal(maps[i]->sid, sid)) {
80 return maps[i];
84 return NULL;
87 char *idmap_fetch_secret(const char *backend, const char *domain,
88 const char *identity)
90 char *tmp, *ret;
91 int r;
93 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
95 if (r < 0)
96 return NULL;
98 /* make sure the key is case insensitive */
99 if (!strupper_m(tmp)) {
100 SAFE_FREE(tmp);
101 return NULL;
104 ret = secrets_fetch_generic(tmp, identity);
106 SAFE_FREE(tmp);
108 return ret;
111 struct id_map **id_map_ptrs_init(TALLOC_CTX *mem_ctx, size_t num_ids)
113 struct id_map **ptrs;
114 struct id_map *maps;
115 struct dom_sid *sids;
116 size_t i;
118 ptrs = talloc_array(mem_ctx, struct id_map *, num_ids+1);
119 if (ptrs == NULL) {
120 return NULL;
122 maps = talloc_array(ptrs, struct id_map, num_ids);
123 if (maps == NULL) {
124 TALLOC_FREE(ptrs);
125 return NULL;
127 sids = talloc_zero_array(ptrs, struct dom_sid, num_ids);
128 if (sids == NULL) {
129 TALLOC_FREE(ptrs);
130 return NULL;
133 for (i=0; i<num_ids; i++) {
134 maps[i] = (struct id_map) { .sid = &sids[i] };
135 ptrs[i] = &maps[i];
137 ptrs[num_ids] = NULL;
139 return ptrs;