dns: Use new DNS debugclass in DNS server
[kai/samba.git] / source3 / utils / smbcacls.c
1 /*
2    Unix SMB/CIFS implementation.
3    ACL get/set utility
4
5    Copyright (C) Andrew Tridgell 2000
6    Copyright (C) Tim Potter      2000
7    Copyright (C) Jeremy Allison  2000
8    Copyright (C) Jelmer Vernooij 2003
9
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.
14
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.
19
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/>.
22 */
23
24 #include "includes.h"
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"
34
35 static int test_args;
36
37 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
38
39 /* numeric is set when the user wants numeric SIDs and ACEs rather
40    than going via LSA calls to resolve them */
41 static int numeric;
42
43 static int sddl;
44 static int query_sec_info = -1;
45 static int set_sec_info = -1;
46
47 static const char *domain_sid = NULL;
48
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};
52
53 struct perm_value {
54         const char *perm;
55         uint32 mask;
56 };
57
58 /* These values discovered by inspection */
59
60 static const struct perm_value special_values[] = {
61         { "R", 0x00120089 },
62         { "W", 0x00120116 },
63         { "X", 0x001200a0 },
64         { "D", 0x00010000 },
65         { "P", 0x00040000 },
66         { "O", 0x00080000 },
67         { NULL, 0 },
68 };
69
70 static const struct perm_value standard_values[] = {
71         { "READ",   0x001200a9 },
72         { "CHANGE", 0x001301bf },
73         { "FULL",   0x001f01ff },
74         { NULL, 0 },
75 };
76
77 /* Open cli connection and policy handle */
78
79 static NTSTATUS cli_lsa_lookup_sid(struct cli_state *cli,
80                                    const struct dom_sid *sid,
81                                    TALLOC_CTX *mem_ctx,
82                                    enum lsa_SidType *type,
83                                    char **domain, char **name)
84 {
85         uint16 orig_cnum = cli_state_get_tid(cli);
86         struct rpc_pipe_client *p = NULL;
87         struct policy_handle handle;
88         NTSTATUS status;
89         TALLOC_CTX *frame = talloc_stackframe();
90         enum lsa_SidType *types;
91         char **domains;
92         char **names;
93
94         status = cli_tree_connect(cli, "IPC$", "?????", "", 0);
95         if (!NT_STATUS_IS_OK(status)) {
96                 goto tcon_fail;
97         }
98
99         status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
100                                           &p);
101         if (!NT_STATUS_IS_OK(status)) {
102                 goto fail;
103         }
104
105         status = rpccli_lsa_open_policy(p, talloc_tos(), True,
106                                         GENERIC_EXECUTE_ACCESS, &handle);
107         if (!NT_STATUS_IS_OK(status)) {
108                 goto fail;
109         }
110
111         status = rpccli_lsa_lookup_sids(p, talloc_tos(), &handle, 1, sid,
112                                         &domains, &names, &types);
113         if (!NT_STATUS_IS_OK(status)) {
114                 goto fail;
115         }
116
117         *type = types[0];
118         *domain = talloc_move(mem_ctx, &domains[0]);
119         *name = talloc_move(mem_ctx, &names[0]);
120
121         status = NT_STATUS_OK;
122  fail:
123         TALLOC_FREE(p);
124         cli_tdis(cli);
125  tcon_fail:
126         cli_state_set_tid(cli, orig_cnum);
127         TALLOC_FREE(frame);
128         return status;
129 }
130
131 static NTSTATUS cli_lsa_lookup_name(struct cli_state *cli,
132                                     const char *name,
133                                     enum lsa_SidType *type,
134                                     struct dom_sid *sid)
135 {
136         uint16 orig_cnum = cli_state_get_tid(cli);
137         struct rpc_pipe_client *p;
138         struct policy_handle handle;
139         NTSTATUS status;
140         TALLOC_CTX *frame = talloc_stackframe();
141         struct dom_sid *sids;
142         enum lsa_SidType *types;
143
144         status = cli_tree_connect(cli, "IPC$", "?????", "", 0);
145         if (!NT_STATUS_IS_OK(status)) {
146                 goto tcon_fail;
147         }
148
149         status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
150                                           &p);
151         if (!NT_STATUS_IS_OK(status)) {
152                 goto fail;
153         }
154
155         status = rpccli_lsa_open_policy(p, talloc_tos(), True,
156                                         GENERIC_EXECUTE_ACCESS, &handle);
157         if (!NT_STATUS_IS_OK(status)) {
158                 goto fail;
159         }
160
161         status = rpccli_lsa_lookup_names(p, talloc_tos(), &handle, 1, &name,
162                                          NULL, 1, &sids, &types);
163         if (!NT_STATUS_IS_OK(status)) {
164                 goto fail;
165         }
166
167         *type = types[0];
168         *sid = sids[0];
169
170         status = NT_STATUS_OK;
171  fail:
172         TALLOC_FREE(p);
173         cli_tdis(cli);
174  tcon_fail:
175         cli_state_set_tid(cli, orig_cnum);
176         TALLOC_FREE(frame);
177         return status;
178 }
179
180
181 static NTSTATUS cli_lsa_lookup_domain_sid(struct cli_state *cli,
182                                           struct dom_sid *sid)
183 {
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;
191
192         status = cli_tree_connect(cli, "IPC$", "?????", "", 0);
193         if (!NT_STATUS_IS_OK(status)) {
194                 goto done;
195         }
196
197         status = cli_rpc_pipe_open_noauth(cli, lsarpc_syntax, &rpc_pipe);
198         if (!NT_STATUS_IS_OK(status)) {
199                 goto tdis;
200         }
201
202         status = rpccli_lsa_open_policy(rpc_pipe, frame, True,
203                                         GENERIC_EXECUTE_ACCESS, &handle);
204         if (!NT_STATUS_IS_OK(status)) {
205                 goto tdis;
206         }
207
208         status = dcerpc_lsa_QueryInfoPolicy2(rpc_pipe->binding_handle,
209                                              frame, &handle,
210                                              LSA_POLICY_INFO_DOMAIN,
211                                              &info, &result);
212
213         if (any_nt_status_not_ok(status, result, &status)) {
214                 goto tdis;
215         }
216
217         *sid = *info->domain.sid;
218
219 tdis:
220         TALLOC_FREE(rpc_pipe);
221         cli_tdis(cli);
222 done:
223         cli_state_set_tid(cli, orig_cnum);
224         TALLOC_FREE(frame);
225         return status;
226 }
227
228 static struct dom_sid *get_domain_sid(struct cli_state *cli)
229 {
230         NTSTATUS status;
231
232         struct dom_sid *sid = talloc(talloc_tos(), struct dom_sid);
233         if (sid == NULL) {
234                 DEBUG(0, ("Out of memory\n"));
235                 return NULL;
236         }
237
238         if (domain_sid) {
239                 if (!dom_sid_parse(domain_sid, sid)) {
240                         DEBUG(0,("failed to parse domain sid\n"));
241                         TALLOC_FREE(sid);
242                 }
243         } else {
244                 status = cli_lsa_lookup_domain_sid(cli, sid);
245
246                 if (!NT_STATUS_IS_OK(status)) {
247                         DEBUG(0,("failed to lookup domain sid: %s\n", nt_errstr(status)));
248                         TALLOC_FREE(sid);
249                 }
250
251         }
252
253         DEBUG(2,("Domain SID: %s\n", sid_string_dbg(sid)));
254         return sid;
255 }
256
257
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)
260 {
261         char *domain = NULL;
262         char *name = NULL;
263         enum lsa_SidType type;
264         NTSTATUS status;
265
266         sid_to_fstring(str, sid);
267
268         if (numeric) {
269                 return;
270         }
271
272         status = cli_lsa_lookup_sid(cli, sid, talloc_tos(), &type,
273                                     &domain, &name);
274
275         if (!NT_STATUS_IS_OK(status)) {
276                 return;
277         }
278
279         if (*domain) {
280                 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
281                         domain, lp_winbind_separator(), name);
282         } else {
283                 fstrcpy(str, name);
284         }
285 }
286
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)
289 {
290         enum lsa_SidType type;
291
292         if (string_to_sid(sid, str)) {
293                 return true;
294         }
295
296         return NT_STATUS_IS_OK(cli_lsa_lookup_name(cli, str, &type, sid));
297 }
298
299 static void print_ace_flags(FILE *f, uint8_t flags)
300 {
301         char *str = talloc_strdup(NULL, "");
302
303         if (!str) {
304                 goto out;
305         }
306
307         if (flags & SEC_ACE_FLAG_OBJECT_INHERIT) {
308                 str = talloc_asprintf(str, "%s%s",
309                                 str, "OI|");
310                 if (!str) {
311                         goto out;
312                 }
313         }
314         if (flags & SEC_ACE_FLAG_CONTAINER_INHERIT) {
315                 str = talloc_asprintf(str, "%s%s",
316                                 str, "CI|");
317                 if (!str) {
318                         goto out;
319                 }
320         }
321         if (flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
322                 str = talloc_asprintf(str, "%s%s",
323                                 str, "NP|");
324                 if (!str) {
325                         goto out;
326                 }
327         }
328         if (flags & SEC_ACE_FLAG_INHERIT_ONLY) {
329                 str = talloc_asprintf(str, "%s%s",
330                                 str, "IO|");
331                 if (!str) {
332                         goto out;
333                 }
334         }
335         if (flags & SEC_ACE_FLAG_INHERITED_ACE) {
336                 str = talloc_asprintf(str, "%s%s",
337                                 str, "I|");
338                 if (!str) {
339                         goto out;
340                 }
341         }
342         /* Ignore define SEC_ACE_FLAG_SUCCESSFUL_ACCESS ( 0x40 )
343            and SEC_ACE_FLAG_FAILED_ACCESS ( 0x80 ) as they're
344            audit ace flags. */
345
346         if (str[strlen(str)-1] == '|') {
347                 str[strlen(str)-1] = '\0';
348                 fprintf(f, "/%s/", str);
349         } else {
350                 fprintf(f, "/0x%x/", flags);
351         }
352         TALLOC_FREE(str);
353         return;
354
355   out:
356         fprintf(f, "/0x%x/", flags);
357 }
358
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)
361 {
362         const struct perm_value *v;
363         fstring sidstr;
364         int do_print = 0;
365         uint32 got_mask;
366
367         SidToString(cli, sidstr, &ace->trustee);
368
369         fprintf(f, "%s:", sidstr);
370
371         if (numeric) {
372                 fprintf(f, "%d/0x%x/0x%08x",
373                         ace->type, ace->flags, ace->access_mask);
374                 return;
375         }
376
377         /* Ace type */
378
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");
383         } else {
384                 fprintf(f, "%d", ace->type);
385         }
386
387         print_ace_flags(f, ace->flags);
388
389         /* Standard permissions */
390
391         for (v = standard_values; v->perm; v++) {
392                 if (ace->access_mask == v->mask) {
393                         fprintf(f, "%s", v->perm);
394                         return;
395                 }
396         }
397
398         /* Special permissions.  Print out a hex value if we have
399            leftover bits in the mask. */
400
401         got_mask = ace->access_mask;
402
403  again:
404         for (v = special_values; v->perm; v++) {
405                 if ((ace->access_mask & v->mask) == v->mask) {
406                         if (do_print) {
407                                 fprintf(f, "%s", v->perm);
408                         }
409                         got_mask &= ~v->mask;
410                 }
411         }
412
413         if (!do_print) {
414                 if (got_mask != 0) {
415                         fprintf(f, "0x%08x", ace->access_mask);
416                 } else {
417                         do_print = 1;
418                         goto again;
419                 }
420         }
421 }
422
423 static bool parse_ace_flags(const char *str, unsigned int *pflags)
424 {
425         const char *p = str;
426         *pflags = 0;
427
428         while (*p) {
429                 if (strnequal(p, "OI", 2)) {
430                         *pflags |= SEC_ACE_FLAG_OBJECT_INHERIT;
431                         p += 2;
432                 } else if (strnequal(p, "CI", 2)) {
433                         *pflags |= SEC_ACE_FLAG_CONTAINER_INHERIT;
434                         p += 2;
435                 } else if (strnequal(p, "NP", 2)) {
436                         *pflags |= SEC_ACE_FLAG_NO_PROPAGATE_INHERIT;
437                         p += 2;
438                 } else if (strnequal(p, "IO", 2)) {
439                         *pflags |= SEC_ACE_FLAG_INHERIT_ONLY;
440                         p += 2;
441                 } else if (*p == 'I') {
442                         *pflags |= SEC_ACE_FLAG_INHERITED_ACE;
443                         p += 1;
444                 } else if (*p) {
445                         return false;
446                 }
447
448                 switch (*p) {
449                 case '|':
450                         p++;
451                 case '\0':
452                         continue;
453                 default:
454                         return false;
455                 }
456         }
457         return true;
458 }
459
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)
463 {
464         char *p;
465         const char *cp;
466         char *tok;
467         unsigned int atype = 0;
468         unsigned int aflags = 0;
469         unsigned int amask = 0;
470         struct dom_sid sid;
471         uint32_t mask;
472         const struct perm_value *v;
473         char *str = SMB_STRDUP(orig_str);
474         TALLOC_CTX *frame = talloc_stackframe();
475
476         if (!str) {
477                 TALLOC_FREE(frame);
478                 return False;
479         }
480
481         ZERO_STRUCTP(ace);
482         p = strchr_m(str,':');
483         if (!p) {
484                 printf("ACE '%s': missing ':'.\n", orig_str);
485                 SAFE_FREE(str);
486                 TALLOC_FREE(frame);
487                 return False;
488         }
489         *p = '\0';
490         p++;
491         /* Try to parse numeric form */
492
493         if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
494             StringToSid(cli, &sid, str)) {
495                 goto done;
496         }
497
498         /* Try to parse text form */
499
500         if (!StringToSid(cli, &sid, str)) {
501                 printf("ACE '%s': failed to convert '%s' to SID\n",
502                         orig_str, str);
503                 SAFE_FREE(str);
504                 TALLOC_FREE(frame);
505                 return False;
506         }
507
508         cp = p;
509         if (!next_token_talloc(frame, &cp, &tok, "/")) {
510                 printf("ACE '%s': failed to find '/' character.\n",
511                         orig_str);
512                 SAFE_FREE(str);
513                 TALLOC_FREE(frame);
514                 return False;
515         }
516
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;
521         } else {
522                 printf("ACE '%s': missing 'ALLOWED' or 'DENIED' entry at '%s'\n",
523                         orig_str, tok);
524                 SAFE_FREE(str);
525                 TALLOC_FREE(frame);
526                 return False;
527         }
528
529         /* Only numeric form accepted for flags at present */
530
531         if (!next_token_talloc(frame, &cp, &tok, "/")) {
532                 printf("ACE '%s': bad flags entry at '%s'\n",
533                         orig_str, tok);
534                 SAFE_FREE(str);
535                 TALLOC_FREE(frame);
536                 return False;
537         }
538
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",
542                                 orig_str, tok);
543                         SAFE_FREE(str);
544                         TALLOC_FREE(frame);
545                         return False;
546                 }
547         } else if (strnequal(tok, "0x", 2)) {
548                 if (!sscanf(tok, "%x", &aflags)) {
549                         printf("ACE '%s': bad hex flags entry at '%s'\n",
550                                 orig_str, tok);
551                         SAFE_FREE(str);
552                         TALLOC_FREE(frame);
553                         return False;
554                 }
555         } else {
556                 if (!sscanf(tok, "%i", &aflags)) {
557                         printf("ACE '%s': bad integer flags entry at '%s'\n",
558                                 orig_str, tok);
559                         SAFE_FREE(str);
560                         TALLOC_FREE(frame);
561                         return False;
562                 }
563         }
564
565         if (!next_token_talloc(frame, &cp, &tok, "/")) {
566                 printf("ACE '%s': missing / at '%s'\n",
567                         orig_str, tok);
568                 SAFE_FREE(str);
569                 TALLOC_FREE(frame);
570                 return False;
571         }
572
573         if (strncmp(tok, "0x", 2) == 0) {
574                 if (sscanf(tok, "%i", &amask) != 1) {
575                         printf("ACE '%s': bad hex number at '%s'\n",
576                                 orig_str, tok);
577                         SAFE_FREE(str);
578                         TALLOC_FREE(frame);
579                         return False;
580                 }
581                 goto done;
582         }
583
584         for (v = standard_values; v->perm; v++) {
585                 if (strcmp(tok, v->perm) == 0) {
586                         amask = v->mask;
587                         goto done;
588                 }
589         }
590
591         p = tok;
592
593         while(*p) {
594                 bool found = False;
595
596                 for (v = special_values; v->perm; v++) {
597                         if (v->perm[0] == *p) {
598                                 amask |= v->mask;
599                                 found = True;
600                         }
601                 }
602
603                 if (!found) {
604                         printf("ACE '%s': bad permission value at '%s'\n",
605                                 orig_str, p);
606                         SAFE_FREE(str);
607                         TALLOC_FREE(frame);
608                         return False;
609                 }
610                 p++;
611         }
612
613         if (*p) {
614                 TALLOC_FREE(frame);
615                 SAFE_FREE(str);
616                 return False;
617         }
618
619  done:
620         mask = amask;
621         init_sec_ace(ace, &sid, atype, mask, aflags);
622         TALLOC_FREE(frame);
623         SAFE_FREE(str);
624         return True;
625 }
626
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)
629 {
630         struct security_acl *new_ace;
631         struct security_ace *aces;
632         if (! *the_acl) {
633                 return (((*the_acl) = make_sec_acl(talloc_tos(), 3, 1, ace))
634                         != NULL);
635         }
636
637         if (!(aces = SMB_CALLOC_ARRAY(struct security_ace, 1+(*the_acl)->num_aces))) {
638                 return False;
639         }
640         memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(struct
641         security_ace));
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);
644         SAFE_FREE(aces);
645         (*the_acl) = new_ace;
646         return True;
647 }
648
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)
651 {
652         const char *p = str;
653         char *tok;
654         struct security_descriptor *ret = NULL;
655         size_t sd_size;
656         struct dom_sid *grp_sid=NULL, *owner_sid=NULL;
657         struct security_acl *dacl=NULL;
658         int revision=1;
659
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);
663                         continue;
664                 }
665
666                 if (strncmp(tok,"OWNER:", 6) == 0) {
667                         if (owner_sid) {
668                                 printf("Only specify owner once\n");
669                                 goto done;
670                         }
671                         owner_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
672                         if (!owner_sid ||
673                             !StringToSid(cli, owner_sid, tok+6)) {
674                                 printf("Failed to parse owner sid\n");
675                                 goto done;
676                         }
677                         continue;
678                 }
679
680                 if (strncmp(tok,"GROUP:", 6) == 0) {
681                         if (grp_sid) {
682                                 printf("Only specify group once\n");
683                                 goto done;
684                         }
685                         grp_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
686                         if (!grp_sid ||
687                             !StringToSid(cli, grp_sid, tok+6)) {
688                                 printf("Failed to parse group sid\n");
689                                 goto done;
690                         }
691                         continue;
692                 }
693
694                 if (strncmp(tok,"ACL:", 4) == 0) {
695                         struct security_ace ace;
696                         if (!parse_ace(cli, &ace, tok+4)) {
697                                 goto done;
698                         }
699                         if(!add_ace(&dacl, &ace)) {
700                                 printf("Failed to add ACL %s\n", tok);
701                                 goto done;
702                         }
703                         continue;
704                 }
705
706                 printf("Failed to parse token '%s' in security descriptor,\n", tok);
707                 goto done;
708         }
709
710         ret = make_sec_desc(ctx,revision, SEC_DESC_SELF_RELATIVE, owner_sid, grp_sid,
711                             NULL, dacl, &sd_size);
712
713   done:
714         SAFE_FREE(grp_sid);
715         SAFE_FREE(owner_sid);
716
717         return ret;
718 }
719
720 static const struct {
721         uint16_t mask;
722         const char *str;
723         const char *desc;
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"},
741 };
742
743 static void print_acl_ctrl(FILE *file, uint16_t ctrl)
744 {
745         int i;
746         const char* separator = "";
747
748         fprintf(file, "CONTROL:");
749         if (numeric) {
750                 fprintf(file, "0x%x\n", ctrl);
751                 return;
752         }
753
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);
757                         separator = "|";
758                 }
759         }
760         fputc('\n', file);
761 }
762
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)
765 {
766         fstring sidstr;
767         uint32 i;
768
769         fprintf(f, "REVISION:%d\n", sd->revision);
770         print_acl_ctrl(f, sd->type);
771
772         /* Print owner and group sid */
773
774         if (sd->owner_sid) {
775                 SidToString(cli, sidstr, sd->owner_sid);
776         } else {
777                 fstrcpy(sidstr, "");
778         }
779
780         fprintf(f, "OWNER:%s\n", sidstr);
781
782         if (sd->group_sid) {
783                 SidToString(cli, sidstr, sd->group_sid);
784         } else {
785                 fstrcpy(sidstr, "");
786         }
787
788         fprintf(f, "GROUP:%s\n", sidstr);
789
790         /* Print aces */
791         for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
792                 struct security_ace *ace = &sd->dacl->aces[i];
793                 fprintf(f, "ACL:");
794                 print_ace(cli, f, ace);
795                 fprintf(f, "\n");
796         }
797
798 }
799
800 /*****************************************************
801 get fileinfo for filename
802 *******************************************************/
803 static uint16 get_fileinfo(struct cli_state *cli, const char *filename)
804 {
805         uint16_t fnum = (uint16_t)-1;
806         uint16 mode = 0;
807         NTSTATUS status;
808
809         /* The desired access below is the only one I could find that works
810            with NT4, W2KP and Samba */
811
812         status = cli_ntcreate(cli, filename, 0, CREATE_ACCESS_READ,
813                               0, FILE_SHARE_READ|FILE_SHARE_WRITE,
814                               FILE_OPEN, 0x0, 0x0, &fnum);
815         if (!NT_STATUS_IS_OK(status)) {
816                 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
817                 return 0;
818         }
819
820         status = cli_qfileinfo_basic(cli, fnum, &mode, NULL, NULL, NULL,
821                                      NULL, NULL, NULL);
822         if (!NT_STATUS_IS_OK(status)) {
823                 printf("Failed to file info %s: %s\n", filename,
824                        nt_errstr(status));
825         }
826
827         cli_close(cli, fnum);
828
829         return mode;
830 }
831
832 /*****************************************************
833 get sec desc for filename
834 *******************************************************/
835 static struct security_descriptor *get_secdesc(struct cli_state *cli, const char *filename)
836 {
837         uint16_t fnum = (uint16_t)-1;
838         struct security_descriptor *sd;
839         NTSTATUS status;
840         uint32_t sec_info;
841         uint32_t desired_access = 0;
842
843         if (query_sec_info == -1) {
844                 sec_info = SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL;
845         } else {
846                 sec_info = query_sec_info;
847         }
848
849         if (sec_info & (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL)) {
850                 desired_access |= SEC_STD_READ_CONTROL;
851         }
852         if (sec_info & SECINFO_SACL) {
853                 desired_access |= SEC_FLAG_SYSTEM_SECURITY;
854         }
855
856         if (desired_access == 0) {
857                 desired_access |= SEC_STD_READ_CONTROL;
858         }
859
860         status = cli_ntcreate(cli, filename, 0, desired_access,
861                               0, FILE_SHARE_READ|FILE_SHARE_WRITE,
862                               FILE_OPEN, 0x0, 0x0, &fnum);
863         if (!NT_STATUS_IS_OK(status)) {
864                 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
865                 return NULL;
866         }
867
868         status = cli_query_security_descriptor(cli, fnum, sec_info,
869                                                talloc_tos(), &sd);
870
871         cli_close(cli, fnum);
872
873         if (!NT_STATUS_IS_OK(status)) {
874                 printf("Failed to get security descriptor: %s\n",
875                        nt_errstr(status));
876                 return NULL;
877         }
878         return sd;
879 }
880
881 /*****************************************************
882 set sec desc for filename
883 *******************************************************/
884 static bool set_secdesc(struct cli_state *cli, const char *filename,
885                         struct security_descriptor *sd)
886 {
887         uint16_t fnum = (uint16_t)-1;
888         bool result=true;
889         NTSTATUS status;
890         uint32_t desired_access = 0;
891         uint32_t sec_info;
892
893         if (set_sec_info == -1) {
894                 sec_info = 0;
895
896                 if (sd->dacl || (sd->type & SEC_DESC_DACL_PRESENT)) {
897                         sec_info |= SECINFO_DACL;
898                 }
899                 if (sd->sacl || (sd->type & SEC_DESC_SACL_PRESENT)) {
900                         sec_info |= SECINFO_SACL;
901                 }
902                 if (sd->owner_sid) {
903                         sec_info |= SECINFO_OWNER;
904                 }
905                 if (sd->group_sid) {
906                         sec_info |= SECINFO_GROUP;
907                 }
908         } else {
909                 sec_info = set_sec_info;
910         }
911
912         /* Make the desired_access more specific. */
913         if (sec_info & SECINFO_DACL) {
914                 desired_access |= SEC_STD_WRITE_DAC;
915         }
916         if (sec_info & SECINFO_SACL) {
917                 desired_access |= SEC_FLAG_SYSTEM_SECURITY;
918         }
919         if (sec_info & (SECINFO_OWNER | SECINFO_GROUP)) {
920                 desired_access |= SEC_STD_WRITE_OWNER;
921         }
922
923         status = cli_ntcreate(cli, filename, 0,
924                               desired_access,
925                               0, FILE_SHARE_READ|FILE_SHARE_WRITE,
926                               FILE_OPEN, 0x0, 0x0, &fnum);
927         if (!NT_STATUS_IS_OK(status)) {
928                 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
929                 return false;
930         }
931
932         status = cli_set_security_descriptor(cli, fnum, sec_info, sd);
933         if (!NT_STATUS_IS_OK(status)) {
934                 printf("ERROR: security description set failed: %s\n",
935                        nt_errstr(status));
936                 result=false;
937         }
938
939         cli_close(cli, fnum);
940         return result;
941 }
942
943 /*****************************************************
944 dump the acls for a file
945 *******************************************************/
946 static int cacl_dump(struct cli_state *cli, const char *filename)
947 {
948         struct security_descriptor *sd;
949
950         if (test_args) {
951                 return EXIT_OK;
952         }
953
954         sd = get_secdesc(cli, filename);
955         if (sd == NULL) {
956                 return EXIT_FAILED;
957         }
958
959         if (sddl) {
960                 char *str = sddl_encode(talloc_tos(), sd, get_domain_sid(cli));
961                 if (str == NULL) {
962                         return EXIT_FAILED;
963                 }
964                 printf("%s\n", str);
965                 TALLOC_FREE(str);
966         } else {
967                 sec_desc_print(cli, stdout, sd);
968         }
969
970         return EXIT_OK;
971 }
972
973 /***************************************************** 
974 Change the ownership or group ownership of a file. Just
975 because the NT docs say this can't be done :-). JRA.
976 *******************************************************/
977
978 static int owner_set(struct cli_state *cli, enum chown_mode change_mode, 
979                         const char *filename, const char *new_username)
980 {
981         struct dom_sid sid;
982         struct security_descriptor *sd, *old;
983         size_t sd_size;
984
985         if (!StringToSid(cli, &sid, new_username))
986                 return EXIT_PARSE_ERROR;
987
988         old = get_secdesc(cli, filename);
989
990         if (!old) {
991                 return EXIT_FAILED;
992         }
993
994         sd = make_sec_desc(talloc_tos(),old->revision, old->type,
995                                 (change_mode == REQUEST_CHOWN) ? &sid : NULL,
996                                 (change_mode == REQUEST_CHGRP) ? &sid : NULL,
997                            NULL, NULL, &sd_size);
998
999         if (!set_secdesc(cli, filename, sd)) {
1000                 return EXIT_FAILED;
1001         }
1002
1003         return EXIT_OK;
1004 }
1005
1006
1007 /* The MSDN is contradictory over the ordering of ACE entries in an
1008    ACL.  However NT4 gives a "The information may have been modified
1009    by a computer running Windows NT 5.0" if denied ACEs do not appear
1010    before allowed ACEs. At
1011    http://technet.microsoft.com/en-us/library/cc781716.aspx the
1012    canonical order is specified as "Explicit Deny, Explicit Allow,
1013    Inherited ACEs unchanged" */
1014
1015 static int ace_compare(struct security_ace *ace1, struct security_ace *ace2)
1016 {
1017         if (sec_ace_equal(ace1, ace2))
1018                 return 0;
1019
1020         if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
1021                         !(ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
1022                 return 1;
1023         if (!(ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
1024                         (ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
1025                 return -1;
1026         if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
1027                         (ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
1028                 return ace1 - ace2;
1029
1030         if (ace1->type != ace2->type)
1031                 return ace2->type - ace1->type;
1032
1033         if (dom_sid_compare(&ace1->trustee, &ace2->trustee))
1034                 return dom_sid_compare(&ace1->trustee, &ace2->trustee);
1035
1036         if (ace1->flags != ace2->flags)
1037                 return ace1->flags - ace2->flags;
1038
1039         if (ace1->access_mask != ace2->access_mask)
1040                 return ace1->access_mask - ace2->access_mask;
1041
1042         if (ace1->size != ace2->size)
1043                 return ace1->size - ace2->size;
1044
1045         return memcmp(ace1, ace2, sizeof(struct security_ace));
1046 }
1047
1048 static void sort_acl(struct security_acl *the_acl)
1049 {
1050         uint32 i;
1051         if (!the_acl) return;
1052
1053         TYPESAFE_QSORT(the_acl->aces, the_acl->num_aces, ace_compare);
1054
1055         for (i=1;i<the_acl->num_aces;) {
1056                 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
1057                         int j;
1058                         for (j=i; j<the_acl->num_aces-1; j++) {
1059                                 the_acl->aces[j] = the_acl->aces[j+1];
1060                         }
1061                         the_acl->num_aces--;
1062                 } else {
1063                         i++;
1064                 }
1065         }
1066 }
1067
1068 /***************************************************** 
1069 set the ACLs on a file given an ascii description
1070 *******************************************************/
1071
1072 static int cacl_set(struct cli_state *cli, const char *filename,
1073                     char *the_acl, enum acl_mode mode)
1074 {
1075         struct security_descriptor *sd, *old;
1076         uint32 i, j;
1077         size_t sd_size;
1078         int result = EXIT_OK;
1079
1080         if (sddl) {
1081                 sd = sddl_decode(talloc_tos(), the_acl, get_domain_sid(cli));
1082         } else {
1083                 sd = sec_desc_parse(talloc_tos(), cli, the_acl);
1084         }
1085
1086         if (!sd) return EXIT_PARSE_ERROR;
1087         if (test_args) return EXIT_OK;
1088
1089         old = get_secdesc(cli, filename);
1090
1091         if (!old) {
1092                 return EXIT_FAILED;
1093         }
1094
1095         /* the logic here is rather more complex than I would like */
1096         switch (mode) {
1097         case SMB_ACL_DELETE:
1098                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1099                         bool found = False;
1100
1101                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
1102                                 if (sec_ace_equal(&sd->dacl->aces[i],
1103                                                   &old->dacl->aces[j])) {
1104                                         uint32 k;
1105                                         for (k=j; k<old->dacl->num_aces-1;k++) {
1106                                                 old->dacl->aces[k] = old->dacl->aces[k+1];
1107                                         }
1108                                         old->dacl->num_aces--;
1109                                         found = True;
1110                                         break;
1111                                 }
1112                         }
1113
1114                         if (!found) {
1115                                 printf("ACL for ACE:");
1116                                 print_ace(cli, stdout, &sd->dacl->aces[i]);
1117                                 printf(" not found\n");
1118                         }
1119                 }
1120                 break;
1121
1122         case SMB_ACL_MODIFY:
1123                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1124                         bool found = False;
1125
1126                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
1127                                 if (dom_sid_equal(&sd->dacl->aces[i].trustee,
1128                                               &old->dacl->aces[j].trustee)) {
1129                                         old->dacl->aces[j] = sd->dacl->aces[i];
1130                                         found = True;
1131                                 }
1132                         }
1133
1134                         if (!found) {
1135                                 fstring str;
1136
1137                                 SidToString(cli, str,
1138                                             &sd->dacl->aces[i].trustee);
1139                                 printf("ACL for SID %s not found\n", str);
1140                         }
1141                 }
1142
1143                 if (sd->owner_sid) {
1144                         old->owner_sid = sd->owner_sid;
1145                 }
1146
1147                 if (sd->group_sid) {
1148                         old->group_sid = sd->group_sid;
1149                 }
1150
1151                 break;
1152
1153         case SMB_ACL_ADD:
1154                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1155                         add_ace(&old->dacl, &sd->dacl->aces[i]);
1156                 }
1157                 break;
1158
1159         case SMB_ACL_SET:
1160                 old = sd;
1161                 break;
1162         }
1163
1164         /* Denied ACE entries must come before allowed ones */
1165         sort_acl(old->dacl);
1166
1167         /* Create new security descriptor and set it */
1168
1169         /* We used to just have "WRITE_DAC_ACCESS" without WRITE_OWNER.
1170            But if we're sending an owner, even if it's the same as the one
1171            that already exists then W2K3 insists we open with WRITE_OWNER access.
1172            I need to check that setting a SD with no owner set works against WNT
1173            and W2K. JRA.
1174         */
1175
1176         sd = make_sec_desc(talloc_tos(),old->revision, old->type,
1177                            old->owner_sid, old->group_sid,
1178                            NULL, old->dacl, &sd_size);
1179
1180         if (!set_secdesc(cli, filename, sd)) {
1181                 result = EXIT_FAILED;
1182         }
1183
1184         return result;
1185 }
1186
1187 /*****************************************************
1188 set the inherit on a file
1189 *******************************************************/
1190 static int inherit(struct cli_state *cli, const char *filename,
1191                    const char *type)
1192 {
1193         struct security_descriptor *old,*sd;
1194         uint32 oldattr;
1195         size_t sd_size;
1196         int result = EXIT_OK;
1197
1198         old = get_secdesc(cli, filename);
1199
1200         if (!old) {
1201                 return EXIT_FAILED;
1202         }
1203
1204         oldattr = get_fileinfo(cli,filename);
1205
1206         if (strcmp(type,"allow")==0) {
1207                 if ((old->type & SEC_DESC_DACL_PROTECTED) ==
1208                     SEC_DESC_DACL_PROTECTED) {
1209                         int i;
1210                         char *parentname,*temp;
1211                         struct security_descriptor *parent;
1212                         temp = talloc_strdup(talloc_tos(), filename);
1213
1214                         old->type=old->type & (~SEC_DESC_DACL_PROTECTED);
1215
1216                         /* look at parent and copy in all its inheritable ACL's. */
1217                         string_replace(temp, '\\', '/');
1218                         if (!parent_dirname(talloc_tos(),temp,&parentname,NULL)) {
1219                                 return EXIT_FAILED;
1220                         }
1221                         string_replace(parentname, '/', '\\');
1222                         parent = get_secdesc(cli,parentname);
1223                         if (parent == NULL) {
1224                                 return EXIT_FAILED;
1225                         }
1226                         for (i=0;i<parent->dacl->num_aces;i++) {
1227                                 struct security_ace *ace=&parent->dacl->aces[i];
1228                                 /* Add inherited flag to all aces */
1229                                 ace->flags=ace->flags|
1230                                            SEC_ACE_FLAG_INHERITED_ACE;
1231                                 if ((oldattr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) {
1232                                         if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ==
1233                                             SEC_ACE_FLAG_CONTAINER_INHERIT) {
1234                                                 add_ace(&old->dacl, ace);
1235                                         }
1236                                 } else {
1237                                         if ((ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT) ==
1238                                             SEC_ACE_FLAG_OBJECT_INHERIT) {
1239                                                 /* clear flags for files */
1240                                                 ace->flags=0;
1241                                                 add_ace(&old->dacl, ace);
1242                                         }
1243                                 }
1244                         }
1245                 } else {
1246                         printf("Already set to inheritable permissions.\n");
1247                         return EXIT_FAILED;
1248                 }
1249         } else if (strcmp(type,"remove")==0) {
1250                 if ((old->type & SEC_DESC_DACL_PROTECTED) !=
1251                     SEC_DESC_DACL_PROTECTED) {
1252                         old->type=old->type | SEC_DESC_DACL_PROTECTED;
1253
1254                         /* remove all inherited ACL's. */
1255                         if (old->dacl) {
1256                                 int i;
1257                                 struct security_acl *temp=old->dacl;
1258                                 old->dacl=make_sec_acl(talloc_tos(), 3, 0, NULL);
1259                                 for (i=temp->num_aces-1;i>=0;i--) {
1260                                         struct security_ace *ace=&temp->aces[i];
1261                                         /* Remove all ace with INHERITED flag set */
1262                                         if ((ace->flags & SEC_ACE_FLAG_INHERITED_ACE) !=
1263                                             SEC_ACE_FLAG_INHERITED_ACE) {
1264                                                 add_ace(&old->dacl,ace);
1265                                         }
1266                                 }
1267                         }
1268                 } else {
1269                         printf("Already set to no inheritable permissions.\n");
1270                         return EXIT_FAILED;
1271                 }
1272         } else if (strcmp(type,"copy")==0) {
1273                 if ((old->type & SEC_DESC_DACL_PROTECTED) !=
1274                     SEC_DESC_DACL_PROTECTED) {
1275                         old->type=old->type | SEC_DESC_DACL_PROTECTED;
1276
1277                         /* convert all inherited ACL's to non inherated ACL's. */
1278                         if (old->dacl) {
1279                                 int i;
1280                                 for (i=0;i<old->dacl->num_aces;i++) {
1281                                         struct security_ace *ace=&old->dacl->aces[i];
1282                                         /* Remove INHERITED FLAG from all aces */
1283                                         ace->flags=ace->flags&(~SEC_ACE_FLAG_INHERITED_ACE);
1284                                 }
1285                         }
1286                 } else {
1287                         printf("Already set to no inheritable permissions.\n");
1288                         return EXIT_FAILED;
1289                 }
1290         }
1291
1292         /* Denied ACE entries must come before allowed ones */
1293         sort_acl(old->dacl);
1294
1295         sd = make_sec_desc(talloc_tos(),old->revision, old->type,
1296                            old->owner_sid, old->group_sid,
1297                            NULL, old->dacl, &sd_size);
1298
1299         if (!set_secdesc(cli, filename, sd)) {
1300                 result = EXIT_FAILED;
1301         }
1302
1303         return result;
1304 }
1305
1306 /*****************************************************
1307  Return a connection to a server.
1308 *******************************************************/
1309 static struct cli_state *connect_one(struct user_auth_info *auth_info,
1310                                      const char *server, const char *share)
1311 {
1312         struct cli_state *c = NULL;
1313         NTSTATUS nt_status;
1314         uint32_t flags = 0;
1315
1316         if (get_cmdline_auth_info_use_kerberos(auth_info)) {
1317                 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
1318                          CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1319         }
1320
1321         if (get_cmdline_auth_info_use_machine_account(auth_info) &&
1322             !set_cmdline_auth_info_machine_account_creds(auth_info)) {
1323                 return NULL;
1324         }
1325
1326         set_cmdline_auth_info_getpass(auth_info);
1327
1328         nt_status = cli_full_connection(&c, lp_netbios_name(), server,
1329                                 NULL, 0,
1330                                 share, "?????",
1331                                 get_cmdline_auth_info_username(auth_info),
1332                                 lp_workgroup(),
1333                                 get_cmdline_auth_info_password(auth_info),
1334                                 flags,
1335                                 get_cmdline_auth_info_signing_state(auth_info));
1336         if (!NT_STATUS_IS_OK(nt_status)) {
1337                 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
1338                 return NULL;
1339         }
1340
1341         if (get_cmdline_auth_info_smb_encrypt(auth_info)) {
1342                 nt_status = cli_cm_force_encryption(c,
1343                                         get_cmdline_auth_info_username(auth_info),
1344                                         get_cmdline_auth_info_password(auth_info),
1345                                         lp_workgroup(),
1346                                         share);
1347                 if (!NT_STATUS_IS_OK(nt_status)) {
1348                         cli_shutdown(c);
1349                         c = NULL;
1350                 }
1351         }
1352
1353         return c;
1354 }
1355
1356 /****************************************************************************
1357   main program
1358 ****************************************************************************/
1359  int main(int argc, const char *argv[])
1360 {
1361         char *share;
1362         int opt;
1363         enum acl_mode mode = SMB_ACL_SET;
1364         static char *the_acl = NULL;
1365         enum chown_mode change_mode = REQUEST_NONE;
1366         int result;
1367         char *path;
1368         char *filename = NULL;
1369         poptContext pc;
1370         struct poptOption long_options[] = {
1371                 POPT_AUTOHELP
1372                 { "delete", 'D', POPT_ARG_STRING, NULL, 'D', "Delete an acl", "ACL" },
1373                 { "modify", 'M', POPT_ARG_STRING, NULL, 'M', "Modify an acl", "ACL" },
1374                 { "add", 'a', POPT_ARG_STRING, NULL, 'a', "Add an acl", "ACL" },
1375                 { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls", "ACLS" },
1376                 { "chown", 'C', POPT_ARG_STRING, NULL, 'C', "Change ownership of a file", "USERNAME" },
1377                 { "chgrp", 'G', POPT_ARG_STRING, NULL, 'G', "Change group ownership of a file", "GROUPNAME" },
1378                 { "inherit", 'I', POPT_ARG_STRING, NULL, 'I', "Inherit allow|remove|copy" },
1379                 { "numeric", 0, POPT_ARG_NONE, &numeric, 1, "Don't resolve sids or masks to names" },
1380                 { "sddl", 0, POPT_ARG_NONE, &sddl, 1, "Output and input acls in sddl format" },
1381                 { "query-security-info", 0, POPT_ARG_INT, &query_sec_info, 1,
1382                   "The security-info flags for queries"
1383                 },
1384                 { "set-security-info", 0, POPT_ARG_INT, &set_sec_info, 1,
1385                   "The security-info flags for modifications"
1386                 },
1387                 { "test-args", 't', POPT_ARG_NONE, &test_args, 1, "Test arguments"},
1388                 { "domain-sid", 0, POPT_ARG_STRING, &domain_sid, 0, "Domain SID for sddl", "SID"},
1389                 POPT_COMMON_SAMBA
1390                 POPT_COMMON_CONNECTION
1391                 POPT_COMMON_CREDENTIALS
1392                 POPT_TABLEEND
1393         };
1394
1395         struct cli_state *cli;
1396         TALLOC_CTX *frame = talloc_stackframe();
1397         const char *owner_username = "";
1398         char *server;
1399         struct user_auth_info *auth_info;
1400
1401         load_case_tables();
1402
1403         /* set default debug level to 1 regardless of what smb.conf sets */
1404         setup_logging( "smbcacls", DEBUG_STDERR);
1405         lp_set_cmdline("log level", "1");
1406
1407         setlinebuf(stdout);
1408
1409         lp_load_global(get_dyn_CONFIGFILE());
1410         load_interfaces();
1411
1412         auth_info = user_auth_info_init(frame);
1413         if (auth_info == NULL) {
1414                 exit(1);
1415         }
1416         popt_common_set_auth_info(auth_info);
1417
1418         pc = poptGetContext("smbcacls", argc, argv, long_options, 0);
1419
1420         poptSetOtherOptionHelp(pc, "//server1/share1 filename\nACLs look like: "
1421                 "'ACL:user:[ALLOWED|DENIED]/flags/permissions'");
1422
1423         while ((opt = poptGetNextOpt(pc)) != -1) {
1424                 switch (opt) {
1425                 case 'S':
1426                         the_acl = smb_xstrdup(poptGetOptArg(pc));
1427                         mode = SMB_ACL_SET;
1428                         break;
1429
1430                 case 'D':
1431                         the_acl = smb_xstrdup(poptGetOptArg(pc));
1432                         mode = SMB_ACL_DELETE;
1433                         break;
1434
1435                 case 'M':
1436                         the_acl = smb_xstrdup(poptGetOptArg(pc));
1437                         mode = SMB_ACL_MODIFY;
1438                         break;
1439
1440                 case 'a':
1441                         the_acl = smb_xstrdup(poptGetOptArg(pc));
1442                         mode = SMB_ACL_ADD;
1443                         break;
1444
1445                 case 'C':
1446                         owner_username = poptGetOptArg(pc);
1447                         change_mode = REQUEST_CHOWN;
1448                         break;
1449
1450                 case 'G':
1451                         owner_username = poptGetOptArg(pc);
1452                         change_mode = REQUEST_CHGRP;
1453                         break;
1454
1455                 case 'I':
1456                         owner_username = poptGetOptArg(pc);
1457                         change_mode = REQUEST_INHERIT;
1458                         break;
1459                 }
1460         }
1461
1462         /* Make connection to server */
1463         if(!poptPeekArg(pc)) {
1464                 poptPrintUsage(pc, stderr, 0);
1465                 return -1;
1466         }
1467
1468         path = talloc_strdup(frame, poptGetArg(pc));
1469         if (!path) {
1470                 return -1;
1471         }
1472
1473         if(!poptPeekArg(pc)) {
1474                 poptPrintUsage(pc, stderr, 0);
1475                 return -1;
1476         }
1477
1478         filename = talloc_strdup(frame, poptGetArg(pc));
1479         if (!filename) {
1480                 return -1;
1481         }
1482
1483         poptFreeContext(pc);
1484         popt_burn_cmdline_password(argc, argv);
1485
1486         string_replace(path,'/','\\');
1487
1488         server = talloc_strdup(frame, path+2);
1489         if (!server) {
1490                 return -1;
1491         }
1492         share = strchr_m(server,'\\');
1493         if (!share) {
1494                 printf("Invalid argument: %s\n", share);
1495                 return -1;
1496         }
1497
1498         *share = 0;
1499         share++;
1500
1501         if (!test_args) {
1502                 cli = connect_one(auth_info, server, share);
1503                 if (!cli) {
1504                         exit(EXIT_FAILED);
1505                 }
1506         } else {
1507                 exit(0);
1508         }
1509
1510         string_replace(filename, '/', '\\');
1511         if (filename[0] != '\\') {
1512                 filename = talloc_asprintf(frame,
1513                                 "\\%s",
1514                                 filename);
1515                 if (!filename) {
1516                         return -1;
1517                 }
1518         }
1519
1520         /* Perform requested action */
1521
1522         if (change_mode == REQUEST_INHERIT) {
1523                 result = inherit(cli, filename, owner_username);
1524         } else if (change_mode != REQUEST_NONE) {
1525                 result = owner_set(cli, change_mode, filename, owner_username);
1526         } else if (the_acl) {
1527                 result = cacl_set(cli, filename, the_acl, mode);
1528         } else {
1529                 result = cacl_dump(cli, filename);
1530         }
1531
1532         TALLOC_FREE(frame);
1533
1534         return result;
1535 }