This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[ab/samba.git/.git] / source3 / utils / smbcacls.c
1 /* 
2    Unix SMB/CIFS implementation.
3    ACL get/set utility
4    
5    Copyright (C) Andrew Tridgell 2000
6    Copyright (C) Tim Potter      2000
7    Copyright (C) Jeremy Allison  2000
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 static fstring password;
27 static pstring username;
28 static pstring owner_username;
29 static fstring server;
30 static int got_pass;
31 static int test_args;
32 TALLOC_CTX *ctx;
33
34 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
35 #define CREATE_ACCESS_WRITE (WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS)
36
37 /* numeric is set when the user wants numeric SIDs and ACEs rather
38    than going via LSA calls to resolve them */
39 static int numeric;
40
41 enum acl_mode {SMB_ACL_SET, SMB_ACL_DELETE, SMB_ACL_MODIFY, SMB_ACL_ADD };
42 enum chown_mode {REQUEST_NONE, REQUEST_CHOWN, REQUEST_CHGRP};
43 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
44
45 struct perm_value {
46         char *perm;
47         uint32 mask;
48 };
49
50 /* These values discovered by inspection */
51
52 static struct perm_value special_values[] = {
53         { "R", 0x00120089 },
54         { "W", 0x00120116 },
55         { "X", 0x001200a0 },
56         { "D", 0x00010000 },
57         { "P", 0x00040000 },
58         { "O", 0x00080000 },
59         { NULL, 0 },
60 };
61
62 static struct perm_value standard_values[] = {
63         { "READ",   0x001200a9 },
64         { "CHANGE", 0x001301bf },
65         { "FULL",   0x001f01ff },
66         { NULL, 0 },
67 };
68
69 struct cli_state lsa_cli;
70 POLICY_HND pol;
71 struct ntuser_creds creds;
72 BOOL got_policy_hnd;
73
74 /* Open cli connection and policy handle */
75
76 static BOOL cacls_open_policy_hnd(void)
77 {
78         creds.pwd.null_pwd = 1;
79
80         /* Initialise cli LSA connection */
81
82         if (!lsa_cli.initialised && 
83             !cli_lsa_initialise(&lsa_cli, server, &creds)) {
84                 return False;
85         }
86
87         /* Open policy handle */
88
89         if (!got_policy_hnd) {
90
91                 /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
92                    but NT sends 0x2000000 so we might as well do it too. */
93
94                 if (!NT_STATUS_IS_OK(cli_lsa_open_policy(&lsa_cli, lsa_cli.mem_ctx, True, 
95                                                          GENERIC_EXECUTE_ACCESS, &pol))) {
96                         return False;
97                 }
98
99                 got_policy_hnd = True;
100         }
101         
102         return True;
103 }
104
105 /* convert a SID to a string, either numeric or username/group */
106 static void SidToString(fstring str, DOM_SID *sid)
107 {
108         char **domains = NULL;
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,  
121                                                  &pol, 1, sid, &domains, &names, 
122                                                  &types, &num_names)) ||
123             !domains || !domains[0] || !names || !names[0]) {
124                 return;
125         }
126
127         /* Converted OK */
128
129         slprintf(str, sizeof(fstring) - 1, "%s%s%s",
130                  domains[0], lp_winbind_separator(),
131                  names[0]);
132         
133 }
134
135 /* convert a string to a SID, either numeric or username/group */
136 static BOOL StringToSid(DOM_SID *sid, const char *str)
137 {
138         uint32 *types = NULL;
139         DOM_SID *sids = NULL;
140         int num_sids;
141         BOOL result = True;
142
143         if (strncmp(str, "S-", 2) == 0) {
144                 return string_to_sid(sid, str);
145         }
146
147         if (!cacls_open_policy_hnd() ||
148             !NT_STATUS_IS_OK(cli_lsa_lookup_names(&lsa_cli, lsa_cli.mem_ctx, &pol, 1, 
149                                                   &str, 
150                                                   &sids, &types, &num_sids))) {
151                 result = False;
152                 goto done;
153         }
154
155         sid_copy(sid, &sids[0]);
156
157  done:
158
159         return result;
160 }
161
162
163 /* print an ACE on a FILE, using either numeric or ascii representation */
164 static void print_ace(FILE *f, SEC_ACE *ace)
165 {
166         struct perm_value *v;
167         fstring sidstr;
168         int do_print = 0;
169         uint32 got_mask;
170
171         SidToString(sidstr, &ace->trustee);
172
173         fprintf(f, "%s:", sidstr);
174
175         if (numeric) {
176                 fprintf(f, "%d/%d/0x%08x", 
177                         ace->type, ace->flags, ace->info.mask);
178                 return;
179         }
180
181         /* Ace type */
182
183         if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
184                 fprintf(f, "ALLOWED");
185         } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
186                 fprintf(f, "DENIED");
187         } else {
188                 fprintf(f, "%d", ace->type);
189         }
190
191         /* Not sure what flags can be set in a file ACL */
192
193         fprintf(f, "/%d/", ace->flags);
194
195         /* Standard permissions */
196
197         for (v = standard_values; v->perm; v++) {
198                 if (ace->info.mask == v->mask) {
199                         fprintf(f, "%s", v->perm);
200                         return;
201                 }
202         }
203
204         /* Special permissions.  Print out a hex value if we have
205            leftover bits in the mask. */
206
207         got_mask = ace->info.mask;
208
209  again:
210         for (v = special_values; v->perm; v++) {
211                 if ((ace->info.mask & v->mask) == v->mask) {
212                         if (do_print) {
213                                 fprintf(f, "%s", v->perm);
214                         }
215                         got_mask &= ~v->mask;
216                 }
217         }
218
219         if (!do_print) {
220                 if (got_mask != 0) {
221                         fprintf(f, "0x%08x", ace->info.mask);
222                 } else {
223                         do_print = 1;
224                         goto again;
225                 }
226         }
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_m(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(&p, tok, "/", sizeof(fstring)) &&
274               sscanf(tok, "%i", &aflags))) {
275                 return False;
276         }
277
278         if (!next_token(&p, 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(ctx, 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(ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
336         SAFE_FREE(aces);
337         (*the_acl) = new;
338         return True;
339 }
340
341 /* parse a ascii version of a security descriptor */
342 static SEC_DESC *sec_desc_parse(char *str)
343 {
344         char *p = str;
345         fstring tok;
346         SEC_DESC *ret;
347         size_t sd_size;
348         DOM_SID *grp_sid=NULL, *owner_sid=NULL;
349         SEC_ACL *dacl=NULL;
350         int revision=1;
351
352         while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
353
354                 if (strncmp(tok,"REVISION:", 9) == 0) {
355                         revision = strtol(tok+9, NULL, 16);
356                         continue;
357                 }
358
359                 if (strncmp(tok,"OWNER:", 6) == 0) {
360                         owner_sid = (DOM_SID *)calloc(1, sizeof(DOM_SID));
361                         if (!owner_sid ||
362                             !StringToSid(owner_sid, tok+6)) {
363                                 printf("Failed to parse owner sid\n");
364                                 return NULL;
365                         }
366                         continue;
367                 }
368
369                 if (strncmp(tok,"GROUP:", 6) == 0) {
370                         grp_sid = (DOM_SID *)calloc(1, sizeof(DOM_SID));
371                         if (!grp_sid ||
372                             !StringToSid(grp_sid, tok+6)) {
373                                 printf("Failed to parse group sid\n");
374                                 return NULL;
375                         }
376                         continue;
377                 }
378
379                 if (strncmp(tok,"ACL:", 4) == 0) {
380                         SEC_ACE ace;
381                         if (!parse_ace(&ace, tok+4)) {
382                                 printf("Failed to parse ACL %s\n", tok);
383                                 return NULL;
384                         }
385                         if(!add_ace(&dacl, &ace)) {
386                                 printf("Failed to add ACL %s\n", tok);
387                                 return NULL;
388                         }
389                         continue;
390                 }
391
392                 printf("Failed to parse security descriptor\n");
393                 return NULL;
394         }
395
396         ret = make_sec_desc(ctx,revision, owner_sid, grp_sid, 
397                             NULL, dacl, &sd_size);
398
399         SAFE_FREE(grp_sid);
400         SAFE_FREE(owner_sid);
401
402         return ret;
403 }
404
405
406 /* print a ascii version of a security descriptor on a FILE handle */
407 static void sec_desc_print(FILE *f, SEC_DESC *sd)
408 {
409         fstring sidstr;
410         uint32 i;
411
412         printf("REVISION:%d\n", sd->revision);
413
414         /* Print owner and group sid */
415
416         if (sd->owner_sid) {
417                 SidToString(sidstr, sd->owner_sid);
418         } else {
419                 fstrcpy(sidstr, "");
420         }
421
422         printf("OWNER:%s\n", sidstr);
423
424         if (sd->grp_sid) {
425                 SidToString(sidstr, sd->grp_sid);
426         } else {
427                 fstrcpy(sidstr, "");
428         }
429
430         fprintf(f, "GROUP:%s\n", sidstr);
431
432         /* Print aces */
433         for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
434                 SEC_ACE *ace = &sd->dacl->ace[i];
435                 fprintf(f, "ACL:");
436                 print_ace(f, ace);
437                 fprintf(f, "\n");
438         }
439
440 }
441
442 /***************************************************** 
443 dump the acls for a file
444 *******************************************************/
445 static int cacl_dump(struct cli_state *cli, char *filename)
446 {
447         int fnum;
448         SEC_DESC *sd;
449
450         if (test_args) return EXIT_OK;
451
452         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
453         if (fnum == -1) {
454                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
455                 return EXIT_FAILED;
456         }
457
458         sd = cli_query_secdesc(cli, fnum, ctx);
459
460         if (!sd) {
461                 printf("ERROR: secdesc query failed: %s\n", cli_errstr(cli));
462                 return EXIT_FAILED;
463         }
464
465         sec_desc_print(stdout, sd);
466
467         cli_close(cli, fnum);
468
469         return EXIT_OK;
470 }
471
472 /***************************************************** 
473 Change the ownership or group ownership of a file. Just
474 because the NT docs say this can't be done :-). JRA.
475 *******************************************************/
476
477 static int owner_set(struct cli_state *cli, enum chown_mode change_mode, 
478                      char *filename, char *new_username)
479 {
480         int fnum;
481         DOM_SID sid;
482         SEC_DESC *sd, *old;
483         size_t sd_size;
484
485         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
486
487         if (fnum == -1) {
488                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
489                 return EXIT_FAILED;
490         }
491
492         if (!StringToSid(&sid, new_username))
493                 return EXIT_PARSE_ERROR;
494
495         old = cli_query_secdesc(cli, fnum, ctx);
496
497         cli_close(cli, fnum);
498
499         if (!old) {
500                 printf("owner_set: Failed to query old descriptor\n");
501                 return EXIT_FAILED;
502         }
503
504         sd = make_sec_desc(ctx,old->revision,
505                                 (change_mode == REQUEST_CHOWN) ? &sid : old->owner_sid,
506                                 (change_mode == REQUEST_CHGRP) ? &sid : old->grp_sid,
507                            NULL, old->dacl, &sd_size);
508
509         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
510
511         if (fnum == -1) {
512                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
513                 return EXIT_FAILED;
514         }
515
516         if (!cli_set_secdesc(cli, fnum, sd)) {
517                 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
518         }
519
520         cli_close(cli, fnum);
521
522         return EXIT_OK;
523 }
524
525
526 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
527    However NT4 gives a "The information may have been modified by a
528    computer running Windows NT 5.0" if denied ACEs do not appear before
529    allowed ACEs. */
530
531 static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
532 {
533         if (sec_ace_equal(ace1, ace2)) 
534                 return 0;
535
536         if (ace1->type != ace2->type) 
537                 return ace2->type - ace1->type;
538
539         if (sid_compare(&ace1->trustee, &ace2->trustee)) 
540                 return sid_compare(&ace1->trustee, &ace2->trustee);
541
542         if (ace1->flags != ace2->flags) 
543                 return ace1->flags - ace2->flags;
544
545         if (ace1->info.mask != ace2->info.mask) 
546                 return ace1->info.mask - ace2->info.mask;
547
548         if (ace1->size != ace2->size) 
549                 return ace1->size - ace2->size;
550
551         return memcmp(ace1, ace2, sizeof(SEC_ACE));
552 }
553
554 static void sort_acl(SEC_ACL *the_acl)
555 {
556         uint32 i;
557         if (!the_acl) return;
558
559         qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]), QSORT_CAST ace_compare);
560
561         for (i=1;i<the_acl->num_aces;) {
562                 if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
563                         int j;
564                         for (j=i; j<the_acl->num_aces-1; j++) {
565                                 the_acl->ace[j] = the_acl->ace[j+1];
566                         }
567                         the_acl->num_aces--;
568                 } else {
569                         i++;
570                 }
571         }
572 }
573
574 /***************************************************** 
575 set the ACLs on a file given an ascii description
576 *******************************************************/
577 static int cacl_set(struct cli_state *cli, char *filename, 
578                     char *the_acl, enum acl_mode mode)
579 {
580         int fnum;
581         SEC_DESC *sd, *old;
582         uint32 i, j;
583         size_t sd_size;
584         int result = EXIT_OK;
585
586         sd = sec_desc_parse(the_acl);
587
588         if (!sd) return EXIT_PARSE_ERROR;
589         if (test_args) return EXIT_OK;
590
591         /* The desired access below is the only one I could find that works
592            with NT4, W2KP and Samba */
593
594         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
595
596         if (fnum == -1) {
597                 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
598                 return EXIT_FAILED;
599         }
600
601         old = cli_query_secdesc(cli, fnum, ctx);
602
603         if (!old) {
604                 printf("calc_set: Failed to query old descriptor\n");
605                 return EXIT_FAILED;
606         }
607
608         cli_close(cli, fnum);
609
610         /* the logic here is rather more complex than I would like */
611         switch (mode) {
612         case SMB_ACL_DELETE:
613                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
614                         BOOL found = False;
615
616                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
617                                 if (sec_ace_equal(&sd->dacl->ace[i],
618                                                   &old->dacl->ace[j])) {
619                                         uint32 k;
620                                         for (k=j; k<old->dacl->num_aces-1;k++) {
621                                                 old->dacl->ace[k] = old->dacl->ace[k+1];
622                                         }
623                                         old->dacl->num_aces--;
624                                         if (old->dacl->num_aces == 0) {
625                                                 SAFE_FREE(old->dacl->ace);
626                                                 SAFE_FREE(old->dacl);
627                                                 old->off_dacl = 0;
628                                         }
629                                         found = True;
630                                         break;
631                                 }
632                         }
633
634                         if (!found) {
635                                 printf("ACL for ACE:"); 
636                                 print_ace(stdout, &sd->dacl->ace[i]);
637                                 printf(" not found\n");
638                         }
639                 }
640                 break;
641
642         case SMB_ACL_MODIFY:
643                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
644                         BOOL found = False;
645
646                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
647                                 if (sid_equal(&sd->dacl->ace[i].trustee,
648                                               &old->dacl->ace[j].trustee)) {
649                                         old->dacl->ace[j] = sd->dacl->ace[i];
650                                         found = True;
651                                 }
652                         }
653
654                         if (!found) {
655                                 fstring str;
656
657                                 SidToString(str, &sd->dacl->ace[i].trustee);
658                                 printf("ACL for SID %s not found\n", str);
659                         }
660                 }
661
662                 break;
663
664         case SMB_ACL_ADD:
665                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
666                         add_ace(&old->dacl, &sd->dacl->ace[i]);
667                 }
668                 break;
669
670         case SMB_ACL_SET:
671                 old = sd;
672                 break;
673         }
674
675         /* Denied ACE entries must come before allowed ones */
676         sort_acl(old->dacl);
677
678         /* Create new security descriptor and set it */
679         sd = make_sec_desc(ctx,old->revision, old->owner_sid, old->grp_sid, 
680                            NULL, old->dacl, &sd_size);
681
682         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
683
684         if (fnum == -1) {
685                 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
686                 return EXIT_FAILED;
687         }
688
689         if (!cli_set_secdesc(cli, fnum, sd)) {
690                 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
691                 result = EXIT_FAILED;
692         }
693
694         /* Clean up */
695
696         cli_close(cli, fnum);
697
698         return result;
699 }
700
701
702 /***************************************************** 
703 return a connection to a server
704 *******************************************************/
705 struct cli_state *connect_one(char *share)
706 {
707         struct cli_state *c;
708         struct nmb_name called, calling;
709         struct in_addr ip;
710         extern pstring global_myname;
711
712         fstrcpy(server,share+2);
713         share = strchr_m(server,'\\');
714         if (!share) return NULL;
715         *share = 0;
716         share++;
717
718         zero_ip(&ip);
719
720         make_nmb_name(&calling, global_myname, 0x0);
721         make_nmb_name(&called , server, 0x20);
722
723  again:
724         zero_ip(&ip);
725
726         /* have to open a new connection */
727         if (!(c=cli_initialise(NULL)) || !cli_connect(c, server, &ip)) {
728                 DEBUG(0,("Connection to %s failed\n", server));
729                 cli_shutdown(c);
730                 return NULL;
731         }
732
733         if (!cli_session_request(c, &calling, &called)) {
734                 DEBUG(0,("session request to %s failed\n", called.name));
735                 cli_shutdown(c);
736                 if (strcmp(called.name, "*SMBSERVER")) {
737                         make_nmb_name(&called , "*SMBSERVER", 0x20);
738                         goto again;
739                 }
740                 return NULL;
741         }
742
743         DEBUG(4,(" session request ok\n"));
744
745         if (!cli_negprot(c)) {
746                 DEBUG(0,("protocol negotiation failed\n"));
747                 cli_shutdown(c);
748                 return NULL;
749         }
750
751         if (!got_pass) {
752                 char *pass = getpass("Password: ");
753                 if (pass) {
754                         pstrcpy(password, pass);
755                 }
756         }
757
758         if (!cli_session_setup(c, username, 
759                                password, strlen(password),
760                                password, strlen(password),
761                                lp_workgroup())) {
762                 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
763                 cli_shutdown(c);
764                 return NULL;
765         }
766
767         DEBUG(4,(" session setup ok\n"));
768
769         if (!cli_send_tconX(c, share, "?????",
770                             password, strlen(password)+1)) {
771                 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
772                 cli_shutdown(c);
773                 return NULL;
774         }
775
776         DEBUG(4,(" tconx ok\n"));
777
778         return c;
779 }
780
781
782 static void usage(void)
783 {
784         printf(
785 "Usage: smbcacls //server1/share1 filename [options]\n\
786 \n\
787 \t-D <acls>               delete an acl\n\
788 \t-M <acls>               modify an acl\n\
789 \t-A <acls>               add an acl\n\
790 \t-S <acls>               set acls\n\
791 \t-C username             change ownership of a file\n\
792 \t-G username             change group ownership of a file\n\
793 \t-n                      don't resolve sids or masks to names\n\
794 \t-h                      print help\n\
795 \t-d debuglevel           set debug output level\n\
796 \t-U username             user to autheticate as\n\
797 \n\
798 The username can be of the form username%%password or\n\
799 workgroup\\username%%password.\n\n\
800 An acl is of the form ACL:<SID>:type/flags/mask\n\
801 You can string acls together with spaces, commas or newlines\n\
802 ");
803 }
804
805 /****************************************************************************
806   main program
807 ****************************************************************************/
808  int main(int argc,char *argv[])
809 {
810         char *share;
811         pstring filename;
812         extern char *optarg;
813         extern int optind;
814         int opt;
815         char *p;
816         struct cli_state *cli=NULL;
817         enum acl_mode mode = SMB_ACL_SET;
818         char *the_acl = NULL;
819         enum chown_mode change_mode = REQUEST_NONE;
820         int result;
821
822         ctx=talloc_init();
823
824         setlinebuf(stdout);
825
826         dbf = x_stderr;
827
828         if (argc < 3 || argv[1][0] == '-') {
829                 usage();
830                 talloc_destroy(ctx);
831                 exit(EXIT_PARSE_ERROR);
832         }
833
834         setup_logging(argv[0],True);
835
836         share = argv[1];
837         pstrcpy(filename, argv[2]);
838         all_string_sub(share,"/","\\",0);
839
840         argc -= 2;
841         argv += 2;
842
843         lp_load(dyn_CONFIGFILE,True,False,False);
844         load_interfaces();
845
846         if (getenv("USER")) {
847                 pstrcpy(username,getenv("USER"));
848
849                 if ((p=strchr_m(username,'%'))) {
850                         *p = 0;
851                         pstrcpy(password,p+1);
852                         got_pass = True;
853                         memset(strchr_m(getenv("USER"), '%') + 1, 'X',
854                                strlen(password));
855                 }
856         }
857
858         while ((opt = getopt(argc, argv, "U:nhS:D:A:M:C:G:td:")) != EOF) {
859                 switch (opt) {
860                 case 'U':
861                         pstrcpy(username,optarg);
862                         p = strchr_m(username,'%');
863                         if (p) {
864                                 *p = 0;
865                                 pstrcpy(password, p+1);
866                                 got_pass = 1;
867                         }
868                         break;
869
870                 case 'S':
871                         the_acl = optarg;
872                         mode = SMB_ACL_SET;
873                         break;
874
875                 case 'D':
876                         the_acl = optarg;
877                         mode = SMB_ACL_DELETE;
878                         break;
879
880                 case 'M':
881                         the_acl = optarg;
882                         mode = SMB_ACL_MODIFY;
883                         break;
884
885                 case 'A':
886                         the_acl = optarg;
887                         mode = SMB_ACL_ADD;
888                         break;
889
890                 case 'C':
891                         pstrcpy(owner_username,optarg);
892                         change_mode = REQUEST_CHOWN;
893                         break;
894
895                 case 'G':
896                         pstrcpy(owner_username,optarg);
897                         change_mode = REQUEST_CHGRP;
898                         break;
899
900                 case 'n':
901                         numeric = 1;
902                         break;
903
904                 case 't':
905                         test_args = 1;
906                         break;
907
908                 case 'h':
909                         usage();
910                         talloc_destroy(ctx);
911                         exit(EXIT_PARSE_ERROR);
912
913                 case 'd':
914                         DEBUGLEVEL = atoi(optarg);
915                         break;
916
917                 default:
918                         printf("Unknown option %c (%d)\n", (char)opt, opt);
919                         talloc_destroy(ctx);
920                         exit(EXIT_PARSE_ERROR);
921                 }
922         }
923
924         argc -= optind;
925         argv += optind;
926         
927         if (argc > 0) {
928                 usage();
929                 talloc_destroy(ctx);
930                 exit(EXIT_PARSE_ERROR);
931         }
932
933         /* Make connection to server */
934
935         if (!test_args) {
936                 cli = connect_one(share);
937                 if (!cli) {
938                         talloc_destroy(ctx);
939                         exit(EXIT_FAILED);
940                 }
941         }
942
943         all_string_sub(filename, "/", "\\", 0);
944         if (filename[0] != '\\') {
945                 pstring s;
946                 s[0] = '\\';
947                 safe_strcpy(&s[1], filename, sizeof(pstring)-1);
948                 pstrcpy(filename, s);
949         }
950
951         /* Perform requested action */
952
953         if (change_mode != REQUEST_NONE) {
954                 result = owner_set(cli, change_mode, filename, owner_username);
955         } else if (the_acl) {
956                 result = cacl_set(cli, filename, the_acl, mode);
957         } else {
958                 result = cacl_dump(cli, filename);
959         }
960
961         talloc_destroy(ctx);
962
963         return result;
964 }