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
;
30 static fstring workgroup
= "";
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 */
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 struct perm_value special_values
[] = {
63 static struct perm_value standard_values
[] = {
64 { "READ", 0x001200a9 },
65 { "CHANGE", 0x001301bf },
66 { "FULL", 0x001f01ff },
70 struct cli_state lsa_cli
;
72 struct ntuser_creds creds
;
75 /* Open cli connection and policy handle */
77 static BOOL
cacls_open_policy_hnd(void)
79 creds
.pwd
.null_pwd
= 1;
81 /* Initialise cli LSA connection */
83 if (!lsa_cli
.initialised
&&
84 !cli_lsa_initialise(&lsa_cli
, server
, &creds
)) {
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(&lsa_cli
, lsa_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 if (strcmp(str
, "S-1-1-0") == 0) {
119 fstrcpy(str
, "everyone");
124 /* Ask LSA to convert the sid to a name */
126 if (!cacls_open_policy_hnd() ||
127 !NT_STATUS_IS_OK(cli_lsa_lookup_sids(&lsa_cli
, lsa_cli
.mem_ctx
,
128 &pol
, 1, sid
, &domains
,
130 !domains
|| !domains
[0] || !names
|| !names
[0]) {
136 slprintf(str
, sizeof(fstring
) - 1, "%s%s%s",
137 domains
[0], lp_winbind_separator(),
142 /* convert a string to a SID, either numeric or username/group */
143 static BOOL
StringToSid(DOM_SID
*sid
, const char *str
)
145 uint32
*types
= NULL
;
146 DOM_SID
*sids
= NULL
;
149 if (strncmp(str
, "S-", 2) == 0) {
150 return string_to_sid(sid
, str
);
153 if (!cacls_open_policy_hnd() ||
154 !NT_STATUS_IS_OK(cli_lsa_lookup_names(&lsa_cli
, lsa_cli
.mem_ctx
,
155 &pol
, 1, &str
, &sids
,
161 sid_copy(sid
, &sids
[0]);
169 /* print an ACE on a FILE, using either numeric or ascii representation */
170 static void print_ace(FILE *f
, SEC_ACE
*ace
)
172 struct perm_value
*v
;
177 SidToString(sidstr
, &ace
->trustee
);
179 fprintf(f
, "%s:", sidstr
);
182 fprintf(f
, "%d/%d/0x%08x",
183 ace
->type
, ace
->flags
, ace
->info
.mask
);
189 if (ace
->type
== SEC_ACE_TYPE_ACCESS_ALLOWED
) {
190 fprintf(f
, "ALLOWED");
191 } else if (ace
->type
== SEC_ACE_TYPE_ACCESS_DENIED
) {
192 fprintf(f
, "DENIED");
194 fprintf(f
, "%d", ace
->type
);
197 /* Not sure what flags can be set in a file ACL */
199 fprintf(f
, "/%d/", ace
->flags
);
201 /* Standard permissions */
203 for (v
= standard_values
; v
->perm
; v
++) {
204 if (ace
->info
.mask
== v
->mask
) {
205 fprintf(f
, "%s", v
->perm
);
210 /* Special permissions. Print out a hex value if we have
211 leftover bits in the mask. */
213 got_mask
= ace
->info
.mask
;
216 for (v
= special_values
; v
->perm
; v
++) {
217 if ((ace
->info
.mask
& v
->mask
) == v
->mask
) {
219 fprintf(f
, "%s", v
->perm
);
221 got_mask
&= ~v
->mask
;
227 fprintf(f
, "0x%08x", ace
->info
.mask
);
236 /* parse an ACE in the same format as print_ace() */
237 static BOOL
parse_ace(SEC_ACE
*ace
, char *str
)
242 unsigned atype
, aflags
, amask
;
245 struct perm_value
*v
;
249 if (!p
) return False
;
253 /* Try to parse numeric form */
255 if (sscanf(p
, "%i/%i/%i", &atype
, &aflags
, &amask
) == 3 &&
256 StringToSid(&sid
, str
)) {
260 /* Try to parse text form */
262 if (!StringToSid(&sid
, str
)) {
267 if (!next_token(&cp
, tok
, "/", sizeof(fstring
))) {
271 if (strncmp(tok
, "ALLOWED", strlen("ALLOWED")) == 0) {
272 atype
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
273 } else if (strncmp(tok
, "DENIED", strlen("DENIED")) == 0) {
274 atype
= SEC_ACE_TYPE_ACCESS_DENIED
;
279 /* Only numeric form accepted for flags at present */
281 if (!(next_token(&cp
, tok
, "/", sizeof(fstring
)) &&
282 sscanf(tok
, "%i", &aflags
))) {
286 if (!next_token(&cp
, tok
, "/", sizeof(fstring
))) {
290 if (strncmp(tok
, "0x", 2) == 0) {
291 if (sscanf(tok
, "%i", &amask
) != 1) {
297 for (v
= standard_values
; v
->perm
; v
++) {
298 if (strcmp(tok
, v
->perm
) == 0) {
309 for (v
= special_values
; v
->perm
; v
++) {
310 if (v
->perm
[0] == *p
) {
316 if (!found
) return False
;
326 init_sec_ace(ace
, &sid
, atype
, mask
, aflags
);
330 /* add an ACE to a list of ACEs in a SEC_ACL */
331 static BOOL
add_ace(SEC_ACL
**the_acl
, SEC_ACE
*ace
)
336 (*the_acl
) = make_sec_acl(ctx
, 3, 1, ace
);
340 aces
= calloc(1+(*the_acl
)->num_aces
,sizeof(SEC_ACE
));
341 memcpy(aces
, (*the_acl
)->ace
, (*the_acl
)->num_aces
* sizeof(SEC_ACE
));
342 memcpy(aces
+(*the_acl
)->num_aces
, ace
, sizeof(SEC_ACE
));
343 new = make_sec_acl(ctx
,(*the_acl
)->revision
,1+(*the_acl
)->num_aces
, aces
);
349 /* parse a ascii version of a security descriptor */
350 static SEC_DESC
*sec_desc_parse(char *str
)
356 DOM_SID
*grp_sid
=NULL
, *owner_sid
=NULL
;
360 while (next_token(&p
, tok
, "\t,\r\n", sizeof(tok
))) {
362 if (strncmp(tok
,"REVISION:", 9) == 0) {
363 revision
= strtol(tok
+9, NULL
, 16);
367 if (strncmp(tok
,"OWNER:", 6) == 0) {
368 owner_sid
= (DOM_SID
*)calloc(1, sizeof(DOM_SID
));
370 !StringToSid(owner_sid
, tok
+6)) {
371 printf("Failed to parse owner sid\n");
377 if (strncmp(tok
,"GROUP:", 6) == 0) {
378 grp_sid
= (DOM_SID
*)calloc(1, sizeof(DOM_SID
));
380 !StringToSid(grp_sid
, tok
+6)) {
381 printf("Failed to parse group sid\n");
387 if (strncmp(tok
,"ACL:", 4) == 0) {
389 if (!parse_ace(&ace
, tok
+4)) {
390 printf("Failed to parse ACL %s\n", tok
);
393 if(!add_ace(&dacl
, &ace
)) {
394 printf("Failed to add ACL %s\n", tok
);
400 printf("Failed to parse security descriptor\n");
404 ret
= make_sec_desc(ctx
,revision
, owner_sid
, grp_sid
,
405 NULL
, dacl
, &sd_size
);
408 SAFE_FREE(owner_sid
);
414 /* print a ascii version of a security descriptor on a FILE handle */
415 static void sec_desc_print(FILE *f
, SEC_DESC
*sd
)
420 printf("REVISION:%d\n", sd
->revision
);
422 /* Print owner and group sid */
425 SidToString(sidstr
, sd
->owner_sid
);
430 printf("OWNER:%s\n", sidstr
);
433 SidToString(sidstr
, sd
->grp_sid
);
438 fprintf(f
, "GROUP:%s\n", sidstr
);
441 for (i
= 0; sd
->dacl
&& i
< sd
->dacl
->num_aces
; i
++) {
442 SEC_ACE
*ace
= &sd
->dacl
->ace
[i
];
450 /*****************************************************
451 dump the acls for a file
452 *******************************************************/
453 static int cacl_dump(struct cli_state
*cli
, char *filename
)
458 if (test_args
) return EXIT_OK
;
460 fnum
= cli_nt_create(cli
, filename
, CREATE_ACCESS_READ
);
462 printf("Failed to open %s: %s\n", filename
, cli_errstr(cli
));
466 sd
= cli_query_secdesc(cli
, fnum
, ctx
);
469 printf("ERROR: secdesc query failed: %s\n", cli_errstr(cli
));
473 sec_desc_print(stdout
, sd
);
475 cli_close(cli
, fnum
);
480 /*****************************************************
481 Change the ownership or group ownership of a file. Just
482 because the NT docs say this can't be done :-). JRA.
483 *******************************************************/
485 static int owner_set(struct cli_state
*cli
, enum chown_mode change_mode
,
486 char *filename
, char *new_username
)
493 fnum
= cli_nt_create(cli
, filename
, CREATE_ACCESS_READ
);
496 printf("Failed to open %s: %s\n", filename
, cli_errstr(cli
));
500 if (!StringToSid(&sid
, new_username
))
501 return EXIT_PARSE_ERROR
;
503 old
= cli_query_secdesc(cli
, fnum
, ctx
);
505 cli_close(cli
, fnum
);
508 printf("owner_set: Failed to query old descriptor\n");
512 sd
= make_sec_desc(ctx
,old
->revision
,
513 (change_mode
== REQUEST_CHOWN
) ? &sid
: old
->owner_sid
,
514 (change_mode
== REQUEST_CHGRP
) ? &sid
: old
->grp_sid
,
515 NULL
, old
->dacl
, &sd_size
);
517 fnum
= cli_nt_create(cli
, filename
, CREATE_ACCESS_WRITE
);
520 printf("Failed to open %s: %s\n", filename
, cli_errstr(cli
));
524 if (!cli_set_secdesc(cli
, fnum
, sd
)) {
525 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli
));
528 cli_close(cli
, fnum
);
534 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
535 However NT4 gives a "The information may have been modified by a
536 computer running Windows NT 5.0" if denied ACEs do not appear before
539 static int ace_compare(SEC_ACE
*ace1
, SEC_ACE
*ace2
)
541 if (sec_ace_equal(ace1
, ace2
))
544 if (ace1
->type
!= ace2
->type
)
545 return ace2
->type
- ace1
->type
;
547 if (sid_compare(&ace1
->trustee
, &ace2
->trustee
))
548 return sid_compare(&ace1
->trustee
, &ace2
->trustee
);
550 if (ace1
->flags
!= ace2
->flags
)
551 return ace1
->flags
- ace2
->flags
;
553 if (ace1
->info
.mask
!= ace2
->info
.mask
)
554 return ace1
->info
.mask
- ace2
->info
.mask
;
556 if (ace1
->size
!= ace2
->size
)
557 return ace1
->size
- ace2
->size
;
559 return memcmp(ace1
, ace2
, sizeof(SEC_ACE
));
562 static void sort_acl(SEC_ACL
*the_acl
)
565 if (!the_acl
) return;
567 qsort(the_acl
->ace
, the_acl
->num_aces
, sizeof(the_acl
->ace
[0]), QSORT_CAST ace_compare
);
569 for (i
=1;i
<the_acl
->num_aces
;) {
570 if (sec_ace_equal(&the_acl
->ace
[i
-1], &the_acl
->ace
[i
])) {
572 for (j
=i
; j
<the_acl
->num_aces
-1; j
++) {
573 the_acl
->ace
[j
] = the_acl
->ace
[j
+1];
582 /*****************************************************
583 set the ACLs on a file given an ascii description
584 *******************************************************/
585 static int cacl_set(struct cli_state
*cli
, char *filename
,
586 char *the_acl
, enum acl_mode mode
)
592 int result
= EXIT_OK
;
594 sd
= sec_desc_parse(the_acl
);
596 if (!sd
) return EXIT_PARSE_ERROR
;
597 if (test_args
) return EXIT_OK
;
599 /* The desired access below is the only one I could find that works
600 with NT4, W2KP and Samba */
602 fnum
= cli_nt_create(cli
, filename
, CREATE_ACCESS_READ
);
605 printf("cacl_set failed to open %s: %s\n", filename
, cli_errstr(cli
));
609 old
= cli_query_secdesc(cli
, fnum
, ctx
);
612 printf("calc_set: Failed to query old descriptor\n");
616 cli_close(cli
, fnum
);
618 /* the logic here is rather more complex than I would like */
621 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
624 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
625 if (sec_ace_equal(&sd
->dacl
->ace
[i
],
626 &old
->dacl
->ace
[j
])) {
628 for (k
=j
; k
<old
->dacl
->num_aces
-1;k
++) {
629 old
->dacl
->ace
[k
] = old
->dacl
->ace
[k
+1];
631 old
->dacl
->num_aces
--;
632 if (old
->dacl
->num_aces
== 0) {
633 SAFE_FREE(old
->dacl
->ace
);
634 SAFE_FREE(old
->dacl
);
643 printf("ACL for ACE:");
644 print_ace(stdout
, &sd
->dacl
->ace
[i
]);
645 printf(" not found\n");
651 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
654 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
655 if (sid_equal(&sd
->dacl
->ace
[i
].trustee
,
656 &old
->dacl
->ace
[j
].trustee
)) {
657 old
->dacl
->ace
[j
] = sd
->dacl
->ace
[i
];
665 SidToString(str
, &sd
->dacl
->ace
[i
].trustee
);
666 printf("ACL for SID %s not found\n", str
);
673 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
674 add_ace(&old
->dacl
, &sd
->dacl
->ace
[i
]);
683 /* Denied ACE entries must come before allowed ones */
686 /* Create new security descriptor and set it */
687 sd
= make_sec_desc(ctx
,old
->revision
, old
->owner_sid
, old
->grp_sid
,
688 NULL
, old
->dacl
, &sd_size
);
690 fnum
= cli_nt_create(cli
, filename
, CREATE_ACCESS_WRITE
);
693 printf("cacl_set failed to open %s: %s\n", filename
, cli_errstr(cli
));
697 if (!cli_set_secdesc(cli
, fnum
, sd
)) {
698 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli
));
699 result
= EXIT_FAILED
;
704 cli_close(cli
, fnum
);
710 /*****************************************************
711 return a connection to a server
712 *******************************************************/
713 struct cli_state
*connect_one(char *share
)
716 struct nmb_name called
, calling
;
718 extern pstring global_myname
;
720 fstrcpy(server
,share
+2);
721 share
= strchr(server
,'\\');
722 if (!share
) return NULL
;
728 make_nmb_name(&calling
, global_myname
, 0x0);
729 make_nmb_name(&called
, server
, 0x20);
734 /* have to open a new connection */
735 if (!(c
=cli_initialise(NULL
)) || !cli_connect(c
, server
, &ip
)) {
736 DEBUG(0,("Connection to %s failed\n", server
));
741 if (!cli_session_request(c
, &calling
, &called
)) {
742 DEBUG(0,("session request to %s failed\n", called
.name
));
744 if (strcmp(called
.name
, "*SMBSERVER")) {
745 make_nmb_name(&called
, "*SMBSERVER", 0x20);
751 DEBUG(4,(" session request ok\n"));
753 if (!cli_negprot(c
)) {
754 DEBUG(0,("protocol negotiation failed\n"));
760 char *pass
= getpass("Password: ");
762 pstrcpy(password
, pass
);
766 if (!cli_session_setup(c
, username
,
767 password
, strlen(password
),
768 password
, strlen(password
),
769 (workgroup
[0] ? workgroup
: lp_workgroup()))) {
770 DEBUG(0,("session setup failed: %s\n", cli_errstr(c
)));
775 DEBUG(4,(" session setup ok\n"));
777 if (!cli_send_tconX(c
, share
, "?????",
778 password
, strlen(password
)+1)) {
779 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c
)));
784 DEBUG(4,(" tconx ok\n"));
790 static void usage(void)
792 printf("Usage: smbcacls //server1/share1 filename [options]\n");
793 printf("Version: %s\n", VERSION
);
796 \t-D <acls> delete an acl\n\
797 \t-M <acls> modify an acl\n\
798 \t-A <acls> add an acl\n\
799 \t-S <acls> set acls\n\
800 \t-C username change ownership of a file\n\
801 \t-G username change group ownership of a file\n\
802 \t-n don't resolve sids or masks to names\n\
804 \t-d debuglevel set debug output level\n\
805 \t-U username user to autheticate as\n\
806 \t-W workgroup or domain workgroup or domain user is in\n\
808 The username can be of the form username%%password or\n\
809 workgroup\\username%%password.\n\n\
810 An acl is of the form ACL:<SID>:type/flags/mask\n\
811 You can string acls together with spaces, commas or newlines\n\
815 /****************************************************************************
817 ****************************************************************************/
818 int main(int argc
,char *argv
[])
827 static pstring servicesf
= CONFIGFILE
;
828 struct cli_state
*cli
=NULL
;
829 enum acl_mode mode
= SMB_ACL_SET
;
830 char *the_acl
= NULL
;
831 enum chown_mode change_mode
= REQUEST_NONE
;
840 if (argc
< 3 || argv
[1][0] == '-') {
843 exit(EXIT_PARSE_ERROR
);
846 setup_logging(argv
[0],True
);
849 pstrcpy(filename
, argv
[2]);
850 all_string_sub(share
,"/","\\",0);
856 charset_initialise();
858 lp_load(servicesf
,True
,False
,False
);
859 codepage_initialise(lp_client_code_page());
862 if (getenv("USER")) {
863 pstrcpy(username
,getenv("USER"));
865 if ((p
=strchr(username
,'%'))) {
867 pstrcpy(password
,p
+1);
869 memset(strchr(getenv("USER"), '%') + 1, 'X',
874 while ((opt
= getopt(argc
, argv
, "U:nhdS:D:A:M:C:G:t")) != EOF
) {
877 pstrcpy(username
,optarg
);
878 p
= strchr(username
,'%');
881 pstrcpy(password
, p
+1);
887 pstrcpy(workgroup
, optarg
);
897 mode
= SMB_ACL_DELETE
;
902 mode
= SMB_ACL_MODIFY
;
911 pstrcpy(owner_username
,optarg
);
912 change_mode
= REQUEST_CHOWN
;
916 pstrcpy(owner_username
,optarg
);
917 change_mode
= REQUEST_CHGRP
;
931 exit(EXIT_PARSE_ERROR
);
934 DEBUGLEVEL
= atoi(optarg
);
938 printf("Unknown option %c (%d)\n", (char)opt
, opt
);
940 exit(EXIT_PARSE_ERROR
);
950 exit(EXIT_PARSE_ERROR
);
953 /* Make connection to server */
956 cli
= connect_one(share
);
963 all_string_sub(filename
, "/", "\\", 0);
964 if (filename
[0] != '\\') {
967 safe_strcpy(&s
[1], filename
, sizeof(pstring
)-1);
968 pstrcpy(filename
, s
);
971 /* Perform requested action */
973 if (change_mode
!= REQUEST_NONE
) {
974 result
= owner_set(cli
, change_mode
, filename
, owner_username
);
975 } else if (the_acl
) {
976 result
= cacl_set(cli
, filename
, the_acl
, mode
);
978 result
= cacl_dump(cli
, filename
);