]> git.samba.org - sfrench/samba-autobuild/.git/blob - source4/utils/getntacl.c
c3b704d85e198385c021736e4aa6f3d61ede8730
[sfrench/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 "libcli/security/security.h"
26 #include "librpc/gen_ndr/ndr_xattr.h"
27
28 #if HAVE_XATTR_SUPPORT  
29
30 static void ntacl_print_debug_helper(struct ndr_print *ndr, const char *format, ...) PRINTF_ATTRIBUTE(2,3);
31
32 static void ntacl_print_debug_helper(struct ndr_print *ndr, const char *format, ...)
33 {
34         va_list ap;
35         char *s = NULL;
36         int i;
37
38         va_start(ap, format);
39         vasprintf(&s, format, ap);
40         va_end(ap);
41
42         for (i=0;i<ndr->depth;i++) {
43                 printf("    ");
44         }
45
46         printf("%s\n", s);
47         free(s);
48 }
49
50 static NTSTATUS get_ntacl(char *filename, struct xattr_NTACL **ntacl, 
51                           ssize_t *ntacl_len)
52 {
53         DATA_BLOB blob;
54         ssize_t size;
55         NTSTATUS result;
56         struct ndr_pull *ndr;
57         struct ndr_print *pr;
58
59         *ntacl = talloc(NULL, struct xattr_NTACL);
60
61         size = getxattr(filename, XATTR_NTACL_NAME, NULL, 0);
62
63         if (size < 0) {
64                 fprintf(stderr, "get_ntacl: %s\n", strerror(errno));
65                 return NT_STATUS_INTERNAL_ERROR;
66         }
67
68         blob.data = talloc_size(*ntacl, size);
69         blob.length = getxattr(filename, XATTR_NTACL_NAME, blob.data, size);
70
71         if (blob.length < 0) {
72                 fprintf(stderr, "get_ntacl: %s\n", strerror(errno));
73                 return NT_STATUS_INTERNAL_ERROR;
74         }
75
76         ndr = ndr_pull_init_blob(&blob, NULL);
77
78         result = ndr_pull_xattr_NTACL(ndr, NDR_SCALARS|NDR_BUFFERS, *ntacl);
79
80         if (NT_STATUS_IS_OK(result)) {
81                 pr = talloc(*ntacl, struct ndr_print);
82                 pr->print = ntacl_print_debug_helper;
83                 pr->depth = 0;
84                 pr->flags = 0;
85                 
86                 ndr_print_xattr_NTACL(pr, filename, *ntacl);
87         }
88
89         return result;
90 }
91
92 static void print_ntacl(struct xattr_NTACL *ntacl)
93 {
94 }
95
96 int main(int argc, char *argv[])
97 {
98         struct xattr_NTACL *ntacl;
99         ssize_t ntacl_len;
100
101         if (argc != 2) {
102                 fprintf(stderr, "Usage: getntacl FILENAME\n");
103                 return 1;
104         }
105
106
107         get_ntacl(argv[1], &ntacl, &ntacl_len);
108
109         print_ntacl(ntacl);
110
111         return 0;
112 }
113
114 #else
115
116 int main(int argc, char *argv[])
117 {
118         printf("getntacl: not compiled with xattr support!\n");
119         return 1;
120
121 }
122
123 #endif