smb_conn.c 21 KB

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