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 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 "popt_common.h"
26 #include "rpc_client/cli_pipe.h"
27 #include "../librpc/gen_ndr/ndr_lsa.h"
28 #include "rpc_client/cli_lsarpc.h"
29 #include "../libcli/security/security.h"
30 #include "libsmb/libsmb.h"
31 #include "libsmb/clirap.h"
32 #include "passdb/machine_sid.h"
33 #include "../librpc/gen_ndr/ndr_lsa_c.h"
37 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
39 /* numeric is set when the user wants numeric SIDs and ACEs rather
40 than going via LSA calls to resolve them */
44 static int query_sec_info = -1;
45 static int set_sec_info = -1;
47 static const char *domain_sid = NULL;
49 enum acl_mode {SMB_ACL_SET, SMB_ACL_DELETE, SMB_ACL_MODIFY, SMB_ACL_ADD };
50 enum chown_mode {REQUEST_NONE, REQUEST_CHOWN, REQUEST_CHGRP, REQUEST_INHERIT};
51 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
58 /* These values discovered by inspection */
60 static const struct perm_value special_values[] = {
70 static const struct perm_value standard_values[] = {
71 { "READ", 0x001200a9 },
72 { "CHANGE", 0x001301bf },
73 { "FULL", 0x001f01ff },
77 /* Open cli connection and policy handle */
79 static NTSTATUS cli_lsa_lookup_sid(struct cli_state *cli,
80 const struct dom_sid *sid,
82 enum lsa_SidType *type,
83 char **domain, char **name)
85 uint16 orig_cnum = cli_state_get_tid(cli);
86 struct rpc_pipe_client *p = NULL;
87 struct policy_handle handle;
89 TALLOC_CTX *frame = talloc_stackframe();
90 enum lsa_SidType *types;
94 status = cli_tree_connect(cli, "IPC$", "?????", "", 0);
95 if (!NT_STATUS_IS_OK(status)) {
99 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
101 if (!NT_STATUS_IS_OK(status)) {
105 status = rpccli_lsa_open_policy(p, talloc_tos(), True,
106 GENERIC_EXECUTE_ACCESS, &handle);
107 if (!NT_STATUS_IS_OK(status)) {
111 status = rpccli_lsa_lookup_sids(p, talloc_tos(), &handle, 1, sid,
112 &domains, &names, &types);
113 if (!NT_STATUS_IS_OK(status)) {
118 *domain = talloc_move(mem_ctx, &domains[0]);
119 *name = talloc_move(mem_ctx, &names[0]);
121 status = NT_STATUS_OK;
126 cli_state_set_tid(cli, orig_cnum);
131 static NTSTATUS cli_lsa_lookup_name(struct cli_state *cli,
133 enum lsa_SidType *type,
136 uint16 orig_cnum = cli_state_get_tid(cli);
137 struct rpc_pipe_client *p;
138 struct policy_handle handle;
140 TALLOC_CTX *frame = talloc_stackframe();
141 struct dom_sid *sids;
142 enum lsa_SidType *types;
144 status = cli_tree_connect(cli, "IPC$", "?????", "", 0);
145 if (!NT_STATUS_IS_OK(status)) {
149 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
151 if (!NT_STATUS_IS_OK(status)) {
155 status = rpccli_lsa_open_policy(p, talloc_tos(), True,
156 GENERIC_EXECUTE_ACCESS, &handle);
157 if (!NT_STATUS_IS_OK(status)) {
161 status = rpccli_lsa_lookup_names(p, talloc_tos(), &handle, 1, &name,
162 NULL, 1, &sids, &types);
163 if (!NT_STATUS_IS_OK(status)) {
170 status = NT_STATUS_OK;
175 cli_state_set_tid(cli, orig_cnum);
181 static NTSTATUS cli_lsa_lookup_domain_sid(struct cli_state *cli,
184 union lsa_PolicyInformation *info = NULL;
185 uint16 orig_cnum = cli_state_get_tid(cli);
186 struct rpc_pipe_client *rpc_pipe = NULL;
187 struct policy_handle handle;
188 NTSTATUS status, result;
189 TALLOC_CTX *frame = talloc_stackframe();
190 const struct ndr_syntax_id *lsarpc_syntax = &ndr_table_lsarpc.syntax_id;
192 status = cli_tree_connect(cli, "IPC$", "?????", "", 0);
193 if (!NT_STATUS_IS_OK(status)) {
197 status = cli_rpc_pipe_open_noauth(cli, lsarpc_syntax, &rpc_pipe);
198 if (!NT_STATUS_IS_OK(status)) {
202 status = rpccli_lsa_open_policy(rpc_pipe, frame, True,
203 GENERIC_EXECUTE_ACCESS, &handle);
204 if (!NT_STATUS_IS_OK(status)) {
208 status = dcerpc_lsa_QueryInfoPolicy2(rpc_pipe->binding_handle,
210 LSA_POLICY_INFO_DOMAIN,
213 if (any_nt_status_not_ok(status, result, &status)) {
217 *sid = *info->domain.sid;
220 TALLOC_FREE(rpc_pipe);
223 cli_state_set_tid(cli, orig_cnum);
228 static struct dom_sid *get_domain_sid(struct cli_state *cli)
232 struct dom_sid *sid = talloc(talloc_tos(), struct dom_sid);
234 DEBUG(0, ("Out of memory\n"));
239 if (!dom_sid_parse(domain_sid, sid)) {
240 DEBUG(0,("failed to parse domain sid\n"));
244 status = cli_lsa_lookup_domain_sid(cli, sid);
246 if (!NT_STATUS_IS_OK(status)) {
247 DEBUG(0,("failed to lookup domain sid: %s\n", nt_errstr(status)));
253 DEBUG(2,("Domain SID: %s\n", sid_string_dbg(sid)));
258 /* convert a SID to a string, either numeric or username/group */
259 static void SidToString(struct cli_state *cli, fstring str, const struct dom_sid *sid)
263 enum lsa_SidType type;
266 sid_to_fstring(str, sid);
272 status = cli_lsa_lookup_sid(cli, sid, talloc_tos(), &type,
275 if (!NT_STATUS_IS_OK(status)) {
280 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
281 domain, lp_winbind_separator(), name);
287 /* convert a string to a SID, either numeric or username/group */
288 static bool StringToSid(struct cli_state *cli, struct dom_sid *sid, const char *str)
290 enum lsa_SidType type;
292 if (string_to_sid(sid, str)) {
296 return NT_STATUS_IS_OK(cli_lsa_lookup_name(cli, str, &type, sid));
299 static void print_ace_flags(FILE *f, uint8_t flags)
301 char *str = talloc_strdup(NULL, "");
307 if (flags & SEC_ACE_FLAG_OBJECT_INHERIT) {
308 str = talloc_asprintf(str, "%s%s",
314 if (flags & SEC_ACE_FLAG_CONTAINER_INHERIT) {
315 str = talloc_asprintf(str, "%s%s",
321 if (flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
322 str = talloc_asprintf(str, "%s%s",
328 if (flags & SEC_ACE_FLAG_INHERIT_ONLY) {
329 str = talloc_asprintf(str, "%s%s",
335 if (flags & SEC_ACE_FLAG_INHERITED_ACE) {
336 str = talloc_asprintf(str, "%s%s",
342 /* Ignore define SEC_ACE_FLAG_SUCCESSFUL_ACCESS ( 0x40 )
343 and SEC_ACE_FLAG_FAILED_ACCESS ( 0x80 ) as they're
346 if (str[strlen(str)-1] == '|') {
347 str[strlen(str)-1] = '\0';
348 fprintf(f, "/%s/", str);
350 fprintf(f, "/0x%x/", flags);
356 fprintf(f, "/0x%x/", flags);
359 /* print an ACE on a FILE, using either numeric or ascii representation */
360 static void print_ace(struct cli_state *cli, FILE *f, struct security_ace *ace)
362 const struct perm_value *v;
367 SidToString(cli, sidstr, &ace->trustee);
369 fprintf(f, "%s:", sidstr);
372 fprintf(f, "%d/0x%x/0x%08x",
373 ace->type, ace->flags, ace->access_mask);
379 if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
380 fprintf(f, "ALLOWED");
381 } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
382 fprintf(f, "DENIED");
384 fprintf(f, "%d", ace->type);
387 print_ace_flags(f, ace->flags);
389 /* Standard permissions */
391 for (v = standard_values; v->perm; v++) {
392 if (ace->access_mask == v->mask) {
393 fprintf(f, "%s", v->perm);
398 /* Special permissions. Print out a hex value if we have
399 leftover bits in the mask. */
401 got_mask = ace->access_mask;
404 for (v = special_values; v->perm; v++) {
405 if ((ace->access_mask & v->mask) == v->mask) {
407 fprintf(f, "%s", v->perm);
409 got_mask &= ~v->mask;
415 fprintf(f, "0x%08x", ace->access_mask);
423 static bool parse_ace_flags(const char *str, unsigned int *pflags)
429 if (strnequal(p, "OI", 2)) {
430 *pflags |= SEC_ACE_FLAG_OBJECT_INHERIT;
432 } else if (strnequal(p, "CI", 2)) {
433 *pflags |= SEC_ACE_FLAG_CONTAINER_INHERIT;
435 } else if (strnequal(p, "NP", 2)) {
436 *pflags |= SEC_ACE_FLAG_NO_PROPAGATE_INHERIT;
438 } else if (strnequal(p, "IO", 2)) {
439 *pflags |= SEC_ACE_FLAG_INHERIT_ONLY;
441 } else if (*p == 'I') {
442 *pflags |= SEC_ACE_FLAG_INHERITED_ACE;
460 /* parse an ACE in the same format as print_ace() */
461 static bool parse_ace(struct cli_state *cli, struct security_ace *ace,
462 const char *orig_str)
467 unsigned int atype = 0;
468 unsigned int aflags = 0;
469 unsigned int amask = 0;
472 const struct perm_value *v;
473 char *str = SMB_STRDUP(orig_str);
474 TALLOC_CTX *frame = talloc_stackframe();
482 p = strchr_m(str,':');
484 printf("ACE '%s': missing ':'.\n", orig_str);
491 /* Try to parse numeric form */
493 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
494 StringToSid(cli, &sid, str)) {
498 /* Try to parse text form */
500 if (!StringToSid(cli, &sid, str)) {
501 printf("ACE '%s': failed to convert '%s' to SID\n",
509 if (!next_token_talloc(frame, &cp, &tok, "/")) {
510 printf("ACE '%s': failed to find '/' character.\n",
517 if (strncmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
518 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
519 } else if (strncmp(tok, "DENIED", strlen("DENIED")) == 0) {
520 atype = SEC_ACE_TYPE_ACCESS_DENIED;
522 printf("ACE '%s': missing 'ALLOWED' or 'DENIED' entry at '%s'\n",
529 /* Only numeric form accepted for flags at present */
531 if (!next_token_talloc(frame, &cp, &tok, "/")) {
532 printf("ACE '%s': bad flags entry at '%s'\n",
539 if (tok[0] < '0' || tok[0] > '9') {
540 if (!parse_ace_flags(tok, &aflags)) {
541 printf("ACE '%s': bad named flags entry at '%s'\n",
547 } else if (strnequal(tok, "0x", 2)) {
548 if (!sscanf(tok, "%x", &aflags)) {
549 printf("ACE '%s': bad hex flags entry at '%s'\n",
556 if (!sscanf(tok, "%i", &aflags)) {
557 printf("ACE '%s': bad integer flags entry at '%s'\n",
565 if (!next_token_talloc(frame, &cp, &tok, "/")) {
566 printf("ACE '%s': missing / at '%s'\n",
573 if (strncmp(tok, "0x", 2) == 0) {
574 if (sscanf(tok, "%i", &amask) != 1) {
575 printf("ACE '%s': bad hex number at '%s'\n",
584 for (v = standard_values; v->perm; v++) {
585 if (strcmp(tok, v->perm) == 0) {
596 for (v = special_values; v->perm; v++) {
597 if (v->perm[0] == *p) {
604 printf("ACE '%s': bad permission value at '%s'\n",
621 init_sec_ace(ace, &sid, atype, mask, aflags);
627 /* add an ACE to a list of ACEs in a struct security_acl */
628 static bool add_ace(struct security_acl **the_acl, struct security_ace *ace)
630 struct security_acl *new_ace;
631 struct security_ace *aces;
633 return (((*the_acl) = make_sec_acl(talloc_tos(), 3, 1, ace))
637 if (!(aces = SMB_CALLOC_ARRAY(struct security_ace, 1+(*the_acl)->num_aces))) {
640 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(struct
642 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(struct security_ace));
643 new_ace = make_sec_acl(talloc_tos(),(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
645 (*the_acl) = new_ace;
649 /* parse a ascii version of a security descriptor */
650 static struct security_descriptor *sec_desc_parse(TALLOC_CTX *ctx, struct cli_state *cli, char *str)
654 struct security_descriptor *ret = NULL;
656 struct dom_sid *grp_sid=NULL, *owner_sid=NULL;
657 struct security_acl *dacl=NULL;
660 while (next_token_talloc(ctx, &p, &tok, "\t,\r\n")) {
661 if (strncmp(tok,"REVISION:", 9) == 0) {
662 revision = strtol(tok+9, NULL, 16);
666 if (strncmp(tok,"OWNER:", 6) == 0) {
668 printf("Only specify owner once\n");
671 owner_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
673 !StringToSid(cli, owner_sid, tok+6)) {
674 printf("Failed to parse owner sid\n");
680 if (strncmp(tok,"GROUP:", 6) == 0) {
682 printf("Only specify group once\n");
685 grp_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
687 !StringToSid(cli, grp_sid, tok+6)) {
688 printf("Failed to parse group sid\n");
694 if (strncmp(tok,"ACL:", 4) == 0) {
695 struct security_ace ace;
696 if (!parse_ace(cli, &ace, tok+4)) {
699 if(!add_ace(&dacl, &ace)) {
700 printf("Failed to add ACL %s\n", tok);
706 printf("Failed to parse token '%s' in security descriptor,\n", tok);
710 ret = make_sec_desc(ctx,revision, SEC_DESC_SELF_RELATIVE, owner_sid, grp_sid,
711 NULL, dacl, &sd_size);
715 SAFE_FREE(owner_sid);
720 static const struct {
724 } sec_desc_ctrl_bits[] = {
725 {SEC_DESC_OWNER_DEFAULTED, "OD", "Owner Defaulted"},
726 {SEC_DESC_GROUP_DEFAULTED, "GD", "Group Defaulted"},
727 {SEC_DESC_DACL_PRESENT, "DP", "DACL Present"},
728 {SEC_DESC_DACL_DEFAULTED, "DD", "DACL Defaulted"},
729 {SEC_DESC_SACL_PRESENT, "SP", "SACL Present"},
730 {SEC_DESC_SACL_DEFAULTED, "SD", "SACL Defaulted"},
731 {SEC_DESC_DACL_TRUSTED, "DT", "DACL Trusted"},
732 {SEC_DESC_SERVER_SECURITY, "SS", "Server Security"},
733 {SEC_DESC_DACL_AUTO_INHERIT_REQ, "DR", "DACL Inheritance Required"},
734 {SEC_DESC_SACL_AUTO_INHERIT_REQ, "SR", "SACL Inheritance Required"},
735 {SEC_DESC_DACL_AUTO_INHERITED, "DI", "DACL Auto Inherited"},
736 {SEC_DESC_SACL_AUTO_INHERITED, "SI", "SACL Auto Inherited"},
737 {SEC_DESC_DACL_PROTECTED, "PD", "DACL Protected"},
738 {SEC_DESC_SACL_PROTECTED, "PS", "SACL Protected"},
739 {SEC_DESC_RM_CONTROL_VALID, "RM", "RM Control Valid"},
740 {SEC_DESC_SELF_RELATIVE , "SR", "Self Relative"},
743 static void print_acl_ctrl(FILE *file, uint16_t ctrl)
746 const char* separator = "";
748 fprintf(file, "CONTROL:");
750 fprintf(file, "0x%x\n", ctrl);
754 for (i = ARRAY_SIZE(sec_desc_ctrl_bits) - 1; i >= 0; i--) {
755 if (ctrl & sec_desc_ctrl_bits[i].mask) {
756 fprintf(file, "%s%s", separator, sec_desc_ctrl_bits[i].str);
763 /* print a ascii version of a security descriptor on a FILE handle */
764 static void sec_desc_print(struct cli_state *cli, FILE *f, struct security_descriptor *sd)
769 fprintf(f, "REVISION:%d\n", sd->revision);
770 print_acl_ctrl(f, sd->type);
772 /* Print owner and group sid */
775 SidToString(cli, sidstr, sd->owner_sid);
780 fprintf(f, "OWNER:%s\n", sidstr);
783 SidToString(cli, sidstr, sd->group_sid);
788 fprintf(f, "GROUP:%s\n", sidstr);
791 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
792 struct security_ace *ace = &sd->dacl->aces[i];
794 print_ace(cli, f, ace);
799 for (i = 0; sd->sacl && i < sd->sacl->num_aces; i++) {
800 struct security_ace *ace = &sd->sacl->aces[i];
802 print_ace(cli, f, ace);
807 /*****************************************************
808 get fileinfo for filename
809 *******************************************************/
810 static uint16 get_fileinfo(struct cli_state *cli, const char *filename)
812 uint16_t fnum = (uint16_t)-1;
816 /* The desired access below is the only one I could find that works
817 with NT4, W2KP and Samba */
819 status = cli_ntcreate(cli, filename, 0, CREATE_ACCESS_READ,
820 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
821 FILE_OPEN, 0x0, 0x0, &fnum);
822 if (!NT_STATUS_IS_OK(status)) {
823 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
827 status = cli_qfileinfo_basic(cli, fnum, &mode, NULL, NULL, NULL,
829 if (!NT_STATUS_IS_OK(status)) {
830 printf("Failed to file info %s: %s\n", filename,
834 cli_close(cli, fnum);
839 /*****************************************************
840 get sec desc for filename
841 *******************************************************/
842 static struct security_descriptor *get_secdesc(struct cli_state *cli, const char *filename)
844 uint16_t fnum = (uint16_t)-1;
845 struct security_descriptor *sd;
848 uint32_t desired_access = 0;
850 if (query_sec_info == -1) {
851 sec_info = SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL;
853 sec_info = query_sec_info;
856 if (sec_info & (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL)) {
857 desired_access |= SEC_STD_READ_CONTROL;
859 if (sec_info & SECINFO_SACL) {
860 desired_access |= SEC_FLAG_SYSTEM_SECURITY;
863 if (desired_access == 0) {
864 desired_access |= SEC_STD_READ_CONTROL;
867 status = cli_ntcreate(cli, filename, 0, desired_access,
868 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
869 FILE_OPEN, 0x0, 0x0, &fnum);
870 if (!NT_STATUS_IS_OK(status)) {
871 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
875 status = cli_query_security_descriptor(cli, fnum, sec_info,
878 cli_close(cli, fnum);
880 if (!NT_STATUS_IS_OK(status)) {
881 printf("Failed to get security descriptor: %s\n",
888 /*****************************************************
889 set sec desc for filename
890 *******************************************************/
891 static bool set_secdesc(struct cli_state *cli, const char *filename,
892 struct security_descriptor *sd)
894 uint16_t fnum = (uint16_t)-1;
897 uint32_t desired_access = 0;
900 if (set_sec_info == -1) {
903 if (sd->dacl || (sd->type & SEC_DESC_DACL_PRESENT)) {
904 sec_info |= SECINFO_DACL;
906 if (sd->sacl || (sd->type & SEC_DESC_SACL_PRESENT)) {
907 sec_info |= SECINFO_SACL;
910 sec_info |= SECINFO_OWNER;
913 sec_info |= SECINFO_GROUP;
916 sec_info = set_sec_info;
919 /* Make the desired_access more specific. */
920 if (sec_info & SECINFO_DACL) {
921 desired_access |= SEC_STD_WRITE_DAC;
923 if (sec_info & SECINFO_SACL) {
924 desired_access |= SEC_FLAG_SYSTEM_SECURITY;
926 if (sec_info & (SECINFO_OWNER | SECINFO_GROUP)) {
927 desired_access |= SEC_STD_WRITE_OWNER;
930 status = cli_ntcreate(cli, filename, 0,
932 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
933 FILE_OPEN, 0x0, 0x0, &fnum);
934 if (!NT_STATUS_IS_OK(status)) {
935 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
939 status = cli_set_security_descriptor(cli, fnum, sec_info, sd);
940 if (!NT_STATUS_IS_OK(status)) {
941 printf("ERROR: security description set failed: %s\n",
946 cli_close(cli, fnum);
950 /*****************************************************
951 dump the acls for a file
952 *******************************************************/
953 static int cacl_dump(struct cli_state *cli, const char *filename)
955 struct security_descriptor *sd;
961 sd = get_secdesc(cli, filename);
967 char *str = sddl_encode(talloc_tos(), sd, get_domain_sid(cli));
974 sec_desc_print(cli, stdout, sd);
980 /*****************************************************
981 Change the ownership or group ownership of a file. Just
982 because the NT docs say this can't be done :-). JRA.
983 *******************************************************/
985 static int owner_set(struct cli_state *cli, enum chown_mode change_mode,
986 const char *filename, const char *new_username)
989 struct security_descriptor *sd, *old;
992 if (!StringToSid(cli, &sid, new_username))
993 return EXIT_PARSE_ERROR;
995 old = get_secdesc(cli, filename);
1001 sd = make_sec_desc(talloc_tos(),old->revision, old->type,
1002 (change_mode == REQUEST_CHOWN) ? &sid : NULL,
1003 (change_mode == REQUEST_CHGRP) ? &sid : NULL,
1004 NULL, NULL, &sd_size);
1006 if (!set_secdesc(cli, filename, sd)) {
1014 /* The MSDN is contradictory over the ordering of ACE entries in an
1015 ACL. However NT4 gives a "The information may have been modified
1016 by a computer running Windows NT 5.0" if denied ACEs do not appear
1017 before allowed ACEs. At
1018 http://technet.microsoft.com/en-us/library/cc781716.aspx the
1019 canonical order is specified as "Explicit Deny, Explicit Allow,
1020 Inherited ACEs unchanged" */
1022 static int ace_compare(struct security_ace *ace1, struct security_ace *ace2)
1024 if (sec_ace_equal(ace1, ace2))
1027 if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
1028 !(ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
1030 if (!(ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
1031 (ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
1033 if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
1034 (ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
1037 if (ace1->type != ace2->type)
1038 return ace2->type - ace1->type;
1040 if (dom_sid_compare(&ace1->trustee, &ace2->trustee))
1041 return dom_sid_compare(&ace1->trustee, &ace2->trustee);
1043 if (ace1->flags != ace2->flags)
1044 return ace1->flags - ace2->flags;
1046 if (ace1->access_mask != ace2->access_mask)
1047 return ace1->access_mask - ace2->access_mask;
1049 if (ace1->size != ace2->size)
1050 return ace1->size - ace2->size;
1052 return memcmp(ace1, ace2, sizeof(struct security_ace));
1055 static void sort_acl(struct security_acl *the_acl)
1058 if (!the_acl) return;
1060 TYPESAFE_QSORT(the_acl->aces, the_acl->num_aces, ace_compare);
1062 for (i=1;i<the_acl->num_aces;) {
1063 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
1065 for (j=i; j<the_acl->num_aces-1; j++) {
1066 the_acl->aces[j] = the_acl->aces[j+1];
1068 the_acl->num_aces--;
1075 /*****************************************************
1076 set the ACLs on a file given an ascii description
1077 *******************************************************/
1079 static int cacl_set(struct cli_state *cli, const char *filename,
1080 char *the_acl, enum acl_mode mode)
1082 struct security_descriptor *sd, *old;
1085 int result = EXIT_OK;
1088 sd = sddl_decode(talloc_tos(), the_acl, get_domain_sid(cli));
1090 sd = sec_desc_parse(talloc_tos(), cli, the_acl);
1093 if (!sd) return EXIT_PARSE_ERROR;
1094 if (test_args) return EXIT_OK;
1096 old = get_secdesc(cli, filename);
1102 /* the logic here is rather more complex than I would like */
1104 case SMB_ACL_DELETE:
1105 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1108 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
1109 if (sec_ace_equal(&sd->dacl->aces[i],
1110 &old->dacl->aces[j])) {
1112 for (k=j; k<old->dacl->num_aces-1;k++) {
1113 old->dacl->aces[k] = old->dacl->aces[k+1];
1115 old->dacl->num_aces--;
1122 printf("ACL for ACE:");
1123 print_ace(cli, stdout, &sd->dacl->aces[i]);
1124 printf(" not found\n");
1129 case SMB_ACL_MODIFY:
1130 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1133 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
1134 if (dom_sid_equal(&sd->dacl->aces[i].trustee,
1135 &old->dacl->aces[j].trustee)) {
1136 old->dacl->aces[j] = sd->dacl->aces[i];
1144 SidToString(cli, str,
1145 &sd->dacl->aces[i].trustee);
1146 printf("ACL for SID %s not found\n", str);
1150 if (sd->owner_sid) {
1151 old->owner_sid = sd->owner_sid;
1154 if (sd->group_sid) {
1155 old->group_sid = sd->group_sid;
1161 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1162 add_ace(&old->dacl, &sd->dacl->aces[i]);
1171 /* Denied ACE entries must come before allowed ones */
1172 sort_acl(old->dacl);
1174 /* Create new security descriptor and set it */
1176 /* We used to just have "WRITE_DAC_ACCESS" without WRITE_OWNER.
1177 But if we're sending an owner, even if it's the same as the one
1178 that already exists then W2K3 insists we open with WRITE_OWNER access.
1179 I need to check that setting a SD with no owner set works against WNT
1183 sd = make_sec_desc(talloc_tos(),old->revision, old->type,
1184 old->owner_sid, old->group_sid,
1185 NULL, old->dacl, &sd_size);
1187 if (!set_secdesc(cli, filename, sd)) {
1188 result = EXIT_FAILED;
1194 /*****************************************************
1195 set the inherit on a file
1196 *******************************************************/
1197 static int inherit(struct cli_state *cli, const char *filename,
1200 struct security_descriptor *old,*sd;
1203 int result = EXIT_OK;
1205 old = get_secdesc(cli, filename);
1211 oldattr = get_fileinfo(cli,filename);
1213 if (strcmp(type,"allow")==0) {
1214 if ((old->type & SEC_DESC_DACL_PROTECTED) ==
1215 SEC_DESC_DACL_PROTECTED) {
1217 char *parentname,*temp;
1218 struct security_descriptor *parent;
1219 temp = talloc_strdup(talloc_tos(), filename);
1221 old->type=old->type & (~SEC_DESC_DACL_PROTECTED);
1223 /* look at parent and copy in all its inheritable ACL's. */
1224 string_replace(temp, '\\', '/');
1225 if (!parent_dirname(talloc_tos(),temp,&parentname,NULL)) {
1228 string_replace(parentname, '/', '\\');
1229 parent = get_secdesc(cli,parentname);
1230 if (parent == NULL) {
1233 for (i=0;i<parent->dacl->num_aces;i++) {
1234 struct security_ace *ace=&parent->dacl->aces[i];
1235 /* Add inherited flag to all aces */
1236 ace->flags=ace->flags|
1237 SEC_ACE_FLAG_INHERITED_ACE;
1238 if ((oldattr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) {
1239 if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ==
1240 SEC_ACE_FLAG_CONTAINER_INHERIT) {
1241 add_ace(&old->dacl, ace);
1244 if ((ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT) ==
1245 SEC_ACE_FLAG_OBJECT_INHERIT) {
1246 /* clear flags for files */
1248 add_ace(&old->dacl, ace);
1253 printf("Already set to inheritable permissions.\n");
1256 } else if (strcmp(type,"remove")==0) {
1257 if ((old->type & SEC_DESC_DACL_PROTECTED) !=
1258 SEC_DESC_DACL_PROTECTED) {
1259 old->type=old->type | SEC_DESC_DACL_PROTECTED;
1261 /* remove all inherited ACL's. */
1264 struct security_acl *temp=old->dacl;
1265 old->dacl=make_sec_acl(talloc_tos(), 3, 0, NULL);
1266 for (i=temp->num_aces-1;i>=0;i--) {
1267 struct security_ace *ace=&temp->aces[i];
1268 /* Remove all ace with INHERITED flag set */
1269 if ((ace->flags & SEC_ACE_FLAG_INHERITED_ACE) !=
1270 SEC_ACE_FLAG_INHERITED_ACE) {
1271 add_ace(&old->dacl,ace);
1276 printf("Already set to no inheritable permissions.\n");
1279 } else if (strcmp(type,"copy")==0) {
1280 if ((old->type & SEC_DESC_DACL_PROTECTED) !=
1281 SEC_DESC_DACL_PROTECTED) {
1282 old->type=old->type | SEC_DESC_DACL_PROTECTED;
1284 /* convert all inherited ACL's to non inherated ACL's. */
1287 for (i=0;i<old->dacl->num_aces;i++) {
1288 struct security_ace *ace=&old->dacl->aces[i];
1289 /* Remove INHERITED FLAG from all aces */
1290 ace->flags=ace->flags&(~SEC_ACE_FLAG_INHERITED_ACE);
1294 printf("Already set to no inheritable permissions.\n");
1299 /* Denied ACE entries must come before allowed ones */
1300 sort_acl(old->dacl);
1302 sd = make_sec_desc(talloc_tos(),old->revision, old->type,
1303 old->owner_sid, old->group_sid,
1304 NULL, old->dacl, &sd_size);
1306 if (!set_secdesc(cli, filename, sd)) {
1307 result = EXIT_FAILED;
1313 /*****************************************************
1314 Return a connection to a server.
1315 *******************************************************/
1316 static struct cli_state *connect_one(struct user_auth_info *auth_info,
1317 const char *server, const char *share)
1319 struct cli_state *c = NULL;
1323 if (get_cmdline_auth_info_use_kerberos(auth_info)) {
1324 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
1325 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1328 if (get_cmdline_auth_info_use_machine_account(auth_info) &&
1329 !set_cmdline_auth_info_machine_account_creds(auth_info)) {
1333 set_cmdline_auth_info_getpass(auth_info);
1335 nt_status = cli_full_connection(&c, lp_netbios_name(), server,
1338 get_cmdline_auth_info_username(auth_info),
1340 get_cmdline_auth_info_password(auth_info),
1342 get_cmdline_auth_info_signing_state(auth_info));
1343 if (!NT_STATUS_IS_OK(nt_status)) {
1344 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
1348 if (get_cmdline_auth_info_smb_encrypt(auth_info)) {
1349 nt_status = cli_cm_force_encryption(c,
1350 get_cmdline_auth_info_username(auth_info),
1351 get_cmdline_auth_info_password(auth_info),
1354 if (!NT_STATUS_IS_OK(nt_status)) {
1363 /****************************************************************************
1365 ****************************************************************************/
1366 int main(int argc, const char *argv[])
1370 enum acl_mode mode = SMB_ACL_SET;
1371 static char *the_acl = NULL;
1372 enum chown_mode change_mode = REQUEST_NONE;
1375 char *filename = NULL;
1377 struct poptOption long_options[] = {
1379 { "delete", 'D', POPT_ARG_STRING, NULL, 'D', "Delete an acl", "ACL" },
1380 { "modify", 'M', POPT_ARG_STRING, NULL, 'M', "Modify an acl", "ACL" },
1381 { "add", 'a', POPT_ARG_STRING, NULL, 'a', "Add an acl", "ACL" },
1382 { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls", "ACLS" },
1383 { "chown", 'C', POPT_ARG_STRING, NULL, 'C', "Change ownership of a file", "USERNAME" },
1384 { "chgrp", 'G', POPT_ARG_STRING, NULL, 'G', "Change group ownership of a file", "GROUPNAME" },
1385 { "inherit", 'I', POPT_ARG_STRING, NULL, 'I', "Inherit allow|remove|copy" },
1386 { "numeric", 0, POPT_ARG_NONE, &numeric, 1, "Don't resolve sids or masks to names" },
1387 { "sddl", 0, POPT_ARG_NONE, &sddl, 1, "Output and input acls in sddl format" },
1388 { "query-security-info", 0, POPT_ARG_INT, &query_sec_info, 1,
1389 "The security-info flags for queries"
1391 { "set-security-info", 0, POPT_ARG_INT, &set_sec_info, 1,
1392 "The security-info flags for modifications"
1394 { "test-args", 't', POPT_ARG_NONE, &test_args, 1, "Test arguments"},
1395 { "domain-sid", 0, POPT_ARG_STRING, &domain_sid, 0, "Domain SID for sddl", "SID"},
1397 POPT_COMMON_CONNECTION
1398 POPT_COMMON_CREDENTIALS
1402 struct cli_state *cli;
1403 TALLOC_CTX *frame = talloc_stackframe();
1404 const char *owner_username = "";
1406 struct user_auth_info *auth_info;
1410 /* set default debug level to 1 regardless of what smb.conf sets */
1411 setup_logging( "smbcacls", DEBUG_STDERR);
1412 lp_set_cmdline("log level", "1");
1416 lp_load_global(get_dyn_CONFIGFILE());
1419 auth_info = user_auth_info_init(frame);
1420 if (auth_info == NULL) {
1423 popt_common_set_auth_info(auth_info);
1425 pc = poptGetContext("smbcacls", argc, argv, long_options, 0);
1427 poptSetOtherOptionHelp(pc, "//server1/share1 filename\nACLs look like: "
1428 "'ACL:user:[ALLOWED|DENIED]/flags/permissions'");
1430 while ((opt = poptGetNextOpt(pc)) != -1) {
1433 the_acl = smb_xstrdup(poptGetOptArg(pc));
1438 the_acl = smb_xstrdup(poptGetOptArg(pc));
1439 mode = SMB_ACL_DELETE;
1443 the_acl = smb_xstrdup(poptGetOptArg(pc));
1444 mode = SMB_ACL_MODIFY;
1448 the_acl = smb_xstrdup(poptGetOptArg(pc));
1453 owner_username = poptGetOptArg(pc);
1454 change_mode = REQUEST_CHOWN;
1458 owner_username = poptGetOptArg(pc);
1459 change_mode = REQUEST_CHGRP;
1463 owner_username = poptGetOptArg(pc);
1464 change_mode = REQUEST_INHERIT;
1469 /* Make connection to server */
1470 if(!poptPeekArg(pc)) {
1471 poptPrintUsage(pc, stderr, 0);
1475 path = talloc_strdup(frame, poptGetArg(pc));
1480 if(!poptPeekArg(pc)) {
1481 poptPrintUsage(pc, stderr, 0);
1485 filename = talloc_strdup(frame, poptGetArg(pc));
1490 poptFreeContext(pc);
1491 popt_burn_cmdline_password(argc, argv);
1493 string_replace(path,'/','\\');
1495 server = talloc_strdup(frame, path+2);
1499 share = strchr_m(server,'\\');
1501 printf("Invalid argument: %s\n", share);
1509 cli = connect_one(auth_info, server, share);
1517 string_replace(filename, '/', '\\');
1518 if (filename[0] != '\\') {
1519 filename = talloc_asprintf(frame,
1527 /* Perform requested action */
1529 if (change_mode == REQUEST_INHERIT) {
1530 result = inherit(cli, filename, owner_username);
1531 } else if (change_mode != REQUEST_NONE) {
1532 result = owner_set(cli, change_mode, filename, owner_username);
1533 } else if (the_acl) {
1534 result = cacl_set(cli, filename, the_acl, mode);
1536 result = cacl_dump(cli, filename);