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