r10913: This patch isn't as big as it looks ...
[bbaumbach/samba-autobuild/.git] / source4 / utils / getntacl.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Get NT ACLs from UNIX files.
5
6    Copyright (C) Tim Potter <tpot@samba.org> 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "librpc/gen_ndr/ndr_xattr.h"
26
27 #if HAVE_XATTR_SUPPORT  
28
29 static void ntacl_print_debug_helper(struct ndr_print *ndr, const char *format, ...) PRINTF_ATTRIBUTE(2,3);
30
31 static void ntacl_print_debug_helper(struct ndr_print *ndr, const char *format, ...)
32 {
33         va_list ap;
34         char *s = NULL;
35         int i;
36
37         va_start(ap, format);
38         vasprintf(&s, format, ap);
39         va_end(ap);
40
41         for (i=0;i<ndr->depth;i++) {
42                 printf("    ");
43         }
44
45         printf("%s\n", s);
46         free(s);
47 }
48
49 static NTSTATUS get_ntacl(char *filename, struct xattr_NTACL **ntacl, 
50                           ssize_t *ntacl_len)
51 {
52         DATA_BLOB blob;
53         ssize_t size;
54         NTSTATUS result;
55         struct ndr_pull *ndr;
56         struct ndr_print *pr;
57
58         *ntacl = talloc(NULL, struct xattr_NTACL);
59
60         size = getxattr(filename, XATTR_NTACL_NAME, NULL, 0);
61
62         if (size < 0) {
63                 fprintf(stderr, "get_ntacl: %s\n", strerror(errno));
64                 return NT_STATUS_INTERNAL_ERROR;
65         }
66
67         blob.data = talloc_size(*ntacl, size);
68         blob.length = getxattr(filename, XATTR_NTACL_NAME, blob.data, size);
69
70         if (blob.length < 0) {
71                 fprintf(stderr, "get_ntacl: %s\n", strerror(errno));
72                 return NT_STATUS_INTERNAL_ERROR;
73         }
74
75         ndr = ndr_pull_init_blob(&blob, NULL);
76
77         result = ndr_pull_xattr_NTACL(ndr, NDR_SCALARS|NDR_BUFFERS, *ntacl);
78
79         if (NT_STATUS_IS_OK(result)) {
80                 pr = talloc(*ntacl, struct ndr_print);
81                 pr->print = ntacl_print_debug_helper;
82                 pr->depth = 0;
83                 pr->flags = 0;
84                 
85                 ndr_print_xattr_NTACL(pr, filename, *ntacl);
86         }
87
88         return result;
89 }
90
91 static void print_ntacl(struct xattr_NTACL *ntacl)
92 {
93 }
94
95 int main(int argc, char *argv[])
96 {
97         struct xattr_NTACL *ntacl;
98         ssize_t ntacl_len;
99
100         if (argc != 2) {
101                 fprintf(stderr, "Usage: getntacl FILENAME\n");
102                 return 1;
103         }
104
105
106         get_ntacl(argv[1], &ntacl, &ntacl_len);
107
108         print_ntacl(ntacl);
109
110         return 0;
111 }
112
113 #else
114
115 int main(int argc, char *argv[])
116 {
117         printf("getntacl: not compiled with xattr support!\n");
118         return 1;
119
120 }
121
122 #endif