9cb981f74f0b2b00e8fdb474dd0250c18597374e
[gd/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  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *  
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *  
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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