s3:libsmb: add cli_{query,set}_security_descriptor() which take sec_info flags
[bbaumbach/samba-autobuild/.git] / source3 / libsmb / clisecdesc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client security descriptor functions
4    Copyright (C) Andrew Tridgell 2000
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "libsmb/libsmb.h"
22 #include "../libcli/security/secdesc.h"
23
24 NTSTATUS cli_query_security_descriptor(struct cli_state *cli,
25                                        uint16_t fnum,
26                                        uint32_t sec_info,
27                                        TALLOC_CTX *mem_ctx,
28                                        struct security_descriptor **sd)
29 {
30         uint8_t param[8];
31         uint8_t *rdata=NULL;
32         uint32_t rdata_count=0;
33         NTSTATUS status;
34         struct security_descriptor *lsd;
35
36         SIVAL(param, 0, fnum);
37         SIVAL(param, 4, sec_info);
38
39         status = cli_trans(talloc_tos(), cli, SMBnttrans,
40                            NULL, -1, /* name, fid */
41                            NT_TRANSACT_QUERY_SECURITY_DESC, 0, /* function, flags */
42                            NULL, 0, 0, /* setup, length, max */
43                            param, 8, 4, /* param, length, max */
44                            NULL, 0, 0x10000, /* data, length, max */
45                            NULL,             /* recv_flags2 */
46                            NULL, 0, NULL, /* rsetup, length */
47                            NULL, 0, NULL,
48                            &rdata, 0, &rdata_count);
49
50         if (!NT_STATUS_IS_OK(status)) {
51                 DEBUG(1, ("NT_TRANSACT_QUERY_SECURITY_DESC failed: %s\n",
52                           nt_errstr(status)));
53                 goto cleanup;
54         }
55
56         status = unmarshall_sec_desc(mem_ctx, (uint8 *)rdata, rdata_count,
57                                      &lsd);
58         if (!NT_STATUS_IS_OK(status)) {
59                 DEBUG(10, ("unmarshall_sec_desc failed: %s\n",
60                            nt_errstr(status)));
61                 goto cleanup;
62         }
63
64         if (sd != NULL) {
65                 *sd = lsd;
66         } else {
67                 TALLOC_FREE(lsd);
68         }
69
70  cleanup:
71
72         TALLOC_FREE(rdata);
73
74         return status;
75 }
76
77 NTSTATUS cli_query_secdesc(struct cli_state *cli, uint16_t fnum,
78                            TALLOC_CTX *mem_ctx, struct security_descriptor **sd)
79 {
80         uint32_t sec_info = SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL;
81
82         return cli_query_security_descriptor(cli, fnum, sec_info, mem_ctx, sd);
83 }
84
85 /****************************************************************************
86   set the security descriptor for a open file
87  ****************************************************************************/
88 NTSTATUS cli_set_security_descriptor(struct cli_state *cli,
89                                      uint16_t fnum,
90                                      uint32_t sec_info,
91                                      const struct security_descriptor *sd)
92 {
93         uint8_t param[8];
94         uint8 *data;
95         size_t len;
96         NTSTATUS status;
97
98         status = marshall_sec_desc(talloc_tos(), sd, &data, &len);
99         if (!NT_STATUS_IS_OK(status)) {
100                 DEBUG(10, ("marshall_sec_desc failed: %s\n",
101                            nt_errstr(status)));
102                 return status;
103         }
104
105         SIVAL(param, 0, fnum);
106         SIVAL(param, 4, sec_info);
107
108         status = cli_trans(talloc_tos(), cli, SMBnttrans,
109                            NULL, -1, /* name, fid */
110                            NT_TRANSACT_SET_SECURITY_DESC, 0,
111                            NULL, 0, 0, /* setup */
112                            param, 8, 0, /* param */
113                            data, len, 0, /* data */
114                            NULL,         /* recv_flags2 */
115                            NULL, 0, NULL, /* rsetup */
116                            NULL, 0, NULL, /* rparam */
117                            NULL, 0, NULL); /* rdata */
118         TALLOC_FREE(data);
119         if (!NT_STATUS_IS_OK(status)) {
120                 DEBUG(1, ("Failed to send NT_TRANSACT_SET_SECURITY_DESC: %s\n",
121                           nt_errstr(status)));
122         }
123         return status;
124 }
125
126 NTSTATUS cli_set_secdesc(struct cli_state *cli, uint16_t fnum,
127                          const struct security_descriptor *sd)
128 {
129         uint32_t sec_info = 0;
130
131         if (sd->dacl || (sd->type & SEC_DESC_DACL_PRESENT)) {
132                 sec_info |= SECINFO_DACL;
133         }
134         if (sd->sacl || (sd->type & SEC_DESC_SACL_PRESENT)) {
135                 sec_info |= SECINFO_SACL;
136         }
137         if (sd->owner_sid) {
138                 sec_info |= SECINFO_OWNER;
139         }
140         if (sd->group_sid) {
141                 sec_info |= SECINFO_GROUP;
142         }
143
144         return cli_set_security_descriptor(cli, fnum, sec_info, sd);
145 }