r12542: Move some more prototypes out to seperate headers
[abartlet/samba.git/.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 #include "dsdb/samdb/samdb.h"
27
28 /*
29   return a blank security token
30 */
31 struct security_token *security_token_initialise(TALLOC_CTX *mem_ctx)
32 {
33         struct security_token *st;
34
35         st = talloc(mem_ctx, struct security_token);
36         if (!st) {
37                 return NULL;
38         }
39
40         st->user_sid = NULL;
41         st->group_sid = NULL;
42         st->num_sids = 0;
43         st->sids = NULL;
44         st->privilege_mask = 0;
45
46         return st;
47 }
48
49 /****************************************************************************
50  Create the SID list for this user.
51 ****************************************************************************/
52 NTSTATUS security_token_create(TALLOC_CTX *mem_ctx, 
53                                struct dom_sid *user_sid,
54                                struct dom_sid *group_sid, 
55                                int n_groupSIDs,
56                                struct dom_sid **groupSIDs, 
57                                BOOL is_authenticated,
58                                struct security_token **token)
59 {
60         struct security_token *ptoken;
61         int i;
62         NTSTATUS status;
63
64         ptoken = security_token_initialise(mem_ctx);
65         NT_STATUS_HAVE_NO_MEMORY(ptoken);
66
67         ptoken->sids = talloc_array(ptoken, struct dom_sid *, n_groupSIDs + 5);
68         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
69
70         ptoken->user_sid = talloc_reference(ptoken, user_sid);
71         ptoken->group_sid = talloc_reference(ptoken, group_sid);
72         ptoken->privilege_mask = 0;
73
74         ptoken->sids[0] = ptoken->user_sid;
75         ptoken->sids[1] = ptoken->group_sid;
76
77         /*
78          * Finally add the "standard" SIDs.
79          * The only difference between guest and "anonymous"
80          * is the addition of Authenticated_Users.
81          */
82         ptoken->sids[2] = dom_sid_parse_talloc(ptoken->sids, SID_WORLD);
83         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[2]);
84         ptoken->sids[3] = dom_sid_parse_talloc(ptoken->sids, SID_NT_NETWORK);
85         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[3]);
86         ptoken->num_sids = 4;
87
88         if (is_authenticated) {
89                 ptoken->sids[4] = dom_sid_parse_talloc(ptoken->sids, SID_NT_AUTHENTICATED_USERS);
90                 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[4]);
91                 ptoken->num_sids++;
92         }
93
94         for (i = 0; i < n_groupSIDs; i++) {
95                 size_t check_sid_idx;
96                 for (check_sid_idx = 1; 
97                      check_sid_idx < ptoken->num_sids; 
98                      check_sid_idx++) {
99                         if (dom_sid_equal(ptoken->sids[check_sid_idx], groupSIDs[i])) {
100                                 break;
101                         }
102                 }
103
104                 if (check_sid_idx == ptoken->num_sids) {
105                         ptoken->sids[ptoken->num_sids++] = talloc_reference(ptoken->sids, groupSIDs[i]);
106                 }
107         }
108
109         /* setup the privilege mask for this token */
110         status = samdb_privilege_setup(ptoken);
111         if (!NT_STATUS_IS_OK(status)) {
112                 talloc_free(ptoken);
113                 return status;
114         }
115
116         security_token_debug(10, ptoken);
117
118         *token = ptoken;
119
120         return NT_STATUS_OK;
121 }
122
123 /****************************************************************************
124  prints a struct security_token to debug output.
125 ****************************************************************************/
126 void security_token_debug(int dbg_lev, const struct security_token *token)
127 {
128         TALLOC_CTX *mem_ctx;
129         int i;
130         uint_t privilege;
131
132         if (!token) {
133                 DEBUG(dbg_lev, ("Security token: (NULL)\n"));
134                 return;
135         }
136
137         mem_ctx = talloc_init("security_token_debug()");
138         if (!mem_ctx) {
139                 return;
140         }
141
142         DEBUG(dbg_lev, ("Security token of user %s\n",
143                                     dom_sid_string(mem_ctx, token->user_sid) ));
144         DEBUGADD(dbg_lev, (" SIDs (%lu):\n", 
145                                        (unsigned long)token->num_sids));
146         for (i = 0; i < token->num_sids; i++) {
147                 DEBUGADD(dbg_lev, ("  SID[%3lu]: %s\n", (unsigned long)i, 
148                            dom_sid_string(mem_ctx, token->sids[i])));
149         }
150
151         DEBUGADD(dbg_lev, (" Privileges (0x%08X%08X):\n",
152                             (uint32_t)((token->privilege_mask & 0xFFFFFFFF00000000LL) >> 32),
153                             (uint32_t)(token->privilege_mask & 0x00000000FFFFFFFFLL)));
154
155         if (token->privilege_mask) {
156                 i = 0;
157                 for (privilege = 0; privilege < 64; privilege++) {
158                         uint64_t mask = sec_privilege_mask(privilege);
159
160                         if (token->privilege_mask & mask) {
161                                 DEBUGADD(dbg_lev, ("  Privilege[%3lu]: %s\n", (unsigned long)i++, 
162                                         sec_privilege_name(privilege)));
163                         }
164                 }
165         }
166
167         talloc_free(mem_ctx);
168 }
169
170 /* These really should be cheaper... */
171
172 BOOL is_system_token(struct security_token *token) 
173 {
174         TALLOC_CTX *mem_ctx = talloc_new(token);
175         if (dom_sid_equal(token->user_sid, dom_sid_parse_talloc(mem_ctx, SID_NT_SYSTEM))) {
176                 talloc_free(mem_ctx);
177                 return True;
178         }
179         talloc_free(mem_ctx);
180         return False;
181 }
182
183 BOOL is_anonymous_token(struct security_token *token) 
184 {
185         TALLOC_CTX *mem_ctx = talloc_new(token);
186         if (dom_sid_equal(token->user_sid, dom_sid_parse_talloc(mem_ctx, SID_NT_ANONYMOUS))) {
187                 talloc_free(mem_ctx);
188                 return True;
189         }
190         talloc_free(mem_ctx);
191         return False;
192 }
193