Fix compile errors - don't use pstrcpy() on a fstring
[samba.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    Copyright (C) Jelmer Vernooij 2003
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 = False;
33 static 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 BOOL numeric = False;
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         const char *perm;
48         uint32 mask;
49 };
50
51 /* These values discovered by inspection */
52
53 static const 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 const struct perm_value standard_values[] = {
64         { "READ",   0x001200a9 },
65         { "CHANGE", 0x001301bf },
66         { "FULL",   0x001f01ff },
67         { NULL, 0 },
68 };
69
70 static struct cli_state *global_hack_cli;
71 static POLICY_HND pol;
72 static BOOL got_policy_hnd;
73
74 static struct cli_state *connect_one(const char *share);
75
76 /* Open cli connection and policy handle */
77
78 static BOOL cacls_open_policy_hnd(void)
79 {
80         /* Initialise cli LSA connection */
81
82         if (!global_hack_cli) {
83                 global_hack_cli = connect_one("IPC$");
84                 if (!cli_nt_session_open (global_hack_cli, PI_LSARPC)) {
85                                 return False;
86                 }
87         }
88         
89         /* Open policy handle */
90
91         if (!got_policy_hnd) {
92
93                 /* Some systems don't support SEC_RIGHTS_MAXIMUM_ALLOWED,
94                    but NT sends 0x2000000 so we might as well do it too. */
95
96                 if (!NT_STATUS_IS_OK(cli_lsa_open_policy(global_hack_cli, global_hack_cli->mem_ctx, True, 
97                                                          GENERIC_EXECUTE_ACCESS, &pol))) {
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 **domains = NULL;
111         char **names = NULL;
112         uint32 *types = NULL;
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             !NT_STATUS_IS_OK(cli_lsa_lookup_sids(global_hack_cli, global_hack_cli->mem_ctx,  
122                                                  &pol, 1, sid, &domains, 
123                                                  &names, &types)) ||
124             !domains || !domains[0] || !names || !names[0]) {
125                 return;
126         }
127
128         /* Converted OK */
129
130         slprintf(str, sizeof(fstring) - 1, "%s%s%s",
131                  domains[0], lp_winbind_separator(),
132                  names[0]);
133         
134 }
135
136 /* convert a string to a SID, either numeric or username/group */
137 static BOOL StringToSid(DOM_SID *sid, const char *str)
138 {
139         uint32 *types = NULL;
140         DOM_SID *sids = NULL;
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(global_hack_cli, global_hack_cli->mem_ctx, 
149                                                   &pol, 1, &str, &sids, 
150                                                   &types))) {
151                 result = False;
152                 goto done;
153         }
154
155         sid_copy(sid, &sids[0]);
156  done:
157
158         return result;
159 }
160
161
162 /* print an ACE on a FILE, using either numeric or ascii representation */
163 static void print_ace(FILE *f, SEC_ACE *ace)
164 {
165         const struct perm_value *v;
166         fstring sidstr;
167         int do_print = 0;
168         uint32 got_mask;
169
170         SidToString(sidstr, &ace->trustee);
171
172         fprintf(f, "%s:", sidstr);
173
174         if (numeric) {
175                 fprintf(f, "%d/%d/0x%08x", 
176                         ace->type, ace->flags, ace->info.mask);
177                 return;
178         }
179
180         /* Ace type */
181
182         if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
183                 fprintf(f, "ALLOWED");
184         } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
185                 fprintf(f, "DENIED");
186         } else {
187                 fprintf(f, "%d", ace->type);
188         }
189
190         /* Not sure what flags can be set in a file ACL */
191
192         fprintf(f, "/%d/", ace->flags);
193
194         /* Standard permissions */
195
196         for (v = standard_values; v->perm; v++) {
197                 if (ace->info.mask == v->mask) {
198                         fprintf(f, "%s", v->perm);
199                         return;
200                 }
201         }
202
203         /* Special permissions.  Print out a hex value if we have
204            leftover bits in the mask. */
205
206         got_mask = ace->info.mask;
207
208  again:
209         for (v = special_values; v->perm; v++) {
210                 if ((ace->info.mask & v->mask) == v->mask) {
211                         if (do_print) {
212                                 fprintf(f, "%s", v->perm);
213                         }
214                         got_mask &= ~v->mask;
215                 }
216         }
217
218         if (!do_print) {
219                 if (got_mask != 0) {
220                         fprintf(f, "0x%08x", ace->info.mask);
221                 } else {
222                         do_print = 1;
223                         goto again;
224                 }
225         }
226 }
227
228
229 /* parse an ACE in the same format as print_ace() */
230 static BOOL parse_ace(SEC_ACE *ace, char *str)
231 {
232         char *p;
233         const char *cp;
234         fstring tok;
235         unsigned atype, aflags, amask;
236         DOM_SID sid;
237         SEC_ACCESS mask;
238         const 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         /* Try to parse numeric form */
246
247         if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
248             StringToSid(&sid, str)) {
249                 goto done;
250         }
251
252         /* Try to parse text form */
253
254         if (!StringToSid(&sid, str)) {
255                 return False;
256         }
257
258         cp = p;
259         if (!next_token(&cp, 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(&cp, tok, "/", sizeof(fstring)) &&
274               sscanf(tok, "%i", &aflags))) {
275                 return False;
276         }
277
278         if (!next_token(&cp, 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         const 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 result = EXIT_FAILED;
448         int fnum = -1;
449         SEC_DESC *sd;
450
451         if (test_args) 
452                 return EXIT_OK;
453
454         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
455
456         if (fnum == -1) {
457                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
458                 goto done;
459         }
460
461         sd = cli_query_secdesc(cli, fnum, ctx);
462
463         if (!sd) {
464                 printf("ERROR: secdesc query failed: %s\n", cli_errstr(cli));
465                 goto done;
466         }
467
468         sec_desc_print(stdout, sd);
469
470         result = EXIT_OK;
471
472 done:
473         if (fnum != -1)
474                 cli_close(cli, fnum);
475
476         return result;
477 }
478
479 /***************************************************** 
480 Change the ownership or group ownership of a file. Just
481 because the NT docs say this can't be done :-). JRA.
482 *******************************************************/
483
484 static int owner_set(struct cli_state *cli, enum chown_mode change_mode, 
485                      char *filename, char *new_username)
486 {
487         int fnum;
488         DOM_SID sid;
489         SEC_DESC *sd, *old;
490         size_t sd_size;
491
492         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
493
494         if (fnum == -1) {
495                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
496                 return EXIT_FAILED;
497         }
498
499         if (!StringToSid(&sid, new_username))
500                 return EXIT_PARSE_ERROR;
501
502         old = cli_query_secdesc(cli, fnum, ctx);
503
504         cli_close(cli, fnum);
505
506         if (!old) {
507                 printf("owner_set: Failed to query old descriptor\n");
508                 return EXIT_FAILED;
509         }
510
511         sd = make_sec_desc(ctx,old->revision,
512                                 (change_mode == REQUEST_CHOWN) ? &sid : old->owner_sid,
513                                 (change_mode == REQUEST_CHGRP) ? &sid : old->grp_sid,
514                            NULL, old->dacl, &sd_size);
515
516         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
517
518         if (fnum == -1) {
519                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
520                 return EXIT_FAILED;
521         }
522
523         if (!cli_set_secdesc(cli, fnum, sd)) {
524                 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
525         }
526
527         cli_close(cli, fnum);
528
529         return EXIT_OK;
530 }
531
532
533 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
534    However NT4 gives a "The information may have been modified by a
535    computer running Windows NT 5.0" if denied ACEs do not appear before
536    allowed ACEs. */
537
538 static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
539 {
540         if (sec_ace_equal(ace1, ace2)) 
541                 return 0;
542
543         if (ace1->type != ace2->type) 
544                 return ace2->type - ace1->type;
545
546         if (sid_compare(&ace1->trustee, &ace2->trustee)) 
547                 return sid_compare(&ace1->trustee, &ace2->trustee);
548
549         if (ace1->flags != ace2->flags) 
550                 return ace1->flags - ace2->flags;
551
552         if (ace1->info.mask != ace2->info.mask) 
553                 return ace1->info.mask - ace2->info.mask;
554
555         if (ace1->size != ace2->size) 
556                 return ace1->size - ace2->size;
557
558         return memcmp(ace1, ace2, sizeof(SEC_ACE));
559 }
560
561 static void sort_acl(SEC_ACL *the_acl)
562 {
563         uint32 i;
564         if (!the_acl) return;
565
566         qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]), QSORT_CAST ace_compare);
567
568         for (i=1;i<the_acl->num_aces;) {
569                 if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
570                         int j;
571                         for (j=i; j<the_acl->num_aces-1; j++) {
572                                 the_acl->ace[j] = the_acl->ace[j+1];
573                         }
574                         the_acl->num_aces--;
575                 } else {
576                         i++;
577                 }
578         }
579 }
580
581 /***************************************************** 
582 set the ACLs on a file given an ascii description
583 *******************************************************/
584 static int cacl_set(struct cli_state *cli, char *filename, 
585                     char *the_acl, enum acl_mode mode)
586 {
587         int fnum;
588         SEC_DESC *sd, *old;
589         uint32 i, j;
590         size_t sd_size;
591         int result = EXIT_OK;
592
593         sd = sec_desc_parse(the_acl);
594
595         if (!sd) return EXIT_PARSE_ERROR;
596         if (test_args) return EXIT_OK;
597
598         /* The desired access below is the only one I could find that works
599            with NT4, W2KP and Samba */
600
601         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
602
603         if (fnum == -1) {
604                 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
605                 return EXIT_FAILED;
606         }
607
608         old = cli_query_secdesc(cli, fnum, ctx);
609
610         if (!old) {
611                 printf("calc_set: Failed to query old descriptor\n");
612                 return EXIT_FAILED;
613         }
614
615         cli_close(cli, fnum);
616
617         /* the logic here is rather more complex than I would like */
618         switch (mode) {
619         case SMB_ACL_DELETE:
620                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
621                         BOOL found = False;
622
623                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
624                                 if (sec_ace_equal(&sd->dacl->ace[i],
625                                                   &old->dacl->ace[j])) {
626                                         uint32 k;
627                                         for (k=j; k<old->dacl->num_aces-1;k++) {
628                                                 old->dacl->ace[k] = old->dacl->ace[k+1];
629                                         }
630                                         old->dacl->num_aces--;
631                                         if (old->dacl->num_aces == 0) {
632                                                 SAFE_FREE(old->dacl->ace);
633                                                 SAFE_FREE(old->dacl);
634                                                 old->off_dacl = 0;
635                                         }
636                                         found = True;
637                                         break;
638                                 }
639                         }
640
641                         if (!found) {
642                                 printf("ACL for ACE:"); 
643                                 print_ace(stdout, &sd->dacl->ace[i]);
644                                 printf(" not found\n");
645                         }
646                 }
647                 break;
648
649         case SMB_ACL_MODIFY:
650                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
651                         BOOL found = False;
652
653                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
654                                 if (sid_equal(&sd->dacl->ace[i].trustee,
655                                               &old->dacl->ace[j].trustee)) {
656                                         old->dacl->ace[j] = sd->dacl->ace[i];
657                                         found = True;
658                                 }
659                         }
660
661                         if (!found) {
662                                 fstring str;
663
664                                 SidToString(str, &sd->dacl->ace[i].trustee);
665                                 printf("ACL for SID %s not found\n", str);
666                         }
667                 }
668
669                 break;
670
671         case SMB_ACL_ADD:
672                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
673                         add_ace(&old->dacl, &sd->dacl->ace[i]);
674                 }
675                 break;
676
677         case SMB_ACL_SET:
678                 old = sd;
679                 break;
680         }
681
682         /* Denied ACE entries must come before allowed ones */
683         sort_acl(old->dacl);
684
685         /* Create new security descriptor and set it */
686         sd = make_sec_desc(ctx,old->revision, old->owner_sid, old->grp_sid, 
687                            NULL, old->dacl, &sd_size);
688
689         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_WRITE);
690
691         if (fnum == -1) {
692                 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
693                 return EXIT_FAILED;
694         }
695
696         if (!cli_set_secdesc(cli, fnum, sd)) {
697                 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
698                 result = EXIT_FAILED;
699         }
700
701         /* Clean up */
702
703         cli_close(cli, fnum);
704
705         return result;
706 }
707
708
709 /***************************************************** 
710 return a connection to a server
711 *******************************************************/
712 static struct cli_state *connect_one(const char *share)
713 {
714         struct cli_state *c;
715         struct in_addr ip;
716         NTSTATUS nt_status;
717         zero_ip(&ip);
718         
719         if (!got_pass) {
720                 char *pass = getpass("Password: ");
721                 if (pass) {
722                         fstrcpy(password, pass);
723                         got_pass = True;
724                 }
725         }
726
727         if (NT_STATUS_IS_OK(nt_status = cli_full_connection(&c, global_myname(), server, 
728                                                             &ip, 0,
729                                                             share, "?????",  
730                                                             username, lp_workgroup(),
731                                                             password, 0, NULL))) {
732                 return c;
733         } else {
734                 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
735                 return NULL;
736         }
737 }
738
739 /****************************************************************************
740   main program
741 ****************************************************************************/
742  int main(int argc, const char *argv[])
743 {
744         char *share;
745         int opt;
746         char *p;
747         enum acl_mode mode = SMB_ACL_SET;
748         static char *the_acl = NULL;
749         enum chown_mode change_mode = REQUEST_NONE;
750         int result;
751         fstring path;
752         pstring filename;
753         poptContext pc;
754         struct poptOption long_options[] = {
755                 POPT_AUTOHELP
756                 { "delete", 'D', POPT_ARG_STRING, NULL, 'D', "Delete an acl", "ACL" },
757                 { "modify", 'M', POPT_ARG_STRING, NULL, 'M', "Modify an acl", "ACL" },
758                 { "add", 'A', POPT_ARG_STRING, NULL, 'A', "Add an acl", "ACL" },
759                 { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls", "ACLS" },
760                 { "chown", 'C', POPT_ARG_STRING, NULL, 'C', "Change ownership of a file", "USERNAME" },
761                 { "chgrp", 'G', POPT_ARG_STRING, NULL, 'G', "Change group ownership of a file", "GROUPNAME" },
762                 { "numeric", 'n', POPT_ARG_VAL, &numeric, True, "Don't resolve sids or masks to names" },
763                 { "test-args", 't', POPT_ARG_VAL, &test_args, True, "Test arguments"},
764                 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_debug },
765                 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_version },
766                 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_configfile },
767                 {"username", 'U', POPT_ARG_STRING, NULL, 'U', "User to authenticate as", "user%password" },
768                 { NULL }
769         };
770
771         struct cli_state *cli;
772
773         ctx=talloc_init("main");
774
775         setlinebuf(stdout);
776
777         dbf = x_stderr;
778
779         setup_logging(argv[0],True);
780
781         lp_load(dyn_CONFIGFILE,True,False,False);
782         load_interfaces();
783
784         if (getenv("USER")) {
785                 pstrcpy(username,getenv("USER"));
786
787                 if ((p=strchr_m(username,'%'))) {
788                         *p = 0;
789                         fstrcpy(password,p+1);
790                         got_pass = True;
791                         memset(strchr_m(getenv("USER"), '%') + 1, 'X',
792                                strlen(password));
793                 }
794         }
795         pc = poptGetContext("smbcacls", argc, argv, long_options, 0);
796         
797         poptSetOtherOptionHelp(pc, "//server1/share1 filename [options]");
798
799         while ((opt = poptGetNextOpt(pc)) != -1) {
800                 switch (opt) {
801                 case 'U':
802                         pstrcpy(username,poptGetOptArg(pc));
803                         p = strchr_m(username,'%');
804                         if (p) {
805                                 *p = 0;
806                                 fstrcpy(password, p+1);
807                                 got_pass = 1;
808                         }
809                         break;
810
811                 case 'S':
812                         the_acl = smb_xstrdup(poptGetOptArg(pc));
813                         mode = SMB_ACL_SET;
814                         break;
815
816                 case 'D':
817                         the_acl = smb_xstrdup(poptGetOptArg(pc));
818                         mode = SMB_ACL_DELETE;
819                         break;
820
821                 case 'M':
822                         the_acl = smb_xstrdup(poptGetOptArg(pc));
823                         mode = SMB_ACL_MODIFY;
824                         break;
825
826                 case 'A':
827                         the_acl = smb_xstrdup(poptGetOptArg(pc));
828                         mode = SMB_ACL_ADD;
829                         break;
830
831                 case 'C':
832                         pstrcpy(owner_username,poptGetOptArg(pc));
833                         change_mode = REQUEST_CHOWN;
834                         break;
835
836                 case 'G':
837                         pstrcpy(owner_username,poptGetOptArg(pc));
838                         change_mode = REQUEST_CHGRP;
839                         break;
840                 }
841         }
842
843         /* Make connection to server */
844         if(!poptPeekArg(pc)) { 
845                 poptPrintUsage(pc, stderr, 0);
846                 return -1;
847         }
848         
849         fstrcpy(path, poptGetArg(pc));
850         
851         if(!poptPeekArg(pc)) { 
852                 poptPrintUsage(pc, stderr, 0);  
853                 return -1;
854         }
855         
856         pstrcpy(filename, poptGetArg(pc));
857
858         all_string_sub(path,"/","\\",0);
859
860         fstrcpy(server,path+2);
861         share = strchr_m(server,'\\');
862         if (!share) {
863                 share = strchr_m(server,'/');
864                 if (!share) {
865                         printf("Invalid argument: %s\n", share);
866                         return -1;
867                 }
868         }
869
870         *share = 0;
871         share++;
872
873         if (!test_args) {
874                 cli = connect_one(share);
875                 if (!cli) {
876                         talloc_destroy(ctx);
877                         exit(EXIT_FAILED);
878                 }
879         } else {
880                 exit(0);
881         }
882
883         all_string_sub(filename, "/", "\\", 0);
884         if (filename[0] != '\\') {
885                 pstring s;
886                 s[0] = '\\';
887                 safe_strcpy(&s[1], filename, sizeof(pstring)-2);
888                 pstrcpy(filename, s);
889         }
890
891         /* Perform requested action */
892
893         if (change_mode != REQUEST_NONE) {
894                 result = owner_set(cli, change_mode, filename, owner_username);
895         } else if (the_acl) {
896                 result = cacl_set(cli, filename, the_acl, mode);
897         } else {
898                 result = cacl_dump(cli, filename);
899         }
900
901         talloc_destroy(ctx);
902
903         return result;
904 }