smb_conn.c revision 75374
1/* 2 * Copyright (c) 2000-2001 Boris Popov 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Boris Popov. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD: head/sys/netsmb/smb_conn.c 75374 2001-04-10 07:59:06Z bp $ 33 */ 34 35/* 36 * Connection engine. 37 */ 38 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/lock.h> 45#include <sys/sysctl.h> 46#include <sys/socketvar.h> 47 48#include <sys/iconv.h> 49 50#include <netsmb/smb.h> 51#include <netsmb/smb_subr.h> 52#include <netsmb/smb_conn.h> 53#include <netsmb/smb_tran.h> 54#include <netsmb/smb_trantcp.h> 55 56static struct smb_connobj smb_vclist; 57static int smb_vcnext = 1; /* next unique id for VC */ 58 59extern struct linker_set sysctl_net_smb; 60 61SYSCTL_NODE(_net, OID_AUTO, smb, CTLFLAG_RW, NULL, "SMB protocol"); 62 63MALLOC_DEFINE(M_SMBCONN, "SMB conn", "SMB connection"); 64 65static void smb_co_init(struct smb_connobj *cp, int level, char *objname, 66 struct proc *p); 67static void smb_co_done(struct smb_connobj *cp); 68static int smb_co_lockstatus(struct smb_connobj *cp, struct proc *p); 69 70static int smb_vc_disconnect(struct smb_vc *vcp); 71static void smb_vc_free(struct smb_connobj *cp); 72static void smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred); 73static smb_co_free_t smb_share_free; 74static smb_co_gone_t smb_share_gone; 75 76static int smb_sysctl_treedump(SYSCTL_HANDLER_ARGS); 77 78SYSCTL_PROC(_net_smb, OID_AUTO, treedump, CTLFLAG_RD | CTLTYPE_OPAQUE, 79 NULL, 0, smb_sysctl_treedump, "S,treedump", "Requester tree"); 80 81int 82smb_sm_init(void) 83{ 84 85 smb_co_init(&smb_vclist, SMBL_SM, "smbsm", curproc); 86 smb_co_unlock(&smb_vclist, 0, curproc); 87 return 0; 88} 89 90int 91smb_sm_done(void) 92{ 93 94 /* XXX: hold the mutex */ 95 if (smb_vclist.co_usecount > 1) { 96 SMBERROR("%d connections still active\n", smb_vclist.co_usecount - 1); 97 return EBUSY; 98 } 99 smb_co_done(&smb_vclist); 100 return 0; 101} 102 103static int 104smb_sm_lockvclist(int flags, struct proc *p) 105{ 106 107 return smb_co_lock(&smb_vclist, flags | LK_CANRECURSE, p); 108} 109 110static void 111smb_sm_unlockvclist(struct proc *p) 112{ 113 114 smb_co_unlock(&smb_vclist, LK_RELEASE, p); 115} 116 117static int 118smb_sm_lookupint(struct smb_vcspec *vcspec, struct smb_sharespec *shspec, 119 struct smb_cred *scred, struct smb_vc **vcpp) 120{ 121 struct proc *p = scred->scr_p; 122 struct smb_vc *vcp; 123 int exact = 1; 124 int error; 125 126 vcspec->shspec = shspec; 127 error = ENOENT; 128 SMBCO_FOREACH((struct smb_connobj*)vcp, &smb_vclist) { 129 error = smb_vc_lock(vcp, LK_EXCLUSIVE, p); 130 if (error) 131 continue; 132 itry { 133 if ((vcp->obj.co_flags & SMBV_PRIVATE) || 134 !CONNADDREQ(vcp->vc_paddr, vcspec->sap) || 135 strcmp(vcp->vc_username, vcspec->username) != 0) 136 ithrow(1); 137 if (vcspec->owner != SMBM_ANY_OWNER) { 138 if (vcp->vc_uid != vcspec->owner) 139 ithrow(1); 140 } else 141 exact = 0; 142 if (vcspec->group != SMBM_ANY_GROUP) { 143 if (vcp->vc_grp != vcspec->group) 144 ithrow(1); 145 } else 146 exact = 0; 147 148 if (vcspec->mode & SMBM_EXACT) { 149 if (!exact || 150 (vcspec->mode & SMBM_MASK) != vcp->vc_mode) 151 ithrow(1); 152 } 153 if (smb_vc_access(vcp, scred, vcspec->mode) != 0) 154 ithrow(1); 155 vcspec->ssp = NULL; 156 if (shspec) 157 ithrow(smb_vc_lookupshare(vcp, shspec, scred, &vcspec->ssp)); 158 error = 0; 159 break; 160 } icatch(error) { 161 smb_vc_unlock(vcp, 0, p); 162 } ifinally { 163 } iendtry; 164 if (error == 0) 165 break; 166 } 167 if (vcp) { 168 smb_vc_ref(vcp, p); 169 *vcpp = vcp; 170 } 171 return error; 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 proc *p = scred->scr_p; 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, p); 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(p); 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(p); 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 proc *p) 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, p) == 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_p); 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, struct proc *p) 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 proc *p = scred->scr_p; 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, p); 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_p); 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 proc *p = scred->scr_p; 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, p); 327 if ((cp->co_flags & SMBO_GONE) == 0) 328 return; 329 lockmgr(&cp->co_lock, LK_DRAIN, NULL, p); 330 smb_co_gone(cp, scred); 331} 332 333int 334smb_co_lockstatus(struct smb_connobj *cp, struct proc *p) 335{ 336 return lockstatus(&cp->co_lock, p); 337} 338 339int 340smb_co_lock(struct smb_connobj *cp, int flags, struct proc *p) 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, p) == 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, p); 353} 354 355void 356smb_co_unlock(struct smb_connobj *cp, int flags, struct proc *p) 357{ 358 (void)lockmgr(&cp->co_lock, flags | LK_RELEASE, &cp->co_interlock, p); 359} 360 361static void 362smb_co_addchild(struct smb_connobj *parent, struct smb_connobj *child) 363{ 364 KASSERT(smb_co_lockstatus(parent, curproc) == LK_EXCLUSIVE, ("smb_co_addchild: parent not locked")); 365 KASSERT(smb_co_lockstatus(child, curproc) == LK_EXCLUSIVE, ("smb_co_addchild: child not locked")); 366 367 smb_co_ref(parent, curproc); 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 proc *p = scred->scr_p; 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", p); 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"); 417 error = 0; 418 itry { 419 vcp->vc_paddr = dup_sockaddr(vcspec->sap, 1); 420 ierror(vcp->vc_paddr == NULL, ENOMEM); 421 422 vcp->vc_laddr = dup_sockaddr(vcspec->lap, 1); 423 ierror(vcp->vc_laddr == NULL, ENOMEM); 424 425 ierror((vcp->vc_pass = smb_strdup(vcspec->pass)) == NULL, ENOMEM); 426 427 vcp->vc_domain = smb_strdup((domain && domain[0]) ? domain : "NODOMAIN"); 428 ierror(vcp->vc_domain == NULL, ENOMEM); 429 430 ierror((vcp->vc_srvname = smb_strdup(vcspec->srvname)) == NULL, ENOMEM); 431 ierror((vcp->vc_username = smb_strdup(vcspec->username)) == NULL, ENOMEM); 432 433 ithrow(iconv_open("tolower", vcspec->localcs, &vcp->vc_tolower)); 434 ithrow(iconv_open("toupper", vcspec->localcs, &vcp->vc_toupper)); 435 if (vcspec->servercs[0]) { 436 ithrow(iconv_open(vcspec->servercs, vcspec->localcs, 437 &vcp->vc_toserver)); 438 ithrow(iconv_open(vcspec->localcs, vcspec->servercs, 439 &vcp->vc_tolocal)); 440 } 441 442 ithrow(smb_iod_create(vcp)); 443 *vcpp = vcp; 444 smb_co_addchild(&smb_vclist, VCTOCP(vcp)); 445 } icatch(error) { 446 smb_vc_put(vcp, scred); 447 } ifinally { 448 } iendtry; 449 return error; 450} 451 452static void 453smb_vc_free(struct smb_connobj *cp) 454{ 455 struct smb_vc *vcp = CPTOVC(cp); 456 457 if (vcp->vc_iod) 458 smb_iod_destroy(vcp->vc_iod); 459 SMB_STRFREE(vcp->vc_username); 460 SMB_STRFREE(vcp->vc_srvname); 461 SMB_STRFREE(vcp->vc_pass); 462 SMB_STRFREE(vcp->vc_domain); 463 if (vcp->vc_paddr) 464 free(vcp->vc_paddr, M_SONAME); 465 if (vcp->vc_laddr) 466 free(vcp->vc_laddr, M_SONAME); 467 if (vcp->vc_tolower) 468 iconv_close(vcp->vc_tolower); 469 if (vcp->vc_toupper) 470 iconv_close(vcp->vc_toupper); 471 if (vcp->vc_tolocal) 472 iconv_close(vcp->vc_tolocal); 473 if (vcp->vc_toserver) 474 iconv_close(vcp->vc_toserver); 475 smb_co_done(VCTOCP(vcp)); 476 smb_sl_destroy(&vcp->vc_stlock); 477 free(vcp, M_SMBCONN); 478} 479 480/* 481 * Called when use count of VC dropped to zero. 482 * VC should be locked on enter with LK_DRAIN. 483 */ 484static void 485smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred) 486{ 487 struct smb_vc *vcp = CPTOVC(cp); 488 489 smb_vc_disconnect(vcp); 490} 491 492void 493smb_vc_ref(struct smb_vc *vcp, struct proc *p) 494{ 495 smb_co_ref(VCTOCP(vcp), p); 496} 497 498void 499smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred) 500{ 501 smb_co_rele(VCTOCP(vcp), scred); 502} 503 504int 505smb_vc_get(struct smb_vc *vcp, int flags, struct smb_cred *scred) 506{ 507 return smb_co_get(VCTOCP(vcp), flags, scred); 508} 509 510void 511smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred) 512{ 513 smb_co_put(VCTOCP(vcp), scred); 514} 515 516int 517smb_vc_lock(struct smb_vc *vcp, int flags, struct proc *p) 518{ 519 return smb_co_lock(VCTOCP(vcp), flags, p); 520} 521 522void 523smb_vc_unlock(struct smb_vc *vcp, int flags, struct proc *p) 524{ 525 smb_co_unlock(VCTOCP(vcp), flags, p); 526} 527 528int 529smb_vc_access(struct smb_vc *vcp, struct smb_cred *scred, mode_t mode) 530{ 531 struct ucred *cred = scred->scr_cred; 532 533 if (smb_suser(cred) == 0 || cred->cr_uid == vcp->vc_uid) 534 return 0; 535 mode >>= 3; 536 if (!groupmember(vcp->vc_grp, cred)) 537 mode >>= 3; 538 return (vcp->vc_mode & mode) == mode ? 0 : EACCES; 539} 540 541static int 542smb_vc_cmpshare(struct smb_share *ssp, struct smb_sharespec *dp) 543{ 544 int exact = 1; 545 546 if (strcmp(ssp->ss_name, dp->name) != 0) 547 return 1; 548 if (dp->owner != SMBM_ANY_OWNER) { 549 if (ssp->ss_uid != dp->owner) 550 return 1; 551 } else 552 exact = 0; 553 if (dp->group != SMBM_ANY_GROUP) { 554 if (ssp->ss_grp != dp->group) 555 return 1; 556 } else 557 exact = 0; 558 559 if (dp->mode & SMBM_EXACT) { 560 if (!exact) 561 return 1; 562 return (dp->mode & SMBM_MASK) == ssp->ss_mode ? 0 : 1; 563 } 564 if (smb_share_access(ssp, dp->scred, dp->mode) != 0) 565 return 1; 566 return 0; 567} 568 569/* 570 * Lookup share in the given VC. Share referenced and locked on return. 571 * VC expected to be locked on entry and will be left locked on exit. 572 */ 573int 574smb_vc_lookupshare(struct smb_vc *vcp, struct smb_sharespec *dp, 575 struct smb_cred *scred, struct smb_share **sspp) 576{ 577 struct proc *p = scred->scr_p; 578 struct smb_share *ssp = NULL; 579 int error; 580 581 *sspp = NULL; 582 dp->scred = scred; 583 SMBCO_FOREACH((struct smb_connobj*)ssp, VCTOCP(vcp)) { 584 error = smb_share_lock(ssp, LK_EXCLUSIVE, p); 585 if (error) 586 continue; 587 if (smb_vc_cmpshare(ssp, dp) == 0) 588 break; 589 smb_share_unlock(ssp, 0, p); 590 } 591 if (ssp) { 592 smb_share_ref(ssp, p); 593 *sspp = ssp; 594 error = 0; 595 } else 596 error = ENOENT; 597 return error; 598} 599 600int 601smb_vc_connect(struct smb_vc *vcp, struct smb_cred *scred) 602{ 603 604 return smb_iod_request(vcp->vc_iod, SMBIOD_EV_CONNECT | SMBIOD_EV_SYNC, NULL); 605} 606 607/* 608 * Destroy VC to server, invalidate shares linked with it. 609 * Transport should be locked on entry. 610 */ 611int 612smb_vc_disconnect(struct smb_vc *vcp) 613{ 614 615 smb_iod_request(vcp->vc_iod, SMBIOD_EV_DISCONNECT | SMBIOD_EV_SYNC, NULL); 616 return 0; 617} 618 619static char smb_emptypass[] = ""; 620 621const char * 622smb_vc_getpass(struct smb_vc *vcp) 623{ 624 if (vcp->vc_pass) 625 return vcp->vc_pass; 626 return smb_emptypass; 627} 628 629static int 630smb_vc_getinfo(struct smb_vc *vcp, struct smb_vc_info *vip) 631{ 632 bzero(vip, sizeof(struct smb_vc_info)); 633 vip->itype = SMB_INFO_VC; 634 vip->usecount = vcp->obj.co_usecount; 635 vip->uid = vcp->vc_uid; 636 vip->gid = vcp->vc_grp; 637 vip->mode = vcp->vc_mode; 638 vip->flags = vcp->obj.co_flags; 639 vip->sopt = vcp->vc_sopt; 640 vip->iodstate = vcp->vc_iod->iod_state; 641 bzero(&vip->sopt.sv_skey, sizeof(vip->sopt.sv_skey)); 642 snprintf(vip->srvname, sizeof(vip->srvname), "%s", vcp->vc_srvname); 643 snprintf(vip->vcname, sizeof(vip->vcname), "%s", vcp->vc_username); 644 return 0; 645} 646 647u_short 648smb_vc_nextmid(struct smb_vc *vcp) 649{ 650 u_short r; 651 652 SMB_CO_LOCK(&vcp->obj); 653 r = vcp->vc_mid++; 654 SMB_CO_UNLOCK(&vcp->obj); 655 return r; 656} 657 658/* 659 * Share implementation 660 */ 661/* 662 * Allocate share structure and attach it to the given VC 663 * Connection expected to be locked on entry. Share will be returned 664 * in locked state. 665 */ 666int 667smb_share_create(struct smb_vc *vcp, struct smb_sharespec *shspec, 668 struct smb_cred *scred, struct smb_share **sspp) 669{ 670 struct smb_share *ssp; 671 struct proc *p = scred->scr_p; 672 struct ucred *cred = scred->scr_cred; 673 uid_t realuid = cred->cr_uid; 674 uid_t uid = shspec->owner; 675 gid_t gid = shspec->group; 676 int error, isroot; 677 678 isroot = smb_suser(cred) == 0; 679 /* 680 * Only superuser can create shares with different uid and gid 681 */ 682 if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot) 683 return EPERM; 684 if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot) 685 return EPERM; 686 error = smb_vc_lookupshare(vcp, shspec, scred, &ssp); 687 if (!error) { 688 smb_share_put(ssp, scred); 689 return EEXIST; 690 } 691 if (uid == SMBM_ANY_OWNER) 692 uid = realuid; 693 if (gid == SMBM_ANY_GROUP) 694 gid = cred->cr_groups[0]; 695 ssp = smb_zmalloc(sizeof(*ssp), M_SMBCONN, M_WAITOK); 696 smb_co_init(SSTOCP(ssp), SMBL_SHARE, "smbss", p); 697 ssp->obj.co_free = smb_share_free; 698 ssp->obj.co_gone = smb_share_gone; 699 smb_sl_init(&ssp->ss_stlock, "ssstlock"); 700 ssp->ss_name = smb_strdup(shspec->name); 701 if (shspec->pass && shspec->pass[0]) 702 ssp->ss_pass = smb_strdup(shspec->pass); 703 ssp->ss_type = shspec->stype; 704 ssp->ss_tid = SMB_TID_UNKNOWN; 705 ssp->ss_uid = uid; 706 ssp->ss_grp = gid; 707 ssp->ss_mode = shspec->rights & SMBM_MASK; 708 smb_co_addchild(VCTOCP(vcp), SSTOCP(ssp)); 709 *sspp = ssp; 710 return 0; 711} 712 713static void 714smb_share_free(struct smb_connobj *cp) 715{ 716 struct smb_share *ssp = CPTOSS(cp); 717 718 SMB_STRFREE(ssp->ss_name); 719 SMB_STRFREE(ssp->ss_pass); 720 smb_sl_destroy(&ssp->ss_stlock); 721 smb_co_done(SSTOCP(ssp)); 722 free(ssp, M_SMBCONN); 723} 724 725static void 726smb_share_gone(struct smb_connobj *cp, struct smb_cred *scred) 727{ 728 struct smb_share *ssp = CPTOSS(cp); 729 730 smb_smb_treedisconnect(ssp, scred); 731} 732 733void 734smb_share_ref(struct smb_share *ssp, struct proc *p) 735{ 736 smb_co_ref(SSTOCP(ssp), p); 737} 738 739void 740smb_share_rele(struct smb_share *ssp, struct smb_cred *scred) 741{ 742 smb_co_rele(SSTOCP(ssp), scred); 743} 744 745int 746smb_share_get(struct smb_share *ssp, int flags, struct smb_cred *scred) 747{ 748 return smb_co_get(SSTOCP(ssp), flags, scred); 749} 750 751void 752smb_share_put(struct smb_share *ssp, struct smb_cred *scred) 753{ 754 smb_co_put(SSTOCP(ssp), scred); 755} 756 757int 758smb_share_lock(struct smb_share *ssp, int flags, struct proc *p) 759{ 760 return smb_co_lock(SSTOCP(ssp), flags, p); 761} 762 763void 764smb_share_unlock(struct smb_share *ssp, int flags, struct proc *p) 765{ 766 smb_co_unlock(SSTOCP(ssp), flags, p); 767} 768 769int 770smb_share_access(struct smb_share *ssp, struct smb_cred *scred, mode_t mode) 771{ 772 struct ucred *cred = scred->scr_cred; 773 774 if (smb_suser(cred) == 0 || cred->cr_uid == ssp->ss_uid) 775 return 0; 776 mode >>= 3; 777 if (!groupmember(ssp->ss_grp, cred)) 778 mode >>= 3; 779 return (ssp->ss_mode & mode) == mode ? 0 : EACCES; 780} 781 782void 783smb_share_invalidate(struct smb_share *ssp) 784{ 785 ssp->ss_tid = SMB_TID_UNKNOWN; 786} 787 788int 789smb_share_valid(struct smb_share *ssp) 790{ 791 return ssp->ss_tid != SMB_TID_UNKNOWN && 792 ssp->ss_vcgenid == SSTOVC(ssp)->vc_genid; 793} 794 795const char* 796smb_share_getpass(struct smb_share *ssp) 797{ 798 struct smb_vc *vcp; 799 800 if (ssp->ss_pass) 801 return ssp->ss_pass; 802 vcp = SSTOVC(ssp); 803 if (vcp->vc_pass) 804 return vcp->vc_pass; 805 return smb_emptypass; 806} 807 808static int 809smb_share_getinfo(struct smb_share *ssp, struct smb_share_info *sip) 810{ 811 bzero(sip, sizeof(struct smb_share_info)); 812 sip->itype = SMB_INFO_SHARE; 813 sip->usecount = ssp->obj.co_usecount; 814 sip->tid = ssp->ss_tid; 815 sip->type= ssp->ss_type; 816 sip->uid = ssp->ss_uid; 817 sip->gid = ssp->ss_grp; 818 sip->mode= ssp->ss_mode; 819 sip->flags = ssp->obj.co_flags; 820 snprintf(sip->sname, sizeof(sip->sname), "%s", ssp->ss_name); 821 return 0; 822} 823 824/* 825 * Dump an entire tree into sysctl call 826 */ 827static int 828smb_sysctl_treedump(SYSCTL_HANDLER_ARGS) 829{ 830 struct proc *p = req->p; 831 struct smb_cred scred; 832 struct smb_vc *vcp; 833 struct smb_share *ssp; 834 struct smb_vc_info vci; 835 struct smb_share_info ssi; 836 int error, itype; 837 838 smb_makescred(&scred, p, p->p_ucred); 839 error = smb_sm_lockvclist(LK_SHARED, p); 840 if (error) 841 return error; 842 SMBCO_FOREACH((struct smb_connobj*)vcp, &smb_vclist) { 843 error = smb_vc_lock(vcp, LK_SHARED, p); 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, p); 850 break; 851 } 852 SMBCO_FOREACH((struct smb_connobj*)ssp, VCTOCP(vcp)) { 853 error = smb_share_lock(ssp, LK_SHARED, p); 854 if (error) { 855 error = 0; 856 continue; 857 } 858 smb_share_getinfo(ssp, &ssi); 859 smb_share_unlock(ssp, 0, p); 860 error = SYSCTL_OUT(req, &ssi, sizeof(struct smb_share_info)); 861 if (error) 862 break; 863 } 864 smb_vc_unlock(vcp, 0, p); 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(p); 873 return error; 874} 875