s3-privs Directly manipulate the privileges bitmap.
[kai/samba.git] / source3 / lib / util_nttoken.c
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
11  *
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.
16  *  
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.
21  *  
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/>.
24  */
25
26 /* function(s) moved from auth/auth_util.c to minimize linker deps */
27
28 #include "includes.h"
29
30 /****************************************************************************
31  Duplicate a SID token.
32 ****************************************************************************/
33
34 struct security_token *dup_nt_token(TALLOC_CTX *mem_ctx, const struct security_token *ptoken)
35 {
36         struct security_token *token;
37
38         if (!ptoken)
39                 return NULL;
40
41         token = TALLOC_ZERO_P(mem_ctx, struct security_token);
42         if (token == NULL) {
43                 DEBUG(0, ("talloc failed\n"));
44                 return NULL;
45         }
46
47         if (ptoken->sids && ptoken->num_sids) {
48                 token->sids = (struct dom_sid *)talloc_memdup(
49                         token, ptoken->sids, sizeof(struct dom_sid) * ptoken->num_sids );
50
51                 if (token->sids == NULL) {
52                         DEBUG(0, ("talloc_memdup failed\n"));
53                         TALLOC_FREE(token);
54                         return NULL;
55                 }
56                 token->num_sids = ptoken->num_sids;
57         }
58         
59         token->privilege_mask = ptoken->privilege_mask;
60
61         return token;
62 }
63
64 /****************************************************************************
65  merge NT tokens
66 ****************************************************************************/
67
68 NTSTATUS merge_nt_token(TALLOC_CTX *mem_ctx,
69                         const struct security_token *token_1,
70                         const struct security_token *token_2,
71                         struct security_token **token_out)
72 {
73         struct security_token *token = NULL;
74         NTSTATUS status;
75         int i;
76
77         if (!token_1 || !token_2 || !token_out) {
78                 return NT_STATUS_INVALID_PARAMETER;
79         }
80
81         token = TALLOC_ZERO_P(mem_ctx, struct security_token);
82         NT_STATUS_HAVE_NO_MEMORY(token);
83
84         for (i=0; i < token_1->num_sids; i++) {
85                 status = add_sid_to_array_unique(mem_ctx,
86                                                  &token_1->sids[i],
87                                                  &token->sids,
88                                                  &token->num_sids);
89                 if (!NT_STATUS_IS_OK(status)) {
90                         TALLOC_FREE(token);
91                         return status;
92                 }
93         }
94
95         for (i=0; i < token_2->num_sids; i++) {
96                 status = add_sid_to_array_unique(mem_ctx,
97                                                  &token_2->sids[i],
98                                                  &token->sids,
99                                                  &token->num_sids);
100                 if (!NT_STATUS_IS_OK(status)) {
101                         TALLOC_FREE(token);
102                         return status;
103                 }
104         }
105
106         token->privilege_mask |= token_1->privilege_mask;
107         token->privilege_mask |= token_2->privilege_mask;
108
109         *token_out = token;
110
111         return NT_STATUS_OK;
112 }
113
114 /*******************************************************************
115  Check if this struct security_ace has a SID in common with the token.
116 ********************************************************************/
117
118 bool token_sid_in_ace(const struct security_token *token, const struct security_ace *ace)
119 {
120         size_t i;
121
122         for (i = 0; i < token->num_sids; i++) {
123                 if (sid_equal(&ace->trustee, &token->sids[i]))
124                         return true;
125         }
126
127         return false;
128 }