r9240: - move struct security_token to the idl file, with this we can
[sfrench/samba-autobuild/.git] / source4 / libcli / security / security_token.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    security descriptror utility functions
5
6    Copyright (C) Andrew Tridgell                2004
7    Copyright (C) Stefan Metzmacher              2005
8       
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "librpc/gen_ndr/ndr_security.h"
26
27 /*
28   return a blank security token
29 */
30 struct security_token *security_token_initialise(TALLOC_CTX *mem_ctx)
31 {
32         struct security_token *st;
33
34         st = talloc(mem_ctx, struct security_token);
35         if (!st) {
36                 return NULL;
37         }
38
39         st->user_sid = NULL;
40         st->group_sid = NULL;
41         st->num_sids = 0;
42         st->sids = NULL;
43         st->privilege_mask = 0;
44
45         return st;
46 }
47
48 /****************************************************************************
49  Create the SID list for this user.
50 ****************************************************************************/
51 NTSTATUS security_token_create(TALLOC_CTX *mem_ctx, 
52                                struct dom_sid *user_sid,
53                                struct dom_sid *group_sid, 
54                                int n_groupSIDs,
55                                struct dom_sid **groupSIDs, 
56                                BOOL is_authenticated,
57                                struct security_token **token)
58 {
59         struct security_token *ptoken;
60         int i;
61         NTSTATUS status;
62
63         ptoken = security_token_initialise(mem_ctx);
64         NT_STATUS_HAVE_NO_MEMORY(ptoken);
65
66         ptoken->sids = talloc_array(ptoken, struct dom_sid *, n_groupSIDs + 5);
67         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
68
69         ptoken->user_sid = talloc_reference(ptoken, user_sid);
70         ptoken->group_sid = talloc_reference(ptoken, group_sid);
71         ptoken->privilege_mask = 0;
72
73         ptoken->sids[0] = ptoken->user_sid;
74         ptoken->sids[1] = ptoken->group_sid;
75
76         /*
77          * Finally add the "standard" SIDs.
78          * The only difference between guest and "anonymous"
79          * is the addition of Authenticated_Users.
80          */
81         ptoken->sids[2] = dom_sid_parse_talloc(mem_ctx, SID_WORLD);
82         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[2]);
83         ptoken->sids[3] = dom_sid_parse_talloc(mem_ctx, SID_NT_NETWORK);
84         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[3]);
85         ptoken->num_sids = 4;
86
87         if (is_authenticated) {
88                 ptoken->sids[4] = dom_sid_parse_talloc(mem_ctx, SID_NT_AUTHENTICATED_USERS);
89                 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[4]);
90                 ptoken->num_sids++;
91         }
92
93         for (i = 0; i < n_groupSIDs; i++) {
94                 size_t check_sid_idx;
95                 for (check_sid_idx = 1; 
96                      check_sid_idx < ptoken->num_sids; 
97                      check_sid_idx++) {
98                         if (dom_sid_equal(ptoken->sids[check_sid_idx], groupSIDs[i])) {
99                                 break;
100                         }
101                 }
102                 
103                 if (check_sid_idx == ptoken->num_sids) {
104                         ptoken->sids[ptoken->num_sids++] = talloc_reference(ptoken, groupSIDs[i]);
105                 }
106         }
107
108         /* setup the privilege mask for this token */
109         status = samdb_privilege_setup(ptoken);
110         if (!NT_STATUS_IS_OK(status)) {
111                 talloc_free(ptoken);
112                 return status;
113         }
114
115         security_token_debug(10, ptoken);
116
117         *token = ptoken;
118
119         return NT_STATUS_OK;
120 }
121
122 /****************************************************************************
123  prints a struct security_token to debug output.
124 ****************************************************************************/
125 void security_token_debug(int dbg_lev, const struct security_token *token)
126 {
127         TALLOC_CTX *mem_ctx;
128         int i;
129         uint_t privilege;
130
131         if (!token) {
132                 DEBUG(dbg_lev, ("Security token: (NULL)\n"));
133                 return;
134         }
135
136         mem_ctx = talloc_init("security_token_debug()");
137         if (!mem_ctx) {
138                 return;
139         }
140
141         DEBUG(dbg_lev, ("Security token of user %s\n",
142                                     dom_sid_string(mem_ctx, token->user_sid) ));
143         DEBUGADD(dbg_lev, (" SIDs (%lu):\n", 
144                                        (unsigned long)token->num_sids));
145         for (i = 0; i < token->num_sids; i++) {
146                 DEBUGADD(dbg_lev, ("  SID[%3lu]: %s\n", (unsigned long)i, 
147                            dom_sid_string(mem_ctx, token->sids[i])));
148         }
149
150         DEBUGADD(dbg_lev, (" Privileges (0x%08X%08X):\n",
151                             (uint32_t)((token->privilege_mask & 0xFFFFFFFF00000000LL) >> 32),
152                             (uint32_t)(token->privilege_mask & 0x00000000FFFFFFFFLL)));
153
154         if (token->privilege_mask) {
155                 i = 0;
156                 for (privilege = 0; privilege < 64; privilege++) {
157                         uint64_t mask = sec_privilege_mask(privilege);
158
159                         if (token->privilege_mask & mask) {
160                                 DEBUGADD(dbg_lev, ("  Privilege[%3lu]: %s\n", (unsigned long)i++, 
161                                         sec_privilege_name(privilege)));
162                         }
163                 }
164         }
165
166         talloc_free(mem_ctx);
167 }