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;
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 struct cli_state lsa_cli;
71 struct ntuser_creds creds;
74 /* Open cli connection and policy handle */
76 static BOOL cacls_open_policy_hnd(void)
78 creds.pwd.null_pwd = 1;
80 /* Initialise cli LSA connection */
82 if (!lsa_cli.initialised &&
83 !cli_lsa_initialise(&lsa_cli, server, &creds)) {
87 /* Open policy handle */
89 if (!got_policy_hnd) {
91 /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
92 but NT sends 0x2000000 so we might as well do it too. */
94 if (!NT_STATUS_IS_OK(cli_lsa_open_policy(&lsa_cli, lsa_cli.mem_ctx, True,
95 GENERIC_EXECUTE_ACCESS, &pol))) {
99 got_policy_hnd = True;
105 /* convert a SID to a string, either numeric or username/group */
106 static void SidToString(fstring str, DOM_SID *sid)
108 char **domains = NULL;
110 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(&lsa_cli, lsa_cli.mem_ctx,
121 &pol, 1, sid, &domains, &names,
122 &types, &num_names)) ||
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;
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(&lsa_cli, lsa_cli.mem_ctx, &pol, 1,
150 &sids, &types, &num_sids))) {
155 sid_copy(sid, &sids[0]);
163 /* print an ACE on a FILE, using either numeric or ascii representation */
164 static void print_ace(FILE *f, SEC_ACE *ace)
166 struct perm_value *v;
171 SidToString(sidstr, &ace->trustee);
173 fprintf(f, "%s:", sidstr);
176 fprintf(f, "%d/%d/0x%08x",
177 ace->type, ace->flags, ace->info.mask);
183 if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
184 fprintf(f, "ALLOWED");
185 } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
186 fprintf(f, "DENIED");
188 fprintf(f, "%d", ace->type);
191 /* Not sure what flags can be set in a file ACL */
193 fprintf(f, "/%d/", ace->flags);
195 /* Standard permissions */
197 for (v = standard_values; v->perm; v++) {
198 if (ace->info.mask == v->mask) {
199 fprintf(f, "%s", v->perm);
204 /* Special permissions. Print out a hex value if we have
205 leftover bits in the mask. */
207 got_mask = ace->info.mask;
210 for (v = special_values; v->perm; v++) {
211 if ((ace->info.mask & v->mask) == v->mask) {
213 fprintf(f, "%s", v->perm);
215 got_mask &= ~v->mask;
221 fprintf(f, "0x%08x", ace->info.mask);
230 /* parse an ACE in the same format as print_ace() */
231 static BOOL parse_ace(SEC_ACE *ace, char *str)
235 unsigned atype, aflags, amask;
238 struct perm_value *v;
241 p = strchr_m(str,':');
242 if (!p) return False;
246 /* Try to parse numeric form */
248 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
249 StringToSid(&sid, str)) {
253 /* Try to parse text form */
255 if (!StringToSid(&sid, str)) {
259 if (!next_token(&p, 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(&p, tok, "/", sizeof(fstring)) &&
274 sscanf(tok, "%i", &aflags))) {
278 if (!next_token(&p, 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)
450 if (test_args) return EXIT_OK;
452 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
454 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
458 sd = cli_query_secdesc(cli, fnum, ctx);
461 printf("ERROR: secdesc query failed: %s\n", cli_errstr(cli));
465 sec_desc_print(stdout, sd);
467 cli_close(cli, fnum);
472 /*****************************************************
473 Change the ownership or group ownership of a file. Just
474 because the NT docs say this can't be done :-). JRA.
475 *******************************************************/
477 static int owner_set(struct cli_state *cli, enum chown_mode change_mode,
478 char *filename, char *new_username)
485 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
488 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
492 if (!StringToSid(&sid, new_username))
493 return EXIT_PARSE_ERROR;
495 old = cli_query_secdesc(cli, fnum, ctx);
497 cli_close(cli, fnum);
500 printf("owner_set: Failed to query old descriptor\n");
504 sd = make_sec_desc(ctx,old->revision,
505 (change_mode == REQUEST_CHOWN) ? &sid : old->owner_sid,
506 (change_mode == REQUEST_CHGRP) ? &sid : old->grp_sid,
507 NULL, old->dacl, &sd_size);
509 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
512 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
516 if (!cli_set_secdesc(cli, fnum, sd)) {
517 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
520 cli_close(cli, fnum);
526 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
527 However NT4 gives a "The information may have been modified by a
528 computer running Windows NT 5.0" if denied ACEs do not appear before
531 static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
533 if (sec_ace_equal(ace1, ace2))
536 if (ace1->type != ace2->type)
537 return ace2->type - ace1->type;
539 if (sid_compare(&ace1->trustee, &ace2->trustee))
540 return sid_compare(&ace1->trustee, &ace2->trustee);
542 if (ace1->flags != ace2->flags)
543 return ace1->flags - ace2->flags;
545 if (ace1->info.mask != ace2->info.mask)
546 return ace1->info.mask - ace2->info.mask;
548 if (ace1->size != ace2->size)
549 return ace1->size - ace2->size;
551 return memcmp(ace1, ace2, sizeof(SEC_ACE));
554 static void sort_acl(SEC_ACL *the_acl)
557 if (!the_acl) return;
559 qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]), QSORT_CAST ace_compare);
561 for (i=1;i<the_acl->num_aces;) {
562 if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
564 for (j=i; j<the_acl->num_aces-1; j++) {
565 the_acl->ace[j] = the_acl->ace[j+1];
574 /*****************************************************
575 set the ACLs on a file given an ascii description
576 *******************************************************/
577 static int cacl_set(struct cli_state *cli, char *filename,
578 char *the_acl, enum acl_mode mode)
584 int result = EXIT_OK;
586 sd = sec_desc_parse(the_acl);
588 if (!sd) return EXIT_PARSE_ERROR;
589 if (test_args) return EXIT_OK;
591 /* The desired access below is the only one I could find that works
592 with NT4, W2KP and Samba */
594 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
597 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
601 old = cli_query_secdesc(cli, fnum, ctx);
604 printf("calc_set: Failed to query old descriptor\n");
608 cli_close(cli, fnum);
610 /* the logic here is rather more complex than I would like */
613 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
616 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
617 if (sec_ace_equal(&sd->dacl->ace[i],
618 &old->dacl->ace[j])) {
620 for (k=j; k<old->dacl->num_aces-1;k++) {
621 old->dacl->ace[k] = old->dacl->ace[k+1];
623 old->dacl->num_aces--;
624 if (old->dacl->num_aces == 0) {
625 SAFE_FREE(old->dacl->ace);
626 SAFE_FREE(old->dacl);
635 printf("ACL for ACE:");
636 print_ace(stdout, &sd->dacl->ace[i]);
637 printf(" not found\n");
643 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
646 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
647 if (sid_equal(&sd->dacl->ace[i].trustee,
648 &old->dacl->ace[j].trustee)) {
649 old->dacl->ace[j] = sd->dacl->ace[i];
657 SidToString(str, &sd->dacl->ace[i].trustee);
658 printf("ACL for SID %s not found\n", str);
665 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
666 add_ace(&old->dacl, &sd->dacl->ace[i]);
675 /* Denied ACE entries must come before allowed ones */
678 /* Create new security descriptor and set it */
679 sd = make_sec_desc(ctx,old->revision, old->owner_sid, old->grp_sid,
680 NULL, old->dacl, &sd_size);
682 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
685 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
689 if (!cli_set_secdesc(cli, fnum, sd)) {
690 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
691 result = EXIT_FAILED;
696 cli_close(cli, fnum);
702 /*****************************************************
703 return a connection to a server
704 *******************************************************/
705 struct cli_state *connect_one(char *share)
708 struct nmb_name called, calling;
710 extern pstring global_myname;
712 fstrcpy(server,share+2);
713 share = strchr_m(server,'\\');
714 if (!share) return NULL;
720 make_nmb_name(&calling, global_myname, 0x0);
721 make_nmb_name(&called , server, 0x20);
726 /* have to open a new connection */
727 if (!(c=cli_initialise(NULL)) || !cli_connect(c, server, &ip)) {
728 DEBUG(0,("Connection to %s failed\n", server));
733 if (!cli_session_request(c, &calling, &called)) {
734 DEBUG(0,("session request to %s failed\n", called.name));
736 if (strcmp(called.name, "*SMBSERVER")) {
737 make_nmb_name(&called , "*SMBSERVER", 0x20);
743 DEBUG(4,(" session request ok\n"));
745 if (!cli_negprot(c)) {
746 DEBUG(0,("protocol negotiation failed\n"));
752 char *pass = getpass("Password: ");
754 pstrcpy(password, pass);
758 if (!cli_session_setup(c, username,
759 password, strlen(password),
760 password, strlen(password),
762 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
767 DEBUG(4,(" session setup ok\n"));
769 if (!cli_send_tconX(c, share, "?????",
770 password, strlen(password)+1)) {
771 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
776 DEBUG(4,(" tconx ok\n"));
782 static void usage(void)
785 "Usage: smbcacls //server1/share1 filename [options]\n\
787 \t-D <acls> delete an acl\n\
788 \t-M <acls> modify an acl\n\
789 \t-A <acls> add an acl\n\
790 \t-S <acls> set acls\n\
791 \t-C username change ownership of a file\n\
792 \t-G username change group ownership of a file\n\
793 \t-n don't resolve sids or masks to names\n\
795 \t-d debuglevel set debug output level\n\
796 \t-U username user to autheticate as\n\
798 The username can be of the form username%%password or\n\
799 workgroup\\username%%password.\n\n\
800 An acl is of the form ACL:<SID>:type/flags/mask\n\
801 You can string acls together with spaces, commas or newlines\n\
805 /****************************************************************************
807 ****************************************************************************/
808 int main(int argc,char *argv[])
816 struct cli_state *cli=NULL;
817 enum acl_mode mode = SMB_ACL_SET;
818 char *the_acl = NULL;
819 enum chown_mode change_mode = REQUEST_NONE;
828 if (argc < 3 || argv[1][0] == '-') {
831 exit(EXIT_PARSE_ERROR);
834 setup_logging(argv[0],True);
837 pstrcpy(filename, argv[2]);
838 all_string_sub(share,"/","\\",0);
843 lp_load(dyn_CONFIGFILE,True,False,False);
846 if (getenv("USER")) {
847 pstrcpy(username,getenv("USER"));
849 if ((p=strchr_m(username,'%'))) {
851 pstrcpy(password,p+1);
853 memset(strchr_m(getenv("USER"), '%') + 1, 'X',
858 while ((opt = getopt(argc, argv, "U:nhS:D:A:M:C:G:td:")) != EOF) {
861 pstrcpy(username,optarg);
862 p = strchr_m(username,'%');
865 pstrcpy(password, p+1);
877 mode = SMB_ACL_DELETE;
882 mode = SMB_ACL_MODIFY;
891 pstrcpy(owner_username,optarg);
892 change_mode = REQUEST_CHOWN;
896 pstrcpy(owner_username,optarg);
897 change_mode = REQUEST_CHGRP;
911 exit(EXIT_PARSE_ERROR);
914 DEBUGLEVEL = atoi(optarg);
918 printf("Unknown option %c (%d)\n", (char)opt, opt);
920 exit(EXIT_PARSE_ERROR);
930 exit(EXIT_PARSE_ERROR);
933 /* Make connection to server */
936 cli = connect_one(share);
943 all_string_sub(filename, "/", "\\", 0);
944 if (filename[0] != '\\') {
947 safe_strcpy(&s[1], filename, sizeof(pstring)-1);
948 pstrcpy(filename, s);
951 /* Perform requested action */
953 if (change_mode != REQUEST_NONE) {
954 result = owner_set(cli, change_mode, filename, owner_username);
955 } else if (the_acl) {
956 result = cacl_set(cli, filename, the_acl, mode);
958 result = cacl_dump(cli, filename);