1e823144409897c1c147b7efb9eae2116c78c43c
[metze/samba/wip.git] / source3 / winbindd / idmap_cache.c
1 /* 
2    Unix SMB/CIFS implementation.
3    ID Mapping Cache
4
5    based on gencache
6
7    Copyright (C) Simo Sorce             2006
8    Copyright (C) Rafal Szczesniak       2002
9
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.
14
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.
19
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/>.*/
22
23 #include "includes.h"
24 #include "winbindd.h"
25
26 bool idmap_cache_find_sid2uid(const struct dom_sid *sid, uid_t *puid,
27                               bool *expired)
28 {
29         fstring sidstr;
30         char *key;
31         char *value;
32         char *endptr;
33         time_t timeout;
34         uid_t uid;
35         bool ret;
36
37         key = talloc_asprintf(talloc_tos(), "IDMAP/SID2UID/%s",
38                               sid_to_fstring(sidstr, sid));
39         if (key == NULL) {
40                 return false;
41         }
42         ret = gencache_get(key, &value, &timeout);
43         TALLOC_FREE(key);
44         if (!ret) {
45                 return false;
46         }
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));
53         }
54         return ret;
55 }
56
57 bool idmap_cache_find_uid2sid(uid_t uid, struct dom_sid *sid, bool *expired)
58 {
59         char *key;
60         char *value;
61         time_t timeout;
62         bool ret;
63
64         key = talloc_asprintf(talloc_tos(), "IDMAP/UID2SID/%d", (int)uid);
65         if (key == NULL) {
66                 return false;
67         }
68         ret = gencache_get(key, &value, &timeout);
69         TALLOC_FREE(key);
70         if (!ret) {
71                 return false;
72         }
73         ZERO_STRUCTP(sid);
74         ret = string_to_sid(sid, value);
75         SAFE_FREE(value);
76         if (ret) {
77                 *expired = (timeout <= time(NULL));
78         }
79         return ret;
80 }
81
82 void idmap_cache_set_sid2uid(const struct dom_sid *sid, uid_t uid)
83 {
84         time_t now = time(NULL);
85         time_t timeout;
86         fstring sidstr, key, value;
87
88         if (!is_null_sid(sid)) {
89                 fstr_sprintf(key, "IDMAP/SID2UID/%s",
90                              sid_to_fstring(sidstr, sid));
91                 fstr_sprintf(value, "%d", (int)uid);
92                 timeout = (uid == -1)
93                         ? lp_idmap_negative_cache_time()
94                         : lp_idmap_cache_time();
95                 gencache_set(key, value, now + timeout);
96         }
97         if (uid != -1) {
98                 fstr_sprintf(key, "IDMAP/UID2SID/%d", (int)uid);
99                 sid_to_fstring(value, sid);
100                 timeout = is_null_sid(sid)
101                         ? lp_idmap_negative_cache_time()
102                         : lp_idmap_cache_time();
103                 gencache_set(key, value, now + timeout);
104         }
105 }
106
107 bool idmap_cache_find_sid2gid(const struct dom_sid *sid, gid_t *pgid,
108                               bool *expired)
109 {
110         fstring sidstr;
111         char *key;
112         char *value;
113         char *endptr;
114         time_t timeout;
115         gid_t gid;
116         bool ret;
117
118         key = talloc_asprintf(talloc_tos(), "IDMAP/SID2GID/%s",
119                               sid_to_fstring(sidstr, sid));
120         if (key == NULL) {
121                 return false;
122         }
123         ret = gencache_get(key, &value, &timeout);
124         TALLOC_FREE(key);
125         if (!ret) {
126                 return false;
127         }
128         gid = strtol(value, &endptr, 10);
129         ret = (*endptr == '\0');
130         SAFE_FREE(value);
131         if (ret) {
132                 *pgid = gid;
133                 *expired = (timeout <= time(NULL));
134         }
135         return ret;
136 }
137
138 bool idmap_cache_find_gid2sid(gid_t gid, struct dom_sid *sid, bool *expired)
139 {
140         char *key;
141         char *value;
142         time_t timeout;
143         bool ret;
144
145         key = talloc_asprintf(talloc_tos(), "IDMAP/GID2SID/%d", (int)gid);
146         if (key == NULL) {
147                 return false;
148         }
149         ret = gencache_get(key, &value, &timeout);
150         TALLOC_FREE(key);
151         if (!ret) {
152                 return false;
153         }
154         ZERO_STRUCTP(sid);
155         ret = string_to_sid(sid, value);
156         SAFE_FREE(value);
157         if (ret) {
158                 *expired = (timeout <= time(NULL));
159         }
160         return ret;
161 }
162
163 void idmap_cache_set_sid2gid(const struct dom_sid *sid, gid_t gid)
164 {
165         time_t now = time(NULL);
166         time_t timeout;
167         fstring sidstr, key, value;
168
169         if (!is_null_sid(sid)) {
170                 fstr_sprintf(key, "IDMAP/SID2GID/%s",
171                              sid_to_fstring(sidstr, sid));
172                 fstr_sprintf(value, "%d", (int)gid);
173                 timeout = (gid == -1)
174                         ? lp_idmap_negative_cache_time()
175                         : lp_idmap_cache_time();
176                 gencache_set(key, value, now + timeout);
177         }
178         if (gid != -1) {
179                 fstr_sprintf(key, "IDMAP/GID2SID/%d", (int)gid);
180                 sid_to_fstring(value, sid);
181                 timeout = is_null_sid(sid)
182                         ? lp_idmap_negative_cache_time()
183                         : lp_idmap_cache_time();
184                 gencache_set(key, value, now + timeout);
185         }
186 }