idmap rewrite
[Samba.git] / source / winbindd / idmap_cache.c
blobb818d0dafb9477e64ea7a1301f0b445954c4e6d6
1 /*
2 Unix SMB/CIFS implementation.
3 ID Mapping Cache
5 based on gencache
7 Copyright (C) Simo Sorce 2006
8 Copyright (C) Rafal Szczesniak 2002
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.*/
23 #include "includes.h"
24 #include "winbindd.h"
26 bool idmap_cache_find_sid2uid(const struct dom_sid *sid, uid_t *puid,
27 bool *expired)
29 fstring sidstr;
30 char *key;
31 char *value;
32 char *endptr;
33 time_t timeout;
34 uid_t uid;
35 bool ret;
37 key = talloc_asprintf(talloc_tos(), "IDMAP/SID2UID/%s",
38 sid_to_fstring(sidstr, sid));
39 if (key == NULL) {
40 return false;
42 ret = gencache_get(key, &value, &timeout);
43 TALLOC_FREE(key);
44 if (!ret) {
45 return false;
47 uid = strtol(value, &endptr, 10);
48 ret = (*endptr == '\0');
49 SAFE_FREE(value);
50 if (ret) {
51 *puid = uid;
52 *expired = (timeout <= time(NULL));
54 return ret;
57 bool idmap_cache_find_uid2sid(uid_t uid, struct dom_sid *sid, bool *expired)
59 char *key;
60 char *value;
61 time_t timeout;
62 bool ret = true;
64 key = talloc_asprintf(talloc_tos(), "IDMAP/UID2SID/%d", (int)uid);
65 if (key == NULL) {
66 return false;
68 ret = gencache_get(key, &value, &timeout);
69 TALLOC_FREE(key);
70 if (!ret) {
71 return false;
73 ZERO_STRUCTP(sid);
74 if (value[0] != '-') {
75 ret = string_to_sid(sid, value);
77 SAFE_FREE(value);
78 if (ret) {
79 *expired = (timeout <= time(NULL));
81 return ret;
84 void idmap_cache_set_sid2uid(const struct dom_sid *sid, uid_t uid)
86 time_t now = time(NULL);
87 time_t timeout;
88 fstring sidstr, key, value;
90 if (!is_null_sid(sid)) {
91 fstr_sprintf(key, "IDMAP/SID2UID/%s",
92 sid_to_fstring(sidstr, sid));
93 fstr_sprintf(value, "%d", (int)uid);
94 timeout = (uid == -1)
95 ? lp_idmap_negative_cache_time()
96 : lp_idmap_cache_time();
97 gencache_set(key, value, now + timeout);
99 if (uid != -1) {
100 fstr_sprintf(key, "IDMAP/UID2SID/%d", (int)uid);
101 if (is_null_sid(sid)) {
102 /* negative uid mapping */
103 fstrcpy(value, "-");
104 timeout = lp_idmap_negative_cache_time();
106 else {
107 sid_to_fstring(value, sid);
108 timeout = lp_idmap_cache_time();
110 gencache_set(key, value, now + timeout);
114 bool idmap_cache_find_sid2gid(const struct dom_sid *sid, gid_t *pgid,
115 bool *expired)
117 fstring sidstr;
118 char *key;
119 char *value;
120 char *endptr;
121 time_t timeout;
122 gid_t gid;
123 bool ret;
125 key = talloc_asprintf(talloc_tos(), "IDMAP/SID2GID/%s",
126 sid_to_fstring(sidstr, sid));
127 if (key == NULL) {
128 return false;
130 ret = gencache_get(key, &value, &timeout);
131 TALLOC_FREE(key);
132 if (!ret) {
133 return false;
135 gid = strtol(value, &endptr, 10);
136 ret = (*endptr == '\0');
137 SAFE_FREE(value);
138 if (ret) {
139 *pgid = gid;
140 *expired = (timeout <= time(NULL));
142 return ret;
145 bool idmap_cache_find_gid2sid(gid_t gid, struct dom_sid *sid, bool *expired)
147 char *key;
148 char *value;
149 time_t timeout;
150 bool ret = true;
152 key = talloc_asprintf(talloc_tos(), "IDMAP/GID2SID/%d", (int)gid);
153 if (key == NULL) {
154 return false;
156 ret = gencache_get(key, &value, &timeout);
157 TALLOC_FREE(key);
158 if (!ret) {
159 return false;
161 ZERO_STRUCTP(sid);
162 if (value[0] != '-') {
163 ret = string_to_sid(sid, value);
165 SAFE_FREE(value);
166 if (ret) {
167 *expired = (timeout <= time(NULL));
169 return ret;
172 void idmap_cache_set_sid2gid(const struct dom_sid *sid, gid_t gid)
174 time_t now = time(NULL);
175 time_t timeout;
176 fstring sidstr, key, value;
178 if (!is_null_sid(sid)) {
179 fstr_sprintf(key, "IDMAP/SID2GID/%s",
180 sid_to_fstring(sidstr, sid));
181 fstr_sprintf(value, "%d", (int)gid);
182 timeout = (gid == -1)
183 ? lp_idmap_negative_cache_time()
184 : lp_idmap_cache_time();
185 gencache_set(key, value, now + timeout);
187 if (gid != -1) {
188 fstr_sprintf(key, "IDMAP/GID2SID/%d", (int)gid);
189 if (is_null_sid(sid)) {
190 /* negative gid mapping */
191 fstrcpy(value, "-");
192 timeout = lp_idmap_negative_cache_time();
194 else {
195 sid_to_fstring(value, sid);
196 timeout = lp_idmap_cache_time();
198 gencache_set(key, value, now + timeout);