xref: /freebsd/sys/netsmb/smb_usr.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1c398230bSWarner Losh /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
4681a5bbeSBoris Popov  * Copyright (c) 2000-2001 Boris Popov
5681a5bbeSBoris Popov  * All rights reserved.
6681a5bbeSBoris Popov  *
7681a5bbeSBoris Popov  * Redistribution and use in source and binary forms, with or without
8681a5bbeSBoris Popov  * modification, are permitted provided that the following conditions
9681a5bbeSBoris Popov  * are met:
10681a5bbeSBoris Popov  * 1. Redistributions of source code must retain the above copyright
11681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer.
12681a5bbeSBoris Popov  * 2. Redistributions in binary form must reproduce the above copyright
13681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer in the
14681a5bbeSBoris Popov  *    documentation and/or other materials provided with the distribution.
15681a5bbeSBoris Popov  *
16681a5bbeSBoris Popov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17681a5bbeSBoris Popov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18681a5bbeSBoris Popov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19681a5bbeSBoris Popov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20681a5bbeSBoris Popov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21681a5bbeSBoris Popov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22681a5bbeSBoris Popov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23681a5bbeSBoris Popov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24681a5bbeSBoris Popov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25681a5bbeSBoris Popov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26681a5bbeSBoris Popov  * SUCH DAMAGE.
27681a5bbeSBoris Popov  */
28ab0de15bSDavid E. O'Brien 
29681a5bbeSBoris Popov #include <sys/param.h>
30681a5bbeSBoris Popov #include <sys/malloc.h>
31681a5bbeSBoris Popov #include <sys/kernel.h>
32681a5bbeSBoris Popov #include <sys/systm.h>
33681a5bbeSBoris Popov #include <sys/conf.h>
34681a5bbeSBoris Popov #include <sys/proc.h>
35681a5bbeSBoris Popov #include <sys/fcntl.h>
36681a5bbeSBoris Popov #include <sys/socket.h>
37681a5bbeSBoris Popov #include <sys/socketvar.h>
38681a5bbeSBoris Popov #include <sys/sysctl.h>
399f86067aSPoul-Henning Kamp #include <sys/mbuf.h>
40681a5bbeSBoris Popov 
41681a5bbeSBoris Popov #include <sys/iconv.h>
42681a5bbeSBoris Popov 
43681a5bbeSBoris Popov #include <netsmb/smb.h>
44681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
45681a5bbeSBoris Popov #include <netsmb/smb_rq.h>
46681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
47681a5bbeSBoris Popov #include <netsmb/smb_dev.h>
48681a5bbeSBoris Popov 
49681a5bbeSBoris Popov /*
50681a5bbeSBoris Popov  * helpers for nsmb device. Can be moved to the smb_dev.c file.
51681a5bbeSBoris Popov  */
52681a5bbeSBoris Popov static void smb_usr_vcspec_free(struct smb_vcspec *spec);
53681a5bbeSBoris Popov 
54681a5bbeSBoris Popov static int
smb_usr_vc2spec(struct smbioc_ossn * dp,struct smb_vcspec * spec)55681a5bbeSBoris Popov smb_usr_vc2spec(struct smbioc_ossn *dp, struct smb_vcspec *spec)
56681a5bbeSBoris Popov {
57681a5bbeSBoris Popov 	int flags = 0;
58681a5bbeSBoris Popov 
59681a5bbeSBoris Popov 	bzero(spec, sizeof(*spec));
60994ad180SBoris Popov 
61994ad180SBoris Popov #ifdef NETSMB_NO_ANON_USER
62681a5bbeSBoris Popov 	if (dp->ioc_user[0] == 0)
63681a5bbeSBoris Popov 		return EINVAL;
64994ad180SBoris Popov #endif
65994ad180SBoris Popov 
66681a5bbeSBoris Popov 	if (dp->ioc_server == NULL)
67681a5bbeSBoris Popov 		return EINVAL;
68681a5bbeSBoris Popov 	if (dp->ioc_localcs[0] == 0) {
69681a5bbeSBoris Popov 		SMBERROR("no local charset ?\n");
70681a5bbeSBoris Popov 		return EINVAL;
71681a5bbeSBoris Popov 	}
72681a5bbeSBoris Popov 
73681a5bbeSBoris Popov 	spec->sap = smb_memdupin(dp->ioc_server, dp->ioc_svlen);
74681a5bbeSBoris Popov 	if (spec->sap == NULL)
75681a5bbeSBoris Popov 		return ENOMEM;
76681a5bbeSBoris Popov 	if (dp->ioc_local) {
77681a5bbeSBoris Popov 		spec->lap = smb_memdupin(dp->ioc_local, dp->ioc_lolen);
78681a5bbeSBoris Popov 		if (spec->lap == NULL) {
79681a5bbeSBoris Popov 			smb_usr_vcspec_free(spec);
80681a5bbeSBoris Popov 			return ENOMEM;
81681a5bbeSBoris Popov 		}
82681a5bbeSBoris Popov 	}
83681a5bbeSBoris Popov 	spec->srvname = dp->ioc_srvname;
84681a5bbeSBoris Popov 	spec->pass = dp->ioc_password;
85681a5bbeSBoris Popov 	spec->domain = dp->ioc_workgroup;
86681a5bbeSBoris Popov 	spec->username = dp->ioc_user;
87681a5bbeSBoris Popov 	spec->mode = dp->ioc_mode;
88681a5bbeSBoris Popov 	spec->rights = dp->ioc_rights;
89681a5bbeSBoris Popov 	spec->owner = dp->ioc_owner;
90681a5bbeSBoris Popov 	spec->group = dp->ioc_group;
91681a5bbeSBoris Popov 	spec->localcs = dp->ioc_localcs;
92681a5bbeSBoris Popov 	spec->servercs = dp->ioc_servercs;
93681a5bbeSBoris Popov 	if (dp->ioc_opt & SMBVOPT_PRIVATE)
94681a5bbeSBoris Popov 		flags |= SMBV_PRIVATE;
95681a5bbeSBoris Popov 	if (dp->ioc_opt & SMBVOPT_SINGLESHARE)
96681a5bbeSBoris Popov 		flags |= SMBV_PRIVATE | SMBV_SINGLESHARE;
97681a5bbeSBoris Popov 	spec->flags = flags;
98681a5bbeSBoris Popov 	return 0;
99681a5bbeSBoris Popov }
100681a5bbeSBoris Popov 
101681a5bbeSBoris Popov static void
smb_usr_vcspec_free(struct smb_vcspec * spec)102681a5bbeSBoris Popov smb_usr_vcspec_free(struct smb_vcspec *spec)
103681a5bbeSBoris Popov {
104681a5bbeSBoris Popov 	if (spec->sap)
105681a5bbeSBoris Popov 		smb_memfree(spec->sap);
106681a5bbeSBoris Popov 	if (spec->lap)
107681a5bbeSBoris Popov 		smb_memfree(spec->lap);
108681a5bbeSBoris Popov }
109681a5bbeSBoris Popov 
110681a5bbeSBoris Popov static int
smb_usr_share2spec(struct smbioc_oshare * dp,struct smb_sharespec * spec)111681a5bbeSBoris Popov smb_usr_share2spec(struct smbioc_oshare *dp, struct smb_sharespec *spec)
112681a5bbeSBoris Popov {
113681a5bbeSBoris Popov 	bzero(spec, sizeof(*spec));
114681a5bbeSBoris Popov 	spec->mode = dp->ioc_mode;
115681a5bbeSBoris Popov 	spec->rights = dp->ioc_rights;
116681a5bbeSBoris Popov 	spec->owner = dp->ioc_owner;
117681a5bbeSBoris Popov 	spec->group = dp->ioc_group;
118681a5bbeSBoris Popov 	spec->name = dp->ioc_share;
119681a5bbeSBoris Popov 	spec->stype = dp->ioc_stype;
120681a5bbeSBoris Popov 	spec->pass = dp->ioc_password;
121681a5bbeSBoris Popov 	return 0;
122681a5bbeSBoris Popov }
123681a5bbeSBoris Popov 
124681a5bbeSBoris Popov int
smb_usr_lookup(struct smbioc_lookup * dp,struct smb_cred * scred,struct smb_vc ** vcpp,struct smb_share ** sspp)125681a5bbeSBoris Popov smb_usr_lookup(struct smbioc_lookup *dp, struct smb_cred *scred,
126681a5bbeSBoris Popov 	struct smb_vc **vcpp, struct smb_share **sspp)
127681a5bbeSBoris Popov {
128681a5bbeSBoris Popov 	struct smb_vc *vcp = NULL;
129afe09751SDavide Italiano 	struct smb_vcspec vspec;			/* XXX */
130afe09751SDavide Italiano 	struct smb_sharespec sspec, *sspecp = NULL;	/* XXX */
131681a5bbeSBoris Popov 	int error;
132681a5bbeSBoris Popov 
133681a5bbeSBoris Popov 	if (dp->ioc_level < SMBL_VC || dp->ioc_level > SMBL_SHARE)
134681a5bbeSBoris Popov 		return EINVAL;
135681a5bbeSBoris Popov 	error = smb_usr_vc2spec(&dp->ioc_ssn, &vspec);
136681a5bbeSBoris Popov 	if (error)
137681a5bbeSBoris Popov 		return error;
138681a5bbeSBoris Popov 	if (dp->ioc_flags & SMBLK_CREATE)
139681a5bbeSBoris Popov 		vspec.flags |= SMBV_CREATE;
140681a5bbeSBoris Popov 
141681a5bbeSBoris Popov 	if (dp->ioc_level >= SMBL_SHARE) {
142681a5bbeSBoris Popov 		error = smb_usr_share2spec(&dp->ioc_sh, &sspec);
143681a5bbeSBoris Popov 		if (error)
144681a5bbeSBoris Popov 			goto out;
145681a5bbeSBoris Popov 		sspecp = &sspec;
146681a5bbeSBoris Popov 	}
147681a5bbeSBoris Popov 	error = smb_sm_lookup(&vspec, sspecp, scred, &vcp);
148681a5bbeSBoris Popov 	if (error == 0) {
149681a5bbeSBoris Popov 		*vcpp = vcp;
150681a5bbeSBoris Popov 		*sspp = vspec.ssp;
151681a5bbeSBoris Popov 	}
152681a5bbeSBoris Popov out:
153681a5bbeSBoris Popov 	smb_usr_vcspec_free(&vspec);
154681a5bbeSBoris Popov 	return error;
155681a5bbeSBoris Popov }
156681a5bbeSBoris Popov 
157681a5bbeSBoris Popov /*
158681a5bbeSBoris Popov  * Connect to the resource specified by smbioc_ossn structure.
159681a5bbeSBoris Popov  * It may either find an existing connection or try to establish a new one.
160a4641f4eSPedro F. Giffuni  * If no errors occurred smb_vc returned locked and referenced.
161681a5bbeSBoris Popov  */
162681a5bbeSBoris Popov int
smb_usr_opensession(struct smbioc_ossn * dp,struct smb_cred * scred,struct smb_vc ** vcpp)163681a5bbeSBoris Popov smb_usr_opensession(struct smbioc_ossn *dp, struct smb_cred *scred,
164681a5bbeSBoris Popov 	struct smb_vc **vcpp)
165681a5bbeSBoris Popov {
166681a5bbeSBoris Popov 	struct smb_vc *vcp = NULL;
167681a5bbeSBoris Popov 	struct smb_vcspec vspec;
168681a5bbeSBoris Popov 	int error;
169681a5bbeSBoris Popov 
170681a5bbeSBoris Popov 	error = smb_usr_vc2spec(dp, &vspec);
171681a5bbeSBoris Popov 	if (error)
172681a5bbeSBoris Popov 		return error;
173681a5bbeSBoris Popov 	if (dp->ioc_opt & SMBVOPT_CREATE)
174681a5bbeSBoris Popov 		vspec.flags |= SMBV_CREATE;
175681a5bbeSBoris Popov 
176681a5bbeSBoris Popov 	error = smb_sm_lookup(&vspec, NULL, scred, &vcp);
177681a5bbeSBoris Popov 	smb_usr_vcspec_free(&vspec);
178681a5bbeSBoris Popov 	return error;
179681a5bbeSBoris Popov }
180681a5bbeSBoris Popov 
181681a5bbeSBoris Popov int
smb_usr_openshare(struct smb_vc * vcp,struct smbioc_oshare * dp,struct smb_cred * scred,struct smb_share ** sspp)182681a5bbeSBoris Popov smb_usr_openshare(struct smb_vc *vcp, struct smbioc_oshare *dp,
183681a5bbeSBoris Popov 	struct smb_cred *scred, struct smb_share **sspp)
184681a5bbeSBoris Popov {
185681a5bbeSBoris Popov 	struct smb_share *ssp;
186681a5bbeSBoris Popov 	struct smb_sharespec shspec;
187681a5bbeSBoris Popov 	int error;
188681a5bbeSBoris Popov 
189681a5bbeSBoris Popov 	error = smb_usr_share2spec(dp, &shspec);
190681a5bbeSBoris Popov 	if (error)
191681a5bbeSBoris Popov 		return error;
192681a5bbeSBoris Popov 	error = smb_vc_lookupshare(vcp, &shspec, scred, &ssp);
193681a5bbeSBoris Popov 	if (error == 0) {
194681a5bbeSBoris Popov 		*sspp = ssp;
195681a5bbeSBoris Popov 		return 0;
196681a5bbeSBoris Popov 	}
197681a5bbeSBoris Popov 	if ((dp->ioc_opt & SMBSOPT_CREATE) == 0)
198681a5bbeSBoris Popov 		return error;
199681a5bbeSBoris Popov 	error = smb_share_create(vcp, &shspec, scred, &ssp);
200681a5bbeSBoris Popov 	if (error)
201681a5bbeSBoris Popov 		return error;
202681a5bbeSBoris Popov 	error = smb_smb_treeconnect(ssp, scred);
203681a5bbeSBoris Popov 	if (error) {
204681a5bbeSBoris Popov 		smb_share_put(ssp, scred);
205681a5bbeSBoris Popov 	} else
206681a5bbeSBoris Popov 		*sspp = ssp;
207681a5bbeSBoris Popov 	return error;
208681a5bbeSBoris Popov }
209681a5bbeSBoris Popov 
210681a5bbeSBoris Popov int
smb_usr_simplerequest(struct smb_share * ssp,struct smbioc_rq * dp,struct smb_cred * scred)211681a5bbeSBoris Popov smb_usr_simplerequest(struct smb_share *ssp, struct smbioc_rq *dp,
212681a5bbeSBoris Popov 	struct smb_cred *scred)
213681a5bbeSBoris Popov {
214afe09751SDavide Italiano 	struct smb_rq *rqp;
215681a5bbeSBoris Popov 	struct mbchain *mbp;
216681a5bbeSBoris Popov 	struct mdchain *mdp;
217681a5bbeSBoris Popov 	u_int8_t wc;
218681a5bbeSBoris Popov 	u_int16_t bc;
219681a5bbeSBoris Popov 	int error;
220681a5bbeSBoris Popov 
221681a5bbeSBoris Popov 	switch (dp->ioc_cmd) {
222681a5bbeSBoris Popov 	    case SMB_COM_TRANSACTION2:
223681a5bbeSBoris Popov 	    case SMB_COM_TRANSACTION2_SECONDARY:
224681a5bbeSBoris Popov 	    case SMB_COM_CLOSE_AND_TREE_DISC:
225681a5bbeSBoris Popov 	    case SMB_COM_TREE_CONNECT:
226681a5bbeSBoris Popov 	    case SMB_COM_TREE_DISCONNECT:
227681a5bbeSBoris Popov 	    case SMB_COM_NEGOTIATE:
228681a5bbeSBoris Popov 	    case SMB_COM_SESSION_SETUP_ANDX:
229681a5bbeSBoris Popov 	    case SMB_COM_LOGOFF_ANDX:
230681a5bbeSBoris Popov 	    case SMB_COM_TREE_CONNECT_ANDX:
231681a5bbeSBoris Popov 		return EPERM;
232681a5bbeSBoris Popov 	}
233afe09751SDavide Italiano 	rqp = malloc(sizeof(struct smb_rq), M_SMBTEMP, M_WAITOK);
234681a5bbeSBoris Popov 	error = smb_rq_init(rqp, SSTOCP(ssp), dp->ioc_cmd, scred);
235afe09751SDavide Italiano 	if (error) {
236afe09751SDavide Italiano 		free(rqp, M_SMBTEMP);
237681a5bbeSBoris Popov 		return error;
238afe09751SDavide Italiano 	}
239681a5bbeSBoris Popov 	mbp = &rqp->sr_rq;
240681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
241681a5bbeSBoris Popov 	error = mb_put_mem(mbp, dp->ioc_twords, dp->ioc_twc * 2, MB_MUSER);
242681a5bbeSBoris Popov 	if (error)
243681a5bbeSBoris Popov 		goto bad;
244681a5bbeSBoris Popov 	smb_rq_wend(rqp);
245681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
246681a5bbeSBoris Popov 	error = mb_put_mem(mbp, dp->ioc_tbytes, dp->ioc_tbc, MB_MUSER);
247681a5bbeSBoris Popov 	if (error)
248681a5bbeSBoris Popov 		goto bad;
249681a5bbeSBoris Popov 	smb_rq_bend(rqp);
250681a5bbeSBoris Popov 	error = smb_rq_simple(rqp);
251681a5bbeSBoris Popov 	if (error)
252681a5bbeSBoris Popov 		goto bad;
253681a5bbeSBoris Popov 	mdp = &rqp->sr_rp;
254681a5bbeSBoris Popov 	md_get_uint8(mdp, &wc);
255681a5bbeSBoris Popov 	dp->ioc_rwc = wc;
256681a5bbeSBoris Popov 	wc *= 2;
257681a5bbeSBoris Popov 	if (wc > dp->ioc_rpbufsz) {
258681a5bbeSBoris Popov 		error = EBADRPC;
259681a5bbeSBoris Popov 		goto bad;
260681a5bbeSBoris Popov 	}
261681a5bbeSBoris Popov 	error = md_get_mem(mdp, dp->ioc_rpbuf, wc, MB_MUSER);
262681a5bbeSBoris Popov 	if (error)
263681a5bbeSBoris Popov 		goto bad;
264681a5bbeSBoris Popov 	md_get_uint16le(mdp, &bc);
265681a5bbeSBoris Popov 	if ((wc + bc) > dp->ioc_rpbufsz) {
266681a5bbeSBoris Popov 		error = EBADRPC;
267681a5bbeSBoris Popov 		goto bad;
268681a5bbeSBoris Popov 	}
269681a5bbeSBoris Popov 	dp->ioc_rbc = bc;
270681a5bbeSBoris Popov 	error = md_get_mem(mdp, dp->ioc_rpbuf + wc, bc, MB_MUSER);
271681a5bbeSBoris Popov bad:
272681a5bbeSBoris Popov 	dp->ioc_errclass = rqp->sr_errclass;
273681a5bbeSBoris Popov 	dp->ioc_serror = rqp->sr_serror;
274681a5bbeSBoris Popov 	dp->ioc_error = rqp->sr_error;
275681a5bbeSBoris Popov 	smb_rq_done(rqp);
276afe09751SDavide Italiano 	free(rqp, M_SMBTEMP);
277681a5bbeSBoris Popov 	return error;
278681a5bbeSBoris Popov 
279681a5bbeSBoris Popov }
280681a5bbeSBoris Popov 
281681a5bbeSBoris Popov static int
smb_cpdatain(struct mbchain * mbp,int len,caddr_t data)282681a5bbeSBoris Popov smb_cpdatain(struct mbchain *mbp, int len, caddr_t data)
283681a5bbeSBoris Popov {
284681a5bbeSBoris Popov 	int error;
285681a5bbeSBoris Popov 
286681a5bbeSBoris Popov 	if (len == 0)
287681a5bbeSBoris Popov 		return 0;
288681a5bbeSBoris Popov 	error = mb_init(mbp);
289681a5bbeSBoris Popov 	if (error)
290681a5bbeSBoris Popov 		return error;
291681a5bbeSBoris Popov 	return mb_put_mem(mbp, data, len, MB_MUSER);
292681a5bbeSBoris Popov }
293681a5bbeSBoris Popov 
294681a5bbeSBoris Popov int
smb_usr_t2request(struct smb_share * ssp,struct smbioc_t2rq * dp,struct smb_cred * scred)295681a5bbeSBoris Popov smb_usr_t2request(struct smb_share *ssp, struct smbioc_t2rq *dp,
296681a5bbeSBoris Popov 	struct smb_cred *scred)
297681a5bbeSBoris Popov {
298afe09751SDavide Italiano 	struct smb_t2rq *t2p;
299681a5bbeSBoris Popov 	struct mdchain *mdp;
300681a5bbeSBoris Popov 	int error, len;
301681a5bbeSBoris Popov 
3027d6207b7SPeter Wemm 	if (dp->ioc_setupcnt > 3)
303681a5bbeSBoris Popov 		return EINVAL;
304afe09751SDavide Italiano 	t2p = malloc(sizeof(struct smb_t2rq), M_SMBTEMP, M_WAITOK);
305681a5bbeSBoris Popov 	error = smb_t2_init(t2p, SSTOCP(ssp), dp->ioc_setup[0], scred);
306afe09751SDavide Italiano 	if (error) {
307afe09751SDavide Italiano 		free(t2p, M_SMBTEMP);
308681a5bbeSBoris Popov 		return error;
309afe09751SDavide Italiano 	}
310681a5bbeSBoris Popov 	len = t2p->t2_setupcount = dp->ioc_setupcnt;
311681a5bbeSBoris Popov 	if (len > 1)
312681a5bbeSBoris Popov 		t2p->t2_setupdata = dp->ioc_setup;
313681a5bbeSBoris Popov 	if (dp->ioc_name) {
314681a5bbeSBoris Popov 		t2p->t_name = smb_strdupin(dp->ioc_name, 128);
315681a5bbeSBoris Popov 		if (t2p->t_name == NULL) {
316681a5bbeSBoris Popov 			error = ENOMEM;
317681a5bbeSBoris Popov 			goto bad;
318681a5bbeSBoris Popov 		}
319681a5bbeSBoris Popov 	}
320681a5bbeSBoris Popov 	t2p->t2_maxscount = 0;
321681a5bbeSBoris Popov 	t2p->t2_maxpcount = dp->ioc_rparamcnt;
322681a5bbeSBoris Popov 	t2p->t2_maxdcount = dp->ioc_rdatacnt;
323681a5bbeSBoris Popov 	error = smb_cpdatain(&t2p->t2_tparam, dp->ioc_tparamcnt, dp->ioc_tparam);
324681a5bbeSBoris Popov 	if (error)
325681a5bbeSBoris Popov 		goto bad;
326681a5bbeSBoris Popov 	error = smb_cpdatain(&t2p->t2_tdata, dp->ioc_tdatacnt, dp->ioc_tdata);
327681a5bbeSBoris Popov 	if (error)
328681a5bbeSBoris Popov 		goto bad;
329681a5bbeSBoris Popov 	error = smb_t2_request(t2p);
330681a5bbeSBoris Popov 	if (error)
331681a5bbeSBoris Popov 		goto bad;
332681a5bbeSBoris Popov 	mdp = &t2p->t2_rparam;
333681a5bbeSBoris Popov 	if (mdp->md_top) {
334681a5bbeSBoris Popov 		len = m_fixhdr(mdp->md_top);
335681a5bbeSBoris Popov 		if (len > dp->ioc_rparamcnt) {
336681a5bbeSBoris Popov 			error = EMSGSIZE;
337681a5bbeSBoris Popov 			goto bad;
338681a5bbeSBoris Popov 		}
339681a5bbeSBoris Popov 		dp->ioc_rparamcnt = len;
340681a5bbeSBoris Popov 		error = md_get_mem(mdp, dp->ioc_rparam, len, MB_MUSER);
341681a5bbeSBoris Popov 		if (error)
342681a5bbeSBoris Popov 			goto bad;
343681a5bbeSBoris Popov 	} else
344681a5bbeSBoris Popov 		dp->ioc_rparamcnt = 0;
345681a5bbeSBoris Popov 	mdp = &t2p->t2_rdata;
346681a5bbeSBoris Popov 	if (mdp->md_top) {
347681a5bbeSBoris Popov 		len = m_fixhdr(mdp->md_top);
348681a5bbeSBoris Popov 		if (len > dp->ioc_rdatacnt) {
349681a5bbeSBoris Popov 			error = EMSGSIZE;
350681a5bbeSBoris Popov 			goto bad;
351681a5bbeSBoris Popov 		}
352681a5bbeSBoris Popov 		dp->ioc_rdatacnt = len;
353681a5bbeSBoris Popov 		error = md_get_mem(mdp, dp->ioc_rdata, len, MB_MUSER);
354681a5bbeSBoris Popov 	} else
355681a5bbeSBoris Popov 		dp->ioc_rdatacnt = 0;
356681a5bbeSBoris Popov bad:
357681a5bbeSBoris Popov 	if (t2p->t_name)
358681a5bbeSBoris Popov 		smb_strfree(t2p->t_name);
359681a5bbeSBoris Popov 	smb_t2_done(t2p);
360afe09751SDavide Italiano 	free(t2p, M_SMBTEMP);
361681a5bbeSBoris Popov 	return error;
362681a5bbeSBoris Popov }
363