2 Unix SMB/CIFS implementation.
4 security descriptor utility functions
6 Copyright (C) Andrew Tridgell 2004
7 Copyright (C) Andrew Bartlett 2010
8 Copyright (C) Stefan Metzmacher 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "lib/util/talloc_stack.h"
27 #include "lib/util/debug.h"
28 #include "lib/util/fault.h"
29 #include "libcli/security/security_token.h"
30 #include "libcli/security/dom_sid.h"
31 #include "libcli/security/privileges.h"
32 #include "librpc/gen_ndr/ndr_security.h"
33 #include "lib/util/talloc_stack.h"
36 return a blank security token
38 struct security_token
*security_token_initialise(TALLOC_CTX
*mem_ctx
,
39 enum claims_evaluation_control evaluate_claims
)
41 struct security_token
*st
= talloc_zero(
42 mem_ctx
, struct security_token
);
43 st
->evaluate_claims
= evaluate_claims
;
48 /****************************************************************************
49 Duplicate a SID token.
50 ****************************************************************************/
52 struct security_token
*security_token_duplicate(TALLOC_CTX
*mem_ctx
, const struct security_token
*src
)
54 TALLOC_CTX
*frame
= NULL
;
55 struct security_token
*dst
= NULL
;
57 enum ndr_err_code ndr_err
;
63 frame
= talloc_stackframe();
65 ndr_err
= ndr_push_struct_blob(
69 (ndr_push_flags_fn_t
)ndr_push_security_token
);
70 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
71 DBG_ERR("Failed to duplicate security_token ndr_push_security_token failed: %s\n",
77 dst
= talloc_zero(mem_ctx
, struct security_token
);
79 DBG_ERR("talloc failed\n");
84 ndr_err
= ndr_pull_struct_blob(
88 (ndr_pull_flags_fn_t
)ndr_pull_security_token
);
90 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
91 DBG_ERR("Failed to duplicate security_token ndr_pull_security_token "
103 /****************************************************************************
104 prints a struct security_token to debug output.
105 ****************************************************************************/
106 void security_token_debug(int dbg_class
, int dbg_lev
, const struct security_token
*token
)
108 TALLOC_CTX
*frame
= talloc_stackframe();
114 DEBUGC(dbg_class
, dbg_lev
, ("Security token: (NULL)\n"));
119 sids
= talloc_asprintf(frame
,
120 "Security token SIDs (%" PRIu32
"):\n",
122 for (i
= 0; i
< token
->num_sids
; i
++) {
123 struct dom_sid_buf sidbuf
;
124 talloc_asprintf_addbuf(
126 " SID[%3" PRIu32
"]: %s\n",
128 dom_sid_str_buf(&token
->sids
[i
], &sidbuf
));
131 privs
= security_token_debug_privileges(frame
, token
);
135 ("%s%s", sids
? sids
: "(NULL)", privs
? privs
: "(NULL)"));
140 /* These really should be cheaper... */
142 bool security_token_is_sid(const struct security_token
*token
, const struct dom_sid
*sid
)
146 if (token
->sids
== NULL
) {
149 ret
= dom_sid_equal(&token
->sids
[PRIMARY_USER_SID_INDEX
], sid
);
153 bool security_token_is_system(const struct security_token
*token
)
155 return security_token_is_sid(token
, &global_sid_System
);
158 bool security_token_is_anonymous(const struct security_token
*token
)
160 return security_token_is_sid(token
, &global_sid_Anonymous
);
163 bool security_token_has_sid(const struct security_token
*token
, const struct dom_sid
*sid
)
166 for (i
= 0; i
< token
->num_sids
; i
++) {
167 if (dom_sid_equal(&token
->sids
[i
], sid
)) {
174 size_t security_token_count_flag_sids(const struct security_token
*token
,
175 const struct dom_sid
*prefix_sid
,
177 const struct dom_sid
**_flag_sid
)
179 const size_t num_auths_expected
= prefix_sid
->num_auths
+ num_flags
;
180 const struct dom_sid
*found
= NULL
;
184 SMB_ASSERT(num_auths_expected
<= ARRAY_SIZE(prefix_sid
->sub_auths
));
186 for (i
= 0; i
< token
->num_sids
; i
++) {
187 const struct dom_sid
*sid
= &token
->sids
[i
];
190 if (sid
->num_auths
!= num_auths_expected
) {
194 cmp
= dom_sid_compare_domain(sid
, prefix_sid
);
203 if ((num
== 1) && (_flag_sid
!= NULL
)) {
210 bool security_token_has_builtin_guests(const struct security_token
*token
)
212 return security_token_has_sid(token
, &global_sid_Builtin_Guests
);
215 bool security_token_has_builtin_administrators(const struct security_token
*token
)
217 return security_token_has_sid(token
, &global_sid_Builtin_Administrators
);
220 bool security_token_has_nt_authenticated_users(const struct security_token
*token
)
222 return security_token_has_sid(token
, &global_sid_Authenticated_Users
);
225 bool security_token_has_enterprise_dcs(const struct security_token
*token
)
227 return security_token_has_sid(token
, &global_sid_Enterprise_DCs
);