r2576: Some userspace tools for getting and setting ntacls via the 'security.ntacl'
[jra/samba/.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 <attr/xattr.h>
25
26 static void setntacl(char *filename, struct security_descriptor *sd)
27 {
28         NTSTATUS status;
29         struct ndr_push *ndr;
30         ssize_t result;
31
32         ndr = ndr_push_init();
33
34         status = ndr_push_security_descriptor(
35                 ndr, NDR_SCALARS|NDR_BUFFERS, sd);
36
37         result = setxattr(
38                 filename, "security.ntacl", ndr->data, ndr->offset, 0);
39
40         if (result == -1) {
41                 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
42                 exit(1);
43         }
44
45 }
46
47  int main(int argc, char **argv)
48 {
49         char line[255];
50         struct security_descriptor *sd;
51         TALLOC_CTX *mem_ctx;
52         struct security_acl *acl;
53
54         setup_logging("setntacl", DEBUG_STDOUT);
55
56         mem_ctx = talloc_init("setntacl");
57
58         sd = sd_initialise(mem_ctx);
59
60         fgets(line, sizeof(line), stdin);
61         sd->owner_sid = dom_sid_parse_talloc(mem_ctx, line);
62
63         fgets(line, sizeof(line), stdin);
64         sd->group_sid = dom_sid_parse_talloc(mem_ctx, line);
65
66         acl = talloc(mem_ctx, sizeof(struct security_acl));
67
68         acl->revision = 2;
69         acl->size = 0;
70         acl->num_aces = 0;
71         acl->aces = NULL;
72
73         while(fgets(line, sizeof(line), stdin)) {
74                 int ace_type, ace_flags;
75                 uint32 ace_mask;
76                 char sidstr[255];
77                 struct dom_sid *sid;
78                 
79                 if (sscanf(line, "%d %d 0x%x %s", &ace_type, &ace_flags,
80                            &ace_mask, sidstr) != 4) {
81                         fprintf(stderr, "invalid ACL line\ndr");
82                         return 1;
83                 }
84                 
85                 acl->aces = talloc_realloc(
86                         acl->aces, 
87                         (acl->num_aces + 1) * sizeof(struct security_ace));
88
89                 acl->aces[acl->num_aces].type = ace_type;
90                 acl->aces[acl->num_aces].flags = ace_flags;
91                 acl->aces[acl->num_aces].access_mask = ace_mask;
92
93                 sid = dom_sid_parse_talloc(mem_ctx, sidstr);
94
95                 acl->aces[acl->num_aces].trustee = *sid;
96
97                 acl->num_aces++;                
98         }
99
100         sd->dacl = acl;
101
102         setntacl(argv[1], sd);
103
104         return 0;
105 }