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"
28 return a blank security token
30 struct security_token *security_token_initialise(TALLOC_CTX *mem_ctx)
32 struct security_token *st;
34 st = talloc(mem_ctx, struct security_token);
43 st->privilege_mask = 0;
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,
55 struct dom_sid **groupSIDs,
56 BOOL is_authenticated,
57 struct security_token **token)
59 struct security_token *ptoken;
63 ptoken = security_token_initialise(mem_ctx);
64 NT_STATUS_HAVE_NO_MEMORY(ptoken);
66 ptoken->sids = talloc_array(ptoken, struct dom_sid *, n_groupSIDs + 5);
67 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
69 ptoken->user_sid = talloc_reference(ptoken, user_sid);
70 ptoken->group_sid = talloc_reference(ptoken, group_sid);
71 ptoken->privilege_mask = 0;
73 ptoken->sids[0] = ptoken->user_sid;
74 ptoken->sids[1] = ptoken->group_sid;
77 * Finally add the "standard" SIDs.
78 * The only difference between guest and "anonymous"
79 * is the addition of Authenticated_Users.
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]);
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]);
93 for (i = 0; i < n_groupSIDs; i++) {
95 for (check_sid_idx = 1;
96 check_sid_idx < ptoken->num_sids;
98 if (dom_sid_equal(ptoken->sids[check_sid_idx], groupSIDs[i])) {
103 if (check_sid_idx == ptoken->num_sids) {
104 ptoken->sids[ptoken->num_sids++] = talloc_reference(ptoken, groupSIDs[i]);
108 /* setup the privilege mask for this token */
109 status = samdb_privilege_setup(ptoken);
110 if (!NT_STATUS_IS_OK(status)) {
115 security_token_debug(10, ptoken);
122 /****************************************************************************
123 prints a struct security_token to debug output.
124 ****************************************************************************/
125 void security_token_debug(int dbg_lev, const struct security_token *token)
132 DEBUG(dbg_lev, ("Security token: (NULL)\n"));
136 mem_ctx = talloc_init("security_token_debug()");
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])));
150 DEBUGADD(dbg_lev, (" Privileges (0x%08X%08X):\n",
151 (uint32_t)((token->privilege_mask & 0xFFFFFFFF00000000LL) >> 32),
152 (uint32_t)(token->privilege_mask & 0x00000000FFFFFFFFLL)));
154 if (token->privilege_mask) {
156 for (privilege = 0; privilege < 64; privilege++) {
157 uint64_t mask = sec_privilege_mask(privilege);
159 if (token->privilege_mask & mask) {
160 DEBUGADD(dbg_lev, (" Privilege[%3lu]: %s\n", (unsigned long)i++,
161 sec_privilege_name(privilege)));
166 talloc_free(mem_ctx);