s4-smbtorture: add ndr test for nbt_netlogon_packet to avoid future regressions.
[Samba/gebeck_regimport.git] / source3 / smbd / msg_idmap.c
blob757cac0e3f766e0e08ef6ab06d415720022a0b57
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 #include "includes.h"
21 #include "smbd/globals.h"
22 #include "smbd/smbd.h"
23 #include "../libcli/security/dom_sid.h"
24 #include "../libcli/security/security_token.h"
25 #include "idmap_cache.h"
26 #include "passdb/lookup_sid.h"
27 #include "auth.h"
28 #include "messages.h"
29 #include "lib/id_cache.h"
31 static bool uid_in_use(const struct user_struct *user, uid_t uid)
33 while (user) {
34 if (user->session_info &&
35 (user->session_info->unix_token->uid == uid)) {
36 return true;
38 user = user->next;
40 return false;
43 static bool gid_in_use(const struct user_struct *user, gid_t gid)
45 while (user) {
46 if (user->session_info != NULL) {
47 int i;
48 struct security_unix_token *utok;
50 utok = user->session_info->unix_token;
51 if (utok->gid == gid) {
52 return true;
54 for(i=0; i<utok->ngroups; i++) {
55 if (utok->groups[i] == gid) {
56 return true;
60 user = user->next;
62 return false;
65 static bool sid_in_use(const struct user_struct *user,
66 const struct dom_sid *psid)
68 while (user) {
69 struct security_token *tok;
71 if (user->session_info == NULL) {
72 continue;
74 tok = user->session_info->security_token;
75 if (tok == NULL) {
77 * Not sure session_info->security_token can
78 * ever be NULL. This check might be not
79 * necessary.
81 continue;
83 if (security_token_has_sid(tok, psid)) {
84 return true;
86 user = user->next;
88 return false;
91 static bool id_in_use(const struct user_struct *user,
92 const struct id_cache_ref *id)
94 switch(id->type) {
95 case UID:
96 return uid_in_use(user, id->id.uid);
97 case GID:
98 return gid_in_use(user, id->id.gid);
99 case SID:
100 return sid_in_use(user, &id->id.sid);
101 default:
102 break;
104 return false;
107 static void id_cache_kill(struct messaging_context *msg_ctx,
108 void *private_data,
109 uint32_t msg_type,
110 struct server_id server_id,
111 DATA_BLOB* data)
113 const char *msg = (data && data->data)
114 ? (const char *)data->data : "<NULL>";
115 struct smbd_server_connection *sconn;
116 struct user_struct *validated_users;
117 struct id_cache_ref id;
119 sconn = msg_ctx_to_sconn(msg_ctx);
120 if (sconn == NULL) {
121 DEBUG(1, ("could not find sconn\n"));
122 return;
125 validated_users = sconn->smb1.sessions.validated_users;
127 if (!id_cache_ref_parse(msg, &id)) {
128 DEBUG(0, ("Invalid ?ID: %s\n", msg));
129 return;
132 if (am_parent) {
133 messaging_send_to_children(msg_ctx, msg_type, data);
136 if (id_in_use(validated_users, &id)) {
137 exit_server_cleanly(msg);
139 id_cache_delete_from_cache(&id);
142 static void id_cache_flush(struct messaging_context *ctx,
143 void* data,
144 uint32_t msg_type,
145 struct server_id srv_id,
146 DATA_BLOB* msg_data)
148 id_cache_flush_message(ctx, data, msg_type, srv_id, msg_data);
150 if (am_parent) {
151 messaging_send_to_children(ctx, msg_type, msg_data);
155 static void id_cache_delete(struct messaging_context *ctx,
156 void* data,
157 uint32_t msg_type,
158 struct server_id srv_id,
159 DATA_BLOB* msg_data)
161 id_cache_delete_message(ctx, data, msg_type, srv_id, msg_data);
163 if (am_parent) {
164 messaging_send_to_children(ctx, msg_type, msg_data);
169 void msg_idmap_register_msg(struct messaging_context *ctx)
171 messaging_register(ctx, NULL, ID_CACHE_FLUSH, id_cache_flush);
172 messaging_register(ctx, NULL, ID_CACHE_DELETE, id_cache_delete);
173 messaging_register(ctx, NULL, ID_CACHE_KILL, id_cache_kill);