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/>.
26 #include "popt_common.h"
27 #include "../libcli/security/security.h"
28 #include "../librpc/gen_ndr/ndr_security.h"
29 #include "passdb/machine_sid.h"
31 static TALLOC_CTX
*ctx
;
33 enum acl_mode
{ SMB_ACL_DELETE
,
48 /* These values discovered by inspection */
50 static const struct perm_value special_values
[] = {
51 { "R", SEC_RIGHTS_FILE_READ
},
52 { "W", SEC_RIGHTS_FILE_WRITE
},
53 { "X", SEC_RIGHTS_FILE_EXECUTE
},
54 { "D", SEC_STD_DELETE
},
55 { "P", SEC_STD_WRITE_DAC
},
56 { "O", SEC_STD_WRITE_OWNER
},
60 #define SEC_RIGHTS_DIR_CHANGE ( SEC_RIGHTS_DIR_READ|SEC_STD_DELETE|\
61 SEC_RIGHTS_DIR_WRITE|SEC_DIR_TRAVERSE )
63 static const struct perm_value standard_values
[] = {
64 { "READ", SEC_RIGHTS_DIR_READ
|SEC_DIR_TRAVERSE
},
65 { "CHANGE", SEC_RIGHTS_DIR_CHANGE
},
66 { "FULL", SEC_RIGHTS_DIR_ALL
},
70 /********************************************************************
71 parse an ACE in the same format as print_ace()
72 ********************************************************************/
74 static bool parse_ace(struct security_ace
*ace
, const char *orig_str
)
79 unsigned int atype
= 0;
80 unsigned int aflags
= 0;
81 unsigned int amask
= 0;
84 const struct perm_value
*v
;
85 char *str
= SMB_STRDUP(orig_str
);
86 TALLOC_CTX
*frame
= talloc_stackframe();
94 p
= strchr_m(str
,':');
96 fprintf(stderr
, "ACE '%s': missing ':'.\n", orig_str
);
103 /* Try to parse numeric form */
105 if (sscanf(p
, "%u/%u/%u", &atype
, &aflags
, &amask
) == 3 &&
106 string_to_sid(&sid
, str
)) {
110 /* Try to parse text form */
112 if (!string_to_sid(&sid
, str
)) {
113 fprintf(stderr
, "ACE '%s': failed to convert '%s' to SID\n",
121 if (!next_token_talloc(frame
, &cp
, &tok
, "/")) {
122 fprintf(stderr
, "ACE '%s': failed to find '/' character.\n",
129 if (strncmp(tok
, "ALLOWED", strlen("ALLOWED")) == 0) {
130 atype
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
131 } else if (strncmp(tok
, "DENIED", strlen("DENIED")) == 0) {
132 atype
= SEC_ACE_TYPE_ACCESS_DENIED
;
134 fprintf(stderr
, "ACE '%s': missing 'ALLOWED' or 'DENIED' "
135 "entry at '%s'\n", orig_str
, tok
);
141 /* Only numeric form accepted for flags at present */
142 /* no flags on share permissions */
144 if (!(next_token_talloc(frame
, &cp
, &tok
, "/") &&
145 sscanf(tok
, "%u", &aflags
) && aflags
== 0)) {
146 fprintf(stderr
, "ACE '%s': bad integer flags entry at '%s'\n",
153 if (!next_token_talloc(frame
, &cp
, &tok
, "/")) {
154 fprintf(stderr
, "ACE '%s': missing / at '%s'\n",
161 if (strncmp(tok
, "0x", 2) == 0) {
162 if (sscanf(tok
, "%u", &amask
) != 1) {
163 fprintf(stderr
, "ACE '%s': bad hex number at '%s'\n",
172 for (v
= standard_values
; v
->perm
; v
++) {
173 if (strcmp(tok
, v
->perm
) == 0) {
184 for (v
= special_values
; v
->perm
; v
++) {
185 if (v
->perm
[0] == *p
) {
192 fprintf(stderr
, "ACE '%s': bad permission value at "
193 "'%s'\n", orig_str
, p
);
209 init_sec_ace(ace
, &sid
, atype
, mask
, aflags
);
216 /********************************************************************
217 ********************************************************************/
219 static struct security_descriptor
* parse_acl_string(TALLOC_CTX
*mem_ctx
, const char *szACL
, size_t *sd_size
)
221 struct security_descriptor
*sd
= NULL
;
222 struct security_ace
*ace
;
223 struct security_acl
*theacl
;
232 num_ace
= count_chars( pacl
, ',' ) + 1;
234 if ( !(ace
= talloc_zero_array( mem_ctx
, struct security_ace
, num_ace
)) )
237 for ( i
=0; i
<num_ace
; i
++ ) {
238 char *end_acl
= strchr_m( pacl
, ',' );
241 strncpy( acl_string
, pacl
, MIN( PTR_DIFF( end_acl
, pacl
), sizeof(fstring
)-1) );
242 acl_string
[MIN( PTR_DIFF( end_acl
, pacl
), sizeof(fstring
)-1)] = '\0';
244 if ( !parse_ace( &ace
[i
], acl_string
) )
251 if ( !(theacl
= make_sec_acl( mem_ctx
, NT4_ACL_REVISION
, num_ace
, ace
)) )
254 sd
= make_sec_desc( mem_ctx
, SD_REVISION
, SEC_DESC_SELF_RELATIVE
,
255 NULL
, NULL
, NULL
, theacl
, sd_size
);
260 /* add an ACE to a list of ACEs in a struct security_acl */
261 static bool add_ace(TALLOC_CTX
*mem_ctx
, struct security_acl
**the_acl
, struct security_ace
*ace
)
263 struct security_acl
*new_ace
;
264 struct security_ace
*aces
;
266 return (((*the_acl
) = make_sec_acl(mem_ctx
, 3, 1, ace
)) != NULL
);
269 if (!(aces
= SMB_CALLOC_ARRAY(struct security_ace
, 1+(*the_acl
)->num_aces
))) {
272 memcpy(aces
, (*the_acl
)->aces
, (*the_acl
)->num_aces
* sizeof(struct
274 memcpy(aces
+(*the_acl
)->num_aces
, ace
, sizeof(struct security_ace
));
275 new_ace
= make_sec_acl(mem_ctx
,(*the_acl
)->revision
,1+(*the_acl
)->num_aces
, aces
);
277 (*the_acl
) = new_ace
;
281 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
282 However NT4 gives a "The information may have been modified by a
283 computer running Windows NT 5.0" if denied ACEs do not appear before
286 static int ace_compare(struct security_ace
*ace1
, struct security_ace
*ace2
)
288 if (security_ace_equal(ace1
, ace2
))
291 if (ace1
->type
!= ace2
->type
)
292 return ace2
->type
- ace1
->type
;
294 if (dom_sid_compare(&ace1
->trustee
, &ace2
->trustee
))
295 return dom_sid_compare(&ace1
->trustee
, &ace2
->trustee
);
297 if (ace1
->flags
!= ace2
->flags
)
298 return ace1
->flags
- ace2
->flags
;
300 if (ace1
->access_mask
!= ace2
->access_mask
)
301 return ace1
->access_mask
- ace2
->access_mask
;
303 if (ace1
->size
!= ace2
->size
)
304 return ace1
->size
- ace2
->size
;
306 return memcmp(ace1
, ace2
, sizeof(struct security_ace
));
309 static void sort_acl(struct security_acl
*the_acl
)
312 if (!the_acl
) return;
314 TYPESAFE_QSORT(the_acl
->aces
, the_acl
->num_aces
, ace_compare
);
316 for (i
=1;i
<the_acl
->num_aces
;) {
317 if (security_ace_equal(&the_acl
->aces
[i
-1],
318 &the_acl
->aces
[i
])) {
320 for (j
=i
; j
<the_acl
->num_aces
-1; j
++) {
321 the_acl
->aces
[j
] = the_acl
->aces
[j
+1];
331 static int change_share_sec(TALLOC_CTX
*mem_ctx
, const char *sharename
, char *the_acl
, enum acl_mode mode
)
333 struct security_descriptor
*sd
= NULL
;
334 struct security_descriptor
*old
= NULL
;
339 if (mode
!= SMB_ACL_SET
&& mode
!= SMB_SD_DELETE
) {
340 if (!(old
= get_share_security( mem_ctx
, sharename
, &sd_size
)) ) {
341 fprintf(stderr
, "Unable to retrieve permissions for share "
342 "[%s]\n", sharename
);
347 if ( (mode
!= SMB_ACL_VIEW
&& mode
!= SMB_SD_DELETE
) &&
348 !(sd
= parse_acl_string(mem_ctx
, the_acl
, &sd_size
)) ) {
349 fprintf( stderr
, "Failed to parse acl\n");
354 case SMB_ACL_VIEW_ALL
:
355 /* should not happen */
358 sd_str
= ndr_print_struct_string(mem_ctx
,
359 (ndr_print_fn_t
)ndr_print_security_descriptor
,
361 fprintf(stdout
, "%s\n", sd_str
);
365 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
368 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
369 if (security_ace_equal(&sd
->dacl
->aces
[i
],
370 &old
->dacl
->aces
[j
])) {
372 for (k
=j
; k
<old
->dacl
->num_aces
-1;k
++) {
373 old
->dacl
->aces
[k
] = old
->dacl
->aces
[k
+1];
375 old
->dacl
->num_aces
--;
382 sd_str
= ndr_print_struct_string(mem_ctx
,
383 (ndr_print_fn_t
)ndr_print_security_ace
,
384 "", &sd
->dacl
->aces
[i
]);
385 printf("ACL for ACE: %s not found\n", sd_str
);
391 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
394 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
395 if (dom_sid_equal(&sd
->dacl
->aces
[i
].trustee
,
396 &old
->dacl
->aces
[j
].trustee
)) {
397 old
->dacl
->aces
[j
] = sd
->dacl
->aces
[i
];
403 printf("ACL for SID %s not found\n",
404 sid_string_tos(&sd
->dacl
->aces
[i
].trustee
));
409 old
->owner_sid
= sd
->owner_sid
;
413 old
->group_sid
= sd
->group_sid
;
417 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
418 add_ace(mem_ctx
, &old
->dacl
, &sd
->dacl
->aces
[i
]);
425 if (!delete_share_security(sharename
)) {
426 fprintf( stderr
, "Failed to delete security descriptor for "
427 "share [%s]\n", sharename
);
432 fprintf(stderr
, "invalid command\n");
436 /* Denied ACE entries must come before allowed ones */
439 if ( !set_share_security( sharename
, old
) ) {
440 fprintf( stderr
, "Failed to store acl for share [%s]\n", sharename
);
446 static int set_sharesec_sddl(const char *sharename
, const char *sddl
)
448 struct security_descriptor
*sd
;
451 sd
= sddl_decode(talloc_tos(), sddl
, get_global_sam_sid());
453 fprintf(stderr
, "Failed to parse acl\n");
457 ret
= set_share_security(sharename
, sd
);
460 fprintf(stderr
, "Failed to store acl for share [%s]\n",
468 static int view_sharesec_sddl(const char *sharename
)
470 struct security_descriptor
*sd
;
474 sd
= get_share_security(talloc_tos(), sharename
, &sd_size
);
476 fprintf(stderr
, "Unable to retrieve permissions for share "
477 "[%s]\n", sharename
);
481 acl
= sddl_encode(talloc_tos(), sd
, get_global_sam_sid());
484 fprintf(stderr
, "Unable to sddl-encode permissions for share "
485 "[%s]\n", sharename
);
493 /********************************************************************
495 ********************************************************************/
501 int main(int argc
, const char *argv
[])
505 enum acl_mode mode
= SMB_ACL_SET
;
506 static char *the_acl
= NULL
;
508 bool force_acl
= False
;
511 bool initialize_sid
= False
;
512 struct poptOption long_options
[] = {
514 { "remove", 'r', POPT_ARG_STRING
, &the_acl
, 'r', "Remove ACEs", "ACL" },
515 { "modify", 'm', POPT_ARG_STRING
, &the_acl
, 'm', "Modify existing ACEs", "ACL" },
516 { "add", 'a', POPT_ARG_STRING
, &the_acl
, 'a', "Add ACEs", "ACL" },
517 { "replace", 'R', POPT_ARG_STRING
, &the_acl
, 'R', "Overwrite share permission ACL", "ACLS" },
518 { "delete", 'D', POPT_ARG_NONE
, NULL
, 'D', "Delete the entire security descriptor" },
519 { "setsddl", 'S', POPT_ARG_STRING
, the_acl
, 'S',
520 "Set the SD in sddl format" },
521 { "viewsddl", 'V', POPT_ARG_NONE
, the_acl
, 'V',
522 "View the SD in sddl format" },
523 { "view", 'v', POPT_ARG_NONE
, NULL
, 'v', "View current share permissions" },
524 { "view-all", 0, POPT_ARG_NONE
, NULL
, OPT_VIEW_ALL
,
525 "View all current share permissions" },
526 { "machine-sid", 'M', POPT_ARG_NONE
, NULL
, 'M', "Initialize the machine SID" },
527 { "force", 'F', POPT_ARG_NONE
, NULL
, 'F', "Force storing the ACL", "ACLS" },
532 if ( !(ctx
= talloc_stackframe()) ) {
533 fprintf( stderr
, "Failed to initialize talloc context!\n");
537 /* set default debug level to 1 regardless of what smb.conf sets */
538 setup_logging( "sharesec", DEBUG_STDERR
);
542 lp_set_cmdline("log level", "1");
544 pc
= poptGetContext("sharesec", argc
, argv
, long_options
, 0);
546 poptSetOtherOptionHelp(pc
, "sharename\n");
548 while ((opt
= poptGetNextOpt(pc
)) != -1) {
551 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
552 mode
= SMB_ACL_DELETE
;
556 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
557 mode
= SMB_ACL_MODIFY
;
561 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
566 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
571 mode
= SMB_SD_DELETE
;
575 mode
= SMB_SD_SETSDDL
;
576 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
580 mode
= SMB_SD_VIEWSDDL
;
592 initialize_sid
= True
;
595 mode
= SMB_ACL_VIEW_ALL
;
602 lp_load_with_registry_shares( get_dyn_CONFIGFILE(), False
, False
, False
,
605 /* check for initializing secrets.tdb first */
607 if ( initialize_sid
) {
608 struct dom_sid
*sid
= get_global_sam_sid();
611 fprintf( stderr
, "Failed to retrieve Machine SID!\n");
615 printf ("%s\n", sid_string_tos( sid
) );
619 if ( mode
== SMB_ACL_VIEW
&& force_acl
) {
620 fprintf( stderr
, "Invalid combination of -F and -v\n");
624 if (mode
== SMB_ACL_VIEW_ALL
) {
627 for (i
=0; i
<lp_numservices(); i
++) {
628 TALLOC_CTX
*frame
= talloc_stackframe();
629 const char *service
= lp_servicename(frame
, i
);
631 if (service
== NULL
) {
635 printf("[%s]\n", service
);
636 change_share_sec(frame
, service
, NULL
, SMB_ACL_VIEW
);
643 /* get the sharename */
645 if(!poptPeekArg(pc
)) {
646 poptPrintUsage(pc
, stderr
, 0);
650 fstrcpy(sharename
, poptGetArg(pc
));
652 snum
= lp_servicenumber( sharename
);
654 if ( snum
== -1 && !force_acl
) {
655 fprintf( stderr
, "Invalid sharename: %s\n", sharename
);
661 retval
= set_sharesec_sddl(sharename
, the_acl
);
663 case SMB_SD_VIEWSDDL
:
664 retval
= view_sharesec_sddl(sharename
);
667 retval
= change_share_sec(ctx
, sharename
, the_acl
, mode
);