27629ea96d4a0a35031bb78c933b11547291721b
[samba.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
22 /****************************************************************************
23   query the security descriptor for a open file
24  ****************************************************************************/
25 SEC_DESC *cli_query_secdesc(struct cli_state *cli, int fnum, 
26                             TALLOC_CTX *mem_ctx)
27 {
28         char param[8];
29         char *rparam=NULL, *rdata=NULL;
30         unsigned int rparam_count=0, rdata_count=0;
31         prs_struct pd;
32         BOOL pd_initialized = False;
33         SEC_DESC *psd = NULL;
34
35         SIVAL(param, 0, fnum);
36         SIVAL(param, 4, 0x7);
37
38         if (!cli_send_nt_trans(cli, 
39                                NT_TRANSACT_QUERY_SECURITY_DESC, 
40                                0, 
41                                NULL, 0, 0,
42                                param, 8, 4,
43                                NULL, 0, 0x10000)) {
44                 DEBUG(1,("Failed to send NT_TRANSACT_QUERY_SECURITY_DESC\n"));
45                 goto cleanup;
46         }
47
48
49         if (!cli_receive_nt_trans(cli, 
50                                   &rparam, &rparam_count,
51                                   &rdata, &rdata_count)) {
52                 DEBUG(1,("Failed to recv NT_TRANSACT_QUERY_SECURITY_DESC\n"));
53                 goto cleanup;
54         }
55
56         if (cli_is_error(cli))
57                 goto cleanup;
58
59         if (!prs_init(&pd, rdata_count, mem_ctx, UNMARSHALL)) {
60                 goto cleanup;
61         }
62         pd_initialized = True;
63         prs_copy_data_in(&pd, rdata, rdata_count);
64         prs_set_offset(&pd,0);
65
66         if (!sec_io_desc("sd data", &psd, &pd, 1)) {
67                 DEBUG(1,("Failed to parse secdesc\n"));
68                 goto cleanup;
69         }
70
71  cleanup:
72
73         SAFE_FREE(rparam);
74         SAFE_FREE(rdata);
75
76         if (pd_initialized)
77                 prs_mem_free(&pd);
78         return psd;
79 }
80
81 /****************************************************************************
82   set the security descriptor for a open file
83  ****************************************************************************/
84 BOOL cli_set_secdesc(struct cli_state *cli, int fnum, SEC_DESC *sd)
85 {
86         char param[8];
87         char *rparam=NULL, *rdata=NULL;
88         unsigned int rparam_count=0, rdata_count=0;
89         uint32 sec_info = 0;
90         TALLOC_CTX *mem_ctx;
91         prs_struct pd;
92         BOOL ret = False;
93
94         if ((mem_ctx = talloc_init("cli_set_secdesc")) == NULL) {
95                 DEBUG(0,("talloc_init failed.\n"));
96                 goto cleanup;
97         }
98
99         prs_init(&pd, 0, mem_ctx, MARSHALL);
100         prs_give_memory(&pd, NULL, 0, True);
101
102         if (!sec_io_desc("sd data", &sd, &pd, 1)) {
103                 DEBUG(1,("Failed to marshall secdesc\n"));
104                 goto cleanup;
105         }
106
107         SIVAL(param, 0, fnum);
108
109         if (sd->dacl)
110                 sec_info |= DACL_SECURITY_INFORMATION;
111         if (sd->owner_sid)
112                 sec_info |= OWNER_SECURITY_INFORMATION;
113         if (sd->group_sid)
114                 sec_info |= GROUP_SECURITY_INFORMATION;
115         SSVAL(param, 4, sec_info);
116
117         if (!cli_send_nt_trans(cli, 
118                                NT_TRANSACT_SET_SECURITY_DESC, 
119                                0, 
120                                NULL, 0, 0,
121                                param, 8, 0,
122                                prs_data_p(&pd), prs_offset(&pd), 0)) {
123                 DEBUG(1,("Failed to send NT_TRANSACT_SET_SECURITY_DESC\n"));
124                 goto cleanup;
125         }
126
127
128         if (!cli_receive_nt_trans(cli, 
129                                   &rparam, &rparam_count,
130                                   &rdata, &rdata_count)) {
131                 DEBUG(1,("NT_TRANSACT_SET_SECURITY_DESC failed\n"));
132                 goto cleanup;
133         }
134
135         ret = True;
136
137   cleanup:
138
139         SAFE_FREE(rparam);
140         SAFE_FREE(rdata);
141
142         talloc_destroy(mem_ctx);
143
144         prs_mem_free(&pd);
145         return ret;
146 }