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