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