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
)) {
59 } else if (strncmp(str
, "USER ", 5) == 0) {
60 id
->id
.name
= str
+ 5;
67 static bool delete_uid_cache(uid_t puid
)
69 DATA_BLOB uid
= data_blob_const(&puid
, sizeof(puid
));
72 if (!memcache_lookup(NULL
, UID_SID_CACHE
, uid
, &sid
)) {
73 DEBUG(3, ("UID %d is not memcached!\n", (int)puid
));
76 DEBUG(3, ("Delete mapping UID %d <-> %s from memcache\n", (int)puid
,
77 sid_string_dbg((struct dom_sid
*)sid
.data
)));
78 memcache_delete(NULL
, SID_UID_CACHE
, sid
);
79 memcache_delete(NULL
, UID_SID_CACHE
, uid
);
83 static bool delete_gid_cache(gid_t pgid
)
85 DATA_BLOB gid
= data_blob_const(&pgid
, sizeof(pgid
));
87 if (!memcache_lookup(NULL
, GID_SID_CACHE
, gid
, &sid
)) {
88 DEBUG(3, ("GID %d is not memcached!\n", (int)pgid
));
91 DEBUG(3, ("Delete mapping GID %d <-> %s from memcache\n", (int)pgid
,
92 sid_string_dbg((struct dom_sid
*)sid
.data
)));
93 memcache_delete(NULL
, SID_GID_CACHE
, sid
);
94 memcache_delete(NULL
, GID_SID_CACHE
, gid
);
98 static bool delete_sid_cache(const struct dom_sid
* psid
)
100 DATA_BLOB sid
= data_blob_const(psid
, ndr_size_dom_sid(psid
, 0));
102 if (memcache_lookup(NULL
, SID_GID_CACHE
, sid
, &id
)) {
103 DEBUG(3, ("Delete mapping %s <-> GID %d from memcache\n",
104 sid_string_dbg(psid
), *(int*)id
.data
));
105 memcache_delete(NULL
, SID_GID_CACHE
, sid
);
106 memcache_delete(NULL
, GID_SID_CACHE
, id
);
107 } else if (memcache_lookup(NULL
, SID_UID_CACHE
, sid
, &id
)) {
108 DEBUG(3, ("Delete mapping %s <-> UID %d from memcache\n",
109 sid_string_dbg(psid
), *(int*)id
.data
));
110 memcache_delete(NULL
, SID_UID_CACHE
, sid
);
111 memcache_delete(NULL
, UID_SID_CACHE
, id
);
113 DEBUG(3, ("SID %s is not memcached!\n", sid_string_dbg(psid
)));
119 static bool delete_getpwnam_cache(const char *username
)
121 DATA_BLOB name
= data_blob_string_const_null(username
);
122 DEBUG(6, ("Delete passwd struct for %s from memcache\n",
124 memcache_delete(NULL
, GETPWNAM_CACHE
, name
);
128 static void flush_gid_cache(void)
130 DEBUG(3, ("Flush GID <-> SID memcache\n"));
131 memcache_flush(NULL
, SID_GID_CACHE
);
132 memcache_flush(NULL
, GID_SID_CACHE
);
135 static void flush_uid_cache(void)
137 DEBUG(3, ("Flush UID <-> SID memcache\n"));
138 memcache_flush(NULL
, SID_UID_CACHE
);
139 memcache_flush(NULL
, UID_SID_CACHE
);
141 void id_cache_delete_from_cache(const struct id_cache_ref
* id
)
145 delete_uid_cache(id
->id
.uid
);
146 idmap_cache_del_uid(id
->id
.uid
);
149 delete_gid_cache(id
->id
.gid
);
150 idmap_cache_del_gid(id
->id
.gid
);
153 delete_sid_cache(&id
->id
.sid
);
154 idmap_cache_del_sid(&id
->id
.sid
);
157 delete_getpwnam_cache(id
->id
.name
);
164 void id_cache_flush_message(struct messaging_context
*msg_ctx
,
167 struct server_id server_id
,
170 const char *msg
= data
? (const char *)data
->data
: NULL
;
172 if ((msg
== NULL
) || (msg
[0] == '\0')) {
175 } else if (strncmp(msg
, "GID", 3)) {
177 } else if (strncmp(msg
, "UID", 3)) {
180 DEBUG(0, ("Invalid argument: %s\n", msg
));
184 void id_cache_delete_message(struct messaging_context
*msg_ctx
,
187 struct server_id server_id
,
190 const char *msg
= (data
&& data
->data
) ? (const char *)data
->data
: "<NULL>";
191 struct id_cache_ref id
;
193 if (!id_cache_ref_parse(msg
, &id
)) {
194 DEBUG(0, ("Invalid ?ID: %s\n", msg
));
198 id_cache_delete_from_cache(&id
);
201 void id_cache_register_msgs(struct messaging_context
*ctx
)
203 messaging_register(ctx
, NULL
, ID_CACHE_FLUSH
, id_cache_flush_message
);
204 messaging_register(ctx
, NULL
, ID_CACHE_DELETE
, id_cache_delete_message
);