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 #include "popt_common.h"
28 #include "../libcli/security/security.h"
29 #include "passdb/machine_sid.h"
31 #include "cmdline_contexts.h"
33 static TALLOC_CTX *ctx;
35 enum acl_mode { SMB_ACL_DELETE,
45 /********************************************************************
46 ********************************************************************/
48 static struct security_descriptor* parse_acl_string(TALLOC_CTX *mem_ctx, const char *szACL, size_t *sd_size )
50 struct security_descriptor *sd = NULL;
51 struct security_ace *ace;
52 struct security_acl *theacl;
61 num_ace = count_chars( pacl, ',' ) + 1;
63 if ( !(ace = talloc_zero_array( mem_ctx, struct security_ace, num_ace )) )
66 for ( i=0; i<num_ace; i++ ) {
67 char *end_acl = strchr_m( pacl, ',' );
70 strncpy( acl_string, pacl, MIN( PTR_DIFF( end_acl, pacl ), sizeof(fstring)-1) );
71 acl_string[MIN( PTR_DIFF( end_acl, pacl ), sizeof(fstring)-1)] = '\0';
73 if ( !parse_ace(NULL, &ace[i], acl_string ) )
80 if ( !(theacl = make_sec_acl( mem_ctx, NT4_ACL_REVISION, num_ace, ace )) )
83 sd = make_sec_desc( mem_ctx, SD_REVISION, SEC_DESC_SELF_RELATIVE,
84 NULL, NULL, NULL, theacl, sd_size);
89 /* add an ACE to a list of ACEs in a struct security_acl */
90 static bool add_ace(TALLOC_CTX *mem_ctx, struct security_acl **the_acl, struct security_ace *ace)
92 struct security_acl *new_ace;
93 struct security_ace *aces;
95 return (((*the_acl) = make_sec_acl(mem_ctx, 3, 1, ace)) != NULL);
98 if (!(aces = SMB_CALLOC_ARRAY(struct security_ace, 1+(*the_acl)->num_aces))) {
101 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(struct
103 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(struct security_ace));
104 new_ace = make_sec_acl(mem_ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
106 (*the_acl) = new_ace;
110 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
111 However NT4 gives a "The information may have been modified by a
112 computer running Windows NT 5.0" if denied ACEs do not appear before
115 static int ace_compare(struct security_ace *ace1, struct security_ace *ace2)
117 if (security_ace_equal(ace1, ace2))
120 if (ace1->type != ace2->type)
121 return ace2->type - ace1->type;
123 if (dom_sid_compare(&ace1->trustee, &ace2->trustee))
124 return dom_sid_compare(&ace1->trustee, &ace2->trustee);
126 if (ace1->flags != ace2->flags)
127 return ace1->flags - ace2->flags;
129 if (ace1->access_mask != ace2->access_mask)
130 return ace1->access_mask - ace2->access_mask;
132 if (ace1->size != ace2->size)
133 return ace1->size - ace2->size;
135 return memcmp(ace1, ace2, sizeof(struct security_ace));
138 static void sort_acl(struct security_acl *the_acl)
141 if (!the_acl) return;
143 TYPESAFE_QSORT(the_acl->aces, the_acl->num_aces, ace_compare);
145 for (i=1;i<the_acl->num_aces;) {
146 if (security_ace_equal(&the_acl->aces[i-1],
147 &the_acl->aces[i])) {
149 for (j=i; j<the_acl->num_aces-1; j++) {
150 the_acl->aces[j] = the_acl->aces[j+1];
160 static int change_share_sec(TALLOC_CTX *mem_ctx, const char *sharename, char *the_acl, enum acl_mode mode)
162 struct security_descriptor *sd = NULL;
163 struct security_descriptor *old = NULL;
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);
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");
182 case SMB_ACL_VIEW_ALL:
183 /* should not happen */
186 sec_desc_print(NULL, stdout, old, false);
189 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
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])) {
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--;
206 printf("ACL for ACE:");
207 print_ace(NULL, stdout, &sd->dacl->aces[i], false);
208 printf(" not found\n");
213 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
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];
225 printf("ACL for SID %s not found\n",
226 sid_string_tos(&sd->dacl->aces[i].trustee));
231 old->owner_sid = sd->owner_sid;
235 old->group_sid = sd->group_sid;
239 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
240 add_ace(mem_ctx, &old->dacl, &sd->dacl->aces[i]);
247 if (!delete_share_security(sharename)) {
248 fprintf( stderr, "Failed to delete security descriptor for "
249 "share [%s]\n", sharename );
254 fprintf(stderr, "invalid command\n");
258 /* Denied ACE entries must come before allowed ones */
261 if ( !set_share_security( sharename, old ) ) {
262 fprintf( stderr, "Failed to store acl for share [%s]\n", sharename );
268 static int set_sharesec_sddl(const char *sharename, const char *sddl)
270 struct security_descriptor *sd;
273 sd = sddl_decode(talloc_tos(), sddl, get_global_sam_sid());
275 fprintf(stderr, "Failed to parse acl\n");
279 ret = set_share_security(sharename, sd);
282 fprintf(stderr, "Failed to store acl for share [%s]\n",
290 static int view_sharesec_sddl(const char *sharename)
292 struct security_descriptor *sd;
296 sd = get_share_security(talloc_tos(), sharename, &sd_size);
298 fprintf(stderr, "Unable to retrieve permissions for share "
299 "[%s]\n", sharename);
303 acl = sddl_encode(talloc_tos(), sd, get_global_sam_sid());
306 fprintf(stderr, "Unable to sddl-encode permissions for share "
307 "[%s]\n", sharename);
315 /********************************************************************
317 ********************************************************************/
323 int main(int argc, const char *argv[])
327 enum acl_mode mode = SMB_ACL_SET;
328 static char *the_acl = NULL;
330 bool force_acl = False;
333 bool initialize_sid = False;
334 struct poptOption long_options[] = {
336 { "remove", 'r', POPT_ARG_STRING, &the_acl, 'r', "Remove ACEs", "ACL" },
337 { "modify", 'm', POPT_ARG_STRING, &the_acl, 'm', "Modify existing ACEs", "ACL" },
338 { "add", 'a', POPT_ARG_STRING, &the_acl, 'a', "Add ACEs", "ACL" },
339 { "replace", 'R', POPT_ARG_STRING, &the_acl, 'R', "Overwrite share permission ACL", "ACLS" },
340 { "delete", 'D', POPT_ARG_NONE, NULL, 'D', "Delete the entire security descriptor" },
341 { "setsddl", 'S', POPT_ARG_STRING, the_acl, 'S',
342 "Set the SD in sddl format" },
343 { "viewsddl", 'V', POPT_ARG_NONE, the_acl, 'V',
344 "View the SD in sddl format" },
345 { "view", 'v', POPT_ARG_NONE, NULL, 'v', "View current share permissions" },
346 { "view-all", 0, POPT_ARG_NONE, NULL, OPT_VIEW_ALL,
347 "View all current share permissions" },
348 { "machine-sid", 'M', POPT_ARG_NONE, NULL, 'M', "Initialize the machine SID" },
349 { "force", 'F', POPT_ARG_NONE, NULL, 'F', "Force storing the ACL", "ACLS" },
354 if ( !(ctx = talloc_stackframe()) ) {
355 fprintf( stderr, "Failed to initialize talloc context!\n");
359 /* set default debug level to 1 regardless of what smb.conf sets */
360 setup_logging( "sharesec", DEBUG_STDERR);
364 lp_set_cmdline("log level", "1");
366 pc = poptGetContext("sharesec", argc, argv, long_options, 0);
368 poptSetOtherOptionHelp(pc, "sharename\n");
370 while ((opt = poptGetNextOpt(pc)) != -1) {
373 the_acl = smb_xstrdup(poptGetOptArg(pc));
374 mode = SMB_ACL_DELETE;
378 the_acl = smb_xstrdup(poptGetOptArg(pc));
379 mode = SMB_ACL_MODIFY;
383 the_acl = smb_xstrdup(poptGetOptArg(pc));
388 the_acl = smb_xstrdup(poptGetOptArg(pc));
393 mode = SMB_SD_DELETE;
397 mode = SMB_SD_SETSDDL;
398 the_acl = smb_xstrdup(poptGetOptArg(pc));
402 mode = SMB_SD_VIEWSDDL;
414 initialize_sid = True;
417 mode = SMB_ACL_VIEW_ALL;
424 cmdline_messaging_context(get_dyn_CONFIGFILE());
425 lp_load_with_registry_shares(get_dyn_CONFIGFILE());
427 /* check for initializing secrets.tdb first */
429 if ( initialize_sid ) {
430 struct dom_sid *sid = get_global_sam_sid();
433 fprintf( stderr, "Failed to retrieve Machine SID!\n");
437 printf ("%s\n", sid_string_tos( sid ) );
441 if ( mode == SMB_ACL_VIEW && force_acl ) {
442 fprintf( stderr, "Invalid combination of -F and -v\n");
446 if (mode == SMB_ACL_VIEW_ALL) {
449 for (i=0; i<lp_numservices(); i++) {
450 TALLOC_CTX *frame = talloc_stackframe();
451 const char *service = lp_servicename(frame, i);
453 if (service == NULL) {
457 printf("[%s]\n", service);
458 change_share_sec(frame, service, NULL, SMB_ACL_VIEW);
465 /* get the sharename */
467 if(!poptPeekArg(pc)) {
468 poptPrintUsage(pc, stderr, 0);
472 fstrcpy(sharename, poptGetArg(pc));
474 snum = lp_servicenumber( sharename );
476 if ( snum == -1 && !force_acl ) {
477 fprintf( stderr, "Invalid sharename: %s\n", sharename);
483 retval = set_sharesec_sddl(sharename, the_acl);
485 case SMB_SD_VIEWSDDL:
486 retval = view_sharesec_sddl(sharename);
489 retval = change_share_sec(ctx, sharename, the_acl, mode);