s3: Reformat msg_idmap.c to match Samba coding
[mat/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 #include "includes.h"
21 #include "smbd/globals.h"
22 #include "smbd/smbd.h"
23 #include "../libcli/security/dom_sid.h"
24 #include "idmap_cache.h"
25 #include "passdb/lookup_sid.h"
26 #include "auth.h"
27 #include "messages.h"
28 #include "lib/id_cache.h"
29
30 static bool uid_in_use(const struct user_struct *user, uid_t uid)
31 {
32         while (user) {
33                 if (user->session_info &&
34                     (user->session_info->unix_token->uid == uid)) {
35                         return true;
36                 }
37                 user = user->next;
38         }
39         return false;
40 }
41
42 static bool gid_in_use(const struct user_struct *user, gid_t gid)
43 {
44         while (user) {
45                 if (user->session_info != NULL) {
46                         int i;
47                         struct security_unix_token *utok;
48
49                         utok = user->session_info->unix_token;
50                         if (utok->gid == gid) {
51                                 return true;
52                         }
53                         for(i=0; i<utok->ngroups; i++) {
54                                 if (utok->groups[i] == gid) {
55                                         return true;
56                                 }
57                         }
58                 }
59                 user = user->next;
60         }
61         return false;
62 }
63
64 static bool sid_in_use(const struct user_struct *user,
65                        const struct dom_sid *psid)
66 {
67         uid_t uid;
68         gid_t gid;
69         if (sid_to_gid(psid, &gid)) {
70                 return gid_in_use(user, gid);
71         } else if (sid_to_uid(psid, &uid)) {
72                 return uid_in_use(user, uid);
73         }
74         return false;
75 }
76
77 static bool id_in_use(const struct user_struct *user,
78                       const struct id_cache_ref *id)
79 {
80         switch(id->type) {
81         case UID:
82                 return uid_in_use(user, id->id.uid);
83         case GID:
84                 return gid_in_use(user, id->id.gid);
85         case SID:
86                 return sid_in_use(user, &id->id.sid);
87         default:
88                 break;
89         }
90         return false;
91 }
92
93 static void id_cache_kill(struct messaging_context *msg_ctx,
94                           void *private_data,
95                           uint32_t msg_type,
96                           struct server_id server_id,
97                           DATA_BLOB* data)
98 {
99         const char *msg = (data && data->data)
100                 ? (const char *)data->data : "<NULL>";
101         struct user_struct *validated_users =
102                 smbd_server_conn->smb1.sessions.validated_users;
103         struct id_cache_ref id;
104
105         if (!id_cache_ref_parse(msg, &id)) {
106                 DEBUG(0, ("Invalid ?ID: %s\n", msg));
107                 return;
108         }
109
110         if (id_in_use(validated_users, &id)) {
111                 exit_server_cleanly(msg);
112         }
113 }
114
115 void id_cache_register_kill_msg(struct messaging_context *ctx)
116 {
117         messaging_register(ctx, NULL, ID_CACHE_KILL, id_cache_kill);
118 }