2 Unix SMB/CIFS implementation.
5 Copyright (C) Andrew Tridgell 2000
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jeremy Allison 2000
8 Copyright (C) Jelmer Vernooij 2003
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.
27 static fstring password;
28 static pstring username;
29 static pstring owner_username;
30 static fstring server;
32 static int test_args = False;
33 static TALLOC_CTX *ctx;
35 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
36 #define CREATE_ACCESS_WRITE (WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS)
38 /* numeric is set when the user wants numeric SIDs and ACEs rather
39 than going via LSA calls to resolve them */
40 static BOOL numeric = False;
42 enum acl_mode {SMB_ACL_SET, SMB_ACL_DELETE, SMB_ACL_MODIFY, SMB_ACL_ADD };
43 enum chown_mode {REQUEST_NONE, REQUEST_CHOWN, REQUEST_CHGRP};
44 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
51 /* These values discovered by inspection */
53 static const struct perm_value special_values[] = {
63 static const struct perm_value standard_values[] = {
64 { "READ", 0x001200a9 },
65 { "CHANGE", 0x001301bf },
66 { "FULL", 0x001f01ff },
70 static struct cli_state *global_hack_cli;
71 static POLICY_HND pol;
72 static BOOL got_policy_hnd;
74 static struct cli_state *connect_one(const char *share);
76 /* Open cli connection and policy handle */
78 static BOOL cacls_open_policy_hnd(void)
80 /* Initialise cli LSA connection */
82 if (!global_hack_cli) {
83 global_hack_cli = connect_one("IPC$");
84 if (!cli_nt_session_open (global_hack_cli, PI_LSARPC)) {
89 /* Open policy handle */
91 if (!got_policy_hnd) {
93 /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
94 but NT sends 0x2000000 so we might as well do it too. */
96 if (!NT_STATUS_IS_OK(cli_lsa_open_policy(global_hack_cli, global_hack_cli->mem_ctx, True,
97 GENERIC_EXECUTE_ACCESS, &pol))) {
101 got_policy_hnd = True;
107 /* convert a SID to a string, either numeric or username/group */
108 static void SidToString(fstring str, DOM_SID *sid)
110 char **domains = NULL;
112 uint32 *types = NULL;
114 sid_to_string(str, sid);
118 /* Ask LSA to convert the sid to a name */
120 if (!cacls_open_policy_hnd() ||
121 !NT_STATUS_IS_OK(cli_lsa_lookup_sids(global_hack_cli, global_hack_cli->mem_ctx,
122 &pol, 1, sid, &domains,
124 !domains || !domains[0] || !names || !names[0]) {
130 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
131 domains[0], lp_winbind_separator(),
136 /* convert a string to a SID, either numeric or username/group */
137 static BOOL StringToSid(DOM_SID *sid, const char *str)
139 uint32 *types = NULL;
140 DOM_SID *sids = NULL;
143 if (strncmp(str, "S-", 2) == 0) {
144 return string_to_sid(sid, str);
147 if (!cacls_open_policy_hnd() ||
148 !NT_STATUS_IS_OK(cli_lsa_lookup_names(global_hack_cli, global_hack_cli->mem_ctx,
149 &pol, 1, &str, &sids,
155 sid_copy(sid, &sids[0]);
162 /* print an ACE on a FILE, using either numeric or ascii representation */
163 static void print_ace(FILE *f, SEC_ACE *ace)
165 const struct perm_value *v;
170 SidToString(sidstr, &ace->trustee);
172 fprintf(f, "%s:", sidstr);
175 fprintf(f, "%d/%d/0x%08x",
176 ace->type, ace->flags, ace->info.mask);
182 if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
183 fprintf(f, "ALLOWED");
184 } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
185 fprintf(f, "DENIED");
187 fprintf(f, "%d", ace->type);
190 /* Not sure what flags can be set in a file ACL */
192 fprintf(f, "/%d/", ace->flags);
194 /* Standard permissions */
196 for (v = standard_values; v->perm; v++) {
197 if (ace->info.mask == v->mask) {
198 fprintf(f, "%s", v->perm);
203 /* Special permissions. Print out a hex value if we have
204 leftover bits in the mask. */
206 got_mask = ace->info.mask;
209 for (v = special_values; v->perm; v++) {
210 if ((ace->info.mask & v->mask) == v->mask) {
212 fprintf(f, "%s", v->perm);
214 got_mask &= ~v->mask;
220 fprintf(f, "0x%08x", ace->info.mask);
229 /* parse an ACE in the same format as print_ace() */
230 static BOOL parse_ace(SEC_ACE *ace, char *str)
235 unsigned atype, aflags, amask;
238 const struct perm_value *v;
241 p = strchr_m(str,':');
242 if (!p) return False;
245 /* Try to parse numeric form */
247 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
248 StringToSid(&sid, str)) {
252 /* Try to parse text form */
254 if (!StringToSid(&sid, str)) {
259 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
263 if (strncmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
264 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
265 } else if (strncmp(tok, "DENIED", strlen("DENIED")) == 0) {
266 atype = SEC_ACE_TYPE_ACCESS_DENIED;
271 /* Only numeric form accepted for flags at present */
273 if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
274 sscanf(tok, "%i", &aflags))) {
278 if (!next_token(&cp, tok, "/", sizeof(fstring))) {
282 if (strncmp(tok, "0x", 2) == 0) {
283 if (sscanf(tok, "%i", &amask) != 1) {
289 for (v = standard_values; v->perm; v++) {
290 if (strcmp(tok, v->perm) == 0) {
301 for (v = special_values; v->perm; v++) {
302 if (v->perm[0] == *p) {
308 if (!found) return False;
318 init_sec_ace(ace, &sid, atype, mask, aflags);
322 /* add an ACE to a list of ACEs in a SEC_ACL */
323 static BOOL add_ace(SEC_ACL **the_acl, SEC_ACE *ace)
328 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
332 aces = calloc(1+(*the_acl)->num_aces,sizeof(SEC_ACE));
333 memcpy(aces, (*the_acl)->ace, (*the_acl)->num_aces * sizeof(SEC_ACE));
334 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
335 new = make_sec_acl(ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
341 /* parse a ascii version of a security descriptor */
342 static SEC_DESC *sec_desc_parse(char *str)
348 DOM_SID *grp_sid=NULL, *owner_sid=NULL;
352 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
354 if (strncmp(tok,"REVISION:", 9) == 0) {
355 revision = strtol(tok+9, NULL, 16);
359 if (strncmp(tok,"OWNER:", 6) == 0) {
360 owner_sid = (DOM_SID *)calloc(1, sizeof(DOM_SID));
362 !StringToSid(owner_sid, tok+6)) {
363 printf("Failed to parse owner sid\n");
369 if (strncmp(tok,"GROUP:", 6) == 0) {
370 grp_sid = (DOM_SID *)calloc(1, sizeof(DOM_SID));
372 !StringToSid(grp_sid, tok+6)) {
373 printf("Failed to parse group sid\n");
379 if (strncmp(tok,"ACL:", 4) == 0) {
381 if (!parse_ace(&ace, tok+4)) {
382 printf("Failed to parse ACL %s\n", tok);
385 if(!add_ace(&dacl, &ace)) {
386 printf("Failed to add ACL %s\n", tok);
392 printf("Failed to parse security descriptor\n");
396 ret = make_sec_desc(ctx,revision, owner_sid, grp_sid,
397 NULL, dacl, &sd_size);
400 SAFE_FREE(owner_sid);
406 /* print a ascii version of a security descriptor on a FILE handle */
407 static void sec_desc_print(FILE *f, SEC_DESC *sd)
412 printf("REVISION:%d\n", sd->revision);
414 /* Print owner and group sid */
417 SidToString(sidstr, sd->owner_sid);
422 printf("OWNER:%s\n", sidstr);
425 SidToString(sidstr, sd->grp_sid);
430 fprintf(f, "GROUP:%s\n", sidstr);
433 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
434 SEC_ACE *ace = &sd->dacl->ace[i];
442 /*****************************************************
443 dump the acls for a file
444 *******************************************************/
445 static int cacl_dump(struct cli_state *cli, char *filename)
447 int result = EXIT_FAILED;
454 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
457 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
461 sd = cli_query_secdesc(cli, fnum, ctx);
464 printf("ERROR: secdesc query failed: %s\n", cli_errstr(cli));
468 sec_desc_print(stdout, sd);
474 cli_close(cli, fnum);
479 /*****************************************************
480 Change the ownership or group ownership of a file. Just
481 because the NT docs say this can't be done :-). JRA.
482 *******************************************************/
484 static int owner_set(struct cli_state *cli, enum chown_mode change_mode,
485 char *filename, char *new_username)
492 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
495 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
499 if (!StringToSid(&sid, new_username))
500 return EXIT_PARSE_ERROR;
502 old = cli_query_secdesc(cli, fnum, ctx);
504 cli_close(cli, fnum);
507 printf("owner_set: Failed to query old descriptor\n");
511 sd = make_sec_desc(ctx,old->revision,
512 (change_mode == REQUEST_CHOWN) ? &sid : old->owner_sid,
513 (change_mode == REQUEST_CHGRP) ? &sid : old->grp_sid,
514 NULL, old->dacl, &sd_size);
516 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
519 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
523 if (!cli_set_secdesc(cli, fnum, sd)) {
524 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
527 cli_close(cli, fnum);
533 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
534 However NT4 gives a "The information may have been modified by a
535 computer running Windows NT 5.0" if denied ACEs do not appear before
538 static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
540 if (sec_ace_equal(ace1, ace2))
543 if (ace1->type != ace2->type)
544 return ace2->type - ace1->type;
546 if (sid_compare(&ace1->trustee, &ace2->trustee))
547 return sid_compare(&ace1->trustee, &ace2->trustee);
549 if (ace1->flags != ace2->flags)
550 return ace1->flags - ace2->flags;
552 if (ace1->info.mask != ace2->info.mask)
553 return ace1->info.mask - ace2->info.mask;
555 if (ace1->size != ace2->size)
556 return ace1->size - ace2->size;
558 return memcmp(ace1, ace2, sizeof(SEC_ACE));
561 static void sort_acl(SEC_ACL *the_acl)
564 if (!the_acl) return;
566 qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]), QSORT_CAST ace_compare);
568 for (i=1;i<the_acl->num_aces;) {
569 if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
571 for (j=i; j<the_acl->num_aces-1; j++) {
572 the_acl->ace[j] = the_acl->ace[j+1];
581 /*****************************************************
582 set the ACLs on a file given an ascii description
583 *******************************************************/
584 static int cacl_set(struct cli_state *cli, char *filename,
585 char *the_acl, enum acl_mode mode)
591 int result = EXIT_OK;
593 sd = sec_desc_parse(the_acl);
595 if (!sd) return EXIT_PARSE_ERROR;
596 if (test_args) return EXIT_OK;
598 /* The desired access below is the only one I could find that works
599 with NT4, W2KP and Samba */
601 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
604 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
608 old = cli_query_secdesc(cli, fnum, ctx);
611 printf("calc_set: Failed to query old descriptor\n");
615 cli_close(cli, fnum);
617 /* the logic here is rather more complex than I would like */
620 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
623 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
624 if (sec_ace_equal(&sd->dacl->ace[i],
625 &old->dacl->ace[j])) {
627 for (k=j; k<old->dacl->num_aces-1;k++) {
628 old->dacl->ace[k] = old->dacl->ace[k+1];
630 old->dacl->num_aces--;
631 if (old->dacl->num_aces == 0) {
632 SAFE_FREE(old->dacl->ace);
633 SAFE_FREE(old->dacl);
642 printf("ACL for ACE:");
643 print_ace(stdout, &sd->dacl->ace[i]);
644 printf(" not found\n");
650 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
653 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
654 if (sid_equal(&sd->dacl->ace[i].trustee,
655 &old->dacl->ace[j].trustee)) {
656 old->dacl->ace[j] = sd->dacl->ace[i];
664 SidToString(str, &sd->dacl->ace[i].trustee);
665 printf("ACL for SID %s not found\n", str);
672 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
673 add_ace(&old->dacl, &sd->dacl->ace[i]);
682 /* Denied ACE entries must come before allowed ones */
685 /* Create new security descriptor and set it */
686 sd = make_sec_desc(ctx,old->revision, old->owner_sid, old->grp_sid,
687 NULL, old->dacl, &sd_size);
689 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
692 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
696 if (!cli_set_secdesc(cli, fnum, sd)) {
697 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
698 result = EXIT_FAILED;
703 cli_close(cli, fnum);
709 /*****************************************************
710 return a connection to a server
711 *******************************************************/
712 static struct cli_state *connect_one(const char *share)
720 char *pass = getpass("Password: ");
722 fstrcpy(password, pass);
727 if (NT_STATUS_IS_OK(nt_status = cli_full_connection(&c, global_myname(), server,
730 username, lp_workgroup(),
731 password, 0, NULL))) {
734 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
739 /****************************************************************************
741 ****************************************************************************/
742 int main(int argc, const char *argv[])
747 enum acl_mode mode = SMB_ACL_SET;
748 static char *the_acl = NULL;
749 enum chown_mode change_mode = REQUEST_NONE;
754 struct poptOption long_options[] = {
756 { "delete", 'D', POPT_ARG_STRING, NULL, 'D', "Delete an acl", "ACL" },
757 { "modify", 'M', POPT_ARG_STRING, NULL, 'M', "Modify an acl", "ACL" },
758 { "add", 'A', POPT_ARG_STRING, NULL, 'A', "Add an acl", "ACL" },
759 { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls", "ACLS" },
760 { "chown", 'C', POPT_ARG_STRING, NULL, 'C', "Change ownership of a file", "USERNAME" },
761 { "chgrp", 'G', POPT_ARG_STRING, NULL, 'G', "Change group ownership of a file", "GROUPNAME" },
762 { "numeric", 'n', POPT_ARG_VAL, &numeric, True, "Don't resolve sids or masks to names" },
763 { "test-args", 't', POPT_ARG_VAL, &test_args, True, "Test arguments"},
764 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_debug },
765 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_version },
766 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_configfile },
767 {"username", 'U', POPT_ARG_STRING, NULL, 'U', "User to authenticate as", "user%password" },
771 struct cli_state *cli;
773 ctx=talloc_init("main");
779 setup_logging(argv[0],True);
781 lp_load(dyn_CONFIGFILE,True,False,False);
784 if (getenv("USER")) {
785 pstrcpy(username,getenv("USER"));
787 if ((p=strchr_m(username,'%'))) {
789 fstrcpy(password,p+1);
791 memset(strchr_m(getenv("USER"), '%') + 1, 'X',
795 pc = poptGetContext("smbcacls", argc, argv, long_options, 0);
797 poptSetOtherOptionHelp(pc, "//server1/share1 filename [options]");
799 while ((opt = poptGetNextOpt(pc)) != -1) {
802 pstrcpy(username,poptGetOptArg(pc));
803 p = strchr_m(username,'%');
806 fstrcpy(password, p+1);
812 the_acl = smb_xstrdup(poptGetOptArg(pc));
817 the_acl = smb_xstrdup(poptGetOptArg(pc));
818 mode = SMB_ACL_DELETE;
822 the_acl = smb_xstrdup(poptGetOptArg(pc));
823 mode = SMB_ACL_MODIFY;
827 the_acl = smb_xstrdup(poptGetOptArg(pc));
832 pstrcpy(owner_username,poptGetOptArg(pc));
833 change_mode = REQUEST_CHOWN;
837 pstrcpy(owner_username,poptGetOptArg(pc));
838 change_mode = REQUEST_CHGRP;
843 /* Make connection to server */
844 if(!poptPeekArg(pc)) {
845 poptPrintUsage(pc, stderr, 0);
849 fstrcpy(path, poptGetArg(pc));
851 if(!poptPeekArg(pc)) {
852 poptPrintUsage(pc, stderr, 0);
856 pstrcpy(filename, poptGetArg(pc));
858 all_string_sub(path,"/","\\",0);
860 fstrcpy(server,path+2);
861 share = strchr_m(server,'\\');
863 share = strchr_m(server,'/');
865 printf("Invalid argument: %s\n", share);
874 cli = connect_one(share);
883 all_string_sub(filename, "/", "\\", 0);
884 if (filename[0] != '\\') {
887 safe_strcpy(&s[1], filename, sizeof(pstring)-2);
888 pstrcpy(filename, s);
891 /* Perform requested action */
893 if (change_mode != REQUEST_NONE) {
894 result = owner_set(cli, change_mode, filename, owner_username);
895 } else if (the_acl) {
896 result = cacl_set(cli, filename, the_acl, mode);
898 result = cacl_dump(cli, filename);