r23792: convert Samba4 to GPLv3
[kai/samba.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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "librpc/gen_ndr/ndr_xattr.h"
25 #include "lib/util/wrap_xattr.h"
26
27 static void ntacl_print_debug_helper(struct ndr_print *ndr, const char *format, ...) PRINTF_ATTRIBUTE(2,3);
28
29 static void ntacl_print_debug_helper(struct ndr_print *ndr, const char *format, ...)
30 {
31         va_list ap;
32         char *s = NULL;
33         int i;
34
35         va_start(ap, format);
36         vasprintf(&s, format, ap);
37         va_end(ap);
38
39         for (i=0;i<ndr->depth;i++) {
40                 printf("    ");
41         }
42
43         printf("%s\n", s);
44         free(s);
45 }
46
47 static NTSTATUS get_ntacl(char *filename, struct xattr_NTACL **ntacl, 
48                           ssize_t *ntacl_len)
49 {
50         DATA_BLOB blob;
51         ssize_t size;
52         NTSTATUS result;
53         struct ndr_pull *ndr;
54         struct ndr_print *pr;
55
56         *ntacl = talloc(NULL, struct xattr_NTACL);
57
58         size = wrap_getxattr(filename, XATTR_NTACL_NAME, NULL, 0);
59
60         if (size < 0) {
61                 fprintf(stderr, "get_ntacl: %s\n", strerror(errno));
62                 return NT_STATUS_INTERNAL_ERROR;
63         }
64
65         blob.data = talloc_size(*ntacl, size);
66         size = wrap_getxattr(filename, XATTR_NTACL_NAME, blob.data, size);
67         if (size < 0) {
68                 fprintf(stderr, "get_ntacl: %s\n", strerror(errno));
69                 return NT_STATUS_INTERNAL_ERROR;
70         }
71         blob.length = size;
72
73         ndr = ndr_pull_init_blob(&blob, NULL);
74
75         result = ndr_pull_xattr_NTACL(ndr, NDR_SCALARS|NDR_BUFFERS, *ntacl);
76
77         if (NT_STATUS_IS_OK(result)) {
78                 pr = talloc(*ntacl, struct ndr_print);
79                 pr->print = ntacl_print_debug_helper;
80                 pr->depth = 0;
81                 pr->flags = 0;
82                 
83                 ndr_print_xattr_NTACL(pr, filename, *ntacl);
84         }
85
86         return result;
87 }
88
89 static void print_ntacl(struct xattr_NTACL *ntacl)
90 {
91 }
92
93 int main(int argc, char *argv[])
94 {
95         struct xattr_NTACL *ntacl;
96         ssize_t ntacl_len;
97
98         if (argc != 2) {
99                 fprintf(stderr, "Usage: getntacl FILENAME\n");
100                 return 1;
101         }
102
103
104         get_ntacl(argv[1], &ntacl, &ntacl_len);
105
106         print_ntacl(ntacl);
107
108         return 0;
109 }