r18745: Use the Samba4 data structures for security descriptors and security descriptor
[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 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
26 #include "includes.h"
27
28 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
29
30 /* numeric is set when the user wants numeric SIDs and ACEs rather
31    than going via LSA calls to resolve them */
32 static BOOL numeric = False;
33
34 enum acl_mode {SMB_ACL_REMOVE, SMB_ACL_MODIFY, SMB_ACL_ADD, SMB_ACL_REPLACE,  SMB_ACL_VIEW };
35 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
36
37 struct perm_value {
38         const char *perm;
39         uint32 mask;
40 };
41
42 /* These values discovered by inspection */
43
44 static const struct perm_value special_values[] = {
45         { "R", 0x00120089 },
46         { "W", 0x00120116 },
47         { "X", 0x001200a0 },
48         { "D", 0x00010000 },
49         { "P", 0x00040000 },
50         { "O", 0x00080000 },
51         { NULL, 0 },
52 };
53
54 static const struct perm_value standard_values[] = {
55         { "READ",   0x001200a9 },
56         { "CHANGE", 0x001301bf },
57         { "FULL",   0x001f01ff },
58         { NULL, 0 },
59 };
60
61 /********************************************************************
62  print an ACE on a FILE, using either numeric or ascii representation
63 ********************************************************************/
64
65 static void print_ace(FILE *f, SEC_ACE *ace)
66 {
67         const struct perm_value *v;
68         fstring sidstr;
69         int do_print = 0;
70         uint32 got_mask;
71
72         sid_to_string(sidstr, &ace->trustee);
73
74         fprintf(f, "%s:", sidstr);
75
76         if (numeric) {
77                 fprintf(f, "%d/%d/0x%08x", 
78                         ace->type, ace->flags, ace->access_mask);
79                 return;
80         }
81
82         /* Ace type */
83
84         if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
85                 fprintf(f, "ALLOWED");
86         } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
87                 fprintf(f, "DENIED");
88         } else {
89                 fprintf(f, "%d", ace->type);
90         }
91
92         /* Not sure what flags can be set in a file ACL */
93
94         fprintf(f, "/%d/", ace->flags);
95
96         /* Standard permissions */
97
98         for (v = standard_values; v->perm; v++) {
99                 if (ace->access_mask == v->mask) {
100                         fprintf(f, "%s", v->perm);
101                         return;
102                 }
103         }
104
105         /* Special permissions.  Print out a hex value if we have
106            leftover bits in the mask. */
107
108         got_mask = ace->access_mask;
109
110  again:
111         for (v = special_values; v->perm; v++) {
112                 if ((ace->access_mask & v->mask) == v->mask) {
113                         if (do_print) {
114                                 fprintf(f, "%s", v->perm);
115                         }
116                         got_mask &= ~v->mask;
117                 }
118         }
119
120         if (!do_print) {
121                 if (got_mask != 0) {
122                         fprintf(f, "0x%08x", ace->access_mask);
123                 } else {
124                         do_print = 1;
125                         goto again;
126                 }
127         }
128 }
129
130 /********************************************************************
131  print a ascii version of a security descriptor on a FILE handle
132 ********************************************************************/
133
134 static void sec_desc_print(FILE *f, SEC_DESC *sd)
135 {
136         fstring sidstr;
137         uint32 i;
138
139         fprintf(f, "REVISION:%d\n", sd->revision);
140
141         /* Print owner and group sid */
142
143         if (sd->owner_sid) {
144                 sid_to_string(sidstr, sd->owner_sid);
145         } else {
146                 fstrcpy(sidstr, "");
147         }
148
149         fprintf(f, "OWNER:%s\n", sidstr);
150
151         if (sd->group_sid) {
152                 sid_to_string(sidstr, sd->group_sid);
153         } else {
154                 fstrcpy(sidstr, "");
155         }
156
157         fprintf(f, "GROUP:%s\n", sidstr);
158
159         /* Print aces */
160         for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
161                 SEC_ACE *ace = &sd->dacl->aces[i];
162                 fprintf(f, "ACL:");
163                 print_ace(f, ace);
164                 fprintf(f, "\n");
165         }
166
167 }
168
169 /********************************************************************
170 ********************************************************************/
171
172 static BOOL parse_ace( TALLOC_CTX *ctx, SEC_ACE *ace, char *entry )
173 {
174         SEC_ACCESS sa;
175         char *p = strchr_m( entry, ':' );
176         DOM_SID sid;
177         uint32 mask;
178         
179         if ( !p )
180                 return False;
181                 
182         *p = '\0';
183         p++;
184         
185         string_to_sid( &sid, entry );
186         
187         switch ( *p ) {
188                 case 'F':
189                 case 'f':
190                         mask = GENERIC_RIGHTS_FILE_ALL_ACCESS|STD_RIGHT_ALL_ACCESS;
191                         break;
192
193                 case 'R':
194                 case 'r':
195                         mask = GENERIC_RIGHTS_FILE_READ|GENERIC_RIGHTS_FILE_EXECUTE|\
196                                 STANDARD_RIGHTS_READ_ACCESS|STANDARD_RIGHTS_EXECUTE_ACCESS;
197                         break;
198
199                 default:
200                         return False;
201         }
202         
203         init_sec_access( &sa, mask );
204
205         /* no flags on share permissions */
206         init_sec_ace( ace, &sid, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0 );
207         
208         return True;
209 }
210
211
212 /********************************************************************
213 ********************************************************************/
214
215 static SEC_DESC* parse_acl_string( TALLOC_CTX *ctx, const char *szACL, size_t *sd_size )
216 {
217         SEC_DESC *sd = NULL;
218         SEC_ACE *ace;
219         SEC_ACL *acl;
220         int num_ace;
221         const char *pacl;
222         int i;
223         
224         if ( !szACL )
225                 return NULL;
226
227         pacl = szACL;
228         num_ace = count_chars( pacl, ',' ) + 1;
229         
230         if ( !(ace = TALLOC_ZERO_ARRAY( ctx, SEC_ACE, num_ace )) ) 
231                 return NULL;
232         
233         for ( i=0; i<num_ace; i++ ) {
234                 char *end_acl = strchr_m( pacl, ',' );
235                 fstring acl_string;
236
237                 strncpy( acl_string, pacl, MIN( PTR_DIFF( end_acl, pacl ), sizeof(fstring)-1) );
238                 acl_string[MIN( PTR_DIFF( end_acl, pacl ), sizeof(fstring)-1)] = '\0';
239                 
240                 if ( !parse_ace( ctx, &ace[i], acl_string ) )
241                         return NULL;
242
243                 pacl = end_acl;
244                 pacl++;
245         }
246         
247         if ( !(acl = make_sec_acl( ctx, NT4_ACL_REVISION, num_ace, ace )) )
248                 return NULL;
249                 
250         sd = make_sec_desc( ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, 
251                 &global_sid_Builtin_Administrators,
252                 &global_sid_Builtin_Administrators,
253                 NULL, acl, sd_size);
254
255         return sd;
256 }
257
258 /********************************************************************
259   main program
260 ********************************************************************/
261
262 int main(int argc, const char *argv[])
263 {
264         int opt;
265         enum acl_mode mode = SMB_ACL_REPLACE;
266         static char *the_acl = NULL;
267         fstring sharename;
268         BOOL force_acl = False;
269         size_t sd_size = 0;
270         SEC_DESC *secdesc;
271         int snum;
272         poptContext pc;
273         TALLOC_CTX *ctx;
274         BOOL initialize_sid = False;
275         struct poptOption long_options[] = {
276                 POPT_AUTOHELP
277 #if 0
278                 { "remove", 'r', POPT_ARG_STRING, NULL, 'r', "Delete an ACE", "ACL" },
279                 { "modify", 'm', POPT_ARG_STRING, NULL, 'm', "Modify an acl", "ACL" },
280                 { "add", 'a', POPT_ARG_STRING, NULL, 'a', "Add an ACE", "ACL" },
281 #endif
282                 { "replace", 'R', POPT_ARG_STRING, NULL, 'R', "Set share mission ACL", "ACLS" },
283                 { "view", 'v', POPT_ARG_NONE, NULL, 'v', "View current share permissions" },
284                 { "machine-sid", 'M', POPT_ARG_NONE, NULL, 'M', "Initialize the machine SID" },
285                 { "force", 'F', POPT_ARG_NONE, NULL, 'F', "Force storing the ACL", "ACLS" },
286                 POPT_COMMON_SAMBA
287                 { NULL }
288         };
289
290         if ( !(ctx = talloc_init("main")) ) {
291                 fprintf( stderr, "Failed to initialize talloc context!\n");
292                 return -1;
293         }
294
295         /* set default debug level to 1 regardless of what smb.conf sets */
296         setup_logging( "sharesec", True );
297         DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
298         dbf = x_stderr;
299         x_setbuf( x_stderr, NULL );
300
301         setlinebuf(stdout);
302
303         load_case_tables();
304
305         lp_load( dyn_CONFIGFILE, False, False, False, True );
306
307         pc = poptGetContext("smbcacls", argc, argv, long_options, 0);
308         
309         poptSetOtherOptionHelp(pc, "sharename\n");
310
311         while ((opt = poptGetNextOpt(pc)) != -1) {
312                 switch (opt) {
313 #if 0
314                 case 'r':
315                         the_acl = smb_xstrdup(poptGetOptArg(pc));
316                         mode = SMB_ACL_REMOVE;
317                         break;
318
319                 case 'm':
320                         the_acl = smb_xstrdup(poptGetOptArg(pc));
321                         mode = SMB_ACL_MODIFY;
322                         break;
323
324                 case 'a':
325                         the_acl = smb_xstrdup(poptGetOptArg(pc));
326                         mode = SMB_ACL_ADD;
327                         break;
328 #endif
329                 case 'R':
330                         the_acl = smb_xstrdup(poptGetOptArg(pc));
331                         mode = SMB_ACL_REPLACE;
332                         break;
333
334                 case 'v':
335                         mode = SMB_ACL_VIEW;
336                         break;
337
338                 case 'F':
339                         force_acl = True;
340                         break;
341                         
342                 case 'M':
343                         initialize_sid = True;
344                         break;
345                 }
346         }
347         
348         /* check for initializing secrets.tdb first */
349         
350         if ( initialize_sid ) {
351                 DOM_SID *sid = get_global_sam_sid();
352                 
353                 if ( !sid ) {
354                         fprintf( stderr, "Failed to retrieve Machine SID!\n");
355                         return 3;
356                 }
357                 
358                 printf ("%s\n", sid_string_static( sid ) );
359                 return 0;
360         }
361
362         if ( mode == SMB_ACL_VIEW && force_acl ) {
363                 fprintf( stderr, "Invalid combination of -F and -v\n");
364                 return -1;
365         }
366
367         /* get the sharename */
368
369         if(!poptPeekArg(pc)) { 
370                 poptPrintUsage(pc, stderr, 0);  
371                 return -1;
372         }
373         
374         fstrcpy(sharename, poptGetArg(pc));
375         
376         snum = lp_servicenumber( sharename );
377         
378         if ( snum == -1 && !force_acl ) {
379                 fprintf( stderr, "Invalid sharename: %s\n", sharename);
380                 return -1;
381         }
382                 
383         switch ( mode ) {
384                 case SMB_ACL_VIEW:
385                         if (!(secdesc = get_share_security( ctx, sharename,
386                                                             &sd_size )) ) {
387                                 fprintf(stderr, "Unable to retrieve permissions for share [%s]\n", sharename);
388                                 return -1;
389                         }
390                         sec_desc_print( stdout, secdesc );
391                         break;
392
393                 case SMB_ACL_REMOVE:
394                 case SMB_ACL_ADD:
395                 case SMB_ACL_MODIFY:
396                         printf( "Not implemented\n");
397                         break;
398
399                 case SMB_ACL_REPLACE:
400                         if ( !(secdesc = parse_acl_string( ctx, the_acl, &sd_size )) ) {
401                                 fprintf( stderr, "Failed to parse acl\n");
402                                 return -1;
403                         }
404                         
405                         if ( !set_share_security( ctx, lp_servicename(snum), secdesc ) ) {
406                                 fprintf( stderr, "Failed to store acl for share [%s]\n", sharename );
407                                 return 2;
408                         }
409                         break;
410         }
411
412         talloc_destroy(ctx);
413
414         return 0;
415 }