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/>.
21 * @brief Notify smbd about idmap changes
23 * @author Gregor Beck <gb@sernet.de>
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
)
42 if (sscanf(str
, "%cID %lu%c", &c
, &ul
, &trash
) == 2) {
55 } else if (string_to_sid(&sid
, str
)) {
63 static bool delete_uid_cache(uid_t puid
)
65 DATA_BLOB uid
= data_blob_const(&puid
, sizeof(puid
));
68 if (!memcache_lookup(NULL
, UID_SID_CACHE
, uid
, &sid
)) {
69 DEBUG(3, ("UID %d is not memcached!\n", (int)puid
));
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
);
79 static bool delete_gid_cache(gid_t pgid
)
81 DATA_BLOB gid
= data_blob_const(&pgid
, sizeof(pgid
));
83 if (!memcache_lookup(NULL
, GID_SID_CACHE
, gid
, &sid
)) {
84 DEBUG(3, ("GID %d is not memcached!\n", (int)pgid
));
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
);
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));
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
);
109 DEBUG(3, ("SID %s is not memcached!\n", sid_string_dbg(psid
)));
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
)
132 delete_uid_cache(id
->id
.uid
);
133 idmap_cache_del_uid(id
->id
.uid
);
136 delete_gid_cache(id
->id
.gid
);
137 idmap_cache_del_gid(id
->id
.gid
);
140 delete_sid_cache(&id
->id
.sid
);
141 idmap_cache_del_sid(&id
->id
.sid
);
149 static void message_idmap_flush(struct messaging_context
*msg_ctx
,
152 struct server_id server_id
,
155 const char *msg
= data
? (const char *)data
->data
: NULL
;
157 if ((msg
== NULL
) || (msg
[0] == '\0')) {
160 } else if (strncmp(msg
, "GID", 3)) {
162 } else if (strncmp(msg
, "UID", 3)) {
165 DEBUG(0, ("Invalid argument: %s\n", msg
));
169 static void message_idmap_delete(struct messaging_context
*msg_ctx
,
172 struct server_id server_id
,
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
));
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
);