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