2 Unix SMB/CIFS implementation.
5 Copyright (C) Volker Lendecke 2008
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.*/
23 * Find a sid2uid mapping
24 * @param[in] sid the sid to map
25 * @param[out] puid where to put the result
26 * @param[out] expired is the cache entry expired?
27 * @retval Was anything in the cache at all?
29 * If *puid == -1 this was a negative mapping.
32 bool idmap_cache_find_sid2uid(const struct dom_sid
*sid
, uid_t
*puid
,
43 key
= talloc_asprintf(talloc_tos(), "IDMAP/SID2UID/%s",
44 sid_to_fstring(sidstr
, sid
));
48 ret
= gencache_get(key
, &value
, &timeout
);
53 uid
= strtol(value
, &endptr
, 10);
54 ret
= (*endptr
== '\0');
58 *expired
= (timeout
<= time(NULL
));
64 * Find a uid2sid mapping
65 * @param[in] uid the uid to map
66 * @param[out] sid where to put the result
67 * @param[out] expired is the cache entry expired?
68 * @retval Was anything in the cache at all?
70 * If "is_null_sid(sid)", this was a negative mapping.
73 bool idmap_cache_find_uid2sid(uid_t uid
, struct dom_sid
*sid
, bool *expired
)
80 key
= talloc_asprintf(talloc_tos(), "IDMAP/UID2SID/%d", (int)uid
);
84 ret
= gencache_get(key
, &value
, &timeout
);
90 if (value
[0] != '-') {
91 ret
= string_to_sid(sid
, value
);
95 *expired
= (timeout
<= time(NULL
));
101 * Store a mapping in the idmap cache
102 * @param[in] sid the sid to map
103 * @param[in] uid the uid to map
105 * If both parameters are valid values, then a positive mapping in both
106 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
107 * negative mapping of uid, we want to cache that for this uid we could not
108 * find anything. Likewise if "uid==-1", then we want to cache that we did not
109 * find a mapping for the sid passed here.
112 void idmap_cache_set_sid2uid(const struct dom_sid
*sid
, uid_t uid
)
114 time_t now
= time(NULL
);
116 fstring sidstr
, key
, value
;
118 if (!is_null_sid(sid
)) {
119 fstr_sprintf(key
, "IDMAP/SID2UID/%s",
120 sid_to_fstring(sidstr
, sid
));
121 fstr_sprintf(value
, "%d", (int)uid
);
122 timeout
= (uid
== -1)
123 ? lp_idmap_negative_cache_time()
124 : lp_idmap_cache_time();
125 gencache_set(key
, value
, now
+ timeout
);
128 fstr_sprintf(key
, "IDMAP/UID2SID/%d", (int)uid
);
129 if (is_null_sid(sid
)) {
130 /* negative uid mapping */
132 timeout
= lp_idmap_negative_cache_time();
135 sid_to_fstring(value
, sid
);
136 timeout
= lp_idmap_cache_time();
138 gencache_set(key
, value
, now
+ timeout
);
143 * Find a sid2gid mapping
144 * @param[in] sid the sid to map
145 * @param[out] pgid where to put the result
146 * @param[out] expired is the cache entry expired?
147 * @retval Was anything in the cache at all?
149 * If *pgid == -1 this was a negative mapping.
152 bool idmap_cache_find_sid2gid(const struct dom_sid
*sid
, gid_t
*pgid
,
163 key
= talloc_asprintf(talloc_tos(), "IDMAP/SID2GID/%s",
164 sid_to_fstring(sidstr
, sid
));
168 ret
= gencache_get(key
, &value
, &timeout
);
173 gid
= strtol(value
, &endptr
, 10);
174 ret
= (*endptr
== '\0');
178 *expired
= (timeout
<= time(NULL
));
184 * Find a gid2sid mapping
185 * @param[in] gid the gid to map
186 * @param[out] sid where to put the result
187 * @param[out] expired is the cache entry expired?
188 * @retval Was anything in the cache at all?
190 * If "is_null_sid(sid)", this was a negative mapping.
193 bool idmap_cache_find_gid2sid(gid_t gid
, struct dom_sid
*sid
, bool *expired
)
200 key
= talloc_asprintf(talloc_tos(), "IDMAP/GID2SID/%d", (int)gid
);
204 ret
= gencache_get(key
, &value
, &timeout
);
210 if (value
[0] != '-') {
211 ret
= string_to_sid(sid
, value
);
215 *expired
= (timeout
<= time(NULL
));
221 * Store a mapping in the idmap cache
222 * @param[in] sid the sid to map
223 * @param[in] gid the gid to map
225 * If both parameters are valid values, then a positive mapping in both
226 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
227 * negative mapping of gid, we want to cache that for this gid we could not
228 * find anything. Likewise if "gid==-1", then we want to cache that we did not
229 * find a mapping for the sid passed here.
232 void idmap_cache_set_sid2gid(const struct dom_sid
*sid
, gid_t gid
)
234 time_t now
= time(NULL
);
236 fstring sidstr
, key
, value
;
238 if (!is_null_sid(sid
)) {
239 fstr_sprintf(key
, "IDMAP/SID2GID/%s",
240 sid_to_fstring(sidstr
, sid
));
241 fstr_sprintf(value
, "%d", (int)gid
);
242 timeout
= (gid
== -1)
243 ? lp_idmap_negative_cache_time()
244 : lp_idmap_cache_time();
245 gencache_set(key
, value
, now
+ timeout
);
248 fstr_sprintf(key
, "IDMAP/GID2SID/%d", (int)gid
);
249 if (is_null_sid(sid
)) {
250 /* negative gid mapping */
252 timeout
= lp_idmap_negative_cache_time();
255 sid_to_fstring(value
, sid
);
256 timeout
= lp_idmap_cache_time();
258 gencache_set(key
, value
, now
+ timeout
);