source: vendor/3.6.23/source3/lib/privileges.c

Last change on this file was 740, checked in by Silvan Scherrer, 13 years ago

Samba Server: update vendor to 3.6.0

File size: 13.4 KB
Line 
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
8
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.
13
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.
18
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/>.
21*/
22
23
24#include "includes.h"
25#include "lib/privileges.h"
26#include "dbwrap.h"
27#include "libcli/security/privileges_private.h"
28#include "../libcli/security/security.h"
29#include "passdb.h"
30
31#define PRIVPREFIX "PRIV_"
32
33typedef struct {
34 uint32_t count;
35 struct dom_sid *list;
36} SID_LIST;
37
38typedef struct {
39 TALLOC_CTX *mem_ctx;
40 uint64_t privilege;
41 SID_LIST sids;
42} PRIV_SID_LIST;
43
44/*
45 interpret an old style SE_PRIV structure
46 */
47static uint64_t map_old_SE_PRIV(unsigned char *dptr)
48{
49 uint32_t *old_masks = (uint32_t *)dptr;
50 /*
51 * the old privileges code only ever used up to 0x800, except
52 * for a special case of 'SE_ALL_PRIVS' which was 0xFFFFFFFF
53 */
54 if (old_masks[0] == 0xFFFFFFFF) {
55 /* they set all privileges */
56 return SE_ALL_PRIVS;
57 }
58
59 /* the old code used the machine byte order, but we don't know
60 * the byte order of the machine that wrote it. However we can
61 * tell what byte order it was by taking advantage of the fact
62 * that it only ever use up to 0x800
63 */
64 if (dptr[0] || dptr[1]) {
65 /* it was little endian */
66 return IVAL(dptr, 0);
67 }
68
69 /* it was either zero or big-endian */
70 return RIVAL(dptr, 0);
71}
72
73
74static bool get_privileges( const struct dom_sid *sid, uint64_t *mask )
75{
76 struct db_context *db = get_account_pol_db();
77 fstring tmp, keystr;
78 TDB_DATA data;
79
80 /* Fail if the admin has not enable privileges */
81
82 if ( !lp_enable_privileges() ) {
83 return False;
84 }
85
86 if ( db == NULL )
87 return False;
88
89 /* PRIV_<SID> (NULL terminated) as the key */
90
91 fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
92
93 data = dbwrap_fetch_bystring( db, talloc_tos(), keystr );
94
95 if ( !data.dptr ) {
96 DEBUG(4, ("get_privileges: No privileges assigned to SID "
97 "[%s]\n", sid_string_dbg(sid)));
98 return False;
99 }
100
101 if (data.dsize == 4*4) {
102 /* it's an old style SE_PRIV structure. */
103 *mask = map_old_SE_PRIV(data.dptr);
104 } else {
105 if (data.dsize != sizeof( uint64_t ) ) {
106 DEBUG(3, ("get_privileges: Invalid privileges record assigned to SID "
107 "[%s]\n", sid_string_dbg(sid)));
108 return False;
109 }
110
111 *mask = BVAL(data.dptr, 0);
112 }
113
114 TALLOC_FREE(data.dptr);
115
116 return True;
117}
118
119/***************************************************************************
120 Store the privilege mask (set) for a given SID
121****************************************************************************/
122
123static bool set_privileges( const struct dom_sid *sid, uint64_t mask )
124{
125 struct db_context *db = get_account_pol_db();
126 uint8_t privbuf[8];
127 fstring tmp, keystr;
128 TDB_DATA data;
129
130 if ( !lp_enable_privileges() )
131 return False;
132
133 if ( db == NULL )
134 return False;
135
136 if ( !sid || (sid->num_auths == 0) ) {
137 DEBUG(0,("set_privileges: Refusing to store empty SID!\n"));
138 return False;
139 }
140
141 /* PRIV_<SID> (NULL terminated) as the key */
142
143 fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
144
145 /* This writes the 64 bit bitmask out in little endian format */
146 SBVAL(privbuf,0,mask);
147
148 data.dptr = privbuf;
149 data.dsize = sizeof(privbuf);
150
151 return NT_STATUS_IS_OK(dbwrap_store_bystring(db, keystr, data,
152 TDB_REPLACE));
153}
154
155/*********************************************************************
156 get a list of all privileges for all sids in the list
157*********************************************************************/
158
159bool get_privileges_for_sids(uint64_t *privileges, struct dom_sid *slist, int scount)
160{
161 uint64_t mask;
162 int i;
163 bool found = False;
164
165 *privileges = 0;
166
167 for ( i=0; i<scount; i++ ) {
168 /* don't add unless we actually have a privilege assigned */
169
170 if ( !get_privileges( &slist[i], &mask ) )
171 continue;
172
173 DEBUG(5,("get_privileges_for_sids: sid = %s\nPrivilege "
174 "set: 0x%llx\n", sid_string_dbg(&slist[i]),
175 (unsigned long long)mask));
176
177 *privileges |= mask;
178 found = True;
179 }
180
181 return found;
182}
183
184NTSTATUS get_privileges_for_sid_as_set(TALLOC_CTX *mem_ctx, PRIVILEGE_SET **privileges, struct dom_sid *sid)
185{
186 uint64_t mask;
187 if (!get_privileges(sid, &mask)) {
188 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
189 }
190
191 *privileges = talloc_zero(mem_ctx, PRIVILEGE_SET);
192 if (!*privileges) {
193 return NT_STATUS_NO_MEMORY;
194 }
195
196 if (!se_priv_to_privilege_set(*privileges, mask)) {
197 return NT_STATUS_NO_MEMORY;
198 }
199 return NT_STATUS_OK;
200}
201
202/*********************************************************************
203 traversal functions for privilege_enumerate_accounts
204*********************************************************************/
205
206static int priv_traverse_fn(struct db_record *rec, void *state)
207{
208 PRIV_SID_LIST *priv = (PRIV_SID_LIST *)state;
209 int prefixlen = strlen(PRIVPREFIX);
210 struct dom_sid sid;
211 fstring sid_string;
212
213 /* check we have a PRIV_+SID entry */
214
215 if ( strncmp((char *)rec->key.dptr, PRIVPREFIX, prefixlen) != 0)
216 return 0;
217
218 /* check to see if we are looking for a particular privilege */
219
220 fstrcpy( sid_string, (char *)&(rec->key.dptr[strlen(PRIVPREFIX)]) );
221
222 if (priv->privilege != 0) {
223 uint64_t mask;
224
225 if (rec->value.dsize == 4*4) {
226 mask = map_old_SE_PRIV(rec->value.dptr);
227 } else {
228 if (rec->value.dsize != sizeof( uint64_t ) ) {
229 DEBUG(3, ("get_privileges: Invalid privileges record assigned to SID "
230 "[%s]\n", sid_string));
231 return 0;
232 }
233 mask = BVAL(rec->value.dptr, 0);
234 }
235
236 /* if the SID does not have the specified privilege
237 then just return */
238
239 if ((mask & priv->privilege) == 0) {
240 return 0;
241 }
242 }
243
244 /* this is a last ditch safety check to preventing returning
245 and invalid SID (i've somehow run into this on development branches) */
246
247 if ( strcmp( "S-0-0", sid_string ) == 0 )
248 return 0;
249
250 if ( !string_to_sid(&sid, sid_string) ) {
251 DEBUG(0,("travsersal_fn_enum__acct: Could not convert SID [%s]\n",
252 sid_string));
253 return 0;
254 }
255
256 if (!NT_STATUS_IS_OK(add_sid_to_array(priv->mem_ctx, &sid,
257 &priv->sids.list,
258 &priv->sids.count)))
259 {
260 return 0;
261 }
262
263 return 0;
264}
265
266/*********************************************************************
267 Retreive list of privileged SIDs (for _lsa_enumerate_accounts()
268*********************************************************************/
269
270NTSTATUS privilege_enumerate_accounts(struct dom_sid **sids, int *num_sids)
271{
272 struct db_context *db = get_account_pol_db();
273 PRIV_SID_LIST priv;
274
275 if (db == NULL) {
276 return NT_STATUS_ACCESS_DENIED;
277 }
278
279 ZERO_STRUCT(priv);
280
281 db->traverse_read(db, priv_traverse_fn, &priv);
282
283 /* give the memory away; caller will free */
284
285 *sids = priv.sids.list;
286 *num_sids = priv.sids.count;
287
288 return NT_STATUS_OK;
289}
290
291/*********************************************************************
292 Retrieve list of SIDs granted a particular privilege
293*********************************************************************/
294
295NTSTATUS privilege_enum_sids(enum sec_privilege privilege, TALLOC_CTX *mem_ctx,
296 struct dom_sid **sids, int *num_sids)
297{
298 struct db_context *db = get_account_pol_db();
299 PRIV_SID_LIST priv;
300
301 if (db == NULL) {
302 return NT_STATUS_ACCESS_DENIED;
303 }
304
305 ZERO_STRUCT(priv);
306
307 priv.privilege = sec_privilege_mask(privilege);
308 priv.mem_ctx = mem_ctx;
309
310 db->traverse_read(db, priv_traverse_fn, &priv);
311
312 /* give the memory away; caller will free */
313
314 *sids = priv.sids.list;
315 *num_sids = priv.sids.count;
316
317 return NT_STATUS_OK;
318}
319
320/***************************************************************************
321 Add privilege to sid
322****************************************************************************/
323
324static bool grant_privilege_bitmap(const struct dom_sid *sid, const uint64_t priv_mask)
325{
326 uint64_t old_mask, new_mask;
327
328 ZERO_STRUCT( old_mask );
329 ZERO_STRUCT( new_mask );
330
331 if ( get_privileges( sid, &old_mask ) )
332 new_mask = old_mask;
333 else
334 new_mask = 0;
335
336 new_mask |= priv_mask;
337
338 DEBUG(10,("grant_privilege: %s\n", sid_string_dbg(sid)));
339
340 DEBUGADD( 10, ("original privilege mask: 0x%llx\n", (unsigned long long)new_mask));
341
342 DEBUGADD( 10, ("new privilege mask: 0x%llx\n", (unsigned long long)new_mask));
343
344 return set_privileges( sid, new_mask );
345}
346
347/*********************************************************************
348 Add a privilege based on its name
349*********************************************************************/
350
351bool grant_privilege_by_name(const struct dom_sid *sid, const char *name)
352{
353 uint64_t mask;
354
355 if (! se_priv_from_name(name, &mask)) {
356 DEBUG(3, ("grant_privilege_by_name: "
357 "No Such Privilege Found (%s)\n", name));
358 return False;
359 }
360
361 return grant_privilege_bitmap( sid, mask );
362}
363
364/***************************************************************************
365 Grant a privilege set (list of LUID values) from a sid
366****************************************************************************/
367
368bool grant_privilege_set(const struct dom_sid *sid, struct lsa_PrivilegeSet *set)
369{
370 uint64_t privilege_mask;
371 if (!privilege_set_to_se_priv(&privilege_mask, set)) {
372 return false;
373 }
374 return grant_privilege_bitmap(sid, privilege_mask);
375}
376
377/***************************************************************************
378 Remove privilege from sid
379****************************************************************************/
380
381static bool revoke_privilege_bitmap(const struct dom_sid *sid, const uint64_t priv_mask)
382{
383 uint64_t mask;
384
385 /* if the user has no privileges, then we can't revoke any */
386
387 if ( !get_privileges( sid, &mask ) )
388 return True;
389
390 DEBUG(10,("revoke_privilege: %s\n", sid_string_dbg(sid)));
391
392 DEBUGADD( 10, ("original privilege mask: 0x%llx\n", (unsigned long long)mask));
393
394 mask &= ~priv_mask;
395
396 DEBUGADD( 10, ("new privilege mask: 0x%llx\n", (unsigned long long)mask));
397
398 return set_privileges( sid, mask );
399}
400
401/***************************************************************************
402 Remove a privilege set (list of LUID values) from a sid
403****************************************************************************/
404
405bool revoke_privilege_set(const struct dom_sid *sid, struct lsa_PrivilegeSet *set)
406{
407 uint64_t privilege_mask;
408 if (!privilege_set_to_se_priv(&privilege_mask, set)) {
409 return false;
410 }
411 return revoke_privilege_bitmap(sid, privilege_mask);
412}
413
414/*********************************************************************
415 Revoke all privileges
416*********************************************************************/
417
418bool revoke_all_privileges( const struct dom_sid *sid )
419{
420 return revoke_privilege_bitmap( sid, SE_ALL_PRIVS);
421}
422
423/*********************************************************************
424 Add a privilege based on its name
425*********************************************************************/
426
427bool revoke_privilege_by_name(const struct dom_sid *sid, const char *name)
428{
429 uint64_t mask;
430
431 if (! se_priv_from_name(name, &mask)) {
432 DEBUG(3, ("revoke_privilege_by_name: "
433 "No Such Privilege Found (%s)\n", name));
434 return False;
435 }
436
437 return revoke_privilege_bitmap(sid, mask);
438
439}
440
441/***************************************************************************
442 Retrieve the SIDs assigned to a given privilege
443****************************************************************************/
444
445NTSTATUS privilege_create_account(const struct dom_sid *sid )
446{
447 return ( grant_privilege_bitmap(sid, 0) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
448}
449
450/***************************************************************************
451 Delete a privileged account
452****************************************************************************/
453
454NTSTATUS privilege_delete_account(const struct dom_sid *sid)
455{
456 struct db_context *db = get_account_pol_db();
457 fstring tmp, keystr;
458
459 if (!lp_enable_privileges()) {
460 return NT_STATUS_OK;
461 }
462
463 if (!db) {
464 return NT_STATUS_INVALID_HANDLE;
465 }
466
467 if (!sid || (sid->num_auths == 0)) {
468 return NT_STATUS_INVALID_SID;
469 }
470
471 /* PRIV_<SID> (NULL terminated) as the key */
472
473 fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
474
475 return dbwrap_delete_bystring(db, keystr);
476}
477
478/*******************************************************************
479*******************************************************************/
480
481bool is_privileged_sid( const struct dom_sid *sid )
482{
483 uint64_t mask;
484
485 return get_privileges( sid, &mask );
486}
487
488/*******************************************************************
489*******************************************************************/
490
491bool grant_all_privileges( const struct dom_sid *sid )
492{
493 uint64_t mask;
494
495 se_priv_put_all_privileges(&mask);
496
497 return grant_privilege_bitmap( sid, mask );
498}
Note: See TracBrowser for help on using the repository browser.