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));
152 NTSTATUS get_privileges_for_sid_as_set(TALLOC_CTX *mem_ctx, PRIVILEGE_SET **privileges, struct dom_sid *sid)
155 if (!get_privileges(sid, &mask)) {
156 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
159 *privileges = talloc_zero(mem_ctx, PRIVILEGE_SET);
161 return NT_STATUS_NO_MEMORY;
164 if (!se_priv_to_privilege_set(*privileges, mask)) {
165 return NT_STATUS_NO_MEMORY;
170 /*********************************************************************
171 traversal functions for privilege_enumerate_accounts
172 *********************************************************************/
174 static int priv_traverse_fn(struct db_record *rec, void *state)
176 PRIV_SID_LIST *priv = (PRIV_SID_LIST *)state;
177 int prefixlen = strlen(PRIVPREFIX);
181 /* easy check first */
183 if (rec->value.dsize != sizeof(uint64_t) )
186 /* check we have a PRIV_+SID entry */
188 if ( strncmp((char *)rec->key.dptr, PRIVPREFIX, prefixlen) != 0)
191 /* check to see if we are looking for a particular privilege */
193 fstrcpy( sid_string, (char *)&(rec->key.dptr[strlen(PRIVPREFIX)]) );
195 if (priv->privilege != 0) {
198 if (rec->value.dsize == 4*4) {
199 DEBUG(3, ("get_privileges: Should not have obtained old-style privileges record for SID "
200 "[%s]\n", sid_string));
204 if (rec->value.dsize != sizeof( uint64_t ) ) {
205 DEBUG(3, ("get_privileges: Invalid privileges record assigned to SID "
206 "[%s]\n", sid_string));
210 mask = BVAL(rec->value.dptr, 0);
212 /* if the SID does not have the specified privilege
215 if ((mask & priv->privilege) == 0) {
220 /* this is a last ditch safety check to preventing returning
221 and invalid SID (i've somehow run into this on development branches) */
223 if ( strcmp( "S-0-0", sid_string ) == 0 )
226 if ( !string_to_sid(&sid, sid_string) ) {
227 DEBUG(0,("travsersal_fn_enum__acct: Could not convert SID [%s]\n",
232 if (!NT_STATUS_IS_OK(add_sid_to_array(priv->mem_ctx, &sid,
242 /*********************************************************************
243 Retreive list of privileged SIDs (for _lsa_enumerate_accounts()
244 *********************************************************************/
246 NTSTATUS privilege_enumerate_accounts(struct dom_sid **sids, int *num_sids)
248 struct db_context *db = get_account_pol_db();
252 return NT_STATUS_ACCESS_DENIED;
257 db->traverse_read(db, priv_traverse_fn, &priv);
259 /* give the memory away; caller will free */
261 *sids = priv.sids.list;
262 *num_sids = priv.sids.count;
267 /*********************************************************************
268 Retrieve list of SIDs granted a particular privilege
269 *********************************************************************/
271 NTSTATUS privilege_enum_sids(enum sec_privilege privilege, TALLOC_CTX *mem_ctx,
272 struct dom_sid **sids, int *num_sids)
274 struct db_context *db = get_account_pol_db();
278 return NT_STATUS_ACCESS_DENIED;
283 priv.privilege = sec_privilege_mask(privilege);
284 priv.mem_ctx = mem_ctx;
286 db->traverse_read(db, priv_traverse_fn, &priv);
288 /* give the memory away; caller will free */
290 *sids = priv.sids.list;
291 *num_sids = priv.sids.count;
296 /***************************************************************************
298 ****************************************************************************/
300 static bool grant_privilege_bitmap(const struct dom_sid *sid, const uint64_t priv_mask)
302 uint64_t old_mask, new_mask;
304 ZERO_STRUCT( old_mask );
305 ZERO_STRUCT( new_mask );
307 if ( get_privileges( sid, &old_mask ) )
312 new_mask |= priv_mask;
314 DEBUG(10,("grant_privilege: %s\n", sid_string_dbg(sid)));
316 DEBUGADD( 10, ("original privilege mask: 0x%llx\n", (unsigned long long)new_mask));
318 DEBUGADD( 10, ("new privilege mask: 0x%llx\n", (unsigned long long)new_mask));
320 return set_privileges( sid, &new_mask );
323 /*********************************************************************
324 Add a privilege based on its name
325 *********************************************************************/
327 bool grant_privilege_by_name(struct dom_sid *sid, const char *name)
331 if (! se_priv_from_name(name, &mask)) {
332 DEBUG(3, ("grant_privilege_by_name: "
333 "No Such Privilege Found (%s)\n", name));
337 return grant_privilege_bitmap( sid, mask );
340 /***************************************************************************
341 Grant a privilege set (list of LUID values) from a sid
342 ****************************************************************************/
344 bool grant_privilege_set(const struct dom_sid *sid, struct lsa_PrivilegeSet *set)
346 uint64_t privilege_mask;
347 if (!privilege_set_to_se_priv(&privilege_mask, set)) {
350 return grant_privilege_bitmap(sid, privilege_mask);
353 /***************************************************************************
354 Remove privilege from sid
355 ****************************************************************************/
357 static bool revoke_privilege_bitmap(const struct dom_sid *sid, const uint64_t priv_mask)
361 /* if the user has no privileges, then we can't revoke any */
363 if ( !get_privileges( sid, &mask ) )
366 DEBUG(10,("revoke_privilege: %s\n", sid_string_dbg(sid)));
368 DEBUGADD( 10, ("original privilege mask: 0x%llx\n", (unsigned long long)mask));
372 DEBUGADD( 10, ("new privilege mask: 0x%llx\n", (unsigned long long)mask));
374 return set_privileges( sid, &mask );
377 /***************************************************************************
378 Remove a privilege set (list of LUID values) from a sid
379 ****************************************************************************/
381 bool revoke_privilege_set(const struct dom_sid *sid, struct lsa_PrivilegeSet *set)
383 uint64_t privilege_mask;
384 if (!privilege_set_to_se_priv(&privilege_mask, set)) {
387 return revoke_privilege_bitmap(sid, privilege_mask);
390 /*********************************************************************
391 Revoke all privileges
392 *********************************************************************/
394 bool revoke_all_privileges( struct dom_sid *sid )
396 return revoke_privilege_bitmap( sid, SE_ALL_PRIVS);
399 /*********************************************************************
400 Add a privilege based on its name
401 *********************************************************************/
403 bool revoke_privilege_by_name(struct dom_sid *sid, const char *name)
407 if (! se_priv_from_name(name, &mask)) {
408 DEBUG(3, ("revoke_privilege_by_name: "
409 "No Such Privilege Found (%s)\n", name));
413 return revoke_privilege_bitmap(sid, mask);
417 /***************************************************************************
418 Retrieve the SIDs assigned to a given privilege
419 ****************************************************************************/
421 NTSTATUS privilege_create_account(const struct dom_sid *sid )
423 return ( grant_privilege_bitmap(sid, 0) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
426 /***************************************************************************
427 Delete a privileged account
428 ****************************************************************************/
430 NTSTATUS privilege_delete_account(const struct dom_sid *sid)
432 struct db_context *db = get_account_pol_db();
435 if (!lp_enable_privileges()) {
440 return NT_STATUS_INVALID_HANDLE;
443 if (!sid || (sid->num_auths == 0)) {
444 return NT_STATUS_INVALID_SID;
447 /* PRIV_<SID> (NULL terminated) as the key */
449 fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
451 return dbwrap_delete_bystring(db, keystr);
454 /*******************************************************************
455 *******************************************************************/
457 bool is_privileged_sid( const struct dom_sid *sid )
461 return get_privileges( sid, &mask );
464 /*******************************************************************
465 *******************************************************************/
467 bool grant_all_privileges( const struct dom_sid *sid )
471 if (!se_priv_put_all_privileges(&mask)) {
475 return grant_privilege_bitmap( sid, mask );