s3: don't build krb5 locator plugin if we don't build winbind
[Samba/gebeck_regimport.git] / source4 / dsdb / samdb / samdb_privilege.c
blob69c4ebea6172f594883773057fb263cb47c22bfe
1 /*
2 Unix SMB/CIFS implementation.
4 manipulate privilege records in samdb
6 Copyright (C) Andrew Tridgell 2004
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.
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.
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/>.
22 #include "includes.h"
23 #include "libcli/ldap/ldap_ndr.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "auth/auth.h"
26 #include "libcli/security/security.h"
27 #include "../lib/util/util_ldb.h"
28 #include "param/param.h"
29 #include "ldb_wrap.h"
31 /* connect to the privilege database */
32 struct ldb_context *privilege_connect(TALLOC_CTX *mem_ctx,
33 struct tevent_context *ev_ctx,
34 struct loadparm_context *lp_ctx)
36 return ldb_wrap_connect(mem_ctx, ev_ctx, lp_ctx, "privilege.ldb",
37 NULL, NULL, 0);
41 add privilege bits for one sid to a security_token
43 static NTSTATUS samdb_privilege_setup_sid(struct ldb_context *pdb, TALLOC_CTX *mem_ctx,
44 struct security_token *token,
45 const struct dom_sid *sid)
47 const char * const attrs[] = { "privilege", NULL };
48 struct ldb_message **res = NULL;
49 struct ldb_message_element *el;
50 unsigned int i;
51 int ret;
52 char *sidstr;
54 sidstr = ldap_encode_ndr_dom_sid(mem_ctx, sid);
55 NT_STATUS_HAVE_NO_MEMORY(sidstr);
57 ret = gendb_search(pdb, mem_ctx, NULL, &res, attrs, "objectSid=%s", sidstr);
58 talloc_free(sidstr);
59 if (ret != 1) {
60 /* not an error to not match */
61 return NT_STATUS_OK;
64 el = ldb_msg_find_element(res[0], "privilege");
65 if (el == NULL) {
66 return NT_STATUS_OK;
69 for (i=0;i<el->num_values;i++) {
70 const char *priv_str = (const char *)el->values[i].data;
71 enum sec_privilege privilege = sec_privilege_id(priv_str);
72 if (privilege == SEC_PRIV_INVALID) {
73 uint32_t right_bit = sec_right_bit(priv_str);
74 security_token_set_right_bit(token, right_bit);
75 if (right_bit == 0) {
76 DEBUG(1,("Unknown privilege '%s' in samdb\n",
77 priv_str));
79 continue;
81 security_token_set_privilege(token, privilege);
84 return NT_STATUS_OK;
88 setup the privilege mask for this security token based on our
89 local SAM
91 NTSTATUS samdb_privilege_setup(struct tevent_context *ev_ctx,
92 struct loadparm_context *lp_ctx, struct security_token *token)
94 struct ldb_context *pdb;
95 TALLOC_CTX *mem_ctx;
96 unsigned int i;
97 NTSTATUS status;
99 /* Shortcuts to prevent recursion and avoid lookups */
100 if (token->sids == NULL) {
101 token->privilege_mask = 0;
102 return NT_STATUS_OK;
105 if (security_token_is_system(token)) {
106 token->privilege_mask = ~0;
107 return NT_STATUS_OK;
110 if (security_token_is_anonymous(token)) {
111 token->privilege_mask = 0;
112 return NT_STATUS_OK;
115 mem_ctx = talloc_new(token);
116 pdb = privilege_connect(mem_ctx, ev_ctx, lp_ctx);
117 if (pdb == NULL) {
118 talloc_free(mem_ctx);
119 return NT_STATUS_INTERNAL_DB_CORRUPTION;
122 token->privilege_mask = 0;
124 for (i=0;i<token->num_sids;i++) {
125 status = samdb_privilege_setup_sid(pdb, mem_ctx,
126 token, &token->sids[i]);
127 if (!NT_STATUS_IS_OK(status)) {
128 talloc_free(mem_ctx);
129 return status;
133 talloc_free(mem_ctx);
135 return NT_STATUS_OK;