source: vendor/current/source3/utils/smbcacls.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

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