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