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