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