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/>.
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;
40 pipe_hnd = pipe_hnd->next) {
42 if (pipe_hnd->pipe_idx == PI_LSARPC) {
51 * Sort ACEs according to the documentation at
52 * http://support.microsoft.com/kb/269175, at least as far as it defines the
57 ace_compare(SEC_ACE *ace1,
63 /* If the ACEs are equal, we have nothing more to do. */
64 if (sec_ace_equal(ace1, ace2)) {
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);
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);
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);
101 * ACEs applying to an entity's object follow those applying to the
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);
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));
144 sort_acl(SEC_ACL *the_acl)
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])) {
155 for (j=i; j<the_acl->num_aces-1; j++) {
156 the_acl->aces[j] = the_acl->aces[j+1];
165 /* convert a SID to a string, either numeric or username/group */
167 convert_sid_to_string(struct cli_state *ipc_cli,
173 char **domains = NULL;
175 enum lsa_SidType *types = NULL;
176 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
179 sid_to_fstring(str, sid);
182 return; /* no lookup desired */
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,
196 !domains || !domains[0] || !names || !names[0]) {
204 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
205 domains[0], lp_winbind_separator(),
209 /* convert a string to a SID, either numeric or username/group */
211 convert_string_to_sid(struct cli_state *ipc_cli,
217 enum lsa_SidType *types = NULL;
218 DOM_SID *sids = NULL;
220 TALLOC_CTX *ctx = NULL;
221 struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
228 if (strncmp(str, "S-", 2) == 0) {
229 return string_to_sid(sid, str);
236 ctx = talloc_stackframe();
237 if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ctx,
238 pol, 1, &str, NULL, 1, &sids,
244 sid_copy(sid, &sids[0]);
252 /* parse an ACE in the same format as print_ace() */
254 parse_ace(struct cli_state *ipc_cli,
268 const struct perm_value *v;
273 TALLOC_CTX *frame = talloc_stackframe();
275 /* These values discovered by inspection */
276 static const struct perm_value special_values[] = {
286 static const struct perm_value standard_values[] = {
287 { "READ", 0x001200a9 },
288 { "CHANGE", 0x001301bf },
289 { "FULL", 0x001f01ff },
295 p = strchr_m(str,':');
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)) {
309 /* Try to parse text form */
311 if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
317 if (!next_token_talloc(frame, &cp, &tok, "/")) {
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;
331 /* Only numeric form accepted for flags at present */
333 if (!(next_token_talloc(frame, &cp, &tok, "/") &&
334 sscanf(tok, "%i", &aflags))) {
339 if (!next_token_talloc(frame, &cp, &tok, "/")) {
344 if (strncmp(tok, "0x", 2) == 0) {
345 if (sscanf(tok, "%i", &amask) != 1) {
352 for (v = standard_values; v->perm; v++) {
353 if (strcmp(tok, v->perm) == 0) {
364 for (v = special_values; v->perm; v++) {
365 if (v->perm[0] == *p) {
385 init_sec_ace(ace, &sid, atype, mask, aflags);
390 /* add an ACE to a list of ACEs in a SEC_ACL */
392 add_ace(SEC_ACL **the_acl,
400 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
404 if ((aces = SMB_CALLOC_ARRAY(SEC_ACE, 1+(*the_acl)->num_aces)) == NULL) {
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);
417 /* parse a ascii version of a security descriptor */
419 sec_desc_parse(TALLOC_CTX *ctx,
420 struct cli_state *ipc_cli,
427 SEC_DESC *ret = NULL;
429 DOM_SID *group_sid=NULL;
430 DOM_SID *owner_sid=NULL;
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);
441 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
443 DEBUG(5, ("OWNER specified more than once!\n"));
446 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
448 !convert_string_to_sid(ipc_cli, pol,
451 DEBUG(5, ("Failed to parse owner sid\n"));
457 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
459 DEBUG(5, ("OWNER specified more than once!\n"));
462 owner_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
464 !convert_string_to_sid(ipc_cli, pol,
467 DEBUG(5, ("Failed to parse owner sid\n"));
473 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
475 DEBUG(5, ("GROUP specified more than once!\n"));
478 group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
480 !convert_string_to_sid(ipc_cli, pol,
483 DEBUG(5, ("Failed to parse group sid\n"));
489 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
491 DEBUG(5, ("GROUP specified more than once!\n"));
494 group_sid = SMB_CALLOC_ARRAY(DOM_SID, 1);
496 !convert_string_to_sid(ipc_cli, pol,
499 DEBUG(5, ("Failed to parse group sid\n"));
505 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
507 if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
508 DEBUG(5, ("Failed to parse ACL %s\n", tok));
511 if(!add_ace(&dacl, &ace, ctx)) {
512 DEBUG(5, ("Failed to add ACL %s\n", tok));
518 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
520 if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
521 DEBUG(5, ("Failed to parse ACL %s\n", tok));
524 if(!add_ace(&dacl, &ace, ctx)) {
525 DEBUG(5, ("Failed to add ACL %s\n", tok));
531 DEBUG(5, ("Failed to parse security descriptor\n"));
535 ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE,
536 owner_sid, group_sid, NULL, dacl, &sd_size);
539 SAFE_FREE(group_sid);
540 SAFE_FREE(owner_sid);
546 /* Obtain the current dos attributes */
547 static DOS_ATTR_DESC *
548 dos_attr_query(SMBCCTX *context,
550 const char *filename,
553 struct timespec create_time_ts;
554 struct timespec write_time_ts;
555 struct timespec access_time_ts;
556 struct timespec change_time_ts;
562 ret = TALLOC_P(ctx, DOS_ATTR_DESC);
568 /* Obtain the DOS attributes */
569 if (!SMBC_getatr(context, srv, CONST_DISCARD(char *, filename),
576 errno = SMBC_errno(context, srv->cli);
577 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
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);
593 /* parse a ascii version of a security descriptor */
595 dos_attr_parse(SMBCCTX *context,
603 TALLOC_CTX *frame = NULL;
605 const char * create_time_attr;
606 const char * access_time_attr;
607 const char * write_time_attr;
608 const char * change_time_attr;
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";
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... */
628 /* ... then increment past the first colon if there is one */
629 if ((p = strchr(str, ':')) != NULL) {
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);
641 dad->mode = (request |
642 (IS_DOS_DIR(dad->mode)
643 ? FILE_ATTRIBUTE_DIRECTORY
644 : FILE_ATTRIBUTE_NORMAL));
651 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
652 dad->size = (SMB_OFF_T)atof(tok+5);
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);
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);
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);
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,
678 dad->create_time = (time_t)strtol(tok+n+1,
684 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
685 dad->inode = (SMB_INO_T)atof(tok+6);
692 /*****************************************************
693 Retrieve the acls for a file.
694 *******************************************************/
697 cacl_get(SMBCCTX *context,
700 struct cli_state *ipc_cli,
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;
728 bool determine_size = (bufsize == 0);
732 fstring name_sandbox;
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;
747 struct cli_state *cli = srv->cli;
749 const char * create_time_attr;
750 const char * access_time_attr;
751 const char * write_time_attr;
752 const char * change_time_attr;
755 const char * create_time_attr;
756 const char * access_time_attr;
757 const char * write_time_attr;
758 const char * change_time_attr;
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";
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 */
795 /* If there are any exclusions, point to them and mask them from name */
796 if ((pExclude = strchr(name, '!')) != NULL)
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 '!' */
815 pExclude = (p == NULL ? NULL : p + 1)) {
817 /* Find end of this exclusion name */
818 if ((p = strchr(pExclude, '!')) != NULL)
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 &&
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;
863 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
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);
885 DEBUG(5, ("cacl_get failed to open %s: %s\n",
886 filename, cli_errstr(cli)));
891 sd = cli_query_secdesc(cli, fnum, ctx);
895 ("cacl_get Failed to query old descriptor\n"));
900 cli_close(cli, fnum);
902 if (! exclude_nt_revision) {
904 if (determine_size) {
905 p = talloc_asprintf(ctx,
914 n = snprintf(buf, bufsize,
918 } else if (StrCaseCmp(name, "revision") == 0) {
919 if (determine_size) {
920 p = talloc_asprintf(ctx, "%d",
928 n = snprintf(buf, bufsize, "%d",
933 if (!determine_size && n > bufsize) {
943 if (! exclude_nt_owner) {
944 /* Get owner and group sid */
946 convert_sid_to_string(ipc_cli, pol,
955 if (determine_size) {
956 p = talloc_asprintf(ctx, ",OWNER:%s",
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);
976 n = snprintf(buf, bufsize, "%s",
981 if (!determine_size && n > bufsize) {
991 if (! exclude_nt_group) {
993 convert_sid_to_string(ipc_cli, pol,
1000 if (all || all_nt) {
1001 if (determine_size) {
1002 p = talloc_asprintf(ctx, ",GROUP:%s",
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);
1022 n = snprintf(buf, bufsize,
1027 if (!determine_size && n > bufsize) {
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,
1046 if (all || all_nt) {
1047 if (determine_size) {
1048 p = talloc_asprintf(
1064 ",ACL:%s:%d/%d/0x%08x",
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(
1087 n = snprintf(buf, bufsize,
1093 } else if (all_nt_acls) {
1094 if (determine_size) {
1095 p = talloc_asprintf(
1097 "%s%s:%d/%d/0x%08x",
1109 n = snprintf(buf, bufsize,
1110 "%s%s:%d/%d/0x%08x",
1118 if (!determine_size && n > bufsize) {
1129 /* Restore name pointer to its original value */
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,
1145 errno = SMBC_errno(context, srv->cli);
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,
1171 n = snprintf(buf, bufsize,
1179 } else if (StrCaseCmp(name, "mode") == 0) {
1180 if (determine_size) {
1181 p = talloc_asprintf(ctx, "0x%x", mode);
1188 n = snprintf(buf, bufsize,
1193 if (!determine_size && n > bufsize) {
1203 if (! exclude_dos_size) {
1204 if (all || all_dos) {
1205 if (determine_size) {
1206 p = talloc_asprintf(
1216 n = snprintf(buf, bufsize,
1220 } else if (StrCaseCmp(name, "size") == 0) {
1221 if (determine_size) {
1222 p = talloc_asprintf(
1232 n = snprintf(buf, bufsize,
1238 if (!determine_size && n > bufsize) {
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,
1254 attr_strings.create_time_attr,
1262 n = snprintf(buf, bufsize,
1264 attr_strings.create_time_attr,
1267 } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
1268 if (determine_size) {
1269 p = talloc_asprintf(ctx, "%lu", create_time);
1276 n = snprintf(buf, bufsize,
1277 "%lu", create_time);
1281 if (!determine_size && n > bufsize) {
1291 if (! exclude_dos_access_time) {
1292 if (all || all_dos) {
1293 if (determine_size) {
1294 p = talloc_asprintf(ctx,
1296 attr_strings.access_time_attr,
1304 n = snprintf(buf, bufsize,
1306 attr_strings.access_time_attr,
1309 } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
1310 if (determine_size) {
1311 p = talloc_asprintf(ctx, "%lu", access_time);
1318 n = snprintf(buf, bufsize,
1319 "%lu", access_time);
1323 if (!determine_size && n > bufsize) {
1333 if (! exclude_dos_write_time) {
1334 if (all || all_dos) {
1335 if (determine_size) {
1336 p = talloc_asprintf(ctx,
1338 attr_strings.write_time_attr,
1346 n = snprintf(buf, bufsize,
1348 attr_strings.write_time_attr,
1351 } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
1352 if (determine_size) {
1353 p = talloc_asprintf(ctx, "%lu", write_time);
1360 n = snprintf(buf, bufsize,
1365 if (!determine_size && n > bufsize) {
1375 if (! exclude_dos_change_time) {
1376 if (all || all_dos) {
1377 if (determine_size) {
1378 p = talloc_asprintf(ctx,
1380 attr_strings.change_time_attr,
1388 n = snprintf(buf, bufsize,
1390 attr_strings.change_time_attr,
1393 } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
1394 if (determine_size) {
1395 p = talloc_asprintf(ctx, "%lu", change_time);
1402 n = snprintf(buf, bufsize,
1403 "%lu", change_time);
1407 if (!determine_size && n > bufsize) {
1417 if (! exclude_dos_inode) {
1418 if (all || all_dos) {
1419 if (determine_size) {
1420 p = talloc_asprintf(
1430 n = snprintf(buf, bufsize,
1434 } else if (StrCaseCmp(name, "inode") == 0) {
1435 if (determine_size) {
1436 p = talloc_asprintf(
1446 n = snprintf(buf, bufsize,
1452 if (!determine_size && n > bufsize) {
1462 /* Restore name pointer to its original value */
1474 /*****************************************************
1475 set the ACLs on a file given an ascii description
1476 *******************************************************/
1478 cacl_set(TALLOC_CTX *ctx,
1479 struct cli_state *cli,
1480 struct cli_state *ipc_cli,
1482 const char *filename,
1483 const char *the_acl,
1489 SEC_DESC *sd = NULL, *old;
1490 SEC_ACL *dacl = NULL;
1491 DOM_SID *owner_sid = NULL;
1492 DOM_SID *group_sid = NULL;
1497 bool numeric = True;
1499 /* the_acl will be null for REMOVE_ALL operations */
1501 numeric = ((p = strchr(the_acl, ':')) != NULL &&
1505 /* if this is to set the entire ACL... */
1506 if (*the_acl == '*') {
1507 /* ... then increment past the first colon */
1511 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric,
1512 CONST_DISCARD(char *, the_acl));
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)) {
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);
1534 DEBUG(5, ("cacl_set failed to open %s: %s\n",
1535 filename, cli_errstr(cli)));
1540 old = cli_query_secdesc(cli, fnum, ctx);
1543 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
1548 cli_close(cli, fnum);
1551 case SMBC_XATTR_MODE_REMOVE_ALL:
1552 old->dacl->num_aces = 0;
1556 case SMBC_XATTR_MODE_REMOVE:
1557 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
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])) {
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--;
1583 case SMBC_XATTR_MODE_ADD:
1584 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
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)) {
1595 old->dacl->aces[j] = sd->dacl->aces[i];
1601 if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
1607 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1608 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
1614 case SMBC_XATTR_MODE_SET:
1616 owner_sid = old->owner_sid;
1617 group_sid = old->group_sid;
1621 case SMBC_XATTR_MODE_CHOWN:
1622 owner_sid = sd->owner_sid;
1625 case SMBC_XATTR_MODE_CHGRP:
1626 group_sid = sd->group_sid;
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);
1641 DEBUG(5, ("cacl_set failed to open %s: %s\n",
1642 filename, cli_errstr(cli)));
1647 if (!cli_set_secdesc(cli, fnum, sd)) {
1648 DEBUG(5, ("ERROR: secdesc set failed: %s\n", cli_errstr(cli)));
1655 cli_close(cli, fnum);
1666 SMBC_setxattr_ctx(SMBCCTX *context,
1675 SMBCSRV *srv = NULL;
1676 SMBCSRV *ipc_srv = NULL;
1677 char *server = NULL;
1680 char *password = NULL;
1681 char *workgroup = NULL;
1683 DOS_ATTR_DESC *dad = NULL;
1685 const char * create_time_attr;
1686 const char * access_time_attr;
1687 const char * write_time_attr;
1688 const char * change_time_attr;
1690 TALLOC_CTX *frame = talloc_stackframe();
1692 if (!context || !context->internal->initialized) {
1694 errno = EINVAL; /* Best I can think of ... */
1705 DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
1706 fname, name, (int) size, (const char*)value));
1708 if (SMBC_parse_path(frame,
1723 if (!user || user[0] == (char)0) {
1724 user = talloc_strdup(frame, context->config.user);
1732 srv = SMBC_server(frame, context, True,
1733 server, share, &workgroup, &user, &password);
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);
1743 srv->no_nt_session = True;
1750 * Are they asking to set the entire set of known attributes?
1752 if (StrCaseCmp(name, "system.*") == 0 ||
1753 StrCaseCmp(name, "system.*+") == 0) {
1756 talloc_asprintf(talloc_tos(), "%s:%s",
1757 name+7, (const char *) value);
1766 ret = cacl_set(talloc_tos(), srv->cli,
1767 ipc_srv->cli, &ipc_srv->pol, path,
1770 ? SMBC_XATTR_MODE_SET
1771 : SMBC_XATTR_MODE_ADD),
1777 /* get a DOS Attribute Descriptor with current attributes */
1778 dad = dos_attr_query(context, talloc_tos(), path, srv);
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,
1791 /* cause failure if NT failed too */
1796 /* we only fail if both NT and DOS sets failed */
1797 if (ret < 0 && ! dad) {
1798 ret = -1; /* in case dad was null */
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) {
1820 talloc_asprintf(talloc_tos(), "%s:%s",
1821 name+19, (const char *) value);
1824 ret = -1; /* errno set by SMBC_server() */
1826 else if (! namevalue) {
1830 ret = cacl_set(talloc_tos(), srv->cli,
1831 ipc_srv->cli, &ipc_srv->pol, path,
1834 ? SMBC_XATTR_MODE_SET
1835 : SMBC_XATTR_MODE_ADD),
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) {
1850 talloc_asprintf(talloc_tos(), "%s:%s",
1851 name+19, (const char *) value);
1854 ret = -1; /* errno set by SMBC_server() */
1856 else if (! namevalue) {
1860 ret = cacl_set(talloc_tos(), srv->cli,
1861 ipc_srv->cli, &ipc_srv->pol, path,
1862 namevalue, SMBC_XATTR_MODE_CHOWN, 0);
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) {
1876 talloc_asprintf(talloc_tos(), "%s:%s",
1877 name+19, (const char *) value);
1880 /* errno set by SMBC_server() */
1883 else if (! namevalue) {
1887 ret = cacl_set(talloc_tos(), srv->cli,
1888 ipc_srv->cli, &ipc_srv->pol, path,
1889 namevalue, SMBC_XATTR_MODE_CHGRP, 0);
1895 /* Determine whether to use old-style or new-style attribute names */
1896 if (context->internal->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";
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);
1925 talloc_asprintf(talloc_tos(), "%s:%s",
1926 name+16, (const char *) value);
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,
1942 /* ret2 has True (success) / False (failure) */
1957 /* Unsupported attribute name */
1964 SMBC_getxattr_ctx(SMBCCTX *context,
1971 SMBCSRV *srv = NULL;
1972 SMBCSRV *ipc_srv = NULL;
1973 char *server = NULL;
1976 char *password = NULL;
1977 char *workgroup = NULL;
1980 const char * create_time_attr;
1981 const char * access_time_attr;
1982 const char * write_time_attr;
1983 const char * change_time_attr;
1985 TALLOC_CTX *frame = talloc_stackframe();
1987 if (!context || !context->internal->initialized) {
1989 errno = EINVAL; /* Best I can think of ... */
2000 DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
2002 if (SMBC_parse_path(frame,
2017 if (!user || user[0] == (char)0) {
2018 user = talloc_strdup(frame, context->config.user);
2026 srv = SMBC_server(frame, context, True,
2027 server, share, &workgroup, &user, &password);
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);
2037 srv->no_nt_session = True;
2043 /* Determine whether to use old-style or new-style attribute names */
2044 if (context->internal->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";
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) {
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);
2098 /* Unsupported attribute name */
2106 SMBC_removexattr_ctx(SMBCCTX *context,
2111 SMBCSRV *srv = NULL;
2112 SMBCSRV *ipc_srv = NULL;
2113 char *server = NULL;
2116 char *password = NULL;
2117 char *workgroup = NULL;
2119 TALLOC_CTX *frame = talloc_stackframe();
2121 if (!context || !context->internal->initialized) {
2123 errno = EINVAL; /* Best I can think of ... */
2134 DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
2136 if (SMBC_parse_path(frame,
2151 if (!user || user[0] == (char)0) {
2152 user = talloc_strdup(frame, context->config.user);
2160 srv = SMBC_server(frame, context, True,
2161 server, share, &workgroup, &user, &password);
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);
2171 srv->no_nt_session = True;
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) {
2187 ret = cacl_set(talloc_tos(), srv->cli,
2188 ipc_srv->cli, &ipc_srv->pol, path,
2189 NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
2195 * Are they asking to remove one or more spceific security descriptor
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) {
2207 ret = cacl_set(talloc_tos(), srv->cli,
2208 ipc_srv->cli, &ipc_srv->pol, path,
2209 name + 19, SMBC_XATTR_MODE_REMOVE, 0);
2214 /* Unsupported attribute name */
2221 SMBC_listxattr_ctx(SMBCCTX *context,
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...
2232 const char supported_old[] =
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[] =
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->internal->full_time_names) {
2274 supported = supported_new;
2275 retsize = sizeof(supported_new);
2277 supported = supported_old;
2278 retsize = sizeof(supported_old);
2285 if (retsize > size) {
2290 /* this can't be strcpy() because there are embedded null characters */
2291 memcpy(list, supported, retsize);