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