s3-id_cache: Move id caches mgmt out of smbd
[Samba/bjacke.git] / source3 / lib / id_cache.c
blob50649dd4095f98abb694b2f4d40cabd887a8341f
1 /*
2 * Samba Unix/Linux SMB client library
4 * Copyright (C) Gregor Beck 2011
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 /**
21 * @brief Notify smbd about idmap changes
22 * @file msg_idmap.c
23 * @author Gregor Beck <gb@sernet.de>
24 * @date Feb 2011
28 #include "includes.h"
29 #include "messages.h"
30 #include "lib/id_cache.h"
31 #include "include/memcache.h"
32 #include "idmap_cache.h"
33 #include "../librpc/gen_ndr/ndr_security.h"
34 #include "../libcli/security/dom_sid.h"
36 bool id_cache_ref_parse(const char* str, struct id_cache_ref* id)
38 struct dom_sid sid;
39 unsigned long ul;
40 char c, trash;
42 if (sscanf(str, "%cID %lu%c", &c, &ul, &trash) == 2) {
43 switch(c) {
44 case 'G':
45 id->id.gid = ul;
46 id->type = GID;
47 return true;
48 case 'U':
49 id->id.uid = ul;
50 id->type = UID;
51 return true;
52 default:
53 break;
55 } else if (string_to_sid(&sid, str)) {
56 id->id.sid = sid;
57 id->type = SID;
58 return true;
60 return false;
63 static bool delete_uid_cache(uid_t puid)
65 DATA_BLOB uid = data_blob_const(&puid, sizeof(puid));
66 DATA_BLOB sid;
68 if (!memcache_lookup(NULL, UID_SID_CACHE, uid, &sid)) {
69 DEBUG(3, ("UID %d is not memcached!\n", (int)puid));
70 return false;
72 DEBUG(3, ("Delete mapping UID %d <-> %s from memcache\n", (int)puid,
73 sid_string_dbg((struct dom_sid*)sid.data)));
74 memcache_delete(NULL, SID_UID_CACHE, sid);
75 memcache_delete(NULL, UID_SID_CACHE, uid);
76 return true;
79 static bool delete_gid_cache(gid_t pgid)
81 DATA_BLOB gid = data_blob_const(&pgid, sizeof(pgid));
82 DATA_BLOB sid;
83 if (!memcache_lookup(NULL, GID_SID_CACHE, gid, &sid)) {
84 DEBUG(3, ("GID %d is not memcached!\n", (int)pgid));
85 return false;
87 DEBUG(3, ("Delete mapping GID %d <-> %s from memcache\n", (int)pgid,
88 sid_string_dbg((struct dom_sid*)sid.data)));
89 memcache_delete(NULL, SID_GID_CACHE, sid);
90 memcache_delete(NULL, GID_SID_CACHE, gid);
91 return true;
94 static bool delete_sid_cache(const struct dom_sid* psid)
96 DATA_BLOB sid = data_blob_const(psid, ndr_size_dom_sid(psid, 0));
97 DATA_BLOB id;
98 if (memcache_lookup(NULL, SID_GID_CACHE, sid, &id)) {
99 DEBUG(3, ("Delete mapping %s <-> GID %d from memcache\n",
100 sid_string_dbg(psid), *(int*)id.data));
101 memcache_delete(NULL, SID_GID_CACHE, sid);
102 memcache_delete(NULL, GID_SID_CACHE, id);
103 } else if (memcache_lookup(NULL, SID_UID_CACHE, sid, &id)) {
104 DEBUG(3, ("Delete mapping %s <-> UID %d from memcache\n",
105 sid_string_dbg(psid), *(int*)id.data));
106 memcache_delete(NULL, SID_UID_CACHE, sid);
107 memcache_delete(NULL, UID_SID_CACHE, id);
108 } else {
109 DEBUG(3, ("SID %s is not memcached!\n", sid_string_dbg(psid)));
110 return false;
112 return true;
115 static void flush_gid_cache(void)
117 DEBUG(3, ("Flush GID <-> SID memcache\n"));
118 memcache_flush(NULL, SID_GID_CACHE);
119 memcache_flush(NULL, GID_SID_CACHE);
122 static void flush_uid_cache(void)
124 DEBUG(3, ("Flush UID <-> SID memcache\n"));
125 memcache_flush(NULL, SID_UID_CACHE);
126 memcache_flush(NULL, UID_SID_CACHE);
128 static void delete_from_cache(const struct id_cache_ref* id)
130 switch(id->type) {
131 case UID:
132 delete_uid_cache(id->id.uid);
133 idmap_cache_del_uid(id->id.uid);
134 break;
135 case GID:
136 delete_gid_cache(id->id.gid);
137 idmap_cache_del_gid(id->id.gid);
138 break;
139 case SID:
140 delete_sid_cache(&id->id.sid);
141 idmap_cache_del_sid(&id->id.sid);
142 break;
143 default:
144 break;
149 static void message_idmap_flush(struct messaging_context *msg_ctx,
150 void* private_data,
151 uint32_t msg_type,
152 struct server_id server_id,
153 DATA_BLOB* data)
155 const char *msg = data ? (const char *)data->data : NULL;
157 if ((msg == NULL) || (msg[0] == '\0')) {
158 flush_gid_cache();
159 flush_uid_cache();
160 } else if (strncmp(msg, "GID", 3)) {
161 flush_gid_cache();
162 } else if (strncmp(msg, "UID", 3)) {
163 flush_uid_cache();
164 } else {
165 DEBUG(0, ("Invalid argument: %s\n", msg));
169 static void message_idmap_delete(struct messaging_context *msg_ctx,
170 void *private_data,
171 uint32_t msg_type,
172 struct server_id server_id,
173 DATA_BLOB* data)
175 const char *msg = (data && data->data) ? (const char *)data->data : "<NULL>";
176 struct id_cache_ref id;
178 if (!id_cache_ref_parse(msg, &id)) {
179 DEBUG(0, ("Invalid ?ID: %s\n", msg));
180 return;
183 delete_from_cache(&id);
186 void msg_idmap_register_msgs(struct messaging_context *ctx)
188 messaging_register(ctx, NULL, MSG_IDMAP_FLUSH, message_idmap_flush);
189 messaging_register(ctx, NULL, MSG_IDMAP_DELETE, message_idmap_delete);