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: src/sys/netsmb/smb_conn.c,v 1.1.2.1 2001/05/22 08:32:33 bp Exp $ 33 * $DragonFly: src/sys/netproto/smb/smb_conn.c,v 1.20 2006/12/20 18:14:44 dillon Exp $ 34 */ 35 36 /* 37 * Connection engine. 38 */ 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 "smb.h" 52 #include "smb_subr.h" 53 #include "smb_conn.h" 54 #include "smb_tran.h" 55 #include "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 static void smb_co_done(struct smb_connobj *cp); 66 static int smb_co_lockstatus(struct smb_connobj *cp, struct thread *td); 67 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); 71 72 /* 73 * Connection object 74 */ 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); 81 82 static smb_co_free_t smb_share_free; 83 static smb_co_gone_t smb_share_gone; 84 85 static int smb_sysctl_treedump(SYSCTL_HANDLER_ARGS); 86 87 SYSCTL_PROC(_net_smb, OID_AUTO, treedump, CTLFLAG_RD | CTLTYPE_OPAQUE, 88 NULL, 0, smb_sysctl_treedump, "S,treedump", "Requester tree"); 89 90 int 91 smb_sm_init(void) 92 { 93 smb_co_init(&smb_vclist, SMBL_SM, "smbsm"); 94 smb_co_unlock(&smb_vclist, 0); 95 return 0; 96 } 97 98 int 99 smb_sm_done(void) 100 { 101 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; 106 } 107 smb_co_done(&smb_vclist); 108 return 0; 109 } 110 111 static int 112 smb_sm_lockvclist(int flags) 113 { 114 return smb_co_lock(&smb_vclist, flags | LK_CANRECURSE); 115 } 116 117 static void 118 smb_sm_unlockvclist(void) 119 { 120 smb_co_unlock(&smb_vclist, LK_RELEASE); 121 } 122 123 static int 124 smb_sm_lookupint(struct smb_vcspec *vcspec, struct smb_sharespec *shspec, 125 struct smb_cred *scred, struct smb_vc **vcpp) 126 { 127 struct smb_connobj *scp; 128 struct smb_vc *vcp; 129 int exact = 1; 130 int error; 131 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; 140 itry { 141 if ((vcp->obj.co_flags & SMBV_PRIVATE) || 142 !CONNADDREQ(vcp->vc_paddr, vcspec->sap) || 143 strcmp(vcp->vc_username, vcspec->username) != 0) 144 ithrow(1); 145 if (vcspec->owner != SMBM_ANY_OWNER) { 146 if (vcp->vc_uid != vcspec->owner) 147 ithrow(1); 148 } else 149 exact = 0; 150 if (vcspec->group != SMBM_ANY_GROUP) { 151 if (vcp->vc_grp != vcspec->group) 152 ithrow(1); 153 } else 154 exact = 0; 155 156 if (vcspec->mode & SMBM_EXACT) { 157 if (!exact || 158 (vcspec->mode & SMBM_MASK) != vcp->vc_mode) 159 ithrow(1); 160 } 161 if (smb_vc_access(vcp, scred, vcspec->mode) != 0) 162 ithrow(1); 163 vcspec->ssp = NULL; 164 if (shspec) 165 ithrow(smb_vc_lookupshare(vcp, shspec, scred, &vcspec->ssp)); 166 error = 0; 167 break; 168 } icatch(error) { 169 smb_vc_unlock(vcp, 0); 170 } ifinally { 171 } iendtry; 172 if (error == 0) 173 break; 174 } 175 if (vcp) { 176 smb_vc_ref(vcp); 177 *vcpp = vcp; 178 } 179 return error; 180 } 181 182 int 183 smb_sm_lookup(struct smb_vcspec *vcspec, struct smb_sharespec *shspec, 184 struct smb_cred *scred, struct smb_vc **vcpp) 185 { 186 struct smb_vc *vcp; 187 struct smb_share *ssp = NULL; 188 int error; 189 190 *vcpp = vcp = NULL; 191 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; 199 } 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; 208 } 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; 226 } 227 228 /* 229 * Common code for connection object 230 */ 231 static void 232 smb_co_init(struct smb_connobj *cp, int level, char *objname) 233 { 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); 240 } 241 242 static void 243 smb_co_done(struct smb_connobj *cp) 244 { 245 smb_sl_destroy(&cp->co_interlock); 246 lockdestroy(&cp->co_lock); 247 } 248 249 static void 250 smb_co_gone(struct smb_connobj *cp, struct smb_cred *scred) 251 { 252 struct smb_connobj *parent; 253 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); 261 } 262 lockmgr(&cp->co_lock, LK_RELEASE); 263 while (cp->co_usecount > 0) { 264 tsleep(&cp->co_lock, 0, "smbgone", hz); 265 } 266 if (cp->co_free) 267 cp->co_free(cp); 268 } 269 270 void 271 smb_co_ref(struct smb_connobj *cp) 272 { 273 SMB_CO_LOCK(cp); 274 cp->co_usecount++; 275 SMB_CO_UNLOCK(cp); 276 } 277 278 void 279 smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred) 280 { 281 SMB_CO_LOCK(cp); 282 if (cp->co_usecount > 1) { 283 cp->co_usecount--; 284 SMB_CO_UNLOCK(cp); 285 return; 286 } 287 if (cp->co_usecount <= 0) { 288 SMBERROR("negative use_count for object %d", cp->co_level); 289 SMB_CO_UNLOCK(cp); 290 return; 291 } 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); 301 } 302 } 303 304 int 305 smb_co_get(struct smb_connobj *cp, int flags, struct smb_cred *scred) 306 { 307 int error; 308 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); 330 } 331 } 332 return error; 333 } 334 return 0; 335 } 336 337 void 338 smb_co_put(struct smb_connobj *cp, struct smb_cred *scred) 339 { 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; 346 } 347 if (cp->co_usecount <= 0) { 348 SMBERROR("negative use_count for object %d", cp->co_level); 349 SMB_CO_UNLOCK(cp); 350 return; 351 } 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); 363 } 364 } 365 366 int 367 smb_co_lockstatus(struct smb_connobj *cp, struct thread *td) 368 { 369 return lockstatus(&cp->co_lock, td); 370 } 371 372 int 373 smb_co_lock(struct smb_connobj *cp, int flags) 374 { 375 int error; 376 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; 386 } 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; 391 } 392 return (error); 393 } 394 395 void 396 smb_co_unlock(struct smb_connobj *cp, int flags) 397 { 398 lockmgr(&cp->co_lock, flags | LK_RELEASE); 399 } 400 401 static void 402 smb_co_addchild(struct smb_connobj *parent, struct smb_connobj *child) 403 { 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")); 406 407 smb_co_ref(parent); 408 SLIST_INSERT_HEAD(&parent->co_children, child, co_next); 409 child->co_parent = parent; 410 } 411 412 /* 413 * Session implementation 414 */ 415 416 int 417 smb_vc_create(struct smb_vcspec *vcspec, 418 struct smb_cred *scred, struct smb_vc **vcpp) 419 { 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; 427 428 isroot = smb_suser(cred) == 0; 429 /* 430 * Only superuser can create VCs with different uid and gid 431 */ 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; 436 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 448 if (uid == SMBM_ANY_OWNER) 449 uid = realuid; 450 if (gid == SMBM_ANY_GROUP) 451 gid = cred->cr_groups[0]; 452 vcp->vc_uid = uid; 453 vcp->vc_grp = gid; 454 455 smb_sl_init(&vcp->vc_stlock, "vcstlock"); 456 error = 0; 457 itry { 458 vcp->vc_paddr = dup_sockaddr(vcspec->sap); 459 ierror(vcp->vc_paddr == NULL, ENOMEM); 460 461 vcp->vc_laddr = dup_sockaddr(vcspec->lap); 462 ierror(vcp->vc_laddr == NULL, ENOMEM); 463 464 ierror((vcp->vc_pass = smb_strdup(vcspec->pass)) == NULL, ENOMEM); 465 466 vcp->vc_domain = smb_strdup((domain && domain[0]) ? domain : "NODOMAIN"); 467 ierror(vcp->vc_domain == NULL, ENOMEM); 468 469 ierror((vcp->vc_srvname = smb_strdup(vcspec->srvname)) == NULL, ENOMEM); 470 ierror((vcp->vc_username = smb_strdup(vcspec->username)) == NULL, ENOMEM); 471 472 ithrow(iconv_open("tolower", vcspec->localcs, &vcp->vc_tolower)); 473 ithrow(iconv_open("toupper", vcspec->localcs, &vcp->vc_toupper)); 474 if (vcspec->servercs[0]) { 475 ithrow(iconv_open(vcspec->servercs, vcspec->localcs, 476 &vcp->vc_toserver)); 477 ithrow(iconv_open(vcspec->localcs, vcspec->servercs, 478 &vcp->vc_tolocal)); 479 } 480 481 ithrow(smb_iod_create(vcp)); 482 *vcpp = vcp; 483 smb_co_addchild(&smb_vclist, VCTOCP(vcp)); 484 } icatch(error) { 485 smb_vc_put(vcp, scred); 486 } ifinally { 487 } iendtry; 488 return error; 489 } 490 491 static void 492 smb_vc_free(struct smb_connobj *cp) 493 { 494 struct smb_vc *vcp = CPTOVC(cp); 495 496 if (vcp->vc_iod) 497 smb_iod_destroy(vcp->vc_iod); 498 SMB_STRFREE(vcp->vc_username); 499 SMB_STRFREE(vcp->vc_srvname); 500 SMB_STRFREE(vcp->vc_pass); 501 SMB_STRFREE(vcp->vc_domain); 502 if (vcp->vc_paddr) 503 kfree(vcp->vc_paddr, M_SONAME); 504 if (vcp->vc_laddr) 505 kfree(vcp->vc_laddr, M_SONAME); 506 if (vcp->vc_tolower) 507 iconv_close(vcp->vc_tolower); 508 if (vcp->vc_toupper) 509 iconv_close(vcp->vc_toupper); 510 if (vcp->vc_tolocal) 511 iconv_close(vcp->vc_tolocal); 512 if (vcp->vc_toserver) 513 iconv_close(vcp->vc_toserver); 514 smb_co_done(VCTOCP(vcp)); 515 smb_sl_destroy(&vcp->vc_stlock); 516 kfree(vcp, M_SMBCONN); 517 } 518 519 /* 520 * Called when use count of VC dropped to zero. 521 */ 522 static void 523 smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred) 524 { 525 struct smb_vc *vcp = CPTOVC(cp); 526 527 smb_vc_disconnect(vcp); 528 } 529 530 void 531 smb_vc_ref(struct smb_vc *vcp) 532 { 533 smb_co_ref(VCTOCP(vcp)); 534 } 535 536 void 537 smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred) 538 { 539 smb_co_rele(VCTOCP(vcp), scred); 540 } 541 542 int 543 smb_vc_get(struct smb_vc *vcp, int flags, struct smb_cred *scred) 544 { 545 return smb_co_get(VCTOCP(vcp), flags, scred); 546 } 547 548 void 549 smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred) 550 { 551 smb_co_put(VCTOCP(vcp), scred); 552 } 553 554 int 555 smb_vc_lock(struct smb_vc *vcp, int flags) 556 { 557 return smb_co_lock(VCTOCP(vcp), flags); 558 } 559 560 void 561 smb_vc_unlock(struct smb_vc *vcp, int flags) 562 { 563 smb_co_unlock(VCTOCP(vcp), flags); 564 } 565 566 int 567 smb_vc_access(struct smb_vc *vcp, struct smb_cred *scred, mode_t mode) 568 { 569 struct ucred *cred = scred->scr_cred; 570 571 if (smb_suser(cred) == 0 || cred->cr_uid == vcp->vc_uid) 572 return 0; 573 mode >>= 3; 574 if (!groupmember(vcp->vc_grp, cred)) 575 mode >>= 3; 576 return (vcp->vc_mode & mode) == mode ? 0 : EACCES; 577 } 578 579 static int 580 smb_vc_cmpshare(struct smb_share *ssp, struct smb_sharespec *dp) 581 { 582 int exact = 1; 583 584 if (strcmp(ssp->ss_name, dp->name) != 0) 585 return 1; 586 if (dp->owner != SMBM_ANY_OWNER) { 587 if (ssp->ss_uid != dp->owner) 588 return 1; 589 } else 590 exact = 0; 591 if (dp->group != SMBM_ANY_GROUP) { 592 if (ssp->ss_grp != dp->group) 593 return 1; 594 } else 595 exact = 0; 596 597 if (dp->mode & SMBM_EXACT) { 598 if (!exact) 599 return 1; 600 return (dp->mode & SMBM_MASK) == ssp->ss_mode ? 0 : 1; 601 } 602 if (smb_share_access(ssp, dp->scred, dp->mode) != 0) 603 return 1; 604 return 0; 605 } 606 607 /* 608 * Lookup share in the given VC. Share referenced and locked on return. 609 * VC expected to be locked on entry and will be left locked on exit. 610 */ 611 int 612 smb_vc_lookupshare(struct smb_vc *vcp, struct smb_sharespec *dp, 613 struct smb_cred *scred, struct smb_share **sspp) 614 { 615 struct smb_connobj *scp = NULL; 616 struct smb_share *ssp = NULL; 617 int error; 618 619 *sspp = NULL; 620 dp->scred = scred; 621 SMBCO_FOREACH(scp, VCTOCP(vcp)) { 622 ssp = (struct smb_share *)scp; 623 error = smb_share_lock(ssp, LK_EXCLUSIVE); 624 if (error) 625 continue; 626 if (smb_vc_cmpshare(ssp, dp) == 0) 627 break; 628 smb_share_unlock(ssp, 0); 629 } 630 if (ssp) { 631 smb_share_ref(ssp); 632 *sspp = ssp; 633 error = 0; 634 } else 635 error = ENOENT; 636 return error; 637 } 638 639 int 640 smb_vc_connect(struct smb_vc *vcp, struct smb_cred *scred) 641 { 642 643 return smb_iod_request(vcp->vc_iod, SMBIOD_EV_CONNECT | SMBIOD_EV_SYNC, NULL); 644 } 645 646 /* 647 * Destroy VC to server, invalidate shares linked with it. 648 * Transport should be locked on entry. 649 */ 650 int 651 smb_vc_disconnect(struct smb_vc *vcp) 652 { 653 654 smb_iod_request(vcp->vc_iod, SMBIOD_EV_DISCONNECT | SMBIOD_EV_SYNC, NULL); 655 return 0; 656 } 657 658 static char smb_emptypass[] = ""; 659 660 const char * 661 smb_vc_getpass(struct smb_vc *vcp) 662 { 663 if (vcp->vc_pass) 664 return vcp->vc_pass; 665 return smb_emptypass; 666 } 667 668 static int 669 smb_vc_getinfo(struct smb_vc *vcp, struct smb_vc_info *vip) 670 { 671 bzero(vip, sizeof(struct smb_vc_info)); 672 vip->itype = SMB_INFO_VC; 673 vip->usecount = vcp->obj.co_usecount; 674 vip->uid = vcp->vc_uid; 675 vip->gid = vcp->vc_grp; 676 vip->mode = vcp->vc_mode; 677 vip->flags = vcp->obj.co_flags; 678 vip->sopt = vcp->vc_sopt; 679 vip->iodstate = vcp->vc_iod->iod_state; 680 bzero(&vip->sopt.sv_skey, sizeof(vip->sopt.sv_skey)); 681 ksnprintf(vip->srvname, sizeof(vip->srvname), "%s", vcp->vc_srvname); 682 ksnprintf(vip->vcname, sizeof(vip->vcname), "%s", vcp->vc_username); 683 return 0; 684 } 685 686 u_short 687 smb_vc_nextmid(struct smb_vc *vcp) 688 { 689 u_short r; 690 691 SMB_CO_LOCK(&vcp->obj); 692 r = vcp->vc_mid++; 693 SMB_CO_UNLOCK(&vcp->obj); 694 return r; 695 } 696 697 /* 698 * Share implementation 699 */ 700 /* 701 * Allocate share structure and attach it to the given VC 702 * Connection expected to be locked on entry. Share will be returned 703 * in locked state. 704 */ 705 int 706 smb_share_create(struct smb_vc *vcp, struct smb_sharespec *shspec, 707 struct smb_cred *scred, struct smb_share **sspp) 708 { 709 struct smb_share *ssp; 710 struct ucred *cred = scred->scr_cred; 711 uid_t realuid = cred->cr_uid; 712 uid_t uid = shspec->owner; 713 gid_t gid = shspec->group; 714 int error, isroot; 715 716 isroot = smb_suser(cred) == 0; 717 /* 718 * Only superuser can create shares with different uid and gid 719 */ 720 if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot) 721 return EPERM; 722 if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot) 723 return EPERM; 724 error = smb_vc_lookupshare(vcp, shspec, scred, &ssp); 725 if (!error) { 726 smb_share_put(ssp, scred); 727 return EEXIST; 728 } 729 if (uid == SMBM_ANY_OWNER) 730 uid = realuid; 731 if (gid == SMBM_ANY_GROUP) 732 gid = cred->cr_groups[0]; 733 ssp = smb_zmalloc(sizeof(*ssp), M_SMBCONN, M_WAITOK); 734 smb_co_init(SSTOCP(ssp), SMBL_SHARE, "smbss"); 735 ssp->obj.co_free = smb_share_free; 736 ssp->obj.co_gone = smb_share_gone; 737 smb_sl_init(&ssp->ss_stlock, "ssstlock"); 738 ssp->ss_name = smb_strdup(shspec->name); 739 if (shspec->pass && shspec->pass[0]) 740 ssp->ss_pass = smb_strdup(shspec->pass); 741 ssp->ss_type = shspec->stype; 742 ssp->ss_tid = SMB_TID_UNKNOWN; 743 ssp->ss_uid = uid; 744 ssp->ss_grp = gid; 745 ssp->ss_mode = shspec->rights & SMBM_MASK; 746 smb_co_addchild(VCTOCP(vcp), SSTOCP(ssp)); 747 *sspp = ssp; 748 return 0; 749 } 750 751 static void 752 smb_share_free(struct smb_connobj *cp) 753 { 754 struct smb_share *ssp = CPTOSS(cp); 755 756 SMB_STRFREE(ssp->ss_name); 757 SMB_STRFREE(ssp->ss_pass); 758 smb_sl_destroy(&ssp->ss_stlock); 759 smb_co_done(SSTOCP(ssp)); 760 kfree(ssp, M_SMBCONN); 761 } 762 763 static void 764 smb_share_gone(struct smb_connobj *cp, struct smb_cred *scred) 765 { 766 struct smb_share *ssp = CPTOSS(cp); 767 768 smb_smb_treedisconnect(ssp, scred); 769 } 770 771 void 772 smb_share_ref(struct smb_share *ssp) 773 { 774 smb_co_ref(SSTOCP(ssp)); 775 } 776 777 void 778 smb_share_rele(struct smb_share *ssp, struct smb_cred *scred) 779 { 780 smb_co_rele(SSTOCP(ssp), scred); 781 } 782 783 int 784 smb_share_get(struct smb_share *ssp, int flags, struct smb_cred *scred) 785 { 786 return smb_co_get(SSTOCP(ssp), flags, scred); 787 } 788 789 void 790 smb_share_put(struct smb_share *ssp, struct smb_cred *scred) 791 { 792 smb_co_put(SSTOCP(ssp), scred); 793 } 794 795 int 796 smb_share_lock(struct smb_share *ssp, int flags) 797 { 798 return smb_co_lock(SSTOCP(ssp), flags); 799 } 800 801 void 802 smb_share_unlock(struct smb_share *ssp, int flags) 803 { 804 smb_co_unlock(SSTOCP(ssp), flags); 805 } 806 807 int 808 smb_share_access(struct smb_share *ssp, struct smb_cred *scred, mode_t mode) 809 { 810 struct ucred *cred = scred->scr_cred; 811 812 if (smb_suser(cred) == 0 || cred->cr_uid == ssp->ss_uid) 813 return 0; 814 mode >>= 3; 815 if (!groupmember(ssp->ss_grp, cred)) 816 mode >>= 3; 817 return (ssp->ss_mode & mode) == mode ? 0 : EACCES; 818 } 819 820 void 821 smb_share_invalidate(struct smb_share *ssp) 822 { 823 ssp->ss_tid = SMB_TID_UNKNOWN; 824 } 825 826 int 827 smb_share_valid(struct smb_share *ssp) 828 { 829 return ssp->ss_tid != SMB_TID_UNKNOWN && 830 ssp->ss_vcgenid == SSTOVC(ssp)->vc_genid; 831 } 832 833 const char* 834 smb_share_getpass(struct smb_share *ssp) 835 { 836 struct smb_vc *vcp; 837 838 if (ssp->ss_pass) 839 return ssp->ss_pass; 840 vcp = SSTOVC(ssp); 841 if (vcp->vc_pass) 842 return vcp->vc_pass; 843 return smb_emptypass; 844 } 845 846 static int 847 smb_share_getinfo(struct smb_share *ssp, struct smb_share_info *sip) 848 { 849 bzero(sip, sizeof(struct smb_share_info)); 850 sip->itype = SMB_INFO_SHARE; 851 sip->usecount = ssp->obj.co_usecount; 852 sip->tid = ssp->ss_tid; 853 sip->type= ssp->ss_type; 854 sip->uid = ssp->ss_uid; 855 sip->gid = ssp->ss_grp; 856 sip->mode= ssp->ss_mode; 857 sip->flags = ssp->obj.co_flags; 858 ksnprintf(sip->sname, sizeof(sip->sname), "%s", ssp->ss_name); 859 return 0; 860 } 861 862 /* 863 * Dump an entire tree into sysctl call 864 */ 865 static int 866 smb_sysctl_treedump(SYSCTL_HANDLER_ARGS) 867 { 868 struct thread *td = req->td; 869 struct ucred *ucred; 870 struct smb_cred scred; 871 struct smb_connobj *scp1, *scp2; 872 struct smb_vc *vcp; 873 struct smb_share *ssp; 874 struct smb_vc_info vci; 875 struct smb_share_info ssi; 876 int error, itype; 877 878 KKASSERT(td->td_proc); 879 ucred = td->td_proc->p_ucred; 880 881 smb_makescred(&scred, td, ucred); 882 error = smb_sm_lockvclist(LK_SHARED); 883 if (error) 884 return error; 885 SMBCO_FOREACH(scp1, &smb_vclist) { 886 vcp = (struct smb_vc *)scp1; 887 error = smb_vc_lock(vcp, LK_SHARED); 888 if (error) 889 continue; 890 smb_vc_getinfo(vcp, &vci); 891 error = SYSCTL_OUT(req, &vci, sizeof(struct smb_vc_info)); 892 if (error) { 893 smb_vc_unlock(vcp, 0); 894 break; 895 } 896 SMBCO_FOREACH(scp2, VCTOCP(vcp)) { 897 ssp = (struct smb_share *)scp2; 898 error = smb_share_lock(ssp, LK_SHARED); 899 if (error) { 900 error = 0; 901 continue; 902 } 903 smb_share_getinfo(ssp, &ssi); 904 smb_share_unlock(ssp, 0); 905 error = SYSCTL_OUT(req, &ssi, sizeof(struct smb_share_info)); 906 if (error) 907 break; 908 } 909 smb_vc_unlock(vcp, 0); 910 if (error) 911 break; 912 } 913 if (!error) { 914 itype = SMB_INFO_NONE; 915 error = SYSCTL_OUT(req, &itype, sizeof(itype)); 916 } 917 smb_sm_unlockvclist(); 918 return error; 919 } 920