r23779: Change from v2 or later to v3 or later.
[kai/samba-autobuild/.git] / source / 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, 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         BOOL pd_initialized = False;
34         SEC_DESC *psd = NULL;
35
36         SIVAL(param, 0, fnum);
37         SIVAL(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         if (cli_is_error(cli))
58                 goto cleanup;
59
60         if (!prs_init(&pd, rdata_count, mem_ctx, UNMARSHALL)) {
61                 goto cleanup;
62         }
63         pd_initialized = True;
64         prs_copy_data_in(&pd, rdata, rdata_count);
65         prs_set_offset(&pd,0);
66
67         if (!sec_io_desc("sd data", &psd, &pd, 1)) {
68                 DEBUG(1,("Failed to parse secdesc\n"));
69                 goto cleanup;
70         }
71
72  cleanup:
73
74         SAFE_FREE(rparam);
75         SAFE_FREE(rdata);
76
77         if (pd_initialized)
78                 prs_mem_free(&pd);
79         return psd;
80 }
81
82 /****************************************************************************
83   set the security descriptor for a open file
84  ****************************************************************************/
85 BOOL cli_set_secdesc(struct cli_state *cli, int fnum, SEC_DESC *sd)
86 {
87         char param[8];
88         char *rparam=NULL, *rdata=NULL;
89         unsigned int rparam_count=0, rdata_count=0;
90         uint32 sec_info = 0;
91         TALLOC_CTX *mem_ctx;
92         prs_struct pd;
93         BOOL ret = False;
94
95         if ((mem_ctx = talloc_init("cli_set_secdesc")) == NULL) {
96                 DEBUG(0,("talloc_init failed.\n"));
97                 goto cleanup;
98         }
99
100         prs_init(&pd, 0, mem_ctx, MARSHALL);
101         prs_give_memory(&pd, NULL, 0, True);
102
103         if (!sec_io_desc("sd data", &sd, &pd, 1)) {
104                 DEBUG(1,("Failed to marshall secdesc\n"));
105                 goto cleanup;
106         }
107
108         SIVAL(param, 0, fnum);
109
110         if (sd->dacl)
111                 sec_info |= DACL_SECURITY_INFORMATION;
112         if (sd->owner_sid)
113                 sec_info |= OWNER_SECURITY_INFORMATION;
114         if (sd->group_sid)
115                 sec_info |= GROUP_SECURITY_INFORMATION;
116         SSVAL(param, 4, sec_info);
117
118         if (!cli_send_nt_trans(cli, 
119                                NT_TRANSACT_SET_SECURITY_DESC, 
120                                0, 
121                                NULL, 0, 0,
122                                param, 8, 0,
123                                prs_data_p(&pd), prs_offset(&pd), 0)) {
124                 DEBUG(1,("Failed to send NT_TRANSACT_SET_SECURITY_DESC\n"));
125                 goto cleanup;
126         }
127
128
129         if (!cli_receive_nt_trans(cli, 
130                                   &rparam, &rparam_count,
131                                   &rdata, &rdata_count)) {
132                 DEBUG(1,("NT_TRANSACT_SET_SECURITY_DESC failed\n"));
133                 goto cleanup;
134         }
135
136         ret = True;
137
138   cleanup:
139
140         SAFE_FREE(rparam);
141         SAFE_FREE(rdata);
142
143         talloc_destroy(mem_ctx);
144
145         prs_mem_free(&pd);
146         return ret;
147 }