Deleted Added
full compact
smb_conn.c (116189) smb_conn.c (119376)
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
33/*
34 * Connection engine.
35 */
36
37#include <sys/cdefs.h>
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
33/*
34 * Connection engine.
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/sys/netsmb/smb_conn.c 116189 2003-06-11 05:37:42Z obrien $");
38__FBSDID("$FreeBSD: head/sys/netsmb/smb_conn.c 119376 2003-08-23 21:43:33Z marcel $");
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/malloc.h>
44#include <sys/proc.h>
45#include <sys/lock.h>
46#include <sys/sysctl.h>
47#include <sys/socketvar.h>
48
49#include <sys/iconv.h>
50
51#include <netsmb/smb.h>
52#include <netsmb/smb_subr.h>
53#include <netsmb/smb_conn.h>
54#include <netsmb/smb_tran.h>
55#include <netsmb/smb_trantcp.h>
56
57static struct smb_connobj smb_vclist;
58static int smb_vcnext = 1; /* next unique id for VC */
59
60SYSCTL_NODE(_net, OID_AUTO, smb, CTLFLAG_RW, NULL, "SMB protocol");
61
62MALLOC_DEFINE(M_SMBCONN, "SMB conn", "SMB connection");
63
64static void smb_co_init(struct smb_connobj *cp, int level, char *objname,
65 struct thread *td);
66static void smb_co_done(struct smb_connobj *cp);
67static int smb_co_lockstatus(struct smb_connobj *cp, struct thread *td);
68
69static int smb_vc_disconnect(struct smb_vc *vcp);
70static void smb_vc_free(struct smb_connobj *cp);
71static void smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred);
72static smb_co_free_t smb_share_free;
73static smb_co_gone_t smb_share_gone;
74
75static int smb_sysctl_treedump(SYSCTL_HANDLER_ARGS);
76
77SYSCTL_PROC(_net_smb, OID_AUTO, treedump, CTLFLAG_RD | CTLTYPE_OPAQUE,
78 NULL, 0, smb_sysctl_treedump, "S,treedump", "Requester tree");
79
80int
81smb_sm_init(void)
82{
83
84 smb_co_init(&smb_vclist, SMBL_SM, "smbsm", curthread);
85 smb_co_unlock(&smb_vclist, 0, curthread);
86 return 0;
87}
88
89int
90smb_sm_done(void)
91{
92
93 /* XXX: hold the mutex */
94 if (smb_vclist.co_usecount > 1) {
95 SMBERROR("%d connections still active\n", smb_vclist.co_usecount - 1);
96 return EBUSY;
97 }
98 smb_co_done(&smb_vclist);
99 return 0;
100}
101
102static int
103smb_sm_lockvclist(int flags, struct thread *td)
104{
105
106 return smb_co_lock(&smb_vclist, flags | LK_CANRECURSE, td);
107}
108
109static void
110smb_sm_unlockvclist(struct thread *td)
111{
112
113 smb_co_unlock(&smb_vclist, LK_RELEASE, td);
114}
115
116static int
117smb_sm_lookupint(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
118 struct smb_cred *scred, struct smb_vc **vcpp)
119{
120 struct thread *td = scred->scr_td;
121 struct smb_vc *vcp;
122 int exact = 1;
123 int error;
124
125 vcspec->shspec = shspec;
126 error = ENOENT;
127 SMBCO_FOREACH((struct smb_connobj*)vcp, &smb_vclist) {
128 error = smb_vc_lock(vcp, LK_EXCLUSIVE, td);
129 if (error)
130 continue;
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/malloc.h>
44#include <sys/proc.h>
45#include <sys/lock.h>
46#include <sys/sysctl.h>
47#include <sys/socketvar.h>
48
49#include <sys/iconv.h>
50
51#include <netsmb/smb.h>
52#include <netsmb/smb_subr.h>
53#include <netsmb/smb_conn.h>
54#include <netsmb/smb_tran.h>
55#include <netsmb/smb_trantcp.h>
56
57static struct smb_connobj smb_vclist;
58static int smb_vcnext = 1; /* next unique id for VC */
59
60SYSCTL_NODE(_net, OID_AUTO, smb, CTLFLAG_RW, NULL, "SMB protocol");
61
62MALLOC_DEFINE(M_SMBCONN, "SMB conn", "SMB connection");
63
64static void smb_co_init(struct smb_connobj *cp, int level, char *objname,
65 struct thread *td);
66static void smb_co_done(struct smb_connobj *cp);
67static int smb_co_lockstatus(struct smb_connobj *cp, struct thread *td);
68
69static int smb_vc_disconnect(struct smb_vc *vcp);
70static void smb_vc_free(struct smb_connobj *cp);
71static void smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred);
72static smb_co_free_t smb_share_free;
73static smb_co_gone_t smb_share_gone;
74
75static int smb_sysctl_treedump(SYSCTL_HANDLER_ARGS);
76
77SYSCTL_PROC(_net_smb, OID_AUTO, treedump, CTLFLAG_RD | CTLTYPE_OPAQUE,
78 NULL, 0, smb_sysctl_treedump, "S,treedump", "Requester tree");
79
80int
81smb_sm_init(void)
82{
83
84 smb_co_init(&smb_vclist, SMBL_SM, "smbsm", curthread);
85 smb_co_unlock(&smb_vclist, 0, curthread);
86 return 0;
87}
88
89int
90smb_sm_done(void)
91{
92
93 /* XXX: hold the mutex */
94 if (smb_vclist.co_usecount > 1) {
95 SMBERROR("%d connections still active\n", smb_vclist.co_usecount - 1);
96 return EBUSY;
97 }
98 smb_co_done(&smb_vclist);
99 return 0;
100}
101
102static int
103smb_sm_lockvclist(int flags, struct thread *td)
104{
105
106 return smb_co_lock(&smb_vclist, flags | LK_CANRECURSE, td);
107}
108
109static void
110smb_sm_unlockvclist(struct thread *td)
111{
112
113 smb_co_unlock(&smb_vclist, LK_RELEASE, td);
114}
115
116static int
117smb_sm_lookupint(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
118 struct smb_cred *scred, struct smb_vc **vcpp)
119{
120 struct thread *td = scred->scr_td;
121 struct smb_vc *vcp;
122 int exact = 1;
123 int error;
124
125 vcspec->shspec = shspec;
126 error = ENOENT;
127 SMBCO_FOREACH((struct smb_connobj*)vcp, &smb_vclist) {
128 error = smb_vc_lock(vcp, LK_EXCLUSIVE, td);
129 if (error)
130 continue;
131 itry {
132 if ((vcp->obj.co_flags & SMBV_PRIVATE) ||
133 !CONNADDREQ(vcp->vc_paddr, vcspec->sap) ||
134 strcmp(vcp->vc_username, vcspec->username) != 0)
135 ithrow(1);
136 if (vcspec->owner != SMBM_ANY_OWNER) {
137 if (vcp->vc_uid != vcspec->owner)
138 ithrow(1);
139 } else
140 exact = 0;
141 if (vcspec->group != SMBM_ANY_GROUP) {
142 if (vcp->vc_grp != vcspec->group)
143 ithrow(1);
144 } else
145 exact = 0;
146
131
147 if (vcspec->mode & SMBM_EXACT) {
148 if (!exact ||
149 (vcspec->mode & SMBM_MASK) != vcp->vc_mode)
150 ithrow(1);
151 }
152 if (smb_vc_access(vcp, scred, vcspec->mode) != 0)
153 ithrow(1);
154 vcspec->ssp = NULL;
155 if (shspec)
156 ithrow(smb_vc_lookupshare(vcp, shspec, scred, &vcspec->ssp));
157 error = 0;
158 break;
159 } icatch(error) {
160 smb_vc_unlock(vcp, 0, td);
161 } ifinally {
162 } iendtry;
163 if (error == 0)
164 break;
132 if ((vcp->obj.co_flags & SMBV_PRIVATE) ||
133 !CONNADDREQ(vcp->vc_paddr, vcspec->sap) ||
134 strcmp(vcp->vc_username, vcspec->username) != 0)
135 goto err1;
136 if (vcspec->owner != SMBM_ANY_OWNER) {
137 if (vcp->vc_uid != vcspec->owner)
138 goto err1;
139 } else
140 exact = 0;
141 if (vcspec->group != SMBM_ANY_GROUP) {
142 if (vcp->vc_grp != vcspec->group)
143 goto err1;
144 } else
145 exact = 0;
146 if (vcspec->mode & SMBM_EXACT) {
147 if (!exact || (vcspec->mode & SMBM_MASK) !=
148 vcp->vc_mode)
149 goto err1;
150 }
151 if (smb_vc_access(vcp, scred, vcspec->mode) != 0)
152 goto err1;
153 vcspec->ssp = NULL;
154 if (shspec) {
155 error = (int)smb_vc_lookupshare(vcp, shspec, scred,
156 &vcspec->ssp);
157 if (error)
158 goto fail;
159 }
160 error = 0;
161 break;
162 err1:
163 error = 1;
164 fail:
165 smb_vc_unlock(vcp, 0, td);
165 }
166 if (vcp) {
167 smb_vc_ref(vcp);
168 *vcpp = vcp;
169 }
166 }
167 if (vcp) {
168 smb_vc_ref(vcp);
169 *vcpp = vcp;
170 }
170 return error;
171 return (error);
171}
172
173int
174smb_sm_lookup(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
175 struct smb_cred *scred, struct smb_vc **vcpp)
176{
177 struct thread *td = scred->scr_td;
178 struct smb_vc *vcp;
179 struct smb_share *ssp = NULL;
180 int error;
181
182 *vcpp = vcp = NULL;
183
184 error = smb_sm_lockvclist(LK_EXCLUSIVE, td);
185 if (error)
186 return error;
187 error = smb_sm_lookupint(vcspec, shspec, scred, vcpp);
188 if (error == 0 || (vcspec->flags & SMBV_CREATE) == 0) {
189 smb_sm_unlockvclist(td);
190 return error;
191 }
192 error = smb_sm_lookupint(vcspec, NULL, scred, &vcp);
193 if (error) {
194 error = smb_vc_create(vcspec, scred, &vcp);
195 if (error)
196 goto out;
197 error = smb_vc_connect(vcp, scred);
198 if (error)
199 goto out;
200 }
201 if (shspec == NULL)
202 goto out;
203 error = smb_share_create(vcp, shspec, scred, &ssp);
204 if (error)
205 goto out;
206 error = smb_smb_treeconnect(ssp, scred);
207 if (error == 0)
208 vcspec->ssp = ssp;
209 else
210 smb_share_put(ssp, scred);
211out:
212 smb_sm_unlockvclist(td);
213 if (error == 0)
214 *vcpp = vcp;
215 else if (vcp)
216 smb_vc_put(vcp, scred);
217 return error;
218}
219
220/*
221 * Common code for connection object
222 */
223static void
224smb_co_init(struct smb_connobj *cp, int level, char *objname, struct thread *td)
225{
226 SLIST_INIT(&cp->co_children);
227 smb_sl_init(&cp->co_interlock, objname);
228 lockinit(&cp->co_lock, PZERO, objname, 0, 0);
229 cp->co_level = level;
230 cp->co_usecount = 1;
231 KASSERT(smb_co_lock(cp, LK_EXCLUSIVE, td) == 0, ("smb_co_init: lock failed"));
232}
233
234static void
235smb_co_done(struct smb_connobj *cp)
236{
237 smb_sl_destroy(&cp->co_interlock);
238 lockdestroy(&cp->co_lock);
239}
240
241static void
242smb_co_gone(struct smb_connobj *cp, struct smb_cred *scred)
243{
244 struct smb_connobj *parent;
245
246 if (cp->co_gone)
247 cp->co_gone(cp, scred);
248 parent = cp->co_parent;
249 if (parent) {
250 smb_co_lock(parent, LK_EXCLUSIVE, scred->scr_td);
251 SLIST_REMOVE(&parent->co_children, cp, smb_connobj, co_next);
252 smb_co_put(parent, scred);
253 }
254 if (cp->co_free)
255 cp->co_free(cp);
256}
257
258void
259smb_co_ref(struct smb_connobj *cp)
260{
261
262 SMB_CO_LOCK(cp);
263 cp->co_usecount++;
264 SMB_CO_UNLOCK(cp);
265}
266
267void
268smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred)
269{
270 struct thread *td = scred->scr_td;
271
272 SMB_CO_LOCK(cp);
273 if (cp->co_usecount > 1) {
274 cp->co_usecount--;
275 SMB_CO_UNLOCK(cp);
276 return;
277 }
278 if (cp->co_usecount == 0) {
279 SMBERROR("negative use_count for object %d", cp->co_level);
280 SMB_CO_UNLOCK(cp);
281 return;
282 }
283 cp->co_usecount--;
284 cp->co_flags |= SMBO_GONE;
285
286 lockmgr(&cp->co_lock, LK_DRAIN | LK_INTERLOCK, &cp->co_interlock, td);
287 smb_co_gone(cp, scred);
288}
289
290int
291smb_co_get(struct smb_connobj *cp, int flags, struct smb_cred *scred)
292{
293 int error;
294
295 if ((flags & LK_INTERLOCK) == 0)
296 SMB_CO_LOCK(cp);
297 cp->co_usecount++;
298 error = smb_co_lock(cp, flags | LK_INTERLOCK, scred->scr_td);
299 if (error) {
300 SMB_CO_LOCK(cp);
301 cp->co_usecount--;
302 SMB_CO_UNLOCK(cp);
303 return error;
304 }
305 return 0;
306}
307
308void
309smb_co_put(struct smb_connobj *cp, struct smb_cred *scred)
310{
311 struct thread *td = scred->scr_td;
312 int flags;
313
314 flags = LK_RELEASE;
315 SMB_CO_LOCK(cp);
316 if (cp->co_usecount > 1) {
317 cp->co_usecount--;
318 } else if (cp->co_usecount == 1) {
319 cp->co_usecount--;
320 cp->co_flags |= SMBO_GONE;
321 flags = LK_DRAIN;
322 } else {
323 SMBERROR("negative usecount");
324 }
325 lockmgr(&cp->co_lock, LK_RELEASE | LK_INTERLOCK, &cp->co_interlock, td);
326 if ((cp->co_flags & SMBO_GONE) == 0)
327 return;
328 lockmgr(&cp->co_lock, LK_DRAIN, NULL, td);
329 smb_co_gone(cp, scred);
330}
331
332int
333smb_co_lockstatus(struct smb_connobj *cp, struct thread *td)
334{
335 return lockstatus(&cp->co_lock, td);
336}
337
338int
339smb_co_lock(struct smb_connobj *cp, int flags, struct thread *td)
340{
341
342 if (cp->co_flags & SMBO_GONE)
343 return EINVAL;
344 if ((flags & LK_TYPE_MASK) == 0)
345 flags |= LK_EXCLUSIVE;
346 if (smb_co_lockstatus(cp, td) == LK_EXCLUSIVE &&
347 (flags & LK_CANRECURSE) == 0) {
348 SMBERROR("recursive lock for object %d\n", cp->co_level);
349 return 0;
350 }
351 return lockmgr(&cp->co_lock, flags, &cp->co_interlock, td);
352}
353
354void
355smb_co_unlock(struct smb_connobj *cp, int flags, struct thread *td)
356{
357 (void)lockmgr(&cp->co_lock, flags | LK_RELEASE, &cp->co_interlock, td);
358}
359
360static void
361smb_co_addchild(struct smb_connobj *parent, struct smb_connobj *child)
362{
363 KASSERT(smb_co_lockstatus(parent, curthread) == LK_EXCLUSIVE, ("smb_co_addchild: parent not locked"));
364 KASSERT(smb_co_lockstatus(child, curthread) == LK_EXCLUSIVE, ("smb_co_addchild: child not locked"));
365
366 smb_co_ref(parent);
367 SLIST_INSERT_HEAD(&parent->co_children, child, co_next);
368 child->co_parent = parent;
369}
370
371/*
372 * Session implementation
373 */
374
375int
376smb_vc_create(struct smb_vcspec *vcspec,
377 struct smb_cred *scred, struct smb_vc **vcpp)
378{
379 struct smb_vc *vcp;
380 struct thread *td = scred->scr_td;
381 struct ucred *cred = scred->scr_cred;
382 uid_t uid = vcspec->owner;
383 gid_t gid = vcspec->group;
384 uid_t realuid = cred->cr_uid;
385 char *domain = vcspec->domain;
386 int error, isroot;
387
388 isroot = smb_suser(cred) == 0;
389 /*
390 * Only superuser can create VCs with different uid and gid
391 */
392 if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot)
393 return EPERM;
394 if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot)
395 return EPERM;
396
397 vcp = smb_zmalloc(sizeof(*vcp), M_SMBCONN, M_WAITOK);
398 smb_co_init(VCTOCP(vcp), SMBL_VC, "smb_vc", td);
399 vcp->obj.co_free = smb_vc_free;
400 vcp->obj.co_gone = smb_vc_gone;
401 vcp->vc_number = smb_vcnext++;
402 vcp->vc_timo = SMB_DEFRQTIMO;
403 vcp->vc_smbuid = SMB_UID_UNKNOWN;
404 vcp->vc_mode = vcspec->rights & SMBM_MASK;
405 vcp->obj.co_flags = vcspec->flags & (SMBV_PRIVATE | SMBV_SINGLESHARE);
406 vcp->vc_tdesc = &smb_tran_nbtcp_desc;
407
408 if (uid == SMBM_ANY_OWNER)
409 uid = realuid;
410 if (gid == SMBM_ANY_GROUP)
411 gid = cred->cr_groups[0];
412 vcp->vc_uid = uid;
413 vcp->vc_grp = gid;
414
415 smb_sl_init(&vcp->vc_stlock, "vcstlock");
172}
173
174int
175smb_sm_lookup(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
176 struct smb_cred *scred, struct smb_vc **vcpp)
177{
178 struct thread *td = scred->scr_td;
179 struct smb_vc *vcp;
180 struct smb_share *ssp = NULL;
181 int error;
182
183 *vcpp = vcp = NULL;
184
185 error = smb_sm_lockvclist(LK_EXCLUSIVE, td);
186 if (error)
187 return error;
188 error = smb_sm_lookupint(vcspec, shspec, scred, vcpp);
189 if (error == 0 || (vcspec->flags & SMBV_CREATE) == 0) {
190 smb_sm_unlockvclist(td);
191 return error;
192 }
193 error = smb_sm_lookupint(vcspec, NULL, scred, &vcp);
194 if (error) {
195 error = smb_vc_create(vcspec, scred, &vcp);
196 if (error)
197 goto out;
198 error = smb_vc_connect(vcp, scred);
199 if (error)
200 goto out;
201 }
202 if (shspec == NULL)
203 goto out;
204 error = smb_share_create(vcp, shspec, scred, &ssp);
205 if (error)
206 goto out;
207 error = smb_smb_treeconnect(ssp, scred);
208 if (error == 0)
209 vcspec->ssp = ssp;
210 else
211 smb_share_put(ssp, scred);
212out:
213 smb_sm_unlockvclist(td);
214 if (error == 0)
215 *vcpp = vcp;
216 else if (vcp)
217 smb_vc_put(vcp, scred);
218 return error;
219}
220
221/*
222 * Common code for connection object
223 */
224static void
225smb_co_init(struct smb_connobj *cp, int level, char *objname, struct thread *td)
226{
227 SLIST_INIT(&cp->co_children);
228 smb_sl_init(&cp->co_interlock, objname);
229 lockinit(&cp->co_lock, PZERO, objname, 0, 0);
230 cp->co_level = level;
231 cp->co_usecount = 1;
232 KASSERT(smb_co_lock(cp, LK_EXCLUSIVE, td) == 0, ("smb_co_init: lock failed"));
233}
234
235static void
236smb_co_done(struct smb_connobj *cp)
237{
238 smb_sl_destroy(&cp->co_interlock);
239 lockdestroy(&cp->co_lock);
240}
241
242static void
243smb_co_gone(struct smb_connobj *cp, struct smb_cred *scred)
244{
245 struct smb_connobj *parent;
246
247 if (cp->co_gone)
248 cp->co_gone(cp, scred);
249 parent = cp->co_parent;
250 if (parent) {
251 smb_co_lock(parent, LK_EXCLUSIVE, scred->scr_td);
252 SLIST_REMOVE(&parent->co_children, cp, smb_connobj, co_next);
253 smb_co_put(parent, scred);
254 }
255 if (cp->co_free)
256 cp->co_free(cp);
257}
258
259void
260smb_co_ref(struct smb_connobj *cp)
261{
262
263 SMB_CO_LOCK(cp);
264 cp->co_usecount++;
265 SMB_CO_UNLOCK(cp);
266}
267
268void
269smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred)
270{
271 struct thread *td = scred->scr_td;
272
273 SMB_CO_LOCK(cp);
274 if (cp->co_usecount > 1) {
275 cp->co_usecount--;
276 SMB_CO_UNLOCK(cp);
277 return;
278 }
279 if (cp->co_usecount == 0) {
280 SMBERROR("negative use_count for object %d", cp->co_level);
281 SMB_CO_UNLOCK(cp);
282 return;
283 }
284 cp->co_usecount--;
285 cp->co_flags |= SMBO_GONE;
286
287 lockmgr(&cp->co_lock, LK_DRAIN | LK_INTERLOCK, &cp->co_interlock, td);
288 smb_co_gone(cp, scred);
289}
290
291int
292smb_co_get(struct smb_connobj *cp, int flags, struct smb_cred *scred)
293{
294 int error;
295
296 if ((flags & LK_INTERLOCK) == 0)
297 SMB_CO_LOCK(cp);
298 cp->co_usecount++;
299 error = smb_co_lock(cp, flags | LK_INTERLOCK, scred->scr_td);
300 if (error) {
301 SMB_CO_LOCK(cp);
302 cp->co_usecount--;
303 SMB_CO_UNLOCK(cp);
304 return error;
305 }
306 return 0;
307}
308
309void
310smb_co_put(struct smb_connobj *cp, struct smb_cred *scred)
311{
312 struct thread *td = scred->scr_td;
313 int flags;
314
315 flags = LK_RELEASE;
316 SMB_CO_LOCK(cp);
317 if (cp->co_usecount > 1) {
318 cp->co_usecount--;
319 } else if (cp->co_usecount == 1) {
320 cp->co_usecount--;
321 cp->co_flags |= SMBO_GONE;
322 flags = LK_DRAIN;
323 } else {
324 SMBERROR("negative usecount");
325 }
326 lockmgr(&cp->co_lock, LK_RELEASE | LK_INTERLOCK, &cp->co_interlock, td);
327 if ((cp->co_flags & SMBO_GONE) == 0)
328 return;
329 lockmgr(&cp->co_lock, LK_DRAIN, NULL, td);
330 smb_co_gone(cp, scred);
331}
332
333int
334smb_co_lockstatus(struct smb_connobj *cp, struct thread *td)
335{
336 return lockstatus(&cp->co_lock, td);
337}
338
339int
340smb_co_lock(struct smb_connobj *cp, int flags, struct thread *td)
341{
342
343 if (cp->co_flags & SMBO_GONE)
344 return EINVAL;
345 if ((flags & LK_TYPE_MASK) == 0)
346 flags |= LK_EXCLUSIVE;
347 if (smb_co_lockstatus(cp, td) == LK_EXCLUSIVE &&
348 (flags & LK_CANRECURSE) == 0) {
349 SMBERROR("recursive lock for object %d\n", cp->co_level);
350 return 0;
351 }
352 return lockmgr(&cp->co_lock, flags, &cp->co_interlock, td);
353}
354
355void
356smb_co_unlock(struct smb_connobj *cp, int flags, struct thread *td)
357{
358 (void)lockmgr(&cp->co_lock, flags | LK_RELEASE, &cp->co_interlock, td);
359}
360
361static void
362smb_co_addchild(struct smb_connobj *parent, struct smb_connobj *child)
363{
364 KASSERT(smb_co_lockstatus(parent, curthread) == LK_EXCLUSIVE, ("smb_co_addchild: parent not locked"));
365 KASSERT(smb_co_lockstatus(child, curthread) == LK_EXCLUSIVE, ("smb_co_addchild: child not locked"));
366
367 smb_co_ref(parent);
368 SLIST_INSERT_HEAD(&parent->co_children, child, co_next);
369 child->co_parent = parent;
370}
371
372/*
373 * Session implementation
374 */
375
376int
377smb_vc_create(struct smb_vcspec *vcspec,
378 struct smb_cred *scred, struct smb_vc **vcpp)
379{
380 struct smb_vc *vcp;
381 struct thread *td = scred->scr_td;
382 struct ucred *cred = scred->scr_cred;
383 uid_t uid = vcspec->owner;
384 gid_t gid = vcspec->group;
385 uid_t realuid = cred->cr_uid;
386 char *domain = vcspec->domain;
387 int error, isroot;
388
389 isroot = smb_suser(cred) == 0;
390 /*
391 * Only superuser can create VCs with different uid and gid
392 */
393 if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot)
394 return EPERM;
395 if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot)
396 return EPERM;
397
398 vcp = smb_zmalloc(sizeof(*vcp), M_SMBCONN, M_WAITOK);
399 smb_co_init(VCTOCP(vcp), SMBL_VC, "smb_vc", td);
400 vcp->obj.co_free = smb_vc_free;
401 vcp->obj.co_gone = smb_vc_gone;
402 vcp->vc_number = smb_vcnext++;
403 vcp->vc_timo = SMB_DEFRQTIMO;
404 vcp->vc_smbuid = SMB_UID_UNKNOWN;
405 vcp->vc_mode = vcspec->rights & SMBM_MASK;
406 vcp->obj.co_flags = vcspec->flags & (SMBV_PRIVATE | SMBV_SINGLESHARE);
407 vcp->vc_tdesc = &smb_tran_nbtcp_desc;
408
409 if (uid == SMBM_ANY_OWNER)
410 uid = realuid;
411 if (gid == SMBM_ANY_GROUP)
412 gid = cred->cr_groups[0];
413 vcp->vc_uid = uid;
414 vcp->vc_grp = gid;
415
416 smb_sl_init(&vcp->vc_stlock, "vcstlock");
416 error = 0;
417 itry {
418 vcp->vc_paddr = dup_sockaddr(vcspec->sap, 1);
419 ierror(vcp->vc_paddr == NULL, ENOMEM);
417 error = ENOMEM;
420
418
421 vcp->vc_laddr = dup_sockaddr(vcspec->lap, 1);
422 ierror(vcp->vc_laddr == NULL, ENOMEM);
419 vcp->vc_paddr = dup_sockaddr(vcspec->sap, 1);
420 if (vcp->vc_paddr == NULL)
421 goto fail;
422 vcp->vc_laddr = dup_sockaddr(vcspec->lap, 1);
423 if (vcp->vc_laddr == NULL)
424 goto fail;
425 vcp->vc_pass = smb_strdup(vcspec->pass);
426 if (vcp->vc_pass == NULL)
427 goto fail;
428 vcp->vc_domain = smb_strdup((domain && domain[0]) ? domain :
429 "NODOMAIN");
430 if (vcp->vc_domain == NULL)
431 goto fail;
432 vcp->vc_srvname = smb_strdup(vcspec->srvname);
433 if (vcp->vc_srvname == NULL)
434 goto fail;
435 vcp->vc_username = smb_strdup(vcspec->username);
436 if (vcp->vc_username == NULL)
437 goto fail;
438 error = (int)iconv_open("tolower", vcspec->localcs, &vcp->vc_tolower);
439 if (error)
440 goto fail;
441 error = (int)iconv_open("toupper", vcspec->localcs, &vcp->vc_toupper);
442 if (error)
443 goto fail;
444 if (vcspec->servercs[0]) {
445 error = (int)iconv_open(vcspec->servercs, vcspec->localcs,
446 &vcp->vc_toserver);
447 if (error)
448 goto fail;
449 error = (int)iconv_open(vcspec->localcs, vcspec->servercs,
450 &vcp->vc_tolocal);
451 if (error)
452 goto fail;
453 }
454 error = (int)smb_iod_create(vcp);
455 if (error)
456 goto fail;
457 *vcpp = vcp;
458 smb_co_addchild(&smb_vclist, VCTOCP(vcp));
459 return (0);
423
460
424 ierror((vcp->vc_pass = smb_strdup(vcspec->pass)) == NULL, ENOMEM);
425
426 vcp->vc_domain = smb_strdup((domain && domain[0]) ? domain : "NODOMAIN");
427 ierror(vcp->vc_domain == NULL, ENOMEM);
428
429 ierror((vcp->vc_srvname = smb_strdup(vcspec->srvname)) == NULL, ENOMEM);
430 ierror((vcp->vc_username = smb_strdup(vcspec->username)) == NULL, ENOMEM);
431
432 ithrow(iconv_open("tolower", vcspec->localcs, &vcp->vc_tolower));
433 ithrow(iconv_open("toupper", vcspec->localcs, &vcp->vc_toupper));
434 if (vcspec->servercs[0]) {
435 ithrow(iconv_open(vcspec->servercs, vcspec->localcs,
436 &vcp->vc_toserver));
437 ithrow(iconv_open(vcspec->localcs, vcspec->servercs,
438 &vcp->vc_tolocal));
439 }
440
441 ithrow(smb_iod_create(vcp));
442 *vcpp = vcp;
443 smb_co_addchild(&smb_vclist, VCTOCP(vcp));
444 } icatch(error) {
445 smb_vc_put(vcp, scred);
446 } ifinally {
447 } iendtry;
448 return error;
461 fail:
462 smb_vc_put(vcp, scred);
463 return (error);
449}
450
451static void
452smb_vc_free(struct smb_connobj *cp)
453{
454 struct smb_vc *vcp = CPTOVC(cp);
455
456 if (vcp->vc_iod)
457 smb_iod_destroy(vcp->vc_iod);
458 SMB_STRFREE(vcp->vc_username);
459 SMB_STRFREE(vcp->vc_srvname);
460 SMB_STRFREE(vcp->vc_pass);
461 SMB_STRFREE(vcp->vc_domain);
462 if (vcp->vc_paddr)
463 free(vcp->vc_paddr, M_SONAME);
464 if (vcp->vc_laddr)
465 free(vcp->vc_laddr, M_SONAME);
466 if (vcp->vc_tolower)
467 iconv_close(vcp->vc_tolower);
468 if (vcp->vc_toupper)
469 iconv_close(vcp->vc_toupper);
470 if (vcp->vc_tolocal)
471 iconv_close(vcp->vc_tolocal);
472 if (vcp->vc_toserver)
473 iconv_close(vcp->vc_toserver);
474 smb_co_done(VCTOCP(vcp));
475 smb_sl_destroy(&vcp->vc_stlock);
476 free(vcp, M_SMBCONN);
477}
478
479/*
480 * Called when use count of VC dropped to zero.
481 * VC should be locked on enter with LK_DRAIN.
482 */
483static void
484smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred)
485{
486 struct smb_vc *vcp = CPTOVC(cp);
487
488 smb_vc_disconnect(vcp);
489}
490
491void
492smb_vc_ref(struct smb_vc *vcp)
493{
494 smb_co_ref(VCTOCP(vcp));
495}
496
497void
498smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred)
499{
500 smb_co_rele(VCTOCP(vcp), scred);
501}
502
503int
504smb_vc_get(struct smb_vc *vcp, int flags, struct smb_cred *scred)
505{
506 return smb_co_get(VCTOCP(vcp), flags, scred);
507}
508
509void
510smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred)
511{
512 smb_co_put(VCTOCP(vcp), scred);
513}
514
515int
516smb_vc_lock(struct smb_vc *vcp, int flags, struct thread *td)
517{
518 return smb_co_lock(VCTOCP(vcp), flags, td);
519}
520
521void
522smb_vc_unlock(struct smb_vc *vcp, int flags, struct thread *td)
523{
524 smb_co_unlock(VCTOCP(vcp), flags, td);
525}
526
527int
528smb_vc_access(struct smb_vc *vcp, struct smb_cred *scred, mode_t mode)
529{
530 struct ucred *cred = scred->scr_cred;
531
532 if (smb_suser(cred) == 0 || cred->cr_uid == vcp->vc_uid)
533 return 0;
534 mode >>= 3;
535 if (!groupmember(vcp->vc_grp, cred))
536 mode >>= 3;
537 return (vcp->vc_mode & mode) == mode ? 0 : EACCES;
538}
539
540static int
541smb_vc_cmpshare(struct smb_share *ssp, struct smb_sharespec *dp)
542{
543 int exact = 1;
544
545 if (strcmp(ssp->ss_name, dp->name) != 0)
546 return 1;
547 if (dp->owner != SMBM_ANY_OWNER) {
548 if (ssp->ss_uid != dp->owner)
549 return 1;
550 } else
551 exact = 0;
552 if (dp->group != SMBM_ANY_GROUP) {
553 if (ssp->ss_grp != dp->group)
554 return 1;
555 } else
556 exact = 0;
557
558 if (dp->mode & SMBM_EXACT) {
559 if (!exact)
560 return 1;
561 return (dp->mode & SMBM_MASK) == ssp->ss_mode ? 0 : 1;
562 }
563 if (smb_share_access(ssp, dp->scred, dp->mode) != 0)
564 return 1;
565 return 0;
566}
567
568/*
569 * Lookup share in the given VC. Share referenced and locked on return.
570 * VC expected to be locked on entry and will be left locked on exit.
571 */
572int
573smb_vc_lookupshare(struct smb_vc *vcp, struct smb_sharespec *dp,
574 struct smb_cred *scred, struct smb_share **sspp)
575{
576 struct thread *td = scred->scr_td;
577 struct smb_share *ssp = NULL;
578 int error;
579
580 *sspp = NULL;
581 dp->scred = scred;
582 SMBCO_FOREACH((struct smb_connobj*)ssp, VCTOCP(vcp)) {
583 error = smb_share_lock(ssp, LK_EXCLUSIVE, td);
584 if (error)
585 continue;
586 if (smb_vc_cmpshare(ssp, dp) == 0)
587 break;
588 smb_share_unlock(ssp, 0, td);
589 }
590 if (ssp) {
591 smb_share_ref(ssp);
592 *sspp = ssp;
593 error = 0;
594 } else
595 error = ENOENT;
596 return error;
597}
598
599int
600smb_vc_connect(struct smb_vc *vcp, struct smb_cred *scred)
601{
602
603 return smb_iod_request(vcp->vc_iod, SMBIOD_EV_CONNECT | SMBIOD_EV_SYNC, NULL);
604}
605
606/*
607 * Destroy VC to server, invalidate shares linked with it.
608 * Transport should be locked on entry.
609 */
610int
611smb_vc_disconnect(struct smb_vc *vcp)
612{
613
614 smb_iod_request(vcp->vc_iod, SMBIOD_EV_DISCONNECT | SMBIOD_EV_SYNC, NULL);
615 return 0;
616}
617
618static char smb_emptypass[] = "";
619
620const char *
621smb_vc_getpass(struct smb_vc *vcp)
622{
623 if (vcp->vc_pass)
624 return vcp->vc_pass;
625 return smb_emptypass;
626}
627
628static int
629smb_vc_getinfo(struct smb_vc *vcp, struct smb_vc_info *vip)
630{
631 bzero(vip, sizeof(struct smb_vc_info));
632 vip->itype = SMB_INFO_VC;
633 vip->usecount = vcp->obj.co_usecount;
634 vip->uid = vcp->vc_uid;
635 vip->gid = vcp->vc_grp;
636 vip->mode = vcp->vc_mode;
637 vip->flags = vcp->obj.co_flags;
638 vip->sopt = vcp->vc_sopt;
639 vip->iodstate = vcp->vc_iod->iod_state;
640 bzero(&vip->sopt.sv_skey, sizeof(vip->sopt.sv_skey));
641 snprintf(vip->srvname, sizeof(vip->srvname), "%s", vcp->vc_srvname);
642 snprintf(vip->vcname, sizeof(vip->vcname), "%s", vcp->vc_username);
643 return 0;
644}
645
646u_short
647smb_vc_nextmid(struct smb_vc *vcp)
648{
649 u_short r;
650
651 SMB_CO_LOCK(&vcp->obj);
652 r = vcp->vc_mid++;
653 SMB_CO_UNLOCK(&vcp->obj);
654 return r;
655}
656
657/*
658 * Share implementation
659 */
660/*
661 * Allocate share structure and attach it to the given VC
662 * Connection expected to be locked on entry. Share will be returned
663 * in locked state.
664 */
665int
666smb_share_create(struct smb_vc *vcp, struct smb_sharespec *shspec,
667 struct smb_cred *scred, struct smb_share **sspp)
668{
669 struct smb_share *ssp;
670 struct thread *td = scred->scr_td;
671 struct ucred *cred = scred->scr_cred;
672 uid_t realuid = cred->cr_uid;
673 uid_t uid = shspec->owner;
674 gid_t gid = shspec->group;
675 int error, isroot;
676
677 isroot = smb_suser(cred) == 0;
678 /*
679 * Only superuser can create shares with different uid and gid
680 */
681 if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot)
682 return EPERM;
683 if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot)
684 return EPERM;
685 error = smb_vc_lookupshare(vcp, shspec, scred, &ssp);
686 if (!error) {
687 smb_share_put(ssp, scred);
688 return EEXIST;
689 }
690 if (uid == SMBM_ANY_OWNER)
691 uid = realuid;
692 if (gid == SMBM_ANY_GROUP)
693 gid = cred->cr_groups[0];
694 ssp = smb_zmalloc(sizeof(*ssp), M_SMBCONN, M_WAITOK);
695 smb_co_init(SSTOCP(ssp), SMBL_SHARE, "smbss", td);
696 ssp->obj.co_free = smb_share_free;
697 ssp->obj.co_gone = smb_share_gone;
698 smb_sl_init(&ssp->ss_stlock, "ssstlock");
699 ssp->ss_name = smb_strdup(shspec->name);
700 if (shspec->pass && shspec->pass[0])
701 ssp->ss_pass = smb_strdup(shspec->pass);
702 ssp->ss_type = shspec->stype;
703 ssp->ss_tid = SMB_TID_UNKNOWN;
704 ssp->ss_uid = uid;
705 ssp->ss_grp = gid;
706 ssp->ss_mode = shspec->rights & SMBM_MASK;
707 smb_co_addchild(VCTOCP(vcp), SSTOCP(ssp));
708 *sspp = ssp;
709 return 0;
710}
711
712static void
713smb_share_free(struct smb_connobj *cp)
714{
715 struct smb_share *ssp = CPTOSS(cp);
716
717 SMB_STRFREE(ssp->ss_name);
718 SMB_STRFREE(ssp->ss_pass);
719 smb_sl_destroy(&ssp->ss_stlock);
720 smb_co_done(SSTOCP(ssp));
721 free(ssp, M_SMBCONN);
722}
723
724static void
725smb_share_gone(struct smb_connobj *cp, struct smb_cred *scred)
726{
727 struct smb_share *ssp = CPTOSS(cp);
728
729 smb_smb_treedisconnect(ssp, scred);
730}
731
732void
733smb_share_ref(struct smb_share *ssp)
734{
735 smb_co_ref(SSTOCP(ssp));
736}
737
738void
739smb_share_rele(struct smb_share *ssp, struct smb_cred *scred)
740{
741 smb_co_rele(SSTOCP(ssp), scred);
742}
743
744int
745smb_share_get(struct smb_share *ssp, int flags, struct smb_cred *scred)
746{
747 return smb_co_get(SSTOCP(ssp), flags, scred);
748}
749
750void
751smb_share_put(struct smb_share *ssp, struct smb_cred *scred)
752{
753 smb_co_put(SSTOCP(ssp), scred);
754}
755
756int
757smb_share_lock(struct smb_share *ssp, int flags, struct thread *td)
758{
759 return smb_co_lock(SSTOCP(ssp), flags, td);
760}
761
762void
763smb_share_unlock(struct smb_share *ssp, int flags, struct thread *td)
764{
765 smb_co_unlock(SSTOCP(ssp), flags, td);
766}
767
768int
769smb_share_access(struct smb_share *ssp, struct smb_cred *scred, mode_t mode)
770{
771 struct ucred *cred = scred->scr_cred;
772
773 if (smb_suser(cred) == 0 || cred->cr_uid == ssp->ss_uid)
774 return 0;
775 mode >>= 3;
776 if (!groupmember(ssp->ss_grp, cred))
777 mode >>= 3;
778 return (ssp->ss_mode & mode) == mode ? 0 : EACCES;
779}
780
781void
782smb_share_invalidate(struct smb_share *ssp)
783{
784 ssp->ss_tid = SMB_TID_UNKNOWN;
785}
786
787int
788smb_share_valid(struct smb_share *ssp)
789{
790 return ssp->ss_tid != SMB_TID_UNKNOWN &&
791 ssp->ss_vcgenid == SSTOVC(ssp)->vc_genid;
792}
793
794const char*
795smb_share_getpass(struct smb_share *ssp)
796{
797 struct smb_vc *vcp;
798
799 if (ssp->ss_pass)
800 return ssp->ss_pass;
801 vcp = SSTOVC(ssp);
802 if (vcp->vc_pass)
803 return vcp->vc_pass;
804 return smb_emptypass;
805}
806
807static int
808smb_share_getinfo(struct smb_share *ssp, struct smb_share_info *sip)
809{
810 bzero(sip, sizeof(struct smb_share_info));
811 sip->itype = SMB_INFO_SHARE;
812 sip->usecount = ssp->obj.co_usecount;
813 sip->tid = ssp->ss_tid;
814 sip->type= ssp->ss_type;
815 sip->uid = ssp->ss_uid;
816 sip->gid = ssp->ss_grp;
817 sip->mode= ssp->ss_mode;
818 sip->flags = ssp->obj.co_flags;
819 snprintf(sip->sname, sizeof(sip->sname), "%s", ssp->ss_name);
820 return 0;
821}
822
823/*
824 * Dump an entire tree into sysctl call
825 */
826static int
827smb_sysctl_treedump(SYSCTL_HANDLER_ARGS)
828{
829 struct thread *td = req->td;
830 struct smb_cred scred;
831 struct smb_vc *vcp;
832 struct smb_share *ssp;
833 struct smb_vc_info vci;
834 struct smb_share_info ssi;
835 int error, itype;
836
837 smb_makescred(&scred, td, td->td_ucred);
838 sysctl_wire_old_buffer(req, 0);
839 error = smb_sm_lockvclist(LK_SHARED, td);
840 if (error)
841 return error;
842 SMBCO_FOREACH((struct smb_connobj*)vcp, &smb_vclist) {
843 error = smb_vc_lock(vcp, LK_SHARED, td);
844 if (error)
845 continue;
846 smb_vc_getinfo(vcp, &vci);
847 error = SYSCTL_OUT(req, &vci, sizeof(struct smb_vc_info));
848 if (error) {
849 smb_vc_unlock(vcp, 0, td);
850 break;
851 }
852 SMBCO_FOREACH((struct smb_connobj*)ssp, VCTOCP(vcp)) {
853 error = smb_share_lock(ssp, LK_SHARED, td);
854 if (error) {
855 error = 0;
856 continue;
857 }
858 smb_share_getinfo(ssp, &ssi);
859 smb_share_unlock(ssp, 0, td);
860 error = SYSCTL_OUT(req, &ssi, sizeof(struct smb_share_info));
861 if (error)
862 break;
863 }
864 smb_vc_unlock(vcp, 0, td);
865 if (error)
866 break;
867 }
868 if (!error) {
869 itype = SMB_INFO_NONE;
870 error = SYSCTL_OUT(req, &itype, sizeof(itype));
871 }
872 smb_sm_unlockvclist(td);
873 return error;
874}
464}
465
466static void
467smb_vc_free(struct smb_connobj *cp)
468{
469 struct smb_vc *vcp = CPTOVC(cp);
470
471 if (vcp->vc_iod)
472 smb_iod_destroy(vcp->vc_iod);
473 SMB_STRFREE(vcp->vc_username);
474 SMB_STRFREE(vcp->vc_srvname);
475 SMB_STRFREE(vcp->vc_pass);
476 SMB_STRFREE(vcp->vc_domain);
477 if (vcp->vc_paddr)
478 free(vcp->vc_paddr, M_SONAME);
479 if (vcp->vc_laddr)
480 free(vcp->vc_laddr, M_SONAME);
481 if (vcp->vc_tolower)
482 iconv_close(vcp->vc_tolower);
483 if (vcp->vc_toupper)
484 iconv_close(vcp->vc_toupper);
485 if (vcp->vc_tolocal)
486 iconv_close(vcp->vc_tolocal);
487 if (vcp->vc_toserver)
488 iconv_close(vcp->vc_toserver);
489 smb_co_done(VCTOCP(vcp));
490 smb_sl_destroy(&vcp->vc_stlock);
491 free(vcp, M_SMBCONN);
492}
493
494/*
495 * Called when use count of VC dropped to zero.
496 * VC should be locked on enter with LK_DRAIN.
497 */
498static void
499smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred)
500{
501 struct smb_vc *vcp = CPTOVC(cp);
502
503 smb_vc_disconnect(vcp);
504}
505
506void
507smb_vc_ref(struct smb_vc *vcp)
508{
509 smb_co_ref(VCTOCP(vcp));
510}
511
512void
513smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred)
514{
515 smb_co_rele(VCTOCP(vcp), scred);
516}
517
518int
519smb_vc_get(struct smb_vc *vcp, int flags, struct smb_cred *scred)
520{
521 return smb_co_get(VCTOCP(vcp), flags, scred);
522}
523
524void
525smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred)
526{
527 smb_co_put(VCTOCP(vcp), scred);
528}
529
530int
531smb_vc_lock(struct smb_vc *vcp, int flags, struct thread *td)
532{
533 return smb_co_lock(VCTOCP(vcp), flags, td);
534}
535
536void
537smb_vc_unlock(struct smb_vc *vcp, int flags, struct thread *td)
538{
539 smb_co_unlock(VCTOCP(vcp), flags, td);
540}
541
542int
543smb_vc_access(struct smb_vc *vcp, struct smb_cred *scred, mode_t mode)
544{
545 struct ucred *cred = scred->scr_cred;
546
547 if (smb_suser(cred) == 0 || cred->cr_uid == vcp->vc_uid)
548 return 0;
549 mode >>= 3;
550 if (!groupmember(vcp->vc_grp, cred))
551 mode >>= 3;
552 return (vcp->vc_mode & mode) == mode ? 0 : EACCES;
553}
554
555static int
556smb_vc_cmpshare(struct smb_share *ssp, struct smb_sharespec *dp)
557{
558 int exact = 1;
559
560 if (strcmp(ssp->ss_name, dp->name) != 0)
561 return 1;
562 if (dp->owner != SMBM_ANY_OWNER) {
563 if (ssp->ss_uid != dp->owner)
564 return 1;
565 } else
566 exact = 0;
567 if (dp->group != SMBM_ANY_GROUP) {
568 if (ssp->ss_grp != dp->group)
569 return 1;
570 } else
571 exact = 0;
572
573 if (dp->mode & SMBM_EXACT) {
574 if (!exact)
575 return 1;
576 return (dp->mode & SMBM_MASK) == ssp->ss_mode ? 0 : 1;
577 }
578 if (smb_share_access(ssp, dp->scred, dp->mode) != 0)
579 return 1;
580 return 0;
581}
582
583/*
584 * Lookup share in the given VC. Share referenced and locked on return.
585 * VC expected to be locked on entry and will be left locked on exit.
586 */
587int
588smb_vc_lookupshare(struct smb_vc *vcp, struct smb_sharespec *dp,
589 struct smb_cred *scred, struct smb_share **sspp)
590{
591 struct thread *td = scred->scr_td;
592 struct smb_share *ssp = NULL;
593 int error;
594
595 *sspp = NULL;
596 dp->scred = scred;
597 SMBCO_FOREACH((struct smb_connobj*)ssp, VCTOCP(vcp)) {
598 error = smb_share_lock(ssp, LK_EXCLUSIVE, td);
599 if (error)
600 continue;
601 if (smb_vc_cmpshare(ssp, dp) == 0)
602 break;
603 smb_share_unlock(ssp, 0, td);
604 }
605 if (ssp) {
606 smb_share_ref(ssp);
607 *sspp = ssp;
608 error = 0;
609 } else
610 error = ENOENT;
611 return error;
612}
613
614int
615smb_vc_connect(struct smb_vc *vcp, struct smb_cred *scred)
616{
617
618 return smb_iod_request(vcp->vc_iod, SMBIOD_EV_CONNECT | SMBIOD_EV_SYNC, NULL);
619}
620
621/*
622 * Destroy VC to server, invalidate shares linked with it.
623 * Transport should be locked on entry.
624 */
625int
626smb_vc_disconnect(struct smb_vc *vcp)
627{
628
629 smb_iod_request(vcp->vc_iod, SMBIOD_EV_DISCONNECT | SMBIOD_EV_SYNC, NULL);
630 return 0;
631}
632
633static char smb_emptypass[] = "";
634
635const char *
636smb_vc_getpass(struct smb_vc *vcp)
637{
638 if (vcp->vc_pass)
639 return vcp->vc_pass;
640 return smb_emptypass;
641}
642
643static int
644smb_vc_getinfo(struct smb_vc *vcp, struct smb_vc_info *vip)
645{
646 bzero(vip, sizeof(struct smb_vc_info));
647 vip->itype = SMB_INFO_VC;
648 vip->usecount = vcp->obj.co_usecount;
649 vip->uid = vcp->vc_uid;
650 vip->gid = vcp->vc_grp;
651 vip->mode = vcp->vc_mode;
652 vip->flags = vcp->obj.co_flags;
653 vip->sopt = vcp->vc_sopt;
654 vip->iodstate = vcp->vc_iod->iod_state;
655 bzero(&vip->sopt.sv_skey, sizeof(vip->sopt.sv_skey));
656 snprintf(vip->srvname, sizeof(vip->srvname), "%s", vcp->vc_srvname);
657 snprintf(vip->vcname, sizeof(vip->vcname), "%s", vcp->vc_username);
658 return 0;
659}
660
661u_short
662smb_vc_nextmid(struct smb_vc *vcp)
663{
664 u_short r;
665
666 SMB_CO_LOCK(&vcp->obj);
667 r = vcp->vc_mid++;
668 SMB_CO_UNLOCK(&vcp->obj);
669 return r;
670}
671
672/*
673 * Share implementation
674 */
675/*
676 * Allocate share structure and attach it to the given VC
677 * Connection expected to be locked on entry. Share will be returned
678 * in locked state.
679 */
680int
681smb_share_create(struct smb_vc *vcp, struct smb_sharespec *shspec,
682 struct smb_cred *scred, struct smb_share **sspp)
683{
684 struct smb_share *ssp;
685 struct thread *td = scred->scr_td;
686 struct ucred *cred = scred->scr_cred;
687 uid_t realuid = cred->cr_uid;
688 uid_t uid = shspec->owner;
689 gid_t gid = shspec->group;
690 int error, isroot;
691
692 isroot = smb_suser(cred) == 0;
693 /*
694 * Only superuser can create shares with different uid and gid
695 */
696 if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot)
697 return EPERM;
698 if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot)
699 return EPERM;
700 error = smb_vc_lookupshare(vcp, shspec, scred, &ssp);
701 if (!error) {
702 smb_share_put(ssp, scred);
703 return EEXIST;
704 }
705 if (uid == SMBM_ANY_OWNER)
706 uid = realuid;
707 if (gid == SMBM_ANY_GROUP)
708 gid = cred->cr_groups[0];
709 ssp = smb_zmalloc(sizeof(*ssp), M_SMBCONN, M_WAITOK);
710 smb_co_init(SSTOCP(ssp), SMBL_SHARE, "smbss", td);
711 ssp->obj.co_free = smb_share_free;
712 ssp->obj.co_gone = smb_share_gone;
713 smb_sl_init(&ssp->ss_stlock, "ssstlock");
714 ssp->ss_name = smb_strdup(shspec->name);
715 if (shspec->pass && shspec->pass[0])
716 ssp->ss_pass = smb_strdup(shspec->pass);
717 ssp->ss_type = shspec->stype;
718 ssp->ss_tid = SMB_TID_UNKNOWN;
719 ssp->ss_uid = uid;
720 ssp->ss_grp = gid;
721 ssp->ss_mode = shspec->rights & SMBM_MASK;
722 smb_co_addchild(VCTOCP(vcp), SSTOCP(ssp));
723 *sspp = ssp;
724 return 0;
725}
726
727static void
728smb_share_free(struct smb_connobj *cp)
729{
730 struct smb_share *ssp = CPTOSS(cp);
731
732 SMB_STRFREE(ssp->ss_name);
733 SMB_STRFREE(ssp->ss_pass);
734 smb_sl_destroy(&ssp->ss_stlock);
735 smb_co_done(SSTOCP(ssp));
736 free(ssp, M_SMBCONN);
737}
738
739static void
740smb_share_gone(struct smb_connobj *cp, struct smb_cred *scred)
741{
742 struct smb_share *ssp = CPTOSS(cp);
743
744 smb_smb_treedisconnect(ssp, scred);
745}
746
747void
748smb_share_ref(struct smb_share *ssp)
749{
750 smb_co_ref(SSTOCP(ssp));
751}
752
753void
754smb_share_rele(struct smb_share *ssp, struct smb_cred *scred)
755{
756 smb_co_rele(SSTOCP(ssp), scred);
757}
758
759int
760smb_share_get(struct smb_share *ssp, int flags, struct smb_cred *scred)
761{
762 return smb_co_get(SSTOCP(ssp), flags, scred);
763}
764
765void
766smb_share_put(struct smb_share *ssp, struct smb_cred *scred)
767{
768 smb_co_put(SSTOCP(ssp), scred);
769}
770
771int
772smb_share_lock(struct smb_share *ssp, int flags, struct thread *td)
773{
774 return smb_co_lock(SSTOCP(ssp), flags, td);
775}
776
777void
778smb_share_unlock(struct smb_share *ssp, int flags, struct thread *td)
779{
780 smb_co_unlock(SSTOCP(ssp), flags, td);
781}
782
783int
784smb_share_access(struct smb_share *ssp, struct smb_cred *scred, mode_t mode)
785{
786 struct ucred *cred = scred->scr_cred;
787
788 if (smb_suser(cred) == 0 || cred->cr_uid == ssp->ss_uid)
789 return 0;
790 mode >>= 3;
791 if (!groupmember(ssp->ss_grp, cred))
792 mode >>= 3;
793 return (ssp->ss_mode & mode) == mode ? 0 : EACCES;
794}
795
796void
797smb_share_invalidate(struct smb_share *ssp)
798{
799 ssp->ss_tid = SMB_TID_UNKNOWN;
800}
801
802int
803smb_share_valid(struct smb_share *ssp)
804{
805 return ssp->ss_tid != SMB_TID_UNKNOWN &&
806 ssp->ss_vcgenid == SSTOVC(ssp)->vc_genid;
807}
808
809const char*
810smb_share_getpass(struct smb_share *ssp)
811{
812 struct smb_vc *vcp;
813
814 if (ssp->ss_pass)
815 return ssp->ss_pass;
816 vcp = SSTOVC(ssp);
817 if (vcp->vc_pass)
818 return vcp->vc_pass;
819 return smb_emptypass;
820}
821
822static int
823smb_share_getinfo(struct smb_share *ssp, struct smb_share_info *sip)
824{
825 bzero(sip, sizeof(struct smb_share_info));
826 sip->itype = SMB_INFO_SHARE;
827 sip->usecount = ssp->obj.co_usecount;
828 sip->tid = ssp->ss_tid;
829 sip->type= ssp->ss_type;
830 sip->uid = ssp->ss_uid;
831 sip->gid = ssp->ss_grp;
832 sip->mode= ssp->ss_mode;
833 sip->flags = ssp->obj.co_flags;
834 snprintf(sip->sname, sizeof(sip->sname), "%s", ssp->ss_name);
835 return 0;
836}
837
838/*
839 * Dump an entire tree into sysctl call
840 */
841static int
842smb_sysctl_treedump(SYSCTL_HANDLER_ARGS)
843{
844 struct thread *td = req->td;
845 struct smb_cred scred;
846 struct smb_vc *vcp;
847 struct smb_share *ssp;
848 struct smb_vc_info vci;
849 struct smb_share_info ssi;
850 int error, itype;
851
852 smb_makescred(&scred, td, td->td_ucred);
853 sysctl_wire_old_buffer(req, 0);
854 error = smb_sm_lockvclist(LK_SHARED, td);
855 if (error)
856 return error;
857 SMBCO_FOREACH((struct smb_connobj*)vcp, &smb_vclist) {
858 error = smb_vc_lock(vcp, LK_SHARED, td);
859 if (error)
860 continue;
861 smb_vc_getinfo(vcp, &vci);
862 error = SYSCTL_OUT(req, &vci, sizeof(struct smb_vc_info));
863 if (error) {
864 smb_vc_unlock(vcp, 0, td);
865 break;
866 }
867 SMBCO_FOREACH((struct smb_connobj*)ssp, VCTOCP(vcp)) {
868 error = smb_share_lock(ssp, LK_SHARED, td);
869 if (error) {
870 error = 0;
871 continue;
872 }
873 smb_share_getinfo(ssp, &ssi);
874 smb_share_unlock(ssp, 0, td);
875 error = SYSCTL_OUT(req, &ssi, sizeof(struct smb_share_info));
876 if (error)
877 break;
878 }
879 smb_vc_unlock(vcp, 0, td);
880 if (error)
881 break;
882 }
883 if (!error) {
884 itype = SMB_INFO_NONE;
885 error = SYSCTL_OUT(req, &itype, sizeof(itype));
886 }
887 smb_sm_unlockvclist(td);
888 return error;
889}