Renamed formal parameter fd to fnum because we're talking about SMB file
[samba.git] / source3 / libsmb / clisecdesc.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    client security descriptor functions
5    Copyright (C) Andrew Tridgell 2000
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /****************************************************************************
25   query the security descriptor for a open file
26  ****************************************************************************/
27 SEC_DESC *cli_query_secdesc(struct cli_state *cli, int fnum, 
28                             TALLOC_CTX *mem_ctx)
29 {
30         char param[8];
31         char *rparam=NULL, *rdata=NULL;
32         int rparam_count=0, rdata_count=0;
33         prs_struct pd;
34         SEC_DESC *psd = NULL;
35
36         SIVAL(param, 0, fnum);
37         SSVAL(param, 4, 0x7);
38
39         if (!cli_send_nt_trans(cli, 
40                                NT_TRANSACT_QUERY_SECURITY_DESC, 
41                                0, 
42                                NULL, 0, 0,
43                                param, 8, 4,
44                                NULL, 0, 0x10000)) {
45                 DEBUG(1,("Failed to send NT_TRANSACT_QUERY_SECURITY_DESC\n"));
46                 goto cleanup;
47         }
48
49
50         if (!cli_receive_nt_trans(cli, 
51                                   &rparam, &rparam_count,
52                                   &rdata, &rdata_count)) {
53                 DEBUG(1,("Failed to recv NT_TRANSACT_QUERY_SECURITY_DESC\n"));
54                 goto cleanup;
55         }
56
57         prs_init(&pd, rdata_count, mem_ctx, UNMARSHALL);
58         prs_append_data(&pd, rdata, rdata_count);
59         pd.data_offset = 0;
60
61         if (!sec_io_desc("sd data", &psd, &pd, 1)) {
62                 DEBUG(1,("Failed to parse secdesc\n"));
63                 goto cleanup;
64         }
65
66  cleanup:
67
68         safe_free(rparam);
69         safe_free(rdata);
70
71         prs_mem_free(&pd);
72         return psd;
73 }
74
75 /****************************************************************************
76   set the security descriptor for a open file
77  ****************************************************************************/
78 BOOL cli_set_secdesc(struct cli_state *cli, int fnum, SEC_DESC *sd)
79 {
80         char param[8];
81         char *rparam=NULL, *rdata=NULL;
82         int rparam_count=0, rdata_count=0;
83         TALLOC_CTX *mem_ctx;
84         prs_struct pd;
85         BOOL ret = False;
86
87         if ((mem_ctx = talloc_init()) == NULL) {
88                 DEBUG(0,("talloc_init failed.\n"));
89                 goto cleanup;
90         }
91
92         prs_init(&pd, 0, mem_ctx, MARSHALL);
93         prs_give_memory(&pd, NULL, 0, True);
94
95         if (!sec_io_desc("sd data", &sd, &pd, 1)) {
96                 DEBUG(1,("Failed to marshall secdesc\n"));
97                 goto cleanup;
98         }
99
100         SIVAL(param, 0, fnum);
101         SSVAL(param, 4, 0x7);
102
103         if (!cli_send_nt_trans(cli, 
104                                NT_TRANSACT_SET_SECURITY_DESC, 
105                                0, 
106                                NULL, 0, 0,
107                                param, 8, 0,
108                                pd.data_p, pd.data_offset, 0)) {
109                 DEBUG(1,("Failed to send NT_TRANSACT_SET_SECURITY_DESC\n"));
110                 goto cleanup;
111         }
112
113
114         if (!cli_receive_nt_trans(cli, 
115                                   &rparam, &rparam_count,
116                                   &rdata, &rdata_count)) {
117                 DEBUG(1,("NT_TRANSACT_SET_SECURITY_DESC failed\n"));
118                 goto cleanup;
119         }
120
121         ret = True;
122
123   cleanup:
124
125         safe_free(rparam);
126         safe_free(rdata);
127
128         talloc_destroy(mem_ctx);
129
130         prs_mem_free(&pd);
131         return ret;
132 }