provision: BIND 9.15.x is not supported
[samba.git] / source3 / lib / privileges.c
blob9900aa6f93bd59ebdb39abdeefc4fd4664f6e594
1 /*
2 Unix SMB/CIFS implementation.
3 Privileges handling functions
4 Copyright (C) Jean François Micouleau 1998-2001
5 Copyright (C) Simo Sorce 2002-2003
6 Copyright (C) Gerald (Jerry) Carter 2005
7 Copyright (C) Michael Adam 2007
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 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "lib/privileges.h"
26 #include "dbwrap/dbwrap.h"
27 #include "libcli/security/privileges_private.h"
28 #include "../libcli/security/security.h"
29 #include "passdb.h"
30 #include "lib/util/string_wrappers.h"
32 #define PRIVPREFIX "PRIV_"
34 typedef struct {
35 uint32_t count;
36 struct dom_sid *list;
37 } SID_LIST;
39 typedef struct {
40 TALLOC_CTX *mem_ctx;
41 uint64_t privilege;
42 SID_LIST sids;
43 } PRIV_SID_LIST;
46 interpret an old style SE_PRIV structure
48 static uint64_t map_old_SE_PRIV(unsigned char *dptr)
50 uint32_t *old_masks = (uint32_t *)dptr;
52 * the old privileges code only ever used up to 0x800, except
53 * for a special case of 'SE_ALL_PRIVS' which was 0xFFFFFFFF
55 if (old_masks[0] == 0xFFFFFFFF) {
56 /* they set all privileges */
57 return SE_ALL_PRIVS;
60 /* the old code used the machine byte order, but we don't know
61 * the byte order of the machine that wrote it. However we can
62 * tell what byte order it was by taking advantage of the fact
63 * that it only ever use up to 0x800
65 if (dptr[0] || dptr[1]) {
66 /* it was little endian */
67 return IVAL(dptr, 0);
70 /* it was either zero or big-endian */
71 return RIVAL(dptr, 0);
75 static bool get_privileges( const struct dom_sid *sid, uint64_t *mask )
77 struct db_context *db = get_account_pol_db();
78 struct dom_sid_buf tmp;
79 fstring keystr;
80 TDB_DATA data;
81 NTSTATUS status;
83 /* Fail if the admin has not enable privileges */
85 if ( !lp_enable_privileges() ) {
86 return False;
89 if ( db == NULL )
90 return False;
92 /* PRIV_<SID> (NULL terminated) as the key */
94 fstr_sprintf(keystr, "%s%s", PRIVPREFIX, dom_sid_str_buf(sid, &tmp));
96 status = dbwrap_fetch_bystring(db, talloc_tos(), keystr, &data);
98 if (!NT_STATUS_IS_OK(status)) {
99 DEBUG(4, ("get_privileges: No privileges assigned to SID "
100 "[%s]\n", tmp.buf));
101 return False;
104 if (data.dsize == 4*4) {
105 /* it's an old style SE_PRIV structure. */
106 *mask = map_old_SE_PRIV(data.dptr);
107 } else {
108 if (data.dsize != sizeof( uint64_t ) ) {
109 DEBUG(3, ("get_privileges: Invalid privileges record assigned to SID "
110 "[%s]\n", tmp.buf));
111 return False;
114 *mask = BVAL(data.dptr, 0);
117 TALLOC_FREE(data.dptr);
119 return True;
122 /***************************************************************************
123 Store the privilege mask (set) for a given SID
124 ****************************************************************************/
126 static bool set_privileges( const struct dom_sid *sid, uint64_t mask )
128 struct db_context *db = get_account_pol_db();
129 uint8_t privbuf[8];
130 struct dom_sid_buf tmp;
131 fstring keystr;
132 TDB_DATA data;
134 if ( !lp_enable_privileges() )
135 return False;
137 if ( db == NULL )
138 return False;
140 if ( !sid || (sid->num_auths == 0) ) {
141 DEBUG(0,("set_privileges: Refusing to store empty SID!\n"));
142 return False;
145 /* PRIV_<SID> (NULL terminated) as the key */
147 fstr_sprintf(keystr, "%s%s", PRIVPREFIX, dom_sid_str_buf(sid, &tmp));
149 /* This writes the 64 bit bitmask out in little endian format */
150 SBVAL(privbuf,0,mask);
152 data.dptr = privbuf;
153 data.dsize = sizeof(privbuf);
155 return NT_STATUS_IS_OK(dbwrap_store_bystring(db, keystr, data,
156 TDB_REPLACE));
159 /*********************************************************************
160 get a list of all privileges for all sids in the list
161 *********************************************************************/
163 bool get_privileges_for_sids(uint64_t *privileges, struct dom_sid *slist, int scount)
165 uint64_t mask;
166 int i;
167 bool found = False;
169 *privileges = 0;
171 for ( i=0; i<scount; i++ ) {
172 struct dom_sid_buf buf;
174 /* don't add unless we actually have a privilege assigned */
176 if ( !get_privileges( &slist[i], &mask ) )
177 continue;
179 DEBUG(5,("get_privileges_for_sids: sid = %s\nPrivilege "
180 "set: 0x%llx\n",
181 dom_sid_str_buf(&slist[i], &buf),
182 (unsigned long long)mask));
184 *privileges |= mask;
185 found = True;
188 return found;
191 NTSTATUS get_privileges_for_sid_as_set(TALLOC_CTX *mem_ctx, PRIVILEGE_SET **privileges, struct dom_sid *sid)
193 uint64_t mask;
194 if (!get_privileges(sid, &mask)) {
195 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
198 *privileges = talloc_zero(mem_ctx, PRIVILEGE_SET);
199 if (!*privileges) {
200 return NT_STATUS_NO_MEMORY;
203 if (!se_priv_to_privilege_set(*privileges, mask)) {
204 return NT_STATUS_NO_MEMORY;
206 return NT_STATUS_OK;
209 /*********************************************************************
210 traversal functions for privilege_enumerate_accounts
211 *********************************************************************/
213 static int priv_traverse_fn(struct db_record *rec, void *state)
215 PRIV_SID_LIST *priv = (PRIV_SID_LIST *)state;
216 int prefixlen = strlen(PRIVPREFIX);
217 struct dom_sid sid;
218 fstring sid_string;
219 TDB_DATA key;
221 key = dbwrap_record_get_key(rec);
223 /* check we have a PRIV_+SID entry */
225 if (strncmp((char *)key.dptr, PRIVPREFIX, prefixlen) != 0)
226 return 0;
228 /* check to see if we are looking for a particular privilege */
230 fstrcpy( sid_string, (char *)&(key.dptr[strlen(PRIVPREFIX)]) );
232 if (priv->privilege != 0) {
233 uint64_t mask;
234 TDB_DATA value;
236 value = dbwrap_record_get_value(rec);
238 if (value.dsize == 4*4) {
239 mask = map_old_SE_PRIV(value.dptr);
240 } else {
241 if (value.dsize != sizeof( uint64_t ) ) {
242 DEBUG(3, ("get_privileges: Invalid privileges record assigned to SID "
243 "[%s]\n", sid_string));
244 return 0;
246 mask = BVAL(value.dptr, 0);
249 /* if the SID does not have the specified privilege
250 then just return */
252 if ((mask & priv->privilege) == 0) {
253 return 0;
257 /* this is a last ditch safety check to preventing returning
258 and invalid SID (i've somehow run into this on development branches) */
260 if ( strcmp( "S-0-0", sid_string ) == 0 )
261 return 0;
263 if ( !string_to_sid(&sid, sid_string) ) {
264 DEBUG(0,("travsersal_fn_enum__acct: Could not convert SID [%s]\n",
265 sid_string));
266 return 0;
269 if (!NT_STATUS_IS_OK(add_sid_to_array(priv->mem_ctx, &sid,
270 &priv->sids.list,
271 &priv->sids.count)))
273 return 0;
276 return 0;
279 /*********************************************************************
280 Retrieve list of privileged SIDs (for _lsa_enumerate_accounts()
281 *********************************************************************/
283 NTSTATUS privilege_enumerate_accounts(struct dom_sid **sids, int *num_sids)
285 struct db_context *db = get_account_pol_db();
286 PRIV_SID_LIST priv;
287 NTSTATUS status;
289 if (db == NULL) {
290 return NT_STATUS_ACCESS_DENIED;
293 ZERO_STRUCT(priv);
295 status = dbwrap_traverse_read(db, priv_traverse_fn, &priv, NULL);
296 if (!NT_STATUS_IS_OK(status)) {
297 return status;
300 /* give the memory away; caller will free */
302 *sids = priv.sids.list;
303 *num_sids = priv.sids.count;
305 return NT_STATUS_OK;
308 /*********************************************************************
309 Retrieve list of SIDs granted a particular privilege
310 *********************************************************************/
312 NTSTATUS privilege_enum_sids(enum sec_privilege privilege, TALLOC_CTX *mem_ctx,
313 struct dom_sid **sids, int *num_sids)
315 struct db_context *db = get_account_pol_db();
316 PRIV_SID_LIST priv;
317 NTSTATUS status;
319 if (db == NULL) {
320 return NT_STATUS_ACCESS_DENIED;
323 ZERO_STRUCT(priv);
325 priv.privilege = sec_privilege_mask(privilege);
326 priv.mem_ctx = mem_ctx;
328 status = dbwrap_traverse_read(db, priv_traverse_fn, &priv, NULL);
329 if (!NT_STATUS_IS_OK(status)) {
330 return status;
333 /* give the memory away; caller will free */
335 *sids = priv.sids.list;
336 *num_sids = priv.sids.count;
338 return NT_STATUS_OK;
341 /***************************************************************************
342 Add privilege to sid
343 ****************************************************************************/
345 static bool grant_privilege_bitmap(const struct dom_sid *sid, const uint64_t priv_mask)
347 uint64_t old_mask, new_mask;
348 struct dom_sid_buf buf;
350 ZERO_STRUCT( old_mask );
351 ZERO_STRUCT( new_mask );
353 if ( get_privileges( sid, &old_mask ) )
354 new_mask = old_mask;
355 else
356 new_mask = 0;
358 new_mask |= priv_mask;
360 DEBUG(10,("grant_privilege: %s\n", dom_sid_str_buf(sid, &buf)));
362 DEBUGADD( 10, ("original privilege mask: 0x%llx\n", (unsigned long long)new_mask));
364 DEBUGADD( 10, ("new privilege mask: 0x%llx\n", (unsigned long long)new_mask));
366 return set_privileges( sid, new_mask );
369 /*********************************************************************
370 Add a privilege based on its name
371 *********************************************************************/
373 bool grant_privilege_by_name(const struct dom_sid *sid, const char *name)
375 uint64_t mask;
377 if (! se_priv_from_name(name, &mask)) {
378 DEBUG(3, ("grant_privilege_by_name: "
379 "No Such Privilege Found (%s)\n", name));
380 return False;
383 return grant_privilege_bitmap( sid, mask );
386 /***************************************************************************
387 Grant a privilege set (list of LUID values) from a sid
388 ****************************************************************************/
390 bool grant_privilege_set(const struct dom_sid *sid, struct lsa_PrivilegeSet *set)
392 uint64_t privilege_mask;
393 if (!privilege_set_to_se_priv(&privilege_mask, set)) {
394 return false;
396 return grant_privilege_bitmap(sid, privilege_mask);
399 /***************************************************************************
400 Remove privilege from sid
401 ****************************************************************************/
403 static bool revoke_privilege_bitmap(const struct dom_sid *sid, const uint64_t priv_mask)
405 uint64_t mask;
406 struct dom_sid_buf buf;
408 /* if the user has no privileges, then we can't revoke any */
410 if ( !get_privileges( sid, &mask ) )
411 return True;
413 DEBUG(10,("revoke_privilege: %s\n", dom_sid_str_buf(sid, &buf)));
415 DEBUGADD( 10, ("original privilege mask: 0x%llx\n", (unsigned long long)mask));
417 mask &= ~priv_mask;
419 DEBUGADD( 10, ("new privilege mask: 0x%llx\n", (unsigned long long)mask));
421 return set_privileges( sid, mask );
424 /***************************************************************************
425 Remove a privilege set (list of LUID values) from a sid
426 ****************************************************************************/
428 bool revoke_privilege_set(const struct dom_sid *sid, struct lsa_PrivilegeSet *set)
430 uint64_t privilege_mask;
431 if (!privilege_set_to_se_priv(&privilege_mask, set)) {
432 return false;
434 return revoke_privilege_bitmap(sid, privilege_mask);
437 /*********************************************************************
438 Revoke all privileges
439 *********************************************************************/
441 bool revoke_all_privileges( const struct dom_sid *sid )
443 return revoke_privilege_bitmap( sid, SE_ALL_PRIVS);
446 /*********************************************************************
447 Add a privilege based on its name
448 *********************************************************************/
450 bool revoke_privilege_by_name(const struct dom_sid *sid, const char *name)
452 uint64_t mask;
454 if (! se_priv_from_name(name, &mask)) {
455 DEBUG(3, ("revoke_privilege_by_name: "
456 "No Such Privilege Found (%s)\n", name));
457 return False;
460 return revoke_privilege_bitmap(sid, mask);
464 /***************************************************************************
465 Retrieve the SIDs assigned to a given privilege
466 ****************************************************************************/
468 NTSTATUS privilege_create_account(const struct dom_sid *sid )
470 return ( grant_privilege_bitmap(sid, 0) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
473 /***************************************************************************
474 Delete a privileged account
475 ****************************************************************************/
477 NTSTATUS privilege_delete_account(const struct dom_sid *sid)
479 struct db_context *db = get_account_pol_db();
480 struct dom_sid_buf tmp;
481 fstring keystr;
483 if (!lp_enable_privileges()) {
484 return NT_STATUS_OK;
487 if (!db) {
488 return NT_STATUS_INVALID_HANDLE;
491 if (!sid || (sid->num_auths == 0)) {
492 return NT_STATUS_INVALID_SID;
495 /* PRIV_<SID> (NULL terminated) as the key */
497 fstr_sprintf(keystr, "%s%s", PRIVPREFIX, dom_sid_str_buf(sid, &tmp));
499 return dbwrap_delete_bystring(db, keystr);
502 /*******************************************************************
503 *******************************************************************/
505 bool is_privileged_sid( const struct dom_sid *sid )
507 uint64_t mask;
509 return get_privileges( sid, &mask );
512 /*******************************************************************
513 *******************************************************************/
515 bool grant_all_privileges( const struct dom_sid *sid )
517 uint64_t mask;
519 se_priv_put_all_privileges(&mask);
521 return grant_privilege_bitmap( sid, mask );