Merge branch 'v4-0-stable' into newmaster
[samba.git] / source3 / libsmb / libsmb_xattr.c
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
10
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.
15
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.
20
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/>.
23 */
24
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/dom_sid.h"
31
32
33 /*
34  * Find an lsa pipe handle associated with a cli struct.
35  */
36 static struct rpc_pipe_client *
37 find_lsa_pipe_hnd(struct cli_state *ipc_cli)
38 {
39         struct rpc_pipe_client *pipe_hnd;
40
41         for (pipe_hnd = ipc_cli->pipe_list;
42              pipe_hnd;
43              pipe_hnd = pipe_hnd->next) {
44                 if (ndr_syntax_id_equal(&pipe_hnd->abstract_syntax,
45                                         &ndr_table_lsarpc.syntax_id)) {
46                         return pipe_hnd;
47                 }
48         }
49         return NULL;
50 }
51
52 /*
53  * Sort ACEs according to the documentation at
54  * http://support.microsoft.com/kb/269175, at least as far as it defines the
55  * order.
56  */
57
58 static int
59 ace_compare(struct security_ace *ace1,
60             struct security_ace *ace2)
61 {
62         bool b1;
63         bool b2;
64
65         /* If the ACEs are equal, we have nothing more to do. */
66         if (sec_ace_equal(ace1, ace2)) {
67                 return 0;
68         }
69
70         /* Inherited follow non-inherited */
71         b1 = ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
72         b2 = ((ace2->flags & SEC_ACE_FLAG_INHERITED_ACE) != 0);
73         if (b1 != b2) {
74                 return (b1 ? 1 : -1);
75         }
76
77         /*
78          * What shall we do with AUDITs and ALARMs?  It's undefined.  We'll
79          * sort them after DENY and ALLOW.
80          */
81         b1 = (ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
82               ace1->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
83               ace1->type != SEC_ACE_TYPE_ACCESS_DENIED &&
84               ace1->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
85         b2 = (ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED &&
86               ace2->type != SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT &&
87               ace2->type != SEC_ACE_TYPE_ACCESS_DENIED &&
88               ace2->type != SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
89         if (b1 != b2) {
90                 return (b1 ? 1 : -1);
91         }
92
93         /* Allowed ACEs follow denied ACEs */
94         b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
95               ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
96         b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED ||
97               ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT);
98         if (b1 != b2) {
99                 return (b1 ? 1 : -1);
100         }
101
102         /*
103          * ACEs applying to an entity's object follow those applying to the
104          * entity itself
105          */
106         b1 = (ace1->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
107               ace1->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
108         b2 = (ace2->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
109               ace2->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT);
110         if (b1 != b2) {
111                 return (b1 ? 1 : -1);
112         }
113
114         /*
115          * If we get this far, the ACEs are similar as far as the
116          * characteristics we typically care about (those defined by the
117          * referenced MS document).  We'll now sort by characteristics that
118          * just seems reasonable.
119          */
120
121         if (ace1->type != ace2->type) {
122                 return ace2->type - ace1->type;
123         }
124
125         if (dom_sid_compare(&ace1->trustee, &ace2->trustee)) {
126                 return dom_sid_compare(&ace1->trustee, &ace2->trustee);
127         }
128
129         if (ace1->flags != ace2->flags) {
130                 return ace1->flags - ace2->flags;
131         }
132
133         if (ace1->access_mask != ace2->access_mask) {
134                 return ace1->access_mask - ace2->access_mask;
135         }
136
137         if (ace1->size != ace2->size) {
138                 return ace1->size - ace2->size;
139         }
140
141         return memcmp(ace1, ace2, sizeof(struct security_ace));
142 }
143
144
145 static void
146 sort_acl(struct security_acl *the_acl)
147 {
148         uint32 i;
149         if (!the_acl) return;
150
151         TYPESAFE_QSORT(the_acl->aces, the_acl->num_aces, ace_compare);
152
153         for (i=1;i<the_acl->num_aces;) {
154                 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
155                         int j;
156                         for (j=i; j<the_acl->num_aces-1; j++) {
157                                 the_acl->aces[j] = the_acl->aces[j+1];
158                         }
159                         the_acl->num_aces--;
160                 } else {
161                         i++;
162                 }
163         }
164 }
165
166 /* convert a SID to a string, either numeric or username/group */
167 static void
168 convert_sid_to_string(struct cli_state *ipc_cli,
169                       struct policy_handle *pol,
170                       fstring str,
171                       bool numeric,
172                       struct dom_sid *sid)
173 {
174         char **domains = NULL;
175         char **names = NULL;
176         enum lsa_SidType *types = NULL;
177         struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
178         TALLOC_CTX *ctx;
179
180         sid_to_fstring(str, sid);
181
182         if (numeric) {
183                 return;     /* no lookup desired */
184         }
185
186         if (!pipe_hnd) {
187                 return;
188         }
189
190         /* Ask LSA to convert the sid to a name */
191
192         ctx = talloc_stackframe();
193
194         if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_sids(pipe_hnd, ctx,
195                                                     pol, 1, sid, &domains,
196                                                     &names, &types)) ||
197             !domains || !domains[0] || !names || !names[0]) {
198                 TALLOC_FREE(ctx);
199                 return;
200         }
201
202         /* Converted OK */
203
204         slprintf(str, sizeof(fstring) - 1, "%s%s%s",
205                  domains[0], lp_winbind_separator(),
206                  names[0]);
207
208         TALLOC_FREE(ctx);
209 }
210
211 /* convert a string to a SID, either numeric or username/group */
212 static bool
213 convert_string_to_sid(struct cli_state *ipc_cli,
214                       struct policy_handle *pol,
215                       bool numeric,
216                       struct dom_sid *sid,
217                       const char *str)
218 {
219         enum lsa_SidType *types = NULL;
220         struct dom_sid *sids = NULL;
221         bool result = True;
222         TALLOC_CTX *ctx = NULL;
223         struct rpc_pipe_client *pipe_hnd = find_lsa_pipe_hnd(ipc_cli);
224
225         if (!pipe_hnd) {
226                 return False;
227         }
228
229         if (numeric) {
230                 if (strncmp(str, "S-", 2) == 0) {
231                         return string_to_sid(sid, str);
232                 }
233
234                 result = False;
235                 goto done;
236         }
237
238         ctx = talloc_stackframe();
239         if (!NT_STATUS_IS_OK(rpccli_lsa_lookup_names(pipe_hnd, ctx,
240                                                      pol, 1, &str,
241                                                      NULL, 1, &sids,
242                                                      &types))) {
243                 result = False;
244                 goto done;
245         }
246
247         sid_copy(sid, &sids[0]);
248 done:
249         TALLOC_FREE(ctx);
250         return result;
251 }
252
253
254 /* parse an struct security_ace in the same format as print_ace() */
255 static bool
256 parse_ace(struct cli_state *ipc_cli,
257           struct policy_handle *pol,
258           struct security_ace *ace,
259           bool numeric,
260           char *str)
261 {
262         char *p;
263         const char *cp;
264         char *tok;
265         unsigned int atype;
266         unsigned int aflags;
267         unsigned int amask;
268         struct dom_sid sid;
269         uint32_t mask;
270         const struct perm_value *v;
271         struct perm_value {
272                 const char perm[7];
273                 uint32 mask;
274         };
275         TALLOC_CTX *frame = talloc_stackframe();
276
277         /* These values discovered by inspection */
278         static const struct perm_value special_values[] = {
279                 { "R", 0x00120089 },
280                 { "W", 0x00120116 },
281                 { "X", 0x001200a0 },
282                 { "D", 0x00010000 },
283                 { "P", 0x00040000 },
284                 { "O", 0x00080000 },
285                 { "", 0 },
286         };
287
288         static const struct perm_value standard_values[] = {
289                 { "READ",   0x001200a9 },
290                 { "CHANGE", 0x001301bf },
291                 { "FULL",   0x001f01ff },
292                 { "", 0 },
293         };
294
295         ZERO_STRUCTP(ace);
296         p = strchr_m(str,':');
297         if (!p) {
298                 TALLOC_FREE(frame);
299                 return False;
300         }
301         *p = '\0';
302         p++;
303         /* Try to parse numeric form */
304
305         if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
306             convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
307                 goto done;
308         }
309
310         /* Try to parse text form */
311
312         if (!convert_string_to_sid(ipc_cli, pol, numeric, &sid, str)) {
313                 TALLOC_FREE(frame);
314                 return false;
315         }
316
317         cp = p;
318         if (!next_token_talloc(frame, &cp, &tok, "/")) {
319                 TALLOC_FREE(frame);
320                 return false;
321         }
322
323         if (StrnCaseCmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
324                 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
325         } else if (StrnCaseCmp(tok, "DENIED", strlen("DENIED")) == 0) {
326                 atype = SEC_ACE_TYPE_ACCESS_DENIED;
327         } else {
328                 TALLOC_FREE(frame);
329                 return false;
330         }
331
332         /* Only numeric form accepted for flags at present */
333
334         if (!(next_token_talloc(frame, &cp, &tok, "/") &&
335               sscanf(tok, "%i", &aflags))) {
336                 TALLOC_FREE(frame);
337                 return false;
338         }
339
340         if (!next_token_talloc(frame, &cp, &tok, "/")) {
341                 TALLOC_FREE(frame);
342                 return false;
343         }
344
345         if (strncmp(tok, "0x", 2) == 0) {
346                 if (sscanf(tok, "%i", &amask) != 1) {
347                         TALLOC_FREE(frame);
348                         return false;
349                 }
350                 goto done;
351         }
352
353         for (v = standard_values; v->perm; v++) {
354                 if (strcmp(tok, v->perm) == 0) {
355                         amask = v->mask;
356                         goto done;
357                 }
358         }
359
360         p = tok;
361
362         while(*p) {
363                 bool found = False;
364
365                 for (v = special_values; v->perm; v++) {
366                         if (v->perm[0] == *p) {
367                                 amask |= v->mask;
368                                 found = True;
369                         }
370                 }
371
372                 if (!found) {
373                         TALLOC_FREE(frame);
374                         return false;
375                 }
376                 p++;
377         }
378
379         if (*p) {
380                 TALLOC_FREE(frame);
381                 return false;
382         }
383
384 done:
385         mask = amask;
386         init_sec_ace(ace, &sid, atype, mask, aflags);
387         TALLOC_FREE(frame);
388         return true;
389 }
390
391 /* add an struct security_ace to a list of struct security_aces in a struct security_acl */
392 static bool
393 add_ace(struct security_acl **the_acl,
394         struct security_ace *ace,
395         TALLOC_CTX *ctx)
396 {
397         struct security_acl *newacl;
398         struct security_ace *aces;
399
400         if (! *the_acl) {
401                 (*the_acl) = make_sec_acl(ctx, 3, 1, ace);
402                 return True;
403         }
404
405         if ((aces = SMB_CALLOC_ARRAY(struct security_ace,
406                                      1+(*the_acl)->num_aces)) == NULL) {
407                 return False;
408         }
409         memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(struct security_ace));
410         memcpy(aces+(*the_acl)->num_aces, ace, sizeof(struct security_ace));
411         newacl = make_sec_acl(ctx, (*the_acl)->revision,
412                               1+(*the_acl)->num_aces, aces);
413         SAFE_FREE(aces);
414         (*the_acl) = newacl;
415         return True;
416 }
417
418
419 /* parse a ascii version of a security descriptor */
420 static struct security_descriptor *
421 sec_desc_parse(TALLOC_CTX *ctx,
422                struct cli_state *ipc_cli,
423                struct policy_handle *pol,
424                bool numeric,
425                const char *str)
426 {
427         const char *p = str;
428         char *tok;
429         struct security_descriptor *ret = NULL;
430         size_t sd_size;
431         struct dom_sid *group_sid=NULL;
432         struct dom_sid *owner_sid=NULL;
433         struct security_acl *dacl=NULL;
434         int revision=1;
435
436         while (next_token_talloc(ctx, &p, &tok, "\t,\r\n")) {
437
438                 if (StrnCaseCmp(tok,"REVISION:", 9) == 0) {
439                         revision = strtol(tok+9, NULL, 16);
440                         continue;
441                 }
442
443                 if (StrnCaseCmp(tok,"OWNER:", 6) == 0) {
444                         if (owner_sid) {
445                                 DEBUG(5,("OWNER specified more than once!\n"));
446                                 goto done;
447                         }
448                         owner_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
449                         if (!owner_sid ||
450                             !convert_string_to_sid(ipc_cli, pol,
451                                                    numeric,
452                                                    owner_sid, tok+6)) {
453                                 DEBUG(5, ("Failed to parse owner sid\n"));
454                                 goto done;
455                         }
456                         continue;
457                 }
458
459                 if (StrnCaseCmp(tok,"OWNER+:", 7) == 0) {
460                         if (owner_sid) {
461                                 DEBUG(5,("OWNER specified more than once!\n"));
462                                 goto done;
463                         }
464                         owner_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
465                         if (!owner_sid ||
466                             !convert_string_to_sid(ipc_cli, pol,
467                                                    False,
468                                                    owner_sid, tok+7)) {
469                                 DEBUG(5, ("Failed to parse owner sid\n"));
470                                 goto done;
471                         }
472                         continue;
473                 }
474
475                 if (StrnCaseCmp(tok,"GROUP:", 6) == 0) {
476                         if (group_sid) {
477                                 DEBUG(5,("GROUP specified more than once!\n"));
478                                 goto done;
479                         }
480                         group_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
481                         if (!group_sid ||
482                             !convert_string_to_sid(ipc_cli, pol,
483                                                    numeric,
484                                                    group_sid, tok+6)) {
485                                 DEBUG(5, ("Failed to parse group sid\n"));
486                                 goto done;
487                         }
488                         continue;
489                 }
490
491                 if (StrnCaseCmp(tok,"GROUP+:", 7) == 0) {
492                         if (group_sid) {
493                                 DEBUG(5,("GROUP specified more than once!\n"));
494                                 goto done;
495                         }
496                         group_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
497                         if (!group_sid ||
498                             !convert_string_to_sid(ipc_cli, pol,
499                                                    False,
500                                                    group_sid, tok+6)) {
501                                 DEBUG(5, ("Failed to parse group sid\n"));
502                                 goto done;
503                         }
504                         continue;
505                 }
506
507                 if (StrnCaseCmp(tok,"ACL:", 4) == 0) {
508                         struct security_ace ace;
509                         if (!parse_ace(ipc_cli, pol, &ace, numeric, tok+4)) {
510                                 DEBUG(5, ("Failed to parse ACL %s\n", tok));
511                                 goto done;
512                         }
513                         if(!add_ace(&dacl, &ace, ctx)) {
514                                 DEBUG(5, ("Failed to add ACL %s\n", tok));
515                                 goto done;
516                         }
517                         continue;
518                 }
519
520                 if (StrnCaseCmp(tok,"ACL+:", 5) == 0) {
521                         struct security_ace ace;
522                         if (!parse_ace(ipc_cli, pol, &ace, False, tok+5)) {
523                                 DEBUG(5, ("Failed to parse ACL %s\n", tok));
524                                 goto done;
525                         }
526                         if(!add_ace(&dacl, &ace, ctx)) {
527                                 DEBUG(5, ("Failed to add ACL %s\n", tok));
528                                 goto done;
529                         }
530                         continue;
531                 }
532
533                 DEBUG(5, ("Failed to parse security descriptor\n"));
534                 goto done;
535         }
536
537         ret = make_sec_desc(ctx, revision, SEC_DESC_SELF_RELATIVE, 
538                             owner_sid, group_sid, NULL, dacl, &sd_size);
539
540 done:
541         SAFE_FREE(group_sid);
542         SAFE_FREE(owner_sid);
543         return ret;
544 }
545
546
547 /* Obtain the current dos attributes */
548 static DOS_ATTR_DESC *
549 dos_attr_query(SMBCCTX *context,
550                TALLOC_CTX *ctx,
551                const char *filename,
552                SMBCSRV *srv)
553 {
554         struct timespec create_time_ts;
555         struct timespec write_time_ts;
556         struct timespec access_time_ts;
557         struct timespec change_time_ts;
558         SMB_OFF_T size = 0;
559         uint16 mode = 0;
560         SMB_INO_T inode = 0;
561         DOS_ATTR_DESC *ret;
562
563         ret = TALLOC_P(ctx, DOS_ATTR_DESC);
564         if (!ret) {
565                 errno = ENOMEM;
566                 return NULL;
567         }
568
569         /* Obtain the DOS attributes */
570         if (!SMBC_getatr(context, srv, CONST_DISCARD(char *, filename),
571                          &mode, &size,
572                          &create_time_ts,
573                          &access_time_ts,
574                          &write_time_ts,
575                          &change_time_ts,
576                          &inode)) {
577                 errno = SMBC_errno(context, srv->cli);
578                 DEBUG(5, ("dos_attr_query Failed to query old attributes\n"));
579                 return NULL;
580         }
581
582         ret->mode = mode;
583         ret->size = size;
584         ret->create_time = convert_timespec_to_time_t(create_time_ts);
585         ret->access_time = convert_timespec_to_time_t(access_time_ts);
586         ret->write_time = convert_timespec_to_time_t(write_time_ts);
587         ret->change_time = convert_timespec_to_time_t(change_time_ts);
588         ret->inode = inode;
589
590         return ret;
591 }
592
593
594 /* parse a ascii version of a security descriptor */
595 static void
596 dos_attr_parse(SMBCCTX *context,
597                DOS_ATTR_DESC *dad,
598                SMBCSRV *srv,
599                char *str)
600 {
601         int n;
602         const char *p = str;
603         char *tok = NULL;
604         TALLOC_CTX *frame = NULL;
605         struct {
606                 const char * create_time_attr;
607                 const char * access_time_attr;
608                 const char * write_time_attr;
609                 const char * change_time_attr;
610         } attr_strings;
611
612         /* Determine whether to use old-style or new-style attribute names */
613         if (context->internal->full_time_names) {
614                 /* new-style names */
615                 attr_strings.create_time_attr = "CREATE_TIME";
616                 attr_strings.access_time_attr = "ACCESS_TIME";
617                 attr_strings.write_time_attr = "WRITE_TIME";
618                 attr_strings.change_time_attr = "CHANGE_TIME";
619         } else {
620                 /* old-style names */
621                 attr_strings.create_time_attr = NULL;
622                 attr_strings.access_time_attr = "A_TIME";
623                 attr_strings.write_time_attr = "M_TIME";
624                 attr_strings.change_time_attr = "C_TIME";
625         }
626
627         /* if this is to set the entire ACL... */
628         if (*str == '*') {
629                 /* ... then increment past the first colon if there is one */
630                 if ((p = strchr(str, ':')) != NULL) {
631                         ++p;
632                 } else {
633                         p = str;
634                 }
635         }
636
637         frame = talloc_stackframe();
638         while (next_token_talloc(frame, &p, &tok, "\t,\r\n")) {
639                 if (StrnCaseCmp(tok, "MODE:", 5) == 0) {
640                         long request = strtol(tok+5, NULL, 16);
641                         if (request == 0) {
642                                 dad->mode = (request |
643                                              (IS_DOS_DIR(dad->mode)
644                                               ? FILE_ATTRIBUTE_DIRECTORY
645                                               : FILE_ATTRIBUTE_NORMAL));
646                         } else {
647                                 dad->mode = request;
648                         }
649                         continue;
650                 }
651
652                 if (StrnCaseCmp(tok, "SIZE:", 5) == 0) {
653                         dad->size = (SMB_OFF_T)atof(tok+5);
654                         continue;
655                 }
656
657                 n = strlen(attr_strings.access_time_attr);
658                 if (StrnCaseCmp(tok, attr_strings.access_time_attr, n) == 0) {
659                         dad->access_time = (time_t)strtol(tok+n+1, NULL, 10);
660                         continue;
661                 }
662
663                 n = strlen(attr_strings.change_time_attr);
664                 if (StrnCaseCmp(tok, attr_strings.change_time_attr, n) == 0) {
665                         dad->change_time = (time_t)strtol(tok+n+1, NULL, 10);
666                         continue;
667                 }
668
669                 n = strlen(attr_strings.write_time_attr);
670                 if (StrnCaseCmp(tok, attr_strings.write_time_attr, n) == 0) {
671                         dad->write_time = (time_t)strtol(tok+n+1, NULL, 10);
672                         continue;
673                 }
674
675                 if (attr_strings.create_time_attr != NULL) {
676                         n = strlen(attr_strings.create_time_attr);
677                         if (StrnCaseCmp(tok, attr_strings.create_time_attr,
678                                         n) == 0) {
679                                 dad->create_time = (time_t)strtol(tok+n+1,
680                                                                   NULL, 10);
681                                 continue;
682                         }
683                 }
684
685                 if (StrnCaseCmp(tok, "INODE:", 6) == 0) {
686                         dad->inode = (SMB_INO_T)atof(tok+6);
687                         continue;
688                 }
689         }
690         TALLOC_FREE(frame);
691 }
692
693 /*****************************************************
694  Retrieve the acls for a file.
695 *******************************************************/
696
697 static int
698 cacl_get(SMBCCTX *context,
699          TALLOC_CTX *ctx,
700          SMBCSRV *srv,
701          struct cli_state *ipc_cli,
702          struct policy_handle *pol,
703          char *filename,
704          char *attr_name,
705          char *buf,
706          int bufsize)
707 {
708         uint32 i;
709         int n = 0;
710         int n_used;
711         bool all;
712         bool all_nt;
713         bool all_nt_acls;
714         bool all_dos;
715         bool some_nt;
716         bool some_dos;
717         bool exclude_nt_revision = False;
718         bool exclude_nt_owner = False;
719         bool exclude_nt_group = False;
720         bool exclude_nt_acl = False;
721         bool exclude_dos_mode = False;
722         bool exclude_dos_size = False;
723         bool exclude_dos_create_time = False;
724         bool exclude_dos_access_time = False;
725         bool exclude_dos_write_time = False;
726         bool exclude_dos_change_time = False;
727         bool exclude_dos_inode = False;
728         bool numeric = True;
729         bool determine_size = (bufsize == 0);
730         uint16_t fnum;
731         struct security_descriptor *sd;
732         fstring sidstr;
733         fstring name_sandbox;
734         char *name;
735         char *pExclude;
736         char *p;
737         struct timespec create_time_ts;
738         struct timespec write_time_ts;
739         struct timespec access_time_ts;
740         struct timespec change_time_ts;
741         time_t create_time = (time_t)0;
742         time_t write_time = (time_t)0;
743         time_t access_time = (time_t)0;
744         time_t change_time = (time_t)0;
745         SMB_OFF_T size = 0;
746         uint16 mode = 0;
747         SMB_INO_T ino = 0;
748         struct cli_state *cli = srv->cli;
749         struct {
750                 const char * create_time_attr;
751                 const char * access_time_attr;
752                 const char * write_time_attr;
753                 const char * change_time_attr;
754         } attr_strings;
755         struct {
756                 const char * create_time_attr;
757                 const char * access_time_attr;
758                 const char * write_time_attr;
759                 const char * change_time_attr;
760         } excl_attr_strings;
761
762         /* Determine whether to use old-style or new-style attribute names */
763         if (context->internal->full_time_names) {
764                 /* new-style names */
765                 attr_strings.create_time_attr = "CREATE_TIME";
766                 attr_strings.access_time_attr = "ACCESS_TIME";
767                 attr_strings.write_time_attr = "WRITE_TIME";
768                 attr_strings.change_time_attr = "CHANGE_TIME";
769
770                 excl_attr_strings.create_time_attr = "CREATE_TIME";
771                 excl_attr_strings.access_time_attr = "ACCESS_TIME";
772                 excl_attr_strings.write_time_attr = "WRITE_TIME";
773                 excl_attr_strings.change_time_attr = "CHANGE_TIME";
774         } else {
775                 /* old-style names */
776                 attr_strings.create_time_attr = NULL;
777                 attr_strings.access_time_attr = "A_TIME";
778                 attr_strings.write_time_attr = "M_TIME";
779                 attr_strings.change_time_attr = "C_TIME";
780
781                 excl_attr_strings.create_time_attr = NULL;
782                 excl_attr_strings.access_time_attr = "dos_attr.A_TIME";
783                 excl_attr_strings.write_time_attr = "dos_attr.M_TIME";
784                 excl_attr_strings.change_time_attr = "dos_attr.C_TIME";
785         }
786
787         /* Copy name so we can strip off exclusions (if any are specified) */
788         strncpy(name_sandbox, attr_name, sizeof(name_sandbox) - 1);
789
790         /* Ensure name is null terminated */
791         name_sandbox[sizeof(name_sandbox) - 1] = '\0';
792
793         /* Play in the sandbox */
794         name = name_sandbox;
795
796         /* If there are any exclusions, point to them and mask them from name */
797         if ((pExclude = strchr(name, '!')) != NULL)
798         {
799                 *pExclude++ = '\0';
800         }
801
802         all = (StrnCaseCmp(name, "system.*", 8) == 0);
803         all_nt = (StrnCaseCmp(name, "system.nt_sec_desc.*", 20) == 0);
804         all_nt_acls = (StrnCaseCmp(name, "system.nt_sec_desc.acl.*", 24) == 0);
805         all_dos = (StrnCaseCmp(name, "system.dos_attr.*", 17) == 0);
806         some_nt = (StrnCaseCmp(name, "system.nt_sec_desc.", 19) == 0);
807         some_dos = (StrnCaseCmp(name, "system.dos_attr.", 16) == 0);
808         numeric = (* (name + strlen(name) - 1) != '+');
809
810         /* Look for exclusions from "all" requests */
811         if (all || all_nt || all_dos) {
812                 /* Exclusions are delimited by '!' */
813                 for (;
814                      pExclude != NULL;
815                      pExclude = (p == NULL ? NULL : p + 1)) {
816
817                         /* Find end of this exclusion name */
818                         if ((p = strchr(pExclude, '!')) != NULL)
819                         {
820                                 *p = '\0';
821                         }
822
823                         /* Which exclusion name is this? */
824                         if (StrCaseCmp(pExclude,
825                                        "nt_sec_desc.revision") == 0) {
826                                 exclude_nt_revision = True;
827                         }
828                         else if (StrCaseCmp(pExclude,
829                                             "nt_sec_desc.owner") == 0) {
830                                 exclude_nt_owner = True;
831                         }
832                         else if (StrCaseCmp(pExclude,
833                                             "nt_sec_desc.group") == 0) {
834                                 exclude_nt_group = True;
835                         }
836                         else if (StrCaseCmp(pExclude,
837                                             "nt_sec_desc.acl") == 0) {
838                                 exclude_nt_acl = True;
839                         }
840                         else if (StrCaseCmp(pExclude,
841                                             "dos_attr.mode") == 0) {
842                                 exclude_dos_mode = True;
843                         }
844                         else if (StrCaseCmp(pExclude,
845                                             "dos_attr.size") == 0) {
846                                 exclude_dos_size = True;
847                         }
848                         else if (excl_attr_strings.create_time_attr != NULL &&
849                                  StrCaseCmp(pExclude,
850                                             excl_attr_strings.change_time_attr) == 0) {
851                                 exclude_dos_create_time = True;
852                         }
853                         else if (StrCaseCmp(pExclude,
854                                             excl_attr_strings.access_time_attr) == 0) {
855                                 exclude_dos_access_time = True;
856                         }
857                         else if (StrCaseCmp(pExclude,
858                                             excl_attr_strings.write_time_attr) == 0) {
859                                 exclude_dos_write_time = True;
860                         }
861                         else if (StrCaseCmp(pExclude,
862                                             excl_attr_strings.change_time_attr) == 0) {
863                                 exclude_dos_change_time = True;
864                         }
865                         else if (StrCaseCmp(pExclude, "dos_attr.inode") == 0) {
866                                 exclude_dos_inode = True;
867                         }
868                         else {
869                                 DEBUG(5, ("cacl_get received unknown exclusion: %s\n",
870                                           pExclude));
871                                 errno = ENOATTR;
872                                 return -1;
873                         }
874                 }
875         }
876
877         n_used = 0;
878
879         /*
880          * If we are (possibly) talking to an NT or new system and some NT
881          * attributes have been requested...
882          */
883         if (ipc_cli && (all || some_nt || all_nt_acls)) {
884                 char *targetpath = NULL;
885                 struct cli_state *targetcli = NULL;
886
887                 /* Point to the portion after "system.nt_sec_desc." */
888                 name += 19;     /* if (all) this will be invalid but unused */
889
890                 if (!cli_resolve_path(ctx, "", context->internal->auth_info,
891                                 cli, filename,
892                                 &targetcli, &targetpath)) {
893                         DEBUG(5, ("cacl_get Could not resolve %s\n",
894                                 filename));
895                         errno = ENOENT;
896                         return -1;
897                 }
898
899                 /* ... then obtain any NT attributes which were requested */
900                 if (!NT_STATUS_IS_OK(cli_ntcreate(targetcli, targetpath, 0, CREATE_ACCESS_READ, 0,
901                                 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
902                         DEBUG(5, ("cacl_get failed to open %s: %s\n",
903                                 targetpath, cli_errstr(targetcli)));
904                         errno = 0;
905                         return -1;
906                 }
907
908                 sd = cli_query_secdesc(targetcli, fnum, ctx);
909
910                 if (!sd) {
911                         DEBUG(5,
912                               ("cacl_get Failed to query old descriptor\n"));
913                         errno = 0;
914                         return -1;
915                 }
916
917                 cli_close(targetcli, fnum);
918
919                 if (! exclude_nt_revision) {
920                         if (all || all_nt) {
921                                 if (determine_size) {
922                                         p = talloc_asprintf(ctx,
923                                                             "REVISION:%d",
924                                                             sd->revision);
925                                         if (!p) {
926                                                 errno = ENOMEM;
927                                                 return -1;
928                                         }
929                                         n = strlen(p);
930                                 } else {
931                                         n = snprintf(buf, bufsize,
932                                                      "REVISION:%d",
933                                                      sd->revision);
934                                 }
935                         } else if (StrCaseCmp(name, "revision") == 0) {
936                                 if (determine_size) {
937                                         p = talloc_asprintf(ctx, "%d",
938                                                             sd->revision);
939                                         if (!p) {
940                                                 errno = ENOMEM;
941                                                 return -1;
942                                         }
943                                         n = strlen(p);
944                                 } else {
945                                         n = snprintf(buf, bufsize, "%d",
946                                                      sd->revision);
947                                 }
948                         }
949
950                         if (!determine_size && n > bufsize) {
951                                 errno = ERANGE;
952                                 return -1;
953                         }
954                         buf += n;
955                         n_used += n;
956                         bufsize -= n;
957                         n = 0;
958                 }
959
960                 if (! exclude_nt_owner) {
961                         /* Get owner and group sid */
962                         if (sd->owner_sid) {
963                                 convert_sid_to_string(ipc_cli, pol,
964                                                       sidstr,
965                                                       numeric,
966                                                       sd->owner_sid);
967                         } else {
968                                 fstrcpy(sidstr, "");
969                         }
970
971                         if (all || all_nt) {
972                                 if (determine_size) {
973                                         p = talloc_asprintf(ctx, ",OWNER:%s",
974                                                             sidstr);
975                                         if (!p) {
976                                                 errno = ENOMEM;
977                                                 return -1;
978                                         }
979                                         n = strlen(p);
980                                 } else if (sidstr[0] != '\0') {
981                                         n = snprintf(buf, bufsize,
982                                                      ",OWNER:%s", sidstr);
983                                 }
984                         } else if (StrnCaseCmp(name, "owner", 5) == 0) {
985                                 if (determine_size) {
986                                         p = talloc_asprintf(ctx, "%s", sidstr);
987                                         if (!p) {
988                                                 errno = ENOMEM;
989                                                 return -1;
990                                         }
991                                         n = strlen(p);
992                                 } else {
993                                         n = snprintf(buf, bufsize, "%s",
994                                                      sidstr);
995                                 }
996                         }
997
998                         if (!determine_size && n > bufsize) {
999                                 errno = ERANGE;
1000                                 return -1;
1001                         }
1002                         buf += n;
1003                         n_used += n;
1004                         bufsize -= n;
1005                         n = 0;
1006                 }
1007
1008                 if (! exclude_nt_group) {
1009                         if (sd->group_sid) {
1010                                 convert_sid_to_string(ipc_cli, pol,
1011                                                       sidstr, numeric,
1012                                                       sd->group_sid);
1013                         } else {
1014                                 fstrcpy(sidstr, "");
1015                         }
1016
1017                         if (all || all_nt) {
1018                                 if (determine_size) {
1019                                         p = talloc_asprintf(ctx, ",GROUP:%s",
1020                                                             sidstr);
1021                                         if (!p) {
1022                                                 errno = ENOMEM;
1023                                                 return -1;
1024                                         }
1025                                         n = strlen(p);
1026                                 } else if (sidstr[0] != '\0') {
1027                                         n = snprintf(buf, bufsize,
1028                                                      ",GROUP:%s", sidstr);
1029                                 }
1030                         } else if (StrnCaseCmp(name, "group", 5) == 0) {
1031                                 if (determine_size) {
1032                                         p = talloc_asprintf(ctx, "%s", sidstr);
1033                                         if (!p) {
1034                                                 errno = ENOMEM;
1035                                                 return -1;
1036                                         }
1037                                         n = strlen(p);
1038                                 } else {
1039                                         n = snprintf(buf, bufsize,
1040                                                      "%s", sidstr);
1041                                 }
1042                         }
1043
1044                         if (!determine_size && n > bufsize) {
1045                                 errno = ERANGE;
1046                                 return -1;
1047                         }
1048                         buf += n;
1049                         n_used += n;
1050                         bufsize -= n;
1051                         n = 0;
1052                 }
1053
1054                 if (! exclude_nt_acl) {
1055                         /* Add aces to value buffer  */
1056                         for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
1057
1058                                 struct security_ace *ace = &sd->dacl->aces[i];
1059                                 convert_sid_to_string(ipc_cli, pol,
1060                                                       sidstr, numeric,
1061                                                       &ace->trustee);
1062
1063                                 if (all || all_nt) {
1064                                         if (determine_size) {
1065                                                 p = talloc_asprintf(
1066                                                         ctx, 
1067                                                         ",ACL:"
1068                                                         "%s:%d/%d/0x%08x", 
1069                                                         sidstr,
1070                                                         ace->type,
1071                                                         ace->flags,
1072                                                         ace->access_mask);
1073                                                 if (!p) {
1074                                                         errno = ENOMEM;
1075                                                         return -1;
1076                                                 }
1077                                                 n = strlen(p);
1078                                         } else {
1079                                                 n = snprintf(
1080                                                         buf, bufsize,
1081                                                         ",ACL:%s:%d/%d/0x%08x", 
1082                                                         sidstr,
1083                                                         ace->type,
1084                                                         ace->flags,
1085                                                         ace->access_mask);
1086                                         }
1087                                 } else if ((StrnCaseCmp(name, "acl", 3) == 0 &&
1088                                             StrCaseCmp(name+3, sidstr) == 0) ||
1089                                            (StrnCaseCmp(name, "acl+", 4) == 0 &&
1090                                             StrCaseCmp(name+4, sidstr) == 0)) {
1091                                         if (determine_size) {
1092                                                 p = talloc_asprintf(
1093                                                         ctx, 
1094                                                         "%d/%d/0x%08x", 
1095                                                         ace->type,
1096                                                         ace->flags,
1097                                                         ace->access_mask);
1098                                                 if (!p) {
1099                                                         errno = ENOMEM;
1100                                                         return -1;
1101                                                 }
1102                                                 n = strlen(p);
1103                                         } else {
1104                                                 n = snprintf(buf, bufsize,
1105                                                              "%d/%d/0x%08x", 
1106                                                              ace->type,
1107                                                              ace->flags,
1108                                                              ace->access_mask);
1109                                         }
1110                                 } else if (all_nt_acls) {
1111                                         if (determine_size) {
1112                                                 p = talloc_asprintf(
1113                                                         ctx, 
1114                                                         "%s%s:%d/%d/0x%08x",
1115                                                         i ? "," : "",
1116                                                         sidstr,
1117                                                         ace->type,
1118                                                         ace->flags,
1119                                                         ace->access_mask);
1120                                                 if (!p) {
1121                                                         errno = ENOMEM;
1122                                                         return -1;
1123                                                 }
1124                                                 n = strlen(p);
1125                                         } else {
1126                                                 n = snprintf(buf, bufsize,
1127                                                              "%s%s:%d/%d/0x%08x",
1128                                                              i ? "," : "",
1129                                                              sidstr,
1130                                                              ace->type,
1131                                                              ace->flags,
1132                                                              ace->access_mask);
1133                                         }
1134                                 }
1135                                 if (!determine_size && n > bufsize) {
1136                                         errno = ERANGE;
1137                                         return -1;
1138                                 }
1139                                 buf += n;
1140                                 n_used += n;
1141                                 bufsize -= n;
1142                                 n = 0;
1143                         }
1144                 }
1145
1146                 /* Restore name pointer to its original value */
1147                 name -= 19;
1148         }
1149
1150         if (all || some_dos) {
1151                 /* Point to the portion after "system.dos_attr." */
1152                 name += 16;     /* if (all) this will be invalid but unused */
1153
1154                 /* Obtain the DOS attributes */
1155                 if (!SMBC_getatr(context, srv, filename, &mode, &size, 
1156                                  &create_time_ts,
1157                                  &access_time_ts,
1158                                  &write_time_ts,
1159                                  &change_time_ts,
1160                                  &ino)) {
1161
1162                         errno = SMBC_errno(context, srv->cli);
1163                         return -1;
1164                 }
1165
1166                 create_time = convert_timespec_to_time_t(create_time_ts);
1167                 access_time = convert_timespec_to_time_t(access_time_ts);
1168                 write_time = convert_timespec_to_time_t(write_time_ts);
1169                 change_time = convert_timespec_to_time_t(change_time_ts);
1170
1171                 if (! exclude_dos_mode) {
1172                         if (all || all_dos) {
1173                                 if (determine_size) {
1174                                         p = talloc_asprintf(ctx,
1175                                                             "%sMODE:0x%x",
1176                                                             (ipc_cli &&
1177                                                              (all || some_nt)
1178                                                              ? ","
1179                                                              : ""),
1180                                                             mode);
1181                                         if (!p) {
1182                                                 errno = ENOMEM;
1183                                                 return -1;
1184                                         }
1185                                         n = strlen(p);
1186                                 } else {
1187                                         n = snprintf(buf, bufsize,
1188                                                      "%sMODE:0x%x",
1189                                                      (ipc_cli &&
1190                                                       (all || some_nt)
1191                                                       ? ","
1192                                                       : ""),
1193                                                      mode);
1194                                 }
1195                         } else if (StrCaseCmp(name, "mode") == 0) {
1196                                 if (determine_size) {
1197                                         p = talloc_asprintf(ctx, "0x%x", mode);
1198                                         if (!p) {
1199                                                 errno = ENOMEM;
1200                                                 return -1;
1201                                         }
1202                                         n = strlen(p);
1203                                 } else {
1204                                         n = snprintf(buf, bufsize,
1205                                                      "0x%x", mode);
1206                                 }
1207                         }
1208
1209                         if (!determine_size && n > bufsize) {
1210                                 errno = ERANGE;
1211                                 return -1;
1212                         }
1213                         buf += n;
1214                         n_used += n;
1215                         bufsize -= n;
1216                         n = 0;
1217                 }
1218
1219                 if (! exclude_dos_size) {
1220                         if (all || all_dos) {
1221                                 if (determine_size) {
1222                                         p = talloc_asprintf(
1223                                                 ctx,
1224                                                 ",SIZE:%.0f",
1225                                                 (double)size);
1226                                         if (!p) {
1227                                                 errno = ENOMEM;
1228                                                 return -1;
1229                                         }
1230                                         n = strlen(p);
1231                                 } else {
1232                                         n = snprintf(buf, bufsize,
1233                                                      ",SIZE:%.0f",
1234                                                      (double)size);
1235                                 }
1236                         } else if (StrCaseCmp(name, "size") == 0) {
1237                                 if (determine_size) {
1238                                         p = talloc_asprintf(
1239                                                 ctx,
1240                                                 "%.0f",
1241                                                 (double)size);
1242                                         if (!p) {
1243                                                 errno = ENOMEM;
1244                                                 return -1;
1245                                         }
1246                                         n = strlen(p);
1247                                 } else {
1248                                         n = snprintf(buf, bufsize,
1249                                                      "%.0f",
1250                                                      (double)size);
1251                                 }
1252                         }
1253
1254                         if (!determine_size && n > bufsize) {
1255                                 errno = ERANGE;
1256                                 return -1;
1257                         }
1258                         buf += n;
1259                         n_used += n;
1260                         bufsize -= n;
1261                         n = 0;
1262                 }
1263
1264                 if (! exclude_dos_create_time &&
1265                     attr_strings.create_time_attr != NULL) {
1266                         if (all || all_dos) {
1267                                 if (determine_size) {
1268                                         p = talloc_asprintf(ctx,
1269                                                             ",%s:%lu",
1270                                                             attr_strings.create_time_attr,
1271                                                             (unsigned long) create_time);
1272                                         if (!p) {
1273                                                 errno = ENOMEM;
1274                                                 return -1;
1275                                         }
1276                                         n = strlen(p);
1277                                 } else {
1278                                         n = snprintf(buf, bufsize,
1279                                                      ",%s:%lu",
1280                                                      attr_strings.create_time_attr,
1281                                                      (unsigned long) create_time);
1282                                 }
1283                         } else if (StrCaseCmp(name, attr_strings.create_time_attr) == 0) {
1284                                 if (determine_size) {
1285                                         p = talloc_asprintf(ctx, "%lu", (unsigned long) create_time);
1286                                         if (!p) {
1287                                                 errno = ENOMEM;
1288                                                 return -1;
1289                                         }
1290                                         n = strlen(p);
1291                                 } else {
1292                                         n = snprintf(buf, bufsize,
1293                                                      "%lu", (unsigned long) create_time);
1294                                 }
1295                         }
1296
1297                         if (!determine_size && n > bufsize) {
1298                                 errno = ERANGE;
1299                                 return -1;
1300                         }
1301                         buf += n;
1302                         n_used += n;
1303                         bufsize -= n;
1304                         n = 0;
1305                 }
1306
1307                 if (! exclude_dos_access_time) {
1308                         if (all || all_dos) {
1309                                 if (determine_size) {
1310                                         p = talloc_asprintf(ctx,
1311                                                             ",%s:%lu",
1312                                                             attr_strings.access_time_attr,
1313                                                             (unsigned long) access_time);
1314                                         if (!p) {
1315                                                 errno = ENOMEM;
1316                                                 return -1;
1317                                         }
1318                                         n = strlen(p);
1319                                 } else {
1320                                         n = snprintf(buf, bufsize,
1321                                                      ",%s:%lu",
1322                                                      attr_strings.access_time_attr,
1323                                                      (unsigned long) access_time);
1324                                 }
1325                         } else if (StrCaseCmp(name, attr_strings.access_time_attr) == 0) {
1326                                 if (determine_size) {
1327                                         p = talloc_asprintf(ctx, "%lu", (unsigned long) access_time);
1328                                         if (!p) {
1329                                                 errno = ENOMEM;
1330                                                 return -1;
1331                                         }
1332                                         n = strlen(p);
1333                                 } else {
1334                                         n = snprintf(buf, bufsize,
1335                                                      "%lu", (unsigned long) access_time);
1336                                 }
1337                         }
1338
1339                         if (!determine_size && n > bufsize) {
1340                                 errno = ERANGE;
1341                                 return -1;
1342                         }
1343                         buf += n;
1344                         n_used += n;
1345                         bufsize -= n;
1346                         n = 0;
1347                 }
1348
1349                 if (! exclude_dos_write_time) {
1350                         if (all || all_dos) {
1351                                 if (determine_size) {
1352                                         p = talloc_asprintf(ctx,
1353                                                             ",%s:%lu",
1354                                                             attr_strings.write_time_attr,
1355                                                             (unsigned long) write_time);
1356                                         if (!p) {
1357                                                 errno = ENOMEM;
1358                                                 return -1;
1359                                         }
1360                                         n = strlen(p);
1361                                 } else {
1362                                         n = snprintf(buf, bufsize,
1363                                                      ",%s:%lu",
1364                                                      attr_strings.write_time_attr,
1365                                                      (unsigned long) write_time);
1366                                 }
1367                         } else if (StrCaseCmp(name, attr_strings.write_time_attr) == 0) {
1368                                 if (determine_size) {
1369                                         p = talloc_asprintf(ctx, "%lu", (unsigned long) write_time);
1370                                         if (!p) {
1371                                                 errno = ENOMEM;
1372                                                 return -1;
1373                                         }
1374                                         n = strlen(p);
1375                                 } else {
1376                                         n = snprintf(buf, bufsize,
1377                                                      "%lu", (unsigned long) write_time);
1378                                 }
1379                         }
1380
1381                         if (!determine_size && n > bufsize) {
1382                                 errno = ERANGE;
1383                                 return -1;
1384                         }
1385                         buf += n;
1386                         n_used += n;
1387                         bufsize -= n;
1388                         n = 0;
1389                 }
1390
1391                 if (! exclude_dos_change_time) {
1392                         if (all || all_dos) {
1393                                 if (determine_size) {
1394                                         p = talloc_asprintf(ctx,
1395                                                             ",%s:%lu",
1396                                                             attr_strings.change_time_attr,
1397                                                             (unsigned long) change_time);
1398                                         if (!p) {
1399                                                 errno = ENOMEM;
1400                                                 return -1;
1401                                         }
1402                                         n = strlen(p);
1403                                 } else {
1404                                         n = snprintf(buf, bufsize,
1405                                                      ",%s:%lu",
1406                                                      attr_strings.change_time_attr,
1407                                                      (unsigned long) change_time);
1408                                 }
1409                         } else if (StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
1410                                 if (determine_size) {
1411                                         p = talloc_asprintf(ctx, "%lu", (unsigned long) change_time);
1412                                         if (!p) {
1413                                                 errno = ENOMEM;
1414                                                 return -1;
1415                                         }
1416                                         n = strlen(p);
1417                                 } else {
1418                                         n = snprintf(buf, bufsize,
1419                                                      "%lu", (unsigned long) change_time);
1420                                 }
1421                         }
1422
1423                         if (!determine_size && n > bufsize) {
1424                                 errno = ERANGE;
1425                                 return -1;
1426                         }
1427                         buf += n;
1428                         n_used += n;
1429                         bufsize -= n;
1430                         n = 0;
1431                 }
1432
1433                 if (! exclude_dos_inode) {
1434                         if (all || all_dos) {
1435                                 if (determine_size) {
1436                                         p = talloc_asprintf(
1437                                                 ctx,
1438                                                 ",INODE:%.0f",
1439                                                 (double)ino);
1440                                         if (!p) {
1441                                                 errno = ENOMEM;
1442                                                 return -1;
1443                                         }
1444                                         n = strlen(p);
1445                                 } else {
1446                                         n = snprintf(buf, bufsize,
1447                                                      ",INODE:%.0f",
1448                                                      (double) ino);
1449                                 }
1450                         } else if (StrCaseCmp(name, "inode") == 0) {
1451                                 if (determine_size) {
1452                                         p = talloc_asprintf(
1453                                                 ctx,
1454                                                 "%.0f",
1455                                                 (double) ino);
1456                                         if (!p) {
1457                                                 errno = ENOMEM;
1458                                                 return -1;
1459                                         }
1460                                         n = strlen(p);
1461                                 } else {
1462                                         n = snprintf(buf, bufsize,
1463                                                      "%.0f",
1464                                                      (double) ino);
1465                                 }
1466                         }
1467
1468                         if (!determine_size && n > bufsize) {
1469                                 errno = ERANGE;
1470                                 return -1;
1471                         }
1472                         buf += n;
1473                         n_used += n;
1474                         bufsize -= n;
1475                         n = 0;
1476                 }
1477
1478                 /* Restore name pointer to its original value */
1479                 name -= 16;
1480         }
1481
1482         if (n_used == 0) {
1483                 errno = ENOATTR;
1484                 return -1;
1485         }
1486
1487         return n_used;
1488 }
1489
1490 /*****************************************************
1491 set the ACLs on a file given an ascii description
1492 *******************************************************/
1493 static int
1494 cacl_set(SMBCCTX *context,
1495         TALLOC_CTX *ctx,
1496         struct cli_state *cli,
1497         struct cli_state *ipc_cli,
1498         struct policy_handle *pol,
1499         const char *filename,
1500         char *the_acl,
1501         int mode,
1502         int flags)
1503 {
1504         uint16_t fnum = (uint16_t)-1;
1505         int err = 0;
1506         struct security_descriptor *sd = NULL, *old;
1507         struct security_acl *dacl = NULL;
1508         struct dom_sid *owner_sid = NULL;
1509         struct dom_sid *group_sid = NULL;
1510         uint32 i, j;
1511         size_t sd_size;
1512         int ret = 0;
1513         char *p;
1514         bool numeric = True;
1515         char *targetpath = NULL;
1516         struct cli_state *targetcli = NULL;
1517
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] != '+');
1523
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;
1528                 }
1529
1530                 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric, the_acl);
1531                 if (!sd) {
1532                         errno = EINVAL;
1533                         return -1;
1534                 }
1535         }
1536
1537         /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
1538            that doesn't deref sd */
1539
1540         if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
1541                 errno = EINVAL;
1542                 return -1;
1543         }
1544
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;
1551         }
1552
1553         /* The desired access below is the only one I could find that works
1554            with NT4, W2KP and Samba */
1555
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;
1562         }
1563
1564         old = cli_query_secdesc(targetcli, fnum, ctx);
1565
1566         if (!old) {
1567                 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
1568                 errno = 0;
1569                 return -1;
1570         }
1571
1572         cli_close(targetcli, fnum);
1573
1574         switch (mode) {
1575         case SMBC_XATTR_MODE_REMOVE_ALL:
1576                 old->dacl->num_aces = 0;
1577                 dacl = old->dacl;
1578                 break;
1579
1580         case SMBC_XATTR_MODE_REMOVE:
1581                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1582                         bool found = False;
1583
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];
1591                                         }
1592                                         old->dacl->num_aces--;
1593                                         found = True;
1594                                         dacl = old->dacl;
1595                                         break;
1596                                 }
1597                         }
1598
1599                         if (!found) {
1600                                 err = ENOATTR;
1601                                 ret = -1;
1602                                 goto failed;
1603                         }
1604                 }
1605                 break;
1606
1607         case SMBC_XATTR_MODE_ADD:
1608                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1609                         bool found = False;
1610
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;
1618                                         }
1619                                         old->dacl->aces[j] = sd->dacl->aces[i];
1620                                         ret = -1;
1621                                         found = True;
1622                                 }
1623                         }
1624
1625                         if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
1626                                 err = ENOATTR;
1627                                 ret = -1;
1628                                 goto failed;
1629                         }
1630
1631                         for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1632                                 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
1633                         }
1634                 }
1635                 dacl = old->dacl;
1636                 break;
1637
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;
1644
1645         case SMBC_XATTR_MODE_CHOWN:
1646                 owner_sid = sd->owner_sid;
1647                 break;
1648
1649         case SMBC_XATTR_MODE_CHGRP:
1650                 group_sid = sd->group_sid;
1651                 break;
1652         }
1653
1654         /* Denied ACE entries must come before allowed ones */
1655         sort_acl(old->dacl);
1656
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);
1660
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;
1668         }
1669
1670         if (!cli_set_secdesc(targetcli, fnum, sd)) {
1671                 DEBUG(5, ("ERROR: secdesc set failed: %s\n",
1672                         cli_errstr(targetcli)));
1673                 ret = -1;
1674         }
1675
1676         /* Clean up */
1677
1678 failed:
1679         cli_close(targetcli, fnum);
1680
1681         if (err != 0) {
1682                 errno = err;
1683         }
1684
1685         return ret;
1686 }
1687
1688
1689 int
1690 SMBC_setxattr_ctx(SMBCCTX *context,
1691                   const char *fname,
1692                   const char *name,
1693                   const void *value,
1694                   size_t size,
1695                   int flags)
1696 {
1697         int ret;
1698         int ret2;
1699         SMBCSRV *srv = NULL;
1700         SMBCSRV *ipc_srv = NULL;
1701         char *server = NULL;
1702         char *share = NULL;
1703         char *user = NULL;
1704         char *password = NULL;
1705         char *workgroup = NULL;
1706         char *path = NULL;
1707         DOS_ATTR_DESC *dad = NULL;
1708         struct {
1709                 const char * create_time_attr;
1710                 const char * access_time_attr;
1711                 const char * write_time_attr;
1712                 const char * change_time_attr;
1713         } attr_strings;
1714         TALLOC_CTX *frame = talloc_stackframe();
1715
1716         if (!context || !context->internal->initialized) {
1717                 errno = EINVAL;  /* Best I can think of ... */
1718                 TALLOC_FREE(frame);
1719                 return -1;
1720         }
1721
1722         if (!fname) {
1723                 errno = EINVAL;
1724                 TALLOC_FREE(frame);
1725                 return -1;
1726         }
1727
1728         DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
1729                   fname, name, (int) size, (const char*)value));
1730
1731         if (SMBC_parse_path(frame,
1732                             context,
1733                             fname,
1734                             &workgroup,
1735                             &server,
1736                             &share,
1737                             &path,
1738                             &user,
1739                             &password,
1740                             NULL)) {
1741                 errno = EINVAL;
1742                 TALLOC_FREE(frame);
1743                 return -1;
1744         }
1745
1746         if (!user || user[0] == (char)0) {
1747                 user = talloc_strdup(frame, smbc_getUser(context));
1748                 if (!user) {
1749                         errno = ENOMEM;
1750                         TALLOC_FREE(frame);
1751                         return -1;
1752                 }
1753         }
1754
1755         srv = SMBC_server(frame, context, True,
1756                           server, share, &workgroup, &user, &password);
1757         if (!srv) {
1758                 TALLOC_FREE(frame);
1759                 return -1;  /* errno set by SMBC_server */
1760         }
1761
1762         if (! srv->no_nt_session) {
1763                 ipc_srv = SMBC_attr_server(frame, context, server, share,
1764                                            &workgroup, &user, &password);
1765                 if (! ipc_srv) {
1766                         srv->no_nt_session = True;
1767                 }
1768         } else {
1769                 ipc_srv = NULL;
1770         }
1771
1772         /*
1773          * Are they asking to set the entire set of known attributes?
1774          */
1775         if (StrCaseCmp(name, "system.*") == 0 ||
1776             StrCaseCmp(name, "system.*+") == 0) {
1777                 /* Yup. */
1778                 char *namevalue =
1779                         talloc_asprintf(talloc_tos(), "%s:%s",
1780                                         name+7, (const char *) value);
1781                 if (! namevalue) {
1782                         errno = ENOMEM;
1783                         ret = -1;
1784                         TALLOC_FREE(frame);
1785                         return -1;
1786                 }
1787
1788                 if (ipc_srv) {
1789                         ret = cacl_set(context, talloc_tos(), srv->cli,
1790                                        ipc_srv->cli, &ipc_srv->pol, path,
1791                                        namevalue,
1792                                        (*namevalue == '*'
1793                                         ? SMBC_XATTR_MODE_SET
1794                                         : SMBC_XATTR_MODE_ADD),
1795                                        flags);
1796                 } else {
1797                         ret = 0;
1798                 }
1799
1800                 /* get a DOS Attribute Descriptor with current attributes */
1801                 dad = dos_attr_query(context, talloc_tos(), path, srv);
1802                 if (dad) {
1803                         /* Overwrite old with new, using what was provided */
1804                         dos_attr_parse(context, dad, srv, namevalue);
1805
1806                         /* Set the new DOS attributes */
1807                         if (! SMBC_setatr(context, srv, path,
1808                                           dad->create_time,
1809                                           dad->access_time,
1810                                           dad->write_time,
1811                                           dad->change_time,
1812                                           dad->mode)) {
1813
1814                                 /* cause failure if NT failed too */
1815                                 dad = NULL; 
1816                         }
1817                 }
1818
1819                 /* we only fail if both NT and DOS sets failed */
1820                 if (ret < 0 && ! dad) {
1821                         ret = -1; /* in case dad was null */
1822                 }
1823                 else {
1824                         ret = 0;
1825                 }
1826
1827                 TALLOC_FREE(frame);
1828                 return ret;
1829         }
1830
1831         /*
1832          * Are they asking to set an access control element or to set
1833          * the entire access control list?
1834          */
1835         if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
1836             StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
1837             StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
1838             StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
1839             StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
1840
1841                 /* Yup. */
1842                 char *namevalue =
1843                         talloc_asprintf(talloc_tos(), "%s:%s",
1844                                         name+19, (const char *) value);
1845
1846                 if (! ipc_srv) {
1847                         ret = -1; /* errno set by SMBC_server() */
1848                 }
1849                 else if (! namevalue) {
1850                         errno = ENOMEM;
1851                         ret = -1;
1852                 } else {
1853                         ret = cacl_set(context, talloc_tos(), srv->cli,
1854                                        ipc_srv->cli, &ipc_srv->pol, path,
1855                                        namevalue,
1856                                        (*namevalue == '*'
1857                                         ? SMBC_XATTR_MODE_SET
1858                                         : SMBC_XATTR_MODE_ADD),
1859                                        flags);
1860                 }
1861                 TALLOC_FREE(frame);
1862                 return ret;
1863         }
1864
1865         /*
1866          * Are they asking to set the owner?
1867          */
1868         if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
1869             StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
1870
1871                 /* Yup. */
1872                 char *namevalue =
1873                         talloc_asprintf(talloc_tos(), "%s:%s",
1874                                         name+19, (const char *) value);
1875
1876                 if (! ipc_srv) {
1877                         ret = -1; /* errno set by SMBC_server() */
1878                 }
1879                 else if (! namevalue) {
1880                         errno = ENOMEM;
1881                         ret = -1;
1882                 } else {
1883                         ret = cacl_set(context, talloc_tos(), srv->cli,
1884                                        ipc_srv->cli, &ipc_srv->pol, path,
1885                                        namevalue, SMBC_XATTR_MODE_CHOWN, 0);
1886                 }
1887                 TALLOC_FREE(frame);
1888                 return ret;
1889         }
1890
1891         /*
1892          * Are they asking to set the group?
1893          */
1894         if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
1895             StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
1896
1897                 /* Yup. */
1898                 char *namevalue =
1899                         talloc_asprintf(talloc_tos(), "%s:%s",
1900                                         name+19, (const char *) value);
1901
1902                 if (! ipc_srv) {
1903                         /* errno set by SMBC_server() */
1904                         ret = -1;
1905                 }
1906                 else if (! namevalue) {
1907                         errno = ENOMEM;
1908                         ret = -1;
1909                 } else {
1910                         ret = cacl_set(context, talloc_tos(), srv->cli,
1911                                        ipc_srv->cli, &ipc_srv->pol, path,
1912                                        namevalue, SMBC_XATTR_MODE_CHGRP, 0);
1913                 }
1914                 TALLOC_FREE(frame);
1915                 return ret;
1916         }
1917
1918         /* Determine whether to use old-style or new-style attribute names */
1919         if (context->internal->full_time_names) {
1920                 /* new-style names */
1921                 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
1922                 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
1923                 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
1924                 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
1925         } else {
1926                 /* old-style names */
1927                 attr_strings.create_time_attr = NULL;
1928                 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
1929                 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
1930                 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
1931         }
1932
1933         /*
1934          * Are they asking to set a DOS attribute?
1935          */
1936         if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
1937             StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
1938             (attr_strings.create_time_attr != NULL &&
1939              StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
1940             StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
1941             StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
1942             StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
1943
1944                 /* get a DOS Attribute Descriptor with current attributes */
1945                 dad = dos_attr_query(context, talloc_tos(), path, srv);
1946                 if (dad) {
1947                         char *namevalue =
1948                                 talloc_asprintf(talloc_tos(), "%s:%s",
1949                                                 name+16, (const char *) value);
1950                         if (! namevalue) {
1951                                 errno = ENOMEM;
1952                                 ret = -1;
1953                         } else {
1954                                 /* Overwrite old with provided new params */
1955                                 dos_attr_parse(context, dad, srv, namevalue);
1956
1957                                 /* Set the new DOS attributes */
1958                                 ret2 = SMBC_setatr(context, srv, path,
1959                                                    dad->create_time,
1960                                                    dad->access_time,
1961                                                    dad->write_time,
1962                                                    dad->change_time,
1963                                                    dad->mode);
1964
1965                                 /* ret2 has True (success) / False (failure) */
1966                                 if (ret2) {
1967                                         ret = 0;
1968                                 } else {
1969                                         ret = -1;
1970                                 }
1971                         }
1972                 } else {
1973                         ret = -1;
1974                 }
1975
1976                 TALLOC_FREE(frame);
1977                 return ret;
1978         }
1979
1980         /* Unsupported attribute name */
1981         errno = EINVAL;
1982         TALLOC_FREE(frame);
1983         return -1;
1984 }
1985
1986 int
1987 SMBC_getxattr_ctx(SMBCCTX *context,
1988                   const char *fname,
1989                   const char *name,
1990                   const void *value,
1991                   size_t size)
1992 {
1993         int ret;
1994         SMBCSRV *srv = NULL;
1995         SMBCSRV *ipc_srv = NULL;
1996         char *server = NULL;
1997         char *share = NULL;
1998         char *user = NULL;
1999         char *password = NULL;
2000         char *workgroup = NULL;
2001         char *path = NULL;
2002         struct {
2003                 const char * create_time_attr;
2004                 const char * access_time_attr;
2005                 const char * write_time_attr;
2006                 const char * change_time_attr;
2007         } attr_strings;
2008         TALLOC_CTX *frame = talloc_stackframe();
2009
2010         if (!context || !context->internal->initialized) {
2011                 errno = EINVAL;  /* Best I can think of ... */
2012                 TALLOC_FREE(frame);
2013                 return -1;
2014         }
2015
2016         if (!fname) {
2017                 errno = EINVAL;
2018                 TALLOC_FREE(frame);
2019                 return -1;
2020         }
2021
2022         DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
2023
2024         if (SMBC_parse_path(frame,
2025                             context,
2026                             fname,
2027                             &workgroup,
2028                             &server,
2029                             &share,
2030                             &path,
2031                             &user,
2032                             &password,
2033                             NULL)) {
2034                 errno = EINVAL;
2035                 TALLOC_FREE(frame);
2036                 return -1;
2037         }
2038
2039         if (!user || user[0] == (char)0) {
2040                 user = talloc_strdup(frame, smbc_getUser(context));
2041                 if (!user) {
2042                         errno = ENOMEM;
2043                         TALLOC_FREE(frame);
2044                         return -1;
2045                 }
2046         }
2047
2048         srv = SMBC_server(frame, context, True,
2049                           server, share, &workgroup, &user, &password);
2050         if (!srv) {
2051                 TALLOC_FREE(frame);
2052                 return -1;  /* errno set by SMBC_server */
2053         }
2054
2055         if (! srv->no_nt_session) {
2056                 ipc_srv = SMBC_attr_server(frame, context, server, share,
2057                                            &workgroup, &user, &password);
2058                 if (! ipc_srv) {
2059                         srv->no_nt_session = True;
2060                 }
2061         } else {
2062                 ipc_srv = NULL;
2063         }
2064
2065         /* Determine whether to use old-style or new-style attribute names */
2066         if (context->internal->full_time_names) {
2067                 /* new-style names */
2068                 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
2069                 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
2070                 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
2071                 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
2072         } else {
2073                 /* old-style names */
2074                 attr_strings.create_time_attr = NULL;
2075                 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
2076                 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
2077                 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
2078         }
2079
2080         /* Are they requesting a supported attribute? */
2081         if (StrCaseCmp(name, "system.*") == 0 ||
2082             StrnCaseCmp(name, "system.*!", 9) == 0 ||
2083             StrCaseCmp(name, "system.*+") == 0 ||
2084             StrnCaseCmp(name, "system.*+!", 10) == 0 ||
2085             StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
2086             StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
2087             StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
2088             StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
2089             StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
2090             StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
2091             StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
2092             StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
2093             StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
2094             StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
2095             StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
2096             StrCaseCmp(name, "system.dos_attr.*") == 0 ||
2097             StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
2098             StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
2099             StrCaseCmp(name, "system.dos_attr.size") == 0 ||
2100             (attr_strings.create_time_attr != NULL &&
2101              StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
2102             StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
2103             StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
2104             StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
2105             StrCaseCmp(name, "system.dos_attr.inode") == 0) {
2106
2107                 /* Yup. */
2108                 char *filename = (char *) name;
2109                 ret = cacl_get(context, talloc_tos(), srv,
2110                                ipc_srv == NULL ? NULL : ipc_srv->cli, 
2111                                &ipc_srv->pol, path,
2112                                filename,
2113                                CONST_DISCARD(char *, value),
2114                                size);
2115                 if (ret < 0 && errno == 0) {
2116                         errno = SMBC_errno(context, srv->cli);
2117                 }
2118                 TALLOC_FREE(frame);
2119                 return ret;
2120         }
2121
2122         /* Unsupported attribute name */
2123         errno = EINVAL;
2124         TALLOC_FREE(frame);
2125         return -1;
2126 }
2127
2128
2129 int
2130 SMBC_removexattr_ctx(SMBCCTX *context,
2131                      const char *fname,
2132                      const char *name)
2133 {
2134         int ret;
2135         SMBCSRV *srv = NULL;
2136         SMBCSRV *ipc_srv = NULL;
2137         char *server = NULL;
2138         char *share = NULL;
2139         char *user = NULL;
2140         char *password = NULL;
2141         char *workgroup = NULL;
2142         char *path = NULL;
2143         TALLOC_CTX *frame = talloc_stackframe();
2144
2145         if (!context || !context->internal->initialized) {
2146                 errno = EINVAL;  /* Best I can think of ... */
2147                 TALLOC_FREE(frame);
2148                 return -1;
2149         }
2150
2151         if (!fname) {
2152                 errno = EINVAL;
2153                 TALLOC_FREE(frame);
2154                 return -1;
2155         }
2156
2157         DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
2158
2159         if (SMBC_parse_path(frame,
2160                             context,
2161                             fname,
2162                             &workgroup,
2163                             &server,
2164                             &share,
2165                             &path,
2166                             &user,
2167                             &password,
2168                             NULL)) {
2169                 errno = EINVAL;
2170                 TALLOC_FREE(frame);
2171                 return -1;
2172         }
2173
2174         if (!user || user[0] == (char)0) {
2175                 user = talloc_strdup(frame, smbc_getUser(context));
2176                 if (!user) {
2177                         errno = ENOMEM;
2178                         TALLOC_FREE(frame);
2179                         return -1;
2180                 }
2181         }
2182
2183         srv = SMBC_server(frame, context, True,
2184                           server, share, &workgroup, &user, &password);
2185         if (!srv) {
2186                 TALLOC_FREE(frame);
2187                 return -1;  /* errno set by SMBC_server */
2188         }
2189
2190         if (! srv->no_nt_session) {
2191                 ipc_srv = SMBC_attr_server(frame, context, server, share,
2192                                            &workgroup, &user, &password);
2193                 if (! ipc_srv) {
2194                         srv->no_nt_session = True;
2195                 }
2196         } else {
2197                 ipc_srv = NULL;
2198         }
2199
2200         if (! ipc_srv) {
2201                 TALLOC_FREE(frame);
2202                 return -1; /* errno set by SMBC_attr_server */
2203         }
2204
2205         /* Are they asking to set the entire ACL? */
2206         if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
2207             StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
2208
2209                 /* Yup. */
2210                 ret = cacl_set(context, talloc_tos(), srv->cli,
2211                                ipc_srv->cli, &ipc_srv->pol, path,
2212                                NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
2213                 TALLOC_FREE(frame);
2214                 return ret;
2215         }
2216
2217         /*
2218          * Are they asking to remove one or more spceific security descriptor
2219          * attributes?
2220          */
2221         if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
2222             StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
2223             StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
2224             StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
2225             StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
2226             StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
2227             StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
2228
2229                 /* Yup. */
2230                 ret = cacl_set(context, talloc_tos(), srv->cli,
2231                                ipc_srv->cli, &ipc_srv->pol, path,
2232                                CONST_DISCARD(char *, name) + 19,
2233                                SMBC_XATTR_MODE_REMOVE, 0);
2234                 TALLOC_FREE(frame);
2235                 return ret;
2236         }
2237
2238         /* Unsupported attribute name */
2239         errno = EINVAL;
2240         TALLOC_FREE(frame);
2241         return -1;
2242 }
2243
2244 int
2245 SMBC_listxattr_ctx(SMBCCTX *context,
2246                    const char *fname,
2247                    char *list,
2248                    size_t size)
2249 {
2250         /*
2251          * This isn't quite what listxattr() is supposed to do.  This returns
2252          * the complete set of attribute names, always, rather than only those
2253          * attribute names which actually exist for a file.  Hmmm...
2254          */
2255         size_t retsize;
2256         const char supported_old[] =
2257                 "system.*\0"
2258                 "system.*+\0"
2259                 "system.nt_sec_desc.revision\0"
2260                 "system.nt_sec_desc.owner\0"
2261                 "system.nt_sec_desc.owner+\0"
2262                 "system.nt_sec_desc.group\0"
2263                 "system.nt_sec_desc.group+\0"
2264                 "system.nt_sec_desc.acl.*\0"
2265                 "system.nt_sec_desc.acl\0"
2266                 "system.nt_sec_desc.acl+\0"
2267                 "system.nt_sec_desc.*\0"
2268                 "system.nt_sec_desc.*+\0"
2269                 "system.dos_attr.*\0"
2270                 "system.dos_attr.mode\0"
2271                 "system.dos_attr.c_time\0"
2272                 "system.dos_attr.a_time\0"
2273                 "system.dos_attr.m_time\0"
2274                 ;
2275         const char supported_new[] =
2276                 "system.*\0"
2277                 "system.*+\0"
2278                 "system.nt_sec_desc.revision\0"
2279                 "system.nt_sec_desc.owner\0"
2280                 "system.nt_sec_desc.owner+\0"
2281                 "system.nt_sec_desc.group\0"
2282                 "system.nt_sec_desc.group+\0"
2283                 "system.nt_sec_desc.acl.*\0"
2284                 "system.nt_sec_desc.acl\0"
2285                 "system.nt_sec_desc.acl+\0"
2286                 "system.nt_sec_desc.*\0"
2287                 "system.nt_sec_desc.*+\0"
2288                 "system.dos_attr.*\0"
2289                 "system.dos_attr.mode\0"
2290                 "system.dos_attr.create_time\0"
2291                 "system.dos_attr.access_time\0"
2292                 "system.dos_attr.write_time\0"
2293                 "system.dos_attr.change_time\0"
2294                 ;
2295         const char * supported;
2296
2297         if (context->internal->full_time_names) {
2298                 supported = supported_new;
2299                 retsize = sizeof(supported_new);
2300         } else {
2301                 supported = supported_old;
2302                 retsize = sizeof(supported_old);
2303         }
2304
2305         if (size == 0) {
2306                 return retsize;
2307         }
2308
2309         if (retsize > size) {
2310                 errno = ERANGE;
2311                 return -1;
2312         }
2313
2314         /* this can't be strcpy() because there are embedded null characters */
2315         memcpy(list, supported, retsize);
2316         return retsize;
2317 }