Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / netproto / smb / smb_usr.c
1 /*
2  * Copyright (c) 2000-2001 Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/netsmb/smb_usr.c,v 1.1.2.1 2001/05/22 08:32:34 bp Exp $
33  */
34 #include <sys/param.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/conf.h>
39 #include <sys/proc.h>
40 #include <sys/fcntl.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/sysctl.h>
44
45 #include <sys/iconv.h>
46
47 #include <netsmb/smb.h>
48 #include <netsmb/smb_conn.h>
49 #include <netsmb/smb_rq.h>
50 #include <netsmb/smb_subr.h>
51 #include <netsmb/smb_dev.h>
52
53 /*
54  * helpers for nsmb device. Can be moved to the smb_dev.c file.
55  */
56 static void smb_usr_vcspec_free(struct smb_vcspec *spec);
57
58 static int
59 smb_usr_vc2spec(struct smbioc_ossn *dp, struct smb_vcspec *spec)
60 {
61         int flags = 0;
62
63         bzero(spec, sizeof(*spec));
64         if (dp->ioc_user[0] == 0)
65                 return EINVAL;
66         if (dp->ioc_server == NULL)
67                 return EINVAL;
68         if (dp->ioc_localcs[0] == 0) {
69                 SMBERROR("no local charset ?\n");
70                 return EINVAL;
71         }
72
73         spec->sap = smb_memdupin(dp->ioc_server, dp->ioc_svlen);
74         if (spec->sap == NULL)
75                 return ENOMEM;
76         if (dp->ioc_local) {
77                 spec->lap = smb_memdupin(dp->ioc_local, dp->ioc_lolen);
78                 if (spec->lap == NULL) {
79                         smb_usr_vcspec_free(spec);
80                         return ENOMEM;
81                 }
82         }
83         spec->srvname = dp->ioc_srvname;
84         spec->pass = dp->ioc_password;
85         spec->domain = dp->ioc_workgroup;
86         spec->username = dp->ioc_user;
87         spec->mode = dp->ioc_mode;
88         spec->rights = dp->ioc_rights;
89         spec->owner = dp->ioc_owner;
90         spec->group = dp->ioc_group;
91         spec->localcs = dp->ioc_localcs;
92         spec->servercs = dp->ioc_servercs;
93         if (dp->ioc_opt & SMBVOPT_PRIVATE)
94                 flags |= SMBV_PRIVATE;
95         if (dp->ioc_opt & SMBVOPT_SINGLESHARE)
96                 flags |= SMBV_PRIVATE | SMBV_SINGLESHARE;
97         spec->flags = flags;
98         return 0;
99 }
100
101 static void
102 smb_usr_vcspec_free(struct smb_vcspec *spec)
103 {
104         if (spec->sap)
105                 smb_memfree(spec->sap);
106         if (spec->lap)
107                 smb_memfree(spec->lap);
108 }
109
110 static int
111 smb_usr_share2spec(struct smbioc_oshare *dp, struct smb_sharespec *spec)
112 {
113         bzero(spec, sizeof(*spec));
114         spec->mode = dp->ioc_mode;
115         spec->rights = dp->ioc_rights;
116         spec->owner = dp->ioc_owner;
117         spec->group = dp->ioc_group;
118         spec->name = dp->ioc_share;
119         spec->stype = dp->ioc_stype;
120         spec->pass = dp->ioc_password;
121         return 0;
122 }
123
124 int
125 smb_usr_lookup(struct smbioc_lookup *dp, struct smb_cred *scred,
126         struct smb_vc **vcpp, struct smb_share **sspp)
127 {
128         struct smb_vc *vcp = NULL;
129         struct smb_vcspec vspec;
130         struct smb_sharespec sspec, *sspecp = NULL;
131         int error;
132
133         if (dp->ioc_level < SMBL_VC || dp->ioc_level > SMBL_SHARE)
134                 return EINVAL;
135         error = smb_usr_vc2spec(&dp->ioc_ssn, &vspec);
136         if (error)
137                 return error;
138         if (dp->ioc_flags & SMBLK_CREATE)
139                 vspec.flags |= SMBV_CREATE;
140
141         if (dp->ioc_level >= SMBL_SHARE) {
142                 error = smb_usr_share2spec(&dp->ioc_sh, &sspec);
143                 if (error)
144                         goto out;
145                 sspecp = &sspec;
146         }
147         error = smb_sm_lookup(&vspec, sspecp, scred, &vcp);
148         if (error == 0) {
149                 *vcpp = vcp;
150                 *sspp = vspec.ssp;
151         }
152 out:
153         smb_usr_vcspec_free(&vspec);
154         return error;
155 }
156
157 /*
158  * Connect to the resource specified by smbioc_ossn structure.
159  * It may either find an existing connection or try to establish a new one.
160  * If no errors occured smb_vc returned locked and referenced.
161  */
162 int
163 smb_usr_opensession(struct smbioc_ossn *dp, struct smb_cred *scred,
164         struct smb_vc **vcpp)
165 {
166         struct smb_vc *vcp = NULL;
167         struct smb_vcspec vspec;
168         int error;
169
170         error = smb_usr_vc2spec(dp, &vspec);
171         if (error)
172                 return error;
173         if (dp->ioc_opt & SMBVOPT_CREATE)
174                 vspec.flags |= SMBV_CREATE;
175
176         error = smb_sm_lookup(&vspec, NULL, scred, &vcp);
177         smb_usr_vcspec_free(&vspec);
178         return error;
179 }
180
181 int
182 smb_usr_openshare(struct smb_vc *vcp, struct smbioc_oshare *dp,
183         struct smb_cred *scred, struct smb_share **sspp)
184 {
185         struct smb_share *ssp;
186         struct smb_sharespec shspec;
187         int error;
188
189         error = smb_usr_share2spec(dp, &shspec);
190         if (error)
191                 return error;
192         error = smb_vc_lookupshare(vcp, &shspec, scred, &ssp);
193         if (error == 0) {
194                 *sspp = ssp;
195                 return 0;
196         }
197         if ((dp->ioc_opt & SMBSOPT_CREATE) == 0)
198                 return error;
199         error = smb_share_create(vcp, &shspec, scred, &ssp);
200         if (error)
201                 return error;
202         error = smb_smb_treeconnect(ssp, scred);
203         if (error) {
204                 smb_share_put(ssp, scred);
205         } else
206                 *sspp = ssp;
207         return error;
208 }
209
210 int
211 smb_usr_simplerequest(struct smb_share *ssp, struct smbioc_rq *dp,
212         struct smb_cred *scred)
213 {
214         struct smb_rq rq, *rqp = &rq;
215         struct mbchain *mbp;
216         struct mdchain *mdp;
217         u_int8_t wc;
218         u_int16_t bc;
219         int error;
220
221         switch (dp->ioc_cmd) {
222             case SMB_COM_TRANSACTION2:
223             case SMB_COM_TRANSACTION2_SECONDARY:
224             case SMB_COM_CLOSE_AND_TREE_DISC:
225             case SMB_COM_TREE_CONNECT:
226             case SMB_COM_TREE_DISCONNECT:
227             case SMB_COM_NEGOTIATE:
228             case SMB_COM_SESSION_SETUP_ANDX:
229             case SMB_COM_LOGOFF_ANDX:
230             case SMB_COM_TREE_CONNECT_ANDX:
231                 return EPERM;
232         }
233         error = smb_rq_init(rqp, SSTOCP(ssp), dp->ioc_cmd, scred);
234         if (error)
235                 return error;
236         mbp = &rqp->sr_rq;
237         smb_rq_wstart(rqp);
238         error = mb_put_mem(mbp, dp->ioc_twords, dp->ioc_twc * 2, MB_MUSER);
239         if (error)
240                 goto bad;
241         smb_rq_wend(rqp);
242         smb_rq_bstart(rqp);
243         error = mb_put_mem(mbp, dp->ioc_tbytes, dp->ioc_tbc, MB_MUSER);
244         if (error)
245                 goto bad;
246         smb_rq_bend(rqp);
247         error = smb_rq_simple(rqp);
248         if (error)
249                 goto bad;
250         mdp = &rqp->sr_rp;
251         md_get_uint8(mdp, &wc);
252         dp->ioc_rwc = wc;
253         wc *= 2;
254         if (wc > dp->ioc_rpbufsz) {
255                 error = EBADRPC;
256                 goto bad;
257         }
258         error = md_get_mem(mdp, dp->ioc_rpbuf, wc, MB_MUSER);
259         if (error)
260                 goto bad;
261         md_get_uint16le(mdp, &bc);
262         if ((wc + bc) > dp->ioc_rpbufsz) {
263                 error = EBADRPC;
264                 goto bad;
265         }
266         dp->ioc_rbc = bc;
267         error = md_get_mem(mdp, dp->ioc_rpbuf + wc, bc, MB_MUSER);
268 bad:
269         dp->ioc_errclass = rqp->sr_errclass;
270         dp->ioc_serror = rqp->sr_serror;
271         dp->ioc_error = rqp->sr_error;
272         smb_rq_done(rqp);
273         return error;
274
275 }
276
277 static int
278 smb_cpdatain(struct mbchain *mbp, int len, caddr_t data)
279 {
280         int error;
281
282         if (len == 0)
283                 return 0;
284         error = mb_init(mbp);
285         if (error)
286                 return error;
287         return mb_put_mem(mbp, data, len, MB_MUSER);
288 }
289
290 int
291 smb_usr_t2request(struct smb_share *ssp, struct smbioc_t2rq *dp,
292         struct smb_cred *scred)
293 {
294         struct smb_t2rq t2, *t2p = &t2;
295         struct mdchain *mdp;
296         int error, len;
297
298         if (dp->ioc_tparamcnt > 0xffff || dp->ioc_tdatacnt > 0xffff ||
299             dp->ioc_setupcnt > 3)
300                 return EINVAL;
301         error = smb_t2_init(t2p, SSTOCP(ssp), dp->ioc_setup[0], scred);
302         if (error)
303                 return error;
304         len = t2p->t2_setupcount = dp->ioc_setupcnt;
305         if (len > 1)
306                 t2p->t2_setupdata = dp->ioc_setup; 
307         if (dp->ioc_name) {
308                 t2p->t_name = smb_strdupin(dp->ioc_name, 128);
309                 if (t2p->t_name == NULL) {
310                         error = ENOMEM;
311                         goto bad;
312                 }
313         }
314         t2p->t2_maxscount = 0;
315         t2p->t2_maxpcount = dp->ioc_rparamcnt;
316         t2p->t2_maxdcount = dp->ioc_rdatacnt;
317         error = smb_cpdatain(&t2p->t2_tparam, dp->ioc_tparamcnt, dp->ioc_tparam);
318         if (error)
319                 goto bad;
320         error = smb_cpdatain(&t2p->t2_tdata, dp->ioc_tdatacnt, dp->ioc_tdata);
321         if (error)
322                 goto bad;
323         error = smb_t2_request(t2p);
324         if (error)
325                 goto bad;
326         mdp = &t2p->t2_rparam;
327         if (mdp->md_top) {
328                 len = m_fixhdr(mdp->md_top);
329                 if (len > dp->ioc_rparamcnt) {
330                         error = EMSGSIZE;
331                         goto bad;
332                 }
333                 dp->ioc_rparamcnt = len;
334                 error = md_get_mem(mdp, dp->ioc_rparam, len, MB_MUSER);
335                 if (error)
336                         goto bad;
337         } else
338                 dp->ioc_rparamcnt = 0;
339         mdp = &t2p->t2_rdata;
340         if (mdp->md_top) {
341                 len = m_fixhdr(mdp->md_top);
342                 if (len > dp->ioc_rdatacnt) {
343                         error = EMSGSIZE;
344                         goto bad;
345                 }
346                 dp->ioc_rdatacnt = len;
347                 error = md_get_mem(mdp, dp->ioc_rdata, len, MB_MUSER);
348         } else
349                 dp->ioc_rdatacnt = 0;
350 bad:
351         if (t2p->t_name)
352                 smb_strfree(t2p->t_name);
353         smb_t2_done(t2p);
354         return error;
355 }