r11464: Allow smbcacls to modify a SD on W2K3. This may fix several
[metze/samba/wip.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 pstring owner_username;
28 static fstring server;
29 static int test_args = False;
30 static TALLOC_CTX *ctx;
31
32 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
33
34 /* numeric is set when the user wants numeric SIDs and ACEs rather
35    than going via LSA calls to resolve them */
36 static BOOL numeric = False;
37
38 enum acl_mode {SMB_ACL_SET, SMB_ACL_DELETE, SMB_ACL_MODIFY, SMB_ACL_ADD };
39 enum chown_mode {REQUEST_NONE, REQUEST_CHOWN, REQUEST_CHGRP};
40 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
41
42 struct perm_value {
43         const char *perm;
44         uint32 mask;
45 };
46
47 /* These values discovered by inspection */
48
49 static const struct perm_value special_values[] = {
50         { "R", 0x00120089 },
51         { "W", 0x00120116 },
52         { "X", 0x001200a0 },
53         { "D", 0x00010000 },
54         { "P", 0x00040000 },
55         { "O", 0x00080000 },
56         { NULL, 0 },
57 };
58
59 static const struct perm_value standard_values[] = {
60         { "READ",   0x001200a9 },
61         { "CHANGE", 0x001301bf },
62         { "FULL",   0x001f01ff },
63         { NULL, 0 },
64 };
65
66 static struct cli_state *global_hack_cli;
67 static struct rpc_pipe_client *global_pipe_hnd;
68 static POLICY_HND pol;
69 static BOOL got_policy_hnd;
70
71 static struct cli_state *connect_one(const char *share);
72
73 /* Open cli connection and policy handle */
74
75 static BOOL cacls_open_policy_hnd(void)
76 {
77         /* Initialise cli LSA connection */
78
79         if (!global_hack_cli) {
80                 NTSTATUS ret;
81                 global_hack_cli = connect_one("IPC$");
82                 global_pipe_hnd = cli_rpc_pipe_open_noauth(global_hack_cli, PI_LSARPC, &ret);
83                 if (!global_pipe_hnd) {
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(rpccli_lsa_open_policy(global_pipe_hnd, 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(rpccli_lsa_lookup_sids(global_pipe_hnd, 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(rpccli_lsa_lookup_names(global_pipe_hnd, 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, const char *orig_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         char *str = SMB_STRDUP(orig_str);
239
240         if (!str) {
241                 return False;
242         }
243
244         ZERO_STRUCTP(ace);
245         p = strchr_m(str,':');
246         if (!p) {
247                 printf("ACE '%s': missing ':'.\n", orig_str);
248                 SAFE_FREE(str);
249                 return False;
250         }
251         *p = '\0';
252         p++;
253         /* Try to parse numeric form */
254
255         if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
256             StringToSid(&sid, str)) {
257                 goto done;
258         }
259
260         /* Try to parse text form */
261
262         if (!StringToSid(&sid, str)) {
263                 printf("ACE '%s': failed to convert '%s' to SID\n",
264                         orig_str, str);
265                 SAFE_FREE(str);
266                 return False;
267         }
268
269         cp = p;
270         if (!next_token(&cp, tok, "/", sizeof(fstring))) {
271                 printf("ACE '%s': failed to find '/' character.\n",
272                         orig_str);
273                 SAFE_FREE(str);
274                 return False;
275         }
276
277         if (strncmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
278                 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
279         } else if (strncmp(tok, "DENIED", strlen("DENIED")) == 0) {
280                 atype = SEC_ACE_TYPE_ACCESS_DENIED;
281         } else {
282                 printf("ACE '%s': missing 'ALLOWED' or 'DENIED' entry at '%s'\n",
283                         orig_str, tok);
284                 SAFE_FREE(str);
285                 return False;
286         }
287
288         /* Only numeric form accepted for flags at present */
289
290         if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
291               sscanf(tok, "%i", &aflags))) {
292                 printf("ACE '%s': bad integer flags entry at '%s'\n",
293                         orig_str, tok);
294                 SAFE_FREE(str);
295                 return False;
296         }
297
298         if (!next_token(&cp, tok, "/", sizeof(fstring))) {
299                 printf("ACE '%s': missing / at '%s'\n",
300                         orig_str, tok);
301                 SAFE_FREE(str);
302                 return False;
303         }
304
305         if (strncmp(tok, "0x", 2) == 0) {
306                 if (sscanf(tok, "%i", &amask) != 1) {
307                         printf("ACE '%s': bad hex number at '%s'\n",
308                                 orig_str, tok);
309                         SAFE_FREE(str);
310                         return False;
311                 }
312                 goto done;
313         }
314
315         for (v = standard_values; v->perm; v++) {
316                 if (strcmp(tok, v->perm) == 0) {
317                         amask = v->mask;
318                         goto done;
319                 }
320         }
321
322         p = tok;
323
324         while(*p) {
325                 BOOL found = False;
326
327                 for (v = special_values; v->perm; v++) {
328                         if (v->perm[0] == *p) {
329                                 amask |= v->mask;
330                                 found = True;
331                         }
332                 }
333
334                 if (!found) {
335                         printf("ACE '%s': bad permission value at '%s'\n",
336                                 orig_str, p);
337                         SAFE_FREE(str);
338                         return False;
339                 }
340                 p++;
341         }
342
343         if (*p) {
344                 SAFE_FREE(str);
345                 return False;
346         }
347
348  done:
349         mask.mask = amask;
350         init_sec_ace(ace, &sid, atype, mask, aflags);
351         SAFE_FREE(str);
352         return True;
353 }
354
355 /* add an ACE to a list of ACEs in a SEC_ACL */
356 static BOOL add_ace(SEC_ACL **the_acl, SEC_ACE *ace)
357 {
358         SEC_ACL *new_ace;
359         SEC_ACE *aces;
360         if (! *the_acl) {
361                 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
362                 return True;
363         }
364
365         aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces);
366         memcpy(aces, (*the_acl)->ace, (*the_acl)->num_aces * sizeof(SEC_ACE));
367         memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
368         new_ace = make_sec_acl(ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
369         SAFE_FREE(aces);
370         (*the_acl) = new_ace;
371         return True;
372 }
373
374 /* parse a ascii version of a security descriptor */
375 static SEC_DESC *sec_desc_parse(char *str)
376 {
377         const char *p = str;
378         fstring tok;
379         SEC_DESC *ret;
380         size_t sd_size;
381         DOM_SID *grp_sid=NULL, *owner_sid=NULL;
382         SEC_ACL *dacl=NULL;
383         int revision=1;
384
385         while (next_token(&p, tok, "\t,\r\n", sizeof(tok))) {
386
387                 if (strncmp(tok,"REVISION:", 9) == 0) {
388                         revision = strtol(tok+9, NULL, 16);
389                         continue;
390                 }
391
392                 if (strncmp(tok,"OWNER:", 6) == 0) {
393                         owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
394                         if (!owner_sid ||
395                             !StringToSid(owner_sid, tok+6)) {
396                                 printf("Failed to parse owner sid\n");
397                                 return NULL;
398                         }
399                         continue;
400                 }
401
402                 if (strncmp(tok,"GROUP:", 6) == 0) {
403                         grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
404                         if (!grp_sid ||
405                             !StringToSid(grp_sid, tok+6)) {
406                                 printf("Failed to parse group sid\n");
407                                 return NULL;
408                         }
409                         continue;
410                 }
411
412                 if (strncmp(tok,"ACL:", 4) == 0) {
413                         SEC_ACE ace;
414                         if (!parse_ace(&ace, tok+4)) {
415                                 return NULL;
416                         }
417                         if(!add_ace(&dacl, &ace)) {
418                                 printf("Failed to add ACL %s\n", tok);
419                                 return NULL;
420                         }
421                         continue;
422                 }
423
424                 printf("Failed to parse token '%s' in security descriptor,\n", tok);
425                 return NULL;
426         }
427
428         ret = make_sec_desc(ctx,revision, SEC_DESC_SELF_RELATIVE, owner_sid, grp_sid, 
429                             NULL, dacl, &sd_size);
430
431         SAFE_FREE(grp_sid);
432         SAFE_FREE(owner_sid);
433
434         return ret;
435 }
436
437
438 /* print a ascii version of a security descriptor on a FILE handle */
439 static void sec_desc_print(FILE *f, SEC_DESC *sd)
440 {
441         fstring sidstr;
442         uint32 i;
443
444         fprintf(f, "REVISION:%d\n", sd->revision);
445
446         /* Print owner and group sid */
447
448         if (sd->owner_sid) {
449                 SidToString(sidstr, sd->owner_sid);
450         } else {
451                 fstrcpy(sidstr, "");
452         }
453
454         fprintf(f, "OWNER:%s\n", sidstr);
455
456         if (sd->grp_sid) {
457                 SidToString(sidstr, sd->grp_sid);
458         } else {
459                 fstrcpy(sidstr, "");
460         }
461
462         fprintf(f, "GROUP:%s\n", sidstr);
463
464         /* Print aces */
465         for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
466                 SEC_ACE *ace = &sd->dacl->ace[i];
467                 fprintf(f, "ACL:");
468                 print_ace(f, ace);
469                 fprintf(f, "\n");
470         }
471
472 }
473
474 /***************************************************** 
475 dump the acls for a file
476 *******************************************************/
477 static int cacl_dump(struct cli_state *cli, char *filename)
478 {
479         int result = EXIT_FAILED;
480         int fnum = -1;
481         SEC_DESC *sd;
482
483         if (test_args) 
484                 return EXIT_OK;
485
486         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
487
488         if (fnum == -1) {
489                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
490                 goto done;
491         }
492
493         sd = cli_query_secdesc(cli, fnum, ctx);
494
495         if (!sd) {
496                 printf("ERROR: secdesc query failed: %s\n", cli_errstr(cli));
497                 goto done;
498         }
499
500         sec_desc_print(stdout, sd);
501
502         result = EXIT_OK;
503
504 done:
505         if (fnum != -1)
506                 cli_close(cli, fnum);
507
508         return result;
509 }
510
511 /***************************************************** 
512 Change the ownership or group ownership of a file. Just
513 because the NT docs say this can't be done :-). JRA.
514 *******************************************************/
515
516 static int owner_set(struct cli_state *cli, enum chown_mode change_mode, 
517                      char *filename, char *new_username)
518 {
519         int fnum;
520         DOM_SID sid;
521         SEC_DESC *sd, *old;
522         size_t sd_size;
523
524         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
525
526         if (fnum == -1) {
527                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
528                 return EXIT_FAILED;
529         }
530
531         if (!StringToSid(&sid, new_username))
532                 return EXIT_PARSE_ERROR;
533
534         old = cli_query_secdesc(cli, fnum, ctx);
535
536         cli_close(cli, fnum);
537
538         if (!old) {
539                 printf("owner_set: Failed to query old descriptor\n");
540                 return EXIT_FAILED;
541         }
542
543         sd = make_sec_desc(ctx,old->revision, old->type,
544                                 (change_mode == REQUEST_CHOWN) ? &sid : NULL,
545                                 (change_mode == REQUEST_CHGRP) ? &sid : NULL,
546                            NULL, NULL, &sd_size);
547
548         fnum = cli_nt_create(cli, filename, WRITE_OWNER_ACCESS);
549
550         if (fnum == -1) {
551                 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
552                 return EXIT_FAILED;
553         }
554
555         if (!cli_set_secdesc(cli, fnum, sd)) {
556                 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
557         }
558
559         cli_close(cli, fnum);
560
561         return EXIT_OK;
562 }
563
564
565 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
566    However NT4 gives a "The information may have been modified by a
567    computer running Windows NT 5.0" if denied ACEs do not appear before
568    allowed ACEs. */
569
570 static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
571 {
572         if (sec_ace_equal(ace1, ace2)) 
573                 return 0;
574
575         if (ace1->type != ace2->type) 
576                 return ace2->type - ace1->type;
577
578         if (sid_compare(&ace1->trustee, &ace2->trustee)) 
579                 return sid_compare(&ace1->trustee, &ace2->trustee);
580
581         if (ace1->flags != ace2->flags) 
582                 return ace1->flags - ace2->flags;
583
584         if (ace1->info.mask != ace2->info.mask) 
585                 return ace1->info.mask - ace2->info.mask;
586
587         if (ace1->size != ace2->size) 
588                 return ace1->size - ace2->size;
589
590         return memcmp(ace1, ace2, sizeof(SEC_ACE));
591 }
592
593 static void sort_acl(SEC_ACL *the_acl)
594 {
595         uint32 i;
596         if (!the_acl) return;
597
598         qsort(the_acl->ace, the_acl->num_aces, sizeof(the_acl->ace[0]), QSORT_CAST ace_compare);
599
600         for (i=1;i<the_acl->num_aces;) {
601                 if (sec_ace_equal(&the_acl->ace[i-1], &the_acl->ace[i])) {
602                         int j;
603                         for (j=i; j<the_acl->num_aces-1; j++) {
604                                 the_acl->ace[j] = the_acl->ace[j+1];
605                         }
606                         the_acl->num_aces--;
607                 } else {
608                         i++;
609                 }
610         }
611 }
612
613 /***************************************************** 
614 set the ACLs on a file given an ascii description
615 *******************************************************/
616 static int cacl_set(struct cli_state *cli, char *filename, 
617                     char *the_acl, enum acl_mode mode)
618 {
619         int fnum;
620         SEC_DESC *sd, *old;
621         uint32 i, j;
622         size_t sd_size;
623         int result = EXIT_OK;
624
625         sd = sec_desc_parse(the_acl);
626
627         if (!sd) return EXIT_PARSE_ERROR;
628         if (test_args) return EXIT_OK;
629
630         /* The desired access below is the only one I could find that works
631            with NT4, W2KP and Samba */
632
633         fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
634
635         if (fnum == -1) {
636                 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
637                 return EXIT_FAILED;
638         }
639
640         old = cli_query_secdesc(cli, fnum, ctx);
641
642         if (!old) {
643                 printf("calc_set: Failed to query old descriptor\n");
644                 return EXIT_FAILED;
645         }
646
647         cli_close(cli, fnum);
648
649         /* the logic here is rather more complex than I would like */
650         switch (mode) {
651         case SMB_ACL_DELETE:
652                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
653                         BOOL found = False;
654
655                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
656                                 if (sec_ace_equal(&sd->dacl->ace[i],
657                                                   &old->dacl->ace[j])) {
658                                         uint32 k;
659                                         for (k=j; k<old->dacl->num_aces-1;k++) {
660                                                 old->dacl->ace[k] = old->dacl->ace[k+1];
661                                         }
662                                         old->dacl->num_aces--;
663                                         found = True;
664                                         break;
665                                 }
666                         }
667
668                         if (!found) {
669                                 printf("ACL for ACE:"); 
670                                 print_ace(stdout, &sd->dacl->ace[i]);
671                                 printf(" not found\n");
672                         }
673                 }
674                 break;
675
676         case SMB_ACL_MODIFY:
677                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
678                         BOOL found = False;
679
680                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
681                                 if (sid_equal(&sd->dacl->ace[i].trustee,
682                                               &old->dacl->ace[j].trustee)) {
683                                         old->dacl->ace[j] = sd->dacl->ace[i];
684                                         found = True;
685                                 }
686                         }
687
688                         if (!found) {
689                                 fstring str;
690
691                                 SidToString(str, &sd->dacl->ace[i].trustee);
692                                 printf("ACL for SID %s not found\n", str);
693                         }
694                 }
695
696                 if (sd->owner_sid) {
697                         old->owner_sid = sd->owner_sid;
698                 }
699
700                 if (sd->grp_sid) { 
701                         old->grp_sid = sd->grp_sid;
702                 }
703
704                 break;
705
706         case SMB_ACL_ADD:
707                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
708                         add_ace(&old->dacl, &sd->dacl->ace[i]);
709                 }
710                 break;
711
712         case SMB_ACL_SET:
713                 old = sd;
714                 break;
715         }
716
717         /* Denied ACE entries must come before allowed ones */
718         sort_acl(old->dacl);
719
720         /* Create new security descriptor and set it */
721 #if 0
722         /* We used to just have "WRITE_DAC_ACCESS" without WRITE_OWNER.
723            But if we're sending an owner, even if it's the same as the one
724            that already exists then W2K3 insists we open with WRITE_OWNER access.
725            I need to check that setting a SD with no owner set works against WNT
726            and W2K. JRA.
727         */
728
729         sd = make_sec_desc(ctx,old->revision, old->type, old->owner_sid, old->grp_sid,
730                            NULL, old->dacl, &sd_size);
731
732         fnum = cli_nt_create(cli, filename, WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS);
733 #else
734         sd = make_sec_desc(ctx,old->revision, old->type, NULL, NULL,
735                            NULL, old->dacl, &sd_size);
736
737         fnum = cli_nt_create(cli, filename, WRITE_DAC_ACCESS);
738 #endif
739         if (fnum == -1) {
740                 printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
741                 return EXIT_FAILED;
742         }
743
744         if (!cli_set_secdesc(cli, fnum, sd)) {
745                 printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
746                 result = EXIT_FAILED;
747         }
748
749         /* Clean up */
750
751         cli_close(cli, fnum);
752
753         return result;
754 }
755
756
757 /***************************************************** 
758 return a connection to a server
759 *******************************************************/
760 static struct cli_state *connect_one(const char *share)
761 {
762         struct cli_state *c;
763         struct in_addr ip;
764         NTSTATUS nt_status;
765         zero_ip(&ip);
766         
767         if (!cmdline_auth_info.got_pass) {
768                 char *pass = getpass("Password: ");
769                 if (pass) {
770                         pstrcpy(cmdline_auth_info.password, pass);
771                         cmdline_auth_info.got_pass = True;
772                 }
773         }
774
775         if (NT_STATUS_IS_OK(nt_status = cli_full_connection(&c, global_myname(), server, 
776                                                             &ip, 0,
777                                                             share, "?????",  
778                                                             cmdline_auth_info.username, lp_workgroup(),
779                                                             cmdline_auth_info.password, 0,
780                                                             cmdline_auth_info.signing_state, NULL))) {
781                 return c;
782         } else {
783                 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
784                 return NULL;
785         }
786 }
787
788 /****************************************************************************
789   main program
790 ****************************************************************************/
791  int main(int argc, const char *argv[])
792 {
793         char *share;
794         int opt;
795         enum acl_mode mode = SMB_ACL_SET;
796         static char *the_acl = NULL;
797         enum chown_mode change_mode = REQUEST_NONE;
798         int result;
799         fstring path;
800         pstring filename;
801         poptContext pc;
802         struct poptOption long_options[] = {
803                 POPT_AUTOHELP
804                 { "delete", 'D', POPT_ARG_STRING, NULL, 'D', "Delete an acl", "ACL" },
805                 { "modify", 'M', POPT_ARG_STRING, NULL, 'M', "Modify an acl", "ACL" },
806                 { "add", 'a', POPT_ARG_STRING, NULL, 'a', "Add an acl", "ACL" },
807                 { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls", "ACLS" },
808                 { "chown", 'C', POPT_ARG_STRING, NULL, 'C', "Change ownership of a file", "USERNAME" },
809                 { "chgrp", 'G', POPT_ARG_STRING, NULL, 'G', "Change group ownership of a file", "GROUPNAME" },
810                 { "numeric", 0, POPT_ARG_NONE, &numeric, True, "Don't resolve sids or masks to names" },
811                 { "test-args", 't', POPT_ARG_NONE, &test_args, True, "Test arguments"},
812                 POPT_COMMON_SAMBA
813                 POPT_COMMON_CREDENTIALS
814                 { NULL }
815         };
816
817         struct cli_state *cli;
818
819         ctx=talloc_init("main");
820
821         /* set default debug level to 1 regardless of what smb.conf sets */
822         setup_logging( "smbcacls", True );
823         DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
824         dbf = x_stderr;
825         x_setbuf( x_stderr, NULL );
826
827         setlinebuf(stdout);
828
829         lp_load(dyn_CONFIGFILE,True,False,False);
830         load_interfaces();
831
832         pc = poptGetContext("smbcacls", argc, argv, long_options, 0);
833         
834         poptSetOtherOptionHelp(pc, "//server1/share1 filename");
835
836         while ((opt = poptGetNextOpt(pc)) != -1) {
837                 switch (opt) {
838                 case 'S':
839                         the_acl = smb_xstrdup(poptGetOptArg(pc));
840                         mode = SMB_ACL_SET;
841                         break;
842
843                 case 'D':
844                         the_acl = smb_xstrdup(poptGetOptArg(pc));
845                         mode = SMB_ACL_DELETE;
846                         break;
847
848                 case 'M':
849                         the_acl = smb_xstrdup(poptGetOptArg(pc));
850                         mode = SMB_ACL_MODIFY;
851                         break;
852
853                 case 'a':
854                         the_acl = smb_xstrdup(poptGetOptArg(pc));
855                         mode = SMB_ACL_ADD;
856                         break;
857
858                 case 'C':
859                         pstrcpy(owner_username,poptGetOptArg(pc));
860                         change_mode = REQUEST_CHOWN;
861                         break;
862
863                 case 'G':
864                         pstrcpy(owner_username,poptGetOptArg(pc));
865                         change_mode = REQUEST_CHGRP;
866                         break;
867                 }
868         }
869
870         /* Make connection to server */
871         if(!poptPeekArg(pc)) { 
872                 poptPrintUsage(pc, stderr, 0);
873                 return -1;
874         }
875         
876         fstrcpy(path, poptGetArg(pc));
877         
878         if(!poptPeekArg(pc)) { 
879                 poptPrintUsage(pc, stderr, 0);  
880                 return -1;
881         }
882         
883         pstrcpy(filename, poptGetArg(pc));
884
885         all_string_sub(path,"/","\\",0);
886
887         fstrcpy(server,path+2);
888         share = strchr_m(server,'\\');
889         if (!share) {
890                 share = strchr_m(server,'/');
891                 if (!share) {
892                         printf("Invalid argument: %s\n", share);
893                         return -1;
894                 }
895         }
896
897         *share = 0;
898         share++;
899
900         if (!test_args) {
901                 cli = connect_one(share);
902                 if (!cli) {
903                         talloc_destroy(ctx);
904                         exit(EXIT_FAILED);
905                 }
906         } else {
907                 exit(0);
908         }
909
910         all_string_sub(filename, "/", "\\", 0);
911         if (filename[0] != '\\') {
912                 pstring s;
913                 s[0] = '\\';
914                 safe_strcpy(&s[1], filename, sizeof(pstring)-2);
915                 pstrcpy(filename, s);
916         }
917
918         /* Perform requested action */
919
920         if (change_mode != REQUEST_NONE) {
921                 result = owner_set(cli, change_mode, filename, owner_username);
922         } else if (the_acl) {
923                 result = cacl_set(cli, filename, the_acl, mode);
924         } else {
925                 result = cacl_dump(cli, filename);
926         }
927
928         talloc_destroy(ctx);
929
930         return result;
931 }