r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[sharpe/samba-autobuild/.git] / source3 / rpc_client / ndr.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    libndr interface
5
6    Copyright (C) Jelmer Vernooij 2006
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23
24
25 NTSTATUS cli_do_rpc_ndr(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
26                         int p_idx, int opnum, void *data, 
27                         ndr_pull_flags_fn_t pull_fn, ndr_push_flags_fn_t push_fn)
28 {
29         prs_struct q_ps, r_ps;
30         struct ndr_pull *pull;
31         DATA_BLOB blob;
32         struct ndr_push *push;
33         NTSTATUS status;
34
35         SMB_ASSERT(cli->pipe_idx == p_idx);
36
37         push = ndr_push_init_ctx(mem_ctx);
38         if (!push) {
39                 return NT_STATUS_NO_MEMORY;
40         }
41
42         status = push_fn(push, NDR_IN, data);
43         if (!NT_STATUS_IS_OK(status)) {
44                 return status;
45         }
46
47         blob = ndr_push_blob(push);
48
49         if (!prs_init_data_blob(&q_ps, &blob, mem_ctx)) {
50                 return NT_STATUS_NO_MEMORY;
51         }
52
53         talloc_free(push);
54
55         if (!prs_init( &r_ps, 0, mem_ctx, UNMARSHALL )) {
56                 prs_mem_free( &q_ps );
57                 return NT_STATUS_NO_MEMORY;
58         }
59         
60         status = rpc_api_pipe_req(cli, opnum, &q_ps, &r_ps); 
61
62         prs_mem_free( &q_ps );
63
64         if (!NT_STATUS_IS_OK(status)) {
65                 prs_mem_free( &r_ps );
66                 return status;
67         }
68
69         if (!prs_data_blob(&r_ps, &blob, mem_ctx)) {
70                 prs_mem_free( &r_ps );
71                 return NT_STATUS_NO_MEMORY;
72         }
73
74         prs_mem_free( &r_ps );
75
76         pull = ndr_pull_init_blob(&blob, mem_ctx);
77         if (pull == NULL) {
78                 return NT_STATUS_NO_MEMORY;
79         }
80
81         /* have the ndr parser alloc memory for us */
82         pull->flags |= LIBNDR_FLAG_REF_ALLOC;
83         status = pull_fn(pull, NDR_OUT, data);
84         talloc_free(pull);
85
86         if (!NT_STATUS_IS_OK(status)) {
87                 return status;
88         }
89
90         return NT_STATUS_OK;
91 }