Add gp_get_machine_token().
[kai/samba.git] / source / 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 NT_USER_TOKEN *dup_nt_token(TALLOC_CTX *mem_ctx, const NT_USER_TOKEN *ptoken)
35 {
36         NT_USER_TOKEN *token;
37
38         if (!ptoken)
39                 return NULL;
40
41         token = TALLOC_P(mem_ctx, NT_USER_TOKEN);
42         if (token == NULL) {
43                 DEBUG(0, ("talloc failed\n"));
44                 return NULL;
45         }
46
47         ZERO_STRUCTP(token);
48
49         if (ptoken->user_sids && ptoken->num_sids) {
50                 token->user_sids = (DOM_SID *)talloc_memdup(
51                         token, ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids );
52
53                 if (token->user_sids == NULL) {
54                         DEBUG(0, ("talloc_memdup failed\n"));
55                         TALLOC_FREE(token);
56                         return NULL;
57                 }
58                 token->num_sids = ptoken->num_sids;
59         }
60         
61         /* copy the privileges; don't consider failure to be critical here */
62         
63         if ( !se_priv_copy( &token->privileges, &ptoken->privileges ) ) {
64                 DEBUG(0,("dup_nt_token: Failure to copy SE_PRIV!.  "
65                          "Continuing with 0 privileges assigned.\n"));
66         }
67
68         return token;
69 }
70
71 /****************************************************************************
72  merge NT tokens
73 ****************************************************************************/
74
75 NTSTATUS merge_nt_token(TALLOC_CTX *mem_ctx,
76                         const struct nt_user_token *token_1,
77                         const struct nt_user_token *token_2,
78                         struct nt_user_token **token_out)
79 {
80         struct nt_user_token *token = NULL;
81         NTSTATUS status;
82         int i;
83
84         if (!token_1 || !token_2 || !token_out) {
85                 return NT_STATUS_INVALID_PARAMETER;
86         }
87
88         token = TALLOC_ZERO_P(mem_ctx, struct nt_user_token);
89         NT_STATUS_HAVE_NO_MEMORY(token);
90
91         for (i=0; i < token_1->num_sids; i++) {
92                 status = add_sid_to_array_unique(mem_ctx,
93                                                  &token_1->user_sids[i],
94                                                  &token->user_sids,
95                                                  &token->num_sids);
96                 if (!NT_STATUS_IS_OK(status)) {
97                         TALLOC_FREE(token);
98                         return status;
99                 }
100         }
101
102         for (i=0; i < token_2->num_sids; i++) {
103                 status = add_sid_to_array_unique(mem_ctx,
104                                                  &token_2->user_sids[i],
105                                                  &token->user_sids,
106                                                  &token->num_sids);
107                 if (!NT_STATUS_IS_OK(status)) {
108                         TALLOC_FREE(token);
109                         return status;
110                 }
111         }
112
113         se_priv_add(&token->privileges, &token_1->privileges);
114         se_priv_add(&token->privileges, &token_2->privileges);
115
116         *token_out = token;
117
118         return NT_STATUS_OK;
119 }