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 2 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, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 static TALLOC_CTX
*ctx
;
30 enum acl_mode
{SMB_ACL_DELETE
, SMB_ACL_MODIFY
, SMB_ACL_ADD
, SMB_ACL_SET
, SMB_ACL_VIEW
};
37 /* These values discovered by inspection */
39 static const struct perm_value special_values
[] = {
40 { "R", SEC_RIGHTS_FILE_READ
},
41 { "W", SEC_RIGHTS_FILE_WRITE
},
42 { "X", SEC_RIGHTS_FILE_EXECUTE
},
43 { "D", SEC_STD_DELETE
},
44 { "P", SEC_STD_WRITE_DAC
},
45 { "O", SEC_STD_WRITE_OWNER
},
49 #define SEC_RIGHTS_DIR_CHANGE ( SEC_RIGHTS_DIR_READ|SEC_STD_DELETE|SEC_RIGHTS_DIR_WRITE|SEC_DIR_TRAVERSE )
51 static const struct perm_value standard_values
[] = {
52 { "READ", SEC_RIGHTS_DIR_READ
|SEC_DIR_TRAVERSE
},
53 { "CHANGE", SEC_RIGHTS_DIR_CHANGE
},
54 { "FULL", SEC_RIGHTS_DIR_ALL
},
58 /********************************************************************
59 print an ACE on a FILE
60 ********************************************************************/
62 static void print_ace(FILE *f
, SEC_ACE
*ace
)
64 const struct perm_value
*v
;
68 fprintf(f
, "%s:", sid_string_static(&ace
->trustee
));
72 if (ace
->type
== SEC_ACE_TYPE_ACCESS_ALLOWED
) {
73 fprintf(f
, "ALLOWED");
74 } else if (ace
->type
== SEC_ACE_TYPE_ACCESS_DENIED
) {
77 fprintf(f
, "%d", ace
->type
);
80 /* Not sure what flags can be set in a file ACL */
82 fprintf(f
, "/%d/", ace
->flags
);
84 /* Standard permissions */
86 for (v
= standard_values
; v
->perm
; v
++) {
87 if (ace
->access_mask
== v
->mask
) {
88 fprintf(f
, "%s", v
->perm
);
93 /* Special permissions. Print out a hex value if we have
94 leftover bits in the mask. */
96 got_mask
= ace
->access_mask
;
99 for (v
= special_values
; v
->perm
; v
++) {
100 if ((ace
->access_mask
& v
->mask
) == v
->mask
) {
102 fprintf(f
, "%s", v
->perm
);
104 got_mask
&= ~v
->mask
;
110 fprintf(f
, "0x%08x", ace
->access_mask
);
118 /********************************************************************
119 print a ascii version of a security descriptor on a FILE handle
120 ********************************************************************/
122 static void sec_desc_print(FILE *f
, SEC_DESC
*sd
)
126 fprintf(f
, "REVISION:%d\n", sd
->revision
);
128 /* Print owner and group sid */
130 fprintf(f
, "OWNER:%s\n", sid_string_static(sd
->owner_sid
));
132 fprintf(f
, "GROUP:%s\n", sid_string_static(sd
->group_sid
));
135 for (i
= 0; sd
->dacl
&& i
< sd
->dacl
->num_aces
; i
++) {
136 SEC_ACE
*ace
= &sd
->dacl
->aces
[i
];
144 /********************************************************************
145 parse an ACE in the same format as print_ace()
146 ********************************************************************/
148 static BOOL
parse_ace(SEC_ACE
*ace
, const char *orig_str
)
153 unsigned int atype
= 0;
154 unsigned int aflags
= 0;
155 unsigned int amask
= 0;
158 const struct perm_value
*v
;
159 char *str
= SMB_STRDUP(orig_str
);
166 p
= strchr_m(str
,':');
168 printf("ACE '%s': missing ':'.\n", orig_str
);
174 /* Try to parse numeric form */
176 if (sscanf(p
, "%i/%i/%i", &atype
, &aflags
, &amask
) == 3 &&
177 string_to_sid(&sid
, str
)) {
181 /* Try to parse text form */
183 if (!string_to_sid(&sid
, str
)) {
184 printf("ACE '%s': failed to convert '%s' to SID\n",
191 if (!next_token(&cp
, tok
, "/", sizeof(fstring
))) {
192 printf("ACE '%s': failed to find '/' character.\n",
198 if (strncmp(tok
, "ALLOWED", strlen("ALLOWED")) == 0) {
199 atype
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
200 } else if (strncmp(tok
, "DENIED", strlen("DENIED")) == 0) {
201 atype
= SEC_ACE_TYPE_ACCESS_DENIED
;
203 printf("ACE '%s': missing 'ALLOWED' or 'DENIED' entry at '%s'\n",
209 /* Only numeric form accepted for flags at present */
210 /* no flags on share permissions */
212 if (!(next_token(&cp
, tok
, "/", sizeof(fstring
)) &&
213 sscanf(tok
, "%i", &aflags
) && aflags
== 0)) {
214 printf("ACE '%s': bad integer flags entry at '%s'\n",
220 if (!next_token(&cp
, tok
, "/", sizeof(fstring
))) {
221 printf("ACE '%s': missing / at '%s'\n",
227 if (strncmp(tok
, "0x", 2) == 0) {
228 if (sscanf(tok
, "%i", &amask
) != 1) {
229 printf("ACE '%s': bad hex number at '%s'\n",
237 for (v
= standard_values
; v
->perm
; v
++) {
238 if (strcmp(tok
, v
->perm
) == 0) {
249 for (v
= special_values
; v
->perm
; v
++) {
250 if (v
->perm
[0] == *p
) {
257 printf("ACE '%s': bad permission value at '%s'\n",
272 init_sec_ace(ace
, &sid
, atype
, mask
, aflags
);
278 /********************************************************************
279 ********************************************************************/
281 static SEC_DESC
* parse_acl_string(TALLOC_CTX
*mem_ctx
, const char *szACL
, size_t *sd_size
)
294 num_ace
= count_chars( pacl
, ',' ) + 1;
296 if ( !(ace
= TALLOC_ZERO_ARRAY( mem_ctx
, SEC_ACE
, num_ace
)) )
299 for ( i
=0; i
<num_ace
; i
++ ) {
300 char *end_acl
= strchr_m( pacl
, ',' );
303 strncpy( acl_string
, pacl
, MIN( PTR_DIFF( end_acl
, pacl
), sizeof(fstring
)-1) );
304 acl_string
[MIN( PTR_DIFF( end_acl
, pacl
), sizeof(fstring
)-1)] = '\0';
306 if ( !parse_ace( &ace
[i
], acl_string
) )
313 if ( !(acl
= make_sec_acl( mem_ctx
, NT4_ACL_REVISION
, num_ace
, ace
)) )
316 sd
= make_sec_desc( mem_ctx
, SEC_DESC_REVISION
, SEC_DESC_SELF_RELATIVE
,
317 NULL
, NULL
, NULL
, acl
, sd_size
);
322 /* add an ACE to a list of ACEs in a SEC_ACL */
323 static BOOL
add_ace(TALLOC_CTX
*mem_ctx
, SEC_ACL
**the_acl
, SEC_ACE
*ace
)
328 return (((*the_acl
) = make_sec_acl(mem_ctx
, 3, 1, ace
)) != NULL
);
331 if (!(aces
= SMB_CALLOC_ARRAY(SEC_ACE
, 1+(*the_acl
)->num_aces
))) {
334 memcpy(aces
, (*the_acl
)->aces
, (*the_acl
)->num_aces
* sizeof(SEC_ACE
));
335 memcpy(aces
+(*the_acl
)->num_aces
, ace
, sizeof(SEC_ACE
));
336 new_ace
= make_sec_acl(mem_ctx
,(*the_acl
)->revision
,1+(*the_acl
)->num_aces
, aces
);
338 (*the_acl
) = new_ace
;
342 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
343 However NT4 gives a "The information may have been modified by a
344 computer running Windows NT 5.0" if denied ACEs do not appear before
347 static int ace_compare(SEC_ACE
*ace1
, SEC_ACE
*ace2
)
349 if (sec_ace_equal(ace1
, ace2
))
352 if (ace1
->type
!= ace2
->type
)
353 return ace2
->type
- ace1
->type
;
355 if (sid_compare(&ace1
->trustee
, &ace2
->trustee
))
356 return sid_compare(&ace1
->trustee
, &ace2
->trustee
);
358 if (ace1
->flags
!= ace2
->flags
)
359 return ace1
->flags
- ace2
->flags
;
361 if (ace1
->access_mask
!= ace2
->access_mask
)
362 return ace1
->access_mask
- ace2
->access_mask
;
364 if (ace1
->size
!= ace2
->size
)
365 return ace1
->size
- ace2
->size
;
367 return memcmp(ace1
, ace2
, sizeof(SEC_ACE
));
370 static void sort_acl(SEC_ACL
*the_acl
)
373 if (!the_acl
) return;
375 qsort(the_acl
->aces
, the_acl
->num_aces
, sizeof(the_acl
->aces
[0]), QSORT_CAST ace_compare
);
377 for (i
=1;i
<the_acl
->num_aces
;) {
378 if (sec_ace_equal(&the_acl
->aces
[i
-1], &the_acl
->aces
[i
])) {
380 for (j
=i
; j
<the_acl
->num_aces
-1; j
++) {
381 the_acl
->aces
[j
] = the_acl
->aces
[j
+1];
391 static int change_share_sec(TALLOC_CTX
*mem_ctx
, const char *sharename
, char *the_acl
, enum acl_mode mode
)
394 SEC_DESC
*old
= NULL
;
398 if (mode
!= SMB_ACL_SET
) {
399 if (!(old
= get_share_security( mem_ctx
, sharename
, &sd_size
)) ) {
400 fprintf(stderr
, "Unable to retrieve permissions for share [%s]\n", sharename
);
405 if ( (mode
!= SMB_ACL_VIEW
) && !(sd
= parse_acl_string(mem_ctx
, the_acl
, &sd_size
)) ) {
406 fprintf( stderr
, "Failed to parse acl\n");
412 sec_desc_print( stdout
, old
);
415 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
418 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
419 if (sec_ace_equal(&sd
->dacl
->aces
[i
], &old
->dacl
->aces
[j
])) {
421 for (k
=j
; k
<old
->dacl
->num_aces
-1;k
++) {
422 old
->dacl
->aces
[k
] = old
->dacl
->aces
[k
+1];
424 old
->dacl
->num_aces
--;
431 printf("ACL for ACE:");
432 print_ace(stdout
, &sd
->dacl
->aces
[i
]);
433 printf(" not found\n");
439 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
442 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
443 if (sid_equal(&sd
->dacl
->aces
[i
].trustee
,
444 &old
->dacl
->aces
[j
].trustee
)) {
445 old
->dacl
->aces
[j
] = sd
->dacl
->aces
[i
];
451 printf("ACL for SID %s not found\n", sid_string_static(&sd
->dacl
->aces
[i
].trustee
));
456 old
->owner_sid
= sd
->owner_sid
;
460 old
->group_sid
= sd
->group_sid
;
464 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
465 add_ace(mem_ctx
, &old
->dacl
, &sd
->dacl
->aces
[i
]);
473 /* Denied ACE entries must come before allowed ones */
476 if ( !set_share_security( sharename
, old
) ) {
477 fprintf( stderr
, "Failed to store acl for share [%s]\n", sharename
);
483 /********************************************************************
485 ********************************************************************/
487 int main(int argc
, const char *argv
[])
491 enum acl_mode mode
= SMB_ACL_SET
;
492 static char *the_acl
= NULL
;
494 BOOL force_acl
= False
;
497 BOOL initialize_sid
= False
;
498 struct poptOption long_options
[] = {
500 { "remove", 'r', POPT_ARG_STRING
, &the_acl
, 'r', "Delete an ACE", "ACL" },
501 { "modify", 'm', POPT_ARG_STRING
, &the_acl
, 'm', "Modify an acl", "ACL" },
502 { "add", 'a', POPT_ARG_STRING
, &the_acl
, 'a', "Add an ACE", "ACL" },
503 { "replace", 'R', POPT_ARG_STRING
, &the_acl
, 'R', "Set share mission ACL", "ACLS" },
504 { "view", 'v', POPT_ARG_NONE
, NULL
, 'v', "View current share permissions" },
505 { "machine-sid", 'M', POPT_ARG_NONE
, NULL
, 'M', "Initialize the machine SID" },
506 { "force", 'F', POPT_ARG_NONE
, NULL
, 'F', "Force storing the ACL", "ACLS" },
511 if ( !(ctx
= talloc_init("main")) ) {
512 fprintf( stderr
, "Failed to initialize talloc context!\n");
516 /* set default debug level to 1 regardless of what smb.conf sets */
517 setup_logging( "sharesec", True
);
518 DEBUGLEVEL_CLASS
[DBGC_ALL
] = 1;
520 x_setbuf( x_stderr
, NULL
);
522 pc
= poptGetContext("sharesec", argc
, argv
, long_options
, 0);
524 poptSetOtherOptionHelp(pc
, "sharename\n");
526 while ((opt
= poptGetNextOpt(pc
)) != -1) {
529 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
530 mode
= SMB_ACL_DELETE
;
534 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
535 mode
= SMB_ACL_MODIFY
;
539 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
543 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
556 initialize_sid
= True
;
565 lp_load( dyn_CONFIGFILE
, False
, False
, False
, True
);
567 /* check for initializing secrets.tdb first */
569 if ( initialize_sid
) {
570 DOM_SID
*sid
= get_global_sam_sid();
573 fprintf( stderr
, "Failed to retrieve Machine SID!\n");
577 printf ("%s\n", sid_string_static( sid
) );
581 if ( mode
== SMB_ACL_VIEW
&& force_acl
) {
582 fprintf( stderr
, "Invalid combination of -F and -v\n");
586 /* get the sharename */
588 if(!poptPeekArg(pc
)) {
589 poptPrintUsage(pc
, stderr
, 0);
593 fstrcpy(sharename
, poptGetArg(pc
));
595 snum
= lp_servicenumber( sharename
);
597 if ( snum
== -1 && !force_acl
) {
598 fprintf( stderr
, "Invalid sharename: %s\n", sharename
);
602 retval
= change_share_sec(ctx
, sharename
, the_acl
, mode
);