Fix some typos.
[Samba.git] / source3 / utils / sharesec.c
blob2b1e435cdb7c378b506d3cf32f56da999bc49480
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/>.
25 #include "includes.h"
26 #include "popt_common.h"
27 #include "../libcli/security/security.h"
28 #include "passdb/machine_sid.h"
30 static TALLOC_CTX *ctx;
32 enum acl_mode { SMB_ACL_DELETE,
33 SMB_ACL_MODIFY,
34 SMB_ACL_ADD,
35 SMB_ACL_SET,
36 SMB_SD_DELETE,
37 SMB_SD_SETSDDL,
38 SMB_SD_VIEWSDDL,
39 SMB_ACL_VIEW,
40 SMB_ACL_VIEW_ALL };
42 struct perm_value {
43 const char *perm;
44 uint32 mask;
47 /* These values discovered by inspection */
49 static const struct perm_value special_values[] = {
50 { "R", SEC_RIGHTS_FILE_READ },
51 { "W", SEC_RIGHTS_FILE_WRITE },
52 { "X", SEC_RIGHTS_FILE_EXECUTE },
53 { "D", SEC_STD_DELETE },
54 { "P", SEC_STD_WRITE_DAC },
55 { "O", SEC_STD_WRITE_OWNER },
56 { NULL, 0 },
59 #define SEC_RIGHTS_DIR_CHANGE ( SEC_RIGHTS_DIR_READ|SEC_STD_DELETE|\
60 SEC_RIGHTS_DIR_WRITE|SEC_DIR_TRAVERSE )
62 static const struct perm_value standard_values[] = {
63 { "READ", SEC_RIGHTS_DIR_READ|SEC_DIR_TRAVERSE },
64 { "CHANGE", SEC_RIGHTS_DIR_CHANGE },
65 { "FULL", SEC_RIGHTS_DIR_ALL },
66 { NULL, 0 },
69 /********************************************************************
70 print an ACE on a FILE
71 ********************************************************************/
73 static void print_ace(FILE *f, struct security_ace *ace)
75 const struct perm_value *v;
76 int do_print = 0;
77 uint32 got_mask;
79 fprintf(f, "%s:", sid_string_tos(&ace->trustee));
81 /* Ace type */
83 if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
84 fprintf(f, "ALLOWED");
85 } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
86 fprintf(f, "DENIED");
87 } else {
88 fprintf(f, "%d", ace->type);
91 /* Not sure what flags can be set in a file ACL */
93 fprintf(f, "/%d/", ace->flags);
95 /* Standard permissions */
97 for (v = standard_values; v->perm; v++) {
98 if (ace->access_mask == v->mask) {
99 fprintf(f, "%s", v->perm);
100 return;
104 /* Special permissions. Print out a hex value if we have
105 leftover bits in the mask. */
107 got_mask = ace->access_mask;
109 again:
110 for (v = special_values; v->perm; v++) {
111 if ((ace->access_mask & v->mask) == v->mask) {
112 if (do_print) {
113 fprintf(f, "%s", v->perm);
115 got_mask &= ~v->mask;
119 if (!do_print) {
120 if (got_mask != 0) {
121 fprintf(f, "0x%08x", ace->access_mask);
122 } else {
123 do_print = 1;
124 goto again;
129 /********************************************************************
130 print an ascii version of a security descriptor on a FILE handle
131 ********************************************************************/
133 static void sec_desc_print(FILE *f, struct security_descriptor *sd)
135 uint32 i;
137 fprintf(f, "REVISION:%d\n", sd->revision);
139 /* Print owner and group sid */
141 fprintf(f, "OWNER:%s\n", sid_string_tos(sd->owner_sid));
143 fprintf(f, "GROUP:%s\n", sid_string_tos(sd->group_sid));
145 /* Print aces */
146 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
147 struct security_ace *ace = &sd->dacl->aces[i];
148 fprintf(f, "ACL:");
149 print_ace(f, ace);
150 fprintf(f, "\n");
154 /********************************************************************
155 parse an ACE in the same format as print_ace()
156 ********************************************************************/
158 static bool parse_ace(struct security_ace *ace, const char *orig_str)
160 char *p;
161 const char *cp;
162 char *tok;
163 unsigned int atype = 0;
164 unsigned int aflags = 0;
165 unsigned int amask = 0;
166 struct dom_sid sid;
167 uint32_t mask;
168 const struct perm_value *v;
169 char *str = SMB_STRDUP(orig_str);
170 TALLOC_CTX *frame = talloc_stackframe();
172 if (!str) {
173 TALLOC_FREE(frame);
174 return False;
177 ZERO_STRUCTP(ace);
178 p = strchr_m(str,':');
179 if (!p) {
180 fprintf(stderr, "ACE '%s': missing ':'.\n", orig_str);
181 SAFE_FREE(str);
182 TALLOC_FREE(frame);
183 return False;
185 *p = '\0';
186 p++;
187 /* Try to parse numeric form */
189 if (sscanf(p, "%u/%u/%u", &atype, &aflags, &amask) == 3 &&
190 string_to_sid(&sid, str)) {
191 goto done;
194 /* Try to parse text form */
196 if (!string_to_sid(&sid, str)) {
197 fprintf(stderr, "ACE '%s': failed to convert '%s' to SID\n",
198 orig_str, str);
199 SAFE_FREE(str);
200 TALLOC_FREE(frame);
201 return False;
204 cp = p;
205 if (!next_token_talloc(frame, &cp, &tok, "/")) {
206 fprintf(stderr, "ACE '%s': failed to find '/' character.\n",
207 orig_str);
208 SAFE_FREE(str);
209 TALLOC_FREE(frame);
210 return False;
213 if (strncmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
214 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
215 } else if (strncmp(tok, "DENIED", strlen("DENIED")) == 0) {
216 atype = SEC_ACE_TYPE_ACCESS_DENIED;
217 } else {
218 fprintf(stderr, "ACE '%s': missing 'ALLOWED' or 'DENIED' "
219 "entry at '%s'\n", orig_str, tok);
220 SAFE_FREE(str);
221 TALLOC_FREE(frame);
222 return False;
225 /* Only numeric form accepted for flags at present */
226 /* no flags on share permissions */
228 if (!(next_token_talloc(frame, &cp, &tok, "/") &&
229 sscanf(tok, "%u", &aflags) && aflags == 0)) {
230 fprintf(stderr, "ACE '%s': bad integer flags entry at '%s'\n",
231 orig_str, tok);
232 SAFE_FREE(str);
233 TALLOC_FREE(frame);
234 return False;
237 if (!next_token_talloc(frame, &cp, &tok, "/")) {
238 fprintf(stderr, "ACE '%s': missing / at '%s'\n",
239 orig_str, tok);
240 SAFE_FREE(str);
241 TALLOC_FREE(frame);
242 return False;
245 if (strncmp(tok, "0x", 2) == 0) {
246 if (sscanf(tok, "%u", &amask) != 1) {
247 fprintf(stderr, "ACE '%s': bad hex number at '%s'\n",
248 orig_str, tok);
249 TALLOC_FREE(frame);
250 SAFE_FREE(str);
251 return False;
253 goto done;
256 for (v = standard_values; v->perm; v++) {
257 if (strcmp(tok, v->perm) == 0) {
258 amask = v->mask;
259 goto done;
263 p = tok;
265 while(*p) {
266 bool found = False;
268 for (v = special_values; v->perm; v++) {
269 if (v->perm[0] == *p) {
270 amask |= v->mask;
271 found = True;
275 if (!found) {
276 fprintf(stderr, "ACE '%s': bad permission value at "
277 "'%s'\n", orig_str, p);
278 TALLOC_FREE(frame);
279 SAFE_FREE(str);
280 return False;
282 p++;
285 if (*p) {
286 TALLOC_FREE(frame);
287 SAFE_FREE(str);
288 return False;
291 done:
292 mask = amask;
293 init_sec_ace(ace, &sid, atype, mask, aflags);
294 SAFE_FREE(str);
295 TALLOC_FREE(frame);
296 return True;
300 /********************************************************************
301 ********************************************************************/
303 static struct security_descriptor* parse_acl_string(TALLOC_CTX *mem_ctx, const char *szACL, size_t *sd_size )
305 struct security_descriptor *sd = NULL;
306 struct security_ace *ace;
307 struct security_acl *theacl;
308 int num_ace;
309 const char *pacl;
310 int i;
312 if ( !szACL )
313 return NULL;
315 pacl = szACL;
316 num_ace = count_chars( pacl, ',' ) + 1;
318 if ( !(ace = talloc_zero_array( mem_ctx, struct security_ace, num_ace )) )
319 return NULL;
321 for ( i=0; i<num_ace; i++ ) {
322 char *end_acl = strchr_m( pacl, ',' );
323 fstring acl_string;
325 strncpy( acl_string, pacl, MIN( PTR_DIFF( end_acl, pacl ), sizeof(fstring)-1) );
326 acl_string[MIN( PTR_DIFF( end_acl, pacl ), sizeof(fstring)-1)] = '\0';
328 if ( !parse_ace( &ace[i], acl_string ) )
329 return NULL;
331 pacl = end_acl;
332 pacl++;
335 if ( !(theacl = make_sec_acl( mem_ctx, NT4_ACL_REVISION, num_ace, ace )) )
336 return NULL;
338 sd = make_sec_desc( mem_ctx, SD_REVISION, SEC_DESC_SELF_RELATIVE,
339 NULL, NULL, NULL, theacl, sd_size);
341 return sd;
344 /* add an ACE to a list of ACEs in a struct security_acl */
345 static bool add_ace(TALLOC_CTX *mem_ctx, struct security_acl **the_acl, struct security_ace *ace)
347 struct security_acl *new_ace;
348 struct security_ace *aces;
349 if (! *the_acl) {
350 return (((*the_acl) = make_sec_acl(mem_ctx, 3, 1, ace)) != NULL);
353 if (!(aces = SMB_CALLOC_ARRAY(struct security_ace, 1+(*the_acl)->num_aces))) {
354 return False;
356 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(struct
357 security_ace));
358 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(struct security_ace));
359 new_ace = make_sec_acl(mem_ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
360 SAFE_FREE(aces);
361 (*the_acl) = new_ace;
362 return True;
365 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
366 However NT4 gives a "The information may have been modified by a
367 computer running Windows NT 5.0" if denied ACEs do not appear before
368 allowed ACEs. */
370 static int ace_compare(struct security_ace *ace1, struct security_ace *ace2)
372 if (sec_ace_equal(ace1, ace2))
373 return 0;
375 if (ace1->type != ace2->type)
376 return ace2->type - ace1->type;
378 if (dom_sid_compare(&ace1->trustee, &ace2->trustee))
379 return dom_sid_compare(&ace1->trustee, &ace2->trustee);
381 if (ace1->flags != ace2->flags)
382 return ace1->flags - ace2->flags;
384 if (ace1->access_mask != ace2->access_mask)
385 return ace1->access_mask - ace2->access_mask;
387 if (ace1->size != ace2->size)
388 return ace1->size - ace2->size;
390 return memcmp(ace1, ace2, sizeof(struct security_ace));
393 static void sort_acl(struct security_acl *the_acl)
395 uint32 i;
396 if (!the_acl) return;
398 TYPESAFE_QSORT(the_acl->aces, the_acl->num_aces, ace_compare);
400 for (i=1;i<the_acl->num_aces;) {
401 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
402 int j;
403 for (j=i; j<the_acl->num_aces-1; j++) {
404 the_acl->aces[j] = the_acl->aces[j+1];
406 the_acl->num_aces--;
407 } else {
408 i++;
414 static int change_share_sec(TALLOC_CTX *mem_ctx, const char *sharename, char *the_acl, enum acl_mode mode)
416 struct security_descriptor *sd = NULL;
417 struct security_descriptor *old = NULL;
418 size_t sd_size = 0;
419 uint32 i, j;
421 if (mode != SMB_ACL_SET && mode != SMB_SD_DELETE) {
422 if (!(old = get_share_security( mem_ctx, sharename, &sd_size )) ) {
423 fprintf(stderr, "Unable to retrieve permissions for share "
424 "[%s]\n", sharename);
425 return -1;
429 if ( (mode != SMB_ACL_VIEW && mode != SMB_SD_DELETE) &&
430 !(sd = parse_acl_string(mem_ctx, the_acl, &sd_size )) ) {
431 fprintf( stderr, "Failed to parse acl\n");
432 return -1;
435 switch (mode) {
436 case SMB_ACL_VIEW_ALL:
437 /* should not happen */
438 return 0;
439 case SMB_ACL_VIEW:
440 sec_desc_print( stdout, old);
441 return 0;
442 case SMB_ACL_DELETE:
443 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
444 bool found = False;
446 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
447 if (sec_ace_equal(&sd->dacl->aces[i], &old->dacl->aces[j])) {
448 uint32 k;
449 for (k=j; k<old->dacl->num_aces-1;k++) {
450 old->dacl->aces[k] = old->dacl->aces[k+1];
452 old->dacl->num_aces--;
453 found = True;
454 break;
458 if (!found) {
459 printf("ACL for ACE:");
460 print_ace(stdout, &sd->dacl->aces[i]);
461 printf(" not found\n");
464 break;
465 case SMB_ACL_MODIFY:
466 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
467 bool found = False;
469 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
470 if (dom_sid_equal(&sd->dacl->aces[i].trustee,
471 &old->dacl->aces[j].trustee)) {
472 old->dacl->aces[j] = sd->dacl->aces[i];
473 found = True;
477 if (!found) {
478 printf("ACL for SID %s not found\n",
479 sid_string_tos(&sd->dacl->aces[i].trustee));
483 if (sd->owner_sid) {
484 old->owner_sid = sd->owner_sid;
487 if (sd->group_sid) {
488 old->group_sid = sd->group_sid;
490 break;
491 case SMB_ACL_ADD:
492 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
493 add_ace(mem_ctx, &old->dacl, &sd->dacl->aces[i]);
495 break;
496 case SMB_ACL_SET:
497 old = sd;
498 break;
499 case SMB_SD_DELETE:
500 if (!delete_share_security(sharename)) {
501 fprintf( stderr, "Failed to delete security descriptor for "
502 "share [%s]\n", sharename );
503 return -1;
505 return 0;
506 default:
507 fprintf(stderr, "invalid command\n");
508 return -1;
511 /* Denied ACE entries must come before allowed ones */
512 sort_acl(old->dacl);
514 if ( !set_share_security( sharename, old ) ) {
515 fprintf( stderr, "Failed to store acl for share [%s]\n", sharename );
516 return 2;
518 return 0;
521 static int set_sharesec_sddl(const char *sharename, const char *sddl)
523 struct security_descriptor *sd;
524 bool ret;
526 sd = sddl_decode(talloc_tos(), sddl, get_global_sam_sid());
527 if (sd == NULL) {
528 fprintf(stderr, "Failed to parse acl\n");
529 return -1;
532 ret = set_share_security(sharename, sd);
533 TALLOC_FREE(sd);
534 if (!ret) {
535 fprintf(stderr, "Failed to store acl for share [%s]\n",
536 sharename);
537 return -1;
540 return 0;
543 static int view_sharesec_sddl(const char *sharename)
545 struct security_descriptor *sd;
546 size_t sd_size;
547 char *acl;
549 sd = get_share_security(talloc_tos(), sharename, &sd_size);
550 if (sd == NULL) {
551 fprintf(stderr, "Unable to retrieve permissions for share "
552 "[%s]\n", sharename);
553 return -1;
556 acl = sddl_encode(talloc_tos(), sd, get_global_sam_sid());
557 TALLOC_FREE(sd);
558 if (acl == NULL) {
559 fprintf(stderr, "Unable to sddl-encode permissions for share "
560 "[%s]\n", sharename);
561 return -1;
563 printf("%s\n", acl);
564 TALLOC_FREE(acl);
565 return 0;
568 /********************************************************************
569 main program
570 ********************************************************************/
572 enum {
573 OPT_VIEW_ALL = 1000,
576 int main(int argc, const char *argv[])
578 int opt;
579 int retval = 0;
580 enum acl_mode mode = SMB_ACL_SET;
581 static char *the_acl = NULL;
582 fstring sharename;
583 bool force_acl = False;
584 int snum;
585 poptContext pc;
586 bool initialize_sid = False;
587 struct poptOption long_options[] = {
588 POPT_AUTOHELP
589 { "remove", 'r', POPT_ARG_STRING, &the_acl, 'r', "Remove ACEs", "ACL" },
590 { "modify", 'm', POPT_ARG_STRING, &the_acl, 'm', "Modify existing ACEs", "ACL" },
591 { "add", 'a', POPT_ARG_STRING, &the_acl, 'a', "Add ACEs", "ACL" },
592 { "replace", 'R', POPT_ARG_STRING, &the_acl, 'R', "Overwrite share permission ACL", "ACLS" },
593 { "delete", 'D', POPT_ARG_NONE, NULL, 'D', "Delete the entire security descriptor" },
594 { "setsddl", 'S', POPT_ARG_STRING, the_acl, 'S',
595 "Set the SD in sddl format" },
596 { "viewsddl", 'V', POPT_ARG_NONE, the_acl, 'V',
597 "View the SD in sddl format" },
598 { "view", 'v', POPT_ARG_NONE, NULL, 'v', "View current share permissions" },
599 { "view-all", 0, POPT_ARG_NONE, NULL, OPT_VIEW_ALL,
600 "View all current share permissions" },
601 { "machine-sid", 'M', POPT_ARG_NONE, NULL, 'M', "Initialize the machine SID" },
602 { "force", 'F', POPT_ARG_NONE, NULL, 'F', "Force storing the ACL", "ACLS" },
603 POPT_COMMON_SAMBA
604 { NULL }
607 if ( !(ctx = talloc_stackframe()) ) {
608 fprintf( stderr, "Failed to initialize talloc context!\n");
609 return -1;
612 /* set default debug level to 1 regardless of what smb.conf sets */
613 setup_logging( "sharesec", DEBUG_STDERR);
615 load_case_tables();
617 lp_set_cmdline("log level", "1");
619 pc = poptGetContext("sharesec", argc, argv, long_options, 0);
621 poptSetOtherOptionHelp(pc, "sharename\n");
623 while ((opt = poptGetNextOpt(pc)) != -1) {
624 switch (opt) {
625 case 'r':
626 the_acl = smb_xstrdup(poptGetOptArg(pc));
627 mode = SMB_ACL_DELETE;
628 break;
630 case 'm':
631 the_acl = smb_xstrdup(poptGetOptArg(pc));
632 mode = SMB_ACL_MODIFY;
633 break;
635 case 'a':
636 the_acl = smb_xstrdup(poptGetOptArg(pc));
637 mode = SMB_ACL_ADD;
638 break;
640 case 'R':
641 the_acl = smb_xstrdup(poptGetOptArg(pc));
642 mode = SMB_ACL_SET;
643 break;
645 case 'D':
646 mode = SMB_SD_DELETE;
647 break;
649 case 'S':
650 mode = SMB_SD_SETSDDL;
651 the_acl = smb_xstrdup(poptGetOptArg(pc));
652 break;
654 case 'V':
655 mode = SMB_SD_VIEWSDDL;
656 break;
658 case 'v':
659 mode = SMB_ACL_VIEW;
660 break;
662 case 'F':
663 force_acl = True;
664 break;
666 case 'M':
667 initialize_sid = True;
668 break;
669 case OPT_VIEW_ALL:
670 mode = SMB_ACL_VIEW_ALL;
671 break;
675 setlinebuf(stdout);
677 lp_load_with_registry_shares( get_dyn_CONFIGFILE(), False, False, False,
678 True );
680 /* check for initializing secrets.tdb first */
682 if ( initialize_sid ) {
683 struct dom_sid *sid = get_global_sam_sid();
685 if ( !sid ) {
686 fprintf( stderr, "Failed to retrieve Machine SID!\n");
687 return 3;
690 printf ("%s\n", sid_string_tos( sid ) );
691 return 0;
694 if ( mode == SMB_ACL_VIEW && force_acl ) {
695 fprintf( stderr, "Invalid combination of -F and -v\n");
696 return -1;
699 if (mode == SMB_ACL_VIEW_ALL) {
700 int i;
702 for (i=0; i<lp_numservices(); i++) {
703 TALLOC_CTX *frame = talloc_stackframe();
704 const char *service = lp_servicename(frame, i);
706 if (service == NULL) {
707 continue;
710 printf("[%s]\n", service);
711 change_share_sec(frame, service, NULL, SMB_ACL_VIEW);
712 printf("\n");
713 TALLOC_FREE(frame);
715 goto done;
718 /* get the sharename */
720 if(!poptPeekArg(pc)) {
721 poptPrintUsage(pc, stderr, 0);
722 return -1;
725 fstrcpy(sharename, poptGetArg(pc));
727 snum = lp_servicenumber( sharename );
729 if ( snum == -1 && !force_acl ) {
730 fprintf( stderr, "Invalid sharename: %s\n", sharename);
731 return -1;
734 switch (mode) {
735 case SMB_SD_SETSDDL:
736 retval = set_sharesec_sddl(sharename, the_acl);
737 break;
738 case SMB_SD_VIEWSDDL:
739 retval = view_sharesec_sddl(sharename);
740 break;
741 default:
742 retval = change_share_sec(ctx, sharename, the_acl, mode);
743 break;
746 done:
747 talloc_destroy(ctx);
749 return retval;