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