add WITH_SENDFILE profiling data (from Pierre Belanger)
[Samba.git] / source / utils / smbcacls.c
blob7ef2d76d7bfbb77f4d63b2fe4ac1469100ecc1cc
1 /*
2 Unix SMB/CIFS implementation.
3 ACL get/set utility
5 Copyright (C) Andrew Tridgell 2000
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jeremy Allison 2000
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
26 static fstring password;
27 static pstring username;
28 static pstring owner_username;
29 static fstring server;
30 static fstring workgroup = "";
31 static int got_pass;
32 static int test_args;
33 TALLOC_CTX *ctx;
35 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
36 #define CREATE_ACCESS_WRITE (WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS)
38 /* numeric is set when the user wants numeric SIDs and ACEs rather
39 than going via LSA calls to resolve them */
40 static int numeric;
42 enum acl_mode {SMB_ACL_SET, SMB_ACL_DELETE, SMB_ACL_MODIFY, SMB_ACL_ADD };
43 enum chown_mode {REQUEST_NONE, REQUEST_CHOWN, REQUEST_CHGRP};
44 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
46 struct perm_value {
47 char *perm;
48 uint32 mask;
51 /* These values discovered by inspection */
53 static struct perm_value special_values[] = {
54 { "R", 0x00120089 },
55 { "W", 0x00120116 },
56 { "X", 0x001200a0 },
57 { "D", 0x00010000 },
58 { "P", 0x00040000 },
59 { "O", 0x00080000 },
60 { NULL, 0 },
63 static struct perm_value standard_values[] = {
64 { "READ", 0x001200a9 },
65 { "CHANGE", 0x001301bf },
66 { "FULL", 0x001f01ff },
67 { NULL, 0 },
70 struct cli_state lsa_cli;
71 POLICY_HND pol;
72 struct ntuser_creds creds;
73 BOOL got_policy_hnd;
75 /* Open cli connection and policy handle */
77 static BOOL cacls_open_policy_hnd(void)
79 creds.pwd.null_pwd = 1;
81 /* Initialise cli LSA connection */
83 if (!lsa_cli.initialised &&
84 !cli_lsa_initialise(&lsa_cli, server, &creds)) {
85 return False;
88 /* Open policy handle */
90 if (!got_policy_hnd) {
92 /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
93 but NT sends 0x2000000 so we might as well do it too. */
95 if (!NT_STATUS_IS_OK(cli_lsa_open_policy(&lsa_cli, lsa_cli.mem_ctx, True,
96 GENERIC_EXECUTE_ACCESS, &pol))) {
97 return False;
100 got_policy_hnd = True;
103 return True;
106 /* convert a SID to a string, either numeric or username/group */
107 static void SidToString(fstring str, DOM_SID *sid)
109 char **domains = NULL;
110 char **names = NULL;
111 uint32 *types = NULL;
113 sid_to_string(str, sid);
115 if (numeric) return;
117 if (strcmp(str, "S-1-1-0") == 0) {
119 fstrcpy(str, "everyone");
120 return;
124 /* Ask LSA to convert the sid to a name */
126 if (!cacls_open_policy_hnd() ||
127 !NT_STATUS_IS_OK(cli_lsa_lookup_sids(&lsa_cli, lsa_cli.mem_ctx,
128 &pol, 1, sid, &domains,
129 &names, &types)) ||
130 !domains || !domains[0] || !names || !names[0]) {
131 return;
134 /* Converted OK */
136 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
137 domains[0], lp_winbind_separator(),
138 names[0]);
142 /* convert a string to a SID, either numeric or username/group */
143 static BOOL StringToSid(DOM_SID *sid, const char *str)
145 uint32 *types = NULL;
146 DOM_SID *sids = NULL;
147 BOOL result = True;
149 if (strncmp(str, "S-", 2) == 0) {
150 return string_to_sid(sid, str);
153 if (!cacls_open_policy_hnd() ||
154 !NT_STATUS_IS_OK(cli_lsa_lookup_names(&lsa_cli, lsa_cli.mem_ctx,
155 &pol, 1, &str, &sids,
156 &types))) {
157 result = False;
158 goto done;
161 sid_copy(sid, &sids[0]);
163 done:
165 return result;
169 /* print an ACE on a FILE, using either numeric or ascii representation */
170 static void print_ace(FILE *f, SEC_ACE *ace)
172 struct perm_value *v;
173 fstring sidstr;
174 int do_print = 0;
175 uint32 got_mask;
177 SidToString(sidstr, &ace->trustee);
179 fprintf(f, "%s:", sidstr);
181 if (numeric) {
182 fprintf(f, "%d/%d/0x%08x",
183 ace->type, ace->flags, ace->info.mask);
184 return;
187 /* Ace type */
189 if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
190 fprintf(f, "ALLOWED");
191 } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
192 fprintf(f, "DENIED");
193 } else {
194 fprintf(f, "%d", ace->type);
197 /* Not sure what flags can be set in a file ACL */
199 fprintf(f, "/%d/", ace->flags);
201 /* Standard permissions */
203 for (v = standard_values; v->perm; v++) {
204 if (ace->info.mask == v->mask) {
205 fprintf(f, "%s", v->perm);
206 return;
210 /* Special permissions. Print out a hex value if we have
211 leftover bits in the mask. */
213 got_mask = ace->info.mask;
215 again:
216 for (v = special_values; v->perm; v++) {
217 if ((ace->info.mask & v->mask) == v->mask) {
218 if (do_print) {
219 fprintf(f, "%s", v->perm);
221 got_mask &= ~v->mask;
225 if (!do_print) {
226 if (got_mask != 0) {
227 fprintf(f, "0x%08x", ace->info.mask);
228 } else {
229 do_print = 1;
230 goto again;
236 /* parse an ACE in the same format as print_ace() */
237 static BOOL parse_ace(SEC_ACE *ace, char *str)
239 char *p;
240 fstring tok;
241 unsigned atype, aflags, amask;
242 DOM_SID sid;
243 SEC_ACCESS mask;
244 struct perm_value *v;
246 ZERO_STRUCTP(ace);
247 p = strchr(str,':');
248 if (!p) return False;
249 *p = '\0';
250 p++;
252 /* Try to parse numeric form */
254 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
255 StringToSid(&sid, str)) {
256 goto done;
259 /* Try to parse text form */
261 if (!StringToSid(&sid, str)) {
262 return False;
265 if (!next_token(&p, tok, "/", sizeof(fstring))) {
266 return False;
269 if (strncmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
270 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
271 } else if (strncmp(tok, "DENIED", strlen("DENIED")) == 0) {
272 atype = SEC_ACE_TYPE_ACCESS_DENIED;
273 } else {
274 return False;
277 /* Only numeric form accepted for flags at present */
279 if (!(next_token(&p, tok, "/", sizeof(fstring)) &&
280 sscanf(tok, "%i", &aflags))) {
281 return False;
284 if (!next_token(&p, tok, "/", sizeof(fstring))) {
285 return False;
288 if (strncmp(tok, "0x", 2) == 0) {
289 if (sscanf(tok, "%i", &amask) != 1) {
290 return False;
292 goto done;
295 for (v = standard_values; v->perm; v++) {
296 if (strcmp(tok, v->perm) == 0) {
297 amask = v->mask;
298 goto done;
302 p = tok;
304 while(*p) {
305 BOOL found = False;
307 for (v = special_values; v->perm; v++) {
308 if (v->perm[0] == *p) {
309 amask |= v->mask;
310 found = True;
314 if (!found) return False;
315 p++;
318 if (*p) {
319 return False;
322 done:
323 mask.mask = amask;
324 init_sec_ace(ace, &sid, atype, mask, aflags);
325 return True;
328 /* add an ACE to a list of ACEs in a SEC_ACL */
329 static BOOL add_ace(SEC_ACL **the_acl, SEC_ACE *ace)
331 SEC_ACL *new;
332 SEC_ACE *aces;
333 if (! *the_acl) {
334 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
335 return True;
338 aces = calloc(1+(*the_acl)->num_aces,sizeof(SEC_ACE));
339 memcpy(aces, (*the_acl)->ace, (*the_acl)->num_aces * sizeof(SEC_ACE));
340 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
341 new = make_sec_acl(ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
342 SAFE_FREE(aces);
343 (*the_acl) = new;
344 return True;
347 /* parse a ascii version of a security descriptor */
348 static SEC_DESC *sec_desc_parse(char *str)
350 char *p = str;
351 fstring tok;
352 SEC_DESC *ret;
353 size_t sd_size;
354 DOM_SID *grp_sid=NULL, *owner_sid=NULL;
355 SEC_ACL *dacl=NULL;
356 int revision=1;
358 while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
360 if (strncmp(tok,"REVISION:", 9) == 0) {
361 revision = strtol(tok+9, NULL, 16);
362 continue;
365 if (strncmp(tok,"OWNER:", 6) == 0) {
366 owner_sid = (DOM_SID *)calloc(1, sizeof(DOM_SID));
367 if (!owner_sid ||
368 !StringToSid(owner_sid, tok+6)) {
369 printf("Failed to parse owner sid\n");
370 return NULL;
372 continue;
375 if (strncmp(tok,"GROUP:", 6) == 0) {
376 grp_sid = (DOM_SID *)calloc(1, sizeof(DOM_SID));
377 if (!grp_sid ||
378 !StringToSid(grp_sid, tok+6)) {
379 printf("Failed to parse group sid\n");
380 return NULL;
382 continue;
385 if (strncmp(tok,"ACL:", 4) == 0) {
386 SEC_ACE ace;
387 if (!parse_ace(&ace, tok+4)) {
388 printf("Failed to parse ACL %s\n", tok);
389 return NULL;
391 if(!add_ace(&dacl, &ace)) {
392 printf("Failed to add ACL %s\n", tok);
393 return NULL;
395 continue;
398 printf("Failed to parse security descriptor\n");
399 return NULL;
402 ret = make_sec_desc(ctx,revision, owner_sid, grp_sid,
403 NULL, dacl, &sd_size);
405 SAFE_FREE(grp_sid);
406 SAFE_FREE(owner_sid);
408 return ret;
412 /* print a ascii version of a security descriptor on a FILE handle */
413 static void sec_desc_print(FILE *f, SEC_DESC *sd)
415 fstring sidstr;
416 uint32 i;
418 printf("REVISION:%d\n", sd->revision);
420 /* Print owner and group sid */
422 if (sd->owner_sid) {
423 SidToString(sidstr, sd->owner_sid);
424 } else {
425 fstrcpy(sidstr, "");
428 printf("OWNER:%s\n", sidstr);
430 if (sd->grp_sid) {
431 SidToString(sidstr, sd->grp_sid);
432 } else {
433 fstrcpy(sidstr, "");
436 fprintf(f, "GROUP:%s\n", sidstr);
438 /* Print aces */
439 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
440 SEC_ACE *ace = &sd->dacl->ace[i];
441 fprintf(f, "ACL:");
442 print_ace(f, ace);
443 fprintf(f, "\n");
448 /*****************************************************
449 dump the acls for a file
450 *******************************************************/
451 static int cacl_dump(struct cli_state *cli, char *filename)
453 int fnum;
454 SEC_DESC *sd;
456 if (test_args) return EXIT_OK;
458 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
459 if (fnum == -1) {
460 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
461 return EXIT_FAILED;
464 sd = cli_query_secdesc(cli, fnum, ctx);
466 if (!sd) {
467 printf("ERROR: secdesc query failed: %s\n", cli_errstr(cli));
468 return EXIT_FAILED;
471 sec_desc_print(stdout, sd);
473 cli_close(cli, fnum);
475 return EXIT_OK;
478 /*****************************************************
479 Change the ownership or group ownership of a file. Just
480 because the NT docs say this can't be done :-). JRA.
481 *******************************************************/
483 static int owner_set(struct cli_state *cli, enum chown_mode change_mode,
484 char *filename, char *new_username)
486 int fnum;
487 DOM_SID sid;
488 SEC_DESC *sd, *old;
489 size_t sd_size;
491 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
493 if (fnum == -1) {
494 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
495 return EXIT_FAILED;
498 if (!StringToSid(&sid, new_username))
499 return EXIT_PARSE_ERROR;
501 old = cli_query_secdesc(cli, fnum, ctx);
503 cli_close(cli, fnum);
505 if (!old) {
506 printf("owner_set: Failed to query old descriptor\n");
507 return EXIT_FAILED;
510 sd = make_sec_desc(ctx,old->revision,
511 (change_mode == REQUEST_CHOWN) ? &sid : old->owner_sid,
512 (change_mode == REQUEST_CHGRP) ? &sid : old->grp_sid,
513 NULL, old->dacl, &sd_size);
515 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
517 if (fnum == -1) {
518 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
519 return EXIT_FAILED;
522 if (!cli_set_secdesc(cli, fnum, sd)) {
523 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
526 cli_close(cli, fnum);
528 return EXIT_OK;
532 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
533 However NT4 gives a "The information may have been modified by a
534 computer running Windows NT 5.0" if denied ACEs do not appear before
535 allowed ACEs. */
537 static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
539 if (sec_ace_equal(ace1, ace2))
540 return 0;
542 if (ace1->type != ace2->type)
543 return ace2->type - ace1->type;
545 if (sid_compare(&ace1->trustee, &ace2->trustee))
546 return sid_compare(&ace1->trustee, &ace2->trustee);
548 if (ace1->flags != ace2->flags)
549 return ace1->flags - ace2->flags;
551 if (ace1->info.mask != ace2->info.mask)
552 return ace1->info.mask - ace2->info.mask;
554 if (ace1->size != ace2->size)
555 return ace1->size - ace2->size;
557 return memcmp(ace1, ace2, sizeof(SEC_ACE));
560 static void sort_acl(SEC_ACL *the_acl)
562 uint32 i;
563 if (!the_acl) return;
565 qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]), QSORT_CAST ace_compare);
567 for (i=1;i<the_acl->num_aces;) {
568 if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
569 int j;
570 for (j=i; j<the_acl->num_aces-1; j++) {
571 the_acl->ace[j] = the_acl->ace[j+1];
573 the_acl->num_aces--;
574 } else {
575 i++;
580 /*****************************************************
581 set the ACLs on a file given an ascii description
582 *******************************************************/
583 static int cacl_set(struct cli_state *cli, char *filename,
584 char *the_acl, enum acl_mode mode)
586 int fnum;
587 SEC_DESC *sd, *old;
588 uint32 i, j;
589 size_t sd_size;
590 int result = EXIT_OK;
592 sd = sec_desc_parse(the_acl);
594 if (!sd) return EXIT_PARSE_ERROR;
595 if (test_args) return EXIT_OK;
597 /* The desired access below is the only one I could find that works
598 with NT4, W2KP and Samba */
600 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
602 if (fnum == -1) {
603 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
604 return EXIT_FAILED;
607 old = cli_query_secdesc(cli, fnum, ctx);
609 if (!old) {
610 printf("calc_set: Failed to query old descriptor\n");
611 return EXIT_FAILED;
614 cli_close(cli, fnum);
616 /* the logic here is rather more complex than I would like */
617 switch (mode) {
618 case SMB_ACL_DELETE:
619 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
620 BOOL found = False;
622 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
623 if (sec_ace_equal(&sd->dacl->ace[i],
624 &old->dacl->ace[j])) {
625 uint32 k;
626 for (k=j; k<old->dacl->num_aces-1;k++) {
627 old->dacl->ace[k] = old->dacl->ace[k+1];
629 old->dacl->num_aces--;
630 if (old->dacl->num_aces == 0) {
631 SAFE_FREE(old->dacl->ace);
632 SAFE_FREE(old->dacl);
633 old->off_dacl = 0;
635 found = True;
636 break;
640 if (!found) {
641 printf("ACL for ACE:");
642 print_ace(stdout, &sd->dacl->ace[i]);
643 printf(" not found\n");
646 break;
648 case SMB_ACL_MODIFY:
649 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
650 BOOL found = False;
652 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
653 if (sid_equal(&sd->dacl->ace[i].trustee,
654 &old->dacl->ace[j].trustee)) {
655 old->dacl->ace[j] = sd->dacl->ace[i];
656 found = True;
660 if (!found) {
661 fstring str;
663 SidToString(str, &sd->dacl->ace[i].trustee);
664 printf("ACL for SID %s not found\n", str);
668 break;
670 case SMB_ACL_ADD:
671 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
672 add_ace(&old->dacl, &sd->dacl->ace[i]);
674 break;
676 case SMB_ACL_SET:
677 old = sd;
678 break;
681 /* Denied ACE entries must come before allowed ones */
682 sort_acl(old->dacl);
684 /* Create new security descriptor and set it */
685 sd = make_sec_desc(ctx,old->revision, old->owner_sid, old->grp_sid,
686 NULL, old->dacl, &sd_size);
688 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
690 if (fnum == -1) {
691 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
692 return EXIT_FAILED;
695 if (!cli_set_secdesc(cli, fnum, sd)) {
696 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
697 result = EXIT_FAILED;
700 /* Clean up */
702 cli_close(cli, fnum);
704 return result;
708 /*****************************************************
709 return a connection to a server
710 *******************************************************/
711 struct cli_state *connect_one(char *share)
713 struct cli_state *c;
714 struct nmb_name called, calling;
715 struct in_addr ip;
716 extern pstring global_myname;
718 fstrcpy(server,share+2);
719 share = strchr(server,'\\');
720 if (!share) return NULL;
721 *share = 0;
722 share++;
724 zero_ip(&ip);
726 make_nmb_name(&calling, global_myname, 0x0);
727 make_nmb_name(&called , server, 0x20);
729 again:
730 zero_ip(&ip);
732 /* have to open a new connection */
733 if (!(c=cli_initialise(NULL)) || !cli_connect(c, server, &ip)) {
734 DEBUG(0,("Connection to %s failed\n", server));
735 cli_shutdown(c);
736 return NULL;
739 if (!cli_session_request(c, &calling, &called)) {
740 DEBUG(0,("session request to %s failed\n", called.name));
741 cli_shutdown(c);
742 if (strcmp(called.name, "*SMBSERVER")) {
743 make_nmb_name(&called , "*SMBSERVER", 0x20);
744 goto again;
746 return NULL;
749 DEBUG(4,(" session request ok\n"));
751 if (!cli_negprot(c)) {
752 DEBUG(0,("protocol negotiation failed\n"));
753 cli_shutdown(c);
754 return NULL;
757 if (!got_pass) {
758 char *pass = getpass("Password: ");
759 if (pass) {
760 pstrcpy(password, pass);
764 if (!cli_session_setup(c, username,
765 password, strlen(password),
766 password, strlen(password),
767 (workgroup[0] ? workgroup : lp_workgroup()))) {
768 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
769 cli_shutdown(c);
770 return NULL;
773 DEBUG(4,(" session setup ok\n"));
775 if (!cli_send_tconX(c, share, "?????",
776 password, strlen(password)+1)) {
777 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
778 cli_shutdown(c);
779 return NULL;
782 DEBUG(4,(" tconx ok\n"));
784 return c;
788 static void usage(void)
790 printf("Usage: smbcacls //server1/share1 filename [options]\n");
791 printf("Version: %s\n", VERSION);
792 printf(
793 "\n\
794 \t-D <acls> delete an acl\n\
795 \t-M <acls> modify an acl\n\
796 \t-A <acls> add an acl\n\
797 \t-S <acls> set acls\n\
798 \t-C username change ownership of a file\n\
799 \t-G username change group ownership of a file\n\
800 \t-n don't resolve sids or masks to names\n\
801 \t-h print help\n\
802 \t-d debuglevel set debug output level\n\
803 \t-U username user to autheticate as\n\
804 \t-W workgroup or domain workgroup or domain user is in\n\
806 The username can be of the form username%%password or\n\
807 workgroup\\username%%password.\n\n\
808 An acl is of the form ACL:<SID>:type/flags/mask\n\
809 You can string acls together with spaces, commas or newlines\n\
813 /****************************************************************************
814 main program
815 ****************************************************************************/
816 int main(int argc,char *argv[])
818 char *share;
819 pstring filename;
820 extern char *optarg;
821 extern int optind;
822 extern FILE *dbf;
823 int opt;
824 char *p;
825 static pstring servicesf = CONFIGFILE;
826 struct cli_state *cli=NULL;
827 enum acl_mode mode = SMB_ACL_SET;
828 char *the_acl = NULL;
829 enum chown_mode change_mode = REQUEST_NONE;
830 int result;
832 ctx=talloc_init();
834 setlinebuf(stdout);
836 dbf = stderr;
838 if (argc < 3 || argv[1][0] == '-') {
839 usage();
840 talloc_destroy(ctx);
841 exit(EXIT_PARSE_ERROR);
844 setup_logging(argv[0],True);
846 share = argv[1];
847 pstrcpy(filename, argv[2]);
848 all_string_sub(share,"/","\\",0);
850 argc -= 2;
851 argv += 2;
853 TimeInit();
854 charset_initialise();
856 lp_load(servicesf,True,False,False);
857 codepage_initialise(lp_client_code_page());
858 load_interfaces();
860 if (getenv("USER")) {
861 pstrcpy(username,getenv("USER"));
863 if ((p=strchr(username,'%'))) {
864 *p = 0;
865 pstrcpy(password,p+1);
866 got_pass = True;
867 memset(strchr(getenv("USER"), '%') + 1, 'X',
868 strlen(password));
872 while ((opt = getopt(argc, argv, "U:nhdS:D:A:M:C:G:t")) != EOF) {
873 switch (opt) {
874 case 'U':
875 pstrcpy(username,optarg);
876 p = strchr(username,'%');
877 if (p) {
878 *p = 0;
879 pstrcpy(password, p+1);
880 got_pass = 1;
882 break;
884 case 'W':
885 pstrcpy(workgroup, optarg);
886 break;
888 case 'S':
889 the_acl = optarg;
890 mode = SMB_ACL_SET;
891 break;
893 case 'D':
894 the_acl = optarg;
895 mode = SMB_ACL_DELETE;
896 break;
898 case 'M':
899 the_acl = optarg;
900 mode = SMB_ACL_MODIFY;
901 break;
903 case 'A':
904 the_acl = optarg;
905 mode = SMB_ACL_ADD;
906 break;
908 case 'C':
909 pstrcpy(owner_username,optarg);
910 change_mode = REQUEST_CHOWN;
911 break;
913 case 'G':
914 pstrcpy(owner_username,optarg);
915 change_mode = REQUEST_CHGRP;
916 break;
918 case 'n':
919 numeric = 1;
920 break;
922 case 't':
923 test_args = 1;
924 break;
926 case 'h':
927 usage();
928 talloc_destroy(ctx);
929 exit(EXIT_PARSE_ERROR);
931 case 'd':
932 DEBUGLEVEL = atoi(optarg);
933 break;
935 default:
936 printf("Unknown option %c (%d)\n", (char)opt, opt);
937 talloc_destroy(ctx);
938 exit(EXIT_PARSE_ERROR);
942 argc -= optind;
943 argv += optind;
945 if (argc > 0) {
946 usage();
947 talloc_destroy(ctx);
948 exit(EXIT_PARSE_ERROR);
951 /* Make connection to server */
953 if (!test_args) {
954 cli = connect_one(share);
955 if (!cli) {
956 talloc_destroy(ctx);
957 exit(EXIT_FAILED);
961 all_string_sub(filename, "/", "\\", 0);
962 if (filename[0] != '\\') {
963 pstring s;
964 s[0] = '\\';
965 safe_strcpy(&s[1], filename, sizeof(pstring)-1);
966 pstrcpy(filename, s);
969 /* Perform requested action */
971 if (change_mode != REQUEST_NONE) {
972 result = owner_set(cli, change_mode, filename, owner_username);
973 } else if (the_acl) {
974 result = cacl_set(cli, filename, the_acl, mode);
975 } else {
976 result = cacl_dump(cli, filename);
979 talloc_destroy(ctx);
981 return result;