Initial revamp of the libsmbclient interface.
[Samba/gebeck_regimport.git] / source3 / libsmb / libsmb_xattr.c
blob93ca0706b2b316f1609c423074c17d1ab804a16a
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"
31 * Find an lsa pipe handle associated with a cli struct.
33 static struct rpc_pipe_client *
34 find_lsa_pipe_hnd(struct cli_state *ipc_cli)
36 struct rpc_pipe_client *pipe_hnd;
38 for (pipe_hnd = ipc_cli->pipe_list;
39 pipe_hnd;
40 pipe_hnd = pipe_hnd->next) {
42 if (pipe_hnd->pipe_idx == PI_LSARPC) {
43 return pipe_hnd;
47 return NULL;
51 * Sort ACEs according to the documentation at
52 * http://support.microsoft.com/kb/269175, at least as far as it defines the
53 * order.
56 static int
57 ace_compare(SEC_ACE *ace1,
58 SEC_ACE *ace2)
60 bool b1;
61 bool b2;
63 /* If the ACEs are equal, we have nothing more to do. */
64 if (sec_ace_equal(ace1, ace2)) {
65 return 0;
68 /* Inherited follow non-inherited */
69 b1 = ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
70 b2 = ((ace2->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
71 if (b1 != b2) {
72 return (b1 ? 1 : -1);
76 * What shall we do with AUDITs and ALARMs? It's undefined. We'll
77 * sort them after DENY and ALLOW.
79 b1 = (ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
80 ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
81 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED &&
82 ace1->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
83 b2 = (ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
84 ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
85 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED &&
86 ace2->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
87 if (b1 != b2) {
88 return (b1 ? 1 : -1);
91 /* Allowed ACEs follow denied ACEs */
92 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
93 ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
94 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
95 ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
96 if (b1 != b2) {
97 return (b1 ? 1 : -1);
101 * ACEs applying to an entity's object follow those applying to the
102 * entity itself
104 b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
105 ace1->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
106 b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
107 ace2->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
108 if (b1 != b2) {
109 return (b1 ? 1 : -1);
113 * If we get this far, the ACEs are similar as far as the
114 * characteristics we typically care about (those defined by the
115 * referenced MS document). We'll now sort by characteristics that
116 * just seems reasonable.
119 if (ace1->type != ace2->type) {
120 return ace2->type - ace1->type;
123 if (sid_compare(&ace1->trustee, &ace2->trustee)) {
124 return sid_compare(&ace1->trustee, &ace2->trustee);
127 if (ace1->flags != ace2->flags) {
128 return ace1->flags - ace2->flags;
131 if (ace1->access_mask != ace2->access_mask) {
132 return ace1->access_mask - ace2->access_mask;
135 if (ace1->size != ace2->size) {
136 return ace1->size - ace2->size;
139 return memcmp(ace1, ace2, sizeof(SEC_ACE));
143 static void
144 sort_acl(SEC_ACL *the_acl)
146 uint32 i;
147 if (!the_acl) return;
149 qsort(the_acl->aces, the_acl->num_aces, sizeof(the_acl->aces[0]),
150 QSORT_CAST 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 POLICY_HND *pol,
169 fstring str,
170 bool numeric,
171 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 TALLOC_FREE(ctx);
202 /* Converted OK */
204 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
205 domains[0], lp_winbind_separator(),
206 names[0]);
209 /* convert a string to a SID, either numeric or username/group */
210 static bool
211 convert_string_to_sid(struct cli_state *ipc_cli,
212 POLICY_HND *pol,
213 bool numeric,
214 DOM_SID *sid,
215 const char *str)
217 enum lsa_SidType *types = NULL;
218 DOM_SID *sids = NULL;
219 bool result = True;
220 TALLOC_CTX *ctx = NULL;
221 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
223 if (!pipe_hnd) {
224 return False;
227 if (numeric) {
228 if (strncmp(str, "S-", 2) == 0) {
229 return string_to_sid(sid, str);
232 result = False;
233 goto done;
236 ctx = talloc_stackframe();
237 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ctx,
238 pol, 1, &str, NULL, 1, &sids,
239 &types))) {
240 result = False;
241 goto done;
244 sid_copy(sid, &sids[0]);
245 done:
247 TALLOC_FREE(ctx);
248 return result;
252 /* parse an ACE in the same format as print_ace() */
253 static bool
254 parse_ace(struct cli_state *ipc_cli,
255 POLICY_HND *pol,
256 SEC_ACE *ace,
257 bool numeric,
258 char *str)
260 char *p;
261 const char *cp;
262 char *tok;
263 unsigned int atype;
264 unsigned int aflags;
265 unsigned int amask;
266 DOM_SID sid;
267 SEC_ACCESS mask;
268 const struct perm_value *v;
269 struct perm_value {
270 const char *perm;
271 uint32 mask;
273 TALLOC_CTX *frame = talloc_stackframe();
275 /* These values discovered by inspection */
276 static const struct perm_value special_values[] = {
277 { "R", 0x00120089 },
278 { "W", 0x00120116 },
279 { "X", 0x001200a0 },
280 { "D", 0x00010000 },
281 { "P", 0x00040000 },
282 { "O", 0x00080000 },
283 { NULL, 0 },
286 static const struct perm_value standard_values[] = {
287 { "READ", 0x001200a9 },
288 { "CHANGE", 0x001301bf },
289 { "FULL", 0x001f01ff },
290 { NULL, 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 ACE to a list of ACEs in a SEC_ACL */
391 static bool
392 add_ace(SEC_ACL **the_acl,
393 SEC_ACE *ace,
394 TALLOC_CTX *ctx)
396 SEC_ACL *newacl;
397 SEC_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(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
405 return False;
407 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(SEC_ACE));
408 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(SEC_ACE));
409 newacl = make_sec_acl(ctx, (*the_acl)->revision,
410 1+(*the_acl)->num_aces, aces);
411 SAFE_FREE(aces);
412 (*the_acl) = newacl;
413 return True;
417 /* parse a ascii version of a security descriptor */
418 static SEC_DESC *
419 sec_desc_parse(TALLOC_CTX *ctx,
420 struct cli_state *ipc_cli,
421 POLICY_HND *pol,
422 bool numeric,
423 char *str)
425 const char *p = str;
426 char *tok;
427 SEC_DESC *ret = NULL;
428 size_t sd_size;
429 DOM_SID *group_sid=NULL;
430 DOM_SID *owner_sid=NULL;
431 SEC_ACL *dacl=NULL;
432 int revision=1;
434 while (next_token_talloc(ctx, &p, &tok, "\t,\r\n")) {
436 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
437 revision = strtol(tok+9, NULL, 16);
438 continue;
441 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
442 if (owner_sid) {
443 DEBUG(5, ("OWNER specified more than once!\n"));
444 goto done;
446 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
447 if (!owner_sid ||
448 !convert_string_to_sid(ipc_cli, pol,
449 numeric,
450 owner_sid, tok+6)) {
451 DEBUG(5, ("Failed to parse owner sid\n"));
452 goto done;
454 continue;
457 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
458 if (owner_sid) {
459 DEBUG(5, ("OWNER specified more than once!\n"));
460 goto done;
462 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
463 if (!owner_sid ||
464 !convert_string_to_sid(ipc_cli, pol,
465 False,
466 owner_sid, tok+7)) {
467 DEBUG(5, ("Failed to parse owner sid\n"));
468 goto done;
470 continue;
473 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
474 if (group_sid) {
475 DEBUG(5, ("GROUP specified more than once!\n"));
476 goto done;
478 group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
479 if (!group_sid ||
480 !convert_string_to_sid(ipc_cli, pol,
481 numeric,
482 group_sid, tok+6)) {
483 DEBUG(5, ("Failed to parse group sid\n"));
484 goto done;
486 continue;
489 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
490 if (group_sid) {
491 DEBUG(5, ("GROUP specified more than once!\n"));
492 goto done;
494 group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
495 if (!group_sid ||
496 !convert_string_to_sid(ipc_cli, pol,
497 False,
498 group_sid, tok+6)) {
499 DEBUG(5, ("Failed to parse group sid\n"));
500 goto done;
502 continue;
505 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
506 SEC_ACE ace;
507 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
508 DEBUG(5, ("Failed to parse ACL %s\n", tok));
509 goto done;
511 if(!add_ace(&dacl, &ace, ctx)) {
512 DEBUG(5, ("Failed to add ACL %s\n", tok));
513 goto done;
515 continue;
518 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
519 SEC_ACE ace;
520 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
521 DEBUG(5, ("Failed to parse ACL %s\n", tok));
522 goto done;
524 if(!add_ace(&dacl, &ace, ctx)) {
525 DEBUG(5, ("Failed to add ACL %s\n", tok));
526 goto done;
528 continue;
531 DEBUG(5, ("Failed to parse security descriptor\n"));
532 goto done;
535 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
536 owner_sid, group_sid, NULL, dacl, &sd_size);
538 done:
539 SAFE_FREE(group_sid);
540 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->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 POLICY_HND *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 int fnum = -1;
730 SEC_DESC *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->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) {
812 /* Exclusions are delimited by '!' */
813 for (;
814 pExclude != NULL;
815 pExclude = (p == NULL ? NULL : p + 1)) {
817 /* Find end of this exclusion name */
818 if ((p = strchr(pExclude, '!')) != NULL)
820 *p = '\0';
823 /* Which exclusion name is this? */
824 if (StrCaseCmp(pExclude, "nt_sec_desc.revision") == 0) {
825 exclude_nt_revision = True;
827 else if (StrCaseCmp(pExclude, "nt_sec_desc.owner") == 0) {
828 exclude_nt_owner = True;
830 else if (StrCaseCmp(pExclude, "nt_sec_desc.group") == 0) {
831 exclude_nt_group = True;
833 else if (StrCaseCmp(pExclude, "nt_sec_desc.acl") == 0) {
834 exclude_nt_acl = True;
836 else if (StrCaseCmp(pExclude, "dos_attr.mode") == 0) {
837 exclude_dos_mode = True;
839 else if (StrCaseCmp(pExclude, "dos_attr.size") == 0) {
840 exclude_dos_size = True;
842 else if (excl_attr_strings.create_time_attr != NULL &&
843 StrCaseCmp(pExclude,
844 excl_attr_strings.change_time_attr) == 0) {
845 exclude_dos_create_time = True;
847 else if (StrCaseCmp(pExclude,
848 excl_attr_strings.access_time_attr) == 0) {
849 exclude_dos_access_time = True;
851 else if (StrCaseCmp(pExclude,
852 excl_attr_strings.write_time_attr) == 0) {
853 exclude_dos_write_time = True;
855 else if (StrCaseCmp(pExclude,
856 excl_attr_strings.change_time_attr) == 0) {
857 exclude_dos_change_time = True;
859 else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
860 exclude_dos_inode = True;
862 else {
863 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
864 pExclude));
865 errno = ENOATTR;
866 return -1;
871 n_used = 0;
874 * If we are (possibly) talking to an NT or new system and some NT
875 * attributes have been requested...
877 if (ipc_cli && (all || some_nt || all_nt_acls)) {
878 /* Point to the portion after "system.nt_sec_desc." */
879 name += 19; /* if (all) this will be invalid but unused */
881 /* ... then obtain any NT attributes which were requested */
882 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
884 if (fnum == -1) {
885 DEBUG(5, ("cacl_get failed to open %s: %s\n",
886 filename, cli_errstr(cli)));
887 errno = 0;
888 return -1;
891 sd = cli_query_secdesc(cli, fnum, ctx);
893 if (!sd) {
894 DEBUG(5,
895 ("cacl_get Failed to query old descriptor\n"));
896 errno = 0;
897 return -1;
900 cli_close(cli, fnum);
902 if (! exclude_nt_revision) {
903 if (all || all_nt) {
904 if (determine_size) {
905 p = talloc_asprintf(ctx,
906 "REVISION:%d",
907 sd->revision);
908 if (!p) {
909 errno = ENOMEM;
910 return -1;
912 n = strlen(p);
913 } else {
914 n = snprintf(buf, bufsize,
915 "REVISION:%d",
916 sd->revision);
918 } else if (StrCaseCmp(name, "revision") == 0) {
919 if (determine_size) {
920 p = talloc_asprintf(ctx, "%d",
921 sd->revision);
922 if (!p) {
923 errno = ENOMEM;
924 return -1;
926 n = strlen(p);
927 } else {
928 n = snprintf(buf, bufsize, "%d",
929 sd->revision);
933 if (!determine_size && n > bufsize) {
934 errno = ERANGE;
935 return -1;
937 buf += n;
938 n_used += n;
939 bufsize -= n;
940 n = 0;
943 if (! exclude_nt_owner) {
944 /* Get owner and group sid */
945 if (sd->owner_sid) {
946 convert_sid_to_string(ipc_cli, pol,
947 sidstr,
948 numeric,
949 sd->owner_sid);
950 } else {
951 fstrcpy(sidstr, "");
954 if (all || all_nt) {
955 if (determine_size) {
956 p = talloc_asprintf(ctx, ",OWNER:%s",
957 sidstr);
958 if (!p) {
959 errno = ENOMEM;
960 return -1;
962 n = strlen(p);
963 } else if (sidstr[0] != '\0') {
964 n = snprintf(buf, bufsize,
965 ",OWNER:%s", sidstr);
967 } else if (StrnCaseCmp(name, "owner", 5) == 0) {
968 if (determine_size) {
969 p = talloc_asprintf(ctx, "%s", sidstr);
970 if (!p) {
971 errno = ENOMEM;
972 return -1;
974 n = strlen(p);
975 } else {
976 n = snprintf(buf, bufsize, "%s",
977 sidstr);
981 if (!determine_size && n > bufsize) {
982 errno = ERANGE;
983 return -1;
985 buf += n;
986 n_used += n;
987 bufsize -= n;
988 n = 0;
991 if (! exclude_nt_group) {
992 if (sd->group_sid) {
993 convert_sid_to_string(ipc_cli, pol,
994 sidstr, numeric,
995 sd->group_sid);
996 } else {
997 fstrcpy(sidstr, "");
1000 if (all || all_nt) {
1001 if (determine_size) {
1002 p = talloc_asprintf(ctx, ",GROUP:%s",
1003 sidstr);
1004 if (!p) {
1005 errno = ENOMEM;
1006 return -1;
1008 n = strlen(p);
1009 } else if (sidstr[0] != '\0') {
1010 n = snprintf(buf, bufsize,
1011 ",GROUP:%s", sidstr);
1013 } else if (StrnCaseCmp(name, "group", 5) == 0) {
1014 if (determine_size) {
1015 p = talloc_asprintf(ctx, "%s", sidstr);
1016 if (!p) {
1017 errno = ENOMEM;
1018 return -1;
1020 n = strlen(p);
1021 } else {
1022 n = snprintf(buf, bufsize,
1023 "%s", sidstr);
1027 if (!determine_size && n > bufsize) {
1028 errno = ERANGE;
1029 return -1;
1031 buf += n;
1032 n_used += n;
1033 bufsize -= n;
1034 n = 0;
1037 if (! exclude_nt_acl) {
1038 /* Add aces to value buffer */
1039 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
1041 SEC_ACE *ace = &sd->dacl->aces[i];
1042 convert_sid_to_string(ipc_cli, pol,
1043 sidstr, numeric,
1044 &ace->trustee);
1046 if (all || all_nt) {
1047 if (determine_size) {
1048 p = talloc_asprintf(
1049 ctx,
1050 ",ACL:"
1051 "%s:%d/%d/0x%08x",
1052 sidstr,
1053 ace->type,
1054 ace->flags,
1055 ace->access_mask);
1056 if (!p) {
1057 errno = ENOMEM;
1058 return -1;
1060 n = strlen(p);
1061 } else {
1062 n = snprintf(
1063 buf, bufsize,
1064 ",ACL:%s:%d/%d/0x%08x",
1065 sidstr,
1066 ace->type,
1067 ace->flags,
1068 ace->access_mask);
1070 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
1071 StrCaseCmp(name+3, sidstr) == 0) ||
1072 (StrnCaseCmp(name, "acl+", 4) == 0 &&
1073 StrCaseCmp(name+4, sidstr) == 0)) {
1074 if (determine_size) {
1075 p = talloc_asprintf(
1076 ctx,
1077 "%d/%d/0x%08x",
1078 ace->type,
1079 ace->flags,
1080 ace->access_mask);
1081 if (!p) {
1082 errno = ENOMEM;
1083 return -1;
1085 n = strlen(p);
1086 } else {
1087 n = snprintf(buf, bufsize,
1088 "%d/%d/0x%08x",
1089 ace->type,
1090 ace->flags,
1091 ace->access_mask);
1093 } else if (all_nt_acls) {
1094 if (determine_size) {
1095 p = talloc_asprintf(
1096 ctx,
1097 "%s%s:%d/%d/0x%08x",
1098 i ? "," : "",
1099 sidstr,
1100 ace->type,
1101 ace->flags,
1102 ace->access_mask);
1103 if (!p) {
1104 errno = ENOMEM;
1105 return -1;
1107 n = strlen(p);
1108 } else {
1109 n = snprintf(buf, bufsize,
1110 "%s%s:%d/%d/0x%08x",
1111 i ? "," : "",
1112 sidstr,
1113 ace->type,
1114 ace->flags,
1115 ace->access_mask);
1118 if (!determine_size && n > bufsize) {
1119 errno = ERANGE;
1120 return -1;
1122 buf += n;
1123 n_used += n;
1124 bufsize -= n;
1125 n = 0;
1129 /* Restore name pointer to its original value */
1130 name -= 19;
1133 if (all || some_dos) {
1134 /* Point to the portion after "system.dos_attr." */
1135 name += 16; /* if (all) this will be invalid but unused */
1137 /* Obtain the DOS attributes */
1138 if (!SMBC_getatr(context, srv, filename, &mode, &size,
1139 &create_time_ts,
1140 &access_time_ts,
1141 &write_time_ts,
1142 &change_time_ts,
1143 &ino)) {
1145 errno = SMBC_errno(context, srv->cli);
1146 return -1;
1150 create_time = convert_timespec_to_time_t(create_time_ts);
1151 access_time = convert_timespec_to_time_t(access_time_ts);
1152 write_time = convert_timespec_to_time_t(write_time_ts);
1153 change_time = convert_timespec_to_time_t(change_time_ts);
1155 if (! exclude_dos_mode) {
1156 if (all || all_dos) {
1157 if (determine_size) {
1158 p = talloc_asprintf(ctx,
1159 "%sMODE:0x%x",
1160 (ipc_cli &&
1161 (all || some_nt)
1162 ? ","
1163 : ""),
1164 mode);
1165 if (!p) {
1166 errno = ENOMEM;
1167 return -1;
1169 n = strlen(p);
1170 } else {
1171 n = snprintf(buf, bufsize,
1172 "%sMODE:0x%x",
1173 (ipc_cli &&
1174 (all || some_nt)
1175 ? ","
1176 : ""),
1177 mode);
1179 } else if (StrCaseCmp(name, "mode") == 0) {
1180 if (determine_size) {
1181 p = talloc_asprintf(ctx, "0x%x", mode);
1182 if (!p) {
1183 errno = ENOMEM;
1184 return -1;
1186 n = strlen(p);
1187 } else {
1188 n = snprintf(buf, bufsize,
1189 "0x%x", mode);
1193 if (!determine_size && n > bufsize) {
1194 errno = ERANGE;
1195 return -1;
1197 buf += n;
1198 n_used += n;
1199 bufsize -= n;
1200 n = 0;
1203 if (! exclude_dos_size) {
1204 if (all || all_dos) {
1205 if (determine_size) {
1206 p = talloc_asprintf(
1207 ctx,
1208 ",SIZE:%.0f",
1209 (double)size);
1210 if (!p) {
1211 errno = ENOMEM;
1212 return -1;
1214 n = strlen(p);
1215 } else {
1216 n = snprintf(buf, bufsize,
1217 ",SIZE:%.0f",
1218 (double)size);
1220 } else if (StrCaseCmp(name, "size") == 0) {
1221 if (determine_size) {
1222 p = talloc_asprintf(
1223 ctx,
1224 "%.0f",
1225 (double)size);
1226 if (!p) {
1227 errno = ENOMEM;
1228 return -1;
1230 n = strlen(p);
1231 } else {
1232 n = snprintf(buf, bufsize,
1233 "%.0f",
1234 (double)size);
1238 if (!determine_size && n > bufsize) {
1239 errno = ERANGE;
1240 return -1;
1242 buf += n;
1243 n_used += n;
1244 bufsize -= n;
1245 n = 0;
1248 if (! exclude_dos_create_time &&
1249 attr_strings.create_time_attr != NULL) {
1250 if (all || all_dos) {
1251 if (determine_size) {
1252 p = talloc_asprintf(ctx,
1253 ",%s:%lu",
1254 attr_strings.create_time_attr,
1255 create_time);
1256 if (!p) {
1257 errno = ENOMEM;
1258 return -1;
1260 n = strlen(p);
1261 } else {
1262 n = snprintf(buf, bufsize,
1263 ",%s:%lu",
1264 attr_strings.create_time_attr,
1265 create_time);
1267 } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
1268 if (determine_size) {
1269 p = talloc_asprintf(ctx, "%lu", create_time);
1270 if (!p) {
1271 errno = ENOMEM;
1272 return -1;
1274 n = strlen(p);
1275 } else {
1276 n = snprintf(buf, bufsize,
1277 "%lu", create_time);
1281 if (!determine_size && n > bufsize) {
1282 errno = ERANGE;
1283 return -1;
1285 buf += n;
1286 n_used += n;
1287 bufsize -= n;
1288 n = 0;
1291 if (! exclude_dos_access_time) {
1292 if (all || all_dos) {
1293 if (determine_size) {
1294 p = talloc_asprintf(ctx,
1295 ",%s:%lu",
1296 attr_strings.access_time_attr,
1297 access_time);
1298 if (!p) {
1299 errno = ENOMEM;
1300 return -1;
1302 n = strlen(p);
1303 } else {
1304 n = snprintf(buf, bufsize,
1305 ",%s:%lu",
1306 attr_strings.access_time_attr,
1307 access_time);
1309 } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
1310 if (determine_size) {
1311 p = talloc_asprintf(ctx, "%lu", access_time);
1312 if (!p) {
1313 errno = ENOMEM;
1314 return -1;
1316 n = strlen(p);
1317 } else {
1318 n = snprintf(buf, bufsize,
1319 "%lu", access_time);
1323 if (!determine_size && n > bufsize) {
1324 errno = ERANGE;
1325 return -1;
1327 buf += n;
1328 n_used += n;
1329 bufsize -= n;
1330 n = 0;
1333 if (! exclude_dos_write_time) {
1334 if (all || all_dos) {
1335 if (determine_size) {
1336 p = talloc_asprintf(ctx,
1337 ",%s:%lu",
1338 attr_strings.write_time_attr,
1339 write_time);
1340 if (!p) {
1341 errno = ENOMEM;
1342 return -1;
1344 n = strlen(p);
1345 } else {
1346 n = snprintf(buf, bufsize,
1347 ",%s:%lu",
1348 attr_strings.write_time_attr,
1349 write_time);
1351 } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
1352 if (determine_size) {
1353 p = talloc_asprintf(ctx, "%lu", write_time);
1354 if (!p) {
1355 errno = ENOMEM;
1356 return -1;
1358 n = strlen(p);
1359 } else {
1360 n = snprintf(buf, bufsize,
1361 "%lu", write_time);
1365 if (!determine_size && n > bufsize) {
1366 errno = ERANGE;
1367 return -1;
1369 buf += n;
1370 n_used += n;
1371 bufsize -= n;
1372 n = 0;
1375 if (! exclude_dos_change_time) {
1376 if (all || all_dos) {
1377 if (determine_size) {
1378 p = talloc_asprintf(ctx,
1379 ",%s:%lu",
1380 attr_strings.change_time_attr,
1381 change_time);
1382 if (!p) {
1383 errno = ENOMEM;
1384 return -1;
1386 n = strlen(p);
1387 } else {
1388 n = snprintf(buf, bufsize,
1389 ",%s:%lu",
1390 attr_strings.change_time_attr,
1391 change_time);
1393 } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
1394 if (determine_size) {
1395 p = talloc_asprintf(ctx, "%lu", change_time);
1396 if (!p) {
1397 errno = ENOMEM;
1398 return -1;
1400 n = strlen(p);
1401 } else {
1402 n = snprintf(buf, bufsize,
1403 "%lu", change_time);
1407 if (!determine_size && n > bufsize) {
1408 errno = ERANGE;
1409 return -1;
1411 buf += n;
1412 n_used += n;
1413 bufsize -= n;
1414 n = 0;
1417 if (! exclude_dos_inode) {
1418 if (all || all_dos) {
1419 if (determine_size) {
1420 p = talloc_asprintf(
1421 ctx,
1422 ",INODE:%.0f",
1423 (double)ino);
1424 if (!p) {
1425 errno = ENOMEM;
1426 return -1;
1428 n = strlen(p);
1429 } else {
1430 n = snprintf(buf, bufsize,
1431 ",INODE:%.0f",
1432 (double) ino);
1434 } else if (StrCaseCmp(name, "inode") == 0) {
1435 if (determine_size) {
1436 p = talloc_asprintf(
1437 ctx,
1438 "%.0f",
1439 (double) ino);
1440 if (!p) {
1441 errno = ENOMEM;
1442 return -1;
1444 n = strlen(p);
1445 } else {
1446 n = snprintf(buf, bufsize,
1447 "%.0f",
1448 (double) ino);
1452 if (!determine_size && n > bufsize) {
1453 errno = ERANGE;
1454 return -1;
1456 buf += n;
1457 n_used += n;
1458 bufsize -= n;
1459 n = 0;
1462 /* Restore name pointer to its original value */
1463 name -= 16;
1466 if (n_used == 0) {
1467 errno = ENOATTR;
1468 return -1;
1471 return n_used;
1474 /*****************************************************
1475 set the ACLs on a file given an ascii description
1476 *******************************************************/
1477 static int
1478 cacl_set(TALLOC_CTX *ctx,
1479 struct cli_state *cli,
1480 struct cli_state *ipc_cli,
1481 POLICY_HND *pol,
1482 const char *filename,
1483 const char *the_acl,
1484 int mode,
1485 int flags)
1487 int fnum;
1488 int err = 0;
1489 SEC_DESC *sd = NULL, *old;
1490 SEC_ACL *dacl = NULL;
1491 DOM_SID *owner_sid = NULL;
1492 DOM_SID *group_sid = NULL;
1493 uint32 i, j;
1494 size_t sd_size;
1495 int ret = 0;
1496 char *p;
1497 bool numeric = True;
1499 /* the_acl will be null for REMOVE_ALL operations */
1500 if (the_acl) {
1501 numeric = ((p = strchr(the_acl, ':')) != NULL &&
1502 p > the_acl &&
1503 p[-1] != '+');
1505 /* if this is to set the entire ACL... */
1506 if (*the_acl == '*') {
1507 /* ... then increment past the first colon */
1508 the_acl = p + 1;
1511 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric,
1512 CONST_DISCARD(char *, the_acl));
1514 if (!sd) {
1515 errno = EINVAL;
1516 return -1;
1520 /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
1521 that doesn't deref sd */
1523 if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
1524 errno = EINVAL;
1525 return -1;
1528 /* The desired access below is the only one I could find that works
1529 with NT4, W2KP and Samba */
1531 fnum = cli_nt_create(cli, filename, CREATE_ACCESS_READ);
1533 if (fnum == -1) {
1534 DEBUG(5, ("cacl_set failed to open %s: %s\n",
1535 filename, cli_errstr(cli)));
1536 errno = 0;
1537 return -1;
1540 old = cli_query_secdesc(cli, fnum, ctx);
1542 if (!old) {
1543 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
1544 errno = 0;
1545 return -1;
1548 cli_close(cli, fnum);
1550 switch (mode) {
1551 case SMBC_XATTR_MODE_REMOVE_ALL:
1552 old->dacl->num_aces = 0;
1553 dacl = old->dacl;
1554 break;
1556 case SMBC_XATTR_MODE_REMOVE:
1557 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1558 bool found = False;
1560 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
1561 if (sec_ace_equal(&sd->dacl->aces[i],
1562 &old->dacl->aces[j])) {
1563 uint32 k;
1564 for (k=j; k<old->dacl->num_aces-1;k++) {
1565 old->dacl->aces[k] =
1566 old->dacl->aces[k+1];
1568 old->dacl->num_aces--;
1569 found = True;
1570 dacl = old->dacl;
1571 break;
1575 if (!found) {
1576 err = ENOATTR;
1577 ret = -1;
1578 goto failed;
1581 break;
1583 case SMBC_XATTR_MODE_ADD:
1584 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1585 bool found = False;
1587 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
1588 if (sid_equal(&sd->dacl->aces[i].trustee,
1589 &old->dacl->aces[j].trustee)) {
1590 if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
1591 err = EEXIST;
1592 ret = -1;
1593 goto failed;
1595 old->dacl->aces[j] = sd->dacl->aces[i];
1596 ret = -1;
1597 found = True;
1601 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
1602 err = ENOATTR;
1603 ret = -1;
1604 goto failed;
1607 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1608 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
1611 dacl = old->dacl;
1612 break;
1614 case SMBC_XATTR_MODE_SET:
1615 old = sd;
1616 owner_sid = old->owner_sid;
1617 group_sid = old->group_sid;
1618 dacl = old->dacl;
1619 break;
1621 case SMBC_XATTR_MODE_CHOWN:
1622 owner_sid = sd->owner_sid;
1623 break;
1625 case SMBC_XATTR_MODE_CHGRP:
1626 group_sid = sd->group_sid;
1627 break;
1630 /* Denied ACE entries must come before allowed ones */
1631 sort_acl(old->dacl);
1633 /* Create new security descriptor and set it */
1634 sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
1635 owner_sid, group_sid, NULL, dacl, &sd_size);
1637 fnum = cli_nt_create(cli, filename,
1638 WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
1640 if (fnum == -1) {
1641 DEBUG(5, ("cacl_set failed to open %s: %s\n",
1642 filename, cli_errstr(cli)));
1643 errno = 0;
1644 return -1;
1647 if (!cli_set_secdesc(cli, fnum, sd)) {
1648 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
1649 ret = -1;
1652 /* Clean up */
1654 failed:
1655 cli_close(cli, fnum);
1657 if (err != 0) {
1658 errno = err;
1661 return ret;
1666 SMBC_setxattr_ctx(SMBCCTX *context,
1667 const char *fname,
1668 const char *name,
1669 const void *value,
1670 size_t size,
1671 int flags)
1673 int ret;
1674 int ret2;
1675 SMBCSRV *srv = NULL;
1676 SMBCSRV *ipc_srv = NULL;
1677 char *server = NULL;
1678 char *share = NULL;
1679 char *user = NULL;
1680 char *password = NULL;
1681 char *workgroup = NULL;
1682 char *path = NULL;
1683 DOS_ATTR_DESC *dad = NULL;
1684 struct {
1685 const char * create_time_attr;
1686 const char * access_time_attr;
1687 const char * write_time_attr;
1688 const char * change_time_attr;
1689 } attr_strings;
1690 TALLOC_CTX *frame = talloc_stackframe();
1692 if (!context || !context->initialized) {
1694 errno = EINVAL; /* Best I can think of ... */
1695 TALLOC_FREE(frame);
1696 return -1;
1699 if (!fname) {
1700 errno = EINVAL;
1701 TALLOC_FREE(frame);
1702 return -1;
1705 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
1706 fname, name, (int) size, (const char*)value));
1708 if (SMBC_parse_path(frame,
1709 context,
1710 fname,
1711 &workgroup,
1712 &server,
1713 &share,
1714 &path,
1715 &user,
1716 &password,
1717 NULL)) {
1718 errno = EINVAL;
1719 TALLOC_FREE(frame);
1720 return -1;
1723 if (!user || user[0] == (char)0) {
1724 user = talloc_strdup(frame, context->user);
1725 if (!user) {
1726 errno = ENOMEM;
1727 TALLOC_FREE(frame);
1728 return -1;
1732 srv = SMBC_server(frame, context, True,
1733 server, share, &workgroup, &user, &password);
1734 if (!srv) {
1735 TALLOC_FREE(frame);
1736 return -1; /* errno set by SMBC_server */
1739 if (! srv->no_nt_session) {
1740 ipc_srv = SMBC_attr_server(frame, context, server, share,
1741 &workgroup, &user, &password);
1742 if (! ipc_srv) {
1743 srv->no_nt_session = True;
1745 } else {
1746 ipc_srv = NULL;
1750 * Are they asking to set the entire set of known attributes?
1752 if (StrCaseCmp(name, "system.*") == 0 ||
1753 StrCaseCmp(name, "system.*+") == 0) {
1754 /* Yup. */
1755 char *namevalue =
1756 talloc_asprintf(talloc_tos(), "%s:%s",
1757 name+7, (const char *) value);
1758 if (! namevalue) {
1759 errno = ENOMEM;
1760 ret = -1;
1761 TALLOC_FREE(frame);
1762 return -1;
1765 if (ipc_srv) {
1766 ret = cacl_set(talloc_tos(), srv->cli,
1767 ipc_srv->cli, &ipc_srv->pol, path,
1768 namevalue,
1769 (*namevalue == '*'
1770 ? SMBC_XATTR_MODE_SET
1771 : SMBC_XATTR_MODE_ADD),
1772 flags);
1773 } else {
1774 ret = 0;
1777 /* get a DOS Attribute Descriptor with current attributes */
1778 dad = dos_attr_query(context, talloc_tos(), path, srv);
1779 if (dad) {
1780 /* Overwrite old with new, using what was provided */
1781 dos_attr_parse(context, dad, srv, namevalue);
1783 /* Set the new DOS attributes */
1784 if (! SMBC_setatr(context, srv, path,
1785 dad->create_time,
1786 dad->access_time,
1787 dad->write_time,
1788 dad->change_time,
1789 dad->mode)) {
1791 /* cause failure if NT failed too */
1792 dad = NULL;
1796 /* we only fail if both NT and DOS sets failed */
1797 if (ret < 0 && ! dad) {
1798 ret = -1; /* in case dad was null */
1800 else {
1801 ret = 0;
1804 TALLOC_FREE(frame);
1805 return ret;
1809 * Are they asking to set an access control element or to set
1810 * the entire access control list?
1812 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
1813 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
1814 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
1815 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
1816 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
1818 /* Yup. */
1819 char *namevalue =
1820 talloc_asprintf(talloc_tos(), "%s:%s",
1821 name+19, (const char *) value);
1823 if (! ipc_srv) {
1824 ret = -1; /* errno set by SMBC_server() */
1826 else if (! namevalue) {
1827 errno = ENOMEM;
1828 ret = -1;
1829 } else {
1830 ret = cacl_set(talloc_tos(), srv->cli,
1831 ipc_srv->cli, &ipc_srv->pol, path,
1832 namevalue,
1833 (*namevalue == '*'
1834 ? SMBC_XATTR_MODE_SET
1835 : SMBC_XATTR_MODE_ADD),
1836 flags);
1838 TALLOC_FREE(frame);
1839 return ret;
1843 * Are they asking to set the owner?
1845 if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
1846 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
1848 /* Yup. */
1849 char *namevalue =
1850 talloc_asprintf(talloc_tos(), "%s:%s",
1851 name+19, (const char *) value);
1853 if (! ipc_srv) {
1854 ret = -1; /* errno set by SMBC_server() */
1856 else if (! namevalue) {
1857 errno = ENOMEM;
1858 ret = -1;
1859 } else {
1860 ret = cacl_set(talloc_tos(), srv->cli,
1861 ipc_srv->cli, &ipc_srv->pol, path,
1862 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
1864 TALLOC_FREE(frame);
1865 return ret;
1869 * Are they asking to set the group?
1871 if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
1872 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
1874 /* Yup. */
1875 char *namevalue =
1876 talloc_asprintf(talloc_tos(), "%s:%s",
1877 name+19, (const char *) value);
1879 if (! ipc_srv) {
1880 /* errno set by SMBC_server() */
1881 ret = -1;
1883 else if (! namevalue) {
1884 errno = ENOMEM;
1885 ret = -1;
1886 } else {
1887 ret = cacl_set(talloc_tos(), srv->cli,
1888 ipc_srv->cli, &ipc_srv->pol, path,
1889 namevalue, SMBC_XATTR_MODE_CHGRP, 0);
1891 TALLOC_FREE(frame);
1892 return ret;
1895 /* Determine whether to use old-style or new-style attribute names */
1896 if (context->full_time_names) {
1897 /* new-style names */
1898 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
1899 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
1900 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
1901 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
1902 } else {
1903 /* old-style names */
1904 attr_strings.create_time_attr = NULL;
1905 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
1906 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
1907 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
1911 * Are they asking to set a DOS attribute?
1913 if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
1914 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
1915 (attr_strings.create_time_attr != NULL &&
1916 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
1917 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
1918 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
1919 StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
1921 /* get a DOS Attribute Descriptor with current attributes */
1922 dad = dos_attr_query(context, talloc_tos(), path, srv);
1923 if (dad) {
1924 char *namevalue =
1925 talloc_asprintf(talloc_tos(), "%s:%s",
1926 name+16, (const char *) value);
1927 if (! namevalue) {
1928 errno = ENOMEM;
1929 ret = -1;
1930 } else {
1931 /* Overwrite old with provided new params */
1932 dos_attr_parse(context, dad, srv, namevalue);
1934 /* Set the new DOS attributes */
1935 ret2 = SMBC_setatr(context, srv, path,
1936 dad->create_time,
1937 dad->access_time,
1938 dad->write_time,
1939 dad->change_time,
1940 dad->mode);
1942 /* ret2 has True (success) / False (failure) */
1943 if (ret2) {
1944 ret = 0;
1945 } else {
1946 ret = -1;
1949 } else {
1950 ret = -1;
1953 TALLOC_FREE(frame);
1954 return ret;
1957 /* Unsupported attribute name */
1958 errno = EINVAL;
1959 TALLOC_FREE(frame);
1960 return -1;
1964 SMBC_getxattr_ctx(SMBCCTX *context,
1965 const char *fname,
1966 const char *name,
1967 const void *value,
1968 size_t size)
1970 int ret;
1971 SMBCSRV *srv = NULL;
1972 SMBCSRV *ipc_srv = NULL;
1973 char *server = NULL;
1974 char *share = NULL;
1975 char *user = NULL;
1976 char *password = NULL;
1977 char *workgroup = NULL;
1978 char *path = NULL;
1979 struct {
1980 const char * create_time_attr;
1981 const char * access_time_attr;
1982 const char * write_time_attr;
1983 const char * change_time_attr;
1984 } attr_strings;
1985 TALLOC_CTX *frame = talloc_stackframe();
1987 if (!context || !context->initialized) {
1989 errno = EINVAL; /* Best I can think of ... */
1990 TALLOC_FREE(frame);
1991 return -1;
1994 if (!fname) {
1995 errno = EINVAL;
1996 TALLOC_FREE(frame);
1997 return -1;
2000 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
2002 if (SMBC_parse_path(frame,
2003 context,
2004 fname,
2005 &workgroup,
2006 &server,
2007 &share,
2008 &path,
2009 &user,
2010 &password,
2011 NULL)) {
2012 errno = EINVAL;
2013 TALLOC_FREE(frame);
2014 return -1;
2017 if (!user || user[0] == (char)0) {
2018 user = talloc_strdup(frame, context->user);
2019 if (!user) {
2020 errno = ENOMEM;
2021 TALLOC_FREE(frame);
2022 return -1;
2026 srv = SMBC_server(frame, context, True,
2027 server, share, &workgroup, &user, &password);
2028 if (!srv) {
2029 TALLOC_FREE(frame);
2030 return -1; /* errno set by SMBC_server */
2033 if (! srv->no_nt_session) {
2034 ipc_srv = SMBC_attr_server(frame, context, server, share,
2035 &workgroup, &user, &password);
2036 if (! ipc_srv) {
2037 srv->no_nt_session = True;
2039 } else {
2040 ipc_srv = NULL;
2043 /* Determine whether to use old-style or new-style attribute names */
2044 if (context->full_time_names) {
2045 /* new-style names */
2046 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
2047 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
2048 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
2049 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
2050 } else {
2051 /* old-style names */
2052 attr_strings.create_time_attr = NULL;
2053 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
2054 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
2055 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
2058 /* Are they requesting a supported attribute? */
2059 if (StrCaseCmp(name, "system.*") == 0 ||
2060 StrnCaseCmp(name, "system.*!", 9) == 0 ||
2061 StrCaseCmp(name, "system.*+") == 0 ||
2062 StrnCaseCmp(name, "system.*+!", 10) == 0 ||
2063 StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
2064 StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
2065 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
2066 StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
2067 StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
2068 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
2069 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
2070 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
2071 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
2072 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
2073 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
2074 StrCaseCmp(name, "system.dos_attr.*") == 0 ||
2075 StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
2076 StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
2077 StrCaseCmp(name, "system.dos_attr.size") == 0 ||
2078 (attr_strings.create_time_attr != NULL &&
2079 StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
2080 StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
2081 StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
2082 StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
2083 StrCaseCmp(name, "system.dos_attr.inode") == 0) {
2085 /* Yup. */
2086 ret = cacl_get(context, talloc_tos(), srv,
2087 ipc_srv == NULL ? NULL : ipc_srv->cli,
2088 &ipc_srv->pol, path,
2089 CONST_DISCARD(char *, name),
2090 CONST_DISCARD(char *, value), size);
2091 if (ret < 0 && errno == 0) {
2092 errno = SMBC_errno(context, srv->cli);
2094 TALLOC_FREE(frame);
2095 return ret;
2098 /* Unsupported attribute name */
2099 errno = EINVAL;
2100 TALLOC_FREE(frame);
2101 return -1;
2106 SMBC_removexattr_ctx(SMBCCTX *context,
2107 const char *fname,
2108 const char *name)
2110 int ret;
2111 SMBCSRV *srv = NULL;
2112 SMBCSRV *ipc_srv = NULL;
2113 char *server = NULL;
2114 char *share = NULL;
2115 char *user = NULL;
2116 char *password = NULL;
2117 char *workgroup = NULL;
2118 char *path = NULL;
2119 TALLOC_CTX *frame = talloc_stackframe();
2121 if (!context || !context->initialized) {
2123 errno = EINVAL; /* Best I can think of ... */
2124 TALLOC_FREE(frame);
2125 return -1;
2128 if (!fname) {
2129 errno = EINVAL;
2130 TALLOC_FREE(frame);
2131 return -1;
2134 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
2136 if (SMBC_parse_path(frame,
2137 context,
2138 fname,
2139 &workgroup,
2140 &server,
2141 &share,
2142 &path,
2143 &user,
2144 &password,
2145 NULL)) {
2146 errno = EINVAL;
2147 TALLOC_FREE(frame);
2148 return -1;
2151 if (!user || user[0] == (char)0) {
2152 user = talloc_strdup(frame, context->user);
2153 if (!user) {
2154 errno = ENOMEM;
2155 TALLOC_FREE(frame);
2156 return -1;
2160 srv = SMBC_server(frame, context, True,
2161 server, share, &workgroup, &user, &password);
2162 if (!srv) {
2163 TALLOC_FREE(frame);
2164 return -1; /* errno set by SMBC_server */
2167 if (! srv->no_nt_session) {
2168 ipc_srv = SMBC_attr_server(frame, context, server, share,
2169 &workgroup, &user, &password);
2170 if (! ipc_srv) {
2171 srv->no_nt_session = True;
2173 } else {
2174 ipc_srv = NULL;
2177 if (! ipc_srv) {
2178 TALLOC_FREE(frame);
2179 return -1; /* errno set by SMBC_attr_server */
2182 /* Are they asking to set the entire ACL? */
2183 if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
2184 StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
2186 /* Yup. */
2187 ret = cacl_set(talloc_tos(), srv->cli,
2188 ipc_srv->cli, &ipc_srv->pol, path,
2189 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
2190 TALLOC_FREE(frame);
2191 return ret;
2195 * Are they asking to remove one or more spceific security descriptor
2196 * attributes?
2198 if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
2199 StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
2200 StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
2201 StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
2202 StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
2203 StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
2204 StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
2206 /* Yup. */
2207 ret = cacl_set(talloc_tos(), srv->cli,
2208 ipc_srv->cli, &ipc_srv->pol, path,
2209 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
2210 TALLOC_FREE(frame);
2211 return ret;
2214 /* Unsupported attribute name */
2215 errno = EINVAL;
2216 TALLOC_FREE(frame);
2217 return -1;
2221 SMBC_listxattr_ctx(SMBCCTX *context,
2222 const char *fname,
2223 char *list,
2224 size_t size)
2227 * This isn't quite what listxattr() is supposed to do. This returns
2228 * the complete set of attribute names, always, rather than only those
2229 * attribute names which actually exist for a file. Hmmm...
2231 size_t retsize;
2232 const char supported_old[] =
2233 "system.*\0"
2234 "system.*+\0"
2235 "system.nt_sec_desc.revision\0"
2236 "system.nt_sec_desc.owner\0"
2237 "system.nt_sec_desc.owner+\0"
2238 "system.nt_sec_desc.group\0"
2239 "system.nt_sec_desc.group+\0"
2240 "system.nt_sec_desc.acl.*\0"
2241 "system.nt_sec_desc.acl\0"
2242 "system.nt_sec_desc.acl+\0"
2243 "system.nt_sec_desc.*\0"
2244 "system.nt_sec_desc.*+\0"
2245 "system.dos_attr.*\0"
2246 "system.dos_attr.mode\0"
2247 "system.dos_attr.c_time\0"
2248 "system.dos_attr.a_time\0"
2249 "system.dos_attr.m_time\0"
2251 const char supported_new[] =
2252 "system.*\0"
2253 "system.*+\0"
2254 "system.nt_sec_desc.revision\0"
2255 "system.nt_sec_desc.owner\0"
2256 "system.nt_sec_desc.owner+\0"
2257 "system.nt_sec_desc.group\0"
2258 "system.nt_sec_desc.group+\0"
2259 "system.nt_sec_desc.acl.*\0"
2260 "system.nt_sec_desc.acl\0"
2261 "system.nt_sec_desc.acl+\0"
2262 "system.nt_sec_desc.*\0"
2263 "system.nt_sec_desc.*+\0"
2264 "system.dos_attr.*\0"
2265 "system.dos_attr.mode\0"
2266 "system.dos_attr.create_time\0"
2267 "system.dos_attr.access_time\0"
2268 "system.dos_attr.write_time\0"
2269 "system.dos_attr.change_time\0"
2271 const char * supported;
2273 if (context->full_time_names) {
2274 supported = supported_new;
2275 retsize = sizeof(supported_new);
2276 } else {
2277 supported = supported_old;
2278 retsize = sizeof(supported_old);
2281 if (size == 0) {
2282 return retsize;
2285 if (retsize > size) {
2286 errno = ERANGE;
2287 return -1;
2290 /* this can't be strcpy() because there are embedded null characters */
2291 memcpy(list, supported, retsize);
2292 return retsize;