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