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/>.
27 static TALLOC_CTX
*ctx
;
29 enum acl_mode
{ SMB_ACL_DELETE
,
41 /* These values discovered by inspection */
43 static const struct perm_value special_values
[] = {
44 { "R", SEC_RIGHTS_FILE_READ
},
45 { "W", SEC_RIGHTS_FILE_WRITE
},
46 { "X", SEC_RIGHTS_FILE_EXECUTE
},
47 { "D", SEC_STD_DELETE
},
48 { "P", SEC_STD_WRITE_DAC
},
49 { "O", SEC_STD_WRITE_OWNER
},
53 #define SEC_RIGHTS_DIR_CHANGE ( SEC_RIGHTS_DIR_READ|SEC_STD_DELETE|\
54 SEC_RIGHTS_DIR_WRITE|SEC_DIR_TRAVERSE )
56 static const struct perm_value standard_values
[] = {
57 { "READ", SEC_RIGHTS_DIR_READ
|SEC_DIR_TRAVERSE
},
58 { "CHANGE", SEC_RIGHTS_DIR_CHANGE
},
59 { "FULL", SEC_RIGHTS_DIR_ALL
},
63 /********************************************************************
64 print an ACE on a FILE
65 ********************************************************************/
67 static void print_ace(FILE *f
, struct security_ace
*ace
)
69 const struct perm_value
*v
;
73 fprintf(f
, "%s:", sid_string_tos(&ace
->trustee
));
77 if (ace
->type
== SEC_ACE_TYPE_ACCESS_ALLOWED
) {
78 fprintf(f
, "ALLOWED");
79 } else if (ace
->type
== SEC_ACE_TYPE_ACCESS_DENIED
) {
82 fprintf(f
, "%d", ace
->type
);
85 /* Not sure what flags can be set in a file ACL */
87 fprintf(f
, "/%d/", ace
->flags
);
89 /* Standard permissions */
91 for (v
= standard_values
; v
->perm
; v
++) {
92 if (ace
->access_mask
== v
->mask
) {
93 fprintf(f
, "%s", v
->perm
);
98 /* Special permissions. Print out a hex value if we have
99 leftover bits in the mask. */
101 got_mask
= ace
->access_mask
;
104 for (v
= special_values
; v
->perm
; v
++) {
105 if ((ace
->access_mask
& v
->mask
) == v
->mask
) {
107 fprintf(f
, "%s", v
->perm
);
109 got_mask
&= ~v
->mask
;
115 fprintf(f
, "0x%08x", ace
->access_mask
);
123 /********************************************************************
124 print an ascii version of a security descriptor on a FILE handle
125 ********************************************************************/
127 static void sec_desc_print(FILE *f
, struct security_descriptor
*sd
)
131 fprintf(f
, "REVISION:%d\n", sd
->revision
);
133 /* Print owner and group sid */
135 fprintf(f
, "OWNER:%s\n", sid_string_tos(sd
->owner_sid
));
137 fprintf(f
, "GROUP:%s\n", sid_string_tos(sd
->group_sid
));
140 for (i
= 0; sd
->dacl
&& i
< sd
->dacl
->num_aces
; i
++) {
141 struct security_ace
*ace
= &sd
->dacl
->aces
[i
];
148 /********************************************************************
149 parse an ACE in the same format as print_ace()
150 ********************************************************************/
152 static bool parse_ace(struct security_ace
*ace
, const char *orig_str
)
157 unsigned int atype
= 0;
158 unsigned int aflags
= 0;
159 unsigned int amask
= 0;
162 const struct perm_value
*v
;
163 char *str
= SMB_STRDUP(orig_str
);
164 TALLOC_CTX
*frame
= talloc_stackframe();
172 p
= strchr_m(str
,':');
174 fprintf(stderr
, "ACE '%s': missing ':'.\n", orig_str
);
181 /* Try to parse numeric form */
183 if (sscanf(p
, "%i/%i/%i", &atype
, &aflags
, &amask
) == 3 &&
184 string_to_sid(&sid
, str
)) {
188 /* Try to parse text form */
190 if (!string_to_sid(&sid
, str
)) {
191 fprintf(stderr
, "ACE '%s': failed to convert '%s' to SID\n",
199 if (!next_token_talloc(frame
, &cp
, &tok
, "/")) {
200 fprintf(stderr
, "ACE '%s': failed to find '/' character.\n",
207 if (strncmp(tok
, "ALLOWED", strlen("ALLOWED")) == 0) {
208 atype
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
209 } else if (strncmp(tok
, "DENIED", strlen("DENIED")) == 0) {
210 atype
= SEC_ACE_TYPE_ACCESS_DENIED
;
212 fprintf(stderr
, "ACE '%s': missing 'ALLOWED' or 'DENIED' "
213 "entry at '%s'\n", orig_str
, tok
);
219 /* Only numeric form accepted for flags at present */
220 /* no flags on share permissions */
222 if (!(next_token_talloc(frame
, &cp
, &tok
, "/") &&
223 sscanf(tok
, "%i", &aflags
) && aflags
== 0)) {
224 fprintf(stderr
, "ACE '%s': bad integer flags entry at '%s'\n",
231 if (!next_token_talloc(frame
, &cp
, &tok
, "/")) {
232 fprintf(stderr
, "ACE '%s': missing / at '%s'\n",
239 if (strncmp(tok
, "0x", 2) == 0) {
240 if (sscanf(tok
, "%i", &amask
) != 1) {
241 fprintf(stderr
, "ACE '%s': bad hex number at '%s'\n",
250 for (v
= standard_values
; v
->perm
; v
++) {
251 if (strcmp(tok
, v
->perm
) == 0) {
262 for (v
= special_values
; v
->perm
; v
++) {
263 if (v
->perm
[0] == *p
) {
270 fprintf(stderr
, "ACE '%s': bad permission value at "
271 "'%s'\n", orig_str
, p
);
287 init_sec_ace(ace
, &sid
, atype
, mask
, aflags
);
294 /********************************************************************
295 ********************************************************************/
297 static struct security_descriptor
* parse_acl_string(TALLOC_CTX
*mem_ctx
, const char *szACL
, size_t *sd_size
)
299 struct security_descriptor
*sd
= NULL
;
300 struct security_ace
*ace
;
301 struct security_acl
*theacl
;
310 num_ace
= count_chars( pacl
, ',' ) + 1;
312 if ( !(ace
= TALLOC_ZERO_ARRAY( mem_ctx
, struct security_ace
, num_ace
)) )
315 for ( i
=0; i
<num_ace
; i
++ ) {
316 char *end_acl
= strchr_m( pacl
, ',' );
319 strncpy( acl_string
, pacl
, MIN( PTR_DIFF( end_acl
, pacl
), sizeof(fstring
)-1) );
320 acl_string
[MIN( PTR_DIFF( end_acl
, pacl
), sizeof(fstring
)-1)] = '\0';
322 if ( !parse_ace( &ace
[i
], acl_string
) )
329 if ( !(theacl
= make_sec_acl( mem_ctx
, NT4_ACL_REVISION
, num_ace
, ace
)) )
332 sd
= make_sec_desc( mem_ctx
, SD_REVISION
, SEC_DESC_SELF_RELATIVE
,
333 NULL
, NULL
, NULL
, theacl
, sd_size
);
338 /* add an ACE to a list of ACEs in a struct security_acl */
339 static bool add_ace(TALLOC_CTX
*mem_ctx
, struct security_acl
**the_acl
, struct security_ace
*ace
)
341 struct security_acl
*new_ace
;
342 struct security_ace
*aces
;
344 return (((*the_acl
) = make_sec_acl(mem_ctx
, 3, 1, ace
)) != NULL
);
347 if (!(aces
= SMB_CALLOC_ARRAY(struct security_ace
, 1+(*the_acl
)->num_aces
))) {
350 memcpy(aces
, (*the_acl
)->aces
, (*the_acl
)->num_aces
* sizeof(struct
352 memcpy(aces
+(*the_acl
)->num_aces
, ace
, sizeof(struct security_ace
));
353 new_ace
= make_sec_acl(mem_ctx
,(*the_acl
)->revision
,1+(*the_acl
)->num_aces
, aces
);
355 (*the_acl
) = new_ace
;
359 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
360 However NT4 gives a "The information may have been modified by a
361 computer running Windows NT 5.0" if denied ACEs do not appear before
364 static int ace_compare(struct security_ace
*ace1
, struct security_ace
*ace2
)
366 if (sec_ace_equal(ace1
, ace2
))
369 if (ace1
->type
!= ace2
->type
)
370 return ace2
->type
- ace1
->type
;
372 if (sid_compare(&ace1
->trustee
, &ace2
->trustee
))
373 return sid_compare(&ace1
->trustee
, &ace2
->trustee
);
375 if (ace1
->flags
!= ace2
->flags
)
376 return ace1
->flags
- ace2
->flags
;
378 if (ace1
->access_mask
!= ace2
->access_mask
)
379 return ace1
->access_mask
- ace2
->access_mask
;
381 if (ace1
->size
!= ace2
->size
)
382 return ace1
->size
- ace2
->size
;
384 return memcmp(ace1
, ace2
, sizeof(struct security_ace
));
387 static void sort_acl(struct security_acl
*the_acl
)
390 if (!the_acl
) return;
392 TYPESAFE_QSORT(the_acl
->aces
, the_acl
->num_aces
, ace_compare
);
394 for (i
=1;i
<the_acl
->num_aces
;) {
395 if (sec_ace_equal(&the_acl
->aces
[i
-1], &the_acl
->aces
[i
])) {
397 for (j
=i
; j
<the_acl
->num_aces
-1; j
++) {
398 the_acl
->aces
[j
] = the_acl
->aces
[j
+1];
408 static int change_share_sec(TALLOC_CTX
*mem_ctx
, const char *sharename
, char *the_acl
, enum acl_mode mode
)
410 struct security_descriptor
*sd
= NULL
;
411 struct security_descriptor
*old
= NULL
;
415 if (mode
!= SMB_ACL_SET
&& mode
!= SMB_SD_DELETE
) {
416 if (!(old
= get_share_security( mem_ctx
, sharename
, &sd_size
)) ) {
417 fprintf(stderr
, "Unable to retrieve permissions for share "
418 "[%s]\n", sharename
);
423 if ( (mode
!= SMB_ACL_VIEW
&& mode
!= SMB_SD_DELETE
) &&
424 !(sd
= parse_acl_string(mem_ctx
, the_acl
, &sd_size
)) ) {
425 fprintf( stderr
, "Failed to parse acl\n");
431 sec_desc_print( stdout
, old
);
434 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
437 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
438 if (sec_ace_equal(&sd
->dacl
->aces
[i
], &old
->dacl
->aces
[j
])) {
440 for (k
=j
; k
<old
->dacl
->num_aces
-1;k
++) {
441 old
->dacl
->aces
[k
] = old
->dacl
->aces
[k
+1];
443 old
->dacl
->num_aces
--;
450 printf("ACL for ACE:");
451 print_ace(stdout
, &sd
->dacl
->aces
[i
]);
452 printf(" not found\n");
457 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
460 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
461 if (sid_equal(&sd
->dacl
->aces
[i
].trustee
,
462 &old
->dacl
->aces
[j
].trustee
)) {
463 old
->dacl
->aces
[j
] = sd
->dacl
->aces
[i
];
469 printf("ACL for SID %s not found\n",
470 sid_string_tos(&sd
->dacl
->aces
[i
].trustee
));
475 old
->owner_sid
= sd
->owner_sid
;
479 old
->group_sid
= sd
->group_sid
;
483 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
484 add_ace(mem_ctx
, &old
->dacl
, &sd
->dacl
->aces
[i
]);
491 if (!delete_share_security(sharename
)) {
492 fprintf( stderr
, "Failed to delete security descriptor for "
493 "share [%s]\n", sharename
);
499 /* Denied ACE entries must come before allowed ones */
502 if ( !set_share_security( sharename
, old
) ) {
503 fprintf( stderr
, "Failed to store acl for share [%s]\n", sharename
);
509 /********************************************************************
511 ********************************************************************/
513 int main(int argc
, const char *argv
[])
517 enum acl_mode mode
= SMB_ACL_SET
;
518 static char *the_acl
= NULL
;
520 bool force_acl
= False
;
523 bool initialize_sid
= False
;
524 struct poptOption long_options
[] = {
526 { "remove", 'r', POPT_ARG_STRING
, &the_acl
, 'r', "Remove ACEs", "ACL" },
527 { "modify", 'm', POPT_ARG_STRING
, &the_acl
, 'm', "Modify existing ACEs", "ACL" },
528 { "add", 'a', POPT_ARG_STRING
, &the_acl
, 'a', "Add ACEs", "ACL" },
529 { "replace", 'R', POPT_ARG_STRING
, &the_acl
, 'R', "Overwrite share permission ACL", "ACLS" },
530 { "delete", 'D', POPT_ARG_NONE
, NULL
, 'D', "Delete the entire security descriptor" },
531 { "view", 'v', POPT_ARG_NONE
, NULL
, 'v', "View current share permissions" },
532 { "machine-sid", 'M', POPT_ARG_NONE
, NULL
, 'M', "Initialize the machine SID" },
533 { "force", 'F', POPT_ARG_NONE
, NULL
, 'F', "Force storing the ACL", "ACLS" },
538 if ( !(ctx
= talloc_stackframe()) ) {
539 fprintf( stderr
, "Failed to initialize talloc context!\n");
543 /* set default debug level to 1 regardless of what smb.conf sets */
544 setup_logging( "sharesec", True
);
545 DEBUGLEVEL_CLASS
[DBGC_ALL
] = 1;
547 x_setbuf( x_stderr
, NULL
);
549 pc
= poptGetContext("sharesec", argc
, argv
, long_options
, 0);
551 poptSetOtherOptionHelp(pc
, "sharename\n");
553 while ((opt
= poptGetNextOpt(pc
)) != -1) {
556 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
557 mode
= SMB_ACL_DELETE
;
561 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
562 mode
= SMB_ACL_MODIFY
;
566 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
571 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
576 mode
= SMB_SD_DELETE
;
588 initialize_sid
= True
;
597 lp_load( get_dyn_CONFIGFILE(), False
, False
, False
, True
);
599 /* check for initializing secrets.tdb first */
601 if ( initialize_sid
) {
602 struct dom_sid
*sid
= get_global_sam_sid();
605 fprintf( stderr
, "Failed to retrieve Machine SID!\n");
609 printf ("%s\n", sid_string_tos( sid
) );
613 if ( mode
== SMB_ACL_VIEW
&& force_acl
) {
614 fprintf( stderr
, "Invalid combination of -F and -v\n");
618 /* get the sharename */
620 if(!poptPeekArg(pc
)) {
621 poptPrintUsage(pc
, stderr
, 0);
625 fstrcpy(sharename
, poptGetArg(pc
));
627 snum
= lp_servicenumber( sharename
);
629 if ( snum
== -1 && !force_acl
) {
630 fprintf( stderr
, "Invalid sharename: %s\n", sharename
);
634 retval
= change_share_sec(ctx
, sharename
, the_acl
, mode
);