s3-util_sid: use shared dom_sid_compare_auth and dom_sid_equal_X functions.
[Samba.git] / source3 / lib / util_nttoken.c
blob680dd29ba77b10a4f52d451cc26dc1b85111e28f
1 /*
2 * Unix SMB/CIFS implementation.
3 * Authentication utility functions
4 * Copyright (C) Andrew Tridgell 1992-1998
5 * Copyright (C) Andrew Bartlett 2001
6 * Copyright (C) Jeremy Allison 2000-2001
7 * Copyright (C) Rafal Szczesniak 2002
8 * Copyright (C) Volker Lendecke 2006
9 * Copyright (C) Michael Adam 2007
10 * Copyright (C) Guenther Deschner 2007
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <http://www.gnu.org/licenses/>.
26 /* function(s) moved from auth/auth_util.c to minimize linker deps */
28 #include "includes.h"
29 #include "../libcli/security/dom_sid.h"
31 /****************************************************************************
32 Duplicate a SID token.
33 ****************************************************************************/
35 struct security_token *dup_nt_token(TALLOC_CTX *mem_ctx, const struct security_token *ptoken)
37 struct security_token *token;
39 if (!ptoken)
40 return NULL;
42 token = TALLOC_ZERO_P(mem_ctx, struct security_token);
43 if (token == NULL) {
44 DEBUG(0, ("talloc failed\n"));
45 return NULL;
48 if (ptoken->sids && ptoken->num_sids) {
49 token->sids = (struct dom_sid *)talloc_memdup(
50 token, ptoken->sids, sizeof(struct dom_sid) * ptoken->num_sids );
52 if (token->sids == NULL) {
53 DEBUG(0, ("talloc_memdup failed\n"));
54 TALLOC_FREE(token);
55 return NULL;
57 token->num_sids = ptoken->num_sids;
60 token->privilege_mask = ptoken->privilege_mask;
62 return token;
65 /****************************************************************************
66 merge NT tokens
67 ****************************************************************************/
69 NTSTATUS merge_nt_token(TALLOC_CTX *mem_ctx,
70 const struct security_token *token_1,
71 const struct security_token *token_2,
72 struct security_token **token_out)
74 struct security_token *token = NULL;
75 NTSTATUS status;
76 int i;
78 if (!token_1 || !token_2 || !token_out) {
79 return NT_STATUS_INVALID_PARAMETER;
82 token = TALLOC_ZERO_P(mem_ctx, struct security_token);
83 NT_STATUS_HAVE_NO_MEMORY(token);
85 for (i=0; i < token_1->num_sids; i++) {
86 status = add_sid_to_array_unique(mem_ctx,
87 &token_1->sids[i],
88 &token->sids,
89 &token->num_sids);
90 if (!NT_STATUS_IS_OK(status)) {
91 TALLOC_FREE(token);
92 return status;
96 for (i=0; i < token_2->num_sids; i++) {
97 status = add_sid_to_array_unique(mem_ctx,
98 &token_2->sids[i],
99 &token->sids,
100 &token->num_sids);
101 if (!NT_STATUS_IS_OK(status)) {
102 TALLOC_FREE(token);
103 return status;
107 token->privilege_mask |= token_1->privilege_mask;
108 token->privilege_mask |= token_2->privilege_mask;
110 *token_out = token;
112 return NT_STATUS_OK;
115 /*******************************************************************
116 Check if this struct security_ace has a SID in common with the token.
117 ********************************************************************/
119 bool token_sid_in_ace(const struct security_token *token, const struct security_ace *ace)
121 size_t i;
123 for (i = 0; i < token->num_sids; i++) {
124 if (dom_sid_equal(&ace->trustee, &token->sids[i]))
125 return true;
128 return false;