s3:utils: Use C99 initializer for poptOption in sharesec
[metze/samba/wip.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 struct cli_state;
25
26 #include "includes.h"
27 #include "popt_common.h"
28 #include "../libcli/security/security.h"
29 #include "passdb/machine_sid.h"
30 #include "util_sd.h"
31 #include "cmdline_contexts.h"
32
33 static TALLOC_CTX *ctx;
34
35 enum acl_mode { SMB_ACL_DELETE,
36                 SMB_ACL_MODIFY,
37                 SMB_ACL_ADD,
38                 SMB_ACL_SET,
39                 SMB_SD_DELETE,
40                 SMB_SD_SETSDDL,
41                 SMB_SD_VIEWSDDL,
42                 SMB_ACL_VIEW,
43                 SMB_ACL_VIEW_ALL };
44
45 /********************************************************************
46 ********************************************************************/
47
48 static struct security_descriptor* parse_acl_string(TALLOC_CTX *mem_ctx, const char *szACL, size_t *sd_size )
49 {
50         struct security_descriptor *sd = NULL;
51         struct security_ace *ace;
52         struct security_acl *theacl;
53         int num_ace;
54         const char *pacl;
55         int i;
56
57         if ( !szACL )
58                 return NULL;
59
60         pacl = szACL;
61         num_ace = count_chars( pacl, ',' ) + 1;
62
63         if ( !(ace = talloc_zero_array( mem_ctx, struct security_ace, num_ace )) )
64                 return NULL;
65
66         for ( i=0; i<num_ace; i++ ) {
67                 char *end_acl = strchr_m( pacl, ',' );
68                 fstring acl_string;
69
70                 strncpy( acl_string, pacl, MIN( PTR_DIFF( end_acl, pacl ), sizeof(fstring)-1) );
71                 acl_string[MIN( PTR_DIFF( end_acl, pacl ), sizeof(fstring)-1)] = '\0';
72
73                 if ( !parse_ace(NULL, &ace[i], acl_string ) )
74                         return NULL;
75
76                 pacl = end_acl;
77                 pacl++;
78         }
79
80         if ( !(theacl = make_sec_acl( mem_ctx, NT4_ACL_REVISION, num_ace, ace )) )
81                 return NULL;
82
83         sd = make_sec_desc( mem_ctx, SD_REVISION, SEC_DESC_SELF_RELATIVE,
84                 NULL, NULL, NULL, theacl, sd_size);
85
86         return sd;
87 }
88
89 /* add an ACE to a list of ACEs in a struct security_acl */
90 static bool add_ace(TALLOC_CTX *mem_ctx, struct security_acl **the_acl, struct security_ace *ace)
91 {
92         struct security_acl *new_ace;
93         struct security_ace *aces;
94         if (! *the_acl) {
95                 return (((*the_acl) = make_sec_acl(mem_ctx, 3, 1, ace)) != NULL);
96         }
97
98         if (!(aces = SMB_CALLOC_ARRAY(struct security_ace, 1+(*the_acl)->num_aces))) {
99                 return False;
100         }
101         memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(struct
102         security_ace));
103         memcpy(aces+(*the_acl)->num_aces, ace, sizeof(struct security_ace));
104         new_ace = make_sec_acl(mem_ctx,(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
105         SAFE_FREE(aces);
106         (*the_acl) = new_ace;
107         return True;
108 }
109
110 /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
111    However NT4 gives a "The information may have been modified by a
112    computer running Windows NT 5.0" if denied ACEs do not appear before
113    allowed ACEs. */
114
115 static int ace_compare(struct security_ace *ace1, struct security_ace *ace2)
116 {
117         if (security_ace_equal(ace1, ace2))
118                 return 0;
119
120         if (ace1->type != ace2->type)
121                 return ace2->type - ace1->type;
122
123         if (dom_sid_compare(&ace1->trustee, &ace2->trustee))
124                 return dom_sid_compare(&ace1->trustee, &ace2->trustee);
125
126         if (ace1->flags != ace2->flags)
127                 return ace1->flags - ace2->flags;
128
129         if (ace1->access_mask != ace2->access_mask)
130                 return ace1->access_mask - ace2->access_mask;
131
132         if (ace1->size != ace2->size)
133                 return ace1->size - ace2->size;
134
135         return memcmp(ace1, ace2, sizeof(struct security_ace));
136 }
137
138 static void sort_acl(struct security_acl *the_acl)
139 {
140         uint32_t i;
141         if (!the_acl) return;
142
143         TYPESAFE_QSORT(the_acl->aces, the_acl->num_aces, ace_compare);
144
145         for (i=1;i<the_acl->num_aces;) {
146                 if (security_ace_equal(&the_acl->aces[i-1],
147                                        &the_acl->aces[i])) {
148                         int j;
149                         for (j=i; j<the_acl->num_aces-1; j++) {
150                                 the_acl->aces[j] = the_acl->aces[j+1];
151                         }
152                         the_acl->num_aces--;
153                 } else {
154                         i++;
155                 }
156         }
157 }
158
159
160 static int change_share_sec(TALLOC_CTX *mem_ctx, const char *sharename, char *the_acl, enum acl_mode mode)
161 {
162         struct security_descriptor *sd = NULL;
163         struct security_descriptor *old = NULL;
164         size_t sd_size = 0;
165         uint32_t i, j;
166
167         if (mode != SMB_ACL_SET && mode != SMB_SD_DELETE) {
168             if (!(old = get_share_security( mem_ctx, sharename, &sd_size )) ) {
169                 fprintf(stderr, "Unable to retrieve permissions for share "
170                         "[%s]\n", sharename);
171                 return -1;
172             }
173         }
174
175         if ( (mode != SMB_ACL_VIEW && mode != SMB_SD_DELETE) &&
176             !(sd = parse_acl_string(mem_ctx, the_acl, &sd_size )) ) {
177                 fprintf( stderr, "Failed to parse acl\n");
178                 return -1;
179         }
180
181         switch (mode) {
182         case SMB_ACL_VIEW_ALL:
183                 /* should not happen */
184                 return 0;
185         case SMB_ACL_VIEW:
186                 sec_desc_print(NULL, stdout, old, false);
187                 return 0;
188         case SMB_ACL_DELETE:
189             for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
190                 bool found = False;
191
192                 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
193                     if (security_ace_equal(&sd->dacl->aces[i],
194                                            &old->dacl->aces[j])) {
195                         uint32_t k;
196                         for (k=j; k<old->dacl->num_aces-1;k++) {
197                             old->dacl->aces[k] = old->dacl->aces[k+1];
198                         }
199                         old->dacl->num_aces--;
200                         found = True;
201                         break;
202                     }
203                 }
204
205                 if (!found) {
206                         printf("ACL for ACE:");
207                         print_ace(NULL, stdout, &sd->dacl->aces[i], false);
208                         printf(" not found\n");
209                 }
210             }
211             break;
212         case SMB_ACL_MODIFY:
213             for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
214                 bool found = False;
215
216                 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
217                     if (dom_sid_equal(&sd->dacl->aces[i].trustee,
218                         &old->dacl->aces[j].trustee)) {
219                         old->dacl->aces[j] = sd->dacl->aces[i];
220                         found = True;
221                     }
222                 }
223
224                 if (!found) {
225                     struct dom_sid_buf buf;
226                     printf("ACL for SID %s not found\n",
227                            dom_sid_str_buf(&sd->dacl->aces[i].trustee, &buf));
228                 }
229             }
230
231             if (sd->owner_sid) {
232                 old->owner_sid = sd->owner_sid;
233             }
234
235             if (sd->group_sid) {
236                 old->group_sid = sd->group_sid;
237             }
238             break;
239         case SMB_ACL_ADD:
240             for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
241                 add_ace(mem_ctx, &old->dacl, &sd->dacl->aces[i]);
242             }
243             break;
244         case SMB_ACL_SET:
245             old = sd;
246             break;
247         case SMB_SD_DELETE:
248             if (!delete_share_security(sharename)) {
249                 fprintf( stderr, "Failed to delete security descriptor for "
250                          "share [%s]\n", sharename );
251                 return -1;
252             }
253             return 0;
254         default:
255                 fprintf(stderr, "invalid command\n");
256                 return -1;
257         }
258
259         /* Denied ACE entries must come before allowed ones */
260         sort_acl(old->dacl);
261
262         if ( !set_share_security( sharename, old ) ) {
263             fprintf( stderr, "Failed to store acl for share [%s]\n", sharename );
264             return 2;
265         }
266         return 0;
267 }
268
269 static int set_sharesec_sddl(const char *sharename, const char *sddl)
270 {
271         struct security_descriptor *sd;
272         bool ret;
273
274         sd = sddl_decode(talloc_tos(), sddl, get_global_sam_sid());
275         if (sd == NULL) {
276                 fprintf(stderr, "Failed to parse acl\n");
277                 return -1;
278         }
279
280         ret = set_share_security(sharename, sd);
281         TALLOC_FREE(sd);
282         if (!ret) {
283                 fprintf(stderr, "Failed to store acl for share [%s]\n",
284                         sharename);
285                 return -1;
286         }
287
288         return 0;
289 }
290
291 static int view_sharesec_sddl(const char *sharename)
292 {
293         struct security_descriptor *sd;
294         size_t sd_size;
295         char *acl;
296
297         sd = get_share_security(talloc_tos(), sharename, &sd_size);
298         if (sd == NULL) {
299                 fprintf(stderr, "Unable to retrieve permissions for share "
300                         "[%s]\n", sharename);
301                 return -1;
302         }
303
304         acl = sddl_encode(talloc_tos(), sd, get_global_sam_sid());
305         TALLOC_FREE(sd);
306         if (acl == NULL) {
307                 fprintf(stderr, "Unable to sddl-encode permissions for share "
308                         "[%s]\n", sharename);
309                 return -1;
310         }
311         printf("%s\n", acl);
312         TALLOC_FREE(acl);
313         return 0;
314 }
315
316 /********************************************************************
317   main program
318 ********************************************************************/
319
320 enum {
321         OPT_VIEW_ALL = 1000,
322 };
323
324 int main(int argc, const char *argv[])
325 {
326         int opt;
327         int retval = 0;
328         enum acl_mode mode = SMB_ACL_SET;
329         static char *the_acl = NULL;
330         fstring sharename;
331         bool force_acl = False;
332         int snum;
333         poptContext pc;
334         bool initialize_sid = False;
335         struct poptOption long_options[] = {
336                 POPT_AUTOHELP
337                 {
338                         .longName   = "remove",
339                         .shortName  = 'r',
340                         .argInfo    = POPT_ARG_STRING,
341                         .arg        = &the_acl,
342                         .val        = 'r',
343                         .descrip    = "Remove ACEs",
344                         .argDescrip = "ACL",
345                 },
346                 {
347                         .longName   = "modify",
348                         .shortName  = 'm',
349                         .argInfo    = POPT_ARG_STRING,
350                         .arg        = &the_acl,
351                         .val        = 'm',
352                         .descrip    = "Modify existing ACEs",
353                         .argDescrip = "ACL",
354                 },
355                 {
356                         .longName   = "add",
357                         .shortName  = 'a',
358                         .argInfo    = POPT_ARG_STRING,
359                         .arg        = &the_acl,
360                         .val        = 'a',
361                         .descrip    = "Add ACEs",
362                         .argDescrip = "ACL",
363                 },
364                 {
365                         .longName   = "replace",
366                         .shortName  = 'R',
367                         .argInfo    = POPT_ARG_STRING,
368                         .arg        = &the_acl,
369                         .val        = 'R',
370                         .descrip    = "Overwrite share permission ACL",
371                         .argDescrip = "ACLS",
372                 },
373                 {
374                         .longName   = "delete",
375                         .shortName  = 'D',
376                         .argInfo    = POPT_ARG_NONE,
377                         .arg        = NULL,
378                         .val        = 'D',
379                         .descrip    = "Delete the entire security descriptor",
380                 },
381                 {
382                         .longName   = "setsddl",
383                         .shortName  = 'S',
384                         .argInfo    = POPT_ARG_STRING,
385                         .arg        = the_acl,
386                         .val        = 'S',
387                         .descrip    = "Set the SD in sddl format",
388                 },
389                 {
390                         .longName   = "viewsddl",
391                         .shortName  = 'V',
392                         .argInfo    = POPT_ARG_NONE,
393                         .arg        = the_acl,
394                         .val        = 'V',
395                         .descrip    = "View the SD in sddl format",
396                 },
397                 {
398                         .longName   = "view",
399                         .shortName  = 'v',
400                         .argInfo    = POPT_ARG_NONE,
401                         .arg        = NULL,
402                         .val        = 'v',
403                         .descrip    = "View current share permissions",
404                 },
405                 {
406                         .longName   = "view-all",
407                         .shortName  = 0,
408                         .argInfo    = POPT_ARG_NONE,
409                         .arg        = NULL,
410                         .val        = OPT_VIEW_ALL,
411                         .descrip    = "View all current share permissions",
412                 },
413                 {
414                         .longName   = "machine-sid",
415                         .shortName  = 'M',
416                         .argInfo    = POPT_ARG_NONE,
417                         .arg        = NULL,
418                         .val        = 'M',
419                         .descrip    = "Initialize the machine SID",
420                 },
421                 {
422                         .longName   = "force",
423                         .shortName  = 'F',
424                         .argInfo    = POPT_ARG_NONE,
425                         .arg        = NULL,
426                         .val        = 'F',
427                         .descrip    = "Force storing the ACL",
428                         .argDescrip = "ACLS",
429                 },
430                 POPT_COMMON_SAMBA
431                 POPT_TABLEEND
432         };
433
434         if ( !(ctx = talloc_stackframe()) ) {
435                 fprintf( stderr, "Failed to initialize talloc context!\n");
436                 return -1;
437         }
438
439         /* set default debug level to 1 regardless of what smb.conf sets */
440         setup_logging( "sharesec", DEBUG_STDERR);
441
442         smb_init_locale();
443
444         lp_set_cmdline("log level", "1");
445
446         pc = poptGetContext("sharesec", argc, argv, long_options, 0);
447
448         poptSetOtherOptionHelp(pc, "sharename\n");
449
450         while ((opt = poptGetNextOpt(pc)) != -1) {
451                 switch (opt) {
452                 case 'r':
453                         the_acl = smb_xstrdup(poptGetOptArg(pc));
454                         mode = SMB_ACL_DELETE;
455                         break;
456
457                 case 'm':
458                         the_acl = smb_xstrdup(poptGetOptArg(pc));
459                         mode = SMB_ACL_MODIFY;
460                         break;
461
462                 case 'a':
463                         the_acl = smb_xstrdup(poptGetOptArg(pc));
464                         mode = SMB_ACL_ADD;
465                         break;
466
467                 case 'R':
468                         the_acl = smb_xstrdup(poptGetOptArg(pc));
469                         mode = SMB_ACL_SET;
470                         break;
471
472                 case 'D':
473                         mode = SMB_SD_DELETE;
474                         break;
475
476                 case 'S':
477                         mode = SMB_SD_SETSDDL;
478                         the_acl = smb_xstrdup(poptGetOptArg(pc));
479                         break;
480
481                 case 'V':
482                         mode = SMB_SD_VIEWSDDL;
483                         break;
484
485                 case 'v':
486                         mode = SMB_ACL_VIEW;
487                         break;
488
489                 case 'F':
490                         force_acl = True;
491                         break;
492
493                 case 'M':
494                         initialize_sid = True;
495                         break;
496                 case OPT_VIEW_ALL:
497                         mode = SMB_ACL_VIEW_ALL;
498                         break;
499                 }
500         }
501
502         setlinebuf(stdout);
503
504         cmdline_messaging_context(get_dyn_CONFIGFILE());
505         lp_load_with_registry_shares(get_dyn_CONFIGFILE());
506
507         /* check for initializing secrets.tdb first */
508
509         if ( initialize_sid ) {
510                 struct dom_sid *sid = get_global_sam_sid();
511                 struct dom_sid_buf buf;
512
513                 if ( !sid ) {
514                         fprintf( stderr, "Failed to retrieve Machine SID!\n");
515                         return 3;
516                 }
517
518                 printf ("%s\n", dom_sid_str_buf(sid, &buf) );
519                 return 0;
520         }
521
522         if ( mode == SMB_ACL_VIEW && force_acl ) {
523                 fprintf( stderr, "Invalid combination of -F and -v\n");
524                 return -1;
525         }
526
527         if (mode == SMB_ACL_VIEW_ALL) {
528                 int i;
529
530                 for (i=0; i<lp_numservices(); i++) {
531                         TALLOC_CTX *frame = talloc_stackframe();
532                         const char *service = lp_servicename(frame, i);
533
534                         if (service == NULL) {
535                                 continue;
536                         }
537
538                         printf("[%s]\n", service);
539                         change_share_sec(frame, service, NULL, SMB_ACL_VIEW);
540                         printf("\n");
541                         TALLOC_FREE(frame);
542                 }
543                 goto done;
544         }
545
546         /* get the sharename */
547
548         if(!poptPeekArg(pc)) {
549                 poptPrintUsage(pc, stderr, 0);
550                 return -1;
551         }
552
553         fstrcpy(sharename, poptGetArg(pc));
554
555         snum = lp_servicenumber( sharename );
556
557         if ( snum == -1 && !force_acl ) {
558                 fprintf( stderr, "Invalid sharename: %s\n", sharename);
559                 return -1;
560         }
561
562         switch (mode) {
563         case SMB_SD_SETSDDL:
564                 retval = set_sharesec_sddl(sharename, the_acl);
565                 break;
566         case SMB_SD_VIEWSDDL:
567                 retval = view_sharesec_sddl(sharename);
568                 break;
569         default:
570                 retval = change_share_sec(ctx, sharename, the_acl, mode);
571                 break;
572         }
573
574 done:
575         talloc_destroy(ctx);
576
577         return retval;
578 }