2a57ff0d7425445e72cc878e3f980fcc1a985990
[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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/security/security.h"
25 #include "lib/ldb/include/ldb.h"
26
27 /*
28   add privilege bits for one sid to a security_token
29 */
30 static NTSTATUS samdb_privilege_setup_sid(void *samctx, TALLOC_CTX *mem_ctx,
31                                           const struct dom_sid *sid, 
32                                           uint64_t *mask)
33 {
34         char *sidstr;
35         const char * const attrs[] = { "privilege", NULL };
36         struct ldb_message **res = NULL;
37         struct ldb_message_element *el;
38         int ret, i;
39         
40         *mask = 0;
41
42         sidstr = dom_sid_string(mem_ctx, sid);
43         if (sidstr == NULL) {
44                 return NT_STATUS_NO_MEMORY;
45         }
46
47         ret = samdb_search(samctx, mem_ctx, NULL, &res, attrs, "objectSid=%s", sidstr);
48         if (ret != 1) {
49                 talloc_free(sidstr);
50                 /* not an error to not match */
51                 return NT_STATUS_OK;
52         }
53
54         el = ldb_msg_find_element(res[0], "privilege");
55         if (el == NULL) {
56                 talloc_free(sidstr);
57                 return NT_STATUS_OK;
58         }
59
60         for (i=0;i<el->num_values;i++) {
61                 const char *priv_str = el->values[i].data;
62                 int privilege = sec_privilege_id(priv_str);
63                 if (privilege == -1) {
64                         DEBUG(1,("Unknown privilege '%s' in samdb\n",
65                                  priv_str));
66                         continue;
67                 }
68                 *mask |= sec_privilege_mask(privilege);
69         }
70
71         return NT_STATUS_OK;
72 }
73
74 /*
75   setup the privilege mask for this security token based on our
76   local SAM
77 */
78 NTSTATUS samdb_privilege_setup(struct security_token *token)
79 {
80         void *samctx;
81         TALLOC_CTX *mem_ctx = talloc(token, 0);
82         int i;
83         NTSTATUS status;
84
85         samctx = samdb_connect(mem_ctx);
86         if (samctx == NULL) {
87                 talloc_free(mem_ctx);
88                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
89         }
90
91         token->privilege_mask = 0;
92         
93         for (i=0;i<token->num_sids;i++) {
94                 uint64_t mask;
95                 status = samdb_privilege_setup_sid(samctx, mem_ctx, 
96                                                    token->sids[i], &mask);
97                 if (!NT_STATUS_IS_OK(status)) {
98                         talloc_free(mem_ctx);
99                         return status;
100                 }
101                 token->privilege_mask |= mask;
102         }
103
104         talloc_free(mem_ctx);
105
106         return NT_STATUS_OK;    
107 }