1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | ACL get/set utility
|
---|
4 |
|
---|
5 | Copyright (C) Andrew Tridgell 2000
|
---|
6 | Copyright (C) Tim Potter 2000
|
---|
7 | Copyright (C) Jeremy Allison 2000
|
---|
8 | Copyright (C) Jelmer Vernooij 2003
|
---|
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 | #include "includes.h"
|
---|
25 |
|
---|
26 | static int test_args = False;
|
---|
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 int numeric = False;
|
---|
33 |
|
---|
34 | enum acl_mode {SMB_ACL_SET, SMB_ACL_DELETE, SMB_ACL_MODIFY, SMB_ACL_ADD };
|
---|
35 | enum chown_mode {REQUEST_NONE, REQUEST_CHOWN, REQUEST_CHGRP};
|
---|
36 | enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
|
---|
37 |
|
---|
38 | struct perm_value {
|
---|
39 | const char *perm;
|
---|
40 | uint32 mask;
|
---|
41 | };
|
---|
42 |
|
---|
43 | /* These values discovered by inspection */
|
---|
44 |
|
---|
45 | static const struct perm_value special_values[] = {
|
---|
46 | { "R", 0x00120089 },
|
---|
47 | { "W", 0x00120116 },
|
---|
48 | { "X", 0x001200a0 },
|
---|
49 | { "D", 0x00010000 },
|
---|
50 | { "P", 0x00040000 },
|
---|
51 | { "O", 0x00080000 },
|
---|
52 | { NULL, 0 },
|
---|
53 | };
|
---|
54 |
|
---|
55 | static const struct perm_value standard_values[] = {
|
---|
56 | { "READ", 0x001200a9 },
|
---|
57 | { "CHANGE", 0x001301bf },
|
---|
58 | { "FULL", 0x001f01ff },
|
---|
59 | { NULL, 0 },
|
---|
60 | };
|
---|
61 |
|
---|
62 | /* Open cli connection and policy handle */
|
---|
63 |
|
---|
64 | static NTSTATUS cli_lsa_lookup_sid(struct cli_state *cli,
|
---|
65 | const DOM_SID *sid,
|
---|
66 | TALLOC_CTX *mem_ctx,
|
---|
67 | enum lsa_SidType *type,
|
---|
68 | char **domain, char **name)
|
---|
69 | {
|
---|
70 | uint16 orig_cnum = cli->cnum;
|
---|
71 | struct rpc_pipe_client *p;
|
---|
72 | struct policy_handle handle;
|
---|
73 | NTSTATUS status;
|
---|
74 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
75 | enum lsa_SidType *types;
|
---|
76 | char **domains;
|
---|
77 | char **names;
|
---|
78 |
|
---|
79 | if (!cli_send_tconX(cli, "IPC$", "?????", "", 0)) {
|
---|
80 | return cli_nt_error(cli);
|
---|
81 | }
|
---|
82 |
|
---|
83 | p = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &status);
|
---|
84 | if (p == NULL) {
|
---|
85 | goto fail;
|
---|
86 | }
|
---|
87 |
|
---|
88 | status = rpccli_lsa_open_policy(p, talloc_tos(), True,
|
---|
89 | GENERIC_EXECUTE_ACCESS, &handle);
|
---|
90 | if (!NT_STATUS_IS_OK(status)) {
|
---|
91 | goto fail;
|
---|
92 | }
|
---|
93 |
|
---|
94 | status = rpccli_lsa_lookup_sids(p, talloc_tos(), &handle, 1, sid,
|
---|
95 | &domains, &names, &types);
|
---|
96 | if (!NT_STATUS_IS_OK(status)) {
|
---|
97 | goto fail;
|
---|
98 | }
|
---|
99 |
|
---|
100 | *type = types[0];
|
---|
101 | *domain = talloc_move(mem_ctx, &domains[0]);
|
---|
102 | *name = talloc_move(mem_ctx, &names[0]);
|
---|
103 |
|
---|
104 | status = NT_STATUS_OK;
|
---|
105 | fail:
|
---|
106 | if (p != NULL) {
|
---|
107 | cli_rpc_pipe_close(p);
|
---|
108 | }
|
---|
109 | cli_tdis(cli);
|
---|
110 | cli->cnum = orig_cnum;
|
---|
111 | TALLOC_FREE(frame);
|
---|
112 | return status;
|
---|
113 | }
|
---|
114 |
|
---|
115 | static NTSTATUS cli_lsa_lookup_name(struct cli_state *cli,
|
---|
116 | const char *name,
|
---|
117 | enum lsa_SidType *type,
|
---|
118 | DOM_SID *sid)
|
---|
119 | {
|
---|
120 | uint16 orig_cnum = cli->cnum;
|
---|
121 | struct rpc_pipe_client *p;
|
---|
122 | struct policy_handle handle;
|
---|
123 | NTSTATUS status;
|
---|
124 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
125 | DOM_SID *sids;
|
---|
126 | enum lsa_SidType *types;
|
---|
127 |
|
---|
128 | if (!cli_send_tconX(cli, "IPC$", "?????", "", 0)) {
|
---|
129 | return cli_nt_error(cli);
|
---|
130 | }
|
---|
131 |
|
---|
132 | p = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &status);
|
---|
133 | if (p == NULL) {
|
---|
134 | goto fail;
|
---|
135 | }
|
---|
136 |
|
---|
137 | status = rpccli_lsa_open_policy(p, talloc_tos(), True,
|
---|
138 | GENERIC_EXECUTE_ACCESS, &handle);
|
---|
139 | if (!NT_STATUS_IS_OK(status)) {
|
---|
140 | goto fail;
|
---|
141 | }
|
---|
142 |
|
---|
143 | status = rpccli_lsa_lookup_names(p, talloc_tos(), &handle, 1, &name,
|
---|
144 | NULL, 1, &sids, &types);
|
---|
145 | if (!NT_STATUS_IS_OK(status)) {
|
---|
146 | goto fail;
|
---|
147 | }
|
---|
148 |
|
---|
149 | *type = types[0];
|
---|
150 | *sid = sids[0];
|
---|
151 |
|
---|
152 | status = NT_STATUS_OK;
|
---|
153 | fail:
|
---|
154 | if (p != NULL) {
|
---|
155 | cli_rpc_pipe_close(p);
|
---|
156 | }
|
---|
157 | cli_tdis(cli);
|
---|
158 | cli->cnum = orig_cnum;
|
---|
159 | TALLOC_FREE(frame);
|
---|
160 | return status;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /* convert a SID to a string, either numeric or username/group */
|
---|
164 | static void SidToString(struct cli_state *cli, fstring str, const DOM_SID *sid)
|
---|
165 | {
|
---|
166 | char *domain = NULL;
|
---|
167 | char *name = NULL;
|
---|
168 | enum lsa_SidType type;
|
---|
169 | NTSTATUS status;
|
---|
170 |
|
---|
171 | sid_to_fstring(str, sid);
|
---|
172 |
|
---|
173 | if (numeric) {
|
---|
174 | return;
|
---|
175 | }
|
---|
176 |
|
---|
177 | status = cli_lsa_lookup_sid(cli, sid, talloc_tos(), &type,
|
---|
178 | &domain, &name);
|
---|
179 |
|
---|
180 | if (!NT_STATUS_IS_OK(status)) {
|
---|
181 | return;
|
---|
182 | }
|
---|
183 |
|
---|
184 | slprintf(str, sizeof(fstring) - 1, "%s%s%s",
|
---|
185 | domain, lp_winbind_separator(), name);
|
---|
186 |
|
---|
187 | }
|
---|
188 |
|
---|
189 | /* convert a string to a SID, either numeric or username/group */
|
---|
190 | static bool StringToSid(struct cli_state *cli, DOM_SID *sid, const char *str)
|
---|
191 | {
|
---|
192 | enum lsa_SidType type;
|
---|
193 |
|
---|
194 | if (strncmp(str, "S-", 2) == 0) {
|
---|
195 | return string_to_sid(sid, str);
|
---|
196 | }
|
---|
197 |
|
---|
198 | return NT_STATUS_IS_OK(cli_lsa_lookup_name(cli, str, &type, sid));
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | /* print an ACE on a FILE, using either numeric or ascii representation */
|
---|
203 | static void print_ace(struct cli_state *cli, FILE *f, SEC_ACE *ace)
|
---|
204 | {
|
---|
205 | const struct perm_value *v;
|
---|
206 | fstring sidstr;
|
---|
207 | int do_print = 0;
|
---|
208 | uint32 got_mask;
|
---|
209 |
|
---|
210 | SidToString(cli, sidstr, &ace->trustee);
|
---|
211 |
|
---|
212 | fprintf(f, "%s:", sidstr);
|
---|
213 |
|
---|
214 | if (numeric) {
|
---|
215 | fprintf(f, "%d/%d/0x%08x",
|
---|
216 | ace->type, ace->flags, ace->access_mask);
|
---|
217 | return;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /* Ace type */
|
---|
221 |
|
---|
222 | if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
|
---|
223 | fprintf(f, "ALLOWED");
|
---|
224 | } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
|
---|
225 | fprintf(f, "DENIED");
|
---|
226 | } else {
|
---|
227 | fprintf(f, "%d", ace->type);
|
---|
228 | }
|
---|
229 |
|
---|
230 | /* Not sure what flags can be set in a file ACL */
|
---|
231 |
|
---|
232 | fprintf(f, "/%d/", ace->flags);
|
---|
233 |
|
---|
234 | /* Standard permissions */
|
---|
235 |
|
---|
236 | for (v = standard_values; v->perm; v++) {
|
---|
237 | if (ace->access_mask == v->mask) {
|
---|
238 | fprintf(f, "%s", v->perm);
|
---|
239 | return;
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | /* Special permissions. Print out a hex value if we have
|
---|
244 | leftover bits in the mask. */
|
---|
245 |
|
---|
246 | got_mask = ace->access_mask;
|
---|
247 |
|
---|
248 | again:
|
---|
249 | for (v = special_values; v->perm; v++) {
|
---|
250 | if ((ace->access_mask & v->mask) == v->mask) {
|
---|
251 | if (do_print) {
|
---|
252 | fprintf(f, "%s", v->perm);
|
---|
253 | }
|
---|
254 | got_mask &= ~v->mask;
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | if (!do_print) {
|
---|
259 | if (got_mask != 0) {
|
---|
260 | fprintf(f, "0x%08x", ace->access_mask);
|
---|
261 | } else {
|
---|
262 | do_print = 1;
|
---|
263 | goto again;
|
---|
264 | }
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 |
|
---|
269 | /* parse an ACE in the same format as print_ace() */
|
---|
270 | static bool parse_ace(struct cli_state *cli, SEC_ACE *ace,
|
---|
271 | const char *orig_str)
|
---|
272 | {
|
---|
273 | char *p;
|
---|
274 | const char *cp;
|
---|
275 | char *tok;
|
---|
276 | unsigned int atype = 0;
|
---|
277 | unsigned int aflags = 0;
|
---|
278 | unsigned int amask = 0;
|
---|
279 | DOM_SID sid;
|
---|
280 | SEC_ACCESS mask;
|
---|
281 | const struct perm_value *v;
|
---|
282 | char *str = SMB_STRDUP(orig_str);
|
---|
283 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
284 |
|
---|
285 | if (!str) {
|
---|
286 | TALLOC_FREE(frame);
|
---|
287 | return False;
|
---|
288 | }
|
---|
289 |
|
---|
290 | ZERO_STRUCTP(ace);
|
---|
291 | p = strchr_m(str,':');
|
---|
292 | if (!p) {
|
---|
293 | printf("ACE '%s': missing ':'.\n", orig_str);
|
---|
294 | SAFE_FREE(str);
|
---|
295 | TALLOC_FREE(frame);
|
---|
296 | return False;
|
---|
297 | }
|
---|
298 | *p = '\0';
|
---|
299 | p++;
|
---|
300 | /* Try to parse numeric form */
|
---|
301 |
|
---|
302 | if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
|
---|
303 | StringToSid(cli, &sid, str)) {
|
---|
304 | goto done;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /* Try to parse text form */
|
---|
308 |
|
---|
309 | if (!StringToSid(cli, &sid, str)) {
|
---|
310 | printf("ACE '%s': failed to convert '%s' to SID\n",
|
---|
311 | orig_str, str);
|
---|
312 | SAFE_FREE(str);
|
---|
313 | TALLOC_FREE(frame);
|
---|
314 | return False;
|
---|
315 | }
|
---|
316 |
|
---|
317 | cp = p;
|
---|
318 | if (!next_token_talloc(frame, &cp, &tok, "/")) {
|
---|
319 | printf("ACE '%s': failed to find '/' character.\n",
|
---|
320 | orig_str);
|
---|
321 | SAFE_FREE(str);
|
---|
322 | TALLOC_FREE(frame);
|
---|
323 | return False;
|
---|
324 | }
|
---|
325 |
|
---|
326 | if (strncmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
|
---|
327 | atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
|
---|
328 | } else if (strncmp(tok, "DENIED", strlen("DENIED")) == 0) {
|
---|
329 | atype = SEC_ACE_TYPE_ACCESS_DENIED;
|
---|
330 | } else {
|
---|
331 | printf("ACE '%s': missing 'ALLOWED' or 'DENIED' entry at '%s'\n",
|
---|
332 | orig_str, tok);
|
---|
333 | SAFE_FREE(str);
|
---|
334 | TALLOC_FREE(frame);
|
---|
335 | return False;
|
---|
336 | }
|
---|
337 |
|
---|
338 | /* Only numeric form accepted for flags at present */
|
---|
339 |
|
---|
340 | if (!(next_token_talloc(frame, &cp, &tok, "/") &&
|
---|
341 | sscanf(tok, "%i", &aflags))) {
|
---|
342 | printf("ACE '%s': bad integer flags entry at '%s'\n",
|
---|
343 | orig_str, tok);
|
---|
344 | SAFE_FREE(str);
|
---|
345 | TALLOC_FREE(frame);
|
---|
346 | return False;
|
---|
347 | }
|
---|
348 |
|
---|
349 | if (!next_token_talloc(frame, &cp, &tok, "/")) {
|
---|
350 | printf("ACE '%s': missing / at '%s'\n",
|
---|
351 | orig_str, tok);
|
---|
352 | SAFE_FREE(str);
|
---|
353 | TALLOC_FREE(frame);
|
---|
354 | return False;
|
---|
355 | }
|
---|
356 |
|
---|
357 | if (strncmp(tok, "0x", 2) == 0) {
|
---|
358 | if (sscanf(tok, "%i", &amask) != 1) {
|
---|
359 | printf("ACE '%s': bad hex number at '%s'\n",
|
---|
360 | orig_str, tok);
|
---|
361 | SAFE_FREE(str);
|
---|
362 | TALLOC_FREE(frame);
|
---|
363 | return False;
|
---|
364 | }
|
---|
365 | goto done;
|
---|
366 | }
|
---|
367 |
|
---|
368 | for (v = standard_values; v->perm; v++) {
|
---|
369 | if (strcmp(tok, v->perm) == 0) {
|
---|
370 | amask = v->mask;
|
---|
371 | goto done;
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | p = tok;
|
---|
376 |
|
---|
377 | while(*p) {
|
---|
378 | bool found = False;
|
---|
379 |
|
---|
380 | for (v = special_values; v->perm; v++) {
|
---|
381 | if (v->perm[0] == *p) {
|
---|
382 | amask |= v->mask;
|
---|
383 | found = True;
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | if (!found) {
|
---|
388 | printf("ACE '%s': bad permission value at '%s'\n",
|
---|
389 | orig_str, p);
|
---|
390 | SAFE_FREE(str);
|
---|
391 | TALLOC_FREE(frame);
|
---|
392 | return False;
|
---|
393 | }
|
---|
394 | p++;
|
---|
395 | }
|
---|
396 |
|
---|
397 | if (*p) {
|
---|
398 | TALLOC_FREE(frame);
|
---|
399 | SAFE_FREE(str);
|
---|
400 | return False;
|
---|
401 | }
|
---|
402 |
|
---|
403 | done:
|
---|
404 | mask = amask;
|
---|
405 | init_sec_ace(ace, &sid, atype, mask, aflags);
|
---|
406 | TALLOC_FREE(frame);
|
---|
407 | SAFE_FREE(str);
|
---|
408 | return True;
|
---|
409 | }
|
---|
410 |
|
---|
411 | /* add an ACE to a list of ACEs in a SEC_ACL */
|
---|
412 | static bool add_ace(SEC_ACL **the_acl, SEC_ACE *ace)
|
---|
413 | {
|
---|
414 | SEC_ACL *new_ace;
|
---|
415 | SEC_ACE *aces;
|
---|
416 | if (! *the_acl) {
|
---|
417 | return (((*the_acl) = make_sec_acl(talloc_tos(), 3, 1, ace))
|
---|
418 | != NULL);
|
---|
419 | }
|
---|
420 |
|
---|
421 | if (!(aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces))) {
|
---|
422 | return False;
|
---|
423 | }
|
---|
424 | memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(SEC_ACE));
|
---|
425 | memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
|
---|
426 | new_ace = make_sec_acl(talloc_tos(),(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
|
---|
427 | SAFE_FREE(aces);
|
---|
428 | (*the_acl) = new_ace;
|
---|
429 | return True;
|
---|
430 | }
|
---|
431 |
|
---|
432 | /* parse a ascii version of a security descriptor */
|
---|
433 | static SEC_DESC *sec_desc_parse(TALLOC_CTX *ctx, struct cli_state *cli, char *str)
|
---|
434 | {
|
---|
435 | const char *p = str;
|
---|
436 | char *tok;
|
---|
437 | SEC_DESC *ret = NULL;
|
---|
438 | size_t sd_size;
|
---|
439 | DOM_SID *grp_sid=NULL, *owner_sid=NULL;
|
---|
440 | SEC_ACL *dacl=NULL;
|
---|
441 | int revision=1;
|
---|
442 |
|
---|
443 | while (next_token_talloc(ctx, &p, &tok, "\t,\r\n")) {
|
---|
444 | if (strncmp(tok,"REVISION:", 9) == 0) {
|
---|
445 | revision = strtol(tok+9, NULL, 16);
|
---|
446 | continue;
|
---|
447 | }
|
---|
448 |
|
---|
449 | if (strncmp(tok,"OWNER:", 6) == 0) {
|
---|
450 | if (owner_sid) {
|
---|
451 | printf("Only specify owner once\n");
|
---|
452 | goto done;
|
---|
453 | }
|
---|
454 | owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
|
---|
455 | if (!owner_sid ||
|
---|
456 | !StringToSid(cli, owner_sid, tok+6)) {
|
---|
457 | printf("Failed to parse owner sid\n");
|
---|
458 | goto done;
|
---|
459 | }
|
---|
460 | continue;
|
---|
461 | }
|
---|
462 |
|
---|
463 | if (strncmp(tok,"GROUP:", 6) == 0) {
|
---|
464 | if (grp_sid) {
|
---|
465 | printf("Only specify group once\n");
|
---|
466 | goto done;
|
---|
467 | }
|
---|
468 | grp_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
|
---|
469 | if (!grp_sid ||
|
---|
470 | !StringToSid(cli, grp_sid, tok+6)) {
|
---|
471 | printf("Failed to parse group sid\n");
|
---|
472 | goto done;
|
---|
473 | }
|
---|
474 | continue;
|
---|
475 | }
|
---|
476 |
|
---|
477 | if (strncmp(tok,"ACL:", 4) == 0) {
|
---|
478 | SEC_ACE ace;
|
---|
479 | if (!parse_ace(cli, &ace, tok+4)) {
|
---|
480 | goto done;
|
---|
481 | }
|
---|
482 | if(!add_ace(&dacl, &ace)) {
|
---|
483 | printf("Failed to add ACL %s\n", tok);
|
---|
484 | goto done;
|
---|
485 | }
|
---|
486 | continue;
|
---|
487 | }
|
---|
488 |
|
---|
489 | printf("Failed to parse token '%s' in security descriptor,\n", tok);
|
---|
490 | goto done;
|
---|
491 | }
|
---|
492 |
|
---|
493 | ret = make_sec_desc(ctx,revision, SEC_DESC_SELF_RELATIVE, owner_sid, grp_sid,
|
---|
494 | NULL, dacl, &sd_size);
|
---|
495 |
|
---|
496 | done:
|
---|
497 | SAFE_FREE(grp_sid);
|
---|
498 | SAFE_FREE(owner_sid);
|
---|
499 |
|
---|
500 | return ret;
|
---|
501 | }
|
---|
502 |
|
---|
503 |
|
---|
504 | /* print a ascii version of a security descriptor on a FILE handle */
|
---|
505 | static void sec_desc_print(struct cli_state *cli, FILE *f, SEC_DESC *sd)
|
---|
506 | {
|
---|
507 | fstring sidstr;
|
---|
508 | uint32 i;
|
---|
509 |
|
---|
510 | fprintf(f, "REVISION:%d\n", sd->revision);
|
---|
511 |
|
---|
512 | /* Print owner and group sid */
|
---|
513 |
|
---|
514 | if (sd->owner_sid) {
|
---|
515 | SidToString(cli, sidstr, sd->owner_sid);
|
---|
516 | } else {
|
---|
517 | fstrcpy(sidstr, "");
|
---|
518 | }
|
---|
519 |
|
---|
520 | fprintf(f, "OWNER:%s\n", sidstr);
|
---|
521 |
|
---|
522 | if (sd->group_sid) {
|
---|
523 | SidToString(cli, sidstr, sd->group_sid);
|
---|
524 | } else {
|
---|
525 | fstrcpy(sidstr, "");
|
---|
526 | }
|
---|
527 |
|
---|
528 | fprintf(f, "GROUP:%s\n", sidstr);
|
---|
529 |
|
---|
530 | /* Print aces */
|
---|
531 | for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
|
---|
532 | SEC_ACE *ace = &sd->dacl->aces[i];
|
---|
533 | fprintf(f, "ACL:");
|
---|
534 | print_ace(cli, f, ace);
|
---|
535 | fprintf(f, "\n");
|
---|
536 | }
|
---|
537 |
|
---|
538 | }
|
---|
539 |
|
---|
540 | /*****************************************************
|
---|
541 | dump the acls for a file
|
---|
542 | *******************************************************/
|
---|
543 | static int cacl_dump(struct cli_state *cli, char *filename)
|
---|
544 | {
|
---|
545 | int result = EXIT_FAILED;
|
---|
546 | int fnum = -1;
|
---|
547 | SEC_DESC *sd;
|
---|
548 |
|
---|
549 | if (test_args)
|
---|
550 | return EXIT_OK;
|
---|
551 |
|
---|
552 | fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
|
---|
553 |
|
---|
554 | if (fnum == -1) {
|
---|
555 | printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
|
---|
556 | goto done;
|
---|
557 | }
|
---|
558 |
|
---|
559 | sd = cli_query_secdesc(cli, fnum, talloc_tos());
|
---|
560 |
|
---|
561 | if (!sd) {
|
---|
562 | printf("ERROR: secdesc query failed: %s\n", cli_errstr(cli));
|
---|
563 | goto done;
|
---|
564 | }
|
---|
565 |
|
---|
566 | sec_desc_print(cli, stdout, sd);
|
---|
567 |
|
---|
568 | result = EXIT_OK;
|
---|
569 |
|
---|
570 | done:
|
---|
571 | if (fnum != -1)
|
---|
572 | cli_close(cli, fnum);
|
---|
573 |
|
---|
574 | return result;
|
---|
575 | }
|
---|
576 |
|
---|
577 | /*****************************************************
|
---|
578 | Change the ownership or group ownership of a file. Just
|
---|
579 | because the NT docs say this can't be done :-). JRA.
|
---|
580 | *******************************************************/
|
---|
581 |
|
---|
582 | static int owner_set(struct cli_state *cli, enum chown_mode change_mode,
|
---|
583 | const char *filename, const char *new_username)
|
---|
584 | {
|
---|
585 | int fnum;
|
---|
586 | DOM_SID sid;
|
---|
587 | SEC_DESC *sd, *old;
|
---|
588 | size_t sd_size;
|
---|
589 |
|
---|
590 | fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
|
---|
591 |
|
---|
592 | if (fnum == -1) {
|
---|
593 | printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
|
---|
594 | return EXIT_FAILED;
|
---|
595 | }
|
---|
596 |
|
---|
597 | if (!StringToSid(cli, &sid, new_username))
|
---|
598 | return EXIT_PARSE_ERROR;
|
---|
599 |
|
---|
600 | old = cli_query_secdesc(cli, fnum, talloc_tos());
|
---|
601 |
|
---|
602 | cli_close(cli, fnum);
|
---|
603 |
|
---|
604 | if (!old) {
|
---|
605 | printf("owner_set: Failed to query old descriptor\n");
|
---|
606 | return EXIT_FAILED;
|
---|
607 | }
|
---|
608 |
|
---|
609 | sd = make_sec_desc(talloc_tos(),old->revision, old->type,
|
---|
610 | (change_mode == REQUEST_CHOWN) ? &sid : NULL,
|
---|
611 | (change_mode == REQUEST_CHGRP) ? &sid : NULL,
|
---|
612 | NULL, NULL, &sd_size);
|
---|
613 |
|
---|
614 | fnum = cli_nt_create(cli, filename, WRITE_OWNER_ACCESS);
|
---|
615 |
|
---|
616 | if (fnum == -1) {
|
---|
617 | printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
|
---|
618 | return EXIT_FAILED;
|
---|
619 | }
|
---|
620 |
|
---|
621 | if (!cli_set_secdesc(cli, fnum, sd)) {
|
---|
622 | printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
|
---|
623 | }
|
---|
624 |
|
---|
625 | cli_close(cli, fnum);
|
---|
626 |
|
---|
627 | return EXIT_OK;
|
---|
628 | }
|
---|
629 |
|
---|
630 |
|
---|
631 | /* The MSDN is contradictory over the ordering of ACE entries in an ACL.
|
---|
632 | However NT4 gives a "The information may have been modified by a
|
---|
633 | computer running Windows NT 5.0" if denied ACEs do not appear before
|
---|
634 | allowed ACEs. */
|
---|
635 |
|
---|
636 | static int ace_compare(SEC_ACE *ace1, SEC_ACE *ace2)
|
---|
637 | {
|
---|
638 | if (sec_ace_equal(ace1, ace2))
|
---|
639 | return 0;
|
---|
640 |
|
---|
641 | if (ace1->type != ace2->type)
|
---|
642 | return ace2->type - ace1->type;
|
---|
643 |
|
---|
644 | if (sid_compare(&ace1->trustee, &ace2->trustee))
|
---|
645 | return sid_compare(&ace1->trustee, &ace2->trustee);
|
---|
646 |
|
---|
647 | if (ace1->flags != ace2->flags)
|
---|
648 | return ace1->flags - ace2->flags;
|
---|
649 |
|
---|
650 | if (ace1->access_mask != ace2->access_mask)
|
---|
651 | return ace1->access_mask - ace2->access_mask;
|
---|
652 |
|
---|
653 | if (ace1->size != ace2->size)
|
---|
654 | return ace1->size - ace2->size;
|
---|
655 |
|
---|
656 | return memcmp(ace1, ace2, sizeof(SEC_ACE));
|
---|
657 | }
|
---|
658 |
|
---|
659 | static void sort_acl(SEC_ACL *the_acl)
|
---|
660 | {
|
---|
661 | uint32 i;
|
---|
662 | if (!the_acl) return;
|
---|
663 |
|
---|
664 | qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]), QSORT_CAST ace_compare);
|
---|
665 |
|
---|
666 | for (i=1;i<the_acl->num_aces;) {
|
---|
667 | if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
|
---|
668 | int j;
|
---|
669 | for (j=i; j<the_acl->num_aces-1; j++) {
|
---|
670 | the_acl->aces[j] = the_acl->aces[j+1];
|
---|
671 | }
|
---|
672 | the_acl->num_aces--;
|
---|
673 | } else {
|
---|
674 | i++;
|
---|
675 | }
|
---|
676 | }
|
---|
677 | }
|
---|
678 |
|
---|
679 | /*****************************************************
|
---|
680 | set the ACLs on a file given an ascii description
|
---|
681 | *******************************************************/
|
---|
682 | static int cacl_set(struct cli_state *cli, char *filename,
|
---|
683 | char *the_acl, enum acl_mode mode)
|
---|
684 | {
|
---|
685 | int fnum;
|
---|
686 | SEC_DESC *sd, *old;
|
---|
687 | uint32 i, j;
|
---|
688 | size_t sd_size;
|
---|
689 | int result = EXIT_OK;
|
---|
690 |
|
---|
691 | sd = sec_desc_parse(talloc_tos(), cli, the_acl);
|
---|
692 |
|
---|
693 | if (!sd) return EXIT_PARSE_ERROR;
|
---|
694 | if (test_args) return EXIT_OK;
|
---|
695 |
|
---|
696 | /* The desired access below is the only one I could find that works
|
---|
697 | with NT4, W2KP and Samba */
|
---|
698 |
|
---|
699 | fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
|
---|
700 |
|
---|
701 | if (fnum == -1) {
|
---|
702 | printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
|
---|
703 | return EXIT_FAILED;
|
---|
704 | }
|
---|
705 |
|
---|
706 | old = cli_query_secdesc(cli, fnum, talloc_tos());
|
---|
707 |
|
---|
708 | if (!old) {
|
---|
709 | printf("calc_set: Failed to query old descriptor\n");
|
---|
710 | return EXIT_FAILED;
|
---|
711 | }
|
---|
712 |
|
---|
713 | cli_close(cli, fnum);
|
---|
714 |
|
---|
715 | /* the logic here is rather more complex than I would like */
|
---|
716 | switch (mode) {
|
---|
717 | case SMB_ACL_DELETE:
|
---|
718 | for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
|
---|
719 | bool found = False;
|
---|
720 |
|
---|
721 | for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
|
---|
722 | if (sec_ace_equal(&sd->dacl->aces[i],
|
---|
723 | &old->dacl->aces[j])) {
|
---|
724 | uint32 k;
|
---|
725 | for (k=j; k<old->dacl->num_aces-1;k++) {
|
---|
726 | old->dacl->aces[k] = old->dacl->aces[k+1];
|
---|
727 | }
|
---|
728 | old->dacl->num_aces--;
|
---|
729 | found = True;
|
---|
730 | break;
|
---|
731 | }
|
---|
732 | }
|
---|
733 |
|
---|
734 | if (!found) {
|
---|
735 | printf("ACL for ACE:");
|
---|
736 | print_ace(cli, stdout, &sd->dacl->aces[i]);
|
---|
737 | printf(" not found\n");
|
---|
738 | }
|
---|
739 | }
|
---|
740 | break;
|
---|
741 |
|
---|
742 | case SMB_ACL_MODIFY:
|
---|
743 | for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
|
---|
744 | bool found = False;
|
---|
745 |
|
---|
746 | for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
|
---|
747 | if (sid_equal(&sd->dacl->aces[i].trustee,
|
---|
748 | &old->dacl->aces[j].trustee)) {
|
---|
749 | old->dacl->aces[j] = sd->dacl->aces[i];
|
---|
750 | found = True;
|
---|
751 | }
|
---|
752 | }
|
---|
753 |
|
---|
754 | if (!found) {
|
---|
755 | fstring str;
|
---|
756 |
|
---|
757 | SidToString(cli, str,
|
---|
758 | &sd->dacl->aces[i].trustee);
|
---|
759 | printf("ACL for SID %s not found\n", str);
|
---|
760 | }
|
---|
761 | }
|
---|
762 |
|
---|
763 | if (sd->owner_sid) {
|
---|
764 | old->owner_sid = sd->owner_sid;
|
---|
765 | }
|
---|
766 |
|
---|
767 | if (sd->group_sid) {
|
---|
768 | old->group_sid = sd->group_sid;
|
---|
769 | }
|
---|
770 |
|
---|
771 | break;
|
---|
772 |
|
---|
773 | case SMB_ACL_ADD:
|
---|
774 | for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
|
---|
775 | add_ace(&old->dacl, &sd->dacl->aces[i]);
|
---|
776 | }
|
---|
777 | break;
|
---|
778 |
|
---|
779 | case SMB_ACL_SET:
|
---|
780 | old = sd;
|
---|
781 | break;
|
---|
782 | }
|
---|
783 |
|
---|
784 | /* Denied ACE entries must come before allowed ones */
|
---|
785 | sort_acl(old->dacl);
|
---|
786 |
|
---|
787 | /* Create new security descriptor and set it */
|
---|
788 |
|
---|
789 | /* We used to just have "WRITE_DAC_ACCESS" without WRITE_OWNER.
|
---|
790 | But if we're sending an owner, even if it's the same as the one
|
---|
791 | that already exists then W2K3 insists we open with WRITE_OWNER access.
|
---|
792 | I need to check that setting a SD with no owner set works against WNT
|
---|
793 | and W2K. JRA.
|
---|
794 | */
|
---|
795 |
|
---|
796 | sd = make_sec_desc(talloc_tos(),old->revision, old->type,
|
---|
797 | old->owner_sid, old->group_sid,
|
---|
798 | NULL, old->dacl, &sd_size);
|
---|
799 |
|
---|
800 | fnum = cli_nt_create(cli, filename, WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS);
|
---|
801 |
|
---|
802 | if (fnum == -1) {
|
---|
803 | printf("cacl_set failed to open %s: %s\n", filename, cli_errstr(cli));
|
---|
804 | return EXIT_FAILED;
|
---|
805 | }
|
---|
806 |
|
---|
807 | if (!cli_set_secdesc(cli, fnum, sd)) {
|
---|
808 | printf("ERROR: secdesc set failed: %s\n", cli_errstr(cli));
|
---|
809 | result = EXIT_FAILED;
|
---|
810 | }
|
---|
811 |
|
---|
812 | /* Clean up */
|
---|
813 |
|
---|
814 | cli_close(cli, fnum);
|
---|
815 |
|
---|
816 | return result;
|
---|
817 | }
|
---|
818 |
|
---|
819 |
|
---|
820 | /*****************************************************
|
---|
821 | Return a connection to a server.
|
---|
822 | *******************************************************/
|
---|
823 | static struct cli_state *connect_one(const char *server, const char *share)
|
---|
824 | {
|
---|
825 | struct cli_state *c = NULL;
|
---|
826 | struct sockaddr_storage ss;
|
---|
827 | NTSTATUS nt_status;
|
---|
828 | uint32_t flags = 0;
|
---|
829 |
|
---|
830 | zero_addr(&ss);
|
---|
831 |
|
---|
832 | if (get_cmdline_auth_info_use_kerberos()) {
|
---|
833 | flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
|
---|
834 | CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
|
---|
835 | }
|
---|
836 |
|
---|
837 | if (get_cmdline_auth_info_use_machine_account() &&
|
---|
838 | !set_cmdline_auth_info_machine_account_creds()) {
|
---|
839 | return NULL;
|
---|
840 | }
|
---|
841 |
|
---|
842 | if (!get_cmdline_auth_info_got_pass()) {
|
---|
843 | char *pass = getpass("Password: ");
|
---|
844 | if (pass) {
|
---|
845 | set_cmdline_auth_info_password(pass);
|
---|
846 | }
|
---|
847 | }
|
---|
848 |
|
---|
849 | nt_status = cli_full_connection(&c, global_myname(), server,
|
---|
850 | &ss, 0,
|
---|
851 | share, "?????",
|
---|
852 | get_cmdline_auth_info_username(),
|
---|
853 | lp_workgroup(),
|
---|
854 | get_cmdline_auth_info_password(),
|
---|
855 | flags,
|
---|
856 | get_cmdline_auth_info_signing_state(),
|
---|
857 | NULL);
|
---|
858 | if (!NT_STATUS_IS_OK(nt_status)) {
|
---|
859 | DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
|
---|
860 | return NULL;
|
---|
861 | }
|
---|
862 |
|
---|
863 | if (get_cmdline_auth_info_smb_encrypt()) {
|
---|
864 | nt_status = cli_cm_force_encryption(c,
|
---|
865 | get_cmdline_auth_info_username(),
|
---|
866 | get_cmdline_auth_info_password(),
|
---|
867 | lp_workgroup(),
|
---|
868 | share);
|
---|
869 | if (!NT_STATUS_IS_OK(nt_status)) {
|
---|
870 | cli_shutdown(c);
|
---|
871 | c = NULL;
|
---|
872 | }
|
---|
873 | }
|
---|
874 |
|
---|
875 | return c;
|
---|
876 | }
|
---|
877 |
|
---|
878 | /****************************************************************************
|
---|
879 | main program
|
---|
880 | ****************************************************************************/
|
---|
881 | int main(int argc, const char *argv[])
|
---|
882 | {
|
---|
883 | char *share;
|
---|
884 | int opt;
|
---|
885 | enum acl_mode mode = SMB_ACL_SET;
|
---|
886 | static char *the_acl = NULL;
|
---|
887 | enum chown_mode change_mode = REQUEST_NONE;
|
---|
888 | int result;
|
---|
889 | char *path;
|
---|
890 | char *filename = NULL;
|
---|
891 | poptContext pc;
|
---|
892 | struct poptOption long_options[] = {
|
---|
893 | POPT_AUTOHELP
|
---|
894 | { "delete", 'D', POPT_ARG_STRING, NULL, 'D', "Delete an acl", "ACL" },
|
---|
895 | { "modify", 'M', POPT_ARG_STRING, NULL, 'M', "Modify an acl", "ACL" },
|
---|
896 | { "add", 'a', POPT_ARG_STRING, NULL, 'a', "Add an acl", "ACL" },
|
---|
897 | { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls", "ACLS" },
|
---|
898 | { "chown", 'C', POPT_ARG_STRING, NULL, 'C', "Change ownership of a file", "USERNAME" },
|
---|
899 | { "chgrp", 'G', POPT_ARG_STRING, NULL, 'G', "Change group ownership of a file", "GROUPNAME" },
|
---|
900 | { "numeric", 0, POPT_ARG_NONE, &numeric, True, "Don't resolve sids or masks to names" },
|
---|
901 | { "test-args", 't', POPT_ARG_NONE, &test_args, True, "Test arguments"},
|
---|
902 | POPT_COMMON_SAMBA
|
---|
903 | POPT_COMMON_CONNECTION
|
---|
904 | POPT_COMMON_CREDENTIALS
|
---|
905 | POPT_TABLEEND
|
---|
906 | };
|
---|
907 |
|
---|
908 | struct cli_state *cli;
|
---|
909 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
910 | const char *owner_username = "";
|
---|
911 | char *server;
|
---|
912 |
|
---|
913 | load_case_tables();
|
---|
914 |
|
---|
915 |
|
---|
916 | /* set default debug level to 1 regardless of what smb.conf sets */
|
---|
917 | setup_logging( "smbcacls", True );
|
---|
918 | DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
|
---|
919 | dbf = x_stderr;
|
---|
920 | x_setbuf( x_stderr, NULL );
|
---|
921 |
|
---|
922 | setlinebuf(stdout);
|
---|
923 |
|
---|
924 | lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
|
---|
925 | load_interfaces();
|
---|
926 |
|
---|
927 | pc = poptGetContext("smbcacls", argc, argv, long_options, 0);
|
---|
928 |
|
---|
929 | poptSetOtherOptionHelp(pc, "//server1/share1 filename\nACLs look like: "
|
---|
930 | "'ACL:user:[ALLOWED|DENIED]/flags/permissions'");
|
---|
931 |
|
---|
932 | while ((opt = poptGetNextOpt(pc)) != -1) {
|
---|
933 | switch (opt) {
|
---|
934 | case 'S':
|
---|
935 | the_acl = smb_xstrdup(poptGetOptArg(pc));
|
---|
936 | mode = SMB_ACL_SET;
|
---|
937 | break;
|
---|
938 |
|
---|
939 | case 'D':
|
---|
940 | the_acl = smb_xstrdup(poptGetOptArg(pc));
|
---|
941 | mode = SMB_ACL_DELETE;
|
---|
942 | break;
|
---|
943 |
|
---|
944 | case 'M':
|
---|
945 | the_acl = smb_xstrdup(poptGetOptArg(pc));
|
---|
946 | mode = SMB_ACL_MODIFY;
|
---|
947 | break;
|
---|
948 |
|
---|
949 | case 'a':
|
---|
950 | the_acl = smb_xstrdup(poptGetOptArg(pc));
|
---|
951 | mode = SMB_ACL_ADD;
|
---|
952 | break;
|
---|
953 |
|
---|
954 | case 'C':
|
---|
955 | owner_username = poptGetOptArg(pc);
|
---|
956 | change_mode = REQUEST_CHOWN;
|
---|
957 | break;
|
---|
958 |
|
---|
959 | case 'G':
|
---|
960 | owner_username = poptGetOptArg(pc);
|
---|
961 | change_mode = REQUEST_CHGRP;
|
---|
962 | break;
|
---|
963 | }
|
---|
964 | }
|
---|
965 |
|
---|
966 | /* Make connection to server */
|
---|
967 | if(!poptPeekArg(pc)) {
|
---|
968 | poptPrintUsage(pc, stderr, 0);
|
---|
969 | return -1;
|
---|
970 | }
|
---|
971 |
|
---|
972 | path = talloc_strdup(frame, poptGetArg(pc));
|
---|
973 | if (!path) {
|
---|
974 | return -1;
|
---|
975 | }
|
---|
976 |
|
---|
977 | if(!poptPeekArg(pc)) {
|
---|
978 | poptPrintUsage(pc, stderr, 0);
|
---|
979 | return -1;
|
---|
980 | }
|
---|
981 |
|
---|
982 | filename = talloc_strdup(frame, poptGetArg(pc));
|
---|
983 | if (!filename) {
|
---|
984 | return -1;
|
---|
985 | }
|
---|
986 |
|
---|
987 | string_replace(path,'/','\\');
|
---|
988 |
|
---|
989 | server = talloc_strdup(frame, path+2);
|
---|
990 | if (!server) {
|
---|
991 | return -1;
|
---|
992 | }
|
---|
993 | share = strchr_m(server,'\\');
|
---|
994 | if (!share) {
|
---|
995 | printf("Invalid argument: %s\n", share);
|
---|
996 | return -1;
|
---|
997 | }
|
---|
998 |
|
---|
999 | *share = 0;
|
---|
1000 | share++;
|
---|
1001 |
|
---|
1002 | if (!test_args) {
|
---|
1003 | cli = connect_one(server, share);
|
---|
1004 | if (!cli) {
|
---|
1005 | exit(EXIT_FAILED);
|
---|
1006 | }
|
---|
1007 | } else {
|
---|
1008 | exit(0);
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | string_replace(filename, '/', '\\');
|
---|
1012 | if (filename[0] != '\\') {
|
---|
1013 | filename = talloc_asprintf(frame,
|
---|
1014 | "\\%s",
|
---|
1015 | filename);
|
---|
1016 | if (!filename) {
|
---|
1017 | return -1;
|
---|
1018 | }
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | /* Perform requested action */
|
---|
1022 |
|
---|
1023 | if (change_mode != REQUEST_NONE) {
|
---|
1024 | result = owner_set(cli, change_mode, filename, owner_username);
|
---|
1025 | } else if (the_acl) {
|
---|
1026 | result = cacl_set(cli, filename, the_acl, mode);
|
---|
1027 | } else {
|
---|
1028 | result = cacl_dump(cli, filename);
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | TALLOC_FREE(frame);
|
---|
1032 |
|
---|
1033 | return result;
|
---|
1034 | }
|
---|