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