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/>.
27 #define PRIVPREFIX "PRIV_"
41 static bool get_privileges( const struct dom_sid *sid, uint64_t *mask )
43 struct db_context *db = get_account_pol_db();
47 /* Fail if the admin has not enable privileges */
49 if ( !lp_enable_privileges() ) {
56 /* PRIV_<SID> (NULL terminated) as the key */
58 fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
60 data = dbwrap_fetch_bystring( db, talloc_tos(), keystr );
63 DEBUG(3, ("get_privileges: No privileges assigned to SID "
64 "[%s]\n", sid_string_dbg(sid)));
68 if (data.dsize == 4*4) {
69 DEBUG(3, ("get_privileges: Should not have obtained old-style privileges record for SID "
70 "[%s]\n", sid_string_dbg(sid)));
74 if (data.dsize != sizeof( uint64_t ) ) {
75 DEBUG(3, ("get_privileges: Invalid privileges record assigned to SID "
76 "[%s]\n", sid_string_dbg(sid)));
80 *mask = BVAL(data.dptr, 0);
82 TALLOC_FREE(data.dptr);
87 /***************************************************************************
88 Store the privilege mask (set) for a given SID
89 ****************************************************************************/
91 static bool set_privileges( const struct dom_sid *sid, uint64_t *mask )
93 struct db_context *db = get_account_pol_db();
98 if ( !lp_enable_privileges() )
104 if ( !sid || (sid->num_auths == 0) ) {
105 DEBUG(0,("set_privileges: Refusing to store empty SID!\n"));
109 /* PRIV_<SID> (NULL terminated) as the key */
111 fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
113 /* This writes the 64 bit bitmask out in little endian format */
114 SBVAL(privbuf,0,*mask);
116 data.dptr = (uint8 *)mask;
117 data.dsize = sizeof(uint64_t);
119 return NT_STATUS_IS_OK(dbwrap_store_bystring(db, keystr, data,
123 /*********************************************************************
124 get a list of all privileges for all sids in the list
125 *********************************************************************/
127 bool get_privileges_for_sids(uint64_t *privileges, struct dom_sid *slist, int scount)
135 for ( i=0; i<scount; i++ ) {
136 /* don't add unless we actually have a privilege assigned */
138 if ( !get_privileges( &slist[i], &mask ) )
141 DEBUG(5,("get_privileges_for_sids: sid = %s\nPrivilege "
142 "set: 0x%llx\n", sid_string_dbg(&slist[i]),
143 (unsigned long long)mask));
153 /*********************************************************************
154 traversal functions for privilege_enumerate_accounts
155 *********************************************************************/
157 static int priv_traverse_fn(struct db_record *rec, void *state)
159 PRIV_SID_LIST *priv = (PRIV_SID_LIST *)state;
160 int prefixlen = strlen(PRIVPREFIX);
164 /* easy check first */
166 if (rec->value.dsize != sizeof(uint64_t) )
169 /* check we have a PRIV_+SID entry */
171 if ( strncmp((char *)rec->key.dptr, PRIVPREFIX, prefixlen) != 0)
174 /* check to see if we are looking for a particular privilege */
176 if ( !se_priv_equal(&priv->privilege, &se_priv_none) ) {
179 se_priv_copy( &mask, (uint64_t*)rec->value.dptr );
181 /* if the SID does not have the specified privilege
184 if ( !is_privilege_assigned( &mask, &priv->privilege) )
188 fstrcpy( sid_string, (char *)&(rec->key.dptr[strlen(PRIVPREFIX)]) );
190 /* this is a last ditch safety check to preventing returning
191 and invalid SID (i've somehow run into this on development branches) */
193 if ( strcmp( "S-0-0", sid_string ) == 0 )
196 if ( !string_to_sid(&sid, sid_string) ) {
197 DEBUG(0,("travsersal_fn_enum__acct: Could not convert SID [%s]\n",
202 if (!NT_STATUS_IS_OK(add_sid_to_array(priv->mem_ctx, &sid,
212 /*********************************************************************
213 Retreive list of privileged SIDs (for _lsa_enumerate_accounts()
214 *********************************************************************/
216 NTSTATUS privilege_enumerate_accounts(struct dom_sid **sids, int *num_sids)
218 struct db_context *db = get_account_pol_db();
222 return NT_STATUS_ACCESS_DENIED;
227 db->traverse_read(db, priv_traverse_fn, &priv);
229 /* give the memory away; caller will free */
231 *sids = priv.sids.list;
232 *num_sids = priv.sids.count;
237 /*********************************************************************
238 Retrieve list of SIDs granted a particular privilege
239 *********************************************************************/
241 NTSTATUS privilege_enum_sids(const uint64_t *mask, TALLOC_CTX *mem_ctx,
242 struct dom_sid **sids, int *num_sids)
244 struct db_context *db = get_account_pol_db();
248 return NT_STATUS_ACCESS_DENIED;
253 priv.privilege = *mask;
254 priv.mem_ctx = mem_ctx;
256 db->traverse_read(db, priv_traverse_fn, &priv);
258 /* give the memory away; caller will free */
260 *sids = priv.sids.list;
261 *num_sids = priv.sids.count;
266 /***************************************************************************
268 ****************************************************************************/
270 bool grant_privilege(const struct dom_sid *sid, const uint64_t priv_mask)
272 uint64_t old_mask, new_mask;
274 ZERO_STRUCT( old_mask );
275 ZERO_STRUCT( new_mask );
277 if ( get_privileges( sid, &old_mask ) )
282 new_mask |= priv_mask;
284 DEBUG(10,("grant_privilege: %s\n", sid_string_dbg(sid)));
286 DEBUGADD( 10, ("original privilege mask: 0x%llx\n", (unsigned long long)new_mask));
288 DEBUGADD( 10, ("new privilege mask: 0x%llx\n", (unsigned long long)new_mask));
290 return set_privileges( sid, &new_mask );
293 /*********************************************************************
294 Add a privilege based on its name
295 *********************************************************************/
297 bool grant_privilege_by_name(struct dom_sid *sid, const char *name)
301 if (! se_priv_from_name(name, &mask)) {
302 DEBUG(3, ("grant_privilege_by_name: "
303 "No Such Privilege Found (%s)\n", name));
307 return grant_privilege( sid, mask );
310 /***************************************************************************
311 Remove privilege from sid
312 ****************************************************************************/
314 bool revoke_privilege(const struct dom_sid *sid, const uint64_t priv_mask)
318 /* if the user has no privileges, then we can't revoke any */
320 if ( !get_privileges( sid, &mask ) )
323 DEBUG(10,("revoke_privilege: %s\n", sid_string_dbg(sid)));
325 DEBUGADD( 10, ("original privilege mask: 0x%llx\n", (unsigned long long)mask));
329 DEBUGADD( 10, ("new privilege mask: 0x%llx\n", (unsigned long long)mask));
331 return set_privileges( sid, &mask );
334 /*********************************************************************
335 Revoke all privileges
336 *********************************************************************/
338 bool revoke_all_privileges( struct dom_sid *sid )
340 return revoke_privilege( sid, SE_ALL_PRIVS);
343 /*********************************************************************
344 Add a privilege based on its name
345 *********************************************************************/
347 bool revoke_privilege_by_name(struct dom_sid *sid, const char *name)
351 if (! se_priv_from_name(name, &mask)) {
352 DEBUG(3, ("revoke_privilege_by_name: "
353 "No Such Privilege Found (%s)\n", name));
357 return revoke_privilege(sid, mask);
361 /***************************************************************************
362 Retrieve the SIDs assigned to a given privilege
363 ****************************************************************************/
365 NTSTATUS privilege_create_account(const struct dom_sid *sid )
367 return ( grant_privilege(sid, 0) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
370 /***************************************************************************
371 Delete a privileged account
372 ****************************************************************************/
374 NTSTATUS privilege_delete_account(const struct dom_sid *sid)
376 struct db_context *db = get_account_pol_db();
379 if (!lp_enable_privileges()) {
384 return NT_STATUS_INVALID_HANDLE;
387 if (!sid || (sid->num_auths == 0)) {
388 return NT_STATUS_INVALID_SID;
391 /* PRIV_<SID> (NULL terminated) as the key */
393 fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
395 return dbwrap_delete_bystring(db, keystr);
398 /****************************************************************************
399 initialise a privilege list and set the talloc context
400 ****************************************************************************/
402 NTSTATUS privilege_set_init(PRIVILEGE_SET *priv_set)
406 ZERO_STRUCTP( priv_set );
408 mem_ctx = talloc_init("privilege set");
410 DEBUG(0,("privilege_set_init: failed to initialize talloc ctx!\n"));
411 return NT_STATUS_NO_MEMORY;
414 priv_set->mem_ctx = mem_ctx;
419 /****************************************************************************
420 initialise a privilege list and with someone else's talloc context
421 ****************************************************************************/
423 NTSTATUS privilege_set_init_by_ctx(TALLOC_CTX *mem_ctx, PRIVILEGE_SET *priv_set)
425 ZERO_STRUCTP( priv_set );
427 priv_set->mem_ctx = mem_ctx;
428 priv_set->ext_ctx = True;
433 /****************************************************************************
434 Free all memory used by a PRIVILEGE_SET
435 ****************************************************************************/
437 void privilege_set_free(PRIVILEGE_SET *priv_set)
442 if ( !( priv_set->ext_ctx ) )
443 talloc_destroy( priv_set->mem_ctx );
445 ZERO_STRUCTP( priv_set );
448 /****************************************************************************
449 duplicate alloc luid_attr
450 ****************************************************************************/
452 NTSTATUS dup_luid_attr(TALLOC_CTX *mem_ctx, struct lsa_LUIDAttribute **new_la, struct lsa_LUIDAttribute *old_la, int count)
460 *new_la = TALLOC_ARRAY(mem_ctx, struct lsa_LUIDAttribute, count);
462 DEBUG(0,("dup_luid_attr: failed to alloc new struct lsa_LUIDAttribute array [%d]\n", count));
463 return NT_STATUS_NO_MEMORY;
469 for (i=0; i<count; i++) {
470 (*new_la)[i].luid.high = old_la[i].luid.high;
471 (*new_la)[i].luid.low = old_la[i].luid.low;
472 (*new_la)[i].attribute = old_la[i].attribute;
478 /*******************************************************************
479 *******************************************************************/
481 bool is_privileged_sid( const struct dom_sid *sid )
485 return get_privileges( sid, &mask );
488 /*******************************************************************
489 *******************************************************************/
491 bool grant_all_privileges( const struct dom_sid *sid )
495 if (!se_priv_put_all_privileges(&mask)) {
499 return grant_privilege( sid, mask );