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