2 Unix SMB/CIFS implementation.
4 security descriptror utility functions
6 Copyright (C) Andrew Tridgell 2004
7 Copyright (C) Stefan Metzmacher 2005
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.
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.
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.
25 #include "librpc/gen_ndr/ndr_security.h"
26 #include "dsdb/samdb/samdb.h"
29 return a blank security token
31 struct security_token *security_token_initialise(TALLOC_CTX *mem_ctx)
33 struct security_token *st;
35 st = talloc(mem_ctx, struct security_token);
44 st->privilege_mask = 0;
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,
56 struct dom_sid **groupSIDs,
57 BOOL is_authenticated,
58 struct security_token **token)
60 struct security_token *ptoken;
64 ptoken = security_token_initialise(mem_ctx);
65 NT_STATUS_HAVE_NO_MEMORY(ptoken);
67 ptoken->sids = talloc_array(ptoken, struct dom_sid *, n_groupSIDs + 5);
68 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
70 ptoken->user_sid = talloc_reference(ptoken, user_sid);
71 ptoken->group_sid = talloc_reference(ptoken, group_sid);
72 ptoken->privilege_mask = 0;
74 ptoken->sids[0] = ptoken->user_sid;
75 ptoken->sids[1] = ptoken->group_sid;
78 * Finally add the "standard" SIDs.
79 * The only difference between guest and "anonymous"
80 * is the addition of Authenticated_Users.
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]);
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]);
94 for (i = 0; i < n_groupSIDs; i++) {
96 for (check_sid_idx = 1;
97 check_sid_idx < ptoken->num_sids;
99 if (dom_sid_equal(ptoken->sids[check_sid_idx], groupSIDs[i])) {
104 if (check_sid_idx == ptoken->num_sids) {
105 ptoken->sids[ptoken->num_sids++] = talloc_reference(ptoken->sids, groupSIDs[i]);
109 /* setup the privilege mask for this token */
110 status = samdb_privilege_setup(ptoken);
111 if (!NT_STATUS_IS_OK(status)) {
116 security_token_debug(10, ptoken);
123 /****************************************************************************
124 prints a struct security_token to debug output.
125 ****************************************************************************/
126 void security_token_debug(int dbg_lev, const struct security_token *token)
133 DEBUG(dbg_lev, ("Security token: (NULL)\n"));
137 mem_ctx = talloc_init("security_token_debug()");
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])));
151 DEBUGADD(dbg_lev, (" Privileges (0x%08X%08X):\n",
152 (uint32_t)((token->privilege_mask & 0xFFFFFFFF00000000LL) >> 32),
153 (uint32_t)(token->privilege_mask & 0x00000000FFFFFFFFLL)));
155 if (token->privilege_mask) {
157 for (privilege = 0; privilege < 64; privilege++) {
158 uint64_t mask = sec_privilege_mask(privilege);
160 if (token->privilege_mask & mask) {
161 DEBUGADD(dbg_lev, (" Privilege[%3lu]: %s\n", (unsigned long)i++,
162 sec_privilege_name(privilege)));
167 talloc_free(mem_ctx);
170 /* These really should be cheaper... */
172 BOOL is_system_token(struct security_token *token)
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);
179 talloc_free(mem_ctx);
183 BOOL is_anonymous_token(struct security_token *token)
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);
190 talloc_free(mem_ctx);