2 * Copyright (c) 2000-2001 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
42 #include <sys/fcntl.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/sysctl.h>
48 #include <sys/iconv.h>
50 #include <netsmb/smb.h>
51 #include <netsmb/smb_conn.h>
52 #include <netsmb/smb_rq.h>
53 #include <netsmb/smb_subr.h>
54 #include <netsmb/smb_dev.h>
57 * helpers for nsmb device. Can be moved to the smb_dev.c file.
59 static void smb_usr_vcspec_free(struct smb_vcspec *spec);
62 smb_usr_vc2spec(struct smbioc_ossn *dp, struct smb_vcspec *spec)
66 bzero(spec, sizeof(*spec));
68 #ifdef NETSMB_NO_ANON_USER
69 if (dp->ioc_user[0] == 0)
73 if (dp->ioc_server == NULL)
75 if (dp->ioc_localcs[0] == 0) {
76 SMBERROR("no local charset ?\n");
80 spec->sap = smb_memdupin(dp->ioc_server, dp->ioc_svlen);
81 if (spec->sap == NULL)
84 spec->lap = smb_memdupin(dp->ioc_local, dp->ioc_lolen);
85 if (spec->lap == NULL) {
86 smb_usr_vcspec_free(spec);
90 spec->srvname = dp->ioc_srvname;
91 spec->pass = dp->ioc_password;
92 spec->domain = dp->ioc_workgroup;
93 spec->username = dp->ioc_user;
94 spec->mode = dp->ioc_mode;
95 spec->rights = dp->ioc_rights;
96 spec->owner = dp->ioc_owner;
97 spec->group = dp->ioc_group;
98 spec->localcs = dp->ioc_localcs;
99 spec->servercs = dp->ioc_servercs;
100 if (dp->ioc_opt & SMBVOPT_PRIVATE)
101 flags |= SMBV_PRIVATE;
102 if (dp->ioc_opt & SMBVOPT_SINGLESHARE)
103 flags |= SMBV_PRIVATE | SMBV_SINGLESHARE;
109 smb_usr_vcspec_free(struct smb_vcspec *spec)
112 smb_memfree(spec->sap);
114 smb_memfree(spec->lap);
118 smb_usr_share2spec(struct smbioc_oshare *dp, struct smb_sharespec *spec)
120 bzero(spec, sizeof(*spec));
121 spec->mode = dp->ioc_mode;
122 spec->rights = dp->ioc_rights;
123 spec->owner = dp->ioc_owner;
124 spec->group = dp->ioc_group;
125 spec->name = dp->ioc_share;
126 spec->stype = dp->ioc_stype;
127 spec->pass = dp->ioc_password;
132 smb_usr_lookup(struct smbioc_lookup *dp, struct smb_cred *scred,
133 struct smb_vc **vcpp, struct smb_share **sspp)
135 struct smb_vc *vcp = NULL;
136 struct smb_vcspec vspec;
137 struct smb_sharespec sspec, *sspecp = NULL;
140 if (dp->ioc_level < SMBL_VC || dp->ioc_level > SMBL_SHARE)
142 error = smb_usr_vc2spec(&dp->ioc_ssn, &vspec);
145 if (dp->ioc_flags & SMBLK_CREATE)
146 vspec.flags |= SMBV_CREATE;
148 if (dp->ioc_level >= SMBL_SHARE) {
149 error = smb_usr_share2spec(&dp->ioc_sh, &sspec);
154 error = smb_sm_lookup(&vspec, sspecp, scred, &vcp);
160 smb_usr_vcspec_free(&vspec);
165 * Connect to the resource specified by smbioc_ossn structure.
166 * It may either find an existing connection or try to establish a new one.
167 * If no errors occured smb_vc returned locked and referenced.
170 smb_usr_opensession(struct smbioc_ossn *dp, struct smb_cred *scred,
171 struct smb_vc **vcpp)
173 struct smb_vc *vcp = NULL;
174 struct smb_vcspec vspec;
177 error = smb_usr_vc2spec(dp, &vspec);
180 if (dp->ioc_opt & SMBVOPT_CREATE)
181 vspec.flags |= SMBV_CREATE;
183 error = smb_sm_lookup(&vspec, NULL, scred, &vcp);
184 smb_usr_vcspec_free(&vspec);
189 smb_usr_openshare(struct smb_vc *vcp, struct smbioc_oshare *dp,
190 struct smb_cred *scred, struct smb_share **sspp)
192 struct smb_share *ssp;
193 struct smb_sharespec shspec;
196 error = smb_usr_share2spec(dp, &shspec);
199 error = smb_vc_lookupshare(vcp, &shspec, scred, &ssp);
204 if ((dp->ioc_opt & SMBSOPT_CREATE) == 0)
206 error = smb_share_create(vcp, &shspec, scred, &ssp);
209 error = smb_smb_treeconnect(ssp, scred);
211 smb_share_put(ssp, scred);
218 smb_usr_simplerequest(struct smb_share *ssp, struct smbioc_rq *dp,
219 struct smb_cred *scred)
221 struct smb_rq rq, *rqp = &rq;
228 switch (dp->ioc_cmd) {
229 case SMB_COM_TRANSACTION2:
230 case SMB_COM_TRANSACTION2_SECONDARY:
231 case SMB_COM_CLOSE_AND_TREE_DISC:
232 case SMB_COM_TREE_CONNECT:
233 case SMB_COM_TREE_DISCONNECT:
234 case SMB_COM_NEGOTIATE:
235 case SMB_COM_SESSION_SETUP_ANDX:
236 case SMB_COM_LOGOFF_ANDX:
237 case SMB_COM_TREE_CONNECT_ANDX:
240 error = smb_rq_init(rqp, SSTOCP(ssp), dp->ioc_cmd, scred);
245 error = mb_put_mem(mbp, dp->ioc_twords, dp->ioc_twc * 2, MB_MUSER);
250 error = mb_put_mem(mbp, dp->ioc_tbytes, dp->ioc_tbc, MB_MUSER);
254 error = smb_rq_simple(rqp);
258 md_get_uint8(mdp, &wc);
261 if (wc > dp->ioc_rpbufsz) {
265 error = md_get_mem(mdp, dp->ioc_rpbuf, wc, MB_MUSER);
268 md_get_uint16le(mdp, &bc);
269 if ((wc + bc) > dp->ioc_rpbufsz) {
274 error = md_get_mem(mdp, dp->ioc_rpbuf + wc, bc, MB_MUSER);
276 dp->ioc_errclass = rqp->sr_errclass;
277 dp->ioc_serror = rqp->sr_serror;
278 dp->ioc_error = rqp->sr_error;
285 smb_cpdatain(struct mbchain *mbp, int len, caddr_t data)
291 error = mb_init(mbp);
294 return mb_put_mem(mbp, data, len, MB_MUSER);
298 smb_usr_t2request(struct smb_share *ssp, struct smbioc_t2rq *dp,
299 struct smb_cred *scred)
301 struct smb_t2rq t2, *t2p = &t2;
305 if (dp->ioc_setupcnt > 3)
307 error = smb_t2_init(t2p, SSTOCP(ssp), dp->ioc_setup[0], scred);
310 len = t2p->t2_setupcount = dp->ioc_setupcnt;
312 t2p->t2_setupdata = dp->ioc_setup;
314 t2p->t_name = smb_strdupin(dp->ioc_name, 128);
315 if (t2p->t_name == NULL) {
320 t2p->t2_maxscount = 0;
321 t2p->t2_maxpcount = dp->ioc_rparamcnt;
322 t2p->t2_maxdcount = dp->ioc_rdatacnt;
323 error = smb_cpdatain(&t2p->t2_tparam, dp->ioc_tparamcnt, dp->ioc_tparam);
326 error = smb_cpdatain(&t2p->t2_tdata, dp->ioc_tdatacnt, dp->ioc_tdata);
329 error = smb_t2_request(t2p);
332 mdp = &t2p->t2_rparam;
334 len = m_fixhdr(mdp->md_top);
335 if (len > dp->ioc_rparamcnt) {
339 dp->ioc_rparamcnt = len;
340 error = md_get_mem(mdp, dp->ioc_rparam, len, MB_MUSER);
344 dp->ioc_rparamcnt = 0;
345 mdp = &t2p->t2_rdata;
347 len = m_fixhdr(mdp->md_top);
348 if (len > dp->ioc_rdatacnt) {
352 dp->ioc_rdatacnt = len;
353 error = md_get_mem(mdp, dp->ioc_rdata, len, MB_MUSER);
355 dp->ioc_rdatacnt = 0;
358 smb_strfree(t2p->t_name);