sync 3.0 into HEAD for the last time
[amitay/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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 /****************************************************************************
24   query the security descriptor for a open file
25  ****************************************************************************/
26 SEC_DESC *cli_query_secdesc(struct cli_state *cli, int fnum, 
27                             TALLOC_CTX *mem_ctx)
28 {
29         char param[8];
30         char *rparam=NULL, *rdata=NULL;
31         unsigned int rparam_count=0, rdata_count=0;
32         prs_struct pd;
33         SEC_DESC *psd = NULL;
34
35         SIVAL(param, 0, fnum);
36         SSVAL(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         prs_init(&pd, rdata_count, mem_ctx, UNMARSHALL);
57         prs_copy_data_in(&pd, rdata, rdata_count);
58         prs_set_offset(&pd,0);
59
60         if (!sec_io_desc("sd data", &psd, &pd, 1)) {
61                 DEBUG(1,("Failed to parse secdesc\n"));
62                 goto cleanup;
63         }
64
65  cleanup:
66
67         SAFE_FREE(rparam);
68         SAFE_FREE(rdata);
69
70         prs_mem_free(&pd);
71         return psd;
72 }
73
74 /****************************************************************************
75   set the security descriptor for a open file
76  ****************************************************************************/
77 BOOL cli_set_secdesc(struct cli_state *cli, int fnum, SEC_DESC *sd)
78 {
79         char param[8];
80         char *rparam=NULL, *rdata=NULL;
81         unsigned int rparam_count=0, rdata_count=0;
82         uint32 sec_info = 0;
83         TALLOC_CTX *mem_ctx;
84         prs_struct pd;
85         BOOL ret = False;
86
87         if ((mem_ctx = talloc_init("cli_set_secdesc")) == 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
102         if (sd->off_dacl)
103                 sec_info |= DACL_SECURITY_INFORMATION;
104         if (sd->off_owner_sid)
105                 sec_info |= OWNER_SECURITY_INFORMATION;
106         if (sd->off_grp_sid)
107                 sec_info |= GROUP_SECURITY_INFORMATION;
108         SSVAL(param, 4, sec_info);
109
110         if (!cli_send_nt_trans(cli, 
111                                NT_TRANSACT_SET_SECURITY_DESC, 
112                                0, 
113                                NULL, 0, 0,
114                                param, 8, 0,
115                                prs_data_p(&pd), prs_offset(&pd), 0)) {
116                 DEBUG(1,("Failed to send NT_TRANSACT_SET_SECURITY_DESC\n"));
117                 goto cleanup;
118         }
119
120
121         if (!cli_receive_nt_trans(cli, 
122                                   &rparam, &rparam_count,
123                                   &rdata, &rdata_count)) {
124                 DEBUG(1,("NT_TRANSACT_SET_SECURITY_DESC failed\n"));
125                 goto cleanup;
126         }
127
128         ret = True;
129
130   cleanup:
131
132         SAFE_FREE(rparam);
133         SAFE_FREE(rdata);
134
135         talloc_destroy(mem_ctx);
136
137         prs_mem_free(&pd);
138         return ret;
139 }