r25790: - remove some nesting by using error and out logic
[gd/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(TALLOC_CTX *mem_ctx,
48                           char *filename,
49                           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
57         *ntacl = talloc(mem_ctx, struct xattr_NTACL);
58
59         size = wrap_getxattr(filename, XATTR_NTACL_NAME, NULL, 0);
60
61         if (size < 0) {
62                 fprintf(stderr, "get_ntacl: %s\n", strerror(errno));
63                 return NT_STATUS_INTERNAL_ERROR;
64         }
65
66         blob.data = talloc_array(*ntacl, uint8_t, size);
67         size = wrap_getxattr(filename, XATTR_NTACL_NAME, blob.data, size);
68         if (size < 0) {
69                 fprintf(stderr, "get_ntacl: %s\n", strerror(errno));
70                 return NT_STATUS_INTERNAL_ERROR;
71         }
72         blob.length = size;
73
74         ndr = ndr_pull_init_blob(&blob, NULL);
75
76         result = ndr_pull_xattr_NTACL(ndr, NDR_SCALARS|NDR_BUFFERS, *ntacl);
77
78         if (!NT_STATUS_IS_OK(result)) {
79                 return result;
80         }
81
82         return NT_STATUS_OK;
83 }
84
85 static void print_ntacl(TALLOC_CTX *mem_ctx,
86                         const char *fname,
87                         struct xattr_NTACL *ntacl)
88 {
89         struct ndr_print *pr;
90
91         pr = talloc_zero(mem_ctx, struct ndr_print);
92         if (!pr) return;
93         pr->print = ntacl_print_debug_helper;
94
95         ndr_print_xattr_NTACL(pr, fname, ntacl);
96         talloc_free(pr);
97 }
98
99 int main(int argc, char *argv[])
100 {
101         NTSTATUS status;
102         struct xattr_NTACL *ntacl;
103         ssize_t ntacl_len;
104
105         if (argc != 2) {
106                 fprintf(stderr, "Usage: getntacl FILENAME\n");
107                 return 1;
108         }
109
110         status = get_ntacl(NULL, argv[1], &ntacl, &ntacl_len);
111         if (!NT_STATUS_IS_OK(status)) {
112                 fprintf(stderr, "get_ntacl failed: %s\n", nt_errstr(status));
113                 return 1;
114         }
115
116         print_ntacl(ntacl, argv[1], ntacl);
117
118         talloc_free(ntacl);
119
120         return 0;
121 }