2 Unix SMB/CIFS implementation.
5 Copyright (C) Andrew Tridgell 2000
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jeremy Allison 2000
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 static fstring password;
27 static pstring username;
28 static pstring owner_username;
29 static fstring server;
32 static TALLOC_CTX *ctx;
34 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
35 #define CREATE_ACCESS_WRITE (WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS)
37 /* numeric is set when the user wants numeric SIDs and ACEs rather
38 than going via LSA calls to resolve them */
41 enum acl_mode {SMB_ACL_SET, SMB_ACL_DELETE, SMB_ACL_MODIFY, SMB_ACL_ADD };
42 enum chown_mode {REQUEST_NONE, REQUEST_CHOWN, REQUEST_CHGRP};
43 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
50 /* These values discovered by inspection */
52 static struct perm_value special_values[] = {
62 static struct perm_value standard_values[] = {
63 { "READ", 0x001200a9 },
64 { "CHANGE", 0x001301bf },
65 { "FULL", 0x001f01ff },
69 static struct cli_state *global_hack_cli;
70 static POLICY_HND pol;
71 static BOOL got_policy_hnd;
73 static struct cli_state *connect_one(char *share);
75 /* Open cli connection and policy handle */
77 static BOOL cacls_open_policy_hnd(void)
79 /* Initialise cli LSA connection */
81 if (!global_hack_cli) {
82 global_hack_cli = connect_one("IPC$");
83 if (!cli_nt_session_open (global_hack_cli, PI_LSARPC)) {
88 /* Open policy handle */
90 if (!got_policy_hnd) {
92 /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
93 but NT sends 0x2000000 so we might as well do it too. */
95 if (!NT_STATUS_IS_OK(cli_lsa_open_policy(global_hack_cli, global_hack_cli->mem_ctx, True,
96 GENERIC_EXECUTE_ACCESS, &pol))) {
100 got_policy_hnd = True;
106 /* convert a SID to a string, either numeric or username/group */
107 static void SidToString(fstring str, DOM_SID *sid)
109 char **domains = NULL;
111 uint32 *types = NULL;
113 sid_to_string(str, sid);
117 /* Ask LSA to convert the sid to a name */
119 if (!cacls_open_policy_hnd() ||
120 !NT_STATUS_IS_OK(cli_lsa_lookup_sids(global_hack_cli, global_hack_cli->mem_ctx,
121 &pol, 1, sid, &domains,
123 !domains || !domains[0] || !names || !names[0]) {
129 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
130 domains[0], lp_winbind_separator(),
135 /* convert a string to a SID, either numeric or username/group */
136 static BOOL StringToSid(DOM_SID *sid, const char *str)
138 uint32 *types = NULL;
139 DOM_SID *sids = NULL;
142 if (strncmp(str, "S-", 2) == 0) {
143 return string_to_sid(sid, str);
146 if (!cacls_open_policy_hnd() ||
147 !NT_STATUS_IS_OK(cli_lsa_lookup_names(global_hack_cli, global_hack_cli->mem_ctx,
148 &pol, 1, &str, &sids,
154 sid_copy(sid, &sids[0]);
161 /* print an ACE on a FILE, using either numeric or ascii representation */
162 static void print_ace(FILE *f, SEC_ACE *ace)
164 struct perm_value *v;
169 SidToString(sidstr, &ace->trustee);
171 fprintf(f, "%s:", sidstr);
174 fprintf(f, "%d/%d/0x%08x",
175 ace->type, ace->flags, ace->info.mask);
181 if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
182 fprintf(f, "ALLOWED");
183 } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
184 fprintf(f, "DENIED");
186 fprintf(f, "%d", ace->type);
189 /* Not sure what flags can be set in a file ACL */
191 fprintf(f, "/%d/", ace->flags);
193 /* Standard permissions */
195 for (v = standard_values; v->perm; v++) {
196 if (ace->info.mask == v->mask) {
197 fprintf(f, "%s", v->perm);
202 /* Special permissions. Print out a hex value if we have
203 leftover bits in the mask. */
205 got_mask = ace->info.mask;
208 for (v = special_values; v->perm; v++) {
209 if ((ace->info.mask & v->mask) == v->mask) {
211 fprintf(f, "%s", v->perm);
213 got_mask &= ~v->mask;
219 fprintf(f, "0x%08x", ace->info.mask);
228 /* parse an ACE in the same format as print_ace() */
229 static BOOL parse_ace(SEC_ACE *ace, char *str)
234 unsigned atype, aflags, amask;
237 struct perm_value *v;
240 p = strchr_m(str,':');
241 if (!p) return False;
244 /* Try to parse numeric form */
246 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
247 StringToSid(&sid, str)) {
251 /* Try to parse text form */
253 if (!StringToSid(&sid, str)) {
258 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
262 if (strncmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
263 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
264 } else if (strncmp(tok, "DENIED", strlen("DENIED")) == 0) {
265 atype = SEC_ACE_TYPE_ACCESS_DENIED;
270 /* Only numeric form accepted for flags at present */
272 if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
273 sscanf(tok, "%i", &aflags))) {
277 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
281 if (strncmp(tok, "0x", 2) == 0) {
282 if (sscanf(tok, "%i", &amask) != 1) {
288 for (v = standard_values; v->perm; v++) {
289 if (strcmp(tok, v->perm) == 0) {
300 for (v = special_values; v->perm; v++) {
301 if (v->perm[0] == *p) {
307 if (!found) return False;
317 init_sec_ace(ace, &sid, atype, mask, aflags);
321 /* add an ACE to a list of ACEs in a SEC_ACL */
322 static BOOL add_ace(SEC_ACL **the_acl, SEC_ACE *ace)
327 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
331 aces = calloc(1+(*the_acl)->num_aces,sizeof(SEC_ACE));
332 memcpy(aces, (*the_acl)->ace, (*the_acl)->num_aces * sizeof(SEC_ACE));
333 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
334 new = make_sec_acl(ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
340 /* parse a ascii version of a security descriptor */
341 static SEC_DESC *sec_desc_parse(char *str)
347 DOM_SID *grp_sid=NULL, *owner_sid=NULL;
351 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
353 if (strncmp(tok,"REVISION:", 9) == 0) {
354 revision = strtol(tok+9, NULL, 16);
358 if (strncmp(tok,"OWNER:", 6) == 0) {
359 owner_sid = (DOM_SID *)calloc(1, sizeof(DOM_SID));
361 !StringToSid(owner_sid, tok+6)) {
362 printf("Failed to parse owner sid\n");
368 if (strncmp(tok,"GROUP:", 6) == 0) {
369 grp_sid = (DOM_SID *)calloc(1, sizeof(DOM_SID));
371 !StringToSid(grp_sid, tok+6)) {
372 printf("Failed to parse group sid\n");
378 if (strncmp(tok,"ACL:", 4) == 0) {
380 if (!parse_ace(&ace, tok+4)) {
381 printf("Failed to parse ACL %s\n", tok);
384 if(!add_ace(&dacl, &ace)) {
385 printf("Failed to add ACL %s\n", tok);
391 printf("Failed to parse security descriptor\n");
395 ret = make_sec_desc(ctx,revision, owner_sid, grp_sid,
396 NULL, dacl, &sd_size);
399 SAFE_FREE(owner_sid);
405 /* print a ascii version of a security descriptor on a FILE handle */
406 static void sec_desc_print(FILE *f, SEC_DESC *sd)
411 printf("REVISION:%d\n", sd->revision);
413 /* Print owner and group sid */
416 SidToString(sidstr, sd->owner_sid);
421 printf("OWNER:%s\n", sidstr);
424 SidToString(sidstr, sd->grp_sid);
429 fprintf(f, "GROUP:%s\n", sidstr);
432 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
433 SEC_ACE *ace = &sd->dacl->ace[i];
441 /*****************************************************
442 dump the acls for a file
443 *******************************************************/
444 static int cacl_dump(struct cli_state *cli, char *filename)
449 if (test_args) return EXIT_OK;
451 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
453 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
457 sd = cli_query_secdesc(cli, fnum, ctx);
460 printf("ERROR: secdesc query failed: %s\n", cli_errstr(cli));
464 sec_desc_print(stdout, sd);
466 cli_close(cli, fnum);
471 /*****************************************************
472 Change the ownership or group ownership of a file. Just
473 because the NT docs say this can't be done :-). JRA.
474 *******************************************************/
476 static int owner_set(struct cli_state *cli, enum chown_mode change_mode,
477 char *filename, char *new_username)
484 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
487 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
491 if (!StringToSid(&sid, new_username))
492 return EXIT_PARSE_ERROR;
494 old = cli_query_secdesc(cli, fnum, ctx);
496 cli_close(cli, fnum);
499 printf("owner_set: Failed to query old descriptor\n");
503 sd = make_sec_desc(ctx,old->revision,
504 (change_mode == REQUEST_CHOWN) ? &sid : old->owner_sid,
505 (change_mode == REQUEST_CHGRP) ? &sid : old->grp_sid,
506 NULL, old->dacl, &sd_size);
508 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
511 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
515 if (!cli_set_secdesc(cli, fnum, sd)) {
516 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
519 cli_close(cli, fnum);
525 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
526 However NT4 gives a "The information may have been modified by a
527 computer running Windows NT 5.0" if denied ACEs do not appear before
530 static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
532 if (sec_ace_equal(ace1, ace2))
535 if (ace1->type != ace2->type)
536 return ace2->type - ace1->type;
538 if (sid_compare(&ace1->trustee, &ace2->trustee))
539 return sid_compare(&ace1->trustee, &ace2->trustee);
541 if (ace1->flags != ace2->flags)
542 return ace1->flags - ace2->flags;
544 if (ace1->info.mask != ace2->info.mask)
545 return ace1->info.mask - ace2->info.mask;
547 if (ace1->size != ace2->size)
548 return ace1->size - ace2->size;
550 return memcmp(ace1, ace2, sizeof(SEC_ACE));
553 static void sort_acl(SEC_ACL *the_acl)
556 if (!the_acl) return;
558 qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]), QSORT_CAST ace_compare);
560 for (i=1;i<the_acl->num_aces;) {
561 if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
563 for (j=i; j<the_acl->num_aces-1; j++) {
564 the_acl->ace[j] = the_acl->ace[j+1];
573 /*****************************************************
574 set the ACLs on a file given an ascii description
575 *******************************************************/
576 static int cacl_set(struct cli_state *cli, char *filename,
577 char *the_acl, enum acl_mode mode)
583 int result = EXIT_OK;
585 sd = sec_desc_parse(the_acl);
587 if (!sd) return EXIT_PARSE_ERROR;
588 if (test_args) return EXIT_OK;
590 /* The desired access below is the only one I could find that works
591 with NT4, W2KP and Samba */
593 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
596 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
600 old = cli_query_secdesc(cli, fnum, ctx);
603 printf("calc_set: Failed to query old descriptor\n");
607 cli_close(cli, fnum);
609 /* the logic here is rather more complex than I would like */
612 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
615 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
616 if (sec_ace_equal(&sd->dacl->ace[i],
617 &old->dacl->ace[j])) {
619 for (k=j; k<old->dacl->num_aces-1;k++) {
620 old->dacl->ace[k] = old->dacl->ace[k+1];
622 old->dacl->num_aces--;
623 if (old->dacl->num_aces == 0) {
624 SAFE_FREE(old->dacl->ace);
625 SAFE_FREE(old->dacl);
634 printf("ACL for ACE:");
635 print_ace(stdout, &sd->dacl->ace[i]);
636 printf(" not found\n");
642 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
645 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
646 if (sid_equal(&sd->dacl->ace[i].trustee,
647 &old->dacl->ace[j].trustee)) {
648 old->dacl->ace[j] = sd->dacl->ace[i];
656 SidToString(str, &sd->dacl->ace[i].trustee);
657 printf("ACL for SID %s not found\n", str);
664 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
665 add_ace(&old->dacl, &sd->dacl->ace[i]);
674 /* Denied ACE entries must come before allowed ones */
677 /* Create new security descriptor and set it */
678 sd = make_sec_desc(ctx,old->revision, old->owner_sid, old->grp_sid,
679 NULL, old->dacl, &sd_size);
681 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
684 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
688 if (!cli_set_secdesc(cli, fnum, sd)) {
689 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
690 result = EXIT_FAILED;
695 cli_close(cli, fnum);
701 /*****************************************************
702 return a connection to a server
703 *******************************************************/
704 static struct cli_state *connect_one(char *share)
712 char *pass = getpass("Password: ");
714 pstrcpy(password, pass);
719 if (NT_STATUS_IS_OK(nt_status = cli_full_connection(&c, global_myname(), server,
722 username, lp_workgroup(),
723 password, 0, NULL))) {
726 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
732 static void usage(void)
735 "Usage: smbcacls //server1/share1 filename [options]\n\
737 \t-D <acls> delete an acl\n\
738 \t-M <acls> modify an acl\n\
739 \t-A <acls> add an acl\n\
740 \t-S <acls> set acls\n\
741 \t-C username change ownership of a file\n\
742 \t-G username change group ownership of a file\n\
743 \t-n don't resolve sids or masks to names\n\
745 \t-d debuglevel set debug output level\n\
746 \t-U username user to autheticate as\n\
748 The username can be of the form username%%password or\n\
749 workgroup\\username%%password.\n\n\
750 An acl is of the form ACL:<SID>:type/flags/mask\n\
751 You can string acls together with spaces, commas or newlines\n\
755 /****************************************************************************
757 ****************************************************************************/
758 int main(int argc,char *argv[])
766 enum acl_mode mode = SMB_ACL_SET;
767 char *the_acl = NULL;
768 enum chown_mode change_mode = REQUEST_NONE;
771 struct cli_state *cli;
779 if (argc < 3 || argv[1][0] == '-') {
782 exit(EXIT_PARSE_ERROR);
785 setup_logging(argv[0],True);
788 pstrcpy(filename, argv[2]);
789 all_string_sub(share,"/","\\",0);
794 lp_load(dyn_CONFIGFILE,True,False,False);
797 if (getenv("USER")) {
798 pstrcpy(username,getenv("USER"));
800 if ((p=strchr_m(username,'%'))) {
802 pstrcpy(password,p+1);
804 memset(strchr_m(getenv("USER"), '%') + 1, 'X',
809 while ((opt = getopt(argc, argv, "U:nhS:D:A:M:C:G:td:")) != EOF) {
812 pstrcpy(username,optarg);
813 p = strchr_m(username,'%');
816 pstrcpy(password, p+1);
828 mode = SMB_ACL_DELETE;
833 mode = SMB_ACL_MODIFY;
842 pstrcpy(owner_username,optarg);
843 change_mode = REQUEST_CHOWN;
847 pstrcpy(owner_username,optarg);
848 change_mode = REQUEST_CHGRP;
862 exit(EXIT_PARSE_ERROR);
865 DEBUGLEVEL = atoi(optarg);
869 printf("Unknown option %c (%d)\n", (char)opt, opt);
871 exit(EXIT_PARSE_ERROR);
881 exit(EXIT_PARSE_ERROR);
884 /* Make connection to server */
886 fstrcpy(server,share+2);
887 share = strchr_m(server,'\\');
889 share = strchr_m(server,'/');
899 cli = connect_one(share);
908 all_string_sub(filename, "/", "\\", 0);
909 if (filename[0] != '\\') {
912 safe_strcpy(&s[1], filename, sizeof(pstring)-1);
913 pstrcpy(filename, s);
916 /* Perform requested action */
918 if (change_mode != REQUEST_NONE) {
919 result = owner_set(cli, change_mode, filename, owner_username);
920 } else if (the_acl) {
921 result = cacl_set(cli, filename, the_acl, mode);
923 result = cacl_dump(cli, filename);