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