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