s3-messages: only include messages.h where needed.
[kai/samba.git] / source3 / smbd / msg_idmap.c
1 /*
2  * Samba Unix/Linux SMB client library
3  *
4  * Copyright (C) Gregor Beck 2011
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /**
21  * @brief  Notify smbd about idmap changes
22  * @file   msg_idmap.c
23  * @author Gregor Beck <gb@sernet.de>
24  * @date   Feb 2011
25  *
26  */
27
28 #include "includes.h"
29 #include "smbd/smbd.h"
30 #include "memcache.h"
31 #include "globals.h"
32 #include "../libcli/security/dom_sid.h"
33 #include "../librpc/gen_ndr/messaging.h"
34 #include "../librpc/gen_ndr/ndr_security.h"
35 #include "idmap_cache.h"
36 #include "passdb/lookup_sid.h"
37 #include "auth.h"
38 #include "messages.h"
39
40 struct id {
41         union {
42                 uid_t uid;
43                 gid_t gid;
44                 struct dom_sid sid;
45         } id;
46         enum {UID, GID, SID} type;
47 };
48
49 static bool parse_id(const char* str, struct id* id)
50 {
51         struct dom_sid sid;
52         unsigned long ul;
53         char c, trash;
54
55         if (sscanf(str, "%cID %lu%c", &c, &ul, &trash) == 2) {
56                 switch(c) {
57                 case 'G':
58                         id->id.gid = ul;
59                         id->type = GID;
60                         return true;
61                 case 'U':
62                         id->id.uid = ul;
63                         id->type = UID;
64                         return true;
65                 default:
66                         break;
67                 }
68         } else if (string_to_sid(&sid, str)) {
69                 id->id.sid = sid;
70                 id->type = SID;
71                 return true;
72         }
73         return false;
74 }
75
76 static bool uid_in_use(const struct user_struct* user, uid_t uid)
77 {
78         while (user) {
79                 if (user->session_info && (user->session_info->utok.uid == uid)) {
80                         return true;
81                 }
82                 user = user->next;
83         }
84         return false;
85 }
86
87 static bool gid_in_use(const struct user_struct* user, gid_t gid)
88 {
89         while (user) {
90                 if (user->session_info != NULL) {
91                         int i;
92                         struct security_unix_token utok = user->session_info->utok;
93                         if (utok.gid == gid) {
94                                 return true;
95                         }
96                         for(i=0; i<utok.ngroups; i++) {
97                                 if (utok.groups[i] == gid) {
98                                         return true;
99                                 }
100                         }
101                 }
102                 user = user->next;
103         }
104         return false;
105 }
106
107 static bool sid_in_use(const struct user_struct* user, const struct dom_sid* psid)
108 {
109         uid_t uid;
110         gid_t gid;
111         if (sid_to_gid(psid, &gid)) {
112                 return gid_in_use(user, gid);
113         } else if (sid_to_uid(psid, &uid)) {
114                 return uid_in_use(user, uid);
115         }
116         return false;
117 }
118
119
120 static bool id_in_use(const struct user_struct* user, const struct id* id)
121 {
122         switch(id->type) {
123         case UID:
124                 return uid_in_use(user, id->id.uid);
125         case GID:
126                 return gid_in_use(user, id->id.gid);
127         case SID:
128                 return sid_in_use(user, &id->id.sid);
129         default:
130                 break;
131         }
132         return false;
133 }
134
135 static void delete_from_cache(const struct id* id)
136 {
137         switch(id->type) {
138         case UID:
139                 delete_uid_cache(id->id.uid);
140                 idmap_cache_del_uid(id->id.uid);
141                 break;
142         case GID:
143                 delete_gid_cache(id->id.gid);
144                 idmap_cache_del_gid(id->id.gid);
145                 break;
146         case SID:
147                 delete_sid_cache(&id->id.sid);
148                 idmap_cache_del_sid(&id->id.sid);
149                 break;
150         default:
151                 break;
152         }
153 }
154
155
156 static void message_idmap_flush(struct messaging_context *msg_ctx,
157                                 void* private_data,
158                                 uint32_t msg_type,
159                                 struct server_id server_id,
160                                 DATA_BLOB* data)
161 {
162         const char* msg = data ? (const char*)data->data : NULL;
163
164         if ((msg == NULL) || (msg[0] == '\0')) {
165                 flush_gid_cache();
166                 flush_uid_cache();
167         } else if (strncmp(msg, "GID", 3)) {
168                 flush_gid_cache();
169         } else if (strncmp(msg, "UID", 3)) {
170                 flush_uid_cache();
171         } else {
172                 DEBUG(0, ("Invalid argument: %s\n", msg));
173         }
174 }
175
176
177 static void message_idmap_delete(struct messaging_context *msg_ctx,
178                                  void* private_data,
179                                  uint32_t msg_type,
180                                  struct server_id server_id,
181                                  DATA_BLOB* data)
182 {
183         const char* msg = (data && data->data) ? (const char*)data->data : "<NULL>";
184         bool do_kill = (msg_type == MSG_IDMAP_KILL);
185         struct user_struct* validated_users = smbd_server_conn->smb1.sessions.validated_users;
186         struct id id;
187
188         if (!parse_id(msg, &id)) {
189                 DEBUG(0, ("Invalid ?ID: %s\n", msg));
190                 return;
191         }
192
193         if( do_kill && id_in_use(validated_users, &id) ) {
194                 exit_server_cleanly(msg);
195         } else {
196                 delete_from_cache(&id);
197         }
198 }
199
200 void msg_idmap_register_msgs(struct messaging_context *ctx)
201 {
202         messaging_register(ctx, NULL, MSG_IDMAP_FLUSH,  message_idmap_flush);
203         messaging_register(ctx, NULL, MSG_IDMAP_DELETE, message_idmap_delete);
204         messaging_register(ctx, NULL, MSG_IDMAP_KILL,   message_idmap_delete);
205 }