51ae05b52ac7fe609f89389d7abd2de041f2b878
[samba.git] / source / 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 (cli_lsa_open_policy(&lsa_cli, lsa_cli.mem_ctx, True, 
96                                         GENERIC_EXECUTE_ACCESS, &pol)
97                     != NT_STATUS_NOPROBLEMO) {
98                         return False;
99                 }
100
101                 got_policy_hnd = True;
102         }
103         
104         return True;
105 }
106
107 /* convert a SID to a string, either numeric or username/group */
108 static void SidToString(fstring str, DOM_SID *sid)
109 {
110         char **names = NULL;
111         uint32 *types = NULL;
112         int num_names;
113
114         sid_to_string(str, sid);
115
116         if (numeric) return;
117
118         /* Ask LSA to convert the sid to a name */
119
120         if (!cacls_open_policy_hnd() ||
121             cli_lsa_lookup_sids(&lsa_cli, lsa_cli.mem_ctx,  &pol, 1, sid, &names, &types, 
122                                 &num_names) != NT_STATUS_NOPROBLEMO ||
123             !names || !names[0]) {
124                 return;
125         }
126
127         /* Converted OK */
128         
129         fstrcpy(str, names[0]);
130 }
131
132 /* convert a string to a SID, either numeric or username/group */
133 static BOOL StringToSid(DOM_SID *sid, char *str)
134 {
135         uint32 *types = NULL;
136         DOM_SID *sids = NULL;
137         int num_sids;
138         BOOL result = True;
139         
140         if (strncmp(str, "S-", 2) == 0) {
141                 return string_to_sid(sid, str);
142         }
143
144         if (!cacls_open_policy_hnd() ||
145             cli_lsa_lookup_names(&lsa_cli, lsa_cli.mem_ctx, &pol, 1, &str, &sids, &types, 
146                                  &num_sids) != NT_STATUS_NOPROBLEMO) {
147                 result = False;
148                 goto done;
149         }
150
151         sid_copy(sid, &sids[0]);
152
153  done:
154
155         return result;
156 }
157
158
159 /* print an ACE on a FILE, using either numeric or ascii representation */
160 static void print_ace(FILE *f, SEC_ACE *ace)
161 {
162         struct perm_value *v;
163         fstring sidstr;
164         int do_print = 0;
165         uint32 got_mask;
166
167         SidToString(sidstr, &ace->sid);
168
169         fprintf(f, "%s:", sidstr);
170
171         if (numeric) {
172                 fprintf(f, "%d/%d/0x%08x", 
173                         ace->type, ace->flags, ace->info.mask);
174                 return;
175         }
176
177         /* Ace type */
178
179         if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
180                 fprintf(f, "ALLOWED");
181         } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
182                 fprintf(f, "DENIED");
183         } else {
184                 fprintf(f, "%d", ace->type);
185         }
186
187         /* Not sure what flags can be set in a file ACL */
188
189         fprintf(f, "/%d/", ace->flags);
190
191         /* Standard permissions */
192
193         for (v = standard_values; v->perm; v++) {
194                 if (ace->info.mask == v->mask) {
195                         fprintf(f, "%s", v->perm);
196                         return;
197                 }
198         }
199
200         /* Special permissions.  Print out a hex value if we have
201            leftover bits in the mask. */
202
203         got_mask = ace->info.mask;
204
205  again:
206         for (v = special_values; v->perm; v++) {
207                 if ((ace->info.mask & v->mask) == v->mask) {
208                         if (do_print) {
209                                 fprintf(f, "%s", v->perm);
210                         }
211                         got_mask &= ~v->mask;
212                 }
213         }
214
215         if (!do_print) {
216                 if (got_mask != 0) {
217                         fprintf(f, "0x%08x", ace->info.mask);
218                 } else {
219                         do_print = 1;
220                         goto again;
221                 }
222         }
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(&p, tok, "/", sizeof(fstring)) &&
270               sscanf(tok, "%i", &aflags))) {
271                 return False;
272         }
273
274         if (!next_token(&p, 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(ctx, 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(ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
332         free(aces);
333         (*the_acl) = new;
334         return True;
335 }
336
337 /* parse a ascii version of a security descriptor */
338 static SEC_DESC *sec_desc_parse(char *str)
339 {
340         char *p = str;
341         fstring tok;
342         SEC_DESC *ret;
343         size_t sd_size;
344         DOM_SID *grp_sid=NULL, *owner_sid=NULL;
345         SEC_ACL *dacl=NULL;
346         int revision=1;
347
348         while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
349
350                 if (strncmp(tok,"REVISION:", 9) == 0) {
351                         revision = strtol(tok+9, NULL, 16);
352                         continue;
353                 }
354
355                 if (strncmp(tok,"OWNER:", 6) == 0) {
356                         owner_sid = (DOM_SID *)calloc(1, sizeof(DOM_SID));
357                         if (!owner_sid ||
358                             !StringToSid(owner_sid, tok+6)) {
359                                 printf("Failed to parse owner sid\n");
360                                 return NULL;
361                         }
362                         continue;
363                 }
364
365                 if (strncmp(tok,"GROUP:", 6) == 0) {
366                         grp_sid = (DOM_SID *)calloc(1, sizeof(DOM_SID));
367                         if (!grp_sid ||
368                             !StringToSid(grp_sid, tok+6)) {
369                                 printf("Failed to parse group sid\n");
370                                 return NULL;
371                         }
372                         continue;
373                 }
374
375                 if (strncmp(tok,"ACL:", 4) == 0) {
376                         SEC_ACE ace;
377                         if (!parse_ace(&ace, tok+4)) {
378                                 printf("Failed to parse ACL %s\n", tok);
379                                 return NULL;
380                         }
381                         if(!add_ace(&dacl, &ace)) {
382                                 printf("Failed to add ACL %s\n", tok);
383                                 return NULL;
384                         }
385                         continue;
386                 }
387
388                 printf("Failed to parse security descriptor\n");
389                 return NULL;
390         }
391
392         ret = make_sec_desc(ctx,revision, owner_sid, grp_sid, 
393                             NULL, dacl, &sd_size);
394
395         if (grp_sid) free(grp_sid);
396         if (owner_sid) free(owner_sid);
397
398         return ret;
399 }
400
401
402 /* print a ascii version of a security descriptor on a FILE handle */
403 static void sec_desc_print(FILE *f, SEC_DESC *sd)
404 {
405         fstring sidstr;
406         int i;
407
408         printf("REVISION:%d\n", sd->revision);
409
410         /* Print owner and group sid */
411
412         if (sd->owner_sid) {
413                 SidToString(sidstr, sd->owner_sid);
414         } else {
415                 fstrcpy(sidstr, "");
416         }
417
418         printf("OWNER:%s\n", sidstr);
419
420         if (sd->grp_sid) {
421                 SidToString(sidstr, sd->grp_sid);
422         } else {
423                 fstrcpy(sidstr, "");
424         }
425
426         fprintf(f, "GROUP:%s\n", sidstr);
427
428         /* Print aces */
429         for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
430                 SEC_ACE *ace = &sd->dacl->ace[i];
431                 fprintf(f, "ACL:");
432                 print_ace(f, ace);
433                 fprintf(f, "\n");
434         }
435
436 }
437
438 /***************************************************** 
439 dump the acls for a file
440 *******************************************************/
441 static int cacl_dump(struct cli_state *cli, char *filename)
442 {
443         int fnum;
444         SEC_DESC *sd;
445
446         if (test_args) return EXIT_OK;
447
448         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
449         if (fnum == -1) {
450                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
451                 return EXIT_FAILED;
452         }
453
454         sd = cli_query_secdesc(cli, fnum, ctx);
455
456         if (!sd) {
457                 printf("ERROR: secdesc query failed: %s\n", cli_errstr(cli));
458                 return EXIT_FAILED;
459         }
460
461         sec_desc_print(stdout, sd);
462
463         cli_close(cli, fnum);
464
465         return EXIT_OK;
466 }
467
468 /***************************************************** 
469 Change the ownership or group ownership of a file. Just
470 because the NT docs say this can't be done :-). JRA.
471 *******************************************************/
472
473 static int owner_set(struct cli_state *cli, enum chown_mode change_mode, 
474                      char *filename, char *new_username)
475 {
476         int fnum;
477         DOM_SID sid;
478         SEC_DESC *sd, *old;
479         size_t sd_size;
480
481         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
482
483         if (fnum == -1) {
484                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
485                 return EXIT_FAILED;
486         }
487
488         if (!StringToSid(&sid, new_username))
489                 return EXIT_PARSE_ERROR;
490
491         old = cli_query_secdesc(cli, fnum, ctx);
492
493         cli_close(cli, fnum);
494
495         if (!old) {
496                 printf("owner_set: Failed to query old descriptor\n");
497                 return EXIT_FAILED;
498         }
499
500         sd = make_sec_desc(ctx,old->revision,
501                                 (change_mode == REQUEST_CHOWN) ? &sid : old->owner_sid,
502                                 (change_mode == REQUEST_CHGRP) ? &sid : old->grp_sid,
503                            NULL, old->dacl, &sd_size);
504
505         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
506
507         if (fnum == -1) {
508                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
509                 return EXIT_FAILED;
510         }
511
512         if (!cli_set_secdesc(cli, fnum, sd)) {
513                 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
514         }
515
516         cli_close(cli, fnum);
517
518         return EXIT_OK;
519 }
520
521
522 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
523    However NT4 gives a "The information may have been modified by a
524    computer running Windows NT 5.0" if denied ACEs do not appear before
525    allowed ACEs. */
526
527 static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
528 {
529         if (sec_ace_equal(ace1, ace2)) return 0;
530         if (ace1->type != ace2->type) return ace2->type - ace1->type;
531         if (sid_compare(&ace1->sid, &ace2->sid)) return sid_compare(&ace1->sid, &ace2->sid);
532         if (ace1->flags != ace2->flags) return ace1->flags - ace2->flags;
533         if (ace1->info.mask != ace2->info.mask) return ace1->info.mask - ace2->info.mask;
534         if (ace1->size != ace2->size) return ace1->size - ace2->size;
535         return memcmp(ace1, ace2, sizeof(SEC_ACE));
536 }
537
538 static void sort_acl(SEC_ACL *the_acl)
539 {
540         int i;
541         if (!the_acl) return;
542
543         qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]), QSORT_CAST ace_compare);
544
545         for (i=1;i<the_acl->num_aces;) {
546                 if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
547                         int j;
548                         for (j=i; j<the_acl->num_aces-1; j++) {
549                                 the_acl->ace[j] = the_acl->ace[j+1];
550                         }
551                         the_acl->num_aces--;
552                 } else {
553                         i++;
554                 }
555         }
556 }
557
558 /***************************************************** 
559 set the ACLs on a file given an ascii description
560 *******************************************************/
561 static int cacl_set(struct cli_state *cli, char *filename, 
562                     char *the_acl, enum acl_mode mode)
563 {
564         int fnum;
565         SEC_DESC *sd, *old;
566         int i, j;
567         size_t sd_size;
568         int result = EXIT_OK;
569
570         sd = sec_desc_parse(the_acl);
571
572         if (!sd) return EXIT_PARSE_ERROR;
573         if (test_args) return EXIT_OK;
574
575         /* The desired access below is the only one I could find that works
576            with NT4, W2KP and Samba */
577
578         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
579
580         if (fnum == -1) {
581                 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
582                 return EXIT_FAILED;
583         }
584
585         old = cli_query_secdesc(cli, fnum, ctx);
586
587         if (!old) {
588                 printf("calc_set: Failed to query old descriptor\n");
589                 return EXIT_FAILED;
590         }
591
592         cli_close(cli, fnum);
593
594         /* the logic here is rather more complex than I would like */
595         switch (mode) {
596         case SMB_ACL_DELETE:
597                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
598                         BOOL found = False;
599
600                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
601                                 if (sec_ace_equal(&sd->dacl->ace[i],
602                                                   &old->dacl->ace[j])) {
603                                         int k;
604                                         for (k=j; k<old->dacl->num_aces-1;k++) {
605                                                 old->dacl->ace[k] = old->dacl->ace[k+1];
606                                         }
607                                         old->dacl->num_aces--;
608                                         if (old->dacl->num_aces == 0) {
609                                                 free(old->dacl->ace);
610                                                 old->dacl->ace=NULL;
611                                                 free(old->dacl);
612                                                 old->dacl = NULL;
613                                                 old->off_dacl = 0;
614                                         }
615                                         found = True;
616                                         break;
617                                 }
618                         }
619
620                         if (!found) {
621                                 printf("ACL for ACE:"); 
622                                 print_ace(stdout, &sd->dacl->ace[i]);
623                                 printf(" not found\n");
624                         }
625                 }
626                 break;
627
628         case SMB_ACL_MODIFY:
629                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
630                         BOOL found = False;
631
632                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
633                                 if (sid_equal(&sd->dacl->ace[i].sid,
634                                               &old->dacl->ace[j].sid)) {
635                                         old->dacl->ace[j] = sd->dacl->ace[i];
636                                         found = True;
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
648                 break;
649
650         case SMB_ACL_ADD:
651                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
652                         add_ace(&old->dacl, &sd->dacl->ace[i]);
653                 }
654                 break;
655
656         case SMB_ACL_SET:
657                 old = sd;
658                 break;
659         }
660
661         /* Denied ACE entries must come before allowed ones */
662         sort_acl(old->dacl);
663
664         /* Create new security descriptor and set it */
665         sd = make_sec_desc(ctx,old->revision, old->owner_sid, old->grp_sid, 
666                            NULL, old->dacl, &sd_size);
667
668         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
669
670         if (fnum == -1) {
671                 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
672                 return EXIT_FAILED;
673         }
674
675         if (!cli_set_secdesc(cli, fnum, sd)) {
676                 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
677                 result = EXIT_FAILED;
678         }
679
680         /* Clean up */
681
682         cli_close(cli, fnum);
683
684         return result;
685 }
686
687
688 /***************************************************** 
689 return a connection to a server
690 *******************************************************/
691 struct cli_state *connect_one(char *share)
692 {
693         struct cli_state *c;
694         struct nmb_name called, calling;
695         struct in_addr ip;
696         extern struct in_addr ipzero;
697         extern pstring global_myname;
698
699         fstrcpy(server,share+2);
700         share = strchr(server,'\\');
701         if (!share) return NULL;
702         *share = 0;
703         share++;
704
705         ip = ipzero;
706
707         make_nmb_name(&calling, global_myname, 0x0);
708         make_nmb_name(&called , server, 0x20);
709
710  again:
711         ip = ipzero;
712
713         /* have to open a new connection */
714         if (!(c=cli_initialise(NULL)) || (cli_set_port(c, 139) == 0) ||
715             !cli_connect(c, server, &ip)) {
716                 DEBUG(0,("Connection to %s failed\n", server));
717                 cli_shutdown(c);
718                 safe_free(c);
719                 return NULL;
720         }
721
722         if (!cli_session_request(c, &calling, &called)) {
723                 DEBUG(0,("session request to %s failed\n", called.name));
724                 cli_shutdown(c);
725                 safe_free(c);
726                 if (strcmp(called.name, "*SMBSERVER")) {
727                         make_nmb_name(&called , "*SMBSERVER", 0x20);
728                         goto again;
729                 }
730                 return NULL;
731         }
732
733         DEBUG(4,(" session request ok\n"));
734
735         if (!cli_negprot(c)) {
736                 DEBUG(0,("protocol negotiation failed\n"));
737                 cli_shutdown(c);
738                 safe_free(c);
739                 return NULL;
740         }
741
742         if (!got_pass) {
743                 char *pass = getpass("Password: ");
744                 if (pass) {
745                         pstrcpy(password, pass);
746                 }
747         }
748
749         if (!cli_session_setup(c, username, 
750                                password, strlen(password),
751                                password, strlen(password),
752                                lp_workgroup())) {
753                 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
754                 cli_shutdown(c);
755                 safe_free(c);
756                 return NULL;
757         }
758
759         DEBUG(4,(" session setup ok\n"));
760
761         if (!cli_send_tconX(c, share, "?????",
762                             password, strlen(password)+1)) {
763                 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
764                 cli_shutdown(c);
765                 safe_free(c);
766                 return NULL;
767         }
768
769         DEBUG(4,(" tconx ok\n"));
770
771         return c;
772 }
773
774
775 static void usage(void)
776 {
777         printf(
778 "Usage: smbcacls //server1/share1 filename [options]\n\
779 \n\
780 \t-D <acls>               delete an acl\n\
781 \t-M <acls>               modify an acl\n\
782 \t-A <acls>               add an acl\n\
783 \t-S <acls>               set acls\n\
784 \t-C username             change ownership of a file\n\
785 \t-G username             change group ownership of a file\n\
786 \t-n                      don't resolve sids or masks to names\n\
787 \t-h                      print help\n\
788 \t-d debuglevel           set debug output level\n\
789 \t-U username             user to autheticate as\n\
790 \n\
791 The username can be of the form username%%password or\n\
792 workgroup\\username%%password.\n\n\
793 An acl is of the form ACL:<SID>:type/flags/mask\n\
794 You can string acls together with spaces, commas or newlines\n\
795 ");
796 }
797
798 /****************************************************************************
799   main program
800 ****************************************************************************/
801  int main(int argc,char *argv[])
802 {
803         char *share;
804         pstring filename;
805         extern char *optarg;
806         extern int optind;
807         extern FILE *dbf;
808         int opt;
809         char *p;
810         static pstring servicesf = CONFIGFILE;
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 = 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         TimeInit();
839         charset_initialise();
840
841         lp_load(servicesf,True,False,False);
842         codepage_initialise(lp_client_code_page());
843         load_interfaces();
844
845         if (getenv("USER")) {
846                 pstrcpy(username,getenv("USER"));
847
848                 if ((p=strchr(username,'%'))) {
849                         *p = 0;
850                         pstrcpy(password,p+1);
851                         got_pass = True;
852                         memset(strchr(getenv("USER"), '%') + 1, 'X',
853                                strlen(password));
854                 }
855         }
856
857         while ((opt = getopt(argc, argv, "U:nhS:D:A:M:C:G:td:")) != EOF) {
858                 switch (opt) {
859                 case 'U':
860                         pstrcpy(username,optarg);
861                         p = strchr(username,'%');
862                         if (p) {
863                                 *p = 0;
864                                 pstrcpy(password, p+1);
865                                 got_pass = 1;
866                         }
867                         break;
868
869                 case 'S':
870                         the_acl = optarg;
871                         mode = SMB_ACL_SET;
872                         break;
873
874                 case 'D':
875                         the_acl = optarg;
876                         mode = SMB_ACL_DELETE;
877                         break;
878
879                 case 'M':
880                         the_acl = optarg;
881                         mode = SMB_ACL_MODIFY;
882                         break;
883
884                 case 'A':
885                         the_acl = optarg;
886                         mode = SMB_ACL_ADD;
887                         break;
888
889                 case 'C':
890                         pstrcpy(owner_username,optarg);
891                         change_mode = REQUEST_CHOWN;
892                         break;
893
894                 case 'G':
895                         pstrcpy(owner_username,optarg);
896                         change_mode = REQUEST_CHGRP;
897                         break;
898
899                 case 'n':
900                         numeric = 1;
901                         break;
902
903                 case 't':
904                         test_args = 1;
905                         break;
906
907                 case 'h':
908                         usage();
909                         talloc_destroy(ctx);
910                         exit(EXIT_PARSE_ERROR);
911
912                 case 'd':
913                         DEBUGLEVEL = atoi(optarg);
914                         break;
915
916                 default:
917                         printf("Unknown option %c (%d)\n", (char)opt, opt);
918                         talloc_destroy(ctx);
919                         exit(EXIT_PARSE_ERROR);
920                 }
921         }
922
923         argc -= optind;
924         argv += optind;
925         
926         if (argc > 0) {
927                 usage();
928                 talloc_destroy(ctx);
929                 exit(EXIT_PARSE_ERROR);
930         }
931
932         /* Make connection to server */
933
934         if (!test_args) {
935                 cli = connect_one(share);
936                 if (!cli) {
937                         talloc_destroy(ctx);
938                         exit(EXIT_FAILED);
939                 }
940         }
941
942         all_string_sub(filename, "/", "\\", 0);
943         if (filename[0] != '\\') {
944                 pstring s;
945                 s[0] = '\\';
946                 safe_strcpy(&s[1], filename, sizeof(pstring)-1);
947                 pstrcpy(filename, s);
948         }
949
950         /* Perform requested action */
951
952         if (change_mode != REQUEST_NONE) {
953                 result = owner_set(cli, change_mode, filename, owner_username);
954         } else if (the_acl) {
955                 result = cacl_set(cli, filename, the_acl, mode);
956         } else {
957                 result = cacl_dump(cli, filename);
958         }
959
960         talloc_destroy(ctx);
961
962         return result;
963 }