r26003: Split up DB_WRAP, as first step in an attempt to sanitize dependencies.
[kai/samba.git] / source4 / dsdb / samdb / samdb_privilege.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    manipulate privilege records in samdb
5
6    Copyright (C) Andrew Tridgell 2004
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/ldap/ldap.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "auth/auth.h"
26 #include "libcli/security/security.h"
27 #include "util/util_ldb.h"
28
29 /*
30   add privilege bits for one sid to a security_token
31 */
32 static NTSTATUS samdb_privilege_setup_sid(void *samctx, TALLOC_CTX *mem_ctx,
33                                           struct security_token *token,
34                                           const struct dom_sid *sid)
35 {
36         const char * const attrs[] = { "privilege", NULL };
37         struct ldb_message **res = NULL;
38         struct ldb_message_element *el;
39         int ret, i;
40         char *sidstr;
41
42         sidstr = ldap_encode_ndr_dom_sid(mem_ctx, sid);
43         NT_STATUS_HAVE_NO_MEMORY(sidstr);
44
45         ret = gendb_search(samctx, mem_ctx, NULL, &res, attrs, "objectSid=%s", sidstr);
46         talloc_free(sidstr);
47         if (ret != 1) {
48                 /* not an error to not match */
49                 return NT_STATUS_OK;
50         }
51
52         el = ldb_msg_find_element(res[0], "privilege");
53         if (el == NULL) {
54                 return NT_STATUS_OK;
55         }
56
57         for (i=0;i<el->num_values;i++) {
58                 const char *priv_str = (const char *)el->values[i].data;
59                 enum sec_privilege privilege = sec_privilege_id(priv_str);
60                 if (privilege == -1) {
61                         DEBUG(1,("Unknown privilege '%s' in samdb\n",
62                                  priv_str));
63                         continue;
64                 }
65                 security_token_set_privilege(token, privilege);
66         }
67
68         return NT_STATUS_OK;
69 }
70
71 /*
72   setup the privilege mask for this security token based on our
73   local SAM
74 */
75 _PUBLIC_ NTSTATUS samdb_privilege_setup(struct security_token *token)
76 {
77         void *samctx;
78         TALLOC_CTX *mem_ctx;
79         int i;
80         NTSTATUS status;
81
82         /* Shortcuts to prevent recursion and avoid lookups */
83         if (token->user_sid == NULL) {
84                 token->privilege_mask = 0;
85                 return NT_STATUS_OK;
86         }
87
88         if (security_token_is_system(token)) {
89                 token->privilege_mask = ~0;
90                 return NT_STATUS_OK;
91         }
92
93         if (security_token_is_anonymous(token)) {
94                 token->privilege_mask = 0;
95                 return NT_STATUS_OK;
96         }
97
98         mem_ctx = talloc_new(token);
99         samctx = samdb_connect(mem_ctx, system_session(mem_ctx));
100         if (samctx == NULL) {
101                 talloc_free(mem_ctx);
102                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
103         }
104
105         token->privilege_mask = 0;
106         
107         for (i=0;i<token->num_sids;i++) {
108                 status = samdb_privilege_setup_sid(samctx, mem_ctx,
109                                                    token, token->sids[i]);
110                 if (!NT_STATUS_IS_OK(status)) {
111                         talloc_free(mem_ctx);
112                         return status;
113                 }
114         }
115
116         talloc_free(mem_ctx);
117
118         return NT_STATUS_OK;    
119 }