s3: Convert cli_set_secdesc to cli_trans
[Samba/gbeck.git] / source3 / libsmb / libsmb_xattr.c
blob155a6b974171b595ea880682a97a9439fff18582
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 "libsmbclient.h"
27 #include "libsmb_internal.h"
28 #include "../librpc/gen_ndr/ndr_lsa.h"
29 #include "rpc_client/cli_lsarpc.h"
30 #include "../libcli/security/security.h"
33 * Find an lsa pipe handle associated with a cli struct.
35 static struct rpc_pipe_client *
36 find_lsa_pipe_hnd(struct cli_state *ipc_cli)
38 struct rpc_pipe_client *pipe_hnd;
40 for (pipe_hnd = ipc_cli->pipe_list;
41 pipe_hnd;
42 pipe_hnd = pipe_hnd->next) {
43 if (ndr_syntax_id_equal(&pipe_hnd->abstract_syntax,
44 &ndr_table_lsarpc.syntax_id)) {
45 return pipe_hnd;
48 return NULL;
52 * Sort ACEs according to the documentation at
53 * http://support.microsoft.com/kb/269175, at least as far as it defines the
54 * order.
57 static int
58 ace_compare(struct security_ace *ace1,
59 struct security_ace *ace2)
61 bool b1;
62 bool b2;
64 /* If the ACEs are equal, we have nothing more to do. */
65 if (sec_ace_equal(ace1, ace2)) {
66 return 0;
69 /* Inherited follow non-inherited */
70 b1 = ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
71 b2 = ((ace2->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
72 if (b1 != b2) {
73 return (b1 ? 1 : -1);
77 * What shall we do with AUDITs and ALARMs? It's undefined. We'll
78 * sort them after DENY and ALLOW.
80 b1 = (ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
81 ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
82 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED &&
83 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
84 b2 = (ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
85 ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
86 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED &&
87 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
88 if (b1 != b2) {
89 return (b1 ? 1 : -1);
92 /* Allowed ACEs follow denied ACEs */
93 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
94 ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
95 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
96 ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
97 if (b1 != b2) {
98 return (b1 ? 1 : -1);
102 * ACEs applying to an entity's object follow those applying to the
103 * entity itself
105 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
106 ace1->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
107 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
108 ace2->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
109 if (b1 != b2) {
110 return (b1 ? 1 : -1);
114 * If we get this far, the ACEs are similar as far as the
115 * characteristics we typically care about (those defined by the
116 * referenced MS document). We'll now sort by characteristics that
117 * just seems reasonable.
120 if (ace1->type != ace2->type) {
121 return ace2->type - ace1->type;
124 if (dom_sid_compare(&ace1->trustee, &ace2->trustee)) {
125 return dom_sid_compare(&ace1->trustee, &ace2->trustee);
128 if (ace1->flags != ace2->flags) {
129 return ace1->flags - ace2->flags;
132 if (ace1->access_mask != ace2->access_mask) {
133 return ace1->access_mask - ace2->access_mask;
136 if (ace1->size != ace2->size) {
137 return ace1->size - ace2->size;
140 return memcmp(ace1, ace2, sizeof(struct security_ace));
144 static void
145 sort_acl(struct security_acl *the_acl)
147 uint32 i;
148 if (!the_acl) return;
150 TYPESAFE_QSORT(the_acl->aces, the_acl->num_aces, ace_compare);
152 for (i=1;i<the_acl->num_aces;) {
153 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
154 int j;
155 for (j=i; j<the_acl->num_aces-1; j++) {
156 the_acl->aces[j] = the_acl->aces[j+1];
158 the_acl->num_aces--;
159 } else {
160 i++;
165 /* convert a SID to a string, either numeric or username/group */
166 static void
167 convert_sid_to_string(struct cli_state *ipc_cli,
168 struct policy_handle *pol,
169 fstring str,
170 bool numeric,
171 struct dom_sid *sid)
173 char **domains = NULL;
174 char **names = NULL;
175 enum lsa_SidType *types = NULL;
176 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
177 TALLOC_CTX *ctx;
179 sid_to_fstring(str, sid);
181 if (numeric) {
182 return; /* no lookup desired */
185 if (!pipe_hnd) {
186 return;
189 /* Ask LSA to convert the sid to a name */
191 ctx = talloc_stackframe();
193 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd, ctx,
194 pol, 1, sid, &domains,
195 &names, &types)) ||
196 !domains || !domains[0] || !names || !names[0]) {
197 TALLOC_FREE(ctx);
198 return;
201 /* Converted OK */
203 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
204 domains[0], lp_winbind_separator(),
205 names[0]);
207 TALLOC_FREE(ctx);
210 /* convert a string to a SID, either numeric or username/group */
211 static bool
212 convert_string_to_sid(struct cli_state *ipc_cli,
213 struct policy_handle *pol,
214 bool numeric,
215 struct dom_sid *sid,
216 const char *str)
218 enum lsa_SidType *types = NULL;
219 struct dom_sid *sids = NULL;
220 bool result = True;
221 TALLOC_CTX *ctx = NULL;
222 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
224 if (!pipe_hnd) {
225 return False;
228 if (numeric) {
229 if (strncmp(str, "S-", 2) == 0) {
230 return string_to_sid(sid, str);
233 result = False;
234 goto done;
237 ctx = talloc_stackframe();
238 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ctx,
239 pol, 1, &str,
240 NULL, 1, &sids,
241 &types))) {
242 result = False;
243 goto done;
246 sid_copy(sid, &sids[0]);
247 done:
248 TALLOC_FREE(ctx);
249 return result;
253 /* parse an struct security_ace in the same format as print_ace() */
254 static bool
255 parse_ace(struct cli_state *ipc_cli,
256 struct policy_handle *pol,
257 struct security_ace *ace,
258 bool numeric,
259 char *str)
261 char *p;
262 const char *cp;
263 char *tok;
264 unsigned int atype;
265 unsigned int aflags;
266 unsigned int amask;
267 struct dom_sid sid;
268 uint32_t mask;
269 const struct perm_value *v;
270 struct perm_value {
271 const char perm[7];
272 uint32 mask;
274 TALLOC_CTX *frame = talloc_stackframe();
276 /* These values discovered by inspection */
277 static const struct perm_value special_values[] = {
278 { "R", 0x00120089 },
279 { "W", 0x00120116 },
280 { "X", 0x001200a0 },
281 { "D", 0x00010000 },
282 { "P", 0x00040000 },
283 { "O", 0x00080000 },
284 { "", 0 },
287 static const struct perm_value standard_values[] = {
288 { "READ", 0x001200a9 },
289 { "CHANGE", 0x001301bf },
290 { "FULL", 0x001f01ff },
291 { "", 0 },
294 ZERO_STRUCTP(ace);
295 p = strchr_m(str,':');
296 if (!p) {
297 TALLOC_FREE(frame);
298 return False;
300 *p = '\0';
301 p++;
302 /* Try to parse numeric form */
304 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
305 convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
306 goto done;
309 /* Try to parse text form */
311 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
312 TALLOC_FREE(frame);
313 return false;
316 cp = p;
317 if (!next_token_talloc(frame, &cp, &tok, "/")) {
318 TALLOC_FREE(frame);
319 return false;
322 if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
323 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
324 } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
325 atype = SEC_ACE_TYPE_ACCESS_DENIED;
326 } else {
327 TALLOC_FREE(frame);
328 return false;
331 /* Only numeric form accepted for flags at present */
333 if (!(next_token_talloc(frame, &cp, &tok, "/") &&
334 sscanf(tok, "%i", &aflags))) {
335 TALLOC_FREE(frame);
336 return false;
339 if (!next_token_talloc(frame, &cp, &tok, "/")) {
340 TALLOC_FREE(frame);
341 return false;
344 if (strncmp(tok, "0x", 2) == 0) {
345 if (sscanf(tok, "%i", &amask) != 1) {
346 TALLOC_FREE(frame);
347 return false;
349 goto done;
352 for (v = standard_values; v->perm; v++) {
353 if (strcmp(tok, v->perm) == 0) {
354 amask = v->mask;
355 goto done;
359 p = tok;
361 while(*p) {
362 bool found = False;
364 for (v = special_values; v->perm; v++) {
365 if (v->perm[0] == *p) {
366 amask |= v->mask;
367 found = True;
371 if (!found) {
372 TALLOC_FREE(frame);
373 return false;
375 p++;
378 if (*p) {
379 TALLOC_FREE(frame);
380 return false;
383 done:
384 mask = amask;
385 init_sec_ace(ace, &sid, atype, mask, aflags);
386 TALLOC_FREE(frame);
387 return true;
390 /* add an struct security_ace to a list of struct security_aces in a struct security_acl */
391 static bool
392 add_ace(struct security_acl **the_acl,
393 struct security_ace *ace,
394 TALLOC_CTX *ctx)
396 struct security_acl *newacl;
397 struct security_ace *aces;
399 if (! *the_acl) {
400 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
401 return True;
404 if ((aces = SMB_CALLOC_ARRAY(struct security_ace,
405 1+(*the_acl)->num_aces)) == NULL) {
406 return False;
408 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(struct security_ace));
409 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(struct security_ace));
410 newacl = make_sec_acl(ctx, (*the_acl)->revision,
411 1+(*the_acl)->num_aces, aces);
412 SAFE_FREE(aces);
413 (*the_acl) = newacl;
414 return True;
418 /* parse a ascii version of a security descriptor */
419 static struct security_descriptor *
420 sec_desc_parse(TALLOC_CTX *ctx,
421 struct cli_state *ipc_cli,
422 struct policy_handle *pol,
423 bool numeric,
424 const char *str)
426 const char *p = str;
427 char *tok;
428 struct security_descriptor *ret = NULL;
429 size_t sd_size;
430 struct dom_sid *group_sid=NULL;
431 struct dom_sid *owner_sid=NULL;
432 struct security_acl *dacl=NULL;
433 int revision=1;
435 while (next_token_talloc(ctx, &p, &tok, "\t,\r\n")) {
437 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
438 revision = strtol(tok+9, NULL, 16);
439 continue;
442 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
443 if (owner_sid) {
444 DEBUG(5,("OWNER specified more than once!\n"));
445 goto done;
447 owner_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
448 if (!owner_sid ||
449 !convert_string_to_sid(ipc_cli, pol,
450 numeric,
451 owner_sid, tok+6)) {
452 DEBUG(5, ("Failed to parse owner sid\n"));
453 goto done;
455 continue;
458 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
459 if (owner_sid) {
460 DEBUG(5,("OWNER specified more than once!\n"));
461 goto done;
463 owner_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
464 if (!owner_sid ||
465 !convert_string_to_sid(ipc_cli, pol,
466 False,
467 owner_sid, tok+7)) {
468 DEBUG(5, ("Failed to parse owner sid\n"));
469 goto done;
471 continue;
474 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
475 if (group_sid) {
476 DEBUG(5,("GROUP specified more than once!\n"));
477 goto done;
479 group_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
480 if (!group_sid ||
481 !convert_string_to_sid(ipc_cli, pol,
482 numeric,
483 group_sid, tok+6)) {
484 DEBUG(5, ("Failed to parse group sid\n"));
485 goto done;
487 continue;
490 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
491 if (group_sid) {
492 DEBUG(5,("GROUP specified more than once!\n"));
493 goto done;
495 group_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
496 if (!group_sid ||
497 !convert_string_to_sid(ipc_cli, pol,
498 False,
499 group_sid, tok+6)) {
500 DEBUG(5, ("Failed to parse group sid\n"));
501 goto done;
503 continue;
506 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
507 struct security_ace ace;
508 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
509 DEBUG(5, ("Failed to parse ACL %s\n", tok));
510 goto done;
512 if(!add_ace(&dacl, &ace, ctx)) {
513 DEBUG(5, ("Failed to add ACL %s\n", tok));
514 goto done;
516 continue;
519 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
520 struct security_ace ace;
521 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
522 DEBUG(5, ("Failed to parse ACL %s\n", tok));
523 goto done;
525 if(!add_ace(&dacl, &ace, ctx)) {
526 DEBUG(5, ("Failed to add ACL %s\n", tok));
527 goto done;
529 continue;
532 DEBUG(5, ("Failed to parse security descriptor\n"));
533 goto done;
536 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
537 owner_sid, group_sid, NULL, dacl, &sd_size);
539 done:
540 SAFE_FREE(group_sid);
541 SAFE_FREE(owner_sid);
542 return ret;
546 /* Obtain the current dos attributes */
547 static DOS_ATTR_DESC *
548 dos_attr_query(SMBCCTX *context,
549 TALLOC_CTX *ctx,
550 const char *filename,
551 SMBCSRV *srv)
553 struct timespec create_time_ts;
554 struct timespec write_time_ts;
555 struct timespec access_time_ts;
556 struct timespec change_time_ts;
557 SMB_OFF_T size = 0;
558 uint16 mode = 0;
559 SMB_INO_T inode = 0;
560 DOS_ATTR_DESC *ret;
562 ret = TALLOC_P(ctx, DOS_ATTR_DESC);
563 if (!ret) {
564 errno = ENOMEM;
565 return NULL;
568 /* Obtain the DOS attributes */
569 if (!SMBC_getatr(context, srv, CONST_DISCARD(char *, filename),
570 &mode, &size,
571 &create_time_ts,
572 &access_time_ts,
573 &write_time_ts,
574 &change_time_ts,
575 &inode)) {
576 errno = SMBC_errno(context, srv->cli);
577 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
578 return NULL;
581 ret->mode = mode;
582 ret->size = size;
583 ret->create_time = convert_timespec_to_time_t(create_time_ts);
584 ret->access_time = convert_timespec_to_time_t(access_time_ts);
585 ret->write_time = convert_timespec_to_time_t(write_time_ts);
586 ret->change_time = convert_timespec_to_time_t(change_time_ts);
587 ret->inode = inode;
589 return ret;
593 /* parse a ascii version of a security descriptor */
594 static void
595 dos_attr_parse(SMBCCTX *context,
596 DOS_ATTR_DESC *dad,
597 SMBCSRV *srv,
598 char *str)
600 int n;
601 const char *p = str;
602 char *tok = NULL;
603 TALLOC_CTX *frame = NULL;
604 struct {
605 const char * create_time_attr;
606 const char * access_time_attr;
607 const char * write_time_attr;
608 const char * change_time_attr;
609 } attr_strings;
611 /* Determine whether to use old-style or new-style attribute names */
612 if (context->internal->full_time_names) {
613 /* new-style names */
614 attr_strings.create_time_attr = "CREATE_TIME";
615 attr_strings.access_time_attr = "ACCESS_TIME";
616 attr_strings.write_time_attr = "WRITE_TIME";
617 attr_strings.change_time_attr = "CHANGE_TIME";
618 } else {
619 /* old-style names */
620 attr_strings.create_time_attr = NULL;
621 attr_strings.access_time_attr = "A_TIME";
622 attr_strings.write_time_attr = "M_TIME";
623 attr_strings.change_time_attr = "C_TIME";
626 /* if this is to set the entire ACL... */
627 if (*str == '*') {
628 /* ... then increment past the first colon if there is one */
629 if ((p = strchr(str, ':')) != NULL) {
630 ++p;
631 } else {
632 p = str;
636 frame = talloc_stackframe();
637 while (next_token_talloc(frame, &p, &tok, "\t,\r\n")) {
638 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
639 long request = strtol(tok+5, NULL, 16);
640 if (request == 0) {
641 dad->mode = (request |
642 (IS_DOS_DIR(dad->mode)
643 ? FILE_ATTRIBUTE_DIRECTORY
644 : FILE_ATTRIBUTE_NORMAL));
645 } else {
646 dad->mode = request;
648 continue;
651 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
652 dad->size = (SMB_OFF_T)atof(tok+5);
653 continue;
656 n = strlen(attr_strings.access_time_attr);
657 if (StrnCaseCmp(tok, attr_strings.access_time_attr, n) == 0) {
658 dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
659 continue;
662 n = strlen(attr_strings.change_time_attr);
663 if (StrnCaseCmp(tok, attr_strings.change_time_attr, n) == 0) {
664 dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
665 continue;
668 n = strlen(attr_strings.write_time_attr);
669 if (StrnCaseCmp(tok, attr_strings.write_time_attr, n) == 0) {
670 dad->write_time = (time_t)strtol(tok+n+1, NULL, 10);
671 continue;
674 if (attr_strings.create_time_attr != NULL) {
675 n = strlen(attr_strings.create_time_attr);
676 if (StrnCaseCmp(tok, attr_strings.create_time_attr,
677 n) == 0) {
678 dad->create_time = (time_t)strtol(tok+n+1,
679 NULL, 10);
680 continue;
684 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
685 dad->inode = (SMB_INO_T)atof(tok+6);
686 continue;
689 TALLOC_FREE(frame);
692 /*****************************************************
693 Retrieve the acls for a file.
694 *******************************************************/
696 static int
697 cacl_get(SMBCCTX *context,
698 TALLOC_CTX *ctx,
699 SMBCSRV *srv,
700 struct cli_state *ipc_cli,
701 struct policy_handle *pol,
702 char *filename,
703 char *attr_name,
704 char *buf,
705 int bufsize)
707 uint32 i;
708 int n = 0;
709 int n_used;
710 bool all;
711 bool all_nt;
712 bool all_nt_acls;
713 bool all_dos;
714 bool some_nt;
715 bool some_dos;
716 bool exclude_nt_revision = False;
717 bool exclude_nt_owner = False;
718 bool exclude_nt_group = False;
719 bool exclude_nt_acl = False;
720 bool exclude_dos_mode = False;
721 bool exclude_dos_size = False;
722 bool exclude_dos_create_time = False;
723 bool exclude_dos_access_time = False;
724 bool exclude_dos_write_time = False;
725 bool exclude_dos_change_time = False;
726 bool exclude_dos_inode = False;
727 bool numeric = True;
728 bool determine_size = (bufsize == 0);
729 uint16_t fnum;
730 struct security_descriptor *sd;
731 fstring sidstr;
732 fstring name_sandbox;
733 char *name;
734 char *pExclude;
735 char *p;
736 struct timespec create_time_ts;
737 struct timespec write_time_ts;
738 struct timespec access_time_ts;
739 struct timespec change_time_ts;
740 time_t create_time = (time_t)0;
741 time_t write_time = (time_t)0;
742 time_t access_time = (time_t)0;
743 time_t change_time = (time_t)0;
744 SMB_OFF_T size = 0;
745 uint16 mode = 0;
746 SMB_INO_T ino = 0;
747 struct cli_state *cli = srv->cli;
748 struct {
749 const char * create_time_attr;
750 const char * access_time_attr;
751 const char * write_time_attr;
752 const char * change_time_attr;
753 } attr_strings;
754 struct {
755 const char * create_time_attr;
756 const char * access_time_attr;
757 const char * write_time_attr;
758 const char * change_time_attr;
759 } excl_attr_strings;
761 /* Determine whether to use old-style or new-style attribute names */
762 if (context->internal->full_time_names) {
763 /* new-style names */
764 attr_strings.create_time_attr = "CREATE_TIME";
765 attr_strings.access_time_attr = "ACCESS_TIME";
766 attr_strings.write_time_attr = "WRITE_TIME";
767 attr_strings.change_time_attr = "CHANGE_TIME";
769 excl_attr_strings.create_time_attr = "CREATE_TIME";
770 excl_attr_strings.access_time_attr = "ACCESS_TIME";
771 excl_attr_strings.write_time_attr = "WRITE_TIME";
772 excl_attr_strings.change_time_attr = "CHANGE_TIME";
773 } else {
774 /* old-style names */
775 attr_strings.create_time_attr = NULL;
776 attr_strings.access_time_attr = "A_TIME";
777 attr_strings.write_time_attr = "M_TIME";
778 attr_strings.change_time_attr = "C_TIME";
780 excl_attr_strings.create_time_attr = NULL;
781 excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
782 excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
783 excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
786 /* Copy name so we can strip off exclusions (if any are specified) */
787 strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
789 /* Ensure name is null terminated */
790 name_sandbox[sizeof(name_sandbox) - 1] = '\0';
792 /* Play in the sandbox */
793 name = name_sandbox;
795 /* If there are any exclusions, point to them and mask them from name */
796 if ((pExclude = strchr(name, '!')) != NULL)
798 *pExclude++ = '\0';
801 all = (StrnCaseCmp(name, "system.*", 8) == 0);
802 all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
803 all_nt_acls = (StrnCaseCmp(name, "system.nt_sec_desc.acl.*", 24) == 0);
804 all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
805 some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
806 some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
807 numeric = (* (name + strlen(name) - 1) != '+');
809 /* Look for exclusions from "all" requests */
810 if (all || all_nt || all_dos) {
811 /* Exclusions are delimited by '!' */
812 for (;
813 pExclude != NULL;
814 pExclude = (p == NULL ? NULL : p + 1)) {
816 /* Find end of this exclusion name */
817 if ((p = strchr(pExclude, '!')) != NULL)
819 *p = '\0';
822 /* Which exclusion name is this? */
823 if (StrCaseCmp(pExclude,
824 "nt_sec_desc.revision") == 0) {
825 exclude_nt_revision = True;
827 else if (StrCaseCmp(pExclude,
828 "nt_sec_desc.owner") == 0) {
829 exclude_nt_owner = True;
831 else if (StrCaseCmp(pExclude,
832 "nt_sec_desc.group") == 0) {
833 exclude_nt_group = True;
835 else if (StrCaseCmp(pExclude,
836 "nt_sec_desc.acl") == 0) {
837 exclude_nt_acl = True;
839 else if (StrCaseCmp(pExclude,
840 "dos_attr.mode") == 0) {
841 exclude_dos_mode = True;
843 else if (StrCaseCmp(pExclude,
844 "dos_attr.size") == 0) {
845 exclude_dos_size = True;
847 else if (excl_attr_strings.create_time_attr != NULL &&
848 StrCaseCmp(pExclude,
849 excl_attr_strings.change_time_attr) == 0) {
850 exclude_dos_create_time = True;
852 else if (StrCaseCmp(pExclude,
853 excl_attr_strings.access_time_attr) == 0) {
854 exclude_dos_access_time = True;
856 else if (StrCaseCmp(pExclude,
857 excl_attr_strings.write_time_attr) == 0) {
858 exclude_dos_write_time = True;
860 else if (StrCaseCmp(pExclude,
861 excl_attr_strings.change_time_attr) == 0) {
862 exclude_dos_change_time = True;
864 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
865 exclude_dos_inode = True;
867 else {
868 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
869 pExclude));
870 errno = ENOATTR;
871 return -1;
876 n_used = 0;
879 * If we are (possibly) talking to an NT or new system and some NT
880 * attributes have been requested...
882 if (ipc_cli && (all || some_nt || all_nt_acls)) {
883 char *targetpath = NULL;
884 struct cli_state *targetcli = NULL;
886 /* Point to the portion after "system.nt_sec_desc." */
887 name += 19; /* if (all) this will be invalid but unused */
889 if (!cli_resolve_path(ctx, "", context->internal->auth_info,
890 cli, filename,
891 &targetcli, &targetpath)) {
892 DEBUG(5, ("cacl_get Could not resolve %s\n",
893 filename));
894 errno = ENOENT;
895 return -1;
898 /* ... then obtain any NT attributes which were requested */
899 if (!NT_STATUS_IS_OK(cli_ntcreate(targetcli, targetpath, 0, CREATE_ACCESS_READ, 0,
900 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
901 DEBUG(5, ("cacl_get failed to open %s: %s\n",
902 targetpath, cli_errstr(targetcli)));
903 errno = 0;
904 return -1;
907 sd = cli_query_secdesc(targetcli, fnum, ctx);
909 if (!sd) {
910 DEBUG(5,
911 ("cacl_get Failed to query old descriptor\n"));
912 errno = 0;
913 return -1;
916 cli_close(targetcli, fnum);
918 if (! exclude_nt_revision) {
919 if (all || all_nt) {
920 if (determine_size) {
921 p = talloc_asprintf(ctx,
922 "REVISION:%d",
923 sd->revision);
924 if (!p) {
925 errno = ENOMEM;
926 return -1;
928 n = strlen(p);
929 } else {
930 n = snprintf(buf, bufsize,
931 "REVISION:%d",
932 sd->revision);
934 } else if (StrCaseCmp(name, "revision") == 0) {
935 if (determine_size) {
936 p = talloc_asprintf(ctx, "%d",
937 sd->revision);
938 if (!p) {
939 errno = ENOMEM;
940 return -1;
942 n = strlen(p);
943 } else {
944 n = snprintf(buf, bufsize, "%d",
945 sd->revision);
949 if (!determine_size && n > bufsize) {
950 errno = ERANGE;
951 return -1;
953 buf += n;
954 n_used += n;
955 bufsize -= n;
956 n = 0;
959 if (! exclude_nt_owner) {
960 /* Get owner and group sid */
961 if (sd->owner_sid) {
962 convert_sid_to_string(ipc_cli, pol,
963 sidstr,
964 numeric,
965 sd->owner_sid);
966 } else {
967 fstrcpy(sidstr, "");
970 if (all || all_nt) {
971 if (determine_size) {
972 p = talloc_asprintf(ctx, ",OWNER:%s",
973 sidstr);
974 if (!p) {
975 errno = ENOMEM;
976 return -1;
978 n = strlen(p);
979 } else if (sidstr[0] != '\0') {
980 n = snprintf(buf, bufsize,
981 ",OWNER:%s", sidstr);
983 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
984 if (determine_size) {
985 p = talloc_asprintf(ctx, "%s", sidstr);
986 if (!p) {
987 errno = ENOMEM;
988 return -1;
990 n = strlen(p);
991 } else {
992 n = snprintf(buf, bufsize, "%s",
993 sidstr);
997 if (!determine_size && n > bufsize) {
998 errno = ERANGE;
999 return -1;
1001 buf += n;
1002 n_used += n;
1003 bufsize -= n;
1004 n = 0;
1007 if (! exclude_nt_group) {
1008 if (sd->group_sid) {
1009 convert_sid_to_string(ipc_cli, pol,
1010 sidstr, numeric,
1011 sd->group_sid);
1012 } else {
1013 fstrcpy(sidstr, "");
1016 if (all || all_nt) {
1017 if (determine_size) {
1018 p = talloc_asprintf(ctx, ",GROUP:%s",
1019 sidstr);
1020 if (!p) {
1021 errno = ENOMEM;
1022 return -1;
1024 n = strlen(p);
1025 } else if (sidstr[0] != '\0') {
1026 n = snprintf(buf, bufsize,
1027 ",GROUP:%s", sidstr);
1029 } else if (StrnCaseCmp(name, "group", 5) == 0) {
1030 if (determine_size) {
1031 p = talloc_asprintf(ctx, "%s", sidstr);
1032 if (!p) {
1033 errno = ENOMEM;
1034 return -1;
1036 n = strlen(p);
1037 } else {
1038 n = snprintf(buf, bufsize,
1039 "%s", sidstr);
1043 if (!determine_size && n > bufsize) {
1044 errno = ERANGE;
1045 return -1;
1047 buf += n;
1048 n_used += n;
1049 bufsize -= n;
1050 n = 0;
1053 if (! exclude_nt_acl) {
1054 /* Add aces to value buffer */
1055 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
1057 struct security_ace *ace = &sd->dacl->aces[i];
1058 convert_sid_to_string(ipc_cli, pol,
1059 sidstr, numeric,
1060 &ace->trustee);
1062 if (all || all_nt) {
1063 if (determine_size) {
1064 p = talloc_asprintf(
1065 ctx,
1066 ",ACL:"
1067 "%s:%d/%d/0x%08x",
1068 sidstr,
1069 ace->type,
1070 ace->flags,
1071 ace->access_mask);
1072 if (!p) {
1073 errno = ENOMEM;
1074 return -1;
1076 n = strlen(p);
1077 } else {
1078 n = snprintf(
1079 buf, bufsize,
1080 ",ACL:%s:%d/%d/0x%08x",
1081 sidstr,
1082 ace->type,
1083 ace->flags,
1084 ace->access_mask);
1086 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
1087 StrCaseCmp(name+3, sidstr) == 0) ||
1088 (StrnCaseCmp(name, "acl+", 4) == 0 &&
1089 StrCaseCmp(name+4, sidstr) == 0)) {
1090 if (determine_size) {
1091 p = talloc_asprintf(
1092 ctx,
1093 "%d/%d/0x%08x",
1094 ace->type,
1095 ace->flags,
1096 ace->access_mask);
1097 if (!p) {
1098 errno = ENOMEM;
1099 return -1;
1101 n = strlen(p);
1102 } else {
1103 n = snprintf(buf, bufsize,
1104 "%d/%d/0x%08x",
1105 ace->type,
1106 ace->flags,
1107 ace->access_mask);
1109 } else if (all_nt_acls) {
1110 if (determine_size) {
1111 p = talloc_asprintf(
1112 ctx,
1113 "%s%s:%d/%d/0x%08x",
1114 i ? "," : "",
1115 sidstr,
1116 ace->type,
1117 ace->flags,
1118 ace->access_mask);
1119 if (!p) {
1120 errno = ENOMEM;
1121 return -1;
1123 n = strlen(p);
1124 } else {
1125 n = snprintf(buf, bufsize,
1126 "%s%s:%d/%d/0x%08x",
1127 i ? "," : "",
1128 sidstr,
1129 ace->type,
1130 ace->flags,
1131 ace->access_mask);
1134 if (!determine_size && n > bufsize) {
1135 errno = ERANGE;
1136 return -1;
1138 buf += n;
1139 n_used += n;
1140 bufsize -= n;
1141 n = 0;
1145 /* Restore name pointer to its original value */
1146 name -= 19;
1149 if (all || some_dos) {
1150 /* Point to the portion after "system.dos_attr." */
1151 name += 16; /* if (all) this will be invalid but unused */
1153 /* Obtain the DOS attributes */
1154 if (!SMBC_getatr(context, srv, filename, &mode, &size,
1155 &create_time_ts,
1156 &access_time_ts,
1157 &write_time_ts,
1158 &change_time_ts,
1159 &ino)) {
1161 errno = SMBC_errno(context, srv->cli);
1162 return -1;
1165 create_time = convert_timespec_to_time_t(create_time_ts);
1166 access_time = convert_timespec_to_time_t(access_time_ts);
1167 write_time = convert_timespec_to_time_t(write_time_ts);
1168 change_time = convert_timespec_to_time_t(change_time_ts);
1170 if (! exclude_dos_mode) {
1171 if (all || all_dos) {
1172 if (determine_size) {
1173 p = talloc_asprintf(ctx,
1174 "%sMODE:0x%x",
1175 (ipc_cli &&
1176 (all || some_nt)
1177 ? ","
1178 : ""),
1179 mode);
1180 if (!p) {
1181 errno = ENOMEM;
1182 return -1;
1184 n = strlen(p);
1185 } else {
1186 n = snprintf(buf, bufsize,
1187 "%sMODE:0x%x",
1188 (ipc_cli &&
1189 (all || some_nt)
1190 ? ","
1191 : ""),
1192 mode);
1194 } else if (StrCaseCmp(name, "mode") == 0) {
1195 if (determine_size) {
1196 p = talloc_asprintf(ctx, "0x%x", mode);
1197 if (!p) {
1198 errno = ENOMEM;
1199 return -1;
1201 n = strlen(p);
1202 } else {
1203 n = snprintf(buf, bufsize,
1204 "0x%x", mode);
1208 if (!determine_size && n > bufsize) {
1209 errno = ERANGE;
1210 return -1;
1212 buf += n;
1213 n_used += n;
1214 bufsize -= n;
1215 n = 0;
1218 if (! exclude_dos_size) {
1219 if (all || all_dos) {
1220 if (determine_size) {
1221 p = talloc_asprintf(
1222 ctx,
1223 ",SIZE:%.0f",
1224 (double)size);
1225 if (!p) {
1226 errno = ENOMEM;
1227 return -1;
1229 n = strlen(p);
1230 } else {
1231 n = snprintf(buf, bufsize,
1232 ",SIZE:%.0f",
1233 (double)size);
1235 } else if (StrCaseCmp(name, "size") == 0) {
1236 if (determine_size) {
1237 p = talloc_asprintf(
1238 ctx,
1239 "%.0f",
1240 (double)size);
1241 if (!p) {
1242 errno = ENOMEM;
1243 return -1;
1245 n = strlen(p);
1246 } else {
1247 n = snprintf(buf, bufsize,
1248 "%.0f",
1249 (double)size);
1253 if (!determine_size && n > bufsize) {
1254 errno = ERANGE;
1255 return -1;
1257 buf += n;
1258 n_used += n;
1259 bufsize -= n;
1260 n = 0;
1263 if (! exclude_dos_create_time &&
1264 attr_strings.create_time_attr != NULL) {
1265 if (all || all_dos) {
1266 if (determine_size) {
1267 p = talloc_asprintf(ctx,
1268 ",%s:%lu",
1269 attr_strings.create_time_attr,
1270 (unsigned long) create_time);
1271 if (!p) {
1272 errno = ENOMEM;
1273 return -1;
1275 n = strlen(p);
1276 } else {
1277 n = snprintf(buf, bufsize,
1278 ",%s:%lu",
1279 attr_strings.create_time_attr,
1280 (unsigned long) create_time);
1282 } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
1283 if (determine_size) {
1284 p = talloc_asprintf(ctx, "%lu", (unsigned long) create_time);
1285 if (!p) {
1286 errno = ENOMEM;
1287 return -1;
1289 n = strlen(p);
1290 } else {
1291 n = snprintf(buf, bufsize,
1292 "%lu", (unsigned long) create_time);
1296 if (!determine_size && n > bufsize) {
1297 errno = ERANGE;
1298 return -1;
1300 buf += n;
1301 n_used += n;
1302 bufsize -= n;
1303 n = 0;
1306 if (! exclude_dos_access_time) {
1307 if (all || all_dos) {
1308 if (determine_size) {
1309 p = talloc_asprintf(ctx,
1310 ",%s:%lu",
1311 attr_strings.access_time_attr,
1312 (unsigned long) access_time);
1313 if (!p) {
1314 errno = ENOMEM;
1315 return -1;
1317 n = strlen(p);
1318 } else {
1319 n = snprintf(buf, bufsize,
1320 ",%s:%lu",
1321 attr_strings.access_time_attr,
1322 (unsigned long) access_time);
1324 } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
1325 if (determine_size) {
1326 p = talloc_asprintf(ctx, "%lu", (unsigned long) access_time);
1327 if (!p) {
1328 errno = ENOMEM;
1329 return -1;
1331 n = strlen(p);
1332 } else {
1333 n = snprintf(buf, bufsize,
1334 "%lu", (unsigned long) access_time);
1338 if (!determine_size && n > bufsize) {
1339 errno = ERANGE;
1340 return -1;
1342 buf += n;
1343 n_used += n;
1344 bufsize -= n;
1345 n = 0;
1348 if (! exclude_dos_write_time) {
1349 if (all || all_dos) {
1350 if (determine_size) {
1351 p = talloc_asprintf(ctx,
1352 ",%s:%lu",
1353 attr_strings.write_time_attr,
1354 (unsigned long) write_time);
1355 if (!p) {
1356 errno = ENOMEM;
1357 return -1;
1359 n = strlen(p);
1360 } else {
1361 n = snprintf(buf, bufsize,
1362 ",%s:%lu",
1363 attr_strings.write_time_attr,
1364 (unsigned long) write_time);
1366 } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
1367 if (determine_size) {
1368 p = talloc_asprintf(ctx, "%lu", (unsigned long) write_time);
1369 if (!p) {
1370 errno = ENOMEM;
1371 return -1;
1373 n = strlen(p);
1374 } else {
1375 n = snprintf(buf, bufsize,
1376 "%lu", (unsigned long) write_time);
1380 if (!determine_size && n > bufsize) {
1381 errno = ERANGE;
1382 return -1;
1384 buf += n;
1385 n_used += n;
1386 bufsize -= n;
1387 n = 0;
1390 if (! exclude_dos_change_time) {
1391 if (all || all_dos) {
1392 if (determine_size) {
1393 p = talloc_asprintf(ctx,
1394 ",%s:%lu",
1395 attr_strings.change_time_attr,
1396 (unsigned long) change_time);
1397 if (!p) {
1398 errno = ENOMEM;
1399 return -1;
1401 n = strlen(p);
1402 } else {
1403 n = snprintf(buf, bufsize,
1404 ",%s:%lu",
1405 attr_strings.change_time_attr,
1406 (unsigned long) change_time);
1408 } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
1409 if (determine_size) {
1410 p = talloc_asprintf(ctx, "%lu", (unsigned long) change_time);
1411 if (!p) {
1412 errno = ENOMEM;
1413 return -1;
1415 n = strlen(p);
1416 } else {
1417 n = snprintf(buf, bufsize,
1418 "%lu", (unsigned long) change_time);
1422 if (!determine_size && n > bufsize) {
1423 errno = ERANGE;
1424 return -1;
1426 buf += n;
1427 n_used += n;
1428 bufsize -= n;
1429 n = 0;
1432 if (! exclude_dos_inode) {
1433 if (all || all_dos) {
1434 if (determine_size) {
1435 p = talloc_asprintf(
1436 ctx,
1437 ",INODE:%.0f",
1438 (double)ino);
1439 if (!p) {
1440 errno = ENOMEM;
1441 return -1;
1443 n = strlen(p);
1444 } else {
1445 n = snprintf(buf, bufsize,
1446 ",INODE:%.0f",
1447 (double) ino);
1449 } else if (StrCaseCmp(name, "inode") == 0) {
1450 if (determine_size) {
1451 p = talloc_asprintf(
1452 ctx,
1453 "%.0f",
1454 (double) ino);
1455 if (!p) {
1456 errno = ENOMEM;
1457 return -1;
1459 n = strlen(p);
1460 } else {
1461 n = snprintf(buf, bufsize,
1462 "%.0f",
1463 (double) ino);
1467 if (!determine_size && n > bufsize) {
1468 errno = ERANGE;
1469 return -1;
1471 buf += n;
1472 n_used += n;
1473 bufsize -= n;
1474 n = 0;
1477 /* Restore name pointer to its original value */
1478 name -= 16;
1481 if (n_used == 0) {
1482 errno = ENOATTR;
1483 return -1;
1486 return n_used;
1489 /*****************************************************
1490 set the ACLs on a file given an ascii description
1491 *******************************************************/
1492 static int
1493 cacl_set(SMBCCTX *context,
1494 TALLOC_CTX *ctx,
1495 struct cli_state *cli,
1496 struct cli_state *ipc_cli,
1497 struct policy_handle *pol,
1498 const char *filename,
1499 char *the_acl,
1500 int mode,
1501 int flags)
1503 uint16_t fnum = (uint16_t)-1;
1504 int err = 0;
1505 struct security_descriptor *sd = NULL, *old;
1506 struct security_acl *dacl = NULL;
1507 struct dom_sid *owner_sid = NULL;
1508 struct dom_sid *group_sid = NULL;
1509 uint32 i, j;
1510 size_t sd_size;
1511 int ret = 0;
1512 char *p;
1513 bool numeric = True;
1514 char *targetpath = NULL;
1515 struct cli_state *targetcli = NULL;
1516 NTSTATUS status;
1518 /* the_acl will be null for REMOVE_ALL operations */
1519 if (the_acl) {
1520 numeric = ((p = strchr(the_acl, ':')) != NULL &&
1521 p > the_acl &&
1522 p[-1] != '+');
1524 /* if this is to set the entire ACL... */
1525 if (*the_acl == '*') {
1526 /* ... then increment past the first colon */
1527 the_acl = p + 1;
1530 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric, the_acl);
1531 if (!sd) {
1532 errno = EINVAL;
1533 return -1;
1537 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
1538 that doesn't deref sd */
1540 if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
1541 errno = EINVAL;
1542 return -1;
1545 if (!cli_resolve_path(ctx, "", context->internal->auth_info,
1546 cli, filename,
1547 &targetcli, &targetpath)) {
1548 DEBUG(5,("cacl_set: Could not resolve %s\n", filename));
1549 errno = ENOENT;
1550 return -1;
1553 /* The desired access below is the only one I could find that works
1554 with NT4, W2KP and Samba */
1556 if (!NT_STATUS_IS_OK(cli_ntcreate(targetcli, targetpath, 0, CREATE_ACCESS_READ, 0,
1557 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
1558 DEBUG(5, ("cacl_set failed to open %s: %s\n",
1559 targetpath, cli_errstr(targetcli)));
1560 errno = 0;
1561 return -1;
1564 old = cli_query_secdesc(targetcli, fnum, ctx);
1566 if (!old) {
1567 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
1568 errno = 0;
1569 return -1;
1572 cli_close(targetcli, fnum);
1574 switch (mode) {
1575 case SMBC_XATTR_MODE_REMOVE_ALL:
1576 old->dacl->num_aces = 0;
1577 dacl = old->dacl;
1578 break;
1580 case SMBC_XATTR_MODE_REMOVE:
1581 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1582 bool found = False;
1584 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
1585 if (sec_ace_equal(&sd->dacl->aces[i],
1586 &old->dacl->aces[j])) {
1587 uint32 k;
1588 for (k=j; k<old->dacl->num_aces-1;k++) {
1589 old->dacl->aces[k] =
1590 old->dacl->aces[k+1];
1592 old->dacl->num_aces--;
1593 found = True;
1594 dacl = old->dacl;
1595 break;
1599 if (!found) {
1600 err = ENOATTR;
1601 ret = -1;
1602 goto failed;
1605 break;
1607 case SMBC_XATTR_MODE_ADD:
1608 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1609 bool found = False;
1611 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
1612 if (dom_sid_equal(&sd->dacl->aces[i].trustee,
1613 &old->dacl->aces[j].trustee)) {
1614 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
1615 err = EEXIST;
1616 ret = -1;
1617 goto failed;
1619 old->dacl->aces[j] = sd->dacl->aces[i];
1620 ret = -1;
1621 found = True;
1625 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
1626 err = ENOATTR;
1627 ret = -1;
1628 goto failed;
1631 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1632 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
1635 dacl = old->dacl;
1636 break;
1638 case SMBC_XATTR_MODE_SET:
1639 old = sd;
1640 owner_sid = old->owner_sid;
1641 group_sid = old->group_sid;
1642 dacl = old->dacl;
1643 break;
1645 case SMBC_XATTR_MODE_CHOWN:
1646 owner_sid = sd->owner_sid;
1647 break;
1649 case SMBC_XATTR_MODE_CHGRP:
1650 group_sid = sd->group_sid;
1651 break;
1654 /* Denied ACE entries must come before allowed ones */
1655 sort_acl(old->dacl);
1657 /* Create new security descriptor and set it */
1658 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
1659 owner_sid, group_sid, NULL, dacl, &sd_size);
1661 if (!NT_STATUS_IS_OK(cli_ntcreate(targetcli, targetpath, 0,
1662 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS, 0,
1663 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
1664 DEBUG(5, ("cacl_set failed to open %s: %s\n",
1665 targetpath, cli_errstr(targetcli)));
1666 errno = 0;
1667 return -1;
1670 status = cli_set_secdesc(targetcli, fnum, sd);
1671 if (!NT_STATUS_IS_OK(status)) {
1672 DEBUG(5, ("ERROR: secdesc set failed: %s\n",
1673 nt_errstr(status)));
1674 ret = -1;
1677 /* Clean up */
1679 failed:
1680 cli_close(targetcli, fnum);
1682 if (err != 0) {
1683 errno = err;
1686 return ret;
1691 SMBC_setxattr_ctx(SMBCCTX *context,
1692 const char *fname,
1693 const char *name,
1694 const void *value,
1695 size_t size,
1696 int flags)
1698 int ret;
1699 int ret2;
1700 SMBCSRV *srv = NULL;
1701 SMBCSRV *ipc_srv = NULL;
1702 char *server = NULL;
1703 char *share = NULL;
1704 char *user = NULL;
1705 char *password = NULL;
1706 char *workgroup = NULL;
1707 char *path = NULL;
1708 DOS_ATTR_DESC *dad = NULL;
1709 struct {
1710 const char * create_time_attr;
1711 const char * access_time_attr;
1712 const char * write_time_attr;
1713 const char * change_time_attr;
1714 } attr_strings;
1715 TALLOC_CTX *frame = talloc_stackframe();
1717 if (!context || !context->internal->initialized) {
1718 errno = EINVAL; /* Best I can think of ... */
1719 TALLOC_FREE(frame);
1720 return -1;
1723 if (!fname) {
1724 errno = EINVAL;
1725 TALLOC_FREE(frame);
1726 return -1;
1729 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
1730 fname, name, (int) size, (const char*)value));
1732 if (SMBC_parse_path(frame,
1733 context,
1734 fname,
1735 &workgroup,
1736 &server,
1737 &share,
1738 &path,
1739 &user,
1740 &password,
1741 NULL)) {
1742 errno = EINVAL;
1743 TALLOC_FREE(frame);
1744 return -1;
1747 if (!user || user[0] == (char)0) {
1748 user = talloc_strdup(frame, smbc_getUser(context));
1749 if (!user) {
1750 errno = ENOMEM;
1751 TALLOC_FREE(frame);
1752 return -1;
1756 srv = SMBC_server(frame, context, True,
1757 server, share, &workgroup, &user, &password);
1758 if (!srv) {
1759 TALLOC_FREE(frame);
1760 return -1; /* errno set by SMBC_server */
1763 if (! srv->no_nt_session) {
1764 ipc_srv = SMBC_attr_server(frame, context, server, share,
1765 &workgroup, &user, &password);
1766 if (! ipc_srv) {
1767 srv->no_nt_session = True;
1769 } else {
1770 ipc_srv = NULL;
1774 * Are they asking to set the entire set of known attributes?
1776 if (StrCaseCmp(name, "system.*") == 0 ||
1777 StrCaseCmp(name, "system.*+") == 0) {
1778 /* Yup. */
1779 char *namevalue =
1780 talloc_asprintf(talloc_tos(), "%s:%s",
1781 name+7, (const char *) value);
1782 if (! namevalue) {
1783 errno = ENOMEM;
1784 ret = -1;
1785 TALLOC_FREE(frame);
1786 return -1;
1789 if (ipc_srv) {
1790 ret = cacl_set(context, talloc_tos(), srv->cli,
1791 ipc_srv->cli, &ipc_srv->pol, path,
1792 namevalue,
1793 (*namevalue == '*'
1794 ? SMBC_XATTR_MODE_SET
1795 : SMBC_XATTR_MODE_ADD),
1796 flags);
1797 } else {
1798 ret = 0;
1801 /* get a DOS Attribute Descriptor with current attributes */
1802 dad = dos_attr_query(context, talloc_tos(), path, srv);
1803 if (dad) {
1804 /* Overwrite old with new, using what was provided */
1805 dos_attr_parse(context, dad, srv, namevalue);
1807 /* Set the new DOS attributes */
1808 if (! SMBC_setatr(context, srv, path,
1809 dad->create_time,
1810 dad->access_time,
1811 dad->write_time,
1812 dad->change_time,
1813 dad->mode)) {
1815 /* cause failure if NT failed too */
1816 dad = NULL;
1820 /* we only fail if both NT and DOS sets failed */
1821 if (ret < 0 && ! dad) {
1822 ret = -1; /* in case dad was null */
1824 else {
1825 ret = 0;
1828 TALLOC_FREE(frame);
1829 return ret;
1833 * Are they asking to set an access control element or to set
1834 * the entire access control list?
1836 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
1837 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
1838 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
1839 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
1840 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
1842 /* Yup. */
1843 char *namevalue =
1844 talloc_asprintf(talloc_tos(), "%s:%s",
1845 name+19, (const char *) value);
1847 if (! ipc_srv) {
1848 ret = -1; /* errno set by SMBC_server() */
1850 else if (! namevalue) {
1851 errno = ENOMEM;
1852 ret = -1;
1853 } else {
1854 ret = cacl_set(context, talloc_tos(), srv->cli,
1855 ipc_srv->cli, &ipc_srv->pol, path,
1856 namevalue,
1857 (*namevalue == '*'
1858 ? SMBC_XATTR_MODE_SET
1859 : SMBC_XATTR_MODE_ADD),
1860 flags);
1862 TALLOC_FREE(frame);
1863 return ret;
1867 * Are they asking to set the owner?
1869 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
1870 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
1872 /* Yup. */
1873 char *namevalue =
1874 talloc_asprintf(talloc_tos(), "%s:%s",
1875 name+19, (const char *) value);
1877 if (! ipc_srv) {
1878 ret = -1; /* errno set by SMBC_server() */
1880 else if (! namevalue) {
1881 errno = ENOMEM;
1882 ret = -1;
1883 } else {
1884 ret = cacl_set(context, talloc_tos(), srv->cli,
1885 ipc_srv->cli, &ipc_srv->pol, path,
1886 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
1888 TALLOC_FREE(frame);
1889 return ret;
1893 * Are they asking to set the group?
1895 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
1896 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
1898 /* Yup. */
1899 char *namevalue =
1900 talloc_asprintf(talloc_tos(), "%s:%s",
1901 name+19, (const char *) value);
1903 if (! ipc_srv) {
1904 /* errno set by SMBC_server() */
1905 ret = -1;
1907 else if (! namevalue) {
1908 errno = ENOMEM;
1909 ret = -1;
1910 } else {
1911 ret = cacl_set(context, talloc_tos(), srv->cli,
1912 ipc_srv->cli, &ipc_srv->pol, path,
1913 namevalue, SMBC_XATTR_MODE_CHGRP, 0);
1915 TALLOC_FREE(frame);
1916 return ret;
1919 /* Determine whether to use old-style or new-style attribute names */
1920 if (context->internal->full_time_names) {
1921 /* new-style names */
1922 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
1923 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
1924 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
1925 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
1926 } else {
1927 /* old-style names */
1928 attr_strings.create_time_attr = NULL;
1929 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
1930 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
1931 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
1935 * Are they asking to set a DOS attribute?
1937 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
1938 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
1939 (attr_strings.create_time_attr != NULL &&
1940 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
1941 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
1942 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
1943 StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
1945 /* get a DOS Attribute Descriptor with current attributes */
1946 dad = dos_attr_query(context, talloc_tos(), path, srv);
1947 if (dad) {
1948 char *namevalue =
1949 talloc_asprintf(talloc_tos(), "%s:%s",
1950 name+16, (const char *) value);
1951 if (! namevalue) {
1952 errno = ENOMEM;
1953 ret = -1;
1954 } else {
1955 /* Overwrite old with provided new params */
1956 dos_attr_parse(context, dad, srv, namevalue);
1958 /* Set the new DOS attributes */
1959 ret2 = SMBC_setatr(context, srv, path,
1960 dad->create_time,
1961 dad->access_time,
1962 dad->write_time,
1963 dad->change_time,
1964 dad->mode);
1966 /* ret2 has True (success) / False (failure) */
1967 if (ret2) {
1968 ret = 0;
1969 } else {
1970 ret = -1;
1973 } else {
1974 ret = -1;
1977 TALLOC_FREE(frame);
1978 return ret;
1981 /* Unsupported attribute name */
1982 errno = EINVAL;
1983 TALLOC_FREE(frame);
1984 return -1;
1988 SMBC_getxattr_ctx(SMBCCTX *context,
1989 const char *fname,
1990 const char *name,
1991 const void *value,
1992 size_t size)
1994 int ret;
1995 SMBCSRV *srv = NULL;
1996 SMBCSRV *ipc_srv = NULL;
1997 char *server = NULL;
1998 char *share = NULL;
1999 char *user = NULL;
2000 char *password = NULL;
2001 char *workgroup = NULL;
2002 char *path = NULL;
2003 struct {
2004 const char * create_time_attr;
2005 const char * access_time_attr;
2006 const char * write_time_attr;
2007 const char * change_time_attr;
2008 } attr_strings;
2009 TALLOC_CTX *frame = talloc_stackframe();
2011 if (!context || !context->internal->initialized) {
2012 errno = EINVAL; /* Best I can think of ... */
2013 TALLOC_FREE(frame);
2014 return -1;
2017 if (!fname) {
2018 errno = EINVAL;
2019 TALLOC_FREE(frame);
2020 return -1;
2023 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
2025 if (SMBC_parse_path(frame,
2026 context,
2027 fname,
2028 &workgroup,
2029 &server,
2030 &share,
2031 &path,
2032 &user,
2033 &password,
2034 NULL)) {
2035 errno = EINVAL;
2036 TALLOC_FREE(frame);
2037 return -1;
2040 if (!user || user[0] == (char)0) {
2041 user = talloc_strdup(frame, smbc_getUser(context));
2042 if (!user) {
2043 errno = ENOMEM;
2044 TALLOC_FREE(frame);
2045 return -1;
2049 srv = SMBC_server(frame, context, True,
2050 server, share, &workgroup, &user, &password);
2051 if (!srv) {
2052 TALLOC_FREE(frame);
2053 return -1; /* errno set by SMBC_server */
2056 if (! srv->no_nt_session) {
2057 ipc_srv = SMBC_attr_server(frame, context, server, share,
2058 &workgroup, &user, &password);
2059 if (! ipc_srv) {
2060 srv->no_nt_session = True;
2062 } else {
2063 ipc_srv = NULL;
2066 /* Determine whether to use old-style or new-style attribute names */
2067 if (context->internal->full_time_names) {
2068 /* new-style names */
2069 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
2070 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
2071 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
2072 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
2073 } else {
2074 /* old-style names */
2075 attr_strings.create_time_attr = NULL;
2076 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
2077 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
2078 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
2081 /* Are they requesting a supported attribute? */
2082 if (StrCaseCmp(name, "system.*") == 0 ||
2083 StrnCaseCmp(name, "system.*!", 9) == 0 ||
2084 StrCaseCmp(name, "system.*+") == 0 ||
2085 StrnCaseCmp(name, "system.*+!", 10) == 0 ||
2086 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
2087 StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
2088 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
2089 StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
2090 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
2091 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
2092 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
2093 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
2094 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
2095 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
2096 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
2097 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
2098 StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
2099 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
2100 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
2101 (attr_strings.create_time_attr != NULL &&
2102 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
2103 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
2104 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
2105 StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
2106 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
2108 /* Yup. */
2109 char *filename = (char *) name;
2110 ret = cacl_get(context, talloc_tos(), srv,
2111 ipc_srv == NULL ? NULL : ipc_srv->cli,
2112 &ipc_srv->pol, path,
2113 filename,
2114 CONST_DISCARD(char *, value),
2115 size);
2116 if (ret < 0 && errno == 0) {
2117 errno = SMBC_errno(context, srv->cli);
2119 TALLOC_FREE(frame);
2120 return ret;
2123 /* Unsupported attribute name */
2124 errno = EINVAL;
2125 TALLOC_FREE(frame);
2126 return -1;
2131 SMBC_removexattr_ctx(SMBCCTX *context,
2132 const char *fname,
2133 const char *name)
2135 int ret;
2136 SMBCSRV *srv = NULL;
2137 SMBCSRV *ipc_srv = NULL;
2138 char *server = NULL;
2139 char *share = NULL;
2140 char *user = NULL;
2141 char *password = NULL;
2142 char *workgroup = NULL;
2143 char *path = NULL;
2144 TALLOC_CTX *frame = talloc_stackframe();
2146 if (!context || !context->internal->initialized) {
2147 errno = EINVAL; /* Best I can think of ... */
2148 TALLOC_FREE(frame);
2149 return -1;
2152 if (!fname) {
2153 errno = EINVAL;
2154 TALLOC_FREE(frame);
2155 return -1;
2158 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
2160 if (SMBC_parse_path(frame,
2161 context,
2162 fname,
2163 &workgroup,
2164 &server,
2165 &share,
2166 &path,
2167 &user,
2168 &password,
2169 NULL)) {
2170 errno = EINVAL;
2171 TALLOC_FREE(frame);
2172 return -1;
2175 if (!user || user[0] == (char)0) {
2176 user = talloc_strdup(frame, smbc_getUser(context));
2177 if (!user) {
2178 errno = ENOMEM;
2179 TALLOC_FREE(frame);
2180 return -1;
2184 srv = SMBC_server(frame, context, True,
2185 server, share, &workgroup, &user, &password);
2186 if (!srv) {
2187 TALLOC_FREE(frame);
2188 return -1; /* errno set by SMBC_server */
2191 if (! srv->no_nt_session) {
2192 ipc_srv = SMBC_attr_server(frame, context, server, share,
2193 &workgroup, &user, &password);
2194 if (! ipc_srv) {
2195 srv->no_nt_session = True;
2197 } else {
2198 ipc_srv = NULL;
2201 if (! ipc_srv) {
2202 TALLOC_FREE(frame);
2203 return -1; /* errno set by SMBC_attr_server */
2206 /* Are they asking to set the entire ACL? */
2207 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
2208 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
2210 /* Yup. */
2211 ret = cacl_set(context, talloc_tos(), srv->cli,
2212 ipc_srv->cli, &ipc_srv->pol, path,
2213 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
2214 TALLOC_FREE(frame);
2215 return ret;
2219 * Are they asking to remove one or more spceific security descriptor
2220 * attributes?
2222 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
2223 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
2224 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
2225 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
2226 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
2227 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
2228 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
2230 /* Yup. */
2231 ret = cacl_set(context, talloc_tos(), srv->cli,
2232 ipc_srv->cli, &ipc_srv->pol, path,
2233 CONST_DISCARD(char *, name) + 19,
2234 SMBC_XATTR_MODE_REMOVE, 0);
2235 TALLOC_FREE(frame);
2236 return ret;
2239 /* Unsupported attribute name */
2240 errno = EINVAL;
2241 TALLOC_FREE(frame);
2242 return -1;
2246 SMBC_listxattr_ctx(SMBCCTX *context,
2247 const char *fname,
2248 char *list,
2249 size_t size)
2252 * This isn't quite what listxattr() is supposed to do. This returns
2253 * the complete set of attribute names, always, rather than only those
2254 * attribute names which actually exist for a file. Hmmm...
2256 size_t retsize;
2257 const char supported_old[] =
2258 "system.*\0"
2259 "system.*+\0"
2260 "system.nt_sec_desc.revision\0"
2261 "system.nt_sec_desc.owner\0"
2262 "system.nt_sec_desc.owner+\0"
2263 "system.nt_sec_desc.group\0"
2264 "system.nt_sec_desc.group+\0"
2265 "system.nt_sec_desc.acl.*\0"
2266 "system.nt_sec_desc.acl\0"
2267 "system.nt_sec_desc.acl+\0"
2268 "system.nt_sec_desc.*\0"
2269 "system.nt_sec_desc.*+\0"
2270 "system.dos_attr.*\0"
2271 "system.dos_attr.mode\0"
2272 "system.dos_attr.c_time\0"
2273 "system.dos_attr.a_time\0"
2274 "system.dos_attr.m_time\0"
2276 const char supported_new[] =
2277 "system.*\0"
2278 "system.*+\0"
2279 "system.nt_sec_desc.revision\0"
2280 "system.nt_sec_desc.owner\0"
2281 "system.nt_sec_desc.owner+\0"
2282 "system.nt_sec_desc.group\0"
2283 "system.nt_sec_desc.group+\0"
2284 "system.nt_sec_desc.acl.*\0"
2285 "system.nt_sec_desc.acl\0"
2286 "system.nt_sec_desc.acl+\0"
2287 "system.nt_sec_desc.*\0"
2288 "system.nt_sec_desc.*+\0"
2289 "system.dos_attr.*\0"
2290 "system.dos_attr.mode\0"
2291 "system.dos_attr.create_time\0"
2292 "system.dos_attr.access_time\0"
2293 "system.dos_attr.write_time\0"
2294 "system.dos_attr.change_time\0"
2296 const char * supported;
2298 if (context->internal->full_time_names) {
2299 supported = supported_new;
2300 retsize = sizeof(supported_new);
2301 } else {
2302 supported = supported_old;
2303 retsize = sizeof(supported_old);
2306 if (size == 0) {
2307 return retsize;
2310 if (retsize > size) {
2311 errno = ERANGE;
2312 return -1;
2315 /* this can't be strcpy() because there are embedded null characters */
2316 memcpy(list, supported, retsize);
2317 return retsize;