Added -C username and -G username that allow a user (usually Administrator)
[samba.git] / source3 / utils / smbcacls.c
1 /* 
2    Unix SMB/Netbios implementation.
3    ACL get/set utility
4    Version 3.0
5    
6    Copyright (C) Andrew Tridgell 2000
7    Copyright (C) Tim Potter      2000
8    Copyright (C) Jeremy Allison  2000
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 2 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, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26
27 static fstring password;
28 static pstring username;
29 static pstring owner_username;
30 static fstring server;
31 static int got_pass;
32 static int test_args;
33
34 /* numeric is set when the user wants numeric SIDs and ACEs rather
35    than going via LSA calls to resolve them */
36 static int numeric;
37
38 enum acl_mode {ACL_SET, ACL_DELETE, ACL_MODIFY, ACL_ADD };
39 enum chown_mode {REQUEST_NONE, REQUEST_CHOWN, REQUEST_CHGRP};
40
41 struct perm_value {
42         char *perm;
43         uint32 mask;
44 };
45
46 /* These values discovered by inspection */
47
48 static struct perm_value special_values[] = {
49         { "R", 0x00120089 },
50         { "W", 0x00120116 },
51         { "X", 0x001200a0 },
52         { "D", 0x00010000 },
53         { "P", 0x00040000 },
54         { "O", 0x00080000 },
55         { NULL, 0 },
56 };
57
58 static struct perm_value standard_values[] = {
59         { "READ",   0x001200a9 },
60         { "CHANGE", 0x001301bf },
61         { "FULL",   0x001f01ff },
62         { NULL, 0 },
63 };
64
65 struct cli_state lsa_cli;
66 POLICY_HND pol;
67 struct ntuser_creds creds;
68 BOOL got_policy_hnd;
69
70 /* Open cli connection and policy handle */
71
72 static BOOL open_policy_hnd(void)
73 {
74         creds.pwd.null_pwd = 1;
75
76         /* Initialise cli LSA connection */
77
78         if (!lsa_cli.initialised && 
79             !cli_lsa_initialise(&lsa_cli, server, &creds)) {
80                 return False;
81         }
82
83         /* Open policy handle */
84
85         if (!got_policy_hnd) {
86                 if (cli_lsa_open_policy(&lsa_cli, True, 
87                                         SEC_RIGHTS_MAXIMUM_ALLOWED, &pol)
88                     != NT_STATUS_NOPROBLEMO) {
89                         return False;
90                 }
91
92                 got_policy_hnd = True;
93         }
94         
95         return True;
96 }
97
98 /* convert a SID to a string, either numeric or username/group */
99 static void SidToString(fstring str, DOM_SID *sid)
100 {
101         char **names = NULL;
102         uint32 *types = NULL;
103         int num_names;
104
105         sid_to_string(str, sid);
106
107         if (numeric) return;
108
109         /* Ask LSA to convert the sid to a name */
110
111         if (open_policy_hnd() &&
112             cli_lsa_lookup_sids(&lsa_cli, &pol, 1, sid, &names, &types, 
113                                 &num_names) == NT_STATUS_NOPROBLEMO) {
114
115                 /* Converted OK */
116
117                 fstrcpy(str, names[0]);
118
119                 safe_free(names[0]);
120                 safe_free(names);
121                 safe_free(types);
122         }
123 }
124
125 /* convert a string to a SID, either numeric or username/group */
126 static BOOL StringToSid(DOM_SID *sid, char *str)
127 {
128         uint32 *types = NULL;
129         DOM_SID *sids = NULL;
130         int num_sids;
131         BOOL result = True;
132         
133         /* Short cut */
134
135         if (strncmp(str, "S-", 2) == 0) {
136                 result = string_to_sid(sid, str);
137                 goto done;
138         }
139
140         if (open_policy_hnd() &&
141             cli_lsa_lookup_names(&lsa_cli, &pol, 1, &str, &sids, &types, 
142                                  &num_sids) != NT_STATUS_NOPROBLEMO) {
143                 result = string_to_sid(sid, str);
144                 goto done;
145         }
146
147         sid_copy(sid, &sids[0]);
148
149         safe_free(sids);
150         safe_free(types);
151
152  done:
153
154         return result;
155 }
156
157
158 /* print an ACE on a FILE, using either numeric or ascii representation */
159 static void print_ace(FILE *f, SEC_ACE *ace)
160 {
161         struct perm_value *v;
162         fstring sidstr;
163         int do_print = 0;
164         uint32 got_mask;
165
166         SidToString(sidstr, &ace->sid);
167
168         fprintf(f, "%s:", sidstr);
169
170         if (numeric) {
171                 fprintf(f, "%d/%d/0x%08x\n", 
172                         ace->type, ace->flags, ace->info.mask);
173                 return;
174         }
175
176         /* Ace type */
177
178         if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
179                 fprintf(f, "ALLOWED");
180         } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
181                 fprintf(f, "DENIED");
182         } else {
183                 fprintf(f, "%d", ace->type);
184         }
185
186         /* Not sure what flags can be set in a file ACL */
187
188         fprintf(f, "/%d/", ace->flags);
189
190         /* Standard permissions */
191
192         for (v = standard_values; v->perm; v++) {
193                 if (ace->info.mask == v->mask) {
194                         fprintf(f, "%s\n", v->perm);
195                         return;
196                 }
197         }
198
199         /* Special permissions.  Print out a hex value if we have
200            leftover bits in the mask. */
201
202         got_mask = ace->info.mask;
203
204  again:
205         for (v = special_values; v->perm; v++) {
206                 if ((ace->info.mask & v->mask) == v->mask) {
207                         if (do_print) {
208                                 fprintf(f, "%s", v->perm);
209                         }
210                         got_mask &= ~v->mask;
211                 }
212         }
213
214         if (!do_print) {
215                 if (got_mask != 0) {
216                         fprintf(f, "0x%08x", ace->info.mask);
217                 } else {
218                         do_print = 1;
219                         goto again;
220                 }
221         }
222
223         fprintf(f, "\n");
224 }
225
226
227 /* parse an ACE in the same format as print_ace() */
228 static BOOL parse_ace(SEC_ACE *ace, char *str)
229 {
230         char *p;
231         fstring tok;
232         unsigned atype, aflags, amask;
233         DOM_SID sid;
234         SEC_ACCESS mask;
235         struct perm_value *v;
236
237         ZERO_STRUCTP(ace);
238         p = strchr(str,':');
239         if (!p) return False;
240         *p = '\0';
241         p++;
242
243         /* Try to parse numeric form */
244
245         if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
246             StringToSid(&sid, str)) {
247                 goto done;
248         }
249
250         /* Try to parse text form */
251
252         if (!StringToSid(&sid, str)) {
253                 return False;
254         }
255
256         if (!next_token(&p, tok, "/", sizeof(fstring))) {
257                 return False;
258         }
259
260         if (strncmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
261                 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
262         } else if (strncmp(tok, "DENIED", strlen("DENIED")) == 0) {
263                 atype = SEC_ACE_TYPE_ACCESS_DENIED;
264         } else {
265                 return False;
266         }
267
268         /* Only numeric form accepted for flags at present */
269
270         if (!(next_token(NULL, tok, "/", sizeof(fstring)) &&
271               sscanf(tok, "%i", &aflags))) {
272                 return False;
273         }
274
275         if (!next_token(NULL, tok, "/", sizeof(fstring))) {
276                 return False;
277         }
278
279         if (strncmp(tok, "0x", 2) == 0) {
280                 if (sscanf(tok, "%i", &amask) != 1) {
281                         return False;
282                 }
283                 goto done;
284         }
285
286         for (v = standard_values; v->perm; v++) {
287                 if (strcmp(tok, v->perm) == 0) {
288                         amask = v->mask;
289                         goto done;
290                 }
291         }
292
293         p = tok;
294
295         while(*p) {
296                 BOOL found = False;
297
298                 for (v = special_values; v->perm; v++) {
299                         if (v->perm[0] == *p) {
300                                 amask |= v->mask;
301                                 found = True;
302                         }
303                 }
304
305                 if (!found) return False;
306                 p++;
307         }
308
309         if (*p) {
310                 return False;
311         }
312
313  done:
314         mask.mask = amask;
315         init_sec_ace(ace, &sid, atype, mask, aflags);
316         return True;
317 }
318
319 /* add an ACE to a list of ACEs in a SEC_ACL */
320 static BOOL add_ace(SEC_ACL **the_acl, SEC_ACE *ace)
321 {
322         SEC_ACL *new;
323         SEC_ACE *aces;
324         if (! *the_acl) {
325                 (*the_acl) = make_sec_acl(3, 1, ace);
326                 return True;
327         }
328
329         aces = calloc(1+(*the_acl)->num_aces,sizeof(SEC_ACE));
330         memcpy(aces, (*the_acl)->ace, (*the_acl)->num_aces * sizeof(SEC_ACE));
331         memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
332         new = make_sec_acl((*the_acl)->revision,1+(*the_acl)->num_aces, aces);
333         free_sec_acl(the_acl);
334         free(aces);
335         (*the_acl) = new;
336         return True;
337 }
338
339 /* parse a ascii version of a security descriptor */
340 static SEC_DESC *sec_desc_parse(char *str)
341 {
342         char *p = str;
343         fstring tok;
344         SEC_DESC *ret;
345         unsigned sd_size;
346         DOM_SID *grp_sid=NULL, *owner_sid=NULL;
347         SEC_ACL *dacl=NULL;
348         int revision=1;
349
350         while (next_token(&p, tok, " \t,\r\n", sizeof(tok))) {
351
352                 if (strncmp(tok,"REVISION:", 9) == 0) {
353                         revision = strtol(tok+9, NULL, 16);
354                         continue;
355                 }
356
357                 if (strncmp(tok,"OWNER:", 6) == 0) {
358                         owner_sid = (DOM_SID *)calloc(1, sizeof(DOM_SID));
359                         if (!owner_sid ||
360                             !StringToSid(owner_sid, tok+6)) {
361                                 printf("Failed to parse owner sid\n");
362                                 return NULL;
363                         }
364                         continue;
365                 }
366
367                 if (strncmp(tok,"GROUP:", 6) == 0) {
368                         grp_sid = (DOM_SID *)calloc(1, sizeof(DOM_SID));
369                         if (!grp_sid ||
370                             !StringToSid(grp_sid, tok+6)) {
371                                 printf("Failed to parse group sid\n");
372                                 return NULL;
373                         }
374                         continue;
375                 }
376
377                 if (strncmp(tok,"ACL:", 4) == 0) {
378                         SEC_ACE ace;
379                         if (!parse_ace(&ace, tok+4)) {
380                                 printf("Failed to parse ACL %s\n", tok);
381                                 return NULL;
382                         }
383                         if(!add_ace(&dacl, &ace)) {
384                                 printf("Failed to add ACL %s\n", tok);
385                                 return NULL;
386                         }
387                         continue;
388                 }
389
390                 printf("Failed to parse security descriptor\n");
391                 return NULL;
392         }
393
394         ret = make_sec_desc(revision, owner_sid, grp_sid, 
395                             NULL, dacl, &sd_size);
396
397         free_sec_acl(&dacl);
398
399         if (grp_sid) free(grp_sid);
400         if (owner_sid) free(owner_sid);
401
402         return ret;
403 }
404
405
406 /* print a ascii version of a security descriptor on a FILE handle */
407 static void sec_desc_print(FILE *f, SEC_DESC *sd)
408 {
409         fstring sidstr;
410         int i;
411
412         printf("REVISION:%d\n", sd->revision);
413
414         /* Print owner and group sid */
415
416         if (sd->owner_sid) {
417                 SidToString(sidstr, sd->owner_sid);
418         } else {
419                 fstrcpy(sidstr, "");
420         }
421
422         printf("OWNER:%s\n", sidstr);
423
424         if (sd->grp_sid) {
425                 SidToString(sidstr, sd->grp_sid);
426         } else {
427                 fstrcpy(sidstr, "");
428         }
429
430         fprintf(f, "GROUP:%s\n", sidstr);
431
432         /* Print aces */
433         for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
434                 SEC_ACE *ace = &sd->dacl->ace[i];
435                 fprintf(f, "ACL:");
436                 print_ace(f, ace);
437         }
438
439 }
440
441
442 /***************************************************** 
443 dump the acls for a file
444 *******************************************************/
445 static void cacl_dump(struct cli_state *cli, char *filename)
446 {
447         int fnum;
448         SEC_DESC *sd;
449
450         if (test_args) return;
451
452         fnum = cli_nt_create(cli, filename, 0x20000);
453         if (fnum == -1) {
454                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
455                 return;
456         }
457
458         sd = cli_query_secdesc(cli, fnum);
459
460         if (!sd) {
461                 printf("ERROR: secdesc query failed: %s\n", cli_errstr(cli));
462                 return;
463         }
464
465         sec_desc_print(stdout, sd);
466
467         free_sec_desc(&sd);
468
469         cli_close(cli, fnum);
470 }
471
472 /***************************************************** 
473 Change the ownership or group ownership of a file. Just
474 because the NT docs say this can't be done :-). JRA.
475 *******************************************************/
476
477 static void owner_set(struct cli_state *cli, enum chown_mode change_mode, char *filename, char *new_username)
478 {
479         int fnum;
480         DOM_SID sid;
481         SEC_DESC *sd, *old;
482         size_t sd_size;
483
484         fnum = cli_nt_create(cli, filename, READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS);
485         if (fnum == -1) {
486                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
487                 return;
488         }
489
490         if (!StringToSid(&sid, new_username))
491                 return;
492
493         old = cli_query_secdesc(cli, fnum);
494
495         sd = make_sec_desc(old->revision,
496                                 (change_mode == REQUEST_CHOWN) ? &sid : old->owner_sid,
497                                 (change_mode == REQUEST_CHGRP) ? &sid : old->grp_sid,
498                            NULL, old->dacl, &sd_size);
499
500         if (!cli_set_secdesc(cli, fnum, sd)) {
501                 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
502         }
503
504         free_sec_desc(&sd);
505         free_sec_desc(&old);
506
507         cli_close(cli, fnum);
508 }
509
510 /***************************************************** 
511 set the ACLs on a file given an ascii description
512 *******************************************************/
513 static void cacl_set(struct cli_state *cli, char *filename, 
514                      char *the_acl, enum acl_mode mode)
515 {
516         int fnum;
517         SEC_DESC *sd, *old;
518         int i, j;
519         size_t sd_size;
520
521         sd = sec_desc_parse(the_acl);
522
523         if (!sd) return;
524         if (test_args) return;
525
526         /* the desired access below is the only one I could find that works with
527            NT4, W2KP and Samba */
528         fnum = cli_nt_create(cli, filename, MAXIMUM_ALLOWED_ACCESS | 0x60000);
529         if (fnum == -1) {
530                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
531                 return;
532         }
533
534         old = cli_query_secdesc(cli, fnum);
535
536         /* the logic here is rather more complex than I would like */
537         switch (mode) {
538         case ACL_DELETE:
539                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
540                         BOOL found = False;
541
542                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
543                                 if (sec_ace_equal(&sd->dacl->ace[i],
544                                                   &old->dacl->ace[j])) {
545                                         if (j != old->dacl->num_aces-1) {
546                                                 old->dacl->ace[j] = old->dacl->ace[j+1];
547                                         }
548                                         old->dacl->num_aces--;
549                                         if (old->dacl->num_aces == 0) {
550                                                 free(old->dacl->ace);
551                                                 old->dacl->ace=NULL;
552                                                 free(old->dacl);
553                                                 old->dacl = NULL;
554                                                 old->off_dacl = 0;
555                                         }
556                                         found = True;
557                                         break;
558                                 }
559                         }
560
561                         if (!found) {
562                                 fstring str;
563
564                                 SidToString(str, &sd->dacl->ace[i].sid);
565                                 printf("ACL for SID %s not found\n", str);
566                         }
567                 }
568                 break;
569
570         case ACL_MODIFY:
571                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
572                         BOOL found = False;
573
574                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
575                                 if (sid_equal(&sd->dacl->ace[i].sid,
576                                               &old->dacl->ace[j].sid)) {
577                                         old->dacl->ace[j] = sd->dacl->ace[i];
578                                         found = True;
579                                 }
580                         }
581
582                         if (!found) {
583                                 fstring str;
584
585                                 SidToString(str, &sd->dacl->ace[i].sid);
586                                 printf("ACL for SID %s not found\n", str);
587                         }
588                 }
589
590                 break;
591
592         case ACL_ADD:
593                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
594                         add_ace(&old->dacl, &sd->dacl->ace[i]);
595                 }
596                 break;
597
598         case ACL_SET:
599                 free_sec_desc(&old);
600                 old = sd;
601                 break;
602         }
603
604         if (sd != old) {
605                 free_sec_desc(&sd);
606         }
607
608         sd = make_sec_desc(old->revision, old->owner_sid, old->grp_sid, 
609                            NULL, old->dacl, &sd_size);
610
611         if (!cli_set_secdesc(cli, fnum, sd)) {
612                 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
613         }
614
615         free_sec_desc(&sd);
616         free_sec_desc(&old);
617
618         cli_close(cli, fnum);
619 }
620
621
622 /***************************************************** 
623 return a connection to a server
624 *******************************************************/
625 struct cli_state *connect_one(char *share)
626 {
627         struct cli_state *c;
628         struct nmb_name called, calling;
629         char *server_n;
630         struct in_addr ip;
631         extern struct in_addr ipzero;
632         extern pstring global_myname;
633
634         fstrcpy(server,share+2);
635         share = strchr(server,'\\');
636         if (!share) return NULL;
637         *share = 0;
638         share++;
639
640         server_n = server;
641         
642         ip = ipzero;
643
644         make_nmb_name(&calling, global_myname, 0x0);
645         make_nmb_name(&called , server, 0x20);
646
647  again:
648         ip = ipzero;
649
650         /* have to open a new connection */
651         if (!(c=cli_initialise(NULL)) || (cli_set_port(c, 139) == 0) ||
652             !cli_connect(c, server_n, &ip)) {
653                 DEBUG(0,("Connection to %s failed\n", server_n));
654                 cli_shutdown(c);
655                 safe_free(c);
656                 return NULL;
657         }
658
659         if (!cli_session_request(c, &calling, &called)) {
660                 DEBUG(0,("session request to %s failed\n", called.name));
661                 cli_shutdown(c);
662                 safe_free(c);
663                 if (strcmp(called.name, "*SMBSERVER")) {
664                         make_nmb_name(&called , "*SMBSERVER", 0x20);
665                         goto again;
666                 }
667                 return NULL;
668         }
669
670         DEBUG(4,(" session request ok\n"));
671
672         if (!cli_negprot(c)) {
673                 DEBUG(0,("protocol negotiation failed\n"));
674                 cli_shutdown(c);
675                 safe_free(c);
676                 return NULL;
677         }
678
679         if (!got_pass) {
680                 char *pass = getpass("Password: ");
681                 if (pass) {
682                         pstrcpy(password, pass);
683                 }
684         }
685
686         if (!cli_session_setup(c, username, 
687                                password, strlen(password),
688                                password, strlen(password),
689                                lp_workgroup())) {
690                 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
691                 cli_shutdown(c);
692                 safe_free(c);
693                 return NULL;
694         }
695
696         DEBUG(4,(" session setup ok\n"));
697
698         if (!cli_send_tconX(c, share, "?????",
699                             password, strlen(password)+1)) {
700                 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
701                 cli_shutdown(c);
702                 safe_free(c);
703                 return NULL;
704         }
705
706         DEBUG(4,(" tconx ok\n"));
707
708         return c;
709 }
710
711
712 static void usage(void)
713 {
714         printf(
715 "Usage: smbcacls //server1/share1 filename -U username [options]\n\
716 \n\
717 \t-D <acls>               delete an acl\n\
718 \t-M <acls>               modify an acl\n\
719 \t-A <acls>               add an acl\n\
720 \t-S <acls>               set acls\n\
721 \t-C username             change ownership of a file\n\
722 \t-G username             change group ownership of a file\n\
723 \t-n                      don't resolve sids or masks to names\n\
724 \t-h                      print help\n\
725 \n\
726 The username can be of the form username%%password or\n\
727 workgroup\\username%%password.\n\n\
728 An acl is of the form ACL:<SID>:type/flags/mask\n\
729 You can string acls together with spaces, commas or newlines\n\
730 ");
731 }
732
733 /****************************************************************************
734   main program
735 ****************************************************************************/
736  int main(int argc,char *argv[])
737 {
738         char *share;
739         char *filename;
740         extern char *optarg;
741         extern int optind;
742         extern FILE *dbf;
743         int opt;
744         char *p;
745         int seed;
746         static pstring servicesf = CONFIGFILE;
747         struct cli_state *cli;
748         enum acl_mode mode;
749         char *the_acl = NULL;
750         enum chown_mode change_mode = REQUEST_NONE;
751
752         setlinebuf(stdout);
753
754         dbf = stderr;
755
756         if (argc < 4 || argv[1][0] == '-') {
757                 usage();
758                 exit(1);
759         }
760
761         setup_logging(argv[0],True);
762
763         share = argv[1];
764         filename = argv[2];
765         all_string_sub(share,"/","\\",0);
766
767         argc -= 2;
768         argv += 2;
769
770         TimeInit();
771         charset_initialise();
772
773         lp_load(servicesf,True,False,False);
774         codepage_initialise(lp_client_code_page());
775         load_interfaces();
776
777         if (getenv("USER")) {
778                 pstrcpy(username,getenv("USER"));
779         }
780
781         seed = time(NULL);
782
783         while ((opt = getopt(argc, argv, "U:nhS:D:A:M:C:G:t")) != EOF) {
784                 switch (opt) {
785                 case 'U':
786                         pstrcpy(username,optarg);
787                         p = strchr(username,'%');
788                         if (p) {
789                                 *p = 0;
790                                 pstrcpy(password, p+1);
791                                 got_pass = 1;
792                         }
793                         break;
794
795                 case 'S':
796                         the_acl = optarg;
797                         mode = ACL_SET;
798                         break;
799
800                 case 'D':
801                         the_acl = optarg;
802                         mode = ACL_DELETE;
803                         break;
804
805                 case 'M':
806                         the_acl = optarg;
807                         mode = ACL_MODIFY;
808                         break;
809
810                 case 'A':
811                         the_acl = optarg;
812                         mode = ACL_ADD;
813                         break;
814
815                 case 'C':
816                         pstrcpy(owner_username,optarg);
817                         change_mode = REQUEST_CHOWN;
818                         break;
819
820                 case 'G':
821                         pstrcpy(owner_username,optarg);
822                         change_mode = REQUEST_CHGRP;
823                         break;
824
825                 case 'n':
826                         numeric = 1;
827                         break;
828
829                 case 't':
830                         test_args = 1;
831                         break;
832
833                 case 'h':
834                         usage();
835                         exit(1);
836
837                 default:
838                         printf("Unknown option %c (%d)\n", (char)opt, opt);
839                         exit(1);
840                 }
841         }
842
843         argc -= optind;
844         argv += optind;
845         
846         if (argc > 0) {
847                 usage();
848                 exit(1);
849         }
850
851         if (!test_args) {
852                 cli = connect_one(share);
853                 if (!cli) exit(1);
854         }
855
856         if (change_mode != REQUEST_NONE) {
857                 owner_set(cli, change_mode, filename, owner_username);
858         } else if (the_acl) {
859                 cacl_set(cli, filename, the_acl, mode);
860         } else {
861                 cacl_dump(cli, filename);
862         }
863
864         return(0);
865 }