28339c969dd69da8e5eebef8bb123910de8ca3cb
[metze/samba/wip.git] / source4 / libcli / security / security_token.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    security descriptror utility functions
5
6    Copyright (C) Andrew Tridgell                2004
7    Copyright (C) Stefan Metzmacher              2005
8       
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "libcli/security/security.h"
27
28 /*
29   return a blank security token
30 */
31 struct security_token *security_token_initialise(TALLOC_CTX *mem_ctx)
32 {
33         struct security_token *st;
34
35         st = talloc(mem_ctx, struct security_token);
36         if (!st) {
37                 return NULL;
38         }
39
40         st->user_sid = NULL;
41         st->group_sid = NULL;
42         st->num_sids = 0;
43         st->sids = NULL;
44         st->privilege_mask = 0;
45
46         return st;
47 }
48
49 /****************************************************************************
50  prints a struct security_token to debug output.
51 ****************************************************************************/
52 void security_token_debug(int dbg_lev, const struct security_token *token)
53 {
54         TALLOC_CTX *mem_ctx;
55         int i;
56
57         if (!token) {
58                 DEBUG(dbg_lev, ("Security token: (NULL)\n"));
59                 return;
60         }
61
62         mem_ctx = talloc_init("security_token_debug()");
63         if (!mem_ctx) {
64                 return;
65         }
66
67         DEBUG(dbg_lev, ("Security token of user %s\n",
68                                     dom_sid_string(mem_ctx, token->user_sid) ));
69         DEBUGADD(dbg_lev, (" SIDs (%lu):\n", 
70                                        (unsigned long)token->num_sids));
71         for (i = 0; i < token->num_sids; i++) {
72                 DEBUGADD(dbg_lev, ("  SID[%3lu]: %s\n", (unsigned long)i, 
73                            dom_sid_string(mem_ctx, token->sids[i])));
74         }
75
76         security_token_debug_privileges(dbg_lev, token);
77
78         talloc_free(mem_ctx);
79 }
80
81 /* These really should be cheaper... */
82
83 BOOL security_token_is_sid(const struct security_token *token, const struct dom_sid *sid)
84 {
85         if (dom_sid_equal(token->user_sid, sid)) {
86                 return True;
87         }
88         return False;
89 }
90
91 BOOL security_token_is_sid_string(const struct security_token *token, const char *sid_string)
92 {
93         BOOL ret;
94         struct dom_sid *sid = dom_sid_parse_talloc(NULL, sid_string);
95         if (!sid) return False;
96
97         ret = security_token_is_sid(token, sid);
98
99         talloc_free(sid);
100         return ret;
101 }
102
103 BOOL security_token_is_system(const struct security_token *token) 
104 {
105         return security_token_is_sid_string(token, SID_NT_SYSTEM);
106 }
107
108 BOOL security_token_is_anonymous(const struct security_token *token) 
109 {
110         return security_token_is_sid_string(token, SID_NT_ANONYMOUS);
111 }
112
113 BOOL security_token_has_sid(const struct security_token *token, const struct dom_sid *sid)
114 {
115         int i;
116         for (i = 0; i < token->num_sids; i++) {
117                 if (dom_sid_equal(token->sids[i], sid)) {
118                         return True;
119                 }
120         }
121         return False;
122 }
123
124 BOOL security_token_has_sid_string(const struct security_token *token, const char *sid_string)
125 {
126         BOOL ret;
127         struct dom_sid *sid = dom_sid_parse_talloc(NULL, sid_string);
128         if (!sid) return False;
129
130         ret = security_token_has_sid(token, sid);
131
132         talloc_free(sid);
133         return ret;
134 }
135
136 BOOL security_token_has_builtin_administrators(const struct security_token *token)
137 {
138         return security_token_has_sid_string(token, SID_BUILTIN_ADMINISTRATORS);
139 }
140
141 BOOL security_token_has_nt_authenticated_users(const struct security_token *token)
142 {
143         return security_token_has_sid_string(token, SID_NT_AUTHENTICATED_USERS);
144 }