r4055: fixed more places to use type safe allocation macros
[kai/samba-autobuild/.git] / source4 / utils / setntacl.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Set NT ACLs on UNIX files.
5
6    Copyright (C) Tim Potter <tpot@samba.org> 2004
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
26 #if (defined(HAVE_NO_ACLS) || !defined(HAVE_XATTR_SUPPORT))
27
28 int main(int argc, char **argv)
29 {
30         printf("ACL support not compiled in.");
31         return 1;
32 }
33
34 #else
35
36 static void setntacl(char *filename, struct security_descriptor *sd)
37 {
38         NTSTATUS status;
39         struct ndr_push *ndr;
40         ssize_t result;
41
42         ndr = ndr_push_init();
43
44         status = ndr_push_security_descriptor(
45                 ndr, NDR_SCALARS|NDR_BUFFERS, sd);
46
47         result = setxattr(
48                 filename, "security.ntacl", ndr->data, ndr->offset, 0);
49
50         if (result == -1) {
51                 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
52                 exit(1);
53         }
54
55 }
56
57  int main(int argc, char **argv)
58 {
59         char line[255];
60         struct security_descriptor *sd;
61         TALLOC_CTX *mem_ctx;
62         struct security_acl *acl;
63
64         static_init_ntacl;
65
66         setup_logging("setntacl", DEBUG_STDOUT);
67
68         mem_ctx = talloc_init("setntacl");
69
70         sd = sd_initialise(mem_ctx);
71
72         fgets(line, sizeof(line), stdin);
73         sd->owner_sid = dom_sid_parse_talloc(mem_ctx, line);
74
75         fgets(line, sizeof(line), stdin);
76         sd->group_sid = dom_sid_parse_talloc(mem_ctx, line);
77
78         acl = talloc_p(mem_ctx, struct security_acl);
79
80         acl->revision = 2;
81         acl->size = 0;
82         acl->num_aces = 0;
83         acl->aces = NULL;
84
85         while(fgets(line, sizeof(line), stdin)) {
86                 int ace_type, ace_flags;
87                 uint32 ace_mask;
88                 char sidstr[255];
89                 struct dom_sid *sid;
90                 
91                 if (sscanf(line, "%d %d 0x%x %s", &ace_type, &ace_flags,
92                            &ace_mask, sidstr) != 4) {
93                         fprintf(stderr, "invalid ACL line\ndr");
94                         return 1;
95                 }
96                 
97                 acl->aces = talloc_realloc(mem_ctx, acl->aces,
98                                 (acl->num_aces + 1) * sizeof(struct security_ace));
99
100                 acl->aces[acl->num_aces].type = ace_type;
101                 acl->aces[acl->num_aces].flags = ace_flags;
102                 acl->aces[acl->num_aces].access_mask = ace_mask;
103
104                 sid = dom_sid_parse_talloc(mem_ctx, sidstr);
105
106                 acl->aces[acl->num_aces].trustee = *sid;
107
108                 acl->num_aces++;                
109         }
110
111         sd->dacl = acl;
112
113         setntacl(argv[1], sd);
114
115         return 0;
116 }
117
118 #endif /* HAVE_NO_ACLS */