s3-rpc_client: add and use rpc_client/rpc_client.h.
[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/rpc_client.h"
30 #include "rpc_client/cli_lsarpc.h"
31 #include "../libcli/security/security.h"
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         NTSTATUS status;
1518
1519         /* the_acl will be null for REMOVE_ALL operations */
1520         if (the_acl) {
1521                 numeric = ((p = strchr(the_acl, ':')) != NULL &&
1522                            p > the_acl &&
1523                            p[-1] != '+');
1524
1525                 /* if this is to set the entire ACL... */
1526                 if (*the_acl == '*') {
1527                         /* ... then increment past the first colon */
1528                         the_acl = p + 1;
1529                 }
1530
1531                 sd = sec_desc_parse(ctx, ipc_cli, pol, numeric, the_acl);
1532                 if (!sd) {
1533                         errno = EINVAL;
1534                         return -1;
1535                 }
1536         }
1537
1538         /* SMBC_XATTR_MODE_REMOVE_ALL is the only caller
1539            that doesn't deref sd */
1540
1541         if (!sd && (mode != SMBC_XATTR_MODE_REMOVE_ALL)) {
1542                 errno = EINVAL;
1543                 return -1;
1544         }
1545
1546         if (!cli_resolve_path(ctx, "", context->internal->auth_info,
1547                         cli, filename,
1548                         &targetcli, &targetpath)) {
1549                 DEBUG(5,("cacl_set: Could not resolve %s\n", filename));
1550                 errno = ENOENT;
1551                 return -1;
1552         }
1553
1554         /* The desired access below is the only one I could find that works
1555            with NT4, W2KP and Samba */
1556
1557         if (!NT_STATUS_IS_OK(cli_ntcreate(targetcli, targetpath, 0, CREATE_ACCESS_READ, 0,
1558                                 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
1559                 DEBUG(5, ("cacl_set failed to open %s: %s\n",
1560                           targetpath, cli_errstr(targetcli)));
1561                 errno = 0;
1562                 return -1;
1563         }
1564
1565         old = cli_query_secdesc(targetcli, fnum, ctx);
1566
1567         if (!old) {
1568                 DEBUG(5, ("cacl_set Failed to query old descriptor\n"));
1569                 errno = 0;
1570                 return -1;
1571         }
1572
1573         cli_close(targetcli, fnum);
1574
1575         switch (mode) {
1576         case SMBC_XATTR_MODE_REMOVE_ALL:
1577                 old->dacl->num_aces = 0;
1578                 dacl = old->dacl;
1579                 break;
1580
1581         case SMBC_XATTR_MODE_REMOVE:
1582                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1583                         bool found = False;
1584
1585                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
1586                                 if (sec_ace_equal(&sd->dacl->aces[i],
1587                                                   &old->dacl->aces[j])) {
1588                                         uint32 k;
1589                                         for (k=j; k<old->dacl->num_aces-1;k++) {
1590                                                 old->dacl->aces[k] =
1591                                                         old->dacl->aces[k+1];
1592                                         }
1593                                         old->dacl->num_aces--;
1594                                         found = True;
1595                                         dacl = old->dacl;
1596                                         break;
1597                                 }
1598                         }
1599
1600                         if (!found) {
1601                                 err = ENOATTR;
1602                                 ret = -1;
1603                                 goto failed;
1604                         }
1605                 }
1606                 break;
1607
1608         case SMBC_XATTR_MODE_ADD:
1609                 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1610                         bool found = False;
1611
1612                         for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
1613                                 if (dom_sid_equal(&sd->dacl->aces[i].trustee,
1614                                               &old->dacl->aces[j].trustee)) {
1615                                         if (!(flags & SMBC_XATTR_FLAG_CREATE)) {
1616                                                 err = EEXIST;
1617                                                 ret = -1;
1618                                                 goto failed;
1619                                         }
1620                                         old->dacl->aces[j] = sd->dacl->aces[i];
1621                                         ret = -1;
1622                                         found = True;
1623                                 }
1624                         }
1625
1626                         if (!found && (flags & SMBC_XATTR_FLAG_REPLACE)) {
1627                                 err = ENOATTR;
1628                                 ret = -1;
1629                                 goto failed;
1630                         }
1631
1632                         for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
1633                                 add_ace(&old->dacl, &sd->dacl->aces[i], ctx);
1634                         }
1635                 }
1636                 dacl = old->dacl;
1637                 break;
1638
1639         case SMBC_XATTR_MODE_SET:
1640                 old = sd;
1641                 owner_sid = old->owner_sid;
1642                 group_sid = old->group_sid;
1643                 dacl = old->dacl;
1644                 break;
1645
1646         case SMBC_XATTR_MODE_CHOWN:
1647                 owner_sid = sd->owner_sid;
1648                 break;
1649
1650         case SMBC_XATTR_MODE_CHGRP:
1651                 group_sid = sd->group_sid;
1652                 break;
1653         }
1654
1655         /* Denied ACE entries must come before allowed ones */
1656         sort_acl(old->dacl);
1657
1658         /* Create new security descriptor and set it */
1659         sd = make_sec_desc(ctx, old->revision, SEC_DESC_SELF_RELATIVE,
1660                            owner_sid, group_sid, NULL, dacl, &sd_size);
1661
1662         if (!NT_STATUS_IS_OK(cli_ntcreate(targetcli, targetpath, 0,
1663                              WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS, 0,
1664                              FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
1665                 DEBUG(5, ("cacl_set failed to open %s: %s\n",
1666                           targetpath, cli_errstr(targetcli)));
1667                 errno = 0;
1668                 return -1;
1669         }
1670
1671         status = cli_set_secdesc(targetcli, fnum, sd);
1672         if (!NT_STATUS_IS_OK(status)) {
1673                 DEBUG(5, ("ERROR: secdesc set failed: %s\n",
1674                           nt_errstr(status)));
1675                 ret = -1;
1676         }
1677
1678         /* Clean up */
1679
1680 failed:
1681         cli_close(targetcli, fnum);
1682
1683         if (err != 0) {
1684                 errno = err;
1685         }
1686
1687         return ret;
1688 }
1689
1690
1691 int
1692 SMBC_setxattr_ctx(SMBCCTX *context,
1693                   const char *fname,
1694                   const char *name,
1695                   const void *value,
1696                   size_t size,
1697                   int flags)
1698 {
1699         int ret;
1700         int ret2;
1701         SMBCSRV *srv = NULL;
1702         SMBCSRV *ipc_srv = NULL;
1703         char *server = NULL;
1704         char *share = NULL;
1705         char *user = NULL;
1706         char *password = NULL;
1707         char *workgroup = NULL;
1708         char *path = NULL;
1709         DOS_ATTR_DESC *dad = NULL;
1710         struct {
1711                 const char * create_time_attr;
1712                 const char * access_time_attr;
1713                 const char * write_time_attr;
1714                 const char * change_time_attr;
1715         } attr_strings;
1716         TALLOC_CTX *frame = talloc_stackframe();
1717
1718         if (!context || !context->internal->initialized) {
1719                 errno = EINVAL;  /* Best I can think of ... */
1720                 TALLOC_FREE(frame);
1721                 return -1;
1722         }
1723
1724         if (!fname) {
1725                 errno = EINVAL;
1726                 TALLOC_FREE(frame);
1727                 return -1;
1728         }
1729
1730         DEBUG(4, ("smbc_setxattr(%s, %s, %.*s)\n",
1731                   fname, name, (int) size, (const char*)value));
1732
1733         if (SMBC_parse_path(frame,
1734                             context,
1735                             fname,
1736                             &workgroup,
1737                             &server,
1738                             &share,
1739                             &path,
1740                             &user,
1741                             &password,
1742                             NULL)) {
1743                 errno = EINVAL;
1744                 TALLOC_FREE(frame);
1745                 return -1;
1746         }
1747
1748         if (!user || user[0] == (char)0) {
1749                 user = talloc_strdup(frame, smbc_getUser(context));
1750                 if (!user) {
1751                         errno = ENOMEM;
1752                         TALLOC_FREE(frame);
1753                         return -1;
1754                 }
1755         }
1756
1757         srv = SMBC_server(frame, context, True,
1758                           server, share, &workgroup, &user, &password);
1759         if (!srv) {
1760                 TALLOC_FREE(frame);
1761                 return -1;  /* errno set by SMBC_server */
1762         }
1763
1764         if (! srv->no_nt_session) {
1765                 ipc_srv = SMBC_attr_server(frame, context, server, share,
1766                                            &workgroup, &user, &password);
1767                 if (! ipc_srv) {
1768                         srv->no_nt_session = True;
1769                 }
1770         } else {
1771                 ipc_srv = NULL;
1772         }
1773
1774         /*
1775          * Are they asking to set the entire set of known attributes?
1776          */
1777         if (StrCaseCmp(name, "system.*") == 0 ||
1778             StrCaseCmp(name, "system.*+") == 0) {
1779                 /* Yup. */
1780                 char *namevalue =
1781                         talloc_asprintf(talloc_tos(), "%s:%s",
1782                                         name+7, (const char *) value);
1783                 if (! namevalue) {
1784                         errno = ENOMEM;
1785                         ret = -1;
1786                         TALLOC_FREE(frame);
1787                         return -1;
1788                 }
1789
1790                 if (ipc_srv) {
1791                         ret = cacl_set(context, talloc_tos(), srv->cli,
1792                                        ipc_srv->cli, &ipc_srv->pol, path,
1793                                        namevalue,
1794                                        (*namevalue == '*'
1795                                         ? SMBC_XATTR_MODE_SET
1796                                         : SMBC_XATTR_MODE_ADD),
1797                                        flags);
1798                 } else {
1799                         ret = 0;
1800                 }
1801
1802                 /* get a DOS Attribute Descriptor with current attributes */
1803                 dad = dos_attr_query(context, talloc_tos(), path, srv);
1804                 if (dad) {
1805                         /* Overwrite old with new, using what was provided */
1806                         dos_attr_parse(context, dad, srv, namevalue);
1807
1808                         /* Set the new DOS attributes */
1809                         if (! SMBC_setatr(context, srv, path,
1810                                           dad->create_time,
1811                                           dad->access_time,
1812                                           dad->write_time,
1813                                           dad->change_time,
1814                                           dad->mode)) {
1815
1816                                 /* cause failure if NT failed too */
1817                                 dad = NULL; 
1818                         }
1819                 }
1820
1821                 /* we only fail if both NT and DOS sets failed */
1822                 if (ret < 0 && ! dad) {
1823                         ret = -1; /* in case dad was null */
1824                 }
1825                 else {
1826                         ret = 0;
1827                 }
1828
1829                 TALLOC_FREE(frame);
1830                 return ret;
1831         }
1832
1833         /*
1834          * Are they asking to set an access control element or to set
1835          * the entire access control list?
1836          */
1837         if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
1838             StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
1839             StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
1840             StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
1841             StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
1842
1843                 /* Yup. */
1844                 char *namevalue =
1845                         talloc_asprintf(talloc_tos(), "%s:%s",
1846                                         name+19, (const char *) value);
1847
1848                 if (! ipc_srv) {
1849                         ret = -1; /* errno set by SMBC_server() */
1850                 }
1851                 else if (! namevalue) {
1852                         errno = ENOMEM;
1853                         ret = -1;
1854                 } else {
1855                         ret = cacl_set(context, talloc_tos(), srv->cli,
1856                                        ipc_srv->cli, &ipc_srv->pol, path,
1857                                        namevalue,
1858                                        (*namevalue == '*'
1859                                         ? SMBC_XATTR_MODE_SET
1860                                         : SMBC_XATTR_MODE_ADD),
1861                                        flags);
1862                 }
1863                 TALLOC_FREE(frame);
1864                 return ret;
1865         }
1866
1867         /*
1868          * Are they asking to set the owner?
1869          */
1870         if (StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
1871             StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0) {
1872
1873                 /* Yup. */
1874                 char *namevalue =
1875                         talloc_asprintf(talloc_tos(), "%s:%s",
1876                                         name+19, (const char *) value);
1877
1878                 if (! ipc_srv) {
1879                         ret = -1; /* errno set by SMBC_server() */
1880                 }
1881                 else if (! namevalue) {
1882                         errno = ENOMEM;
1883                         ret = -1;
1884                 } else {
1885                         ret = cacl_set(context, talloc_tos(), srv->cli,
1886                                        ipc_srv->cli, &ipc_srv->pol, path,
1887                                        namevalue, SMBC_XATTR_MODE_CHOWN, 0);
1888                 }
1889                 TALLOC_FREE(frame);
1890                 return ret;
1891         }
1892
1893         /*
1894          * Are they asking to set the group?
1895          */
1896         if (StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
1897             StrCaseCmp(name, "system.nt_sec_desc.group+") == 0) {
1898
1899                 /* Yup. */
1900                 char *namevalue =
1901                         talloc_asprintf(talloc_tos(), "%s:%s",
1902                                         name+19, (const char *) value);
1903
1904                 if (! ipc_srv) {
1905                         /* errno set by SMBC_server() */
1906                         ret = -1;
1907                 }
1908                 else if (! namevalue) {
1909                         errno = ENOMEM;
1910                         ret = -1;
1911                 } else {
1912                         ret = cacl_set(context, talloc_tos(), srv->cli,
1913                                        ipc_srv->cli, &ipc_srv->pol, path,
1914                                        namevalue, SMBC_XATTR_MODE_CHGRP, 0);
1915                 }
1916                 TALLOC_FREE(frame);
1917                 return ret;
1918         }
1919
1920         /* Determine whether to use old-style or new-style attribute names */
1921         if (context->internal->full_time_names) {
1922                 /* new-style names */
1923                 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
1924                 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
1925                 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
1926                 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
1927         } else {
1928                 /* old-style names */
1929                 attr_strings.create_time_attr = NULL;
1930                 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
1931                 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
1932                 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
1933         }
1934
1935         /*
1936          * Are they asking to set a DOS attribute?
1937          */
1938         if (StrCaseCmp(name, "system.dos_attr.*") == 0 ||
1939             StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
1940             (attr_strings.create_time_attr != NULL &&
1941              StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
1942             StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
1943             StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
1944             StrCaseCmp(name, attr_strings.change_time_attr) == 0) {
1945
1946                 /* get a DOS Attribute Descriptor with current attributes */
1947                 dad = dos_attr_query(context, talloc_tos(), path, srv);
1948                 if (dad) {
1949                         char *namevalue =
1950                                 talloc_asprintf(talloc_tos(), "%s:%s",
1951                                                 name+16, (const char *) value);
1952                         if (! namevalue) {
1953                                 errno = ENOMEM;
1954                                 ret = -1;
1955                         } else {
1956                                 /* Overwrite old with provided new params */
1957                                 dos_attr_parse(context, dad, srv, namevalue);
1958
1959                                 /* Set the new DOS attributes */
1960                                 ret2 = SMBC_setatr(context, srv, path,
1961                                                    dad->create_time,
1962                                                    dad->access_time,
1963                                                    dad->write_time,
1964                                                    dad->change_time,
1965                                                    dad->mode);
1966
1967                                 /* ret2 has True (success) / False (failure) */
1968                                 if (ret2) {
1969                                         ret = 0;
1970                                 } else {
1971                                         ret = -1;
1972                                 }
1973                         }
1974                 } else {
1975                         ret = -1;
1976                 }
1977
1978                 TALLOC_FREE(frame);
1979                 return ret;
1980         }
1981
1982         /* Unsupported attribute name */
1983         errno = EINVAL;
1984         TALLOC_FREE(frame);
1985         return -1;
1986 }
1987
1988 int
1989 SMBC_getxattr_ctx(SMBCCTX *context,
1990                   const char *fname,
1991                   const char *name,
1992                   const void *value,
1993                   size_t size)
1994 {
1995         int ret;
1996         SMBCSRV *srv = NULL;
1997         SMBCSRV *ipc_srv = NULL;
1998         char *server = NULL;
1999         char *share = NULL;
2000         char *user = NULL;
2001         char *password = NULL;
2002         char *workgroup = NULL;
2003         char *path = NULL;
2004         struct {
2005                 const char * create_time_attr;
2006                 const char * access_time_attr;
2007                 const char * write_time_attr;
2008                 const char * change_time_attr;
2009         } attr_strings;
2010         TALLOC_CTX *frame = talloc_stackframe();
2011
2012         if (!context || !context->internal->initialized) {
2013                 errno = EINVAL;  /* Best I can think of ... */
2014                 TALLOC_FREE(frame);
2015                 return -1;
2016         }
2017
2018         if (!fname) {
2019                 errno = EINVAL;
2020                 TALLOC_FREE(frame);
2021                 return -1;
2022         }
2023
2024         DEBUG(4, ("smbc_getxattr(%s, %s)\n", fname, name));
2025
2026         if (SMBC_parse_path(frame,
2027                             context,
2028                             fname,
2029                             &workgroup,
2030                             &server,
2031                             &share,
2032                             &path,
2033                             &user,
2034                             &password,
2035                             NULL)) {
2036                 errno = EINVAL;
2037                 TALLOC_FREE(frame);
2038                 return -1;
2039         }
2040
2041         if (!user || user[0] == (char)0) {
2042                 user = talloc_strdup(frame, smbc_getUser(context));
2043                 if (!user) {
2044                         errno = ENOMEM;
2045                         TALLOC_FREE(frame);
2046                         return -1;
2047                 }
2048         }
2049
2050         srv = SMBC_server(frame, context, True,
2051                           server, share, &workgroup, &user, &password);
2052         if (!srv) {
2053                 TALLOC_FREE(frame);
2054                 return -1;  /* errno set by SMBC_server */
2055         }
2056
2057         if (! srv->no_nt_session) {
2058                 ipc_srv = SMBC_attr_server(frame, context, server, share,
2059                                            &workgroup, &user, &password);
2060                 if (! ipc_srv) {
2061                         srv->no_nt_session = True;
2062                 }
2063         } else {
2064                 ipc_srv = NULL;
2065         }
2066
2067         /* Determine whether to use old-style or new-style attribute names */
2068         if (context->internal->full_time_names) {
2069                 /* new-style names */
2070                 attr_strings.create_time_attr = "system.dos_attr.CREATE_TIME";
2071                 attr_strings.access_time_attr = "system.dos_attr.ACCESS_TIME";
2072                 attr_strings.write_time_attr = "system.dos_attr.WRITE_TIME";
2073                 attr_strings.change_time_attr = "system.dos_attr.CHANGE_TIME";
2074         } else {
2075                 /* old-style names */
2076                 attr_strings.create_time_attr = NULL;
2077                 attr_strings.access_time_attr = "system.dos_attr.A_TIME";
2078                 attr_strings.write_time_attr = "system.dos_attr.M_TIME";
2079                 attr_strings.change_time_attr = "system.dos_attr.C_TIME";
2080         }
2081
2082         /* Are they requesting a supported attribute? */
2083         if (StrCaseCmp(name, "system.*") == 0 ||
2084             StrnCaseCmp(name, "system.*!", 9) == 0 ||
2085             StrCaseCmp(name, "system.*+") == 0 ||
2086             StrnCaseCmp(name, "system.*+!", 10) == 0 ||
2087             StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
2088             StrnCaseCmp(name, "system.nt_sec_desc.*!", 21) == 0 ||
2089             StrCaseCmp(name, "system.nt_sec_desc.*+") == 0 ||
2090             StrnCaseCmp(name, "system.nt_sec_desc.*+!", 22) == 0 ||
2091             StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
2092             StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
2093             StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
2094             StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
2095             StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
2096             StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
2097             StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0 ||
2098             StrCaseCmp(name, "system.dos_attr.*") == 0 ||
2099             StrnCaseCmp(name, "system.dos_attr.*!", 18) == 0 ||
2100             StrCaseCmp(name, "system.dos_attr.mode") == 0 ||
2101             StrCaseCmp(name, "system.dos_attr.size") == 0 ||
2102             (attr_strings.create_time_attr != NULL &&
2103              StrCaseCmp(name, attr_strings.create_time_attr) == 0) ||
2104             StrCaseCmp(name, attr_strings.access_time_attr) == 0 ||
2105             StrCaseCmp(name, attr_strings.write_time_attr) == 0 ||
2106             StrCaseCmp(name, attr_strings.change_time_attr) == 0 ||
2107             StrCaseCmp(name, "system.dos_attr.inode") == 0) {
2108
2109                 /* Yup. */
2110                 char *filename = (char *) name;
2111                 ret = cacl_get(context, talloc_tos(), srv,
2112                                ipc_srv == NULL ? NULL : ipc_srv->cli, 
2113                                &ipc_srv->pol, path,
2114                                filename,
2115                                CONST_DISCARD(char *, value),
2116                                size);
2117                 if (ret < 0 && errno == 0) {
2118                         errno = SMBC_errno(context, srv->cli);
2119                 }
2120                 TALLOC_FREE(frame);
2121                 return ret;
2122         }
2123
2124         /* Unsupported attribute name */
2125         errno = EINVAL;
2126         TALLOC_FREE(frame);
2127         return -1;
2128 }
2129
2130
2131 int
2132 SMBC_removexattr_ctx(SMBCCTX *context,
2133                      const char *fname,
2134                      const char *name)
2135 {
2136         int ret;
2137         SMBCSRV *srv = NULL;
2138         SMBCSRV *ipc_srv = NULL;
2139         char *server = NULL;
2140         char *share = NULL;
2141         char *user = NULL;
2142         char *password = NULL;
2143         char *workgroup = NULL;
2144         char *path = NULL;
2145         TALLOC_CTX *frame = talloc_stackframe();
2146
2147         if (!context || !context->internal->initialized) {
2148                 errno = EINVAL;  /* Best I can think of ... */
2149                 TALLOC_FREE(frame);
2150                 return -1;
2151         }
2152
2153         if (!fname) {
2154                 errno = EINVAL;
2155                 TALLOC_FREE(frame);
2156                 return -1;
2157         }
2158
2159         DEBUG(4, ("smbc_removexattr(%s, %s)\n", fname, name));
2160
2161         if (SMBC_parse_path(frame,
2162                             context,
2163                             fname,
2164                             &workgroup,
2165                             &server,
2166                             &share,
2167                             &path,
2168                             &user,
2169                             &password,
2170                             NULL)) {
2171                 errno = EINVAL;
2172                 TALLOC_FREE(frame);
2173                 return -1;
2174         }
2175
2176         if (!user || user[0] == (char)0) {
2177                 user = talloc_strdup(frame, smbc_getUser(context));
2178                 if (!user) {
2179                         errno = ENOMEM;
2180                         TALLOC_FREE(frame);
2181                         return -1;
2182                 }
2183         }
2184
2185         srv = SMBC_server(frame, context, True,
2186                           server, share, &workgroup, &user, &password);
2187         if (!srv) {
2188                 TALLOC_FREE(frame);
2189                 return -1;  /* errno set by SMBC_server */
2190         }
2191
2192         if (! srv->no_nt_session) {
2193                 ipc_srv = SMBC_attr_server(frame, context, server, share,
2194                                            &workgroup, &user, &password);
2195                 if (! ipc_srv) {
2196                         srv->no_nt_session = True;
2197                 }
2198         } else {
2199                 ipc_srv = NULL;
2200         }
2201
2202         if (! ipc_srv) {
2203                 TALLOC_FREE(frame);
2204                 return -1; /* errno set by SMBC_attr_server */
2205         }
2206
2207         /* Are they asking to set the entire ACL? */
2208         if (StrCaseCmp(name, "system.nt_sec_desc.*") == 0 ||
2209             StrCaseCmp(name, "system.nt_sec_desc.*+") == 0) {
2210
2211                 /* Yup. */
2212                 ret = cacl_set(context, talloc_tos(), srv->cli,
2213                                ipc_srv->cli, &ipc_srv->pol, path,
2214                                NULL, SMBC_XATTR_MODE_REMOVE_ALL, 0);
2215                 TALLOC_FREE(frame);
2216                 return ret;
2217         }
2218
2219         /*
2220          * Are they asking to remove one or more spceific security descriptor
2221          * attributes?
2222          */
2223         if (StrCaseCmp(name, "system.nt_sec_desc.revision") == 0 ||
2224             StrCaseCmp(name, "system.nt_sec_desc.owner") == 0 ||
2225             StrCaseCmp(name, "system.nt_sec_desc.owner+") == 0 ||
2226             StrCaseCmp(name, "system.nt_sec_desc.group") == 0 ||
2227             StrCaseCmp(name, "system.nt_sec_desc.group+") == 0 ||
2228             StrnCaseCmp(name, "system.nt_sec_desc.acl", 22) == 0 ||
2229             StrnCaseCmp(name, "system.nt_sec_desc.acl+", 23) == 0) {
2230
2231                 /* Yup. */
2232                 ret = cacl_set(context, talloc_tos(), srv->cli,
2233                                ipc_srv->cli, &ipc_srv->pol, path,
2234                                CONST_DISCARD(char *, name) + 19,
2235                                SMBC_XATTR_MODE_REMOVE, 0);
2236                 TALLOC_FREE(frame);
2237                 return ret;
2238         }
2239
2240         /* Unsupported attribute name */
2241         errno = EINVAL;
2242         TALLOC_FREE(frame);
2243         return -1;
2244 }
2245
2246 int
2247 SMBC_listxattr_ctx(SMBCCTX *context,
2248                    const char *fname,
2249                    char *list,
2250                    size_t size)
2251 {
2252         /*
2253          * This isn't quite what listxattr() is supposed to do.  This returns
2254          * the complete set of attribute names, always, rather than only those
2255          * attribute names which actually exist for a file.  Hmmm...
2256          */
2257         size_t retsize;
2258         const char supported_old[] =
2259                 "system.*\0"
2260                 "system.*+\0"
2261                 "system.nt_sec_desc.revision\0"
2262                 "system.nt_sec_desc.owner\0"
2263                 "system.nt_sec_desc.owner+\0"
2264                 "system.nt_sec_desc.group\0"
2265                 "system.nt_sec_desc.group+\0"
2266                 "system.nt_sec_desc.acl.*\0"
2267                 "system.nt_sec_desc.acl\0"
2268                 "system.nt_sec_desc.acl+\0"
2269                 "system.nt_sec_desc.*\0"
2270                 "system.nt_sec_desc.*+\0"
2271                 "system.dos_attr.*\0"
2272                 "system.dos_attr.mode\0"
2273                 "system.dos_attr.c_time\0"
2274                 "system.dos_attr.a_time\0"
2275                 "system.dos_attr.m_time\0"
2276                 ;
2277         const char supported_new[] =
2278                 "system.*\0"
2279                 "system.*+\0"
2280                 "system.nt_sec_desc.revision\0"
2281                 "system.nt_sec_desc.owner\0"
2282                 "system.nt_sec_desc.owner+\0"
2283                 "system.nt_sec_desc.group\0"
2284                 "system.nt_sec_desc.group+\0"
2285                 "system.nt_sec_desc.acl.*\0"
2286                 "system.nt_sec_desc.acl\0"
2287                 "system.nt_sec_desc.acl+\0"
2288                 "system.nt_sec_desc.*\0"
2289                 "system.nt_sec_desc.*+\0"
2290                 "system.dos_attr.*\0"
2291                 "system.dos_attr.mode\0"
2292                 "system.dos_attr.create_time\0"
2293                 "system.dos_attr.access_time\0"
2294                 "system.dos_attr.write_time\0"
2295                 "system.dos_attr.change_time\0"
2296                 ;
2297         const char * supported;
2298
2299         if (context->internal->full_time_names) {
2300                 supported = supported_new;
2301                 retsize = sizeof(supported_new);
2302         } else {
2303                 supported = supported_old;
2304                 retsize = sizeof(supported_old);
2305         }
2306
2307         if (size == 0) {
2308                 return retsize;
2309         }
2310
2311         if (retsize > size) {
2312                 errno = ERANGE;
2313                 return -1;
2314         }
2315
2316         /* this can't be strcpy() because there are embedded null characters */
2317         memcpy(list, supported, retsize);
2318         return retsize;
2319 }