smb.conf(5): client min protocol: add hint at list of available protocols
[Samba/vl.git] / source3 / libsmb / libsmb_xattr.c
blob03cdc34f928d12798ebc8b766a0476acaec3e334
1 /*
2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003-2008
9 Copyright (C) Jeremy Allison 2007, 2008
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "libsmb/libsmb.h"
27 #include "libsmbclient.h"
28 #include "libsmb_internal.h"
29 #include "../librpc/gen_ndr/ndr_lsa.h"
30 #include "rpc_client/rpc_client.h"
31 #include "rpc_client/cli_lsarpc.h"
32 #include "../libcli/security/security.h"
35 * Find an lsa pipe handle associated with a cli struct.
37 static struct rpc_pipe_client *
38 find_lsa_pipe_hnd(struct cli_state *ipc_cli)
40 struct rpc_pipe_client *pipe_hnd;
42 for (pipe_hnd = ipc_cli->pipe_list;
43 pipe_hnd;
44 pipe_hnd = pipe_hnd->next) {
45 if (ndr_syntax_id_equal(&pipe_hnd->abstract_syntax,
46 &ndr_table_lsarpc.syntax_id)) {
47 return pipe_hnd;
50 return NULL;
54 * Sort ACEs according to the documentation at
55 * http://support.microsoft.com/kb/269175, at least as far as it defines the
56 * order.
59 static int
60 ace_compare(struct security_ace *ace1,
61 struct security_ace *ace2)
63 bool b1;
64 bool b2;
66 /* If the ACEs are equal, we have nothing more to do. */
67 if (sec_ace_equal(ace1, ace2)) {
68 return 0;
71 /* Inherited follow non-inherited */
72 b1 = ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
73 b2 = ((ace2->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
74 if (b1 != b2) {
75 return (b1 ? 1 : -1);
79 * What shall we do with AUDITs and ALARMs? It's undefined. We'll
80 * sort them after DENY and ALLOW.
82 b1 = (ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
83 ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
84 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED &&
85 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
86 b2 = (ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
87 ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
88 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED &&
89 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
90 if (b1 != b2) {
91 return (b1 ? 1 : -1);
94 /* Allowed ACEs follow denied ACEs */
95 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
96 ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
97 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
98 ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
99 if (b1 != b2) {
100 return (b1 ? 1 : -1);
104 * ACEs applying to an entity's object follow those applying to the
105 * entity itself
107 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
108 ace1->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
109 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
110 ace2->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
111 if (b1 != b2) {
112 return (b1 ? 1 : -1);
116 * If we get this far, the ACEs are similar as far as the
117 * characteristics we typically care about (those defined by the
118 * referenced MS document). We'll now sort by characteristics that
119 * just seems reasonable.
122 if (ace1->type != ace2->type) {
123 return ace2->type - ace1->type;
126 if (dom_sid_compare(&ace1->trustee, &ace2->trustee)) {
127 return dom_sid_compare(&ace1->trustee, &ace2->trustee);
130 if (ace1->flags != ace2->flags) {
131 return ace1->flags - ace2->flags;
134 if (ace1->access_mask != ace2->access_mask) {
135 return ace1->access_mask - ace2->access_mask;
138 if (ace1->size != ace2->size) {
139 return ace1->size - ace2->size;
142 return memcmp(ace1, ace2, sizeof(struct security_ace));
146 static void
147 sort_acl(struct security_acl *the_acl)
149 uint32 i;
150 if (!the_acl) return;
152 TYPESAFE_QSORT(the_acl->aces, the_acl->num_aces, ace_compare);
154 for (i=1;i<the_acl->num_aces;) {
155 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
156 int j;
157 for (j=i; j<the_acl->num_aces-1; j++) {
158 the_acl->aces[j] = the_acl->aces[j+1];
160 the_acl->num_aces--;
161 } else {
162 i++;
167 /* convert a SID to a string, either numeric or username/group */
168 static void
169 convert_sid_to_string(struct cli_state *ipc_cli,
170 struct policy_handle *pol,
171 fstring str,
172 bool numeric,
173 struct dom_sid *sid)
175 char **domains = NULL;
176 char **names = NULL;
177 enum lsa_SidType *types = NULL;
178 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
179 TALLOC_CTX *ctx;
181 sid_to_fstring(str, sid);
183 if (numeric) {
184 return; /* no lookup desired */
187 if (!pipe_hnd) {
188 return;
191 /* Ask LSA to convert the sid to a name */
193 ctx = talloc_stackframe();
195 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd, ctx,
196 pol, 1, sid, &domains,
197 &names, &types)) ||
198 !domains || !domains[0] || !names || !names[0]) {
199 TALLOC_FREE(ctx);
200 return;
203 /* Converted OK */
205 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
206 domains[0], lp_winbind_separator(),
207 names[0]);
209 TALLOC_FREE(ctx);
212 /* convert a string to a SID, either numeric or username/group */
213 static bool
214 convert_string_to_sid(struct cli_state *ipc_cli,
215 struct policy_handle *pol,
216 bool numeric,
217 struct dom_sid *sid,
218 const char *str)
220 enum lsa_SidType *types = NULL;
221 struct dom_sid *sids = NULL;
222 bool result = True;
223 TALLOC_CTX *ctx = NULL;
224 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
226 if (!pipe_hnd) {
227 return False;
230 if (numeric) {
231 if (strncmp(str, "S-", 2) == 0) {
232 return string_to_sid(sid, str);
235 result = False;
236 goto done;
239 ctx = talloc_stackframe();
240 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ctx,
241 pol, 1, &str,
242 NULL, 1, &sids,
243 &types))) {
244 result = False;
245 goto done;
248 sid_copy(sid, &sids[0]);
249 done:
250 TALLOC_FREE(ctx);
251 return result;
255 /* parse an struct security_ace in the same format as print_ace() */
256 static bool
257 parse_ace(struct cli_state *ipc_cli,
258 struct policy_handle *pol,
259 struct security_ace *ace,
260 bool numeric,
261 char *str)
263 char *p;
264 const char *cp;
265 char *tok;
266 unsigned int atype;
267 unsigned int aflags;
268 unsigned int amask;
269 struct dom_sid sid;
270 uint32_t mask;
271 const struct perm_value *v;
272 struct perm_value {
273 const char perm[7];
274 uint32 mask;
276 TALLOC_CTX *frame = talloc_stackframe();
278 /* These values discovered by inspection */
279 static const struct perm_value special_values[] = {
280 { "R", 0x00120089 },
281 { "W", 0x00120116 },
282 { "X", 0x001200a0 },
283 { "D", 0x00010000 },
284 { "P", 0x00040000 },
285 { "O", 0x00080000 },
286 { "", 0 },
289 static const struct perm_value standard_values[] = {
290 { "READ", 0x001200a9 },
291 { "CHANGE", 0x001301bf },
292 { "FULL", 0x001f01ff },
293 { "", 0 },
296 ZERO_STRUCTP(ace);
297 p = strchr_m(str,':');
298 if (!p) {
299 TALLOC_FREE(frame);
300 return False;
302 *p = '\0';
303 p++;
304 /* Try to parse numeric form */
306 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
307 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
308 goto done;
311 /* Try to parse text form */
313 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
314 TALLOC_FREE(frame);
315 return false;
318 cp = p;
319 if (!next_token_talloc(frame, &cp, &tok, "/")) {
320 TALLOC_FREE(frame);
321 return false;
324 if (strncasecmp_m(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
325 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
326 } else if (strncasecmp_m(tok, "DENIED", strlen("DENIED")) == 0) {
327 atype = SEC_ACE_TYPE_ACCESS_DENIED;
328 } else {
329 TALLOC_FREE(frame);
330 return false;
333 /* Only numeric form accepted for flags at present */
335 if (!(next_token_talloc(frame, &cp, &tok, "/") &&
336 sscanf(tok, "%i", &aflags))) {
337 TALLOC_FREE(frame);
338 return false;
341 if (!next_token_talloc(frame, &cp, &tok, "/")) {
342 TALLOC_FREE(frame);
343 return false;
346 if (strncmp(tok, "0x", 2) == 0) {
347 if (sscanf(tok, "%i", &amask) != 1) {
348 TALLOC_FREE(frame);
349 return false;
351 goto done;
354 for (v = standard_values; v->perm; v++) {
355 if (strcmp(tok, v->perm) == 0) {
356 amask = v->mask;
357 goto done;
361 p = tok;
363 while(*p) {
364 bool found = False;
366 for (v = special_values; v->perm; v++) {
367 if (v->perm[0] == *p) {
368 amask |= v->mask;
369 found = True;
373 if (!found) {
374 TALLOC_FREE(frame);
375 return false;
377 p++;
380 if (*p) {
381 TALLOC_FREE(frame);
382 return false;
385 done:
386 mask = amask;
387 init_sec_ace(ace, &sid, atype, mask, aflags);
388 TALLOC_FREE(frame);
389 return true;
392 /* add an struct security_ace to a list of struct security_aces in a struct security_acl */
393 static bool
394 add_ace(struct security_acl **the_acl,
395 struct security_ace *ace,
396 TALLOC_CTX *ctx)
398 struct security_acl *newacl;
399 struct security_ace *aces;
401 if (! *the_acl) {
402 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
403 return True;
406 if ((aces = SMB_CALLOC_ARRAY(struct security_ace,
407 1+(*the_acl)->num_aces)) == NULL) {
408 return False;
410 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(struct security_ace));
411 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(struct security_ace));
412 newacl = make_sec_acl(ctx, (*the_acl)->revision,
413 1+(*the_acl)->num_aces, aces);
414 SAFE_FREE(aces);
415 (*the_acl) = newacl;
416 return True;
420 /* parse a ascii version of a security descriptor */
421 static struct security_descriptor *
422 sec_desc_parse(TALLOC_CTX *ctx,
423 struct cli_state *ipc_cli,
424 struct policy_handle *pol,
425 bool numeric,
426 const char *str)
428 const char *p = str;
429 char *tok;
430 struct security_descriptor *ret = NULL;
431 size_t sd_size;
432 struct dom_sid *group_sid=NULL;
433 struct dom_sid *owner_sid=NULL;
434 struct security_acl *dacl=NULL;
435 int revision=1;
437 while (next_token_talloc(ctx, &p, &tok, "\t,\r\n")) {
439 if (strncasecmp_m(tok,"REVISION:", 9) == 0) {
440 revision = strtol(tok+9, NULL, 16);
441 continue;
444 if (strncasecmp_m(tok,"OWNER:", 6) == 0) {
445 if (owner_sid) {
446 DEBUG(5,("OWNER specified more than once!\n"));
447 goto done;
449 owner_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
450 if (!owner_sid ||
451 !convert_string_to_sid(ipc_cli, pol,
452 numeric,
453 owner_sid, tok+6)) {
454 DEBUG(5, ("Failed to parse owner sid\n"));
455 goto done;
457 continue;
460 if (strncasecmp_m(tok,"OWNER+:", 7) == 0) {
461 if (owner_sid) {
462 DEBUG(5,("OWNER specified more than once!\n"));
463 goto done;
465 owner_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
466 if (!owner_sid ||
467 !convert_string_to_sid(ipc_cli, pol,
468 False,
469 owner_sid, tok+7)) {
470 DEBUG(5, ("Failed to parse owner sid\n"));
471 goto done;
473 continue;
476 if (strncasecmp_m(tok,"GROUP:", 6) == 0) {
477 if (group_sid) {
478 DEBUG(5,("GROUP specified more than once!\n"));
479 goto done;
481 group_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
482 if (!group_sid ||
483 !convert_string_to_sid(ipc_cli, pol,
484 numeric,
485 group_sid, tok+6)) {
486 DEBUG(5, ("Failed to parse group sid\n"));
487 goto done;
489 continue;
492 if (strncasecmp_m(tok,"GROUP+:", 7) == 0) {
493 if (group_sid) {
494 DEBUG(5,("GROUP specified more than once!\n"));
495 goto done;
497 group_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
498 if (!group_sid ||
499 !convert_string_to_sid(ipc_cli, pol,
500 False,
501 group_sid, tok+6)) {
502 DEBUG(5, ("Failed to parse group sid\n"));
503 goto done;
505 continue;
508 if (strncasecmp_m(tok,"ACL:", 4) == 0) {
509 struct security_ace ace;
510 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
511 DEBUG(5, ("Failed to parse ACL %s\n", tok));
512 goto done;
514 if(!add_ace(&dacl, &ace, ctx)) {
515 DEBUG(5, ("Failed to add ACL %s\n", tok));
516 goto done;
518 continue;
521 if (strncasecmp_m(tok,"ACL+:", 5) == 0) {
522 struct security_ace ace;
523 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
524 DEBUG(5, ("Failed to parse ACL %s\n", tok));
525 goto done;
527 if(!add_ace(&dacl, &ace, ctx)) {
528 DEBUG(5, ("Failed to add ACL %s\n", tok));
529 goto done;
531 continue;
534 DEBUG(5, ("Failed to parse security descriptor\n"));
535 goto done;
538 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
539 owner_sid, group_sid, NULL, dacl, &sd_size);
541 done:
542 SAFE_FREE(group_sid);
543 SAFE_FREE(owner_sid);
544 return ret;
548 /* Obtain the current dos attributes */
549 static DOS_ATTR_DESC *
550 dos_attr_query(SMBCCTX *context,
551 TALLOC_CTX *ctx,
552 const char *filename,
553 SMBCSRV *srv)
555 struct timespec create_time_ts;
556 struct timespec write_time_ts;
557 struct timespec access_time_ts;
558 struct timespec change_time_ts;
559 off_t size = 0;
560 uint16 mode = 0;
561 SMB_INO_T inode = 0;
562 DOS_ATTR_DESC *ret;
564 ret = talloc(ctx, DOS_ATTR_DESC);
565 if (!ret) {
566 errno = ENOMEM;
567 return NULL;
570 /* Obtain the DOS attributes */
571 if (!SMBC_getatr(context, srv, filename,
572 &mode, &size,
573 &create_time_ts,
574 &access_time_ts,
575 &write_time_ts,
576 &change_time_ts,
577 &inode)) {
578 errno = SMBC_errno(context, srv->cli);
579 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
580 TALLOC_FREE(ret);
581 return NULL;
584 ret->mode = mode;
585 ret->size = size;
586 ret->create_time = convert_timespec_to_time_t(create_time_ts);
587 ret->access_time = convert_timespec_to_time_t(access_time_ts);
588 ret->write_time = convert_timespec_to_time_t(write_time_ts);
589 ret->change_time = convert_timespec_to_time_t(change_time_ts);
590 ret->inode = inode;
592 return ret;
596 /* parse a ascii version of a security descriptor */
597 static void
598 dos_attr_parse(SMBCCTX *context,
599 DOS_ATTR_DESC *dad,
600 SMBCSRV *srv,
601 char *str)
603 int n;
604 const char *p = str;
605 char *tok = NULL;
606 TALLOC_CTX *frame = NULL;
607 struct {
608 const char * create_time_attr;
609 const char * access_time_attr;
610 const char * write_time_attr;
611 const char * change_time_attr;
612 } attr_strings;
614 /* Determine whether to use old-style or new-style attribute names */
615 if (context->internal->full_time_names) {
616 /* new-style names */
617 attr_strings.create_time_attr = "CREATE_TIME";
618 attr_strings.access_time_attr = "ACCESS_TIME";
619 attr_strings.write_time_attr = "WRITE_TIME";
620 attr_strings.change_time_attr = "CHANGE_TIME";
621 } else {
622 /* old-style names */
623 attr_strings.create_time_attr = NULL;
624 attr_strings.access_time_attr = "A_TIME";
625 attr_strings.write_time_attr = "M_TIME";
626 attr_strings.change_time_attr = "C_TIME";
629 /* if this is to set the entire ACL... */
630 if (*str == '*') {
631 /* ... then increment past the first colon if there is one */
632 if ((p = strchr(str, ':')) != NULL) {
633 ++p;
634 } else {
635 p = str;
639 frame = talloc_stackframe();
640 while (next_token_talloc(frame, &p, &tok, "\t,\r\n")) {
641 if (strncasecmp_m(tok, "MODE:", 5) == 0) {
642 long request = strtol(tok+5, NULL, 16);
643 if (request == 0) {
644 dad->mode = (request |
645 (IS_DOS_DIR(dad->mode)
646 ? FILE_ATTRIBUTE_DIRECTORY
647 : FILE_ATTRIBUTE_NORMAL));
648 } else {
649 dad->mode = request;
651 continue;
654 if (strncasecmp_m(tok, "SIZE:", 5) == 0) {
655 dad->size = (off_t)atof(tok+5);
656 continue;
659 n = strlen(attr_strings.access_time_attr);
660 if (strncasecmp_m(tok, attr_strings.access_time_attr, n) == 0) {
661 dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
662 continue;
665 n = strlen(attr_strings.change_time_attr);
666 if (strncasecmp_m(tok, attr_strings.change_time_attr, n) == 0) {
667 dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
668 continue;
671 n = strlen(attr_strings.write_time_attr);
672 if (strncasecmp_m(tok, attr_strings.write_time_attr, n) == 0) {
673 dad->write_time = (time_t)strtol(tok+n+1, NULL, 10);
674 continue;
677 if (attr_strings.create_time_attr != NULL) {
678 n = strlen(attr_strings.create_time_attr);
679 if (strncasecmp_m(tok, attr_strings.create_time_attr,
680 n) == 0) {
681 dad->create_time = (time_t)strtol(tok+n+1,
682 NULL, 10);
683 continue;
687 if (strncasecmp_m(tok, "INODE:", 6) == 0) {
688 dad->inode = (SMB_INO_T)atof(tok+6);
689 continue;
692 TALLOC_FREE(frame);
695 /*****************************************************
696 Retrieve the acls for a file.
697 *******************************************************/
699 static int
700 cacl_get(SMBCCTX *context,
701 TALLOC_CTX *ctx,
702 SMBCSRV *srv,
703 struct cli_state *ipc_cli,
704 struct policy_handle *pol,
705 const char *filename,
706 const char *attr_name,
707 char *buf,
708 int bufsize)
710 uint32 i;
711 int n = 0;
712 int n_used;
713 bool all;
714 bool all_nt;
715 bool all_nt_acls;
716 bool all_dos;
717 bool some_nt;
718 bool some_dos;
719 bool exclude_nt_revision = False;
720 bool exclude_nt_owner = False;
721 bool exclude_nt_group = False;
722 bool exclude_nt_acl = False;
723 bool exclude_dos_mode = False;
724 bool exclude_dos_size = False;
725 bool exclude_dos_create_time = False;
726 bool exclude_dos_access_time = False;
727 bool exclude_dos_write_time = False;
728 bool exclude_dos_change_time = False;
729 bool exclude_dos_inode = False;
730 bool numeric = True;
731 bool determine_size = (bufsize == 0);
732 uint16_t fnum;
733 struct security_descriptor *sd;
734 fstring sidstr;
735 fstring name_sandbox;
736 char *name;
737 char *pExclude;
738 char *p;
739 struct timespec create_time_ts;
740 struct timespec write_time_ts;
741 struct timespec access_time_ts;
742 struct timespec change_time_ts;
743 time_t create_time = (time_t)0;
744 time_t write_time = (time_t)0;
745 time_t access_time = (time_t)0;
746 time_t change_time = (time_t)0;
747 off_t size = 0;
748 uint16 mode = 0;
749 SMB_INO_T ino = 0;
750 struct cli_state *cli = srv->cli;
751 struct {
752 const char * create_time_attr;
753 const char * access_time_attr;
754 const char * write_time_attr;
755 const char * change_time_attr;
756 } attr_strings;
757 struct {
758 const char * create_time_attr;
759 const char * access_time_attr;
760 const char * write_time_attr;
761 const char * change_time_attr;
762 } excl_attr_strings;
764 /* Determine whether to use old-style or new-style attribute names */
765 if (context->internal->full_time_names) {
766 /* new-style names */
767 attr_strings.create_time_attr = "CREATE_TIME";
768 attr_strings.access_time_attr = "ACCESS_TIME";
769 attr_strings.write_time_attr = "WRITE_TIME";
770 attr_strings.change_time_attr = "CHANGE_TIME";
772 excl_attr_strings.create_time_attr = "CREATE_TIME";
773 excl_attr_strings.access_time_attr = "ACCESS_TIME";
774 excl_attr_strings.write_time_attr = "WRITE_TIME";
775 excl_attr_strings.change_time_attr = "CHANGE_TIME";
776 } else {
777 /* old-style names */
778 attr_strings.create_time_attr = NULL;
779 attr_strings.access_time_attr = "A_TIME";
780 attr_strings.write_time_attr = "M_TIME";
781 attr_strings.change_time_attr = "C_TIME";
783 excl_attr_strings.create_time_attr = NULL;
784 excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
785 excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
786 excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
789 /* Copy name so we can strip off exclusions (if any are specified) */
790 strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
792 /* Ensure name is null terminated */
793 name_sandbox[sizeof(name_sandbox) - 1] = '\0';
795 /* Play in the sandbox */
796 name = name_sandbox;
798 /* If there are any exclusions, point to them and mask them from name */
799 if ((pExclude = strchr(name, '!')) != NULL)
801 *pExclude++ = '\0';
804 all = (strncasecmp_m(name, "system.*", 8) == 0);
805 all_nt = (strncasecmp_m(name, "system.nt_sec_desc.*", 20) == 0);
806 all_nt_acls = (strncasecmp_m(name, "system.nt_sec_desc.acl.*", 24) == 0);
807 all_dos = (strncasecmp_m(name, "system.dos_attr.*", 17) == 0);
808 some_nt = (strncasecmp_m(name, "system.nt_sec_desc.", 19) == 0);
809 some_dos = (strncasecmp_m(name, "system.dos_attr.", 16) == 0);
810 numeric = (* (name + strlen(name) - 1) != '+');
812 /* Look for exclusions from "all" requests */
813 if (all || all_nt || all_dos) {
814 /* Exclusions are delimited by '!' */
815 for (;
816 pExclude != NULL;
817 pExclude = (p == NULL ? NULL : p + 1)) {
819 /* Find end of this exclusion name */
820 if ((p = strchr(pExclude, '!')) != NULL)
822 *p = '\0';
825 /* Which exclusion name is this? */
826 if (strcasecmp_m(pExclude,
827 "nt_sec_desc.revision") == 0) {
828 exclude_nt_revision = True;
830 else if (strcasecmp_m(pExclude,
831 "nt_sec_desc.owner") == 0) {
832 exclude_nt_owner = True;
834 else if (strcasecmp_m(pExclude,
835 "nt_sec_desc.group") == 0) {
836 exclude_nt_group = True;
838 else if (strcasecmp_m(pExclude,
839 "nt_sec_desc.acl") == 0) {
840 exclude_nt_acl = True;
842 else if (strcasecmp_m(pExclude,
843 "dos_attr.mode") == 0) {
844 exclude_dos_mode = True;
846 else if (strcasecmp_m(pExclude,
847 "dos_attr.size") == 0) {
848 exclude_dos_size = True;
850 else if (excl_attr_strings.create_time_attr != NULL &&
851 strcasecmp_m(pExclude,
852 excl_attr_strings.change_time_attr) == 0) {
853 exclude_dos_create_time = True;
855 else if (strcasecmp_m(pExclude,
856 excl_attr_strings.access_time_attr) == 0) {
857 exclude_dos_access_time = True;
859 else if (strcasecmp_m(pExclude,
860 excl_attr_strings.write_time_attr) == 0) {
861 exclude_dos_write_time = True;
863 else if (strcasecmp_m(pExclude,
864 excl_attr_strings.change_time_attr) == 0) {
865 exclude_dos_change_time = True;
867 else if (strcasecmp_m(pExclude, "dos_attr.inode") == 0) {
868 exclude_dos_inode = True;
870 else {
871 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
872 pExclude));
873 errno = ENOATTR;
874 return -1;
879 n_used = 0;
882 * If we are (possibly) talking to an NT or new system and some NT
883 * attributes have been requested...
885 if (ipc_cli && (all || some_nt || all_nt_acls)) {
886 char *targetpath = NULL;
887 struct cli_state *targetcli = NULL;
888 NTSTATUS status;
890 /* Point to the portion after "system.nt_sec_desc." */
891 name += 19; /* if (all) this will be invalid but unused */
893 status = cli_resolve_path(
894 ctx, "", context->internal->auth_info,
895 cli, filename, &targetcli, &targetpath);
896 if (!NT_STATUS_IS_OK(status)) {
897 DEBUG(5, ("cacl_get Could not resolve %s\n",
898 filename));
899 errno = ENOENT;
900 return -1;
903 /* ... then obtain any NT attributes which were requested */
904 status = cli_ntcreate(targetcli, targetpath, 0,
905 CREATE_ACCESS_READ, 0,
906 FILE_SHARE_READ|FILE_SHARE_WRITE,
907 FILE_OPEN, 0x0, 0x0, &fnum);
908 if (!NT_STATUS_IS_OK(status)) {
909 DEBUG(5, ("cacl_get failed to open %s: %s\n",
910 targetpath, nt_errstr(status)));
911 errno = 0;
912 return -1;
915 status = cli_query_secdesc(targetcli, fnum, ctx, &sd);
916 if (!NT_STATUS_IS_OK(status)) {
917 DEBUG(5,("cacl_get Failed to query old descriptor "
918 "of %s: %s\n",
919 targetpath, nt_errstr(status)));
920 errno = 0;
921 return -1;
924 cli_close(targetcli, fnum);
926 if (! exclude_nt_revision) {
927 if (all || all_nt) {
928 if (determine_size) {
929 p = talloc_asprintf(ctx,
930 "REVISION:%d",
931 sd->revision);
932 if (!p) {
933 errno = ENOMEM;
934 return -1;
936 n = strlen(p);
937 } else {
938 n = snprintf(buf, bufsize,
939 "REVISION:%d",
940 sd->revision);
942 } else if (strcasecmp_m(name, "revision") == 0) {
943 if (determine_size) {
944 p = talloc_asprintf(ctx, "%d",
945 sd->revision);
946 if (!p) {
947 errno = ENOMEM;
948 return -1;
950 n = strlen(p);
951 } else {
952 n = snprintf(buf, bufsize, "%d",
953 sd->revision);
957 if (!determine_size && n > bufsize) {
958 errno = ERANGE;
959 return -1;
961 buf += n;
962 n_used += n;
963 bufsize -= n;
964 n = 0;
967 if (! exclude_nt_owner) {
968 /* Get owner and group sid */
969 if (sd->owner_sid) {
970 convert_sid_to_string(ipc_cli, pol,
971 sidstr,
972 numeric,
973 sd->owner_sid);
974 } else {
975 fstrcpy(sidstr, "");
978 if (all || all_nt) {
979 if (determine_size) {
980 p = talloc_asprintf(ctx, ",OWNER:%s",
981 sidstr);
982 if (!p) {
983 errno = ENOMEM;
984 return -1;
986 n = strlen(p);
987 } else if (sidstr[0] != '\0') {
988 n = snprintf(buf, bufsize,
989 ",OWNER:%s", sidstr);
991 } else if (strncasecmp_m(name, "owner", 5) == 0) {
992 if (determine_size) {
993 p = talloc_asprintf(ctx, "%s", sidstr);
994 if (!p) {
995 errno = ENOMEM;
996 return -1;
998 n = strlen(p);
999 } else {
1000 n = snprintf(buf, bufsize, "%s",
1001 sidstr);
1005 if (!determine_size && n > bufsize) {
1006 errno = ERANGE;
1007 return -1;
1009 buf += n;
1010 n_used += n;
1011 bufsize -= n;
1012 n = 0;
1015 if (! exclude_nt_group) {
1016 if (sd->group_sid) {
1017 convert_sid_to_string(ipc_cli, pol,
1018 sidstr, numeric,
1019 sd->group_sid);
1020 } else {
1021 fstrcpy(sidstr, "");
1024 if (all || all_nt) {
1025 if (determine_size) {
1026 p = talloc_asprintf(ctx, ",GROUP:%s",
1027 sidstr);
1028 if (!p) {
1029 errno = ENOMEM;
1030 return -1;
1032 n = strlen(p);
1033 } else if (sidstr[0] != '\0') {
1034 n = snprintf(buf, bufsize,
1035 ",GROUP:%s", sidstr);
1037 } else if (strncasecmp_m(name, "group", 5) == 0) {
1038 if (determine_size) {
1039 p = talloc_asprintf(ctx, "%s", sidstr);
1040 if (!p) {
1041 errno = ENOMEM;
1042 return -1;
1044 n = strlen(p);
1045 } else {
1046 n = snprintf(buf, bufsize,
1047 "%s", sidstr);
1051 if (!determine_size && n > bufsize) {
1052 errno = ERANGE;
1053 return -1;
1055 buf += n;
1056 n_used += n;
1057 bufsize -= n;
1058 n = 0;
1061 if (! exclude_nt_acl) {
1062 /* Add aces to value buffer */
1063 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
1065 struct security_ace *ace = &sd->dacl->aces[i];
1066 convert_sid_to_string(ipc_cli, pol,
1067 sidstr, numeric,
1068 &ace->trustee);
1070 if (all || all_nt) {
1071 if (determine_size) {
1072 p = talloc_asprintf(
1073 ctx,
1074 ",ACL:"
1075 "%s:%d/%d/0x%08x",
1076 sidstr,
1077 ace->type,
1078 ace->flags,
1079 ace->access_mask);
1080 if (!p) {
1081 errno = ENOMEM;
1082 return -1;
1084 n = strlen(p);
1085 } else {
1086 n = snprintf(
1087 buf, bufsize,
1088 ",ACL:%s:%d/%d/0x%08x",
1089 sidstr,
1090 ace->type,
1091 ace->flags,
1092 ace->access_mask);
1094 } else if ((strncasecmp_m(name, "acl", 3) == 0 &&
1095 strcasecmp_m(name+3, sidstr) == 0) ||
1096 (strncasecmp_m(name, "acl+", 4) == 0 &&
1097 strcasecmp_m(name+4, sidstr) == 0)) {
1098 if (determine_size) {
1099 p = talloc_asprintf(
1100 ctx,
1101 "%d/%d/0x%08x",
1102 ace->type,
1103 ace->flags,
1104 ace->access_mask);
1105 if (!p) {
1106 errno = ENOMEM;
1107 return -1;
1109 n = strlen(p);
1110 } else {
1111 n = snprintf(buf, bufsize,
1112 "%d/%d/0x%08x",
1113 ace->type,
1114 ace->flags,
1115 ace->access_mask);
1117 } else if (all_nt_acls) {
1118 if (determine_size) {
1119 p = talloc_asprintf(
1120 ctx,
1121 "%s%s:%d/%d/0x%08x",
1122 i ? "," : "",
1123 sidstr,
1124 ace->type,
1125 ace->flags,
1126 ace->access_mask);
1127 if (!p) {
1128 errno = ENOMEM;
1129 return -1;
1131 n = strlen(p);
1132 } else {
1133 n = snprintf(buf, bufsize,
1134 "%s%s:%d/%d/0x%08x",
1135 i ? "," : "",
1136 sidstr,
1137 ace->type,
1138 ace->flags,
1139 ace->access_mask);
1142 if (!determine_size && n > bufsize) {
1143 errno = ERANGE;
1144 return -1;
1146 buf += n;
1147 n_used += n;
1148 bufsize -= n;
1149 n = 0;
1153 /* Restore name pointer to its original value */
1154 name -= 19;
1157 if (all || some_dos) {
1158 /* Point to the portion after "system.dos_attr." */
1159 name += 16; /* if (all) this will be invalid but unused */
1161 /* Obtain the DOS attributes */
1162 if (!SMBC_getatr(context, srv, filename, &mode, &size,
1163 &create_time_ts,
1164 &access_time_ts,
1165 &write_time_ts,
1166 &change_time_ts,
1167 &ino)) {
1169 errno = SMBC_errno(context, srv->cli);
1170 return -1;
1173 create_time = convert_timespec_to_time_t(create_time_ts);
1174 access_time = convert_timespec_to_time_t(access_time_ts);
1175 write_time = convert_timespec_to_time_t(write_time_ts);
1176 change_time = convert_timespec_to_time_t(change_time_ts);
1178 if (! exclude_dos_mode) {
1179 if (all || all_dos) {
1180 if (determine_size) {
1181 p = talloc_asprintf(ctx,
1182 "%sMODE:0x%x",
1183 (ipc_cli &&
1184 (all || some_nt)
1185 ? ","
1186 : ""),
1187 mode);
1188 if (!p) {
1189 errno = ENOMEM;
1190 return -1;
1192 n = strlen(p);
1193 } else {
1194 n = snprintf(buf, bufsize,
1195 "%sMODE:0x%x",
1196 (ipc_cli &&
1197 (all || some_nt)
1198 ? ","
1199 : ""),
1200 mode);
1202 } else if (strcasecmp_m(name, "mode") == 0) {
1203 if (determine_size) {
1204 p = talloc_asprintf(ctx, "0x%x", mode);
1205 if (!p) {
1206 errno = ENOMEM;
1207 return -1;
1209 n = strlen(p);
1210 } else {
1211 n = snprintf(buf, bufsize,
1212 "0x%x", mode);
1216 if (!determine_size && n > bufsize) {
1217 errno = ERANGE;
1218 return -1;
1220 buf += n;
1221 n_used += n;
1222 bufsize -= n;
1223 n = 0;
1226 if (! exclude_dos_size) {
1227 if (all || all_dos) {
1228 if (determine_size) {
1229 p = talloc_asprintf(
1230 ctx,
1231 ",SIZE:%.0f",
1232 (double)size);
1233 if (!p) {
1234 errno = ENOMEM;
1235 return -1;
1237 n = strlen(p);
1238 } else {
1239 n = snprintf(buf, bufsize,
1240 ",SIZE:%.0f",
1241 (double)size);
1243 } else if (strcasecmp_m(name, "size") == 0) {
1244 if (determine_size) {
1245 p = talloc_asprintf(
1246 ctx,
1247 "%.0f",
1248 (double)size);
1249 if (!p) {
1250 errno = ENOMEM;
1251 return -1;
1253 n = strlen(p);
1254 } else {
1255 n = snprintf(buf, bufsize,
1256 "%.0f",
1257 (double)size);
1261 if (!determine_size && n > bufsize) {
1262 errno = ERANGE;
1263 return -1;
1265 buf += n;
1266 n_used += n;
1267 bufsize -= n;
1268 n = 0;
1271 if (! exclude_dos_create_time &&
1272 attr_strings.create_time_attr != NULL) {
1273 if (all || all_dos) {
1274 if (determine_size) {
1275 p = talloc_asprintf(ctx,
1276 ",%s:%lu",
1277 attr_strings.create_time_attr,
1278 (unsigned long) create_time);
1279 if (!p) {
1280 errno = ENOMEM;
1281 return -1;
1283 n = strlen(p);
1284 } else {
1285 n = snprintf(buf, bufsize,
1286 ",%s:%lu",
1287 attr_strings.create_time_attr,
1288 (unsigned long) create_time);
1290 } else if (strcasecmp_m(name, attr_strings.create_time_attr) == 0) {
1291 if (determine_size) {
1292 p = talloc_asprintf(ctx, "%lu", (unsigned long) create_time);
1293 if (!p) {
1294 errno = ENOMEM;
1295 return -1;
1297 n = strlen(p);
1298 } else {
1299 n = snprintf(buf, bufsize,
1300 "%lu", (unsigned long) create_time);
1304 if (!determine_size && n > bufsize) {
1305 errno = ERANGE;
1306 return -1;
1308 buf += n;
1309 n_used += n;
1310 bufsize -= n;
1311 n = 0;
1314 if (! exclude_dos_access_time) {
1315 if (all || all_dos) {
1316 if (determine_size) {
1317 p = talloc_asprintf(ctx,
1318 ",%s:%lu",
1319 attr_strings.access_time_attr,
1320 (unsigned long) access_time);
1321 if (!p) {
1322 errno = ENOMEM;
1323 return -1;
1325 n = strlen(p);
1326 } else {
1327 n = snprintf(buf, bufsize,
1328 ",%s:%lu",
1329 attr_strings.access_time_attr,
1330 (unsigned long) access_time);
1332 } else if (strcasecmp_m(name, attr_strings.access_time_attr) == 0) {
1333 if (determine_size) {
1334 p = talloc_asprintf(ctx, "%lu", (unsigned long) access_time);
1335 if (!p) {
1336 errno = ENOMEM;
1337 return -1;
1339 n = strlen(p);
1340 } else {
1341 n = snprintf(buf, bufsize,
1342 "%lu", (unsigned long) access_time);
1346 if (!determine_size && n > bufsize) {
1347 errno = ERANGE;
1348 return -1;
1350 buf += n;
1351 n_used += n;
1352 bufsize -= n;
1353 n = 0;
1356 if (! exclude_dos_write_time) {
1357 if (all || all_dos) {
1358 if (determine_size) {
1359 p = talloc_asprintf(ctx,
1360 ",%s:%lu",
1361 attr_strings.write_time_attr,
1362 (unsigned long) write_time);
1363 if (!p) {
1364 errno = ENOMEM;
1365 return -1;
1367 n = strlen(p);
1368 } else {
1369 n = snprintf(buf, bufsize,
1370 ",%s:%lu",
1371 attr_strings.write_time_attr,
1372 (unsigned long) write_time);
1374 } else if (strcasecmp_m(name, attr_strings.write_time_attr) == 0) {
1375 if (determine_size) {
1376 p = talloc_asprintf(ctx, "%lu", (unsigned long) write_time);
1377 if (!p) {
1378 errno = ENOMEM;
1379 return -1;
1381 n = strlen(p);
1382 } else {
1383 n = snprintf(buf, bufsize,
1384 "%lu", (unsigned long) write_time);
1388 if (!determine_size && n > bufsize) {
1389 errno = ERANGE;
1390 return -1;
1392 buf += n;
1393 n_used += n;
1394 bufsize -= n;
1395 n = 0;
1398 if (! exclude_dos_change_time) {
1399 if (all || all_dos) {
1400 if (determine_size) {
1401 p = talloc_asprintf(ctx,
1402 ",%s:%lu",
1403 attr_strings.change_time_attr,
1404 (unsigned long) change_time);
1405 if (!p) {
1406 errno = ENOMEM;
1407 return -1;
1409 n = strlen(p);
1410 } else {
1411 n = snprintf(buf, bufsize,
1412 ",%s:%lu",
1413 attr_strings.change_time_attr,
1414 (unsigned long) change_time);
1416 } else if (strcasecmp_m(name, attr_strings.change_time_attr) == 0) {
1417 if (determine_size) {
1418 p = talloc_asprintf(ctx, "%lu", (unsigned long) change_time);
1419 if (!p) {
1420 errno = ENOMEM;
1421 return -1;
1423 n = strlen(p);
1424 } else {
1425 n = snprintf(buf, bufsize,
1426 "%lu", (unsigned long) change_time);
1430 if (!determine_size && n > bufsize) {
1431 errno = ERANGE;
1432 return -1;
1434 buf += n;
1435 n_used += n;
1436 bufsize -= n;
1437 n = 0;
1440 if (! exclude_dos_inode) {
1441 if (all || all_dos) {
1442 if (determine_size) {
1443 p = talloc_asprintf(
1444 ctx,
1445 ",INODE:%.0f",
1446 (double)ino);
1447 if (!p) {
1448 errno = ENOMEM;
1449 return -1;
1451 n = strlen(p);
1452 } else {
1453 n = snprintf(buf, bufsize,
1454 ",INODE:%.0f",
1455 (double) ino);
1457 } else if (strcasecmp_m(name, "inode") == 0) {
1458 if (determine_size) {
1459 p = talloc_asprintf(
1460 ctx,
1461 "%.0f",
1462 (double) ino);
1463 if (!p) {
1464 errno = ENOMEM;
1465 return -1;
1467 n = strlen(p);
1468 } else {
1469 n = snprintf(buf, bufsize,
1470 "%.0f",
1471 (double) ino);
1475 if (!determine_size && n > bufsize) {
1476 errno = ERANGE;
1477 return -1;
1479 buf += n;
1480 n_used += n;
1481 bufsize -= n;
1482 n = 0;
1485 /* Restore name pointer to its original value */
1486 name -= 16;
1489 if (n_used == 0) {
1490 errno = ENOATTR;
1491 return -1;
1494 return n_used;
1497 /*****************************************************
1498 set the ACLs on a file given an ascii description
1499 *******************************************************/
1500 static int
1501 cacl_set(SMBCCTX *context,
1502 TALLOC_CTX *ctx,
1503 struct cli_state *cli,
1504 struct cli_state *ipc_cli,
1505 struct policy_handle *pol,
1506 const char *filename,
1507 char *the_acl,
1508 int mode,
1509 int flags)
1511 uint16_t fnum = (uint16_t)-1;
1512 int err = 0;
1513 struct security_descriptor *sd = NULL, *old;
1514 struct security_acl *dacl = NULL;
1515 struct dom_sid *owner_sid = NULL;
1516 struct dom_sid *group_sid = NULL;
1517 uint32 i, j;
1518 size_t sd_size;
1519 int ret = 0;
1520 char *p;
1521 bool numeric = True;
1522 char *targetpath = NULL;
1523 struct cli_state *targetcli = NULL;
1524 NTSTATUS status;
1526 /* the_acl will be null for REMOVE_ALL operations */
1527 if (the_acl) {
1528 numeric = ((p = strchr(the_acl, ':')) != NULL &&
1529 p > the_acl &&
1530 p[-1] != '+');
1532 /* if this is to set the entire ACL... */
1533 if (*the_acl == '*') {
1534 /* ... then increment past the first colon */
1535 the_acl = p + 1;
1538 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric, the_acl);
1539 if (!sd) {
1540 errno = EINVAL;
1541 return -1;
1545 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
1546 that doesn't deref sd */
1548 if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
1549 errno = EINVAL;
1550 return -1;
1553 status = cli_resolve_path(ctx, "", context->internal->auth_info,
1554 cli, filename, &targetcli, &targetpath);
1555 if (!NT_STATUS_IS_OK(status)) {
1556 DEBUG(5,("cacl_set: Could not resolve %s\n", filename));
1557 errno = ENOENT;
1558 return -1;
1561 /* The desired access below is the only one I could find that works
1562 with NT4, W2KP and Samba */
1564 status = cli_ntcreate(targetcli, targetpath, 0, CREATE_ACCESS_READ, 0,
1565 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
1566 0x0, 0x0, &fnum);
1567 if (!NT_STATUS_IS_OK(status)) {
1568 DEBUG(5, ("cacl_set failed to open %s: %s\n",
1569 targetpath, nt_errstr(status)));
1570 errno = 0;
1571 return -1;
1574 status = cli_query_secdesc(targetcli, fnum, ctx, &old);
1575 if (!NT_STATUS_IS_OK(status)) {
1576 DEBUG(5,("cacl_set Failed to query old descriptor of %s: %s\n",
1577 targetpath, nt_errstr(status)));
1578 errno = 0;
1579 return -1;
1582 cli_close(targetcli, fnum);
1584 switch (mode) {
1585 case SMBC_XATTR_MODE_REMOVE_ALL:
1586 old->dacl->num_aces = 0;
1587 dacl = old->dacl;
1588 break;
1590 case SMBC_XATTR_MODE_REMOVE:
1591 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1592 bool found = False;
1594 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
1595 if (sec_ace_equal(&sd->dacl->aces[i],
1596 &old->dacl->aces[j])) {
1597 uint32 k;
1598 for (k=j; k<old->dacl->num_aces-1;k++) {
1599 old->dacl->aces[k] =
1600 old->dacl->aces[k+1];
1602 old->dacl->num_aces--;
1603 found = True;
1604 dacl = old->dacl;
1605 break;
1609 if (!found) {
1610 err = ENOATTR;
1611 ret = -1;
1612 goto failed;
1615 break;
1617 case SMBC_XATTR_MODE_ADD:
1618 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1619 bool found = False;
1621 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
1622 if (dom_sid_equal(&sd->dacl->aces[i].trustee,
1623 &old->dacl->aces[j].trustee)) {
1624 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
1625 err = EEXIST;
1626 ret = -1;
1627 goto failed;
1629 old->dacl->aces[j] = sd->dacl->aces[i];
1630 ret = -1;
1631 found = True;
1635 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
1636 err = ENOATTR;
1637 ret = -1;
1638 goto failed;
1641 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1642 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
1645 dacl = old->dacl;
1646 break;
1648 case SMBC_XATTR_MODE_SET:
1649 old = sd;
1650 owner_sid = old->owner_sid;
1651 group_sid = old->group_sid;
1652 dacl = old->dacl;
1653 break;
1655 case SMBC_XATTR_MODE_CHOWN:
1656 owner_sid = sd->owner_sid;
1657 break;
1659 case SMBC_XATTR_MODE_CHGRP:
1660 group_sid = sd->group_sid;
1661 break;
1664 /* Denied ACE entries must come before allowed ones */
1665 sort_acl(old->dacl);
1667 /* Create new security descriptor and set it */
1668 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
1669 owner_sid, group_sid, NULL, dacl, &sd_size);
1671 status = cli_ntcreate(targetcli, targetpath, 0,
1672 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS, 0,
1673 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
1674 0x0, 0x0, &fnum);
1675 if (!NT_STATUS_IS_OK(status)) {
1676 DEBUG(5, ("cacl_set failed to open %s: %s\n",
1677 targetpath, nt_errstr(status)));
1678 errno = 0;
1679 return -1;
1682 status = cli_set_secdesc(targetcli, fnum, sd);
1683 if (!NT_STATUS_IS_OK(status)) {
1684 DEBUG(5, ("ERROR: secdesc set failed: %s\n",
1685 nt_errstr(status)));
1686 ret = -1;
1689 /* Clean up */
1691 failed:
1692 cli_close(targetcli, fnum);
1694 if (err != 0) {
1695 errno = err;
1698 return ret;
1703 SMBC_setxattr_ctx(SMBCCTX *context,
1704 const char *fname,
1705 const char *name,
1706 const void *value,
1707 size_t size,
1708 int flags)
1710 int ret;
1711 int ret2;
1712 SMBCSRV *srv = NULL;
1713 SMBCSRV *ipc_srv = NULL;
1714 char *server = NULL;
1715 char *share = NULL;
1716 char *user = NULL;
1717 char *password = NULL;
1718 char *workgroup = NULL;
1719 char *path = NULL;
1720 DOS_ATTR_DESC *dad = NULL;
1721 struct {
1722 const char * create_time_attr;
1723 const char * access_time_attr;
1724 const char * write_time_attr;
1725 const char * change_time_attr;
1726 } attr_strings;
1727 TALLOC_CTX *frame = talloc_stackframe();
1729 if (!context || !context->internal->initialized) {
1730 errno = EINVAL; /* Best I can think of ... */
1731 TALLOC_FREE(frame);
1732 return -1;
1735 if (!fname) {
1736 errno = EINVAL;
1737 TALLOC_FREE(frame);
1738 return -1;
1741 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
1742 fname, name, (int) size, (const char*)value));
1744 if (SMBC_parse_path(frame,
1745 context,
1746 fname,
1747 &workgroup,
1748 &server,
1749 &share,
1750 &path,
1751 &user,
1752 &password,
1753 NULL)) {
1754 errno = EINVAL;
1755 TALLOC_FREE(frame);
1756 return -1;
1759 if (!user || user[0] == (char)0) {
1760 user = talloc_strdup(frame, smbc_getUser(context));
1761 if (!user) {
1762 errno = ENOMEM;
1763 TALLOC_FREE(frame);
1764 return -1;
1768 srv = SMBC_server(frame, context, True,
1769 server, share, &workgroup, &user, &password);
1770 if (!srv) {
1771 TALLOC_FREE(frame);
1772 return -1; /* errno set by SMBC_server */
1775 if (! srv->no_nt_session) {
1776 ipc_srv = SMBC_attr_server(frame, context, server, share,
1777 &workgroup, &user, &password);
1778 if (! ipc_srv) {
1779 srv->no_nt_session = True;
1781 } else {
1782 ipc_srv = NULL;
1786 * Are they asking to set the entire set of known attributes?
1788 if (strcasecmp_m(name, "system.*") == 0 ||
1789 strcasecmp_m(name, "system.*+") == 0) {
1790 /* Yup. */
1791 char *namevalue =
1792 talloc_asprintf(talloc_tos(), "%s:%s",
1793 name+7, (const char *) value);
1794 if (! namevalue) {
1795 errno = ENOMEM;
1796 ret = -1;
1797 TALLOC_FREE(frame);
1798 return -1;
1801 if (ipc_srv) {
1802 ret = cacl_set(context, talloc_tos(), srv->cli,
1803 ipc_srv->cli, &ipc_srv->pol, path,
1804 namevalue,
1805 (*namevalue == '*'
1806 ? SMBC_XATTR_MODE_SET
1807 : SMBC_XATTR_MODE_ADD),
1808 flags);
1809 } else {
1810 ret = 0;
1813 /* get a DOS Attribute Descriptor with current attributes */
1814 dad = dos_attr_query(context, talloc_tos(), path, srv);
1815 if (dad) {
1816 /* Overwrite old with new, using what was provided */
1817 dos_attr_parse(context, dad, srv, namevalue);
1819 /* Set the new DOS attributes */
1820 if (! SMBC_setatr(context, srv, path,
1821 dad->create_time,
1822 dad->access_time,
1823 dad->write_time,
1824 dad->change_time,
1825 dad->mode)) {
1827 /* cause failure if NT failed too */
1828 dad = NULL;
1832 /* we only fail if both NT and DOS sets failed */
1833 if (ret < 0 && ! dad) {
1834 ret = -1; /* in case dad was null */
1836 else {
1837 ret = 0;
1840 TALLOC_FREE(frame);
1841 return ret;
1845 * Are they asking to set an access control element or to set
1846 * the entire access control list?
1848 if (strcasecmp_m(name, "system.nt_sec_desc.*") == 0 ||
1849 strcasecmp_m(name, "system.nt_sec_desc.*+") == 0 ||
1850 strcasecmp_m(name, "system.nt_sec_desc.revision") == 0 ||
1851 strncasecmp_m(name, "system.nt_sec_desc.acl", 22) == 0 ||
1852 strncasecmp_m(name, "system.nt_sec_desc.acl+", 23) == 0) {
1854 /* Yup. */
1855 char *namevalue =
1856 talloc_asprintf(talloc_tos(), "%s:%s",
1857 name+19, (const char *) value);
1859 if (! ipc_srv) {
1860 ret = -1; /* errno set by SMBC_server() */
1862 else if (! namevalue) {
1863 errno = ENOMEM;
1864 ret = -1;
1865 } else {
1866 ret = cacl_set(context, talloc_tos(), srv->cli,
1867 ipc_srv->cli, &ipc_srv->pol, path,
1868 namevalue,
1869 (*namevalue == '*'
1870 ? SMBC_XATTR_MODE_SET
1871 : SMBC_XATTR_MODE_ADD),
1872 flags);
1874 TALLOC_FREE(frame);
1875 return ret;
1879 * Are they asking to set the owner?
1881 if (strcasecmp_m(name, "system.nt_sec_desc.owner") == 0 ||
1882 strcasecmp_m(name, "system.nt_sec_desc.owner+") == 0) {
1884 /* Yup. */
1885 char *namevalue =
1886 talloc_asprintf(talloc_tos(), "%s:%s",
1887 name+19, (const char *) value);
1889 if (! ipc_srv) {
1890 ret = -1; /* errno set by SMBC_server() */
1892 else if (! namevalue) {
1893 errno = ENOMEM;
1894 ret = -1;
1895 } else {
1896 ret = cacl_set(context, talloc_tos(), srv->cli,
1897 ipc_srv->cli, &ipc_srv->pol, path,
1898 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
1900 TALLOC_FREE(frame);
1901 return ret;
1905 * Are they asking to set the group?
1907 if (strcasecmp_m(name, "system.nt_sec_desc.group") == 0 ||
1908 strcasecmp_m(name, "system.nt_sec_desc.group+") == 0) {
1910 /* Yup. */
1911 char *namevalue =
1912 talloc_asprintf(talloc_tos(), "%s:%s",
1913 name+19, (const char *) value);
1915 if (! ipc_srv) {
1916 /* errno set by SMBC_server() */
1917 ret = -1;
1919 else if (! namevalue) {
1920 errno = ENOMEM;
1921 ret = -1;
1922 } else {
1923 ret = cacl_set(context, talloc_tos(), srv->cli,
1924 ipc_srv->cli, &ipc_srv->pol, path,
1925 namevalue, SMBC_XATTR_MODE_CHGRP, 0);
1927 TALLOC_FREE(frame);
1928 return ret;
1931 /* Determine whether to use old-style or new-style attribute names */
1932 if (context->internal->full_time_names) {
1933 /* new-style names */
1934 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
1935 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
1936 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
1937 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
1938 } else {
1939 /* old-style names */
1940 attr_strings.create_time_attr = NULL;
1941 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
1942 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
1943 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
1947 * Are they asking to set a DOS attribute?
1949 if (strcasecmp_m(name, "system.dos_attr.*") == 0 ||
1950 strcasecmp_m(name, "system.dos_attr.mode") == 0 ||
1951 (attr_strings.create_time_attr != NULL &&
1952 strcasecmp_m(name, attr_strings.create_time_attr) == 0) ||
1953 strcasecmp_m(name, attr_strings.access_time_attr) == 0 ||
1954 strcasecmp_m(name, attr_strings.write_time_attr) == 0 ||
1955 strcasecmp_m(name, attr_strings.change_time_attr) == 0) {
1957 /* get a DOS Attribute Descriptor with current attributes */
1958 dad = dos_attr_query(context, talloc_tos(), path, srv);
1959 if (dad) {
1960 char *namevalue =
1961 talloc_asprintf(talloc_tos(), "%s:%s",
1962 name+16, (const char *) value);
1963 if (! namevalue) {
1964 errno = ENOMEM;
1965 ret = -1;
1966 } else {
1967 /* Overwrite old with provided new params */
1968 dos_attr_parse(context, dad, srv, namevalue);
1970 /* Set the new DOS attributes */
1971 ret2 = SMBC_setatr(context, srv, path,
1972 dad->create_time,
1973 dad->access_time,
1974 dad->write_time,
1975 dad->change_time,
1976 dad->mode);
1978 /* ret2 has True (success) / False (failure) */
1979 if (ret2) {
1980 ret = 0;
1981 } else {
1982 ret = -1;
1985 } else {
1986 ret = -1;
1989 TALLOC_FREE(frame);
1990 return ret;
1993 /* Unsupported attribute name */
1994 errno = EINVAL;
1995 TALLOC_FREE(frame);
1996 return -1;
2000 SMBC_getxattr_ctx(SMBCCTX *context,
2001 const char *fname,
2002 const char *name,
2003 const void *value,
2004 size_t size)
2006 int ret;
2007 SMBCSRV *srv = NULL;
2008 SMBCSRV *ipc_srv = NULL;
2009 char *server = NULL;
2010 char *share = NULL;
2011 char *user = NULL;
2012 char *password = NULL;
2013 char *workgroup = NULL;
2014 char *path = NULL;
2015 struct {
2016 const char * create_time_attr;
2017 const char * access_time_attr;
2018 const char * write_time_attr;
2019 const char * change_time_attr;
2020 } attr_strings;
2021 TALLOC_CTX *frame = talloc_stackframe();
2023 if (!context || !context->internal->initialized) {
2024 errno = EINVAL; /* Best I can think of ... */
2025 TALLOC_FREE(frame);
2026 return -1;
2029 if (!fname) {
2030 errno = EINVAL;
2031 TALLOC_FREE(frame);
2032 return -1;
2035 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
2037 if (SMBC_parse_path(frame,
2038 context,
2039 fname,
2040 &workgroup,
2041 &server,
2042 &share,
2043 &path,
2044 &user,
2045 &password,
2046 NULL)) {
2047 errno = EINVAL;
2048 TALLOC_FREE(frame);
2049 return -1;
2052 if (!user || user[0] == (char)0) {
2053 user = talloc_strdup(frame, smbc_getUser(context));
2054 if (!user) {
2055 errno = ENOMEM;
2056 TALLOC_FREE(frame);
2057 return -1;
2061 srv = SMBC_server(frame, context, True,
2062 server, share, &workgroup, &user, &password);
2063 if (!srv) {
2064 TALLOC_FREE(frame);
2065 return -1; /* errno set by SMBC_server */
2068 if (! srv->no_nt_session) {
2069 ipc_srv = SMBC_attr_server(frame, context, server, share,
2070 &workgroup, &user, &password);
2071 if (! ipc_srv) {
2072 srv->no_nt_session = True;
2074 } else {
2075 ipc_srv = NULL;
2078 /* Determine whether to use old-style or new-style attribute names */
2079 if (context->internal->full_time_names) {
2080 /* new-style names */
2081 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
2082 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
2083 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
2084 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
2085 } else {
2086 /* old-style names */
2087 attr_strings.create_time_attr = NULL;
2088 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
2089 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
2090 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
2093 /* Are they requesting a supported attribute? */
2094 if (strcasecmp_m(name, "system.*") == 0 ||
2095 strncasecmp_m(name, "system.*!", 9) == 0 ||
2096 strcasecmp_m(name, "system.*+") == 0 ||
2097 strncasecmp_m(name, "system.*+!", 10) == 0 ||
2098 strcasecmp_m(name, "system.nt_sec_desc.*") == 0 ||
2099 strncasecmp_m(name, "system.nt_sec_desc.*!", 21) == 0 ||
2100 strcasecmp_m(name, "system.nt_sec_desc.*+") == 0 ||
2101 strncasecmp_m(name, "system.nt_sec_desc.*+!", 22) == 0 ||
2102 strcasecmp_m(name, "system.nt_sec_desc.revision") == 0 ||
2103 strcasecmp_m(name, "system.nt_sec_desc.owner") == 0 ||
2104 strcasecmp_m(name, "system.nt_sec_desc.owner+") == 0 ||
2105 strcasecmp_m(name, "system.nt_sec_desc.group") == 0 ||
2106 strcasecmp_m(name, "system.nt_sec_desc.group+") == 0 ||
2107 strncasecmp_m(name, "system.nt_sec_desc.acl", 22) == 0 ||
2108 strncasecmp_m(name, "system.nt_sec_desc.acl+", 23) == 0 ||
2109 strcasecmp_m(name, "system.dos_attr.*") == 0 ||
2110 strncasecmp_m(name, "system.dos_attr.*!", 18) == 0 ||
2111 strcasecmp_m(name, "system.dos_attr.mode") == 0 ||
2112 strcasecmp_m(name, "system.dos_attr.size") == 0 ||
2113 (attr_strings.create_time_attr != NULL &&
2114 strcasecmp_m(name, attr_strings.create_time_attr) == 0) ||
2115 strcasecmp_m(name, attr_strings.access_time_attr) == 0 ||
2116 strcasecmp_m(name, attr_strings.write_time_attr) == 0 ||
2117 strcasecmp_m(name, attr_strings.change_time_attr) == 0 ||
2118 strcasecmp_m(name, "system.dos_attr.inode") == 0) {
2120 /* Yup. */
2121 const char *filename = name;
2122 ret = cacl_get(context, talloc_tos(), srv,
2123 ipc_srv == NULL ? NULL : ipc_srv->cli,
2124 &ipc_srv->pol, path,
2125 filename,
2126 discard_const_p(char, value),
2127 size);
2128 if (ret < 0 && errno == 0) {
2129 errno = SMBC_errno(context, srv->cli);
2131 TALLOC_FREE(frame);
2132 return ret;
2135 /* Unsupported attribute name */
2136 errno = EINVAL;
2137 TALLOC_FREE(frame);
2138 return -1;
2143 SMBC_removexattr_ctx(SMBCCTX *context,
2144 const char *fname,
2145 const char *name)
2147 int ret;
2148 SMBCSRV *srv = NULL;
2149 SMBCSRV *ipc_srv = NULL;
2150 char *server = NULL;
2151 char *share = NULL;
2152 char *user = NULL;
2153 char *password = NULL;
2154 char *workgroup = NULL;
2155 char *path = NULL;
2156 TALLOC_CTX *frame = talloc_stackframe();
2158 if (!context || !context->internal->initialized) {
2159 errno = EINVAL; /* Best I can think of ... */
2160 TALLOC_FREE(frame);
2161 return -1;
2164 if (!fname) {
2165 errno = EINVAL;
2166 TALLOC_FREE(frame);
2167 return -1;
2170 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
2172 if (SMBC_parse_path(frame,
2173 context,
2174 fname,
2175 &workgroup,
2176 &server,
2177 &share,
2178 &path,
2179 &user,
2180 &password,
2181 NULL)) {
2182 errno = EINVAL;
2183 TALLOC_FREE(frame);
2184 return -1;
2187 if (!user || user[0] == (char)0) {
2188 user = talloc_strdup(frame, smbc_getUser(context));
2189 if (!user) {
2190 errno = ENOMEM;
2191 TALLOC_FREE(frame);
2192 return -1;
2196 srv = SMBC_server(frame, context, True,
2197 server, share, &workgroup, &user, &password);
2198 if (!srv) {
2199 TALLOC_FREE(frame);
2200 return -1; /* errno set by SMBC_server */
2203 if (! srv->no_nt_session) {
2204 ipc_srv = SMBC_attr_server(frame, context, server, share,
2205 &workgroup, &user, &password);
2206 if (! ipc_srv) {
2207 srv->no_nt_session = True;
2209 } else {
2210 ipc_srv = NULL;
2213 if (! ipc_srv) {
2214 TALLOC_FREE(frame);
2215 return -1; /* errno set by SMBC_attr_server */
2218 /* Are they asking to set the entire ACL? */
2219 if (strcasecmp_m(name, "system.nt_sec_desc.*") == 0 ||
2220 strcasecmp_m(name, "system.nt_sec_desc.*+") == 0) {
2222 /* Yup. */
2223 ret = cacl_set(context, talloc_tos(), srv->cli,
2224 ipc_srv->cli, &ipc_srv->pol, path,
2225 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
2226 TALLOC_FREE(frame);
2227 return ret;
2231 * Are they asking to remove one or more spceific security descriptor
2232 * attributes?
2234 if (strcasecmp_m(name, "system.nt_sec_desc.revision") == 0 ||
2235 strcasecmp_m(name, "system.nt_sec_desc.owner") == 0 ||
2236 strcasecmp_m(name, "system.nt_sec_desc.owner+") == 0 ||
2237 strcasecmp_m(name, "system.nt_sec_desc.group") == 0 ||
2238 strcasecmp_m(name, "system.nt_sec_desc.group+") == 0 ||
2239 strncasecmp_m(name, "system.nt_sec_desc.acl", 22) == 0 ||
2240 strncasecmp_m(name, "system.nt_sec_desc.acl+", 23) == 0) {
2242 /* Yup. */
2243 ret = cacl_set(context, talloc_tos(), srv->cli,
2244 ipc_srv->cli, &ipc_srv->pol, path,
2245 discard_const_p(char, name) + 19,
2246 SMBC_XATTR_MODE_REMOVE, 0);
2247 TALLOC_FREE(frame);
2248 return ret;
2251 /* Unsupported attribute name */
2252 errno = EINVAL;
2253 TALLOC_FREE(frame);
2254 return -1;
2258 SMBC_listxattr_ctx(SMBCCTX *context,
2259 const char *fname,
2260 char *list,
2261 size_t size)
2264 * This isn't quite what listxattr() is supposed to do. This returns
2265 * the complete set of attribute names, always, rather than only those
2266 * attribute names which actually exist for a file. Hmmm...
2268 size_t retsize;
2269 const char supported_old[] =
2270 "system.*\0"
2271 "system.*+\0"
2272 "system.nt_sec_desc.revision\0"
2273 "system.nt_sec_desc.owner\0"
2274 "system.nt_sec_desc.owner+\0"
2275 "system.nt_sec_desc.group\0"
2276 "system.nt_sec_desc.group+\0"
2277 "system.nt_sec_desc.acl.*\0"
2278 "system.nt_sec_desc.acl\0"
2279 "system.nt_sec_desc.acl+\0"
2280 "system.nt_sec_desc.*\0"
2281 "system.nt_sec_desc.*+\0"
2282 "system.dos_attr.*\0"
2283 "system.dos_attr.mode\0"
2284 "system.dos_attr.c_time\0"
2285 "system.dos_attr.a_time\0"
2286 "system.dos_attr.m_time\0"
2288 const char supported_new[] =
2289 "system.*\0"
2290 "system.*+\0"
2291 "system.nt_sec_desc.revision\0"
2292 "system.nt_sec_desc.owner\0"
2293 "system.nt_sec_desc.owner+\0"
2294 "system.nt_sec_desc.group\0"
2295 "system.nt_sec_desc.group+\0"
2296 "system.nt_sec_desc.acl.*\0"
2297 "system.nt_sec_desc.acl\0"
2298 "system.nt_sec_desc.acl+\0"
2299 "system.nt_sec_desc.*\0"
2300 "system.nt_sec_desc.*+\0"
2301 "system.dos_attr.*\0"
2302 "system.dos_attr.mode\0"
2303 "system.dos_attr.create_time\0"
2304 "system.dos_attr.access_time\0"
2305 "system.dos_attr.write_time\0"
2306 "system.dos_attr.change_time\0"
2308 const char * supported;
2310 if (context->internal->full_time_names) {
2311 supported = supported_new;
2312 retsize = sizeof(supported_new);
2313 } else {
2314 supported = supported_old;
2315 retsize = sizeof(supported_old);
2318 if (size == 0) {
2319 return retsize;
2322 if (retsize > size) {
2323 errno = ERANGE;
2324 return -1;
2327 /* this can't be strcpy() because there are embedded null characters */
2328 memcpy(list, supported, retsize);
2329 return retsize;