smb_conn.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2000-2001 Boris Popov
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. /*
  29. * Connection engine.
  30. */
  31. #include <sys/param.h>
  32. #include <sys/systm.h>
  33. #include <sys/kernel.h>
  34. #include <sys/malloc.h>
  35. #include <sys/priv.h>
  36. #include <sys/proc.h>
  37. #include <sys/lock.h>
  38. #include <sys/sysctl.h>
  39. #include <sys/socketvar.h>
  40. #include <sys/iconv.h>
  41. #include <netsmb/smb.h>
  42. #include <netsmb/smb_subr.h>
  43. #include <netsmb/smb_conn.h>
  44. #include <netsmb/smb_tran.h>
  45. #include <netsmb/smb_trantcp.h>
  46. static struct smb_connobj smb_vclist;
  47. static int smb_vcnext = 1; /* next unique id for VC */
  48. SYSCTL_NODE(_net, OID_AUTO, smb, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
  49. "SMB protocol");
  50. static MALLOC_DEFINE(M_SMBCONN, "smb_conn", "SMB connection");
  51. static void smb_co_init(struct smb_connobj *cp, int level, char *ilockname,
  52. char *lockname);
  53. static void smb_co_done(struct smb_connobj *cp);
  54. static int smb_vc_disconnect(struct smb_vc *vcp);
  55. static void smb_vc_free(struct smb_connobj *cp);
  56. static void smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred);
  57. static smb_co_free_t smb_share_free;
  58. static smb_co_gone_t smb_share_gone;
  59. static int smb_sysctl_treedump(SYSCTL_HANDLER_ARGS);
  60. SYSCTL_PROC(_net_smb, OID_AUTO, treedump,
  61. CTLFLAG_RD | CTLTYPE_OPAQUE | CTLFLAG_MPSAFE,
  62. NULL, 0, smb_sysctl_treedump, "S,treedump",
  63. "Requester tree");
  64. int
  65. smb_sm_init(void)
  66. {
  67. smb_co_init(&smb_vclist, SMBL_SM, "smbsm ilock", "smbsm");
  68. sx_xlock(&smb_vclist.co_interlock);
  69. smb_co_unlock(&smb_vclist);
  70. sx_unlock(&smb_vclist.co_interlock);
  71. return 0;
  72. }
  73. int
  74. smb_sm_done(void)
  75. {
  76. /* XXX: hold the mutex */
  77. if (smb_vclist.co_usecount > 1) {
  78. SMBERROR("%d connections still active\n", smb_vclist.co_usecount - 1);
  79. return EBUSY;
  80. }
  81. smb_co_done(&smb_vclist);
  82. return 0;
  83. }
  84. static int
  85. smb_sm_lockvclist(void)
  86. {
  87. int error;
  88. sx_xlock(&smb_vclist.co_interlock);
  89. error = smb_co_lock(&smb_vclist);
  90. sx_unlock(&smb_vclist.co_interlock);
  91. return error;
  92. }
  93. static void
  94. smb_sm_unlockvclist(void)
  95. {
  96. sx_xlock(&smb_vclist.co_interlock);
  97. smb_co_unlock(&smb_vclist);
  98. sx_unlock(&smb_vclist.co_interlock);
  99. }
  100. static int
  101. smb_sm_lookupint(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
  102. struct smb_cred *scred, struct smb_vc **vcpp)
  103. {
  104. struct smb_connobj *scp;
  105. struct smb_vc *vcp;
  106. int exact = 1;
  107. int error;
  108. vcspec->shspec = shspec;
  109. error = ENOENT;
  110. vcp = NULL;
  111. SMBCO_FOREACH(scp, &smb_vclist) {
  112. vcp = (struct smb_vc *)scp;
  113. error = smb_vc_lock(vcp);
  114. if (error)
  115. continue;
  116. if ((vcp->obj.co_flags & SMBV_PRIVATE) ||
  117. !CONNADDREQ(vcp->vc_paddr, vcspec->sap) ||
  118. strcmp(vcp->vc_username, vcspec->username) != 0)
  119. goto err1;
  120. if (vcspec->owner != SMBM_ANY_OWNER) {
  121. if (vcp->vc_uid != vcspec->owner)
  122. goto err1;
  123. } else
  124. exact = 0;
  125. if (vcspec->group != SMBM_ANY_GROUP) {
  126. if (vcp->vc_grp != vcspec->group)
  127. goto err1;
  128. } else
  129. exact = 0;
  130. if (vcspec->mode & SMBM_EXACT) {
  131. if (!exact || (vcspec->mode & SMBM_MASK) !=
  132. vcp->vc_mode)
  133. goto err1;
  134. }
  135. if (smb_vc_access(vcp, scred, vcspec->mode) != 0)
  136. goto err1;
  137. vcspec->ssp = NULL;
  138. if (shspec) {
  139. error = (int)smb_vc_lookupshare(vcp, shspec, scred,
  140. &vcspec->ssp);
  141. if (error)
  142. goto fail;
  143. }
  144. error = 0;
  145. break;
  146. err1:
  147. error = 1;
  148. fail:
  149. smb_vc_unlock(vcp);
  150. }
  151. if (vcp) {
  152. smb_vc_ref(vcp);
  153. *vcpp = vcp;
  154. }
  155. return (error);
  156. }
  157. int
  158. smb_sm_lookup(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
  159. struct smb_cred *scred, struct smb_vc **vcpp)
  160. {
  161. struct smb_vc *vcp;
  162. struct smb_share *ssp = NULL;
  163. int error;
  164. *vcpp = vcp = NULL;
  165. error = smb_sm_lockvclist();
  166. if (error)
  167. return error;
  168. error = smb_sm_lookupint(vcspec, shspec, scred, vcpp);
  169. if (error == 0 || (vcspec->flags & SMBV_CREATE) == 0) {
  170. smb_sm_unlockvclist();
  171. return error;
  172. }
  173. error = smb_sm_lookupint(vcspec, NULL, scred, &vcp);
  174. if (error) {
  175. error = smb_vc_create(vcspec, scred, &vcp);
  176. if (error)
  177. goto out;
  178. error = smb_vc_connect(vcp, scred);
  179. if (error)
  180. goto out;
  181. }
  182. if (shspec == NULL)
  183. goto out;
  184. error = smb_share_create(vcp, shspec, scred, &ssp);
  185. if (error)
  186. goto out;
  187. error = smb_smb_treeconnect(ssp, scred);
  188. if (error == 0)
  189. vcspec->ssp = ssp;
  190. else
  191. smb_share_put(ssp, scred);
  192. out:
  193. smb_sm_unlockvclist();
  194. if (error == 0)
  195. *vcpp = vcp;
  196. else if (vcp) {
  197. smb_vc_lock(vcp);
  198. smb_vc_put(vcp, scred);
  199. }
  200. return error;
  201. }
  202. /*
  203. * Common code for connection object
  204. */
  205. static void
  206. smb_co_init(struct smb_connobj *cp, int level, char *ilockname, char *lockname)
  207. {
  208. SLIST_INIT(&cp->co_children);
  209. sx_init_flags(&cp->co_interlock, ilockname, SX_RECURSE);
  210. cv_init(&cp->co_lock, "smblock");
  211. cp->co_lockcnt = 0;
  212. cp->co_locker = NULL;
  213. cp->co_level = level;
  214. cp->co_usecount = 1;
  215. sx_xlock(&cp->co_interlock);
  216. smb_co_lock(cp);
  217. sx_unlock(&cp->co_interlock);
  218. }
  219. static void
  220. smb_co_done(struct smb_connobj *cp)
  221. {
  222. sx_destroy(&cp->co_interlock);
  223. cv_destroy(&cp->co_lock);
  224. cp->co_locker = NULL;
  225. cp->co_flags = 0;
  226. cp->co_lockcnt = 0;
  227. }
  228. static void
  229. smb_co_gone(struct smb_connobj *cp, struct smb_cred *scred)
  230. {
  231. struct smb_connobj *parent;
  232. if (cp->co_gone)
  233. cp->co_gone(cp, scred);
  234. parent = cp->co_parent;
  235. if (parent) {
  236. sx_xlock(&parent->co_interlock);
  237. smb_co_lock(parent);
  238. sx_unlock(&parent->co_interlock);
  239. SLIST_REMOVE(&parent->co_children, cp, smb_connobj, co_next);
  240. smb_co_put(parent, scred);
  241. }
  242. if (cp->co_free)
  243. cp->co_free(cp);
  244. }
  245. void
  246. smb_co_ref(struct smb_connobj *cp)
  247. {
  248. sx_xlock(&cp->co_interlock);
  249. cp->co_usecount++;
  250. sx_unlock(&cp->co_interlock);
  251. }
  252. void
  253. smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred)
  254. {
  255. sx_xlock(&cp->co_interlock);
  256. smb_co_unlock(cp);
  257. if (cp->co_usecount > 1) {
  258. cp->co_usecount--;
  259. sx_unlock(&cp->co_interlock);
  260. return;
  261. }
  262. if (cp->co_usecount == 0) {
  263. SMBERROR("negative use_count for object %d", cp->co_level);
  264. sx_unlock(&cp->co_interlock);
  265. return;
  266. }
  267. cp->co_usecount--;
  268. cp->co_flags |= SMBO_GONE;
  269. sx_unlock(&cp->co_interlock);
  270. smb_co_gone(cp, scred);
  271. }
  272. int
  273. smb_co_get(struct smb_connobj *cp, struct smb_cred *scred)
  274. {
  275. int error;
  276. MPASS(sx_xholder(&cp->co_interlock) == curthread);
  277. cp->co_usecount++;
  278. error = smb_co_lock(cp);
  279. if (error)
  280. cp->co_usecount--;
  281. return error;
  282. }
  283. void
  284. smb_co_put(struct smb_connobj *cp, struct smb_cred *scred)
  285. {
  286. sx_xlock(&cp->co_interlock);
  287. if (cp->co_usecount > 1) {
  288. cp->co_usecount--;
  289. } else if (cp->co_usecount == 1) {
  290. cp->co_usecount--;
  291. cp->co_flags |= SMBO_GONE;
  292. } else {
  293. SMBERROR("negative usecount");
  294. }
  295. smb_co_unlock(cp);
  296. sx_unlock(&cp->co_interlock);
  297. if ((cp->co_flags & SMBO_GONE) == 0)
  298. return;
  299. smb_co_gone(cp, scred);
  300. }
  301. int
  302. smb_co_lock(struct smb_connobj *cp)
  303. {
  304. MPASS(sx_xholder(&cp->co_interlock) == curthread);
  305. for (;;) {
  306. if (cp->co_flags & SMBO_GONE)
  307. return EINVAL;
  308. if (cp->co_locker == NULL) {
  309. cp->co_locker = curthread;
  310. return 0;
  311. }
  312. if (cp->co_locker == curthread) {
  313. cp->co_lockcnt++;
  314. return 0;
  315. }
  316. cv_wait(&cp->co_lock, &cp->co_interlock);
  317. }
  318. }
  319. void
  320. smb_co_unlock(struct smb_connobj *cp)
  321. {
  322. MPASS(sx_xholder(&cp->co_interlock) == curthread);
  323. MPASS(cp->co_locker == curthread);
  324. if (cp->co_lockcnt != 0) {
  325. cp->co_lockcnt--;
  326. return;
  327. }
  328. cp->co_locker = NULL;
  329. cv_signal(&cp->co_lock);
  330. }
  331. static void
  332. smb_co_addchild(struct smb_connobj *parent, struct smb_connobj *child)
  333. {
  334. smb_co_ref(parent);
  335. SLIST_INSERT_HEAD(&parent->co_children, child, co_next);
  336. child->co_parent = parent;
  337. }
  338. /*
  339. * Session implementation
  340. */
  341. int
  342. smb_vc_create(struct smb_vcspec *vcspec,
  343. struct smb_cred *scred, struct smb_vc **vcpp)
  344. {
  345. struct smb_vc *vcp;
  346. struct ucred *cred = scred->scr_cred;
  347. uid_t uid = vcspec->owner;
  348. gid_t gid = vcspec->group;
  349. uid_t realuid = cred->cr_uid;
  350. char *domain = vcspec->domain;
  351. int error, isroot;
  352. isroot = smb_suser(cred) == 0;
  353. /*
  354. * Only superuser can create VCs with different uid and gid
  355. */
  356. if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot)
  357. return EPERM;
  358. if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot)
  359. return EPERM;
  360. vcp = smb_zmalloc(sizeof(*vcp), M_SMBCONN, M_WAITOK);
  361. smb_co_init(VCTOCP(vcp), SMBL_VC, "smb_vc ilock", "smb_vc");
  362. vcp->obj.co_free = smb_vc_free;
  363. vcp->obj.co_gone = smb_vc_gone;
  364. vcp->vc_number = smb_vcnext++;
  365. vcp->vc_timo = SMB_DEFRQTIMO;
  366. vcp->vc_smbuid = SMB_UID_UNKNOWN;
  367. vcp->vc_mode = vcspec->rights & SMBM_MASK;
  368. vcp->obj.co_flags = vcspec->flags & (SMBV_PRIVATE | SMBV_SINGLESHARE);
  369. vcp->vc_tdesc = &smb_tran_nbtcp_desc;
  370. vcp->vc_seqno = 0;
  371. vcp->vc_mackey = NULL;
  372. vcp->vc_mackeylen = 0;
  373. if (uid == SMBM_ANY_OWNER)
  374. uid = realuid;
  375. if (gid == SMBM_ANY_GROUP)
  376. gid = cred->cr_groups[0];
  377. vcp->vc_uid = uid;
  378. vcp->vc_grp = gid;
  379. smb_sl_init(&vcp->vc_stlock, "vcstlock");
  380. error = ENOMEM;
  381. vcp->vc_paddr = sodupsockaddr(vcspec->sap, M_WAITOK);
  382. if (vcp->vc_paddr == NULL)
  383. goto fail;
  384. vcp->vc_laddr = sodupsockaddr(vcspec->lap, M_WAITOK);
  385. if (vcp->vc_laddr == NULL)
  386. goto fail;
  387. vcp->vc_pass = smb_strdup(vcspec->pass);
  388. if (vcp->vc_pass == NULL)
  389. goto fail;
  390. vcp->vc_domain = smb_strdup((domain && domain[0]) ? domain :
  391. "NODOMAIN");
  392. if (vcp->vc_domain == NULL)
  393. goto fail;
  394. vcp->vc_srvname = smb_strdup(vcspec->srvname);
  395. if (vcp->vc_srvname == NULL)
  396. goto fail;
  397. vcp->vc_username = smb_strdup(vcspec->username);
  398. if (vcp->vc_username == NULL)
  399. goto fail;
  400. error = (int)iconv_open("tolower", vcspec->localcs, &vcp->vc_tolower);
  401. if (error)
  402. goto fail;
  403. error = (int)iconv_open("toupper", vcspec->localcs, &vcp->vc_toupper);
  404. if (error)
  405. goto fail;
  406. if (vcspec->servercs[0]) {
  407. error = (int)iconv_open(vcspec->servercs, vcspec->localcs,
  408. &vcp->vc_cp_toserver);
  409. if (error)
  410. goto fail;
  411. error = (int)iconv_open(vcspec->localcs, vcspec->servercs,
  412. &vcp->vc_cp_tolocal);
  413. if (error)
  414. goto fail;
  415. vcp->vc_toserver = vcp->vc_cp_toserver;
  416. vcp->vc_tolocal = vcp->vc_cp_tolocal;
  417. iconv_add(ENCODING_UNICODE, ENCODING_UNICODE, SMB_UNICODE_NAME);
  418. iconv_add(ENCODING_UNICODE, SMB_UNICODE_NAME, ENCODING_UNICODE);
  419. error = (int)iconv_open(SMB_UNICODE_NAME, vcspec->localcs,
  420. &vcp->vc_ucs_toserver);
  421. if (!error) {
  422. error = (int)iconv_open(vcspec->localcs, SMB_UNICODE_NAME,
  423. &vcp->vc_ucs_tolocal);
  424. }
  425. if (error) {
  426. if (vcp->vc_ucs_toserver)
  427. iconv_close(vcp->vc_ucs_toserver);
  428. vcp->vc_ucs_toserver = NULL;
  429. vcp->vc_ucs_tolocal = NULL;
  430. }
  431. }
  432. error = (int)smb_iod_create(vcp);
  433. if (error)
  434. goto fail;
  435. *vcpp = vcp;
  436. smb_co_addchild(&smb_vclist, VCTOCP(vcp));
  437. return (0);
  438. fail:
  439. smb_vc_put(vcp, scred);
  440. return (error);
  441. }
  442. static void
  443. smb_vc_free(struct smb_connobj *cp)
  444. {
  445. struct smb_vc *vcp = CPTOVC(cp);
  446. if (vcp->vc_iod)
  447. smb_iod_destroy(vcp->vc_iod);
  448. SMB_STRFREE(vcp->vc_username);
  449. SMB_STRFREE(vcp->vc_srvname);
  450. SMB_STRFREE(vcp->vc_pass);
  451. SMB_STRFREE(vcp->vc_domain);
  452. if (vcp->vc_mackey)
  453. free(vcp->vc_mackey, M_SMBTEMP);
  454. if (vcp->vc_paddr)
  455. free(vcp->vc_paddr, M_SONAME);
  456. if (vcp->vc_laddr)
  457. free(vcp->vc_laddr, M_SONAME);
  458. if (vcp->vc_tolower)
  459. iconv_close(vcp->vc_tolower);
  460. if (vcp->vc_toupper)
  461. iconv_close(vcp->vc_toupper);
  462. if (vcp->vc_tolocal)
  463. vcp->vc_tolocal = NULL;
  464. if (vcp->vc_toserver)
  465. vcp->vc_toserver = NULL;
  466. if (vcp->vc_cp_tolocal)
  467. iconv_close(vcp->vc_cp_tolocal);
  468. if (vcp->vc_cp_toserver)
  469. iconv_close(vcp->vc_cp_toserver);
  470. if (vcp->vc_ucs_tolocal)
  471. iconv_close(vcp->vc_ucs_tolocal);
  472. if (vcp->vc_ucs_toserver)
  473. iconv_close(vcp->vc_ucs_toserver);
  474. smb_co_done(VCTOCP(vcp));
  475. smb_sl_destroy(&vcp->vc_stlock);
  476. free(vcp, M_SMBCONN);
  477. }
  478. /*
  479. * Called when use count of VC dropped to zero.
  480. */
  481. static void
  482. smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred)
  483. {
  484. struct smb_vc *vcp = CPTOVC(cp);
  485. smb_vc_disconnect(vcp);
  486. }
  487. void
  488. smb_vc_ref(struct smb_vc *vcp)
  489. {
  490. smb_co_ref(VCTOCP(vcp));
  491. }
  492. void
  493. smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred)
  494. {
  495. smb_co_rele(VCTOCP(vcp), scred);
  496. }
  497. int
  498. smb_vc_get(struct smb_vc *vcp, struct smb_cred *scred)
  499. {
  500. struct smb_connobj *cp;
  501. int error;
  502. cp = VCTOCP(vcp);
  503. sx_xlock(&cp->co_interlock);
  504. error = smb_co_get(cp, scred);
  505. sx_unlock(&cp->co_interlock);
  506. return error;
  507. }
  508. void
  509. smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred)
  510. {
  511. smb_co_put(VCTOCP(vcp), scred);
  512. }
  513. int
  514. smb_vc_lock(struct smb_vc *vcp)
  515. {
  516. struct smb_connobj *cp;
  517. int error;
  518. cp = VCTOCP(vcp);
  519. sx_xlock(&cp->co_interlock);
  520. error = smb_co_lock(cp);
  521. sx_unlock(&cp->co_interlock);
  522. return error;
  523. }
  524. void
  525. smb_vc_unlock(struct smb_vc *vcp)
  526. {
  527. struct smb_connobj *cp;
  528. cp = VCTOCP(vcp);
  529. sx_xlock(&cp->co_interlock);
  530. smb_co_unlock(cp);
  531. sx_unlock(&cp->co_interlock);
  532. }
  533. int
  534. smb_vc_access(struct smb_vc *vcp, struct smb_cred *scred, mode_t mode)
  535. {
  536. struct ucred *cred = scred->scr_cred;
  537. if (smb_suser(cred) == 0 || cred->cr_uid == vcp->vc_uid)
  538. return 0;
  539. mode >>= 3;
  540. if (!groupmember(vcp->vc_grp, cred))
  541. mode >>= 3;
  542. return (vcp->vc_mode & mode) == mode ? 0 : EACCES;
  543. }
  544. static int
  545. smb_vc_cmpshare(struct smb_share *ssp, struct smb_sharespec *dp)
  546. {
  547. int exact = 1;
  548. if (strcmp(ssp->ss_name, dp->name) != 0)
  549. return 1;
  550. if (dp->owner != SMBM_ANY_OWNER) {
  551. if (ssp->ss_uid != dp->owner)
  552. return 1;
  553. } else
  554. exact = 0;
  555. if (dp->group != SMBM_ANY_GROUP) {
  556. if (ssp->ss_grp != dp->group)
  557. return 1;
  558. } else
  559. exact = 0;
  560. if (dp->mode & SMBM_EXACT) {
  561. if (!exact)
  562. return 1;
  563. return (dp->mode & SMBM_MASK) == ssp->ss_mode ? 0 : 1;
  564. }
  565. if (smb_share_access(ssp, dp->scred, dp->mode) != 0)
  566. return 1;
  567. return 0;
  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. */
  573. int
  574. smb_vc_lookupshare(struct smb_vc *vcp, struct smb_sharespec *dp,
  575. struct smb_cred *scred, struct smb_share **sspp)
  576. {
  577. struct smb_connobj *scp = NULL;
  578. struct smb_share *ssp = NULL;
  579. int error;
  580. *sspp = NULL;
  581. dp->scred = scred;
  582. SMBCO_FOREACH(scp, VCTOCP(vcp)) {
  583. ssp = (struct smb_share *)scp;
  584. error = smb_share_lock(ssp);
  585. if (error)
  586. continue;
  587. if (smb_vc_cmpshare(ssp, dp) == 0)
  588. break;
  589. smb_share_unlock(ssp);
  590. }
  591. if (ssp) {
  592. smb_share_ref(ssp);
  593. *sspp = ssp;
  594. error = 0;
  595. } else
  596. error = ENOENT;
  597. return error;
  598. }
  599. int
  600. smb_vc_connect(struct smb_vc *vcp, struct smb_cred *scred)
  601. {
  602. return smb_iod_request(vcp->vc_iod, SMBIOD_EV_CONNECT | SMBIOD_EV_SYNC, NULL);
  603. }
  604. /*
  605. * Destroy VC to server, invalidate shares linked with it.
  606. * Transport should be locked on entry.
  607. */
  608. int
  609. smb_vc_disconnect(struct smb_vc *vcp)
  610. {
  611. if (vcp->vc_iod != NULL)
  612. smb_iod_request(vcp->vc_iod, SMBIOD_EV_DISCONNECT |
  613. SMBIOD_EV_SYNC, NULL);
  614. return 0;
  615. }
  616. static char smb_emptypass[] = "";
  617. const char *
  618. smb_vc_getpass(struct smb_vc *vcp)
  619. {
  620. if (vcp->vc_pass)
  621. return vcp->vc_pass;
  622. return smb_emptypass;
  623. }
  624. static int
  625. smb_vc_getinfo(struct smb_vc *vcp, struct smb_vc_info *vip)
  626. {
  627. bzero(vip, sizeof(struct smb_vc_info));
  628. vip->itype = SMB_INFO_VC;
  629. vip->usecount = vcp->obj.co_usecount;
  630. vip->uid = vcp->vc_uid;
  631. vip->gid = vcp->vc_grp;
  632. vip->mode = vcp->vc_mode;
  633. vip->flags = vcp->obj.co_flags;
  634. vip->sopt = vcp->vc_sopt;
  635. vip->iodstate = vcp->vc_iod->iod_state;
  636. bzero(&vip->sopt.sv_skey, sizeof(vip->sopt.sv_skey));
  637. snprintf(vip->srvname, sizeof(vip->srvname), "%s", vcp->vc_srvname);
  638. snprintf(vip->vcname, sizeof(vip->vcname), "%s", vcp->vc_username);
  639. return 0;
  640. }
  641. u_short
  642. smb_vc_nextmid(struct smb_vc *vcp)
  643. {
  644. u_short r;
  645. sx_xlock(&vcp->obj.co_interlock);
  646. r = vcp->vc_mid++;
  647. sx_unlock(&vcp->obj.co_interlock);
  648. return r;
  649. }
  650. /*
  651. * Share implementation
  652. */
  653. /*
  654. * Allocate share structure and attach it to the given VC
  655. * Connection expected to be locked on entry. Share will be returned
  656. * in locked state.
  657. */
  658. int
  659. smb_share_create(struct smb_vc *vcp, struct smb_sharespec *shspec,
  660. struct smb_cred *scred, struct smb_share **sspp)
  661. {
  662. struct smb_share *ssp;
  663. struct ucred *cred = scred->scr_cred;
  664. uid_t realuid = cred->cr_uid;
  665. uid_t uid = shspec->owner;
  666. gid_t gid = shspec->group;
  667. int error, isroot;
  668. isroot = smb_suser(cred) == 0;
  669. /*
  670. * Only superuser can create shares with different uid and gid
  671. */
  672. if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot)
  673. return EPERM;
  674. if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot)
  675. return EPERM;
  676. error = smb_vc_lookupshare(vcp, shspec, scred, &ssp);
  677. if (!error) {
  678. smb_share_put(ssp, scred);
  679. return EEXIST;
  680. }
  681. if (uid == SMBM_ANY_OWNER)
  682. uid = realuid;
  683. if (gid == SMBM_ANY_GROUP)
  684. gid = cred->cr_groups[0];
  685. ssp = smb_zmalloc(sizeof(*ssp), M_SMBCONN, M_WAITOK);
  686. smb_co_init(SSTOCP(ssp), SMBL_SHARE, "smbss ilock", "smbss");
  687. ssp->obj.co_free = smb_share_free;
  688. ssp->obj.co_gone = smb_share_gone;
  689. smb_sl_init(&ssp->ss_stlock, "ssstlock");
  690. ssp->ss_name = smb_strdup(shspec->name);
  691. if (shspec->pass && shspec->pass[0])
  692. ssp->ss_pass = smb_strdup(shspec->pass);
  693. ssp->ss_type = shspec->stype;
  694. ssp->ss_tid = SMB_TID_UNKNOWN;
  695. ssp->ss_uid = uid;
  696. ssp->ss_grp = gid;
  697. ssp->ss_mode = shspec->rights & SMBM_MASK;
  698. smb_co_addchild(VCTOCP(vcp), SSTOCP(ssp));
  699. *sspp = ssp;
  700. return 0;
  701. }
  702. static void
  703. smb_share_free(struct smb_connobj *cp)
  704. {
  705. struct smb_share *ssp = CPTOSS(cp);
  706. SMB_STRFREE(ssp->ss_name);
  707. SMB_STRFREE(ssp->ss_pass);
  708. smb_sl_destroy(&ssp->ss_stlock);
  709. smb_co_done(SSTOCP(ssp));
  710. free(ssp, M_SMBCONN);
  711. }
  712. static void
  713. smb_share_gone(struct smb_connobj *cp, struct smb_cred *scred)
  714. {
  715. struct smb_share *ssp = CPTOSS(cp);
  716. smb_smb_treedisconnect(ssp, scred);
  717. }
  718. void
  719. smb_share_ref(struct smb_share *ssp)
  720. {
  721. smb_co_ref(SSTOCP(ssp));
  722. }
  723. void
  724. smb_share_rele(struct smb_share *ssp, struct smb_cred *scred)
  725. {
  726. smb_co_rele(SSTOCP(ssp), scred);
  727. }
  728. int
  729. smb_share_get(struct smb_share *ssp, struct smb_cred *scred)
  730. {
  731. struct smb_connobj *cp = SSTOCP(ssp);
  732. int error;
  733. sx_xlock(&cp->co_interlock);
  734. error = smb_co_get(cp, scred);
  735. sx_unlock(&cp->co_interlock);
  736. return error;
  737. }
  738. void
  739. smb_share_put(struct smb_share *ssp, struct smb_cred *scred)
  740. {
  741. smb_co_put(SSTOCP(ssp), scred);
  742. }
  743. int
  744. smb_share_lock(struct smb_share *ssp)
  745. {
  746. struct smb_connobj *cp;
  747. int error;
  748. cp = SSTOCP(ssp);
  749. sx_xlock(&cp->co_interlock);
  750. error = smb_co_lock(cp);
  751. sx_unlock(&cp->co_interlock);
  752. return error;
  753. }
  754. void
  755. smb_share_unlock(struct smb_share *ssp)
  756. {
  757. struct smb_connobj *cp;
  758. cp = SSTOCP(ssp);
  759. sx_xlock(&cp->co_interlock);
  760. smb_co_unlock(cp);
  761. sx_unlock(&cp->co_interlock);
  762. }
  763. int
  764. smb_share_access(struct smb_share *ssp, struct smb_cred *scred, mode_t mode)
  765. {
  766. struct ucred *cred = scred->scr_cred;
  767. if (smb_suser(cred) == 0 || cred->cr_uid == ssp->ss_uid)
  768. return 0;
  769. mode >>= 3;
  770. if (!groupmember(ssp->ss_grp, cred))
  771. mode >>= 3;
  772. return (ssp->ss_mode & mode) == mode ? 0 : EACCES;
  773. }
  774. void
  775. smb_share_invalidate(struct smb_share *ssp)
  776. {
  777. ssp->ss_tid = SMB_TID_UNKNOWN;
  778. }
  779. int
  780. smb_share_valid(struct smb_share *ssp)
  781. {
  782. return ssp->ss_tid != SMB_TID_UNKNOWN &&
  783. ssp->ss_vcgenid == SSTOVC(ssp)->vc_genid;
  784. }
  785. const char*
  786. smb_share_getpass(struct smb_share *ssp)
  787. {
  788. struct smb_vc *vcp;
  789. if (ssp->ss_pass)
  790. return ssp->ss_pass;
  791. vcp = SSTOVC(ssp);
  792. if (vcp->vc_pass)
  793. return vcp->vc_pass;
  794. return smb_emptypass;
  795. }
  796. static int
  797. smb_share_getinfo(struct smb_share *ssp, struct smb_share_info *sip)
  798. {
  799. bzero(sip, sizeof(struct smb_share_info));
  800. sip->itype = SMB_INFO_SHARE;
  801. sip->usecount = ssp->obj.co_usecount;
  802. sip->tid = ssp->ss_tid;
  803. sip->type= ssp->ss_type;
  804. sip->uid = ssp->ss_uid;
  805. sip->gid = ssp->ss_grp;
  806. sip->mode= ssp->ss_mode;
  807. sip->flags = ssp->obj.co_flags;
  808. snprintf(sip->sname, sizeof(sip->sname), "%s", ssp->ss_name);
  809. return 0;
  810. }
  811. /*
  812. * Dump an entire tree into sysctl call
  813. */
  814. static int
  815. smb_sysctl_treedump(SYSCTL_HANDLER_ARGS)
  816. {
  817. struct smb_connobj *scp1, *scp2;
  818. struct smb_vc *vcp;
  819. struct smb_share *ssp;
  820. struct smb_vc_info vci;
  821. struct smb_share_info ssi;
  822. int error, itype;
  823. error = sysctl_wire_old_buffer(req, 0);
  824. if (error)
  825. return (error);
  826. error = smb_sm_lockvclist();
  827. if (error)
  828. return error;
  829. SMBCO_FOREACH(scp1, &smb_vclist) {
  830. vcp = (struct smb_vc *)scp1;
  831. error = smb_vc_lock(vcp);
  832. if (error)
  833. continue;
  834. smb_vc_getinfo(vcp, &vci);
  835. error = SYSCTL_OUT(req, &vci, sizeof(struct smb_vc_info));
  836. if (error) {
  837. smb_vc_unlock(vcp);
  838. break;
  839. }
  840. SMBCO_FOREACH(scp2, VCTOCP(vcp)) {
  841. ssp = (struct smb_share *)scp2;
  842. error = smb_share_lock(ssp);
  843. if (error) {
  844. error = 0;
  845. continue;
  846. }
  847. smb_share_getinfo(ssp, &ssi);
  848. smb_share_unlock(ssp);
  849. error = SYSCTL_OUT(req, &ssi, sizeof(struct smb_share_info));
  850. if (error)
  851. break;
  852. }
  853. smb_vc_unlock(vcp);
  854. if (error)
  855. break;
  856. }
  857. if (!error) {
  858. itype = SMB_INFO_NONE;
  859. error = SYSCTL_OUT(req, &itype, sizeof(itype));
  860. }
  861. smb_sm_unlockvclist();
  862. return error;
  863. }