2 * Copyright (c) 2000-2001 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/sys/netsmb/smb_conn.c,v 1.1.2.1 2001/05/22 08:32:33 bp Exp $
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
46 #include <sys/sysctl.h>
47 #include <sys/socketvar.h>
49 #include <sys/iconv.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
);
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");
93 smb_co_init(&smb_vclist
, SMBL_SM
, "smbsm");
94 smb_co_unlock(&smb_vclist
, 0);
102 /* XXX: hold the mutex */
103 if (smb_vclist
.co_usecount
> 1) {
104 SMBERROR("%d connections still active\n", smb_vclist
.co_usecount
- 1);
107 smb_co_done(&smb_vclist
);
112 smb_sm_lockvclist(int flags
)
114 return smb_co_lock(&smb_vclist
, flags
| LK_CANRECURSE
);
118 smb_sm_unlockvclist(void)
120 smb_co_unlock(&smb_vclist
, LK_RELEASE
);
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
;
132 vcspec
->shspec
= shspec
;
135 SMBCO_FOREACH(scp
, &smb_vclist
) {
136 vcp
= (struct smb_vc
*)scp
;
137 error
= smb_vc_lock(vcp
, LK_EXCLUSIVE
);
142 if ((vcp
->obj
.co_flags
& SMBV_PRIVATE
) ||
143 !CONNADDREQ(vcp
->vc_paddr
, vcspec
->sap
) ||
144 strcmp(vcp
->vc_username
, vcspec
->username
) != 0)
146 if (vcspec
->owner
!= SMBM_ANY_OWNER
) {
147 if (vcp
->vc_uid
!= vcspec
->owner
)
151 if (vcspec
->group
!= SMBM_ANY_GROUP
) {
152 if (vcp
->vc_grp
!= vcspec
->group
)
157 if (vcspec
->mode
& SMBM_EXACT
) {
159 (vcspec
->mode
& SMBM_MASK
) != vcp
->vc_mode
)
162 if (smb_vc_access(vcp
, scred
, vcspec
->mode
) != 0)
166 error
= smb_vc_lookupshare(vcp
, shspec
, scred
, &vcspec
->ssp
);
173 smb_vc_unlock(vcp
, 0);
183 smb_sm_lookup(struct smb_vcspec
*vcspec
, struct smb_sharespec
*shspec
,
184 struct smb_cred
*scred
, struct smb_vc
**vcpp
)
187 struct smb_share
*ssp
= NULL
;
192 error
= smb_sm_lockvclist(LK_EXCLUSIVE
);
195 error
= smb_sm_lookupint(vcspec
, shspec
, scred
, vcpp
);
196 if (error
== 0 || (vcspec
->flags
& SMBV_CREATE
) == 0) {
197 smb_sm_unlockvclist();
200 error
= smb_sm_lookupint(vcspec
, NULL
, scred
, &vcp
);
202 error
= smb_vc_create(vcspec
, scred
, &vcp
);
205 error
= smb_vc_connect(vcp
, scred
);
211 error
= smb_share_create(vcp
, shspec
, scred
, &ssp
);
214 error
= smb_smb_treeconnect(ssp
, scred
);
218 smb_share_put(ssp
, scred
);
220 smb_sm_unlockvclist();
224 smb_vc_put(vcp
, scred
);
229 * Common code for connection object
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
;
239 smb_co_lock(cp
, LK_EXCLUSIVE
);
243 smb_co_done(struct smb_connobj
*cp
)
245 smb_sl_destroy(&cp
->co_interlock
);
246 lockdestroy(&cp
->co_lock
);
250 smb_co_gone(struct smb_connobj
*cp
, struct smb_cred
*scred
)
252 struct smb_connobj
*parent
;
255 cp
->co_gone(cp
, scred
);
256 parent
= cp
->co_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
);
271 smb_co_ref(struct smb_connobj
*cp
)
279 smb_co_rele(struct smb_connobj
*cp
, struct smb_cred
*scred
)
282 if (cp
->co_usecount
> 1) {
287 if (cp
->co_usecount
<= 0) {
288 SMBERROR("negative use_count for object %d", cp
->co_level
);
293 if ((cp
->co_flags
& SMBO_GONE
) == 0) {
294 cp
->co_flags
|= SMBO_GONE
;
296 lockmgr(&cp
->co_lock
, LK_EXCLUSIVE
);
297 smb_co_gone(cp
, scred
);
300 wakeup(&cp
->co_lock
);
305 smb_co_get(struct smb_connobj
*cp
, int flags
, struct smb_cred
*scred
)
312 error
= smb_co_lock(cp
, flags
);
315 if (cp
->co_usecount
> 1) {
318 } else if (cp
->co_usecount
<= 0) {
319 SMBERROR("negative use_count for object %d", cp
->co_level
);
323 if ((cp
->co_flags
& SMBO_GONE
) == 0) {
324 cp
->co_flags
|= SMBO_GONE
;
326 lockmgr(&cp
->co_lock
, LK_EXCLUSIVE
);
327 smb_co_gone(cp
, scred
);
338 smb_co_put(struct smb_connobj
*cp
, struct smb_cred
*scred
)
341 if (cp
->co_usecount
> 1) {
344 lockmgr(&cp
->co_lock
, LK_RELEASE
);
347 if (cp
->co_usecount
<= 0) {
348 SMBERROR("negative use_count for object %d", cp
->co_level
);
353 if ((cp
->co_flags
& SMBO_GONE
) == 0) {
354 cp
->co_flags
|= SMBO_GONE
;
356 lockmgr(&cp
->co_lock
, LK_RELEASE
);
357 lockmgr(&cp
->co_lock
, LK_EXCLUSIVE
);
358 smb_co_gone(cp
, scred
);
361 lockmgr(&cp
->co_lock
, LK_RELEASE
);
362 wakeup(&cp
->co_lock
);
367 smb_co_lockstatus(struct smb_connobj
*cp
, struct thread
*td
)
369 return lockstatus(&cp
->co_lock
, td
);
373 smb_co_lock(struct smb_connobj
*cp
, int flags
)
377 KKASSERT(cp
->co_usecount
> 0);
378 if (cp
->co_flags
& SMBO_GONE
)
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
);
387 error
= lockmgr(&cp
->co_lock
, flags
);
388 if (error
== 0 && (cp
->co_flags
& SMBO_GONE
)) {
389 lockmgr(&cp
->co_lock
, LK_RELEASE
);
396 smb_co_unlock(struct smb_connobj
*cp
, int flags
)
398 lockmgr(&cp
->co_lock
, flags
| LK_RELEASE
);
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"));
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
)
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
;
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
)
434 if (gid
!= SMBM_ANY_GROUP
&& !groupmember(gid
, cred
) && !isroot
)
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
;
448 vcp
->vc_mackey
= NULL
;
449 vcp
->vc_mackeylen
= 0;
451 if (uid
== SMBM_ANY_OWNER
)
453 if (gid
== SMBM_ANY_GROUP
)
454 gid
= cred
->cr_groups
[0];
458 smb_sl_init(&vcp
->vc_stlock
, "vcstlock");
462 vcp
->vc_paddr
= dup_sockaddr(vcspec
->sap
);
463 if (vcp
->vc_paddr
== NULL
)
466 vcp
->vc_laddr
= dup_sockaddr(vcspec
->lap
);
467 if (vcp
->vc_laddr
== NULL
)
470 vcp
->vc_pass
= smb_strdup(vcspec
->pass
);
471 if (vcp
->vc_pass
== NULL
)
474 vcp
->vc_domain
= smb_strdup((domain
&& domain
[0]) ? domain
476 if (vcp
->vc_domain
== NULL
)
479 vcp
->vc_srvname
= smb_strdup(vcspec
->srvname
);
480 if (vcp
->vc_srvname
== NULL
)
482 vcp
->vc_username
= smb_strdup(vcspec
->username
);
483 if (vcp
->vc_username
== NULL
)
486 error
= iconv_open("tolower", vcspec
->localcs
, &vcp
->vc_tolower
);
489 error
= iconv_open("toupper", vcspec
->localcs
, &vcp
->vc_toupper
);
493 if (vcspec
->servercs
[0]) {
494 error
= iconv_open(vcspec
->servercs
, vcspec
->localcs
,
498 error
= iconv_open(vcspec
->localcs
, vcspec
->servercs
,
504 error
= smb_iod_create(vcp
);
508 smb_co_addchild(&smb_vclist
, VCTOCP(vcp
));
513 smb_vc_put(vcp
, scred
);
518 smb_vc_free(struct smb_connobj
*cp
)
520 struct smb_vc
*vcp
= CPTOVC(cp
);
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
);
529 kfree(vcp
->vc_mackey
, M_SMBTEMP
);
531 kfree(vcp
->vc_paddr
, M_SONAME
);
533 kfree(vcp
->vc_laddr
, M_SONAME
);
535 iconv_close(vcp
->vc_tolower
);
537 iconv_close(vcp
->vc_toupper
);
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.
551 smb_vc_gone(struct smb_connobj
*cp
, struct smb_cred
*scred
)
553 struct smb_vc
*vcp
= CPTOVC(cp
);
555 smb_vc_disconnect(vcp
);
559 smb_vc_ref(struct smb_vc
*vcp
)
561 smb_co_ref(VCTOCP(vcp
));
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
);
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
);
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
)
602 if (!groupmember(vcp
->vc_grp
, cred
))
604 return (vcp
->vc_mode
& mode
) == mode
? 0 : EACCES
;
608 smb_vc_cmpshare(struct smb_share
*ssp
, struct smb_sharespec
*dp
)
612 if (strcmp(ssp
->ss_name
, dp
->name
) != 0)
614 if (dp
->owner
!= SMBM_ANY_OWNER
) {
615 if (ssp
->ss_uid
!= dp
->owner
)
619 if (dp
->group
!= SMBM_ANY_GROUP
) {
620 if (ssp
->ss_grp
!= dp
->group
)
625 if (dp
->mode
& SMBM_EXACT
) {
628 return (dp
->mode
& SMBM_MASK
) == ssp
->ss_mode
? 0 : 1;
630 if (smb_share_access(ssp
, dp
->scred
, dp
->mode
) != 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
;
649 SMBCO_FOREACH(scp
, VCTOCP(vcp
)) {
650 ssp
= (struct smb_share
*)scp
;
651 error
= smb_share_lock(ssp
, LK_EXCLUSIVE
);
654 if (smb_vc_cmpshare(ssp
, dp
) == 0)
656 smb_share_unlock(ssp
, 0);
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.
679 smb_vc_disconnect(struct smb_vc
*vcp
)
682 smb_iod_request(vcp
->vc_iod
, SMBIOD_EV_DISCONNECT
| SMBIOD_EV_SYNC
, NULL
);
686 static char smb_emptypass
[] = "";
689 smb_vc_getpass(struct smb_vc
*vcp
)
693 return smb_emptypass
;
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
);
715 smb_vc_nextmid(struct smb_vc
*vcp
)
719 SMB_CO_LOCK(&vcp
->obj
);
721 SMB_CO_UNLOCK(&vcp
->obj
);
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
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
;
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
)
750 if (gid
!= SMBM_ANY_GROUP
&& !groupmember(gid
, cred
) && !isroot
)
752 error
= smb_vc_lookupshare(vcp
, shspec
, scred
, &ssp
);
754 smb_share_put(ssp
, scred
);
757 if (uid
== SMBM_ANY_OWNER
)
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
;
773 ssp
->ss_mode
= shspec
->rights
& SMBM_MASK
;
774 smb_co_addchild(VCTOCP(vcp
), SSTOCP(ssp
));
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
);
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
);
800 smb_share_ref(struct smb_share
*ssp
)
802 smb_co_ref(SSTOCP(ssp
));
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
);
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
);
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
)
843 if (!groupmember(ssp
->ss_grp
, cred
))
845 return (ssp
->ss_mode
& mode
) == mode
? 0 : EACCES
;
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
;
862 smb_share_getpass(struct smb_share
*ssp
)
871 return smb_emptypass
;
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
);
891 * Dump an entire tree into sysctl call
894 smb_sysctl_treedump(SYSCTL_HANDLER_ARGS
)
896 struct thread
*td
= req
->td
;
898 struct smb_cred scred
;
899 struct smb_connobj
*scp1
, *scp2
;
901 struct smb_share
*ssp
;
902 struct smb_vc_info vci
;
903 struct smb_share_info ssi
;
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
);
913 SMBCO_FOREACH(scp1
, &smb_vclist
) {
914 vcp
= (struct smb_vc
*)scp1
;
915 error
= smb_vc_lock(vcp
, LK_SHARED
);
918 smb_vc_getinfo(vcp
, &vci
);
919 error
= SYSCTL_OUT(req
, &vci
, sizeof(struct smb_vc_info
));
921 smb_vc_unlock(vcp
, 0);
924 SMBCO_FOREACH(scp2
, VCTOCP(vcp
)) {
925 ssp
= (struct smb_share
*)scp2
;
926 error
= smb_share_lock(ssp
, LK_SHARED
);
931 smb_share_getinfo(ssp
, &ssi
);
932 smb_share_unlock(ssp
, 0);
933 error
= SYSCTL_OUT(req
, &ssi
, sizeof(struct smb_share_info
));
937 smb_vc_unlock(vcp
, 0);
942 itype
= SMB_INFO_NONE
;
943 error
= SYSCTL_OUT(req
, &itype
, sizeof(itype
));
945 smb_sm_unlockvclist();