r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[nivanova/samba-autobuild/.git] / source3 / utils / sharesec.c
1 /*
2  *  Unix SMB/Netbios implementation.
3  *  Utility for managing share permissions
4  *
5  *  Copyright (C) Tim Potter                    2000
6  *  Copyright (C) Jeremy Allison                2000
7  *  Copyright (C) Jelmer Vernooij               2003
8  *  Copyright (C) Gerald (Jerry) Carter         2005.
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
25 #include "includes.h"
26
27 static TALLOC_CTX *ctx;
28
29 enum acl_mode {SMB_ACL_DELETE, SMB_ACL_MODIFY, SMB_ACL_ADD, SMB_ACL_SET, SMB_ACL_VIEW };
30
31 struct perm_value {
32         const char *perm;
33         uint32 mask;
34 };
35
36 /* These values discovered by inspection */
37
38 static const struct perm_value special_values[] = {
39         { "R", SEC_RIGHTS_FILE_READ },
40         { "W", SEC_RIGHTS_FILE_WRITE },
41         { "X", SEC_RIGHTS_FILE_EXECUTE },
42         { "D", SEC_STD_DELETE },
43         { "P", SEC_STD_WRITE_DAC },
44         { "O", SEC_STD_WRITE_OWNER },
45         { NULL, 0 },
46 };
47
48 #define SEC_RIGHTS_DIR_CHANGE ( SEC_RIGHTS_DIR_READ|SEC_STD_DELETE|SEC_RIGHTS_DIR_WRITE|SEC_DIR_TRAVERSE )
49
50 static const struct perm_value standard_values[] = {
51         { "READ",   SEC_RIGHTS_DIR_READ|SEC_DIR_TRAVERSE },
52         { "CHANGE", SEC_RIGHTS_DIR_CHANGE },
53         { "FULL",   SEC_RIGHTS_DIR_ALL },
54         { NULL, 0 },
55 };
56
57 /********************************************************************
58  print an ACE on a FILE
59 ********************************************************************/
60
61 static void print_ace(FILE *f, SEC_ACE *ace)
62 {
63         const struct perm_value *v;
64         int do_print = 0;
65         uint32 got_mask;
66
67         fprintf(f, "%s:", sid_string_static(&ace->trustee));
68
69         /* Ace type */
70
71         if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
72                 fprintf(f, "ALLOWED");
73         } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
74                 fprintf(f, "DENIED");
75         } else {
76                 fprintf(f, "%d", ace->type);
77         }
78
79         /* Not sure what flags can be set in a file ACL */
80
81         fprintf(f, "/%d/", ace->flags);
82
83         /* Standard permissions */
84
85         for (v = standard_values; v->perm; v++) {
86                 if (ace->access_mask == v->mask) {
87                         fprintf(f, "%s", v->perm);
88                         return;
89                 }
90         }
91
92         /* Special permissions.  Print out a hex value if we have
93            leftover bits in the mask. */
94
95         got_mask = ace->access_mask;
96
97  again:
98         for (v = special_values; v->perm; v++) {
99                 if ((ace->access_mask & v->mask) == v->mask) {
100                         if (do_print) {
101                                 fprintf(f, "%s", v->perm);
102                         }
103                         got_mask &= ~v->mask;
104                 }
105         }
106
107         if (!do_print) {
108                 if (got_mask != 0) {
109                         fprintf(f, "0x%08x", ace->access_mask);
110                 } else {
111                         do_print = 1;
112                         goto again;
113                 }
114         }
115 }
116
117 /********************************************************************
118  print a ascii version of a security descriptor on a FILE handle
119 ********************************************************************/
120
121 static void sec_desc_print(FILE *f, SEC_DESC *sd)
122 {
123         uint32 i;
124
125         fprintf(f, "REVISION:%d\n", sd->revision);
126
127         /* Print owner and group sid */
128
129         fprintf(f, "OWNER:%s\n", sid_string_static(sd->owner_sid));
130
131         fprintf(f, "GROUP:%s\n", sid_string_static(sd->group_sid));
132
133         /* Print aces */
134         for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
135                 SEC_ACE *ace = &sd->dacl->aces[i];
136                 fprintf(f, "ACL:");
137                 print_ace(f, ace);
138                 fprintf(f, "\n");
139         }
140
141 }
142
143 /********************************************************************
144     parse an ACE in the same format as print_ace()
145 ********************************************************************/
146
147 static BOOL parse_ace(SEC_ACE *ace, const char *orig_str)
148 {
149         char *p;
150         const char *cp;
151         fstring tok;
152         unsigned int atype = 0;
153         unsigned int aflags = 0;
154         unsigned int amask = 0;
155         DOM_SID sid;
156         SEC_ACCESS mask;
157         const struct perm_value *v;
158         char *str = SMB_STRDUP(orig_str);
159
160         if (!str) {
161                 return False;
162         }
163
164         ZERO_STRUCTP(ace);
165         p = strchr_m(str,':');
166         if (!p) {
167                 printf("ACE '%s': missing ':'.\n", orig_str);
168                 SAFE_FREE(str);
169                 return False;
170         }
171         *p = '\0';
172         p++;
173         /* Try to parse numeric form */
174
175         if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
176             string_to_sid(&sid, str)) {
177                 goto done;
178         }
179
180         /* Try to parse text form */
181
182         if (!string_to_sid(&sid, str)) {
183                 printf("ACE '%s': failed to convert '%s' to SID\n",
184                         orig_str, str);
185                 SAFE_FREE(str);
186                 return False;
187         }
188
189         cp = p;
190         if (!next_token(&cp, tok, "/", sizeof(fstring))) {
191                 printf("ACE '%s': failed to find '/' character.\n",
192                         orig_str);
193                 SAFE_FREE(str);
194                 return False;
195         }
196
197         if (strncmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
198                 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
199         } else if (strncmp(tok, "DENIED", strlen("DENIED")) == 0) {
200                 atype = SEC_ACE_TYPE_ACCESS_DENIED;
201         } else {
202                 printf("ACE '%s': missing 'ALLOWED' or 'DENIED' entry at '%s'\n",
203                         orig_str, tok);
204                 SAFE_FREE(str);
205                 return False;
206         }
207
208         /* Only numeric form accepted for flags at present */
209         /* no flags on share permissions */
210
211         if (!(next_token(&cp, tok, "/", sizeof(fstring)) &&
212               sscanf(tok, "%i", &aflags) && aflags == 0)) {
213                 printf("ACE '%s': bad integer flags entry at '%s'\n",
214                         orig_str, tok);
215                 SAFE_FREE(str);
216                 return False;
217         }
218
219         if (!next_token(&cp, tok, "/", sizeof(fstring))) {
220                 printf("ACE '%s': missing / at '%s'\n",
221                         orig_str, tok);
222                 SAFE_FREE(str);
223                 return False;
224         }
225
226         if (strncmp(tok, "0x", 2) == 0) {
227                 if (sscanf(tok, "%i", &amask) != 1) {
228                         printf("ACE '%s': bad hex number at '%s'\n",
229                                 orig_str, tok);
230                         SAFE_FREE(str);
231                         return False;
232                 }
233                 goto done;
234         }
235
236         for (v = standard_values; v->perm; v++) {
237                 if (strcmp(tok, v->perm) == 0) {
238                         amask = v->mask;
239                         goto done;
240                 }
241         }
242
243         p = tok;
244
245         while(*p) {
246                 BOOL found = False;
247
248                 for (v = special_values; v->perm; v++) {
249                         if (v->perm[0] == *p) {
250                                 amask |= v->mask;
251                                 found = True;
252                         }
253                 }
254
255                 if (!found) {
256                         printf("ACE '%s': bad permission value at '%s'\n",
257                                 orig_str, p);
258                         SAFE_FREE(str);
259                         return False;
260                 }
261                 p++;
262         }
263
264         if (*p) {
265                 SAFE_FREE(str);
266                 return False;
267         }
268
269  done:
270         mask = amask;
271         init_sec_ace(ace, &sid, atype, mask, aflags);
272         SAFE_FREE(str);
273         return True;
274 }
275
276
277 /********************************************************************
278 ********************************************************************/
279
280 static SEC_DESC* parse_acl_string(TALLOC_CTX *mem_ctx, const char *szACL, size_t *sd_size )
281 {
282         SEC_DESC *sd = NULL;
283         SEC_ACE *ace;
284         SEC_ACL *acl;
285         int num_ace;
286         const char *pacl;
287         int i;
288         
289         if ( !szACL )
290                 return NULL;
291
292         pacl = szACL;
293         num_ace = count_chars( pacl, ',' ) + 1;
294         
295         if ( !(ace = TALLOC_ZERO_ARRAY( mem_ctx, SEC_ACE, num_ace )) ) 
296                 return NULL;
297         
298         for ( i=0; i<num_ace; i++ ) {
299                 char *end_acl = strchr_m( pacl, ',' );
300                 fstring acl_string;
301
302                 strncpy( acl_string, pacl, MIN( PTR_DIFF( end_acl, pacl ), sizeof(fstring)-1) );
303                 acl_string[MIN( PTR_DIFF( end_acl, pacl ), sizeof(fstring)-1)] = '\0';
304                 
305                 if ( !parse_ace( &ace[i], acl_string ) )
306                         return NULL;
307
308                 pacl = end_acl;
309                 pacl++;
310         }
311         
312         if ( !(acl = make_sec_acl( mem_ctx, NT4_ACL_REVISION, num_ace, ace )) )
313                 return NULL;
314                 
315         sd = make_sec_desc( mem_ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, 
316                 NULL, NULL, NULL, acl, sd_size);
317
318         return sd;
319 }
320
321 /* add an ACE to a list of ACEs in a SEC_ACL */
322 static BOOL add_ace(TALLOC_CTX *mem_ctx, SEC_ACL **the_acl, SEC_ACE *ace)
323 {
324         SEC_ACL *new_ace;
325         SEC_ACE *aces;
326         if (! *the_acl) {
327                 return (((*the_acl) = make_sec_acl(mem_ctx, 3, 1, ace)) != NULL);
328         }
329
330         if (!(aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces))) {
331                 return False;
332         }
333         memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(SEC_ACE));
334         memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
335         new_ace = make_sec_acl(mem_ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
336         SAFE_FREE(aces);
337         (*the_acl) = new_ace;
338         return True;
339 }
340
341 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
342    However NT4 gives a "The information may have been modified by a
343    computer running Windows NT 5.0" if denied ACEs do not appear before
344    allowed ACEs. */
345
346 static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
347 {
348         if (sec_ace_equal(ace1, ace2)) 
349                 return 0;
350
351         if (ace1->type != ace2->type) 
352                 return ace2->type - ace1->type;
353
354         if (sid_compare(&ace1->trustee, &ace2->trustee)) 
355                 return sid_compare(&ace1->trustee, &ace2->trustee);
356
357         if (ace1->flags != ace2->flags) 
358                 return ace1->flags - ace2->flags;
359
360         if (ace1->access_mask != ace2->access_mask) 
361                 return ace1->access_mask - ace2->access_mask;
362
363         if (ace1->size != ace2->size) 
364                 return ace1->size - ace2->size;
365
366         return memcmp(ace1, ace2, sizeof(SEC_ACE));
367 }
368
369 static void sort_acl(SEC_ACL *the_acl)
370 {
371         uint32 i;
372         if (!the_acl) return;
373
374         qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]), QSORT_CAST ace_compare);
375
376         for (i=1;i<the_acl->num_aces;) {
377                 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
378                         int j;
379                         for (j=i; j<the_acl->num_aces-1; j++) {
380                                 the_acl->aces[j] = the_acl->aces[j+1];
381                         }
382                         the_acl->num_aces--;
383                 } else {
384                         i++;
385                 }
386         }
387 }
388
389
390 static int change_share_sec(TALLOC_CTX *mem_ctx, const char *sharename, char *the_acl, enum acl_mode mode)
391 {
392         SEC_DESC *sd;
393         SEC_DESC *old = NULL;
394         size_t sd_size = 0;
395         uint32 i, j;
396         
397         if (mode != SMB_ACL_SET) {
398             if (!(old = get_share_security( mem_ctx, sharename, &sd_size )) ) {
399                 fprintf(stderr, "Unable to retrieve permissions for share [%s]\n", sharename);
400                 return -1;
401             }
402         }
403
404         if ( (mode != SMB_ACL_VIEW) && !(sd = parse_acl_string(mem_ctx, the_acl, &sd_size )) ) {
405                 fprintf( stderr, "Failed to parse acl\n");
406                 return -1;
407         }
408         
409         switch (mode) {
410         case SMB_ACL_VIEW:
411                 sec_desc_print( stdout, old);
412                 return 0;
413         case SMB_ACL_DELETE:
414             for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
415                 BOOL found = False;
416
417                 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
418                     if (sec_ace_equal(&sd->dacl->aces[i], &old->dacl->aces[j])) {
419                         uint32 k;
420                         for (k=j; k<old->dacl->num_aces-1;k++) {
421                             old->dacl->aces[k] = old->dacl->aces[k+1];
422                         }
423                         old->dacl->num_aces--;
424                         found = True;
425                         break;
426                     }
427                 }
428
429                 if (!found) {
430                 printf("ACL for ACE:");
431                 print_ace(stdout, &sd->dacl->aces[i]);
432                 printf(" not found\n");
433                 }
434             }
435
436             break;
437         case SMB_ACL_MODIFY:
438             for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
439                 BOOL found = False;
440
441                 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
442                     if (sid_equal(&sd->dacl->aces[i].trustee,
443                         &old->dacl->aces[j].trustee)) {
444                         old->dacl->aces[j] = sd->dacl->aces[i];
445                         found = True;
446                     }
447                 }
448
449                 if (!found) {
450                     printf("ACL for SID %s not found\n", sid_string_static(&sd->dacl->aces[i].trustee));
451                 }
452             }
453
454             if (sd->owner_sid) {
455                 old->owner_sid = sd->owner_sid;
456             }
457
458             if (sd->group_sid) {
459                 old->group_sid = sd->group_sid;
460             }
461             break;
462         case SMB_ACL_ADD:
463             for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
464                 add_ace(mem_ctx, &old->dacl, &sd->dacl->aces[i]);
465             }
466             break;
467         case SMB_ACL_SET:
468             old = sd;
469             break;
470         }
471
472         /* Denied ACE entries must come before allowed ones */
473         sort_acl(old->dacl);
474
475         if ( !set_share_security( sharename, old ) ) {
476             fprintf( stderr, "Failed to store acl for share [%s]\n", sharename );
477             return 2;
478         }
479         return 0;
480 }
481
482 /********************************************************************
483   main program
484 ********************************************************************/
485
486 int main(int argc, const char *argv[])
487 {
488         int opt;
489         int retval = 0;
490         enum acl_mode mode = SMB_ACL_SET;
491         static char *the_acl = NULL;
492         fstring sharename;
493         BOOL force_acl = False;
494         int snum;
495         poptContext pc;
496         BOOL initialize_sid = False;
497         struct poptOption long_options[] = {
498                 POPT_AUTOHELP
499                 { "remove", 'r', POPT_ARG_STRING, &the_acl, 'r', "Delete an ACE", "ACL" },
500                 { "modify", 'm', POPT_ARG_STRING, &the_acl, 'm', "Modify an acl", "ACL" },
501                 { "add", 'a', POPT_ARG_STRING, &the_acl, 'a', "Add an ACE", "ACL" },
502                 { "replace", 'R', POPT_ARG_STRING, &the_acl, 'R', "Set share mission ACL", "ACLS" },
503                 { "view", 'v', POPT_ARG_NONE, NULL, 'v', "View current share permissions" },
504                 { "machine-sid", 'M', POPT_ARG_NONE, NULL, 'M', "Initialize the machine SID" },
505                 { "force", 'F', POPT_ARG_NONE, NULL, 'F', "Force storing the ACL", "ACLS" },
506                 POPT_COMMON_SAMBA
507                 { NULL }
508         };
509
510         if ( !(ctx = talloc_init("main")) ) {
511                 fprintf( stderr, "Failed to initialize talloc context!\n");
512                 return -1;
513         }
514
515         /* set default debug level to 1 regardless of what smb.conf sets */
516         setup_logging( "sharesec", True );
517         DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
518         dbf = x_stderr;
519         x_setbuf( x_stderr, NULL );
520
521         pc = poptGetContext("sharesec", argc, argv, long_options, 0);
522         
523         poptSetOtherOptionHelp(pc, "sharename\n");
524
525         while ((opt = poptGetNextOpt(pc)) != -1) {
526                 switch (opt) {
527                 case 'r':
528                         the_acl = smb_xstrdup(poptGetOptArg(pc));
529                         mode = SMB_ACL_DELETE;
530                         break;
531
532                 case 'm':
533                         the_acl = smb_xstrdup(poptGetOptArg(pc));
534                         mode = SMB_ACL_MODIFY;
535                         break;
536
537                 case 'a':
538                         the_acl = smb_xstrdup(poptGetOptArg(pc));
539                         mode = SMB_ACL_ADD;
540                         break;
541                 case 'R':
542                         the_acl = smb_xstrdup(poptGetOptArg(pc));
543                         mode = SMB_ACL_SET;
544                         break;
545
546                 case 'v':
547                         mode = SMB_ACL_VIEW;
548                         break;
549
550                 case 'F':
551                         force_acl = True;
552                         break;
553                         
554                 case 'M':
555                         initialize_sid = True;
556                         break;
557                 }
558         }
559         
560         setlinebuf(stdout);
561
562         load_case_tables();
563
564         lp_load( dyn_CONFIGFILE, False, False, False, True );
565
566         /* check for initializing secrets.tdb first */
567         
568         if ( initialize_sid ) {
569                 DOM_SID *sid = get_global_sam_sid();
570                 
571                 if ( !sid ) {
572                         fprintf( stderr, "Failed to retrieve Machine SID!\n");
573                         return 3;
574                 }
575                 
576                 printf ("%s\n", sid_string_static( sid ) );
577                 return 0;
578         }
579
580         if ( mode == SMB_ACL_VIEW && force_acl ) {
581                 fprintf( stderr, "Invalid combination of -F and -v\n");
582                 return -1;
583         }
584
585         /* get the sharename */
586
587         if(!poptPeekArg(pc)) { 
588                 poptPrintUsage(pc, stderr, 0);  
589                 return -1;
590         }
591         
592         fstrcpy(sharename, poptGetArg(pc));
593         
594         snum = lp_servicenumber( sharename );
595         
596         if ( snum == -1 && !force_acl ) {
597                 fprintf( stderr, "Invalid sharename: %s\n", sharename);
598                 return -1;
599         }
600                 
601         retval = change_share_sec(ctx, sharename, the_acl, mode);
602         
603         talloc_destroy(ctx);
604
605         return retval;
606 }