lib: talloc: Fix memlimit on pool realloc.
[Samba.git] / source3 / utils / sharesec.c
blob97ff0cfc9c5e7923248b07934885174b10e07314
1 /*
2 * Unix SMB/Netbios implementation.
3 * Utility for managing share permissions
5 * Copyright (C) Tim Potter 2000
6 * Copyright (C) Jeremy Allison 2000
7 * Copyright (C) Jelmer Vernooij 2003
8 * Copyright (C) Gerald (Jerry) Carter 2005.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 struct cli_state;
26 #include "includes.h"
27 #include "popt_common.h"
28 #include "../libcli/security/security.h"
29 #include "passdb/machine_sid.h"
30 #include "util_sd.h"
31 #include "cmdline_contexts.h"
32 #include "lib/util/string_wrappers.h"
34 static TALLOC_CTX *ctx;
36 enum acl_mode { SMB_ACL_DELETE,
37 SMB_ACL_MODIFY,
38 SMB_ACL_ADD,
39 SMB_ACL_SET,
40 SMB_SD_DELETE,
41 SMB_SD_SETSDDL,
42 SMB_SD_VIEWSDDL,
43 SMB_ACL_VIEW,
44 SMB_ACL_VIEW_ALL };
46 /********************************************************************
47 ********************************************************************/
49 static struct security_descriptor* parse_acl_string(TALLOC_CTX *mem_ctx, const char *szACL, size_t *sd_size )
51 struct security_descriptor *sd = NULL;
52 struct security_ace *ace;
53 struct security_acl *theacl;
54 int num_ace;
55 const char *pacl;
56 int i;
58 if ( !szACL )
59 return NULL;
61 pacl = szACL;
62 num_ace = count_chars( pacl, ',' ) + 1;
64 if ( !(ace = talloc_zero_array( mem_ctx, struct security_ace, num_ace )) )
65 return NULL;
67 for ( i=0; i<num_ace; i++ ) {
68 char *end_acl = strchr_m( pacl, ',' );
69 fstring acl_string;
71 strncpy( acl_string, pacl, MIN( PTR_DIFF( end_acl, pacl ), sizeof(fstring)-1) );
72 acl_string[MIN( PTR_DIFF( end_acl, pacl ), sizeof(fstring)-1)] = '\0';
74 if ( !parse_ace(NULL, &ace[i], acl_string ) )
75 return NULL;
77 pacl = end_acl;
78 pacl++;
81 if ( !(theacl = make_sec_acl( mem_ctx, NT4_ACL_REVISION, num_ace, ace )) )
82 return NULL;
84 sd = make_sec_desc( mem_ctx, SD_REVISION, SEC_DESC_SELF_RELATIVE,
85 NULL, NULL, NULL, theacl, sd_size);
87 return sd;
90 /* add an ACE to a list of ACEs in a struct security_acl */
91 static bool add_ace(TALLOC_CTX *mem_ctx, struct security_acl **the_acl, struct security_ace *ace)
93 struct security_acl *new_ace;
94 struct security_ace *aces;
95 if (! *the_acl) {
96 return (((*the_acl) = make_sec_acl(mem_ctx, 3, 1, ace)) != NULL);
99 if (!(aces = SMB_CALLOC_ARRAY(struct security_ace, 1+(*the_acl)->num_aces))) {
100 return False;
102 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(struct
103 security_ace));
104 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(struct security_ace));
105 new_ace = make_sec_acl(mem_ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
106 SAFE_FREE(aces);
107 (*the_acl) = new_ace;
108 return True;
111 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
112 However NT4 gives a "The information may have been modified by a
113 computer running Windows NT 5.0" if denied ACEs do not appear before
114 allowed ACEs. */
116 static int ace_compare(struct security_ace *ace1, struct security_ace *ace2)
118 if (security_ace_equal(ace1, ace2))
119 return 0;
121 if (ace1->type != ace2->type)
122 return ace2->type - ace1->type;
124 if (dom_sid_compare(&ace1->trustee, &ace2->trustee))
125 return dom_sid_compare(&ace1->trustee, &ace2->trustee);
127 if (ace1->flags != ace2->flags)
128 return ace1->flags - ace2->flags;
130 if (ace1->access_mask != ace2->access_mask)
131 return ace1->access_mask - ace2->access_mask;
133 if (ace1->size != ace2->size)
134 return ace1->size - ace2->size;
136 return memcmp(ace1, ace2, sizeof(struct security_ace));
139 static void sort_acl(struct security_acl *the_acl)
141 uint32_t i;
142 if (!the_acl) return;
144 TYPESAFE_QSORT(the_acl->aces, the_acl->num_aces, ace_compare);
146 for (i=1;i<the_acl->num_aces;) {
147 if (security_ace_equal(&the_acl->aces[i-1],
148 &the_acl->aces[i])) {
149 ARRAY_DEL_ELEMENT(
150 the_acl->aces, i, the_acl->num_aces);
151 the_acl->num_aces--;
152 } else {
153 i++;
159 static int change_share_sec(TALLOC_CTX *mem_ctx, const char *sharename, char *the_acl, enum acl_mode mode)
161 struct security_descriptor *sd = NULL;
162 struct security_descriptor *old = NULL;
163 size_t sd_size = 0;
164 uint32_t i, j;
165 NTSTATUS status;
167 if (mode != SMB_ACL_SET && mode != SMB_SD_DELETE) {
168 if (!(old = get_share_security( mem_ctx, sharename, &sd_size )) ) {
169 fprintf(stderr, "Unable to retrieve permissions for share "
170 "[%s]\n", sharename);
171 return -1;
175 if ( (mode != SMB_ACL_VIEW && mode != SMB_SD_DELETE) &&
176 !(sd = parse_acl_string(mem_ctx, the_acl, &sd_size )) ) {
177 fprintf( stderr, "Failed to parse acl\n");
178 return -1;
181 switch (mode) {
182 case SMB_ACL_VIEW_ALL:
183 /* should not happen */
184 return 0;
185 case SMB_ACL_VIEW:
186 sec_desc_print(NULL, stdout, old, false);
187 return 0;
188 case SMB_ACL_DELETE:
189 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
190 bool found = False;
192 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
193 if (security_ace_equal(&sd->dacl->aces[i],
194 &old->dacl->aces[j])) {
195 uint32_t k;
196 for (k=j; k<old->dacl->num_aces-1;k++) {
197 old->dacl->aces[k] = old->dacl->aces[k+1];
199 old->dacl->num_aces--;
200 found = True;
201 break;
205 if (!found) {
206 printf("ACL for ACE:");
207 print_ace(NULL, stdout, &sd->dacl->aces[i], false);
208 printf(" not found\n");
211 break;
212 case SMB_ACL_MODIFY:
213 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
214 bool found = False;
216 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
217 if (dom_sid_equal(&sd->dacl->aces[i].trustee,
218 &old->dacl->aces[j].trustee)) {
219 old->dacl->aces[j] = sd->dacl->aces[i];
220 found = True;
224 if (!found) {
225 struct dom_sid_buf buf;
226 printf("ACL for SID %s not found\n",
227 dom_sid_str_buf(&sd->dacl->aces[i].trustee, &buf));
231 if (sd->owner_sid) {
232 old->owner_sid = sd->owner_sid;
235 if (sd->group_sid) {
236 old->group_sid = sd->group_sid;
238 break;
239 case SMB_ACL_ADD:
240 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
241 add_ace(mem_ctx, &old->dacl, &sd->dacl->aces[i]);
243 break;
244 case SMB_ACL_SET:
245 old = sd;
246 break;
247 case SMB_SD_DELETE:
248 status = delete_share_security(sharename);
249 if (!NT_STATUS_IS_OK(status)) {
250 fprintf( stderr, "Failed to delete security descriptor for "
251 "share [%s]\n", sharename );
252 return -1;
254 return 0;
255 default:
256 fprintf(stderr, "invalid command\n");
257 return -1;
260 /* Denied ACE entries must come before allowed ones */
261 sort_acl(old->dacl);
263 status = set_share_security(sharename, old);
264 if (!NT_STATUS_IS_OK(status)) {
265 fprintf( stderr, "Failed to store acl for share [%s]\n", sharename );
266 return 2;
268 return 0;
271 static int set_sharesec_sddl(const char *sharename, const char *sddl)
273 struct security_descriptor *sd;
274 NTSTATUS status;
276 sd = sddl_decode(talloc_tos(), sddl, get_global_sam_sid());
277 if (sd == NULL) {
278 fprintf(stderr, "Failed to parse acl\n");
279 return -1;
282 status = set_share_security(sharename, sd);
283 TALLOC_FREE(sd);
284 if (!NT_STATUS_IS_OK(status)) {
285 fprintf(stderr, "Failed to store acl for share [%s]\n",
286 sharename);
287 return -1;
290 return 0;
293 static int view_sharesec_sddl(const char *sharename)
295 struct security_descriptor *sd;
296 size_t sd_size;
297 char *acl;
299 sd = get_share_security(talloc_tos(), sharename, &sd_size);
300 if (sd == NULL) {
301 fprintf(stderr, "Unable to retrieve permissions for share "
302 "[%s]\n", sharename);
303 return -1;
306 acl = sddl_encode(talloc_tos(), sd, get_global_sam_sid());
307 TALLOC_FREE(sd);
308 if (acl == NULL) {
309 fprintf(stderr, "Unable to sddl-encode permissions for share "
310 "[%s]\n", sharename);
311 return -1;
313 printf("%s\n", acl);
314 TALLOC_FREE(acl);
315 return 0;
318 /********************************************************************
319 main program
320 ********************************************************************/
322 enum {
323 OPT_VIEW_ALL = 1000,
326 int main(int argc, const char *argv[])
328 int opt;
329 int retval = 0;
330 enum acl_mode mode = SMB_ACL_SET;
331 static char *the_acl = NULL;
332 fstring sharename;
333 bool force_acl = False;
334 int snum;
335 poptContext pc;
336 bool initialize_sid = False;
337 struct poptOption long_options[] = {
338 POPT_AUTOHELP
340 .longName = "remove",
341 .shortName = 'r',
342 .argInfo = POPT_ARG_STRING,
343 .arg = &the_acl,
344 .val = 'r',
345 .descrip = "Remove ACEs",
346 .argDescrip = "ACL",
349 .longName = "modify",
350 .shortName = 'm',
351 .argInfo = POPT_ARG_STRING,
352 .arg = &the_acl,
353 .val = 'm',
354 .descrip = "Modify existing ACEs",
355 .argDescrip = "ACL",
358 .longName = "add",
359 .shortName = 'a',
360 .argInfo = POPT_ARG_STRING,
361 .arg = &the_acl,
362 .val = 'a',
363 .descrip = "Add ACEs",
364 .argDescrip = "ACL",
367 .longName = "replace",
368 .shortName = 'R',
369 .argInfo = POPT_ARG_STRING,
370 .arg = &the_acl,
371 .val = 'R',
372 .descrip = "Overwrite share permission ACL",
373 .argDescrip = "ACLS",
376 .longName = "delete",
377 .shortName = 'D',
378 .argInfo = POPT_ARG_NONE,
379 .arg = NULL,
380 .val = 'D',
381 .descrip = "Delete the entire security descriptor",
384 .longName = "setsddl",
385 .shortName = 'S',
386 .argInfo = POPT_ARG_STRING,
387 .arg = the_acl,
388 .val = 'S',
389 .descrip = "Set the SD in sddl format",
392 .longName = "viewsddl",
393 .shortName = 'V',
394 .argInfo = POPT_ARG_NONE,
395 .arg = the_acl,
396 .val = 'V',
397 .descrip = "View the SD in sddl format",
400 .longName = "view",
401 .shortName = 'v',
402 .argInfo = POPT_ARG_NONE,
403 .arg = NULL,
404 .val = 'v',
405 .descrip = "View current share permissions",
408 .longName = "view-all",
409 .shortName = 0,
410 .argInfo = POPT_ARG_NONE,
411 .arg = NULL,
412 .val = OPT_VIEW_ALL,
413 .descrip = "View all current share permissions",
416 .longName = "machine-sid",
417 .shortName = 'M',
418 .argInfo = POPT_ARG_NONE,
419 .arg = NULL,
420 .val = 'M',
421 .descrip = "Initialize the machine SID",
424 .longName = "force",
425 .shortName = 'F',
426 .argInfo = POPT_ARG_NONE,
427 .arg = NULL,
428 .val = 'F',
429 .descrip = "Force storing the ACL",
430 .argDescrip = "ACLS",
432 POPT_COMMON_SAMBA
433 POPT_TABLEEND
436 if ( !(ctx = talloc_stackframe()) ) {
437 fprintf( stderr, "Failed to initialize talloc context!\n");
438 return -1;
441 /* set default debug level to 1 regardless of what smb.conf sets */
442 setup_logging( "sharesec", DEBUG_STDERR);
444 smb_init_locale();
446 lp_set_cmdline("log level", "1");
448 pc = poptGetContext("sharesec", argc, argv, long_options, 0);
450 poptSetOtherOptionHelp(pc, "sharename\n");
452 while ((opt = poptGetNextOpt(pc)) != -1) {
453 switch (opt) {
454 case 'r':
455 the_acl = smb_xstrdup(poptGetOptArg(pc));
456 mode = SMB_ACL_DELETE;
457 break;
459 case 'm':
460 the_acl = smb_xstrdup(poptGetOptArg(pc));
461 mode = SMB_ACL_MODIFY;
462 break;
464 case 'a':
465 the_acl = smb_xstrdup(poptGetOptArg(pc));
466 mode = SMB_ACL_ADD;
467 break;
469 case 'R':
470 the_acl = smb_xstrdup(poptGetOptArg(pc));
471 mode = SMB_ACL_SET;
472 break;
474 case 'D':
475 mode = SMB_SD_DELETE;
476 break;
478 case 'S':
479 mode = SMB_SD_SETSDDL;
480 the_acl = smb_xstrdup(poptGetOptArg(pc));
481 break;
483 case 'V':
484 mode = SMB_SD_VIEWSDDL;
485 break;
487 case 'v':
488 mode = SMB_ACL_VIEW;
489 break;
491 case 'F':
492 force_acl = True;
493 break;
495 case 'M':
496 initialize_sid = True;
497 break;
498 case OPT_VIEW_ALL:
499 mode = SMB_ACL_VIEW_ALL;
500 break;
504 setlinebuf(stdout);
506 lp_load_with_registry_shares(get_dyn_CONFIGFILE());
508 /* check for initializing secrets.tdb first */
510 if ( initialize_sid ) {
511 struct dom_sid *sid = get_global_sam_sid();
512 struct dom_sid_buf buf;
514 if ( !sid ) {
515 fprintf( stderr, "Failed to retrieve Machine SID!\n");
516 retval = 3;
517 goto done;
520 printf ("%s\n", dom_sid_str_buf(sid, &buf) );
521 retval = 0;
522 goto done;
525 if ( mode == SMB_ACL_VIEW && force_acl ) {
526 fprintf( stderr, "Invalid combination of -F and -v\n");
527 retval = -1;
528 goto done;
531 if (mode == SMB_ACL_VIEW_ALL) {
532 int i;
534 for (i=0; i<lp_numservices(); i++) {
535 TALLOC_CTX *frame = talloc_stackframe();
536 const struct loadparm_substitution *lp_sub =
537 loadparm_s3_global_substitution();
538 const char *service = lp_servicename(frame, lp_sub, i);
540 if (service == NULL) {
541 continue;
544 printf("[%s]\n", service);
545 change_share_sec(frame, service, NULL, SMB_ACL_VIEW);
546 printf("\n");
547 TALLOC_FREE(frame);
549 goto done;
552 /* get the sharename */
554 if(!poptPeekArg(pc)) {
555 poptPrintUsage(pc, stderr, 0);
556 retval = -1;
557 goto done;
560 fstrcpy(sharename, poptGetArg(pc));
562 snum = lp_servicenumber( sharename );
564 if ( snum == -1 && !force_acl ) {
565 fprintf( stderr, "Invalid sharename: %s\n", sharename);
566 retval = -1;
567 goto done;
570 switch (mode) {
571 case SMB_SD_SETSDDL:
572 retval = set_sharesec_sddl(sharename, the_acl);
573 break;
574 case SMB_SD_VIEWSDDL:
575 retval = view_sharesec_sddl(sharename);
576 break;
577 default:
578 retval = change_share_sec(ctx, sharename, the_acl, mode);
579 break;
582 done:
583 poptFreeContext(pc);
584 talloc_destroy(ctx);
586 return retval;