sbin/hammer2: Fix usage()
[dragonfly.git] / sys / netproto / smb / smb_conn.c
blob3c274d4cb4a0fc54dcb385b2d39239d0b69c34c3
1 /*
2 * Copyright (c) 2000-2001 Boris Popov
3 * All rights reserved.
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.
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.
32 * $FreeBSD: src/sys/netsmb/smb_conn.c,v 1.1.2.1 2001/05/22 08:32:33 bp Exp $
36 * Connection engine.
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/proc.h>
44 #include <sys/caps.h>
45 #include <sys/lock.h>
46 #include <sys/sysctl.h>
47 #include <sys/socketvar.h>
49 #include <sys/iconv.h>
51 #include "smb.h"
52 #include "smb_subr.h"
53 #include "smb_conn.h"
54 #include "smb_tran.h"
55 #include "smb_trantcp.h"
57 static struct smb_connobj smb_vclist;
58 static int smb_vcnext = 1; /* next unique id for VC */
60 SYSCTL_NODE(_net, OID_AUTO, smb, CTLFLAG_RW, NULL, "SMB protocol");
62 MALLOC_DEFINE(M_SMBCONN, "SMB conn", "SMB connection");
64 static void smb_co_init(struct smb_connobj *cp, int level, char *objname);
65 static void smb_co_done(struct smb_connobj *cp);
66 static int smb_co_lockstatus(struct smb_connobj *cp, struct thread *td);
68 static int smb_vc_disconnect(struct smb_vc *vcp);
69 static void smb_vc_free(struct smb_connobj *cp);
70 static void smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred);
73 * Connection object
75 static void smb_co_ref(struct smb_connobj *cp);
76 static void smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred);
77 static int smb_co_get(struct smb_connobj *cp, int flags, struct smb_cred *scred);
78 static void smb_co_put(struct smb_connobj *cp, struct smb_cred *scred);
79 static int smb_co_lock(struct smb_connobj *cp, int flags);
80 static void smb_co_unlock(struct smb_connobj *cp, int flags);
82 static smb_co_free_t smb_share_free;
83 static smb_co_gone_t smb_share_gone;
85 static int smb_sysctl_treedump(SYSCTL_HANDLER_ARGS);
87 SYSCTL_PROC(_net_smb, OID_AUTO, treedump, CTLFLAG_RD | CTLTYPE_OPAQUE,
88 NULL, 0, smb_sysctl_treedump, "S,treedump", "Requester tree");
90 int
91 smb_sm_init(void)
93 smb_co_init(&smb_vclist, SMBL_SM, "smbsm");
94 smb_co_unlock(&smb_vclist, 0);
95 return 0;
98 int
99 smb_sm_done(void)
102 /* XXX: hold the mutex */
103 if (smb_vclist.co_usecount > 1) {
104 SMBERROR("%d connections still active\n", smb_vclist.co_usecount - 1);
105 return EBUSY;
107 smb_co_done(&smb_vclist);
108 return 0;
111 static int
112 smb_sm_lockvclist(int flags)
114 return smb_co_lock(&smb_vclist, flags | LK_CANRECURSE);
117 static void
118 smb_sm_unlockvclist(void)
120 smb_co_unlock(&smb_vclist, LK_RELEASE);
123 static int
124 smb_sm_lookupint(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
125 struct smb_cred *scred, struct smb_vc **vcpp)
127 struct smb_connobj *scp;
128 struct smb_vc *vcp;
129 int exact = 1;
130 int error;
132 vcspec->shspec = shspec;
133 error = ENOENT;
134 vcp = NULL;
135 SMBCO_FOREACH(scp, &smb_vclist) {
136 vcp = (struct smb_vc *)scp;
137 error = smb_vc_lock(vcp, LK_EXCLUSIVE);
138 if (error)
139 continue;
141 error = 1;
142 if ((vcp->obj.co_flags & SMBV_PRIVATE) ||
143 !CONNADDREQ(vcp->vc_paddr, vcspec->sap) ||
144 strcmp(vcp->vc_username, vcspec->username) != 0)
145 goto unlock;
146 if (vcspec->owner != SMBM_ANY_OWNER) {
147 if (vcp->vc_uid != vcspec->owner)
148 goto unlock;
149 } else
150 exact = 0;
151 if (vcspec->group != SMBM_ANY_GROUP) {
152 if (vcp->vc_grp != vcspec->group)
153 goto unlock;
154 } else
155 exact = 0;
157 if (vcspec->mode & SMBM_EXACT) {
158 if (!exact ||
159 (vcspec->mode & SMBM_MASK) != vcp->vc_mode)
160 goto unlock;
162 if (smb_vc_access(vcp, scred, vcspec->mode) != 0)
163 goto unlock;
164 vcspec->ssp = NULL;
165 if (shspec) {
166 error = smb_vc_lookupshare(vcp, shspec, scred, &vcspec->ssp);
167 if (error != 0)
168 goto unlock;
170 error = 0;
171 break;
172 unlock:
173 smb_vc_unlock(vcp, 0);
175 if (vcp) {
176 smb_vc_ref(vcp);
177 *vcpp = vcp;
179 return error;
183 smb_sm_lookup(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
184 struct smb_cred *scred, struct smb_vc **vcpp)
186 struct smb_vc *vcp;
187 struct smb_share *ssp = NULL;
188 int error;
190 *vcpp = vcp = NULL;
192 error = smb_sm_lockvclist(LK_EXCLUSIVE);
193 if (error)
194 return error;
195 error = smb_sm_lookupint(vcspec, shspec, scred, vcpp);
196 if (error == 0 || (vcspec->flags & SMBV_CREATE) == 0) {
197 smb_sm_unlockvclist();
198 return error;
200 error = smb_sm_lookupint(vcspec, NULL, scred, &vcp);
201 if (error) {
202 error = smb_vc_create(vcspec, scred, &vcp);
203 if (error)
204 goto out;
205 error = smb_vc_connect(vcp, scred);
206 if (error)
207 goto out;
209 if (shspec == NULL)
210 goto out;
211 error = smb_share_create(vcp, shspec, scred, &ssp);
212 if (error)
213 goto out;
214 error = smb_smb_treeconnect(ssp, scred);
215 if (error == 0)
216 vcspec->ssp = ssp;
217 else
218 smb_share_put(ssp, scred);
219 out:
220 smb_sm_unlockvclist();
221 if (error == 0)
222 *vcpp = vcp;
223 else if (vcp)
224 smb_vc_put(vcp, scred);
225 return error;
229 * Common code for connection object
231 static void
232 smb_co_init(struct smb_connobj *cp, int level, char *objname)
234 SLIST_INIT(&cp->co_children);
235 smb_sl_init(&cp->co_interlock, objname);
236 lockinit(&cp->co_lock, objname, 0, 0);
237 cp->co_level = level;
238 cp->co_usecount = 1;
239 smb_co_lock(cp, LK_EXCLUSIVE);
242 static void
243 smb_co_done(struct smb_connobj *cp)
245 smb_sl_destroy(&cp->co_interlock);
246 lockdestroy(&cp->co_lock);
249 static void
250 smb_co_gone(struct smb_connobj *cp, struct smb_cred *scred)
252 struct smb_connobj *parent;
254 if (cp->co_gone)
255 cp->co_gone(cp, scred);
256 parent = cp->co_parent;
257 if (parent) {
258 smb_co_lock(parent, LK_EXCLUSIVE);
259 SLIST_REMOVE(&parent->co_children, cp, smb_connobj, co_next);
260 smb_co_put(parent, scred);
262 lockmgr(&cp->co_lock, LK_RELEASE);
263 while (cp->co_usecount > 0) {
264 tsleep(&cp->co_lock, 0, "smbgone", hz);
266 if (cp->co_free)
267 cp->co_free(cp);
270 static void
271 smb_co_ref(struct smb_connobj *cp)
273 SMB_CO_LOCK(cp);
274 cp->co_usecount++;
275 SMB_CO_UNLOCK(cp);
278 static void
279 smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred)
281 SMB_CO_LOCK(cp);
282 if (cp->co_usecount > 1) {
283 cp->co_usecount--;
284 SMB_CO_UNLOCK(cp);
285 return;
287 if (cp->co_usecount <= 0) {
288 SMBERROR("negative use_count for object %d", cp->co_level);
289 SMB_CO_UNLOCK(cp);
290 return;
292 cp->co_usecount = 0;
293 if ((cp->co_flags & SMBO_GONE) == 0) {
294 cp->co_flags |= SMBO_GONE;
295 SMB_CO_UNLOCK(cp);
296 lockmgr(&cp->co_lock, LK_EXCLUSIVE);
297 smb_co_gone(cp, scred);
298 } else {
299 SMB_CO_UNLOCK(cp);
300 wakeup(&cp->co_lock);
304 static int
305 smb_co_get(struct smb_connobj *cp, int flags, struct smb_cred *scred)
307 int error;
309 SMB_CO_LOCK(cp);
310 cp->co_usecount++;
311 SMB_CO_UNLOCK(cp);
312 error = smb_co_lock(cp, flags);
313 if (error) {
314 SMB_CO_LOCK(cp);
315 if (cp->co_usecount > 1) {
316 cp->co_usecount--;
317 SMB_CO_UNLOCK(cp);
318 } else if (cp->co_usecount <= 0) {
319 SMBERROR("negative use_count for object %d", cp->co_level);
320 SMB_CO_UNLOCK(cp);
321 } else {
322 cp->co_usecount = 0;
323 if ((cp->co_flags & SMBO_GONE) == 0) {
324 cp->co_flags |= SMBO_GONE;
325 SMB_CO_UNLOCK(cp);
326 lockmgr(&cp->co_lock, LK_EXCLUSIVE);
327 smb_co_gone(cp, scred);
328 } else {
329 SMB_CO_UNLOCK(cp);
332 return error;
334 return 0;
337 static void
338 smb_co_put(struct smb_connobj *cp, struct smb_cred *scred)
340 SMB_CO_LOCK(cp);
341 if (cp->co_usecount > 1) {
342 cp->co_usecount--;
343 SMB_CO_UNLOCK(cp);
344 lockmgr(&cp->co_lock, LK_RELEASE);
345 return;
347 if (cp->co_usecount <= 0) {
348 SMBERROR("negative use_count for object %d", cp->co_level);
349 SMB_CO_UNLOCK(cp);
350 return;
352 cp->co_usecount = 0;
353 if ((cp->co_flags & SMBO_GONE) == 0) {
354 cp->co_flags |= SMBO_GONE;
355 SMB_CO_UNLOCK(cp);
356 lockmgr(&cp->co_lock, LK_RELEASE);
357 lockmgr(&cp->co_lock, LK_EXCLUSIVE);
358 smb_co_gone(cp, scred);
359 } else {
360 SMB_CO_UNLOCK(cp);
361 lockmgr(&cp->co_lock, LK_RELEASE);
362 wakeup(&cp->co_lock);
366 static int
367 smb_co_lockstatus(struct smb_connobj *cp, struct thread *td)
369 return lockstatus(&cp->co_lock, td);
372 static int
373 smb_co_lock(struct smb_connobj *cp, int flags)
375 int error;
377 KKASSERT(cp->co_usecount > 0);
378 if (cp->co_flags & SMBO_GONE)
379 return EINVAL;
380 if ((flags & LK_TYPE_MASK) == 0)
381 flags |= LK_EXCLUSIVE;
382 if (smb_co_lockstatus(cp, curthread) == LK_EXCLUSIVE &&
383 (flags & LK_CANRECURSE) == 0) {
384 SMBERROR("recursive lock for object %d\n", cp->co_level);
385 return 0;
387 error = lockmgr(&cp->co_lock, flags);
388 if (error == 0 && (cp->co_flags & SMBO_GONE)) {
389 lockmgr(&cp->co_lock, LK_RELEASE);
390 error = EINVAL;
392 return (error);
395 static void
396 smb_co_unlock(struct smb_connobj *cp, int flags)
398 lockmgr(&cp->co_lock, flags | LK_RELEASE);
401 static void
402 smb_co_addchild(struct smb_connobj *parent, struct smb_connobj *child)
404 KASSERT(smb_co_lockstatus(parent, curthread) == LK_EXCLUSIVE, ("smb_co_addchild: parent not locked"));
405 KASSERT(smb_co_lockstatus(child, curthread) == LK_EXCLUSIVE, ("smb_co_addchild: child not locked"));
407 smb_co_ref(parent);
408 SLIST_INSERT_HEAD(&parent->co_children, child, co_next);
409 child->co_parent = parent;
413 * Session implementation
417 smb_vc_create(struct smb_vcspec *vcspec,
418 struct smb_cred *scred, struct smb_vc **vcpp)
420 struct smb_vc *vcp;
421 struct ucred *cred = scred->scr_cred;
422 uid_t uid = vcspec->owner;
423 gid_t gid = vcspec->group;
424 uid_t realuid = cred->cr_uid;
425 char *domain = vcspec->domain;
426 int error, isroot;
428 isroot = smb_suser(cred) == 0;
430 * Only superuser can create VCs with different uid and gid
432 if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot)
433 return EPERM;
434 if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot)
435 return EPERM;
437 vcp = smb_zmalloc(sizeof(*vcp), M_SMBCONN, M_WAITOK);
438 smb_co_init(VCTOCP(vcp), SMBL_VC, "smb_vc");
439 vcp->obj.co_free = smb_vc_free;
440 vcp->obj.co_gone = smb_vc_gone;
441 vcp->vc_number = smb_vcnext++;
442 vcp->vc_timo = SMB_DEFRQTIMO;
443 vcp->vc_smbuid = SMB_UID_UNKNOWN;
444 vcp->vc_mode = vcspec->rights & SMBM_MASK;
445 vcp->obj.co_flags = vcspec->flags & (SMBV_PRIVATE | SMBV_SINGLESHARE);
446 vcp->vc_tdesc = &smb_tran_nbtcp_desc;
447 vcp->vc_seqno = 0;
448 vcp->vc_mackey = NULL;
449 vcp->vc_mackeylen = 0;
451 if (uid == SMBM_ANY_OWNER)
452 uid = realuid;
453 if (gid == SMBM_ANY_GROUP)
454 gid = cred->cr_groups[0];
455 vcp->vc_uid = uid;
456 vcp->vc_grp = gid;
458 smb_sl_init(&vcp->vc_stlock, "vcstlock");
460 do {
461 error = ENOMEM;
462 vcp->vc_paddr = dup_sockaddr(vcspec->sap);
463 if (vcp->vc_paddr == NULL)
464 break;
466 vcp->vc_laddr = dup_sockaddr(vcspec->lap);
467 if (vcp->vc_laddr == NULL)
468 break;
470 vcp->vc_pass = smb_strdup(vcspec->pass);
471 if (vcp->vc_pass == NULL)
472 break;
474 vcp->vc_domain = smb_strdup((domain && domain[0]) ? domain
475 : "NODOMAIN");
476 if (vcp->vc_domain == NULL)
477 break;
479 vcp->vc_srvname = smb_strdup(vcspec->srvname);
480 if (vcp->vc_srvname == NULL)
481 break;
482 vcp->vc_username = smb_strdup(vcspec->username);
483 if (vcp->vc_username == NULL)
484 break;
486 error = iconv_open("tolower", vcspec->localcs, &vcp->vc_tolower);
487 if (error != 0)
488 break;
489 error = iconv_open("toupper", vcspec->localcs, &vcp->vc_toupper);
490 if (error != 0)
491 break;
493 if (vcspec->servercs[0]) {
494 error = iconv_open(vcspec->servercs, vcspec->localcs,
495 &vcp->vc_toserver);
496 if (error != 0)
497 break;
498 error = iconv_open(vcspec->localcs, vcspec->servercs,
499 &vcp->vc_tolocal);
500 if (error != 0)
501 break;
504 error = smb_iod_create(vcp);
505 if (error != 0)
506 break;
507 *vcpp = vcp;
508 smb_co_addchild(&smb_vclist, VCTOCP(vcp));
509 error = 0;
510 } while (0);
512 if (error)
513 smb_vc_put(vcp, scred);
514 return error;
517 static void
518 smb_vc_free(struct smb_connobj *cp)
520 struct smb_vc *vcp = CPTOVC(cp);
522 if (vcp->vc_iod)
523 smb_iod_destroy(vcp->vc_iod);
524 SMB_STRFREE(vcp->vc_username);
525 SMB_STRFREE(vcp->vc_srvname);
526 SMB_STRFREE(vcp->vc_pass);
527 SMB_STRFREE(vcp->vc_domain);
528 if (vcp->vc_mackey)
529 kfree(vcp->vc_mackey, M_SMBTEMP);
530 if (vcp->vc_paddr)
531 kfree(vcp->vc_paddr, M_SONAME);
532 if (vcp->vc_laddr)
533 kfree(vcp->vc_laddr, M_SONAME);
534 if (vcp->vc_tolower)
535 iconv_close(vcp->vc_tolower);
536 if (vcp->vc_toupper)
537 iconv_close(vcp->vc_toupper);
538 if (vcp->vc_tolocal)
539 iconv_close(vcp->vc_tolocal);
540 if (vcp->vc_toserver)
541 iconv_close(vcp->vc_toserver);
542 smb_co_done(VCTOCP(vcp));
543 smb_sl_destroy(&vcp->vc_stlock);
544 kfree(vcp, M_SMBCONN);
548 * Called when use count of VC dropped to zero.
550 static void
551 smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred)
553 struct smb_vc *vcp = CPTOVC(cp);
555 smb_vc_disconnect(vcp);
558 void
559 smb_vc_ref(struct smb_vc *vcp)
561 smb_co_ref(VCTOCP(vcp));
564 void
565 smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred)
567 smb_co_rele(VCTOCP(vcp), scred);
571 smb_vc_get(struct smb_vc *vcp, int flags, struct smb_cred *scred)
573 return smb_co_get(VCTOCP(vcp), flags, scred);
576 void
577 smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred)
579 smb_co_put(VCTOCP(vcp), scred);
583 smb_vc_lock(struct smb_vc *vcp, int flags)
585 return smb_co_lock(VCTOCP(vcp), flags);
588 void
589 smb_vc_unlock(struct smb_vc *vcp, int flags)
591 smb_co_unlock(VCTOCP(vcp), flags);
595 smb_vc_access(struct smb_vc *vcp, struct smb_cred *scred, mode_t mode)
597 struct ucred *cred = scred->scr_cred;
599 if (smb_suser(cred) == 0 || cred->cr_uid == vcp->vc_uid)
600 return 0;
601 mode >>= 3;
602 if (!groupmember(vcp->vc_grp, cred))
603 mode >>= 3;
604 return (vcp->vc_mode & mode) == mode ? 0 : EACCES;
607 static int
608 smb_vc_cmpshare(struct smb_share *ssp, struct smb_sharespec *dp)
610 int exact = 1;
612 if (strcmp(ssp->ss_name, dp->name) != 0)
613 return 1;
614 if (dp->owner != SMBM_ANY_OWNER) {
615 if (ssp->ss_uid != dp->owner)
616 return 1;
617 } else
618 exact = 0;
619 if (dp->group != SMBM_ANY_GROUP) {
620 if (ssp->ss_grp != dp->group)
621 return 1;
622 } else
623 exact = 0;
625 if (dp->mode & SMBM_EXACT) {
626 if (!exact)
627 return 1;
628 return (dp->mode & SMBM_MASK) == ssp->ss_mode ? 0 : 1;
630 if (smb_share_access(ssp, dp->scred, dp->mode) != 0)
631 return 1;
632 return 0;
636 * Lookup share in the given VC. Share referenced and locked on return.
637 * VC expected to be locked on entry and will be left locked on exit.
640 smb_vc_lookupshare(struct smb_vc *vcp, struct smb_sharespec *dp,
641 struct smb_cred *scred, struct smb_share **sspp)
643 struct smb_connobj *scp = NULL;
644 struct smb_share *ssp = NULL;
645 int error;
647 *sspp = NULL;
648 dp->scred = scred;
649 SMBCO_FOREACH(scp, VCTOCP(vcp)) {
650 ssp = (struct smb_share *)scp;
651 error = smb_share_lock(ssp, LK_EXCLUSIVE);
652 if (error)
653 continue;
654 if (smb_vc_cmpshare(ssp, dp) == 0)
655 break;
656 smb_share_unlock(ssp, 0);
658 if (ssp) {
659 smb_share_ref(ssp);
660 *sspp = ssp;
661 error = 0;
662 } else
663 error = ENOENT;
664 return error;
668 smb_vc_connect(struct smb_vc *vcp, struct smb_cred *scred)
671 return smb_iod_request(vcp->vc_iod, SMBIOD_EV_CONNECT | SMBIOD_EV_SYNC, NULL);
675 * Destroy VC to server, invalidate shares linked with it.
676 * Transport should be locked on entry.
678 static int
679 smb_vc_disconnect(struct smb_vc *vcp)
682 smb_iod_request(vcp->vc_iod, SMBIOD_EV_DISCONNECT | SMBIOD_EV_SYNC, NULL);
683 return 0;
686 static char smb_emptypass[] = "";
688 const char *
689 smb_vc_getpass(struct smb_vc *vcp)
691 if (vcp->vc_pass)
692 return vcp->vc_pass;
693 return smb_emptypass;
696 static int
697 smb_vc_getinfo(struct smb_vc *vcp, struct smb_vc_info *vip)
699 bzero(vip, sizeof(struct smb_vc_info));
700 vip->itype = SMB_INFO_VC;
701 vip->usecount = vcp->obj.co_usecount;
702 vip->uid = vcp->vc_uid;
703 vip->gid = vcp->vc_grp;
704 vip->mode = vcp->vc_mode;
705 vip->flags = vcp->obj.co_flags;
706 vip->sopt = vcp->vc_sopt;
707 vip->iodstate = vcp->vc_iod->iod_state;
708 bzero(&vip->sopt.sv_skey, sizeof(vip->sopt.sv_skey));
709 ksnprintf(vip->srvname, sizeof(vip->srvname), "%s", vcp->vc_srvname);
710 ksnprintf(vip->vcname, sizeof(vip->vcname), "%s", vcp->vc_username);
711 return 0;
714 u_short
715 smb_vc_nextmid(struct smb_vc *vcp)
717 u_short r;
719 SMB_CO_LOCK(&vcp->obj);
720 r = vcp->vc_mid++;
721 SMB_CO_UNLOCK(&vcp->obj);
722 return r;
726 * Share implementation
729 * Allocate share structure and attach it to the given VC
730 * Connection expected to be locked on entry. Share will be returned
731 * in locked state.
734 smb_share_create(struct smb_vc *vcp, struct smb_sharespec *shspec,
735 struct smb_cred *scred, struct smb_share **sspp)
737 struct smb_share *ssp;
738 struct ucred *cred = scred->scr_cred;
739 uid_t realuid = cred->cr_uid;
740 uid_t uid = shspec->owner;
741 gid_t gid = shspec->group;
742 int error, isroot;
744 isroot = smb_suser(cred) == 0;
746 * Only superuser can create shares with different uid and gid
748 if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot)
749 return EPERM;
750 if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot)
751 return EPERM;
752 error = smb_vc_lookupshare(vcp, shspec, scred, &ssp);
753 if (!error) {
754 smb_share_put(ssp, scred);
755 return EEXIST;
757 if (uid == SMBM_ANY_OWNER)
758 uid = realuid;
759 if (gid == SMBM_ANY_GROUP)
760 gid = cred->cr_groups[0];
761 ssp = smb_zmalloc(sizeof(*ssp), M_SMBCONN, M_WAITOK);
762 smb_co_init(SSTOCP(ssp), SMBL_SHARE, "smbss");
763 ssp->obj.co_free = smb_share_free;
764 ssp->obj.co_gone = smb_share_gone;
765 smb_sl_init(&ssp->ss_stlock, "ssstlock");
766 ssp->ss_name = smb_strdup(shspec->name);
767 if (shspec->pass && shspec->pass[0])
768 ssp->ss_pass = smb_strdup(shspec->pass);
769 ssp->ss_type = shspec->stype;
770 ssp->ss_tid = SMB_TID_UNKNOWN;
771 ssp->ss_uid = uid;
772 ssp->ss_grp = gid;
773 ssp->ss_mode = shspec->rights & SMBM_MASK;
774 smb_co_addchild(VCTOCP(vcp), SSTOCP(ssp));
775 *sspp = ssp;
776 return 0;
779 static void
780 smb_share_free(struct smb_connobj *cp)
782 struct smb_share *ssp = CPTOSS(cp);
784 SMB_STRFREE(ssp->ss_name);
785 SMB_STRFREE(ssp->ss_pass);
786 smb_sl_destroy(&ssp->ss_stlock);
787 smb_co_done(SSTOCP(ssp));
788 kfree(ssp, M_SMBCONN);
791 static void
792 smb_share_gone(struct smb_connobj *cp, struct smb_cred *scred)
794 struct smb_share *ssp = CPTOSS(cp);
796 smb_smb_treedisconnect(ssp, scred);
799 void
800 smb_share_ref(struct smb_share *ssp)
802 smb_co_ref(SSTOCP(ssp));
805 void
806 smb_share_rele(struct smb_share *ssp, struct smb_cred *scred)
808 smb_co_rele(SSTOCP(ssp), scred);
812 smb_share_get(struct smb_share *ssp, int flags, struct smb_cred *scred)
814 return smb_co_get(SSTOCP(ssp), flags, scred);
817 void
818 smb_share_put(struct smb_share *ssp, struct smb_cred *scred)
820 smb_co_put(SSTOCP(ssp), scred);
824 smb_share_lock(struct smb_share *ssp, int flags)
826 return smb_co_lock(SSTOCP(ssp), flags);
829 void
830 smb_share_unlock(struct smb_share *ssp, int flags)
832 smb_co_unlock(SSTOCP(ssp), flags);
836 smb_share_access(struct smb_share *ssp, struct smb_cred *scred, mode_t mode)
838 struct ucred *cred = scred->scr_cred;
840 if (smb_suser(cred) == 0 || cred->cr_uid == ssp->ss_uid)
841 return 0;
842 mode >>= 3;
843 if (!groupmember(ssp->ss_grp, cred))
844 mode >>= 3;
845 return (ssp->ss_mode & mode) == mode ? 0 : EACCES;
848 void
849 smb_share_invalidate(struct smb_share *ssp)
851 ssp->ss_tid = SMB_TID_UNKNOWN;
855 smb_share_valid(struct smb_share *ssp)
857 return ssp->ss_tid != SMB_TID_UNKNOWN &&
858 ssp->ss_vcgenid == SSTOVC(ssp)->vc_genid;
861 const char*
862 smb_share_getpass(struct smb_share *ssp)
864 struct smb_vc *vcp;
866 if (ssp->ss_pass)
867 return ssp->ss_pass;
868 vcp = SSTOVC(ssp);
869 if (vcp->vc_pass)
870 return vcp->vc_pass;
871 return smb_emptypass;
874 static int
875 smb_share_getinfo(struct smb_share *ssp, struct smb_share_info *sip)
877 bzero(sip, sizeof(struct smb_share_info));
878 sip->itype = SMB_INFO_SHARE;
879 sip->usecount = ssp->obj.co_usecount;
880 sip->tid = ssp->ss_tid;
881 sip->type= ssp->ss_type;
882 sip->uid = ssp->ss_uid;
883 sip->gid = ssp->ss_grp;
884 sip->mode= ssp->ss_mode;
885 sip->flags = ssp->obj.co_flags;
886 ksnprintf(sip->sname, sizeof(sip->sname), "%s", ssp->ss_name);
887 return 0;
891 * Dump an entire tree into sysctl call
893 static int
894 smb_sysctl_treedump(SYSCTL_HANDLER_ARGS)
896 struct thread *td = req->td;
897 struct ucred *ucred;
898 struct smb_cred scred;
899 struct smb_connobj *scp1, *scp2;
900 struct smb_vc *vcp;
901 struct smb_share *ssp;
902 struct smb_vc_info vci;
903 struct smb_share_info ssi;
904 int error, itype;
906 KKASSERT(td->td_proc);
907 ucred = td->td_proc->p_ucred;
909 smb_makescred(&scred, td, ucred);
910 error = smb_sm_lockvclist(LK_SHARED);
911 if (error)
912 return error;
913 SMBCO_FOREACH(scp1, &smb_vclist) {
914 vcp = (struct smb_vc *)scp1;
915 error = smb_vc_lock(vcp, LK_SHARED);
916 if (error)
917 continue;
918 smb_vc_getinfo(vcp, &vci);
919 error = SYSCTL_OUT(req, &vci, sizeof(struct smb_vc_info));
920 if (error) {
921 smb_vc_unlock(vcp, 0);
922 break;
924 SMBCO_FOREACH(scp2, VCTOCP(vcp)) {
925 ssp = (struct smb_share *)scp2;
926 error = smb_share_lock(ssp, LK_SHARED);
927 if (error) {
928 error = 0;
929 continue;
931 smb_share_getinfo(ssp, &ssi);
932 smb_share_unlock(ssp, 0);
933 error = SYSCTL_OUT(req, &ssi, sizeof(struct smb_share_info));
934 if (error)
935 break;
937 smb_vc_unlock(vcp, 0);
938 if (error)
939 break;
941 if (!error) {
942 itype = SMB_INFO_NONE;
943 error = SYSCTL_OUT(req, &itype, sizeof(itype));
945 smb_sm_unlockvclist();
946 return error;