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 */
28681a5bbeSBoris Popov
29681a5bbeSBoris Popov /*
30681a5bbeSBoris Popov * Connection engine.
31681a5bbeSBoris Popov */
32681a5bbeSBoris Popov
33681a5bbeSBoris Popov #include <sys/param.h>
34681a5bbeSBoris Popov #include <sys/systm.h>
35681a5bbeSBoris Popov #include <sys/kernel.h>
36681a5bbeSBoris Popov #include <sys/malloc.h>
37acd3428bSRobert Watson #include <sys/priv.h>
38681a5bbeSBoris Popov #include <sys/proc.h>
39681a5bbeSBoris Popov #include <sys/lock.h>
40681a5bbeSBoris Popov #include <sys/sysctl.h>
41681a5bbeSBoris Popov #include <sys/socketvar.h>
42681a5bbeSBoris Popov
43681a5bbeSBoris Popov #include <sys/iconv.h>
44681a5bbeSBoris Popov
45681a5bbeSBoris Popov #include <netsmb/smb.h>
46681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
47681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
48681a5bbeSBoris Popov #include <netsmb/smb_tran.h>
49681a5bbeSBoris Popov #include <netsmb/smb_trantcp.h>
50681a5bbeSBoris Popov
51681a5bbeSBoris Popov static struct smb_connobj smb_vclist;
52681a5bbeSBoris Popov static int smb_vcnext = 1; /* next unique id for VC */
53681a5bbeSBoris Popov
547029da5cSPawel Biernacki SYSCTL_NODE(_net, OID_AUTO, smb, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
557029da5cSPawel Biernacki "SMB protocol");
56681a5bbeSBoris Popov
57d745c852SEd Schouten static MALLOC_DEFINE(M_SMBCONN, "smb_conn", "SMB connection");
58681a5bbeSBoris Popov
5993b651d8SAttilio Rao static void smb_co_init(struct smb_connobj *cp, int level, char *ilockname,
60e3b3762bSRobert Watson char *lockname);
61681a5bbeSBoris Popov static void smb_co_done(struct smb_connobj *cp);
62681a5bbeSBoris Popov static int smb_vc_disconnect(struct smb_vc *vcp);
63681a5bbeSBoris Popov static void smb_vc_free(struct smb_connobj *cp);
64681a5bbeSBoris Popov static void smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred);
65681a5bbeSBoris Popov static smb_co_free_t smb_share_free;
66681a5bbeSBoris Popov static smb_co_gone_t smb_share_gone;
67681a5bbeSBoris Popov
68681a5bbeSBoris Popov static int smb_sysctl_treedump(SYSCTL_HANDLER_ARGS);
69681a5bbeSBoris Popov
707029da5cSPawel Biernacki SYSCTL_PROC(_net_smb, OID_AUTO, treedump,
717029da5cSPawel Biernacki CTLFLAG_RD | CTLTYPE_OPAQUE | CTLFLAG_MPSAFE,
727029da5cSPawel Biernacki NULL, 0, smb_sysctl_treedump, "S,treedump",
737029da5cSPawel Biernacki "Requester tree");
74681a5bbeSBoris Popov
75681a5bbeSBoris Popov int
smb_sm_init(void)76681a5bbeSBoris Popov smb_sm_init(void)
77681a5bbeSBoris Popov {
78681a5bbeSBoris Popov
79e3b3762bSRobert Watson smb_co_init(&smb_vclist, SMBL_SM, "smbsm ilock", "smbsm");
80a4c05984SDavide Italiano sx_xlock(&smb_vclist.co_interlock);
81a4c05984SDavide Italiano smb_co_unlock(&smb_vclist);
82a4c05984SDavide Italiano sx_unlock(&smb_vclist.co_interlock);
83681a5bbeSBoris Popov return 0;
84681a5bbeSBoris Popov }
85681a5bbeSBoris Popov
86681a5bbeSBoris Popov int
smb_sm_done(void)87681a5bbeSBoris Popov smb_sm_done(void)
88681a5bbeSBoris Popov {
89681a5bbeSBoris Popov
90681a5bbeSBoris Popov /* XXX: hold the mutex */
91681a5bbeSBoris Popov if (smb_vclist.co_usecount > 1) {
92681a5bbeSBoris Popov SMBERROR("%d connections still active\n", smb_vclist.co_usecount - 1);
93681a5bbeSBoris Popov return EBUSY;
94681a5bbeSBoris Popov }
95681a5bbeSBoris Popov smb_co_done(&smb_vclist);
96681a5bbeSBoris Popov return 0;
97681a5bbeSBoris Popov }
98681a5bbeSBoris Popov
99681a5bbeSBoris Popov static int
smb_sm_lockvclist(void)100a4c05984SDavide Italiano smb_sm_lockvclist(void)
101681a5bbeSBoris Popov {
102a4c05984SDavide Italiano int error;
103681a5bbeSBoris Popov
104a4c05984SDavide Italiano sx_xlock(&smb_vclist.co_interlock);
105a4c05984SDavide Italiano error = smb_co_lock(&smb_vclist);
106a4c05984SDavide Italiano sx_unlock(&smb_vclist.co_interlock);
107a4c05984SDavide Italiano
108a4c05984SDavide Italiano return error;
109681a5bbeSBoris Popov }
110681a5bbeSBoris Popov
111681a5bbeSBoris Popov static void
smb_sm_unlockvclist(void)112e3b3762bSRobert Watson smb_sm_unlockvclist(void)
113681a5bbeSBoris Popov {
114681a5bbeSBoris Popov
115a4c05984SDavide Italiano sx_xlock(&smb_vclist.co_interlock);
116a4c05984SDavide Italiano smb_co_unlock(&smb_vclist);
117a4c05984SDavide Italiano sx_unlock(&smb_vclist.co_interlock);
118681a5bbeSBoris Popov }
119681a5bbeSBoris Popov
120681a5bbeSBoris Popov static int
smb_sm_lookupint(struct smb_vcspec * vcspec,struct smb_sharespec * shspec,struct smb_cred * scred,struct smb_vc ** vcpp)121681a5bbeSBoris Popov smb_sm_lookupint(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
122681a5bbeSBoris Popov struct smb_cred *scred, struct smb_vc **vcpp)
123681a5bbeSBoris Popov {
124445e045bSAlexander Kabaev struct smb_connobj *scp;
125681a5bbeSBoris Popov struct smb_vc *vcp;
126681a5bbeSBoris Popov int exact = 1;
127681a5bbeSBoris Popov int error;
128681a5bbeSBoris Popov
129681a5bbeSBoris Popov vcspec->shspec = shspec;
130681a5bbeSBoris Popov error = ENOENT;
131445e045bSAlexander Kabaev vcp = NULL;
132445e045bSAlexander Kabaev SMBCO_FOREACH(scp, &smb_vclist) {
133445e045bSAlexander Kabaev vcp = (struct smb_vc *)scp;
134a4c05984SDavide Italiano error = smb_vc_lock(vcp);
135681a5bbeSBoris Popov if (error)
136681a5bbeSBoris Popov continue;
137e51fe875SMarcel Moolenaar
138681a5bbeSBoris Popov if ((vcp->obj.co_flags & SMBV_PRIVATE) ||
139681a5bbeSBoris Popov !CONNADDREQ(vcp->vc_paddr, vcspec->sap) ||
140681a5bbeSBoris Popov strcmp(vcp->vc_username, vcspec->username) != 0)
141e51fe875SMarcel Moolenaar goto err1;
142681a5bbeSBoris Popov if (vcspec->owner != SMBM_ANY_OWNER) {
143681a5bbeSBoris Popov if (vcp->vc_uid != vcspec->owner)
144e51fe875SMarcel Moolenaar goto err1;
145681a5bbeSBoris Popov } else
146681a5bbeSBoris Popov exact = 0;
147681a5bbeSBoris Popov if (vcspec->group != SMBM_ANY_GROUP) {
148681a5bbeSBoris Popov if (vcp->vc_grp != vcspec->group)
149e51fe875SMarcel Moolenaar goto err1;
150681a5bbeSBoris Popov } else
151681a5bbeSBoris Popov exact = 0;
152681a5bbeSBoris Popov if (vcspec->mode & SMBM_EXACT) {
153e51fe875SMarcel Moolenaar if (!exact || (vcspec->mode & SMBM_MASK) !=
154e51fe875SMarcel Moolenaar vcp->vc_mode)
155e51fe875SMarcel Moolenaar goto err1;
156681a5bbeSBoris Popov }
157681a5bbeSBoris Popov if (smb_vc_access(vcp, scred, vcspec->mode) != 0)
158e51fe875SMarcel Moolenaar goto err1;
159681a5bbeSBoris Popov vcspec->ssp = NULL;
160e51fe875SMarcel Moolenaar if (shspec) {
161e51fe875SMarcel Moolenaar error = (int)smb_vc_lookupshare(vcp, shspec, scred,
162e51fe875SMarcel Moolenaar &vcspec->ssp);
163e51fe875SMarcel Moolenaar if (error)
164e51fe875SMarcel Moolenaar goto fail;
165e51fe875SMarcel Moolenaar }
166681a5bbeSBoris Popov error = 0;
167681a5bbeSBoris Popov break;
168e51fe875SMarcel Moolenaar err1:
169e51fe875SMarcel Moolenaar error = 1;
170e51fe875SMarcel Moolenaar fail:
171a4c05984SDavide Italiano smb_vc_unlock(vcp);
172681a5bbeSBoris Popov }
173681a5bbeSBoris Popov if (vcp) {
174fce6fbfaSBoris Popov smb_vc_ref(vcp);
175681a5bbeSBoris Popov *vcpp = vcp;
176681a5bbeSBoris Popov }
177e51fe875SMarcel Moolenaar return (error);
178681a5bbeSBoris Popov }
179681a5bbeSBoris Popov
180681a5bbeSBoris Popov int
smb_sm_lookup(struct smb_vcspec * vcspec,struct smb_sharespec * shspec,struct smb_cred * scred,struct smb_vc ** vcpp)181681a5bbeSBoris Popov smb_sm_lookup(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
182681a5bbeSBoris Popov struct smb_cred *scred, struct smb_vc **vcpp)
183681a5bbeSBoris Popov {
184681a5bbeSBoris Popov struct smb_vc *vcp;
185681a5bbeSBoris Popov struct smb_share *ssp = NULL;
186681a5bbeSBoris Popov int error;
187681a5bbeSBoris Popov
188681a5bbeSBoris Popov *vcpp = vcp = NULL;
189681a5bbeSBoris Popov
190a4c05984SDavide Italiano error = smb_sm_lockvclist();
191681a5bbeSBoris Popov if (error)
192681a5bbeSBoris Popov return error;
193681a5bbeSBoris Popov error = smb_sm_lookupint(vcspec, shspec, scred, vcpp);
194681a5bbeSBoris Popov if (error == 0 || (vcspec->flags & SMBV_CREATE) == 0) {
195e3b3762bSRobert Watson smb_sm_unlockvclist();
196681a5bbeSBoris Popov return error;
197681a5bbeSBoris Popov }
198681a5bbeSBoris Popov error = smb_sm_lookupint(vcspec, NULL, scred, &vcp);
199681a5bbeSBoris Popov if (error) {
200681a5bbeSBoris Popov error = smb_vc_create(vcspec, scred, &vcp);
201681a5bbeSBoris Popov if (error)
202681a5bbeSBoris Popov goto out;
203681a5bbeSBoris Popov error = smb_vc_connect(vcp, scred);
204681a5bbeSBoris Popov if (error)
205681a5bbeSBoris Popov goto out;
206681a5bbeSBoris Popov }
207681a5bbeSBoris Popov if (shspec == NULL)
208681a5bbeSBoris Popov goto out;
209681a5bbeSBoris Popov error = smb_share_create(vcp, shspec, scred, &ssp);
210681a5bbeSBoris Popov if (error)
211681a5bbeSBoris Popov goto out;
212681a5bbeSBoris Popov error = smb_smb_treeconnect(ssp, scred);
213681a5bbeSBoris Popov if (error == 0)
214681a5bbeSBoris Popov vcspec->ssp = ssp;
215681a5bbeSBoris Popov else
216681a5bbeSBoris Popov smb_share_put(ssp, scred);
217681a5bbeSBoris Popov out:
218e3b3762bSRobert Watson smb_sm_unlockvclist();
219681a5bbeSBoris Popov if (error == 0)
220681a5bbeSBoris Popov *vcpp = vcp;
2216aaab444SRobert Watson else if (vcp) {
222a4c05984SDavide Italiano smb_vc_lock(vcp);
223681a5bbeSBoris Popov smb_vc_put(vcp, scred);
2246aaab444SRobert Watson }
225681a5bbeSBoris Popov return error;
226681a5bbeSBoris Popov }
227681a5bbeSBoris Popov
228681a5bbeSBoris Popov /*
229681a5bbeSBoris Popov * Common code for connection object
230681a5bbeSBoris Popov */
231681a5bbeSBoris Popov static void
smb_co_init(struct smb_connobj * cp,int level,char * ilockname,char * lockname)232e3b3762bSRobert Watson smb_co_init(struct smb_connobj *cp, int level, char *ilockname, char *lockname)
233681a5bbeSBoris Popov {
234681a5bbeSBoris Popov SLIST_INIT(&cp->co_children);
235a4c05984SDavide Italiano sx_init_flags(&cp->co_interlock, ilockname, SX_RECURSE);
236a4c05984SDavide Italiano cv_init(&cp->co_lock, "smblock");
237a4c05984SDavide Italiano cp->co_lockcnt = 0;
238a4c05984SDavide Italiano cp->co_locker = NULL;
239681a5bbeSBoris Popov cp->co_level = level;
240681a5bbeSBoris Popov cp->co_usecount = 1;
241a4c05984SDavide Italiano sx_xlock(&cp->co_interlock);
242a4c05984SDavide Italiano smb_co_lock(cp);
243a4c05984SDavide Italiano sx_unlock(&cp->co_interlock);
244681a5bbeSBoris Popov }
245681a5bbeSBoris Popov
246681a5bbeSBoris Popov static void
smb_co_done(struct smb_connobj * cp)247681a5bbeSBoris Popov smb_co_done(struct smb_connobj *cp)
248681a5bbeSBoris Popov {
249a4c05984SDavide Italiano
250a4c05984SDavide Italiano sx_destroy(&cp->co_interlock);
251a4c05984SDavide Italiano cv_destroy(&cp->co_lock);
252a4c05984SDavide Italiano cp->co_locker = NULL;
253a4c05984SDavide Italiano cp->co_flags = 0;
254a4c05984SDavide Italiano cp->co_lockcnt = 0;
255681a5bbeSBoris Popov }
256681a5bbeSBoris Popov
257681a5bbeSBoris Popov static void
smb_co_gone(struct smb_connobj * cp,struct smb_cred * scred)258681a5bbeSBoris Popov smb_co_gone(struct smb_connobj *cp, struct smb_cred *scred)
259681a5bbeSBoris Popov {
260681a5bbeSBoris Popov struct smb_connobj *parent;
261681a5bbeSBoris Popov
262681a5bbeSBoris Popov if (cp->co_gone)
263681a5bbeSBoris Popov cp->co_gone(cp, scred);
264681a5bbeSBoris Popov parent = cp->co_parent;
265681a5bbeSBoris Popov if (parent) {
266a4c05984SDavide Italiano sx_xlock(&parent->co_interlock);
267a4c05984SDavide Italiano smb_co_lock(parent);
268a4c05984SDavide Italiano sx_unlock(&parent->co_interlock);
269681a5bbeSBoris Popov SLIST_REMOVE(&parent->co_children, cp, smb_connobj, co_next);
270681a5bbeSBoris Popov smb_co_put(parent, scred);
271681a5bbeSBoris Popov }
272681a5bbeSBoris Popov if (cp->co_free)
273681a5bbeSBoris Popov cp->co_free(cp);
274681a5bbeSBoris Popov }
275681a5bbeSBoris Popov
276681a5bbeSBoris Popov void
smb_co_ref(struct smb_connobj * cp)277fce6fbfaSBoris Popov smb_co_ref(struct smb_connobj *cp)
278681a5bbeSBoris Popov {
279681a5bbeSBoris Popov
280a4c05984SDavide Italiano sx_xlock(&cp->co_interlock);
281681a5bbeSBoris Popov cp->co_usecount++;
282a4c05984SDavide Italiano sx_unlock(&cp->co_interlock);
283681a5bbeSBoris Popov }
284681a5bbeSBoris Popov
285681a5bbeSBoris Popov void
smb_co_rele(struct smb_connobj * cp,struct smb_cred * scred)286681a5bbeSBoris Popov smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred)
287681a5bbeSBoris Popov {
288681a5bbeSBoris Popov
289a4c05984SDavide Italiano sx_xlock(&cp->co_interlock);
290a4c05984SDavide Italiano smb_co_unlock(cp);
291681a5bbeSBoris Popov if (cp->co_usecount > 1) {
292681a5bbeSBoris Popov cp->co_usecount--;
293a4c05984SDavide Italiano sx_unlock(&cp->co_interlock);
294681a5bbeSBoris Popov return;
295681a5bbeSBoris Popov }
296681a5bbeSBoris Popov if (cp->co_usecount == 0) {
297681a5bbeSBoris Popov SMBERROR("negative use_count for object %d", cp->co_level);
298a4c05984SDavide Italiano sx_unlock(&cp->co_interlock);
299681a5bbeSBoris Popov return;
300681a5bbeSBoris Popov }
301681a5bbeSBoris Popov cp->co_usecount--;
302681a5bbeSBoris Popov cp->co_flags |= SMBO_GONE;
303a4c05984SDavide Italiano sx_unlock(&cp->co_interlock);
304681a5bbeSBoris Popov smb_co_gone(cp, scred);
305681a5bbeSBoris Popov }
306681a5bbeSBoris Popov
307681a5bbeSBoris Popov int
smb_co_get(struct smb_connobj * cp,struct smb_cred * scred)308a4c05984SDavide Italiano smb_co_get(struct smb_connobj *cp, struct smb_cred *scred)
309681a5bbeSBoris Popov {
310681a5bbeSBoris Popov int error;
311681a5bbeSBoris Popov
312a4c05984SDavide Italiano MPASS(sx_xholder(&cp->co_interlock) == curthread);
313681a5bbeSBoris Popov cp->co_usecount++;
314a4c05984SDavide Italiano error = smb_co_lock(cp);
315a4c05984SDavide Italiano if (error)
316681a5bbeSBoris Popov cp->co_usecount--;
317681a5bbeSBoris Popov return error;
318681a5bbeSBoris Popov }
319681a5bbeSBoris Popov
320681a5bbeSBoris Popov void
smb_co_put(struct smb_connobj * cp,struct smb_cred * scred)321681a5bbeSBoris Popov smb_co_put(struct smb_connobj *cp, struct smb_cred *scred)
322681a5bbeSBoris Popov {
323681a5bbeSBoris Popov
324a4c05984SDavide Italiano sx_xlock(&cp->co_interlock);
325681a5bbeSBoris Popov if (cp->co_usecount > 1) {
326681a5bbeSBoris Popov cp->co_usecount--;
327681a5bbeSBoris Popov } else if (cp->co_usecount == 1) {
328681a5bbeSBoris Popov cp->co_usecount--;
329681a5bbeSBoris Popov cp->co_flags |= SMBO_GONE;
330681a5bbeSBoris Popov } else {
331681a5bbeSBoris Popov SMBERROR("negative usecount");
332681a5bbeSBoris Popov }
333a4c05984SDavide Italiano smb_co_unlock(cp);
334a4c05984SDavide Italiano sx_unlock(&cp->co_interlock);
335681a5bbeSBoris Popov if ((cp->co_flags & SMBO_GONE) == 0)
336681a5bbeSBoris Popov return;
337681a5bbeSBoris Popov smb_co_gone(cp, scred);
338681a5bbeSBoris Popov }
339681a5bbeSBoris Popov
340681a5bbeSBoris Popov int
smb_co_lock(struct smb_connobj * cp)341a4c05984SDavide Italiano smb_co_lock(struct smb_connobj *cp)
342681a5bbeSBoris Popov {
343681a5bbeSBoris Popov
344a4c05984SDavide Italiano MPASS(sx_xholder(&cp->co_interlock) == curthread);
345a4c05984SDavide Italiano for (;;) {
346681a5bbeSBoris Popov if (cp->co_flags & SMBO_GONE)
347681a5bbeSBoris Popov return EINVAL;
348a4c05984SDavide Italiano if (cp->co_locker == NULL) {
349a4c05984SDavide Italiano cp->co_locker = curthread;
350681a5bbeSBoris Popov return 0;
351681a5bbeSBoris Popov }
352a4c05984SDavide Italiano if (cp->co_locker == curthread) {
353a4c05984SDavide Italiano cp->co_lockcnt++;
354a4c05984SDavide Italiano return 0;
355a4c05984SDavide Italiano }
356a4c05984SDavide Italiano cv_wait(&cp->co_lock, &cp->co_interlock);
357a4c05984SDavide Italiano }
358681a5bbeSBoris Popov }
359681a5bbeSBoris Popov
360681a5bbeSBoris Popov void
smb_co_unlock(struct smb_connobj * cp)361a4c05984SDavide Italiano smb_co_unlock(struct smb_connobj *cp)
362681a5bbeSBoris Popov {
363a4c05984SDavide Italiano
364a4c05984SDavide Italiano MPASS(sx_xholder(&cp->co_interlock) == curthread);
365a4c05984SDavide Italiano MPASS(cp->co_locker == curthread);
366a4c05984SDavide Italiano if (cp->co_lockcnt != 0) {
367a4c05984SDavide Italiano cp->co_lockcnt--;
368a4c05984SDavide Italiano return;
369a4c05984SDavide Italiano }
370a4c05984SDavide Italiano cp->co_locker = NULL;
371a4c05984SDavide Italiano cv_signal(&cp->co_lock);
372681a5bbeSBoris Popov }
373681a5bbeSBoris Popov
374681a5bbeSBoris Popov static void
smb_co_addchild(struct smb_connobj * parent,struct smb_connobj * child)375681a5bbeSBoris Popov smb_co_addchild(struct smb_connobj *parent, struct smb_connobj *child)
376681a5bbeSBoris Popov {
377e3b3762bSRobert Watson
378fce6fbfaSBoris Popov smb_co_ref(parent);
379681a5bbeSBoris Popov SLIST_INSERT_HEAD(&parent->co_children, child, co_next);
380681a5bbeSBoris Popov child->co_parent = parent;
381681a5bbeSBoris Popov }
382681a5bbeSBoris Popov
383681a5bbeSBoris Popov /*
384681a5bbeSBoris Popov * Session implementation
385681a5bbeSBoris Popov */
386681a5bbeSBoris Popov
387681a5bbeSBoris Popov int
smb_vc_create(struct smb_vcspec * vcspec,struct smb_cred * scred,struct smb_vc ** vcpp)388681a5bbeSBoris Popov smb_vc_create(struct smb_vcspec *vcspec,
389681a5bbeSBoris Popov struct smb_cred *scred, struct smb_vc **vcpp)
390681a5bbeSBoris Popov {
391681a5bbeSBoris Popov struct smb_vc *vcp;
392681a5bbeSBoris Popov struct ucred *cred = scred->scr_cred;
393681a5bbeSBoris Popov uid_t uid = vcspec->owner;
394681a5bbeSBoris Popov gid_t gid = vcspec->group;
395681a5bbeSBoris Popov uid_t realuid = cred->cr_uid;
396681a5bbeSBoris Popov char *domain = vcspec->domain;
397681a5bbeSBoris Popov int error, isroot;
398681a5bbeSBoris Popov
399681a5bbeSBoris Popov isroot = smb_suser(cred) == 0;
400681a5bbeSBoris Popov /*
401681a5bbeSBoris Popov * Only superuser can create VCs with different uid and gid
402681a5bbeSBoris Popov */
403681a5bbeSBoris Popov if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot)
404681a5bbeSBoris Popov return EPERM;
405681a5bbeSBoris Popov if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot)
406681a5bbeSBoris Popov return EPERM;
407681a5bbeSBoris Popov
408a163d034SWarner Losh vcp = smb_zmalloc(sizeof(*vcp), M_SMBCONN, M_WAITOK);
409e3b3762bSRobert Watson smb_co_init(VCTOCP(vcp), SMBL_VC, "smb_vc ilock", "smb_vc");
410681a5bbeSBoris Popov vcp->obj.co_free = smb_vc_free;
411681a5bbeSBoris Popov vcp->obj.co_gone = smb_vc_gone;
412681a5bbeSBoris Popov vcp->vc_number = smb_vcnext++;
413681a5bbeSBoris Popov vcp->vc_timo = SMB_DEFRQTIMO;
414681a5bbeSBoris Popov vcp->vc_smbuid = SMB_UID_UNKNOWN;
415681a5bbeSBoris Popov vcp->vc_mode = vcspec->rights & SMBM_MASK;
416681a5bbeSBoris Popov vcp->obj.co_flags = vcspec->flags & (SMBV_PRIVATE | SMBV_SINGLESHARE);
417681a5bbeSBoris Popov vcp->vc_tdesc = &smb_tran_nbtcp_desc;
418190b2c4fSTim J. Robbins vcp->vc_seqno = 0;
419190b2c4fSTim J. Robbins vcp->vc_mackey = NULL;
420190b2c4fSTim J. Robbins vcp->vc_mackeylen = 0;
421681a5bbeSBoris Popov
422681a5bbeSBoris Popov if (uid == SMBM_ANY_OWNER)
423681a5bbeSBoris Popov uid = realuid;
424681a5bbeSBoris Popov if (gid == SMBM_ANY_GROUP)
425681a5bbeSBoris Popov gid = cred->cr_groups[0];
426681a5bbeSBoris Popov vcp->vc_uid = uid;
427681a5bbeSBoris Popov vcp->vc_grp = gid;
428681a5bbeSBoris Popov
429681a5bbeSBoris Popov smb_sl_init(&vcp->vc_stlock, "vcstlock");
430e51fe875SMarcel Moolenaar error = ENOMEM;
431e51fe875SMarcel Moolenaar
432746e5bf0SRobert Watson vcp->vc_paddr = sodupsockaddr(vcspec->sap, M_WAITOK);
433e51fe875SMarcel Moolenaar if (vcp->vc_paddr == NULL)
434e51fe875SMarcel Moolenaar goto fail;
435746e5bf0SRobert Watson vcp->vc_laddr = sodupsockaddr(vcspec->lap, M_WAITOK);
436e51fe875SMarcel Moolenaar if (vcp->vc_laddr == NULL)
437e51fe875SMarcel Moolenaar goto fail;
438e51fe875SMarcel Moolenaar vcp->vc_pass = smb_strdup(vcspec->pass);
439e51fe875SMarcel Moolenaar if (vcp->vc_pass == NULL)
440e51fe875SMarcel Moolenaar goto fail;
441e51fe875SMarcel Moolenaar vcp->vc_domain = smb_strdup((domain && domain[0]) ? domain :
442e51fe875SMarcel Moolenaar "NODOMAIN");
443e51fe875SMarcel Moolenaar if (vcp->vc_domain == NULL)
444e51fe875SMarcel Moolenaar goto fail;
445e51fe875SMarcel Moolenaar vcp->vc_srvname = smb_strdup(vcspec->srvname);
446e51fe875SMarcel Moolenaar if (vcp->vc_srvname == NULL)
447e51fe875SMarcel Moolenaar goto fail;
448e51fe875SMarcel Moolenaar vcp->vc_username = smb_strdup(vcspec->username);
449e51fe875SMarcel Moolenaar if (vcp->vc_username == NULL)
450e51fe875SMarcel Moolenaar goto fail;
451e51fe875SMarcel Moolenaar error = (int)iconv_open("tolower", vcspec->localcs, &vcp->vc_tolower);
452e51fe875SMarcel Moolenaar if (error)
453e51fe875SMarcel Moolenaar goto fail;
454e51fe875SMarcel Moolenaar error = (int)iconv_open("toupper", vcspec->localcs, &vcp->vc_toupper);
455e51fe875SMarcel Moolenaar if (error)
456e51fe875SMarcel Moolenaar goto fail;
457681a5bbeSBoris Popov if (vcspec->servercs[0]) {
458e51fe875SMarcel Moolenaar error = (int)iconv_open(vcspec->servercs, vcspec->localcs,
45941f1dcccSKevin Lo &vcp->vc_cp_toserver);
460e51fe875SMarcel Moolenaar if (error)
461e51fe875SMarcel Moolenaar goto fail;
462e51fe875SMarcel Moolenaar error = (int)iconv_open(vcspec->localcs, vcspec->servercs,
46341f1dcccSKevin Lo &vcp->vc_cp_tolocal);
464e51fe875SMarcel Moolenaar if (error)
465e51fe875SMarcel Moolenaar goto fail;
46641f1dcccSKevin Lo vcp->vc_toserver = vcp->vc_cp_toserver;
46741f1dcccSKevin Lo vcp->vc_tolocal = vcp->vc_cp_tolocal;
46841f1dcccSKevin Lo iconv_add(ENCODING_UNICODE, ENCODING_UNICODE, SMB_UNICODE_NAME);
46941f1dcccSKevin Lo iconv_add(ENCODING_UNICODE, SMB_UNICODE_NAME, ENCODING_UNICODE);
47041f1dcccSKevin Lo error = (int)iconv_open(SMB_UNICODE_NAME, vcspec->localcs,
47141f1dcccSKevin Lo &vcp->vc_ucs_toserver);
47241f1dcccSKevin Lo if (!error) {
47341f1dcccSKevin Lo error = (int)iconv_open(vcspec->localcs, SMB_UNICODE_NAME,
47441f1dcccSKevin Lo &vcp->vc_ucs_tolocal);
47541f1dcccSKevin Lo }
47641f1dcccSKevin Lo if (error) {
47741f1dcccSKevin Lo if (vcp->vc_ucs_toserver)
47841f1dcccSKevin Lo iconv_close(vcp->vc_ucs_toserver);
47941f1dcccSKevin Lo vcp->vc_ucs_toserver = NULL;
48041f1dcccSKevin Lo vcp->vc_ucs_tolocal = NULL;
48141f1dcccSKevin Lo }
482681a5bbeSBoris Popov }
483e51fe875SMarcel Moolenaar error = (int)smb_iod_create(vcp);
484e51fe875SMarcel Moolenaar if (error)
485e51fe875SMarcel Moolenaar goto fail;
486681a5bbeSBoris Popov *vcpp = vcp;
487681a5bbeSBoris Popov smb_co_addchild(&smb_vclist, VCTOCP(vcp));
488e51fe875SMarcel Moolenaar return (0);
489e51fe875SMarcel Moolenaar
490e51fe875SMarcel Moolenaar fail:
491681a5bbeSBoris Popov smb_vc_put(vcp, scred);
492e51fe875SMarcel Moolenaar return (error);
493681a5bbeSBoris Popov }
494681a5bbeSBoris Popov
495681a5bbeSBoris Popov static void
smb_vc_free(struct smb_connobj * cp)496681a5bbeSBoris Popov smb_vc_free(struct smb_connobj *cp)
497681a5bbeSBoris Popov {
498681a5bbeSBoris Popov struct smb_vc *vcp = CPTOVC(cp);
499681a5bbeSBoris Popov
500681a5bbeSBoris Popov if (vcp->vc_iod)
501681a5bbeSBoris Popov smb_iod_destroy(vcp->vc_iod);
502681a5bbeSBoris Popov SMB_STRFREE(vcp->vc_username);
503681a5bbeSBoris Popov SMB_STRFREE(vcp->vc_srvname);
504681a5bbeSBoris Popov SMB_STRFREE(vcp->vc_pass);
505681a5bbeSBoris Popov SMB_STRFREE(vcp->vc_domain);
506190b2c4fSTim J. Robbins if (vcp->vc_mackey)
507190b2c4fSTim J. Robbins free(vcp->vc_mackey, M_SMBTEMP);
508681a5bbeSBoris Popov if (vcp->vc_paddr)
509681a5bbeSBoris Popov free(vcp->vc_paddr, M_SONAME);
510681a5bbeSBoris Popov if (vcp->vc_laddr)
511681a5bbeSBoris Popov free(vcp->vc_laddr, M_SONAME);
512681a5bbeSBoris Popov if (vcp->vc_tolower)
513681a5bbeSBoris Popov iconv_close(vcp->vc_tolower);
514681a5bbeSBoris Popov if (vcp->vc_toupper)
515681a5bbeSBoris Popov iconv_close(vcp->vc_toupper);
516681a5bbeSBoris Popov if (vcp->vc_tolocal)
51741f1dcccSKevin Lo vcp->vc_tolocal = NULL;
518681a5bbeSBoris Popov if (vcp->vc_toserver)
51941f1dcccSKevin Lo vcp->vc_toserver = NULL;
52041f1dcccSKevin Lo if (vcp->vc_cp_tolocal)
52141f1dcccSKevin Lo iconv_close(vcp->vc_cp_tolocal);
52241f1dcccSKevin Lo if (vcp->vc_cp_toserver)
52341f1dcccSKevin Lo iconv_close(vcp->vc_cp_toserver);
52441f1dcccSKevin Lo if (vcp->vc_ucs_tolocal)
52541f1dcccSKevin Lo iconv_close(vcp->vc_ucs_tolocal);
52641f1dcccSKevin Lo if (vcp->vc_ucs_toserver)
52741f1dcccSKevin Lo iconv_close(vcp->vc_ucs_toserver);
528681a5bbeSBoris Popov smb_co_done(VCTOCP(vcp));
529681a5bbeSBoris Popov smb_sl_destroy(&vcp->vc_stlock);
530681a5bbeSBoris Popov free(vcp, M_SMBCONN);
531681a5bbeSBoris Popov }
532681a5bbeSBoris Popov
533681a5bbeSBoris Popov /*
534681a5bbeSBoris Popov * Called when use count of VC dropped to zero.
535681a5bbeSBoris Popov */
536681a5bbeSBoris Popov static void
smb_vc_gone(struct smb_connobj * cp,struct smb_cred * scred)537681a5bbeSBoris Popov smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred)
538681a5bbeSBoris Popov {
539681a5bbeSBoris Popov struct smb_vc *vcp = CPTOVC(cp);
540681a5bbeSBoris Popov
541681a5bbeSBoris Popov smb_vc_disconnect(vcp);
542681a5bbeSBoris Popov }
543681a5bbeSBoris Popov
544681a5bbeSBoris Popov void
smb_vc_ref(struct smb_vc * vcp)545fce6fbfaSBoris Popov smb_vc_ref(struct smb_vc *vcp)
546681a5bbeSBoris Popov {
547fce6fbfaSBoris Popov smb_co_ref(VCTOCP(vcp));
548681a5bbeSBoris Popov }
549681a5bbeSBoris Popov
550681a5bbeSBoris Popov void
smb_vc_rele(struct smb_vc * vcp,struct smb_cred * scred)551681a5bbeSBoris Popov smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred)
552681a5bbeSBoris Popov {
553681a5bbeSBoris Popov smb_co_rele(VCTOCP(vcp), scred);
554681a5bbeSBoris Popov }
555681a5bbeSBoris Popov
556681a5bbeSBoris Popov int
smb_vc_get(struct smb_vc * vcp,struct smb_cred * scred)557a4c05984SDavide Italiano smb_vc_get(struct smb_vc *vcp, struct smb_cred *scred)
558681a5bbeSBoris Popov {
559a4c05984SDavide Italiano struct smb_connobj *cp;
560a4c05984SDavide Italiano int error;
561a4c05984SDavide Italiano
562a4c05984SDavide Italiano cp = VCTOCP(vcp);
563a4c05984SDavide Italiano sx_xlock(&cp->co_interlock);
564a4c05984SDavide Italiano error = smb_co_get(cp, scred);
565a4c05984SDavide Italiano sx_unlock(&cp->co_interlock);
566a4c05984SDavide Italiano return error;
567681a5bbeSBoris Popov }
568681a5bbeSBoris Popov
569681a5bbeSBoris Popov void
smb_vc_put(struct smb_vc * vcp,struct smb_cred * scred)570681a5bbeSBoris Popov smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred)
571681a5bbeSBoris Popov {
572681a5bbeSBoris Popov smb_co_put(VCTOCP(vcp), scred);
573681a5bbeSBoris Popov }
574681a5bbeSBoris Popov
575681a5bbeSBoris Popov int
smb_vc_lock(struct smb_vc * vcp)576a4c05984SDavide Italiano smb_vc_lock(struct smb_vc *vcp)
577681a5bbeSBoris Popov {
578a4c05984SDavide Italiano struct smb_connobj *cp;
579a4c05984SDavide Italiano int error;
580a4c05984SDavide Italiano
581a4c05984SDavide Italiano cp = VCTOCP(vcp);
582a4c05984SDavide Italiano sx_xlock(&cp->co_interlock);
583a4c05984SDavide Italiano error = smb_co_lock(cp);
584a4c05984SDavide Italiano sx_unlock(&cp->co_interlock);
585a4c05984SDavide Italiano return error;
586681a5bbeSBoris Popov }
587681a5bbeSBoris Popov
588681a5bbeSBoris Popov void
smb_vc_unlock(struct smb_vc * vcp)589a4c05984SDavide Italiano smb_vc_unlock(struct smb_vc *vcp)
590681a5bbeSBoris Popov {
591a4c05984SDavide Italiano
592a4c05984SDavide Italiano struct smb_connobj *cp;
593a4c05984SDavide Italiano
594a4c05984SDavide Italiano cp = VCTOCP(vcp);
595a4c05984SDavide Italiano sx_xlock(&cp->co_interlock);
596a4c05984SDavide Italiano smb_co_unlock(cp);
597a4c05984SDavide Italiano sx_unlock(&cp->co_interlock);
598681a5bbeSBoris Popov }
599681a5bbeSBoris Popov
600681a5bbeSBoris Popov int
smb_vc_access(struct smb_vc * vcp,struct smb_cred * scred,mode_t mode)601681a5bbeSBoris Popov smb_vc_access(struct smb_vc *vcp, struct smb_cred *scred, mode_t mode)
602681a5bbeSBoris Popov {
603681a5bbeSBoris Popov struct ucred *cred = scred->scr_cred;
604681a5bbeSBoris Popov
605681a5bbeSBoris Popov if (smb_suser(cred) == 0 || cred->cr_uid == vcp->vc_uid)
606681a5bbeSBoris Popov return 0;
607681a5bbeSBoris Popov mode >>= 3;
608681a5bbeSBoris Popov if (!groupmember(vcp->vc_grp, cred))
609681a5bbeSBoris Popov mode >>= 3;
610681a5bbeSBoris Popov return (vcp->vc_mode & mode) == mode ? 0 : EACCES;
611681a5bbeSBoris Popov }
612681a5bbeSBoris Popov
613681a5bbeSBoris Popov static int
smb_vc_cmpshare(struct smb_share * ssp,struct smb_sharespec * dp)614681a5bbeSBoris Popov smb_vc_cmpshare(struct smb_share *ssp, struct smb_sharespec *dp)
615681a5bbeSBoris Popov {
616681a5bbeSBoris Popov int exact = 1;
617681a5bbeSBoris Popov
618681a5bbeSBoris Popov if (strcmp(ssp->ss_name, dp->name) != 0)
619681a5bbeSBoris Popov return 1;
620681a5bbeSBoris Popov if (dp->owner != SMBM_ANY_OWNER) {
621681a5bbeSBoris Popov if (ssp->ss_uid != dp->owner)
622681a5bbeSBoris Popov return 1;
623681a5bbeSBoris Popov } else
624681a5bbeSBoris Popov exact = 0;
625681a5bbeSBoris Popov if (dp->group != SMBM_ANY_GROUP) {
626681a5bbeSBoris Popov if (ssp->ss_grp != dp->group)
627681a5bbeSBoris Popov return 1;
628681a5bbeSBoris Popov } else
629681a5bbeSBoris Popov exact = 0;
630681a5bbeSBoris Popov
631681a5bbeSBoris Popov if (dp->mode & SMBM_EXACT) {
632681a5bbeSBoris Popov if (!exact)
633681a5bbeSBoris Popov return 1;
634681a5bbeSBoris Popov return (dp->mode & SMBM_MASK) == ssp->ss_mode ? 0 : 1;
635681a5bbeSBoris Popov }
636681a5bbeSBoris Popov if (smb_share_access(ssp, dp->scred, dp->mode) != 0)
637681a5bbeSBoris Popov return 1;
638681a5bbeSBoris Popov return 0;
639681a5bbeSBoris Popov }
640681a5bbeSBoris Popov
641681a5bbeSBoris Popov /*
642681a5bbeSBoris Popov * Lookup share in the given VC. Share referenced and locked on return.
643681a5bbeSBoris Popov * VC expected to be locked on entry and will be left locked on exit.
644681a5bbeSBoris Popov */
645681a5bbeSBoris Popov int
smb_vc_lookupshare(struct smb_vc * vcp,struct smb_sharespec * dp,struct smb_cred * scred,struct smb_share ** sspp)646681a5bbeSBoris Popov smb_vc_lookupshare(struct smb_vc *vcp, struct smb_sharespec *dp,
647681a5bbeSBoris Popov struct smb_cred *scred, struct smb_share **sspp)
648681a5bbeSBoris Popov {
649445e045bSAlexander Kabaev struct smb_connobj *scp = NULL;
650681a5bbeSBoris Popov struct smb_share *ssp = NULL;
651681a5bbeSBoris Popov int error;
652681a5bbeSBoris Popov
653681a5bbeSBoris Popov *sspp = NULL;
654681a5bbeSBoris Popov dp->scred = scred;
655445e045bSAlexander Kabaev SMBCO_FOREACH(scp, VCTOCP(vcp)) {
656445e045bSAlexander Kabaev ssp = (struct smb_share *)scp;
657a4c05984SDavide Italiano error = smb_share_lock(ssp);
658681a5bbeSBoris Popov if (error)
659681a5bbeSBoris Popov continue;
660681a5bbeSBoris Popov if (smb_vc_cmpshare(ssp, dp) == 0)
661681a5bbeSBoris Popov break;
662a4c05984SDavide Italiano smb_share_unlock(ssp);
663681a5bbeSBoris Popov }
664681a5bbeSBoris Popov if (ssp) {
665fce6fbfaSBoris Popov smb_share_ref(ssp);
666681a5bbeSBoris Popov *sspp = ssp;
667681a5bbeSBoris Popov error = 0;
668681a5bbeSBoris Popov } else
669681a5bbeSBoris Popov error = ENOENT;
670681a5bbeSBoris Popov return error;
671681a5bbeSBoris Popov }
672681a5bbeSBoris Popov
673681a5bbeSBoris Popov int
smb_vc_connect(struct smb_vc * vcp,struct smb_cred * scred)674681a5bbeSBoris Popov smb_vc_connect(struct smb_vc *vcp, struct smb_cred *scred)
675681a5bbeSBoris Popov {
676681a5bbeSBoris Popov
677681a5bbeSBoris Popov return smb_iod_request(vcp->vc_iod, SMBIOD_EV_CONNECT | SMBIOD_EV_SYNC, NULL);
678681a5bbeSBoris Popov }
679681a5bbeSBoris Popov
680681a5bbeSBoris Popov /*
681681a5bbeSBoris Popov * Destroy VC to server, invalidate shares linked with it.
682681a5bbeSBoris Popov * Transport should be locked on entry.
683681a5bbeSBoris Popov */
684681a5bbeSBoris Popov int
smb_vc_disconnect(struct smb_vc * vcp)685681a5bbeSBoris Popov smb_vc_disconnect(struct smb_vc *vcp)
686681a5bbeSBoris Popov {
687681a5bbeSBoris Popov
68869527b11SRick Macklem if (vcp->vc_iod != NULL)
68969527b11SRick Macklem smb_iod_request(vcp->vc_iod, SMBIOD_EV_DISCONNECT |
69069527b11SRick Macklem SMBIOD_EV_SYNC, NULL);
691681a5bbeSBoris Popov return 0;
692681a5bbeSBoris Popov }
693681a5bbeSBoris Popov
694681a5bbeSBoris Popov static char smb_emptypass[] = "";
695681a5bbeSBoris Popov
696681a5bbeSBoris Popov const char *
smb_vc_getpass(struct smb_vc * vcp)697681a5bbeSBoris Popov smb_vc_getpass(struct smb_vc *vcp)
698681a5bbeSBoris Popov {
699681a5bbeSBoris Popov if (vcp->vc_pass)
700681a5bbeSBoris Popov return vcp->vc_pass;
701681a5bbeSBoris Popov return smb_emptypass;
702681a5bbeSBoris Popov }
703681a5bbeSBoris Popov
704681a5bbeSBoris Popov static int
smb_vc_getinfo(struct smb_vc * vcp,struct smb_vc_info * vip)705681a5bbeSBoris Popov smb_vc_getinfo(struct smb_vc *vcp, struct smb_vc_info *vip)
706681a5bbeSBoris Popov {
707681a5bbeSBoris Popov bzero(vip, sizeof(struct smb_vc_info));
708681a5bbeSBoris Popov vip->itype = SMB_INFO_VC;
709681a5bbeSBoris Popov vip->usecount = vcp->obj.co_usecount;
710681a5bbeSBoris Popov vip->uid = vcp->vc_uid;
711681a5bbeSBoris Popov vip->gid = vcp->vc_grp;
712681a5bbeSBoris Popov vip->mode = vcp->vc_mode;
713681a5bbeSBoris Popov vip->flags = vcp->obj.co_flags;
714681a5bbeSBoris Popov vip->sopt = vcp->vc_sopt;
715681a5bbeSBoris Popov vip->iodstate = vcp->vc_iod->iod_state;
716681a5bbeSBoris Popov bzero(&vip->sopt.sv_skey, sizeof(vip->sopt.sv_skey));
717681a5bbeSBoris Popov snprintf(vip->srvname, sizeof(vip->srvname), "%s", vcp->vc_srvname);
718681a5bbeSBoris Popov snprintf(vip->vcname, sizeof(vip->vcname), "%s", vcp->vc_username);
719681a5bbeSBoris Popov return 0;
720681a5bbeSBoris Popov }
721681a5bbeSBoris Popov
722681a5bbeSBoris Popov u_short
smb_vc_nextmid(struct smb_vc * vcp)723681a5bbeSBoris Popov smb_vc_nextmid(struct smb_vc *vcp)
724681a5bbeSBoris Popov {
725681a5bbeSBoris Popov u_short r;
726681a5bbeSBoris Popov
727a4c05984SDavide Italiano sx_xlock(&vcp->obj.co_interlock);
728681a5bbeSBoris Popov r = vcp->vc_mid++;
729a4c05984SDavide Italiano sx_unlock(&vcp->obj.co_interlock);
730681a5bbeSBoris Popov return r;
731681a5bbeSBoris Popov }
732681a5bbeSBoris Popov
733681a5bbeSBoris Popov /*
734681a5bbeSBoris Popov * Share implementation
735681a5bbeSBoris Popov */
736681a5bbeSBoris Popov /*
737681a5bbeSBoris Popov * Allocate share structure and attach it to the given VC
738681a5bbeSBoris Popov * Connection expected to be locked on entry. Share will be returned
739681a5bbeSBoris Popov * in locked state.
740681a5bbeSBoris Popov */
741681a5bbeSBoris Popov int
smb_share_create(struct smb_vc * vcp,struct smb_sharespec * shspec,struct smb_cred * scred,struct smb_share ** sspp)742681a5bbeSBoris Popov smb_share_create(struct smb_vc *vcp, struct smb_sharespec *shspec,
743681a5bbeSBoris Popov struct smb_cred *scred, struct smb_share **sspp)
744681a5bbeSBoris Popov {
745681a5bbeSBoris Popov struct smb_share *ssp;
746681a5bbeSBoris Popov struct ucred *cred = scred->scr_cred;
747681a5bbeSBoris Popov uid_t realuid = cred->cr_uid;
748681a5bbeSBoris Popov uid_t uid = shspec->owner;
749681a5bbeSBoris Popov gid_t gid = shspec->group;
750681a5bbeSBoris Popov int error, isroot;
751681a5bbeSBoris Popov
752681a5bbeSBoris Popov isroot = smb_suser(cred) == 0;
753681a5bbeSBoris Popov /*
754681a5bbeSBoris Popov * Only superuser can create shares with different uid and gid
755681a5bbeSBoris Popov */
756681a5bbeSBoris Popov if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot)
757681a5bbeSBoris Popov return EPERM;
758681a5bbeSBoris Popov if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot)
759681a5bbeSBoris Popov return EPERM;
760681a5bbeSBoris Popov error = smb_vc_lookupshare(vcp, shspec, scred, &ssp);
761681a5bbeSBoris Popov if (!error) {
762681a5bbeSBoris Popov smb_share_put(ssp, scred);
763681a5bbeSBoris Popov return EEXIST;
764681a5bbeSBoris Popov }
765681a5bbeSBoris Popov if (uid == SMBM_ANY_OWNER)
766681a5bbeSBoris Popov uid = realuid;
767681a5bbeSBoris Popov if (gid == SMBM_ANY_GROUP)
768681a5bbeSBoris Popov gid = cred->cr_groups[0];
769a163d034SWarner Losh ssp = smb_zmalloc(sizeof(*ssp), M_SMBCONN, M_WAITOK);
770e3b3762bSRobert Watson smb_co_init(SSTOCP(ssp), SMBL_SHARE, "smbss ilock", "smbss");
771681a5bbeSBoris Popov ssp->obj.co_free = smb_share_free;
772681a5bbeSBoris Popov ssp->obj.co_gone = smb_share_gone;
773681a5bbeSBoris Popov smb_sl_init(&ssp->ss_stlock, "ssstlock");
774681a5bbeSBoris Popov ssp->ss_name = smb_strdup(shspec->name);
775681a5bbeSBoris Popov if (shspec->pass && shspec->pass[0])
776681a5bbeSBoris Popov ssp->ss_pass = smb_strdup(shspec->pass);
777681a5bbeSBoris Popov ssp->ss_type = shspec->stype;
778681a5bbeSBoris Popov ssp->ss_tid = SMB_TID_UNKNOWN;
779681a5bbeSBoris Popov ssp->ss_uid = uid;
780681a5bbeSBoris Popov ssp->ss_grp = gid;
781681a5bbeSBoris Popov ssp->ss_mode = shspec->rights & SMBM_MASK;
782681a5bbeSBoris Popov smb_co_addchild(VCTOCP(vcp), SSTOCP(ssp));
783681a5bbeSBoris Popov *sspp = ssp;
784681a5bbeSBoris Popov return 0;
785681a5bbeSBoris Popov }
786681a5bbeSBoris Popov
787681a5bbeSBoris Popov static void
smb_share_free(struct smb_connobj * cp)788681a5bbeSBoris Popov smb_share_free(struct smb_connobj *cp)
789681a5bbeSBoris Popov {
790681a5bbeSBoris Popov struct smb_share *ssp = CPTOSS(cp);
791681a5bbeSBoris Popov
792681a5bbeSBoris Popov SMB_STRFREE(ssp->ss_name);
793681a5bbeSBoris Popov SMB_STRFREE(ssp->ss_pass);
794681a5bbeSBoris Popov smb_sl_destroy(&ssp->ss_stlock);
795681a5bbeSBoris Popov smb_co_done(SSTOCP(ssp));
796681a5bbeSBoris Popov free(ssp, M_SMBCONN);
797681a5bbeSBoris Popov }
798681a5bbeSBoris Popov
799681a5bbeSBoris Popov static void
smb_share_gone(struct smb_connobj * cp,struct smb_cred * scred)800681a5bbeSBoris Popov smb_share_gone(struct smb_connobj *cp, struct smb_cred *scred)
801681a5bbeSBoris Popov {
802681a5bbeSBoris Popov struct smb_share *ssp = CPTOSS(cp);
803681a5bbeSBoris Popov
804681a5bbeSBoris Popov smb_smb_treedisconnect(ssp, scred);
805681a5bbeSBoris Popov }
806681a5bbeSBoris Popov
807681a5bbeSBoris Popov void
smb_share_ref(struct smb_share * ssp)808fce6fbfaSBoris Popov smb_share_ref(struct smb_share *ssp)
809681a5bbeSBoris Popov {
810fce6fbfaSBoris Popov smb_co_ref(SSTOCP(ssp));
811681a5bbeSBoris Popov }
812681a5bbeSBoris Popov
813681a5bbeSBoris Popov void
smb_share_rele(struct smb_share * ssp,struct smb_cred * scred)814681a5bbeSBoris Popov smb_share_rele(struct smb_share *ssp, struct smb_cred *scred)
815681a5bbeSBoris Popov {
816681a5bbeSBoris Popov smb_co_rele(SSTOCP(ssp), scred);
817681a5bbeSBoris Popov }
818681a5bbeSBoris Popov
819681a5bbeSBoris Popov int
smb_share_get(struct smb_share * ssp,struct smb_cred * scred)820a4c05984SDavide Italiano smb_share_get(struct smb_share *ssp, struct smb_cred *scred)
821681a5bbeSBoris Popov {
822a4c05984SDavide Italiano struct smb_connobj *cp = SSTOCP(ssp);
823a4c05984SDavide Italiano int error;
824a4c05984SDavide Italiano
825a4c05984SDavide Italiano sx_xlock(&cp->co_interlock);
826a4c05984SDavide Italiano error = smb_co_get(cp, scred);
827a4c05984SDavide Italiano sx_unlock(&cp->co_interlock);
828a4c05984SDavide Italiano return error;
829681a5bbeSBoris Popov }
830681a5bbeSBoris Popov
831681a5bbeSBoris Popov void
smb_share_put(struct smb_share * ssp,struct smb_cred * scred)832681a5bbeSBoris Popov smb_share_put(struct smb_share *ssp, struct smb_cred *scred)
833681a5bbeSBoris Popov {
834a4c05984SDavide Italiano
835681a5bbeSBoris Popov smb_co_put(SSTOCP(ssp), scred);
836681a5bbeSBoris Popov }
837681a5bbeSBoris Popov
838681a5bbeSBoris Popov int
smb_share_lock(struct smb_share * ssp)839a4c05984SDavide Italiano smb_share_lock(struct smb_share *ssp)
840681a5bbeSBoris Popov {
841a4c05984SDavide Italiano struct smb_connobj *cp;
842a4c05984SDavide Italiano int error;
843a4c05984SDavide Italiano
844a4c05984SDavide Italiano cp = SSTOCP(ssp);
845a4c05984SDavide Italiano sx_xlock(&cp->co_interlock);
846a4c05984SDavide Italiano error = smb_co_lock(cp);
847a4c05984SDavide Italiano sx_unlock(&cp->co_interlock);
848a4c05984SDavide Italiano return error;
849681a5bbeSBoris Popov }
850681a5bbeSBoris Popov
851681a5bbeSBoris Popov void
smb_share_unlock(struct smb_share * ssp)852a4c05984SDavide Italiano smb_share_unlock(struct smb_share *ssp)
853681a5bbeSBoris Popov {
854a4c05984SDavide Italiano struct smb_connobj *cp;
855a4c05984SDavide Italiano
856a4c05984SDavide Italiano cp = SSTOCP(ssp);
857a4c05984SDavide Italiano sx_xlock(&cp->co_interlock);
858a4c05984SDavide Italiano smb_co_unlock(cp);
859a4c05984SDavide Italiano sx_unlock(&cp->co_interlock);
860681a5bbeSBoris Popov }
861681a5bbeSBoris Popov
862681a5bbeSBoris Popov int
smb_share_access(struct smb_share * ssp,struct smb_cred * scred,mode_t mode)863681a5bbeSBoris Popov smb_share_access(struct smb_share *ssp, struct smb_cred *scred, mode_t mode)
864681a5bbeSBoris Popov {
865681a5bbeSBoris Popov struct ucred *cred = scred->scr_cred;
866681a5bbeSBoris Popov
867681a5bbeSBoris Popov if (smb_suser(cred) == 0 || cred->cr_uid == ssp->ss_uid)
868681a5bbeSBoris Popov return 0;
869681a5bbeSBoris Popov mode >>= 3;
870681a5bbeSBoris Popov if (!groupmember(ssp->ss_grp, cred))
871681a5bbeSBoris Popov mode >>= 3;
872681a5bbeSBoris Popov return (ssp->ss_mode & mode) == mode ? 0 : EACCES;
873681a5bbeSBoris Popov }
874681a5bbeSBoris Popov
875681a5bbeSBoris Popov void
smb_share_invalidate(struct smb_share * ssp)876681a5bbeSBoris Popov smb_share_invalidate(struct smb_share *ssp)
877681a5bbeSBoris Popov {
878681a5bbeSBoris Popov ssp->ss_tid = SMB_TID_UNKNOWN;
879681a5bbeSBoris Popov }
880681a5bbeSBoris Popov
881681a5bbeSBoris Popov int
smb_share_valid(struct smb_share * ssp)882681a5bbeSBoris Popov smb_share_valid(struct smb_share *ssp)
883681a5bbeSBoris Popov {
884681a5bbeSBoris Popov return ssp->ss_tid != SMB_TID_UNKNOWN &&
885681a5bbeSBoris Popov ssp->ss_vcgenid == SSTOVC(ssp)->vc_genid;
886681a5bbeSBoris Popov }
887681a5bbeSBoris Popov
888681a5bbeSBoris Popov const char*
smb_share_getpass(struct smb_share * ssp)889681a5bbeSBoris Popov smb_share_getpass(struct smb_share *ssp)
890681a5bbeSBoris Popov {
891681a5bbeSBoris Popov struct smb_vc *vcp;
892681a5bbeSBoris Popov
893681a5bbeSBoris Popov if (ssp->ss_pass)
894681a5bbeSBoris Popov return ssp->ss_pass;
895681a5bbeSBoris Popov vcp = SSTOVC(ssp);
896681a5bbeSBoris Popov if (vcp->vc_pass)
897681a5bbeSBoris Popov return vcp->vc_pass;
898681a5bbeSBoris Popov return smb_emptypass;
899681a5bbeSBoris Popov }
900681a5bbeSBoris Popov
901681a5bbeSBoris Popov static int
smb_share_getinfo(struct smb_share * ssp,struct smb_share_info * sip)902681a5bbeSBoris Popov smb_share_getinfo(struct smb_share *ssp, struct smb_share_info *sip)
903681a5bbeSBoris Popov {
904681a5bbeSBoris Popov bzero(sip, sizeof(struct smb_share_info));
905681a5bbeSBoris Popov sip->itype = SMB_INFO_SHARE;
906681a5bbeSBoris Popov sip->usecount = ssp->obj.co_usecount;
907681a5bbeSBoris Popov sip->tid = ssp->ss_tid;
908681a5bbeSBoris Popov sip->type= ssp->ss_type;
909681a5bbeSBoris Popov sip->uid = ssp->ss_uid;
910681a5bbeSBoris Popov sip->gid = ssp->ss_grp;
911681a5bbeSBoris Popov sip->mode= ssp->ss_mode;
912681a5bbeSBoris Popov sip->flags = ssp->obj.co_flags;
913681a5bbeSBoris Popov snprintf(sip->sname, sizeof(sip->sname), "%s", ssp->ss_name);
914681a5bbeSBoris Popov return 0;
915681a5bbeSBoris Popov }
916681a5bbeSBoris Popov
917681a5bbeSBoris Popov /*
918681a5bbeSBoris Popov * Dump an entire tree into sysctl call
919681a5bbeSBoris Popov */
920681a5bbeSBoris Popov static int
smb_sysctl_treedump(SYSCTL_HANDLER_ARGS)921681a5bbeSBoris Popov smb_sysctl_treedump(SYSCTL_HANDLER_ARGS)
922681a5bbeSBoris Popov {
923445e045bSAlexander Kabaev struct smb_connobj *scp1, *scp2;
924681a5bbeSBoris Popov struct smb_vc *vcp;
925681a5bbeSBoris Popov struct smb_share *ssp;
926681a5bbeSBoris Popov struct smb_vc_info vci;
927681a5bbeSBoris Popov struct smb_share_info ssi;
928681a5bbeSBoris Popov int error, itype;
929681a5bbeSBoris Popov
93047934cefSDon Lewis error = sysctl_wire_old_buffer(req, 0);
93147934cefSDon Lewis if (error)
93247934cefSDon Lewis return (error);
933a4c05984SDavide Italiano error = smb_sm_lockvclist();
934681a5bbeSBoris Popov if (error)
935681a5bbeSBoris Popov return error;
936445e045bSAlexander Kabaev SMBCO_FOREACH(scp1, &smb_vclist) {
937445e045bSAlexander Kabaev vcp = (struct smb_vc *)scp1;
938a4c05984SDavide Italiano error = smb_vc_lock(vcp);
939681a5bbeSBoris Popov if (error)
940681a5bbeSBoris Popov continue;
941681a5bbeSBoris Popov smb_vc_getinfo(vcp, &vci);
942681a5bbeSBoris Popov error = SYSCTL_OUT(req, &vci, sizeof(struct smb_vc_info));
943681a5bbeSBoris Popov if (error) {
944a4c05984SDavide Italiano smb_vc_unlock(vcp);
945681a5bbeSBoris Popov break;
946681a5bbeSBoris Popov }
947445e045bSAlexander Kabaev SMBCO_FOREACH(scp2, VCTOCP(vcp)) {
948445e045bSAlexander Kabaev ssp = (struct smb_share *)scp2;
949a4c05984SDavide Italiano error = smb_share_lock(ssp);
950681a5bbeSBoris Popov if (error) {
951681a5bbeSBoris Popov error = 0;
952681a5bbeSBoris Popov continue;
953681a5bbeSBoris Popov }
954681a5bbeSBoris Popov smb_share_getinfo(ssp, &ssi);
955a4c05984SDavide Italiano smb_share_unlock(ssp);
956681a5bbeSBoris Popov error = SYSCTL_OUT(req, &ssi, sizeof(struct smb_share_info));
957681a5bbeSBoris Popov if (error)
958681a5bbeSBoris Popov break;
959681a5bbeSBoris Popov }
960a4c05984SDavide Italiano smb_vc_unlock(vcp);
961681a5bbeSBoris Popov if (error)
962681a5bbeSBoris Popov break;
963681a5bbeSBoris Popov }
964681a5bbeSBoris Popov if (!error) {
965681a5bbeSBoris Popov itype = SMB_INFO_NONE;
966681a5bbeSBoris Popov error = SYSCTL_OUT(req, &itype, sizeof(itype));
967681a5bbeSBoris Popov }
968e3b3762bSRobert Watson smb_sm_unlockvclist();
969681a5bbeSBoris Popov return error;
970681a5bbeSBoris Popov }
971