First part of fix for bug #8673 - NT ACL issue.
[Samba.git] / source3 / lib / id_cache.c
blob23dcce1eb49c31fdbce9346a815f845ef99f6d02
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;
59 } else if (strncmp(str, "USER ", 5) == 0) {
60 id->id.name = str + 5;
61 id->type = USERNAME;
62 return true;
64 return false;
67 static bool delete_uid_cache(uid_t puid)
69 DATA_BLOB uid = data_blob_const(&puid, sizeof(puid));
70 DATA_BLOB sid;
72 if (!memcache_lookup(NULL, UID_SID_CACHE, uid, &sid)) {
73 DEBUG(3, ("UID %d is not memcached!\n", (int)puid));
74 return false;
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);
80 return true;
83 static bool delete_gid_cache(gid_t pgid)
85 DATA_BLOB gid = data_blob_const(&pgid, sizeof(pgid));
86 DATA_BLOB sid;
87 if (!memcache_lookup(NULL, GID_SID_CACHE, gid, &sid)) {
88 DEBUG(3, ("GID %d is not memcached!\n", (int)pgid));
89 return false;
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);
95 return true;
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));
101 DATA_BLOB id;
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);
112 } else {
113 DEBUG(3, ("SID %s is not memcached!\n", sid_string_dbg(psid)));
114 return false;
116 return true;
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",
123 username));
124 memcache_delete(NULL, GETPWNAM_CACHE, name);
125 return true;
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)
143 switch(id->type) {
144 case UID:
145 delete_uid_cache(id->id.uid);
146 idmap_cache_del_uid(id->id.uid);
147 break;
148 case GID:
149 delete_gid_cache(id->id.gid);
150 idmap_cache_del_gid(id->id.gid);
151 break;
152 case SID:
153 delete_sid_cache(&id->id.sid);
154 idmap_cache_del_sid(&id->id.sid);
155 break;
156 case USERNAME:
157 delete_getpwnam_cache(id->id.name);
158 default:
159 break;
164 void id_cache_flush_message(struct messaging_context *msg_ctx,
165 void* private_data,
166 uint32_t msg_type,
167 struct server_id server_id,
168 DATA_BLOB* data)
170 const char *msg = data ? (const char *)data->data : NULL;
172 if ((msg == NULL) || (msg[0] == '\0')) {
173 flush_gid_cache();
174 flush_uid_cache();
175 } else if (strncmp(msg, "GID", 3)) {
176 flush_gid_cache();
177 } else if (strncmp(msg, "UID", 3)) {
178 flush_uid_cache();
179 } else {
180 DEBUG(0, ("Invalid argument: %s\n", msg));
184 void id_cache_delete_message(struct messaging_context *msg_ctx,
185 void *private_data,
186 uint32_t msg_type,
187 struct server_id server_id,
188 DATA_BLOB* data)
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));
195 return;
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);