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/>.*/
24 * Find a sid2uid mapping
25 * @param[in] sid the sid to map
26 * @param[out] puid where to put the result
27 * @param[out] expired is the cache entry expired?
28 * @retval Was anything in the cache at all?
30 * If *puid == -1 this was a negative mapping.
33 bool idmap_cache_find_sid2uid(const struct dom_sid
*sid
, uid_t
*puid
,
44 key
= talloc_asprintf(talloc_tos(), "IDMAP/SID2UID/%s",
45 sid_to_fstring(sidstr
, sid
));
49 ret
= gencache_get(key
, &value
, &timeout
);
54 uid
= strtol(value
, &endptr
, 10);
55 ret
= (*endptr
== '\0');
59 *expired
= (timeout
<= time(NULL
));
65 * Find a uid2sid mapping
66 * @param[in] uid the uid to map
67 * @param[out] sid where to put the result
68 * @param[out] expired is the cache entry expired?
69 * @retval Was anything in the cache at all?
71 * If "is_null_sid(sid)", this was a negative mapping.
74 bool idmap_cache_find_uid2sid(uid_t uid
, struct dom_sid
*sid
, bool *expired
)
81 key
= talloc_asprintf(talloc_tos(), "IDMAP/UID2SID/%d", (int)uid
);
85 ret
= gencache_get(key
, &value
, &timeout
);
91 if (value
[0] != '-') {
92 ret
= string_to_sid(sid
, value
);
96 *expired
= (timeout
<= time(NULL
));
102 * Store a mapping in the idmap cache
103 * @param[in] sid the sid to map
104 * @param[in] uid the uid to map
106 * If both parameters are valid values, then a positive mapping in both
107 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
108 * negative mapping of uid, we want to cache that for this uid we could not
109 * find anything. Likewise if "uid==-1", then we want to cache that we did not
110 * find a mapping for the sid passed here.
113 void idmap_cache_set_sid2uid(const struct dom_sid
*sid
, uid_t uid
)
115 time_t now
= time(NULL
);
117 fstring sidstr
, key
, value
;
119 if (!is_null_sid(sid
)) {
120 fstr_sprintf(key
, "IDMAP/SID2UID/%s",
121 sid_to_fstring(sidstr
, sid
));
122 fstr_sprintf(value
, "%d", (int)uid
);
123 timeout
= (uid
== -1)
124 ? lp_idmap_negative_cache_time()
125 : lp_idmap_cache_time();
126 gencache_set(key
, value
, now
+ timeout
);
129 fstr_sprintf(key
, "IDMAP/UID2SID/%d", (int)uid
);
130 if (is_null_sid(sid
)) {
131 /* negative uid mapping */
133 timeout
= lp_idmap_negative_cache_time();
136 sid_to_fstring(value
, sid
);
137 timeout
= lp_idmap_cache_time();
139 gencache_set(key
, value
, now
+ timeout
);
144 * Find a sid2gid mapping
145 * @param[in] sid the sid to map
146 * @param[out] pgid where to put the result
147 * @param[out] expired is the cache entry expired?
148 * @retval Was anything in the cache at all?
150 * If *pgid == -1 this was a negative mapping.
153 bool idmap_cache_find_sid2gid(const struct dom_sid
*sid
, gid_t
*pgid
,
164 key
= talloc_asprintf(talloc_tos(), "IDMAP/SID2GID/%s",
165 sid_to_fstring(sidstr
, sid
));
169 ret
= gencache_get(key
, &value
, &timeout
);
174 gid
= strtol(value
, &endptr
, 10);
175 ret
= (*endptr
== '\0');
179 *expired
= (timeout
<= time(NULL
));
185 * Find a gid2sid mapping
186 * @param[in] gid the gid to map
187 * @param[out] sid where to put the result
188 * @param[out] expired is the cache entry expired?
189 * @retval Was anything in the cache at all?
191 * If "is_null_sid(sid)", this was a negative mapping.
194 bool idmap_cache_find_gid2sid(gid_t gid
, struct dom_sid
*sid
, bool *expired
)
201 key
= talloc_asprintf(talloc_tos(), "IDMAP/GID2SID/%d", (int)gid
);
205 ret
= gencache_get(key
, &value
, &timeout
);
211 if (value
[0] != '-') {
212 ret
= string_to_sid(sid
, value
);
216 *expired
= (timeout
<= time(NULL
));
222 * Store a mapping in the idmap cache
223 * @param[in] sid the sid to map
224 * @param[in] gid the gid to map
226 * If both parameters are valid values, then a positive mapping in both
227 * directions is stored. If "is_null_sid(sid)" is true, then this will be a
228 * negative mapping of gid, we want to cache that for this gid we could not
229 * find anything. Likewise if "gid==-1", then we want to cache that we did not
230 * find a mapping for the sid passed here.
233 void idmap_cache_set_sid2gid(const struct dom_sid
*sid
, gid_t gid
)
235 time_t now
= time(NULL
);
237 fstring sidstr
, key
, value
;
239 if (!is_null_sid(sid
)) {
240 fstr_sprintf(key
, "IDMAP/SID2GID/%s",
241 sid_to_fstring(sidstr
, sid
));
242 fstr_sprintf(value
, "%d", (int)gid
);
243 timeout
= (gid
== -1)
244 ? lp_idmap_negative_cache_time()
245 : lp_idmap_cache_time();
246 gencache_set(key
, value
, now
+ timeout
);
249 fstr_sprintf(key
, "IDMAP/GID2SID/%d", (int)gid
);
250 if (is_null_sid(sid
)) {
251 /* negative gid mapping */
253 timeout
= lp_idmap_negative_cache_time();
256 sid_to_fstring(value
, sid
);
257 timeout
= lp_idmap_cache_time();
259 gencache_set(key
, value
, now
+ timeout
);