6f04ddccd946c2074f492fe5a91b6f0529ae6b39
[vlendec/samba-autobuild/.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_cmdline.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 static int sddl;
38 static int query_sec_info = -1;
39 static int set_sec_info = -1;
40 static bool want_mxac;
41
42 static const char *domain_sid = NULL;
43
44 enum acl_mode {SMB_ACL_SET, SMB_ACL_DELETE, SMB_ACL_MODIFY, SMB_ACL_ADD };
45 enum chown_mode {REQUEST_NONE, REQUEST_CHOWN, REQUEST_CHGRP, REQUEST_INHERIT};
46 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
47
48 static NTSTATUS cli_lsa_lookup_domain_sid(struct cli_state *cli,
49                                           struct dom_sid *sid)
50 {
51         union lsa_PolicyInformation *info = NULL;
52         struct smbXcli_tcon *orig_tcon = NULL;
53         struct rpc_pipe_client *rpc_pipe = NULL;
54         struct policy_handle handle;
55         NTSTATUS status, result;
56         TALLOC_CTX *frame = talloc_stackframe();
57
58         if (cli_state_has_tcon(cli)) {
59                 orig_tcon = cli_state_save_tcon(cli);
60                 if (orig_tcon == NULL) {
61                         status = NT_STATUS_NO_MEMORY;
62                         goto done;
63                 }
64         }
65
66         status = cli_tree_connect(cli, "IPC$", "?????", NULL);
67         if (!NT_STATUS_IS_OK(status)) {
68                 goto done;
69         }
70
71         status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc, &rpc_pipe);
72         if (!NT_STATUS_IS_OK(status)) {
73                 goto tdis;
74         }
75
76         status = rpccli_lsa_open_policy(rpc_pipe, frame, True,
77                                         GENERIC_EXECUTE_ACCESS, &handle);
78         if (!NT_STATUS_IS_OK(status)) {
79                 goto tdis;
80         }
81
82         status = dcerpc_lsa_QueryInfoPolicy2(rpc_pipe->binding_handle,
83                                              frame, &handle,
84                                              LSA_POLICY_INFO_DOMAIN,
85                                              &info, &result);
86
87         if (any_nt_status_not_ok(status, result, &status)) {
88                 goto tdis;
89         }
90
91         *sid = *info->domain.sid;
92
93 tdis:
94         TALLOC_FREE(rpc_pipe);
95         cli_tdis(cli);
96 done:
97         cli_state_restore_tcon(cli, orig_tcon);
98         TALLOC_FREE(frame);
99         return status;
100 }
101
102 static struct dom_sid *get_domain_sid(struct cli_state *cli)
103 {
104         NTSTATUS status;
105         struct dom_sid_buf buf;
106
107         struct dom_sid *sid = talloc(talloc_tos(), struct dom_sid);
108         if (sid == NULL) {
109                 DEBUG(0, ("Out of memory\n"));
110                 return NULL;
111         }
112
113         if (domain_sid) {
114                 if (!dom_sid_parse(domain_sid, sid)) {
115                         DEBUG(0,("failed to parse domain sid\n"));
116                         TALLOC_FREE(sid);
117                 }
118         } else {
119                 status = cli_lsa_lookup_domain_sid(cli, sid);
120
121                 if (!NT_STATUS_IS_OK(status)) {
122                         DEBUG(0,("failed to lookup domain sid: %s\n", nt_errstr(status)));
123                         TALLOC_FREE(sid);
124                 }
125
126         }
127
128         DEBUG(2,("Domain SID: %s\n", dom_sid_str_buf(sid, &buf)));
129         return sid;
130 }
131
132 /* add an ACE to a list of ACEs in a struct security_acl */
133 static bool add_ace(struct security_acl **the_acl, struct security_ace *ace)
134 {
135         struct security_acl *new_ace;
136         struct security_ace *aces;
137         if (! *the_acl) {
138                 return (((*the_acl) = make_sec_acl(talloc_tos(), 3, 1, ace))
139                         != NULL);
140         }
141
142         if (!(aces = SMB_CALLOC_ARRAY(struct security_ace, 1+(*the_acl)->num_aces))) {
143                 return False;
144         }
145         memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(struct
146         security_ace));
147         memcpy(aces+(*the_acl)->num_aces, ace, sizeof(struct security_ace));
148         new_ace = make_sec_acl(talloc_tos(),(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
149         SAFE_FREE(aces);
150         (*the_acl) = new_ace;
151         return True;
152 }
153
154 /* parse a ascii version of a security descriptor */
155 static struct security_descriptor *sec_desc_parse(TALLOC_CTX *ctx, struct cli_state *cli, char *str)
156 {
157         const char *p = str;
158         char *tok;
159         struct security_descriptor *ret = NULL;
160         size_t sd_size;
161         struct dom_sid *grp_sid=NULL, *owner_sid=NULL;
162         struct security_acl *dacl=NULL;
163         int revision=1;
164
165         while (next_token_talloc(ctx, &p, &tok, "\t,\r\n")) {
166                 if (strncmp(tok,"REVISION:", 9) == 0) {
167                         revision = strtol(tok+9, NULL, 16);
168                         continue;
169                 }
170
171                 if (strncmp(tok,"OWNER:", 6) == 0) {
172                         if (owner_sid) {
173                                 printf("Only specify owner once\n");
174                                 goto done;
175                         }
176                         owner_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
177                         if (!owner_sid ||
178                             !StringToSid(cli, owner_sid, tok+6)) {
179                                 printf("Failed to parse owner sid\n");
180                                 goto done;
181                         }
182                         continue;
183                 }
184
185                 if (strncmp(tok,"GROUP:", 6) == 0) {
186                         if (grp_sid) {
187                                 printf("Only specify group once\n");
188                                 goto done;
189                         }
190                         grp_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
191                         if (!grp_sid ||
192                             !StringToSid(cli, grp_sid, tok+6)) {
193                                 printf("Failed to parse group sid\n");
194                                 goto done;
195                         }
196                         continue;
197                 }
198
199                 if (strncmp(tok,"ACL:", 4) == 0) {
200                         struct security_ace ace;
201                         if (!parse_ace(cli, &ace, tok+4)) {
202                                 goto done;
203                         }
204                         if(!add_ace(&dacl, &ace)) {
205                                 printf("Failed to add ACL %s\n", tok);
206                                 goto done;
207                         }
208                         continue;
209                 }
210
211                 printf("Failed to parse token '%s' in security descriptor,\n", tok);
212                 goto done;
213         }
214
215         ret = make_sec_desc(ctx,revision, SEC_DESC_SELF_RELATIVE, owner_sid, grp_sid,
216                             NULL, dacl, &sd_size);
217
218   done:
219         SAFE_FREE(grp_sid);
220         SAFE_FREE(owner_sid);
221
222         return ret;
223 }
224
225 /*****************************************************
226 get fileinfo for filename
227 *******************************************************/
228 static uint16_t get_fileinfo(struct cli_state *cli, const char *filename)
229 {
230         uint16_t fnum = (uint16_t)-1;
231         NTSTATUS status;
232         struct smb_create_returns cr = {0};
233
234         /* The desired access below is the only one I could find that works
235            with NT4, W2KP and Samba */
236
237         status = cli_ntcreate(
238                 cli,                    /* cli */
239                 filename,               /* fname */
240                 0,                      /* CreatFlags */
241                 READ_CONTROL_ACCESS,    /* CreatFlags */
242                 0,                      /* FileAttributes */
243                 FILE_SHARE_READ|
244                 FILE_SHARE_WRITE,       /* ShareAccess */
245                 FILE_OPEN,              /* CreateDisposition */
246                 0x0,                    /* CreateOptions */
247                 0x0,                    /* SecurityFlags */
248                 &fnum,                  /* pfid */
249                 &cr);                   /* cr */
250         if (!NT_STATUS_IS_OK(status)) {
251                 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
252                 return 0;
253         }
254
255         cli_close(cli, fnum);
256         return cr.file_attributes;
257 }
258
259 /*****************************************************
260 get sec desc for filename
261 *******************************************************/
262 static struct security_descriptor *get_secdesc(struct cli_state *cli, const char *filename)
263 {
264         uint16_t fnum = (uint16_t)-1;
265         struct security_descriptor *sd;
266         NTSTATUS status;
267         uint32_t sec_info;
268         uint32_t desired_access = 0;
269
270         if (query_sec_info == -1) {
271                 sec_info = SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL;
272         } else {
273                 sec_info = query_sec_info;
274         }
275
276         if (sec_info & (SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL)) {
277                 desired_access |= SEC_STD_READ_CONTROL;
278         }
279         if (sec_info & SECINFO_SACL) {
280                 desired_access |= SEC_FLAG_SYSTEM_SECURITY;
281         }
282
283         if (desired_access == 0) {
284                 desired_access |= SEC_STD_READ_CONTROL;
285         }
286
287         status = cli_ntcreate(cli, filename, 0, desired_access,
288                               0, FILE_SHARE_READ|FILE_SHARE_WRITE,
289                               FILE_OPEN, 0x0, 0x0, &fnum, NULL);
290         if (!NT_STATUS_IS_OK(status)) {
291                 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
292                 return NULL;
293         }
294
295         status = cli_query_security_descriptor(cli, fnum, sec_info,
296                                                talloc_tos(), &sd);
297
298         cli_close(cli, fnum);
299
300         if (!NT_STATUS_IS_OK(status)) {
301                 printf("Failed to get security descriptor: %s\n",
302                        nt_errstr(status));
303                 return NULL;
304         }
305         return sd;
306 }
307
308 /*****************************************************
309 set sec desc for filename
310 *******************************************************/
311 static bool set_secdesc(struct cli_state *cli, const char *filename,
312                         struct security_descriptor *sd)
313 {
314         uint16_t fnum = (uint16_t)-1;
315         bool result=true;
316         NTSTATUS status;
317         uint32_t desired_access = 0;
318         uint32_t sec_info;
319
320         if (set_sec_info == -1) {
321                 sec_info = 0;
322
323                 if (sd->dacl || (sd->type & SEC_DESC_DACL_PRESENT)) {
324                         sec_info |= SECINFO_DACL;
325                 }
326                 if (sd->sacl || (sd->type & SEC_DESC_SACL_PRESENT)) {
327                         sec_info |= SECINFO_SACL;
328                 }
329                 if (sd->owner_sid) {
330                         sec_info |= SECINFO_OWNER;
331                 }
332                 if (sd->group_sid) {
333                         sec_info |= SECINFO_GROUP;
334                 }
335         } else {
336                 sec_info = set_sec_info;
337         }
338
339         /* Make the desired_access more specific. */
340         if (sec_info & SECINFO_DACL) {
341                 desired_access |= SEC_STD_WRITE_DAC;
342         }
343         if (sec_info & SECINFO_SACL) {
344                 desired_access |= SEC_FLAG_SYSTEM_SECURITY;
345         }
346         if (sec_info & (SECINFO_OWNER | SECINFO_GROUP)) {
347                 desired_access |= SEC_STD_WRITE_OWNER;
348         }
349
350         status = cli_ntcreate(cli, filename, 0,
351                               desired_access,
352                               0, FILE_SHARE_READ|FILE_SHARE_WRITE,
353                               FILE_OPEN, 0x0, 0x0, &fnum, NULL);
354         if (!NT_STATUS_IS_OK(status)) {
355                 printf("Failed to open %s: %s\n", filename, nt_errstr(status));
356                 return false;
357         }
358
359         status = cli_set_security_descriptor(cli, fnum, sec_info, sd);
360         if (!NT_STATUS_IS_OK(status)) {
361                 printf("ERROR: security descriptor set failed: %s\n",
362                        nt_errstr(status));
363                 result=false;
364         }
365
366         cli_close(cli, fnum);
367         return result;
368 }
369
370 /*****************************************************
371 get maximum access for a file
372 *******************************************************/
373 static int cacl_mxac(struct cli_state *cli, const char *filename)
374 {
375         NTSTATUS status;
376         uint32_t mxac;
377
378         status = cli_query_mxac(cli, filename, &mxac);
379         if (!NT_STATUS_IS_OK(status)) {
380                 printf("Failed to get mxac: %s\n", nt_errstr(status));
381                 return EXIT_FAILED;
382         }
383
384         printf("Maximum access: 0x%x\n", mxac);
385
386         return EXIT_OK;
387 }
388
389
390 /*****************************************************
391 dump the acls for a file
392 *******************************************************/
393 static int cacl_dump(struct cli_state *cli, const char *filename, bool numeric)
394 {
395         struct security_descriptor *sd;
396         int ret;
397
398         if (test_args) {
399                 return EXIT_OK;
400         }
401
402         sd = get_secdesc(cli, filename);
403         if (sd == NULL) {
404                 return EXIT_FAILED;
405         }
406
407         if (sddl) {
408                 char *str = sddl_encode(talloc_tos(), sd, get_domain_sid(cli));
409                 if (str == NULL) {
410                         return EXIT_FAILED;
411                 }
412                 printf("%s\n", str);
413                 TALLOC_FREE(str);
414         } else {
415                 sec_desc_print(cli, stdout, sd, numeric);
416         }
417
418         if (want_mxac) {
419                 ret = cacl_mxac(cli, filename);
420                 if (ret != EXIT_OK) {
421                         return ret;
422                 }
423         }
424
425         return EXIT_OK;
426 }
427
428 /***************************************************** 
429 Change the ownership or group ownership of a file. Just
430 because the NT docs say this can't be done :-). JRA.
431 *******************************************************/
432
433 static int owner_set(struct cli_state *cli, enum chown_mode change_mode, 
434                         const char *filename, const char *new_username)
435 {
436         struct dom_sid sid;
437         struct security_descriptor *sd;
438         size_t sd_size;
439
440         if (!StringToSid(cli, &sid, new_username))
441                 return EXIT_PARSE_ERROR;
442
443         sd = make_sec_desc(talloc_tos(),
444                            SECURITY_DESCRIPTOR_REVISION_1,
445                            SEC_DESC_SELF_RELATIVE,
446                            (change_mode == REQUEST_CHOWN) ? &sid : NULL,
447                            (change_mode == REQUEST_CHGRP) ? &sid : NULL,
448                            NULL, NULL, &sd_size);
449
450         if (!set_secdesc(cli, filename, sd)) {
451                 return EXIT_FAILED;
452         }
453
454         return EXIT_OK;
455 }
456
457
458 /* The MSDN is contradictory over the ordering of ACE entries in an
459    ACL.  However NT4 gives a "The information may have been modified
460    by a computer running Windows NT 5.0" if denied ACEs do not appear
461    before allowed ACEs. At
462    http://technet.microsoft.com/en-us/library/cc781716.aspx the
463    canonical order is specified as "Explicit Deny, Explicit Allow,
464    Inherited ACEs unchanged" */
465
466 static int ace_compare(struct security_ace *ace1, struct security_ace *ace2)
467 {
468         if (security_ace_equal(ace1, ace2))
469                 return 0;
470
471         if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
472                         !(ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
473                 return 1;
474         if (!(ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
475                         (ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
476                 return -1;
477         if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
478                         (ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
479                 return ace1 - ace2;
480
481         if (ace1->type != ace2->type)
482                 return ace2->type - ace1->type;
483
484         if (dom_sid_compare(&ace1->trustee, &ace2->trustee))
485                 return dom_sid_compare(&ace1->trustee, &ace2->trustee);
486
487         if (ace1->flags != ace2->flags)
488                 return ace1->flags - ace2->flags;
489
490         if (ace1->access_mask != ace2->access_mask)
491                 return ace1->access_mask - ace2->access_mask;
492
493         if (ace1->size != ace2->size)
494                 return ace1->size - ace2->size;
495
496         return memcmp(ace1, ace2, sizeof(struct security_ace));
497 }
498
499 static void sort_acl(struct security_acl *the_acl)
500 {
501         uint32_t i;
502         if (!the_acl) return;
503
504         TYPESAFE_QSORT(the_acl->aces, the_acl->num_aces, ace_compare);
505
506         for (i=1;i<the_acl->num_aces;) {
507                 if (security_ace_equal(&the_acl->aces[i-1],
508                                        &the_acl->aces[i])) {
509                         ARRAY_DEL_ELEMENT(
510                                 the_acl->aces, i, the_acl->num_aces);
511                         the_acl->num_aces--;
512                 } else {
513                         i++;
514                 }
515         }
516 }
517
518 /***************************************************** 
519 set the ACLs on a file given an ascii description
520 *******************************************************/
521
522 static int cacl_set(struct cli_state *cli, const char *filename,
523                     char *the_acl, enum acl_mode mode, bool numeric)
524 {
525         struct security_descriptor *sd, *old;
526         uint32_t i, j;
527         size_t sd_size;
528         int result = EXIT_OK;
529
530         if (sddl) {
531                 sd = sddl_decode(talloc_tos(), the_acl, get_domain_sid(cli));
532         } else {
533                 sd = sec_desc_parse(talloc_tos(), cli, the_acl);
534         }
535
536         if (!sd) return EXIT_PARSE_ERROR;
537         if (test_args) return EXIT_OK;
538
539         if (mode != SMB_ACL_SET) {
540                 /*
541                  * Do not fetch old ACL when it will be overwritten
542                  * completely with a new one.
543                  */
544                 old = get_secdesc(cli, filename);
545
546                 if (!old) {
547                         return EXIT_FAILED;
548                 }
549         }
550
551         /* the logic here is rather more complex than I would like */
552         switch (mode) {
553         case SMB_ACL_DELETE:
554                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
555                         bool found = False;
556
557                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
558                                 if (security_ace_equal(&sd->dacl->aces[i],
559                                                        &old->dacl->aces[j])) {
560                                         uint32_t k;
561                                         for (k=j; k<old->dacl->num_aces-1;k++) {
562                                                 old->dacl->aces[k] = old->dacl->aces[k+1];
563                                         }
564                                         old->dacl->num_aces--;
565                                         found = True;
566                                         break;
567                                 }
568                         }
569
570                         if (!found) {
571                                 printf("ACL for ACE:");
572                                 print_ace(cli, stdout, &sd->dacl->aces[i],
573                                           numeric);
574                                 printf(" not found\n");
575                         }
576                 }
577                 break;
578
579         case SMB_ACL_MODIFY:
580                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
581                         bool found = False;
582
583                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
584                                 if (dom_sid_equal(&sd->dacl->aces[i].trustee,
585                                               &old->dacl->aces[j].trustee)) {
586                                         old->dacl->aces[j] = sd->dacl->aces[i];
587                                         found = True;
588                                 }
589                         }
590
591                         if (!found) {
592                                 fstring str;
593
594                                 SidToString(cli, str,
595                                             &sd->dacl->aces[i].trustee,
596                                             numeric);
597                                 printf("ACL for SID %s not found\n", str);
598                         }
599                 }
600
601                 if (sd->owner_sid) {
602                         old->owner_sid = sd->owner_sid;
603                 }
604
605                 if (sd->group_sid) {
606                         old->group_sid = sd->group_sid;
607                 }
608
609                 break;
610
611         case SMB_ACL_ADD:
612                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
613                         add_ace(&old->dacl, &sd->dacl->aces[i]);
614                 }
615                 break;
616
617         case SMB_ACL_SET:
618                 old = sd;
619                 break;
620         }
621
622         /* Denied ACE entries must come before allowed ones */
623         sort_acl(old->dacl);
624
625         /* Create new security descriptor and set it */
626
627         /* We used to just have "WRITE_DAC_ACCESS" without WRITE_OWNER.
628            But if we're sending an owner, even if it's the same as the one
629            that already exists then W2K3 insists we open with WRITE_OWNER access.
630            I need to check that setting a SD with no owner set works against WNT
631            and W2K. JRA.
632         */
633
634         sd = make_sec_desc(talloc_tos(),old->revision, old->type,
635                            old->owner_sid, old->group_sid,
636                            NULL, old->dacl, &sd_size);
637
638         if (!set_secdesc(cli, filename, sd)) {
639                 result = EXIT_FAILED;
640         }
641
642         return result;
643 }
644
645 /*****************************************************
646 set the inherit on a file
647 *******************************************************/
648 static int inherit(struct cli_state *cli, const char *filename,
649                    const char *type)
650 {
651         struct security_descriptor *old,*sd;
652         uint32_t oldattr;
653         size_t sd_size;
654         int result = EXIT_OK;
655
656         old = get_secdesc(cli, filename);
657
658         if (!old) {
659                 return EXIT_FAILED;
660         }
661
662         oldattr = get_fileinfo(cli,filename);
663
664         if (strcmp(type,"allow")==0) {
665                 if ((old->type & SEC_DESC_DACL_PROTECTED) ==
666                     SEC_DESC_DACL_PROTECTED) {
667                         uint32_t i;
668                         char *parentname,*temp;
669                         struct security_descriptor *parent;
670                         temp = talloc_strdup(talloc_tos(), filename);
671
672                         old->type=old->type & (~SEC_DESC_DACL_PROTECTED);
673
674                         /* look at parent and copy in all its inheritable ACL's. */
675                         string_replace(temp, '\\', '/');
676                         if (!parent_dirname(talloc_tos(),temp,&parentname,NULL)) {
677                                 return EXIT_FAILED;
678                         }
679                         string_replace(parentname, '/', '\\');
680                         parent = get_secdesc(cli,parentname);
681                         if (parent == NULL) {
682                                 return EXIT_FAILED;
683                         }
684                         for (i=0;i<parent->dacl->num_aces;i++) {
685                                 struct security_ace *ace=&parent->dacl->aces[i];
686                                 /* Add inherited flag to all aces */
687                                 ace->flags=ace->flags|
688                                            SEC_ACE_FLAG_INHERITED_ACE;
689                                 if ((oldattr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) {
690                                         if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ==
691                                             SEC_ACE_FLAG_CONTAINER_INHERIT) {
692                                                 add_ace(&old->dacl, ace);
693                                         }
694                                 } else {
695                                         if ((ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT) ==
696                                             SEC_ACE_FLAG_OBJECT_INHERIT) {
697                                                 /* clear flags for files */
698                                                 ace->flags=0;
699                                                 add_ace(&old->dacl, ace);
700                                         }
701                                 }
702                         }
703                 } else {
704                         printf("Already set to inheritable permissions.\n");
705                         return EXIT_FAILED;
706                 }
707         } else if (strcmp(type,"remove")==0) {
708                 if ((old->type & SEC_DESC_DACL_PROTECTED) !=
709                     SEC_DESC_DACL_PROTECTED) {
710                         old->type=old->type | SEC_DESC_DACL_PROTECTED;
711
712                         /* remove all inherited ACL's. */
713                         if (old->dacl) {
714                                 int i;
715                                 struct security_acl *temp=old->dacl;
716                                 old->dacl=make_sec_acl(talloc_tos(), 3, 0, NULL);
717                                 for (i=temp->num_aces-1;i>=0;i--) {
718                                         struct security_ace *ace=&temp->aces[i];
719                                         /* Remove all ace with INHERITED flag set */
720                                         if ((ace->flags & SEC_ACE_FLAG_INHERITED_ACE) !=
721                                             SEC_ACE_FLAG_INHERITED_ACE) {
722                                                 add_ace(&old->dacl,ace);
723                                         }
724                                 }
725                         }
726                 } else {
727                         printf("Already set to no inheritable permissions.\n");
728                         return EXIT_FAILED;
729                 }
730         } else if (strcmp(type,"copy")==0) {
731                 if ((old->type & SEC_DESC_DACL_PROTECTED) !=
732                     SEC_DESC_DACL_PROTECTED) {
733                         old->type=old->type | SEC_DESC_DACL_PROTECTED;
734
735                         /* convert all inherited ACL's to non inherated ACL's. */
736                         if (old->dacl) {
737                                 uint32_t i;
738                                 for (i=0;i<old->dacl->num_aces;i++) {
739                                         struct security_ace *ace=&old->dacl->aces[i];
740                                         /* Remove INHERITED FLAG from all aces */
741                                         ace->flags=ace->flags&(~SEC_ACE_FLAG_INHERITED_ACE);
742                                 }
743                         }
744                 } else {
745                         printf("Already set to no inheritable permissions.\n");
746                         return EXIT_FAILED;
747                 }
748         }
749
750         /* Denied ACE entries must come before allowed ones */
751         sort_acl(old->dacl);
752
753         sd = make_sec_desc(talloc_tos(),old->revision, old->type,
754                            old->owner_sid, old->group_sid,
755                            NULL, old->dacl, &sd_size);
756
757         if (!set_secdesc(cli, filename, sd)) {
758                 result = EXIT_FAILED;
759         }
760
761         return result;
762 }
763
764 /*****************************************************
765  Return a connection to a server.
766 *******************************************************/
767 static struct cli_state *connect_one(const struct user_auth_info *auth_info,
768                                      const char *server, const char *share)
769 {
770         struct cli_state *c = NULL;
771         NTSTATUS nt_status;
772         uint32_t flags = 0;
773
774         nt_status = cli_full_connection_creds(&c, lp_netbios_name(), server,
775                                 NULL, 0,
776                                 share, "?????",
777                                 get_cmdline_auth_info_creds(auth_info),
778                                 flags,
779                                 get_cmdline_auth_info_signing_state(auth_info));
780         if (!NT_STATUS_IS_OK(nt_status)) {
781                 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
782                 return NULL;
783         }
784
785         if (get_cmdline_auth_info_smb_encrypt(auth_info)) {
786                 nt_status = cli_cm_force_encryption_creds(c,
787                                         get_cmdline_auth_info_creds(auth_info),
788                                         share);
789                 if (!NT_STATUS_IS_OK(nt_status)) {
790                         cli_shutdown(c);
791                         c = NULL;
792                 }
793         }
794
795         return c;
796 }
797
798 /****************************************************************************
799   main program
800 ****************************************************************************/
801 int main(int argc, char *argv[])
802 {
803         const char **argv_const = discard_const_p(const char *, argv);
804         char *share;
805         int opt;
806         enum acl_mode mode = SMB_ACL_SET;
807         static char *the_acl = NULL;
808         enum chown_mode change_mode = REQUEST_NONE;
809         int result;
810         char *path;
811         char *filename = NULL;
812         poptContext pc;
813         /* numeric is set when the user wants numeric SIDs and ACEs rather
814            than going via LSA calls to resolve them */
815         int numeric = 0;
816
817         struct poptOption long_options[] = {
818                 POPT_AUTOHELP
819                 {
820                         .longName   = "delete",
821                         .shortName  = 'D',
822                         .argInfo    = POPT_ARG_STRING,
823                         .arg        = NULL,
824                         .val        = 'D',
825                         .descrip    = "Delete an acl",
826                         .argDescrip = "ACL",
827                 },
828                 {
829                         .longName   = "modify",
830                         .shortName  = 'M',
831                         .argInfo    = POPT_ARG_STRING,
832                         .arg        = NULL,
833                         .val        = 'M',
834                         .descrip    = "Modify an acl",
835                         .argDescrip = "ACL",
836                 },
837                 {
838                         .longName   = "add",
839                         .shortName  = 'a',
840                         .argInfo    = POPT_ARG_STRING,
841                         .arg        = NULL,
842                         .val        = 'a',
843                         .descrip    = "Add an acl",
844                         .argDescrip = "ACL",
845                 },
846                 {
847                         .longName   = "set",
848                         .shortName  = 'S',
849                         .argInfo    = POPT_ARG_STRING,
850                         .arg        = NULL,
851                         .val        = 'S',
852                         .descrip    = "Set acls",
853                         .argDescrip = "ACLS",
854                 },
855                 {
856                         .longName   = "chown",
857                         .shortName  = 'C',
858                         .argInfo    = POPT_ARG_STRING,
859                         .arg        = NULL,
860                         .val        = 'C',
861                         .descrip    = "Change ownership of a file",
862                         .argDescrip = "USERNAME",
863                 },
864                 {
865                         .longName   = "chgrp",
866                         .shortName  = 'G',
867                         .argInfo    = POPT_ARG_STRING,
868                         .arg        = NULL,
869                         .val        = 'G',
870                         .descrip    = "Change group ownership of a file",
871                         .argDescrip = "GROUPNAME",
872                 },
873                 {
874                         .longName   = "inherit",
875                         .shortName  = 'I',
876                         .argInfo    = POPT_ARG_STRING,
877                         .arg        = NULL,
878                         .val        = 'I',
879                         .descrip    = "Inherit allow|remove|copy",
880                 },
881                 {
882                         .longName   = "numeric",
883                         .shortName  = 0,
884                         .argInfo    = POPT_ARG_NONE,
885                         .arg        = &numeric,
886                         .val        = 1,
887                         .descrip    = "Don't resolve sids or masks to names",
888                 },
889                 {
890                         .longName   = "sddl",
891                         .shortName  = 0,
892                         .argInfo    = POPT_ARG_NONE,
893                         .arg        = &sddl,
894                         .val        = 1,
895                         .descrip    = "Output and input acls in sddl format",
896                 },
897                 {
898                         .longName   = "query-security-info",
899                         .shortName  = 0,
900                         .argInfo    = POPT_ARG_INT,
901                         .arg        = &query_sec_info,
902                         .val        = 1,
903                         .descrip    = "The security-info flags for queries"
904                 },
905                 {
906                         .longName   = "set-security-info",
907                         .shortName  = 0,
908                         .argInfo    = POPT_ARG_INT,
909                         .arg        = &set_sec_info,
910                         .val        = 1,
911                         .descrip    = "The security-info flags for modifications"
912                 },
913                 {
914                         .longName   = "test-args",
915                         .shortName  = 't',
916                         .argInfo    = POPT_ARG_NONE,
917                         .arg        = &test_args,
918                         .val        = 1,
919                         .descrip    = "Test arguments"
920                 },
921                 {
922                         .longName   = "domain-sid",
923                         .shortName  = 0,
924                         .argInfo    = POPT_ARG_STRING,
925                         .arg        = &domain_sid,
926                         .val        = 0,
927                         .descrip    = "Domain SID for sddl",
928                         .argDescrip = "SID"},
929                 {
930                         .longName   = "max-protocol",
931                         .shortName  = 'm',
932                         .argInfo    = POPT_ARG_STRING,
933                         .arg        = NULL,
934                         .val        = 'm',
935                         .descrip    = "Set the max protocol level",
936                         .argDescrip = "LEVEL",
937                 },
938                 {
939                         .longName   = "maximum-access",
940                         .shortName  = 'x',
941                         .argInfo    = POPT_ARG_NONE,
942                         .arg        = NULL,
943                         .val        = 'x',
944                         .descrip    = "Query maximum persmissions",
945                 },
946                 POPT_COMMON_SAMBA
947                 POPT_COMMON_CONNECTION
948                 POPT_COMMON_CREDENTIALS
949                 POPT_TABLEEND
950         };
951
952         struct cli_state *cli;
953         TALLOC_CTX *frame = talloc_stackframe();
954         const char *owner_username = "";
955         char *server;
956
957         smb_init_locale();
958
959         /* set default debug level to 1 regardless of what smb.conf sets */
960         setup_logging( "smbcacls", DEBUG_STDERR);
961         lp_set_cmdline("log level", "1");
962
963         setlinebuf(stdout);
964
965         popt_common_credentials_set_ignore_missing_conf();
966
967         pc = poptGetContext("smbcacls", argc, argv_const, long_options, 0);
968
969         poptSetOtherOptionHelp(pc, "//server1/share1 filename\nACLs look like: "
970                 "'ACL:user:[ALLOWED|DENIED]/flags/permissions'");
971
972         while ((opt = poptGetNextOpt(pc)) != -1) {
973                 switch (opt) {
974                 case 'S':
975                         the_acl = smb_xstrdup(poptGetOptArg(pc));
976                         mode = SMB_ACL_SET;
977                         break;
978
979                 case 'D':
980                         the_acl = smb_xstrdup(poptGetOptArg(pc));
981                         mode = SMB_ACL_DELETE;
982                         break;
983
984                 case 'M':
985                         the_acl = smb_xstrdup(poptGetOptArg(pc));
986                         mode = SMB_ACL_MODIFY;
987                         break;
988
989                 case 'a':
990                         the_acl = smb_xstrdup(poptGetOptArg(pc));
991                         mode = SMB_ACL_ADD;
992                         break;
993
994                 case 'C':
995                         owner_username = poptGetOptArg(pc);
996                         change_mode = REQUEST_CHOWN;
997                         break;
998
999                 case 'G':
1000                         owner_username = poptGetOptArg(pc);
1001                         change_mode = REQUEST_CHGRP;
1002                         break;
1003
1004                 case 'I':
1005                         owner_username = poptGetOptArg(pc);
1006                         change_mode = REQUEST_INHERIT;
1007                         break;
1008                 case 'm':
1009                         lp_set_cmdline("client max protocol", poptGetOptArg(pc));
1010                         break;
1011                 case 'x':
1012                         want_mxac = true;
1013                         break;
1014                 }
1015         }
1016
1017         /* Make connection to server */
1018         if(!poptPeekArg(pc)) {
1019                 poptPrintUsage(pc, stderr, 0);
1020                 return -1;
1021         }
1022
1023         path = talloc_strdup(frame, poptGetArg(pc));
1024         if (!path) {
1025                 return -1;
1026         }
1027
1028         if(!poptPeekArg(pc)) {
1029                 poptPrintUsage(pc, stderr, 0);
1030                 return -1;
1031         }
1032
1033         filename = talloc_strdup(frame, poptGetArg(pc));
1034         if (!filename) {
1035                 return -1;
1036         }
1037
1038         poptFreeContext(pc);
1039         popt_burn_cmdline_password(argc, argv);
1040         popt_common_credentials_post();
1041
1042         string_replace(path,'/','\\');
1043
1044         server = talloc_strdup(frame, path+2);
1045         if (!server) {
1046                 return -1;
1047         }
1048         share = strchr_m(server,'\\');
1049         if (share == NULL) {
1050                 printf("Invalid argument\n");
1051                 return -1;
1052         }
1053
1054         *share = 0;
1055         share++;
1056
1057         if (!test_args) {
1058                 cli = connect_one(popt_get_cmdline_auth_info(), server, share);
1059                 if (!cli) {
1060                         exit(EXIT_FAILED);
1061                 }
1062         } else {
1063                 popt_free_cmdline_auth_info();
1064                 exit(0);
1065         }
1066
1067         string_replace(filename, '/', '\\');
1068         if (filename[0] != '\\') {
1069                 filename = talloc_asprintf(frame,
1070                                 "\\%s",
1071                                 filename);
1072                 if (!filename) {
1073                         return -1;
1074                 }
1075         }
1076
1077         /* Perform requested action */
1078
1079         if (change_mode == REQUEST_INHERIT) {
1080                 result = inherit(cli, filename, owner_username);
1081         } else if (change_mode != REQUEST_NONE) {
1082                 result = owner_set(cli, change_mode, filename, owner_username);
1083         } else if (the_acl) {
1084                 result = cacl_set(cli, filename, the_acl, mode, numeric);
1085         } else {
1086                 result = cacl_dump(cli, filename, numeric);
1087         }
1088
1089         popt_free_cmdline_auth_info();
1090         TALLOC_FREE(frame);
1091
1092         return result;
1093 }