summaryrefslogtreecommitdiff
path: root/source4/libcli/raw/rawioctl.c
blob: 28b58ff2a39dfc94f1e22ef46c08721844aed706 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* 
   Unix SMB/CIFS implementation.
   client file operations
   Copyright (C) Andrew Tridgell 2003
   Copyright (C) James J Myers 2003 <myersjj@samba.org>
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "libcli/raw/libcliraw.h"

#define SETUP_REQUEST(cmd, wct, buflen) do { \
	req = smbcli_request_setup(tree, cmd, wct, buflen); \
	if (!req) return NULL; \
} while (0)

/* 
   send a raw smb ioctl - async send
*/
static struct smbcli_request *smb_raw_smbioctl_send(struct smbcli_tree *tree, 
						 union smb_ioctl *parms)
{
	struct smbcli_request *req; 

	SETUP_REQUEST(SMBioctl, 3, 0);

	SSVAL(req->out.vwv, VWV(0), parms->ioctl.file.fnum);
	SIVAL(req->out.vwv, VWV(1), parms->ioctl.in.request);

	if (!smbcli_request_send(req)) {
		smbcli_request_destroy(req);
		return NULL;
	}

	return req;
}

/* 
   send a raw smb ioctl - async recv
*/
static NTSTATUS smb_raw_smbioctl_recv(struct smbcli_request *req, 
				      TALLOC_CTX *mem_ctx, 
				      union smb_ioctl *parms)
{
	if (!smbcli_request_receive(req) ||
	    smbcli_request_is_error(req)) {
		return smbcli_request_destroy(req);
	}

	parms->ioctl.out.blob = smbcli_req_pull_blob(req, mem_ctx, req->in.data, -1);
	return smbcli_request_destroy(req);
}



/****************************************************************************
NT ioctl (async send)
****************************************************************************/
static struct smbcli_request *smb_raw_ntioctl_send(struct smbcli_tree *tree, 
						union smb_ioctl *parms)
{
	struct smb_nttrans nt;
	uint16_t setup[4];

	nt.in.max_setup = 0;
	nt.in.max_param = 0;
	nt.in.max_data = 0;
	nt.in.setup_count = 4;
	nt.in.setup = setup;
	SIVAL(setup, 0, parms->ntioctl.in.function);
	SSVAL(setup, 4, parms->ntioctl.file.fnum);
	SCVAL(setup, 6, parms->ntioctl.in.fsctl);
	SCVAL(setup, 7, parms->ntioctl.in.filter);
	nt.in.function = NT_TRANSACT_IOCTL;
	nt.in.params = data_blob(NULL, 0);
	nt.in.data = data_blob(NULL, 0);

	return smb_raw_nttrans_send(tree, &nt);
}

/****************************************************************************
NT ioctl (async recv)
****************************************************************************/
static NTSTATUS smb_raw_ntioctl_recv(struct smbcli_request *req, 
				     TALLOC_CTX *mem_ctx,
				     union smb_ioctl *parms)
{
	if (!smbcli_request_receive(req) ||
	    smbcli_request_is_error(req)) {
		return smbcli_request_destroy(req);
	}

	parms->ntioctl.out.blob = smbcli_req_pull_blob(req, mem_ctx, req->in.data, -1);
	return smbcli_request_destroy(req);
}


/* 
   send a raw ioctl - async send
*/
struct smbcli_request *smb_raw_ioctl_send(struct smbcli_tree *tree, union smb_ioctl *parms)
{
	struct smbcli_request *req = NULL;
	
	switch (parms->generic.level) {
	case RAW_IOCTL_IOCTL:
		req = smb_raw_smbioctl_send(tree, parms);
		break;
		
	case RAW_IOCTL_NTIOCTL:
		req = smb_raw_ntioctl_send(tree, parms);
		break;
	}

	return req;
}

/* 
   recv a raw ioctl - async recv
*/
NTSTATUS smb_raw_ioctl_recv(struct smbcli_request *req,
			    TALLOC_CTX *mem_ctx, union smb_ioctl *parms)
{
	switch (parms->generic.level) {
	case RAW_IOCTL_IOCTL:
		return smb_raw_smbioctl_recv(req, mem_ctx, parms);
		
	case RAW_IOCTL_NTIOCTL:
		return smb_raw_ntioctl_recv(req, mem_ctx, parms);
	}
	return NT_STATUS_INVALID_LEVEL;
}

/* 
   send a raw ioctl - sync interface
*/
NTSTATUS smb_raw_ioctl(struct smbcli_tree *tree, 
		TALLOC_CTX *mem_ctx, union smb_ioctl *parms)
{
	struct smbcli_request *req;
	req = smb_raw_ioctl_send(tree, parms);
	return smb_raw_ioctl_recv(req, mem_ctx, parms);
}