smb_usr.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. #include <sys/param.h>
  29. #include <sys/malloc.h>
  30. #include <sys/kernel.h>
  31. #include <sys/systm.h>
  32. #include <sys/conf.h>
  33. #include <sys/proc.h>
  34. #include <sys/fcntl.h>
  35. #include <sys/socket.h>
  36. #include <sys/socketvar.h>
  37. #include <sys/sysctl.h>
  38. #include <sys/mbuf.h>
  39. #include <sys/iconv.h>
  40. #include <netsmb/smb.h>
  41. #include <netsmb/smb_conn.h>
  42. #include <netsmb/smb_rq.h>
  43. #include <netsmb/smb_subr.h>
  44. #include <netsmb/smb_dev.h>
  45. /*
  46. * helpers for nsmb device. Can be moved to the smb_dev.c file.
  47. */
  48. static void smb_usr_vcspec_free(struct smb_vcspec *spec);
  49. static int
  50. smb_usr_vc2spec(struct smbioc_ossn *dp, struct smb_vcspec *spec)
  51. {
  52. int flags = 0;
  53. bzero(spec, sizeof(*spec));
  54. #ifdef NETSMB_NO_ANON_USER
  55. if (dp->ioc_user[0] == 0)
  56. return EINVAL;
  57. #endif
  58. if (dp->ioc_server == NULL)
  59. return EINVAL;
  60. if (dp->ioc_localcs[0] == 0) {
  61. SMBERROR("no local charset ?\n");
  62. return EINVAL;
  63. }
  64. spec->sap = smb_memdupin(dp->ioc_server, dp->ioc_svlen);
  65. if (spec->sap == NULL)
  66. return ENOMEM;
  67. if (dp->ioc_local) {
  68. spec->lap = smb_memdupin(dp->ioc_local, dp->ioc_lolen);
  69. if (spec->lap == NULL) {
  70. smb_usr_vcspec_free(spec);
  71. return ENOMEM;
  72. }
  73. }
  74. spec->srvname = dp->ioc_srvname;
  75. spec->pass = dp->ioc_password;
  76. spec->domain = dp->ioc_workgroup;
  77. spec->username = dp->ioc_user;
  78. spec->mode = dp->ioc_mode;
  79. spec->rights = dp->ioc_rights;
  80. spec->owner = dp->ioc_owner;
  81. spec->group = dp->ioc_group;
  82. spec->localcs = dp->ioc_localcs;
  83. spec->servercs = dp->ioc_servercs;
  84. if (dp->ioc_opt & SMBVOPT_PRIVATE)
  85. flags |= SMBV_PRIVATE;
  86. if (dp->ioc_opt & SMBVOPT_SINGLESHARE)
  87. flags |= SMBV_PRIVATE | SMBV_SINGLESHARE;
  88. spec->flags = flags;
  89. return 0;
  90. }
  91. static void
  92. smb_usr_vcspec_free(struct smb_vcspec *spec)
  93. {
  94. if (spec->sap)
  95. smb_memfree(spec->sap);
  96. if (spec->lap)
  97. smb_memfree(spec->lap);
  98. }
  99. static int
  100. smb_usr_share2spec(struct smbioc_oshare *dp, struct smb_sharespec *spec)
  101. {
  102. bzero(spec, sizeof(*spec));
  103. spec->mode = dp->ioc_mode;
  104. spec->rights = dp->ioc_rights;
  105. spec->owner = dp->ioc_owner;
  106. spec->group = dp->ioc_group;
  107. spec->name = dp->ioc_share;
  108. spec->stype = dp->ioc_stype;
  109. spec->pass = dp->ioc_password;
  110. return 0;
  111. }
  112. int
  113. smb_usr_lookup(struct smbioc_lookup *dp, struct smb_cred *scred,
  114. struct smb_vc **vcpp, struct smb_share **sspp)
  115. {
  116. struct smb_vc *vcp = NULL;
  117. struct smb_vcspec vspec; /* XXX */
  118. struct smb_sharespec sspec, *sspecp = NULL; /* XXX */
  119. int error;
  120. if (dp->ioc_level < SMBL_VC || dp->ioc_level > SMBL_SHARE)
  121. return EINVAL;
  122. error = smb_usr_vc2spec(&dp->ioc_ssn, &vspec);
  123. if (error)
  124. return error;
  125. if (dp->ioc_flags & SMBLK_CREATE)
  126. vspec.flags |= SMBV_CREATE;
  127. if (dp->ioc_level >= SMBL_SHARE) {
  128. error = smb_usr_share2spec(&dp->ioc_sh, &sspec);
  129. if (error)
  130. goto out;
  131. sspecp = &sspec;
  132. }
  133. error = smb_sm_lookup(&vspec, sspecp, scred, &vcp);
  134. if (error == 0) {
  135. *vcpp = vcp;
  136. *sspp = vspec.ssp;
  137. }
  138. out:
  139. smb_usr_vcspec_free(&vspec);
  140. return error;
  141. }
  142. /*
  143. * Connect to the resource specified by smbioc_ossn structure.
  144. * It may either find an existing connection or try to establish a new one.
  145. * If no errors occurred smb_vc returned locked and referenced.
  146. */
  147. int
  148. smb_usr_opensession(struct smbioc_ossn *dp, struct smb_cred *scred,
  149. struct smb_vc **vcpp)
  150. {
  151. struct smb_vc *vcp = NULL;
  152. struct smb_vcspec vspec;
  153. int error;
  154. error = smb_usr_vc2spec(dp, &vspec);
  155. if (error)
  156. return error;
  157. if (dp->ioc_opt & SMBVOPT_CREATE)
  158. vspec.flags |= SMBV_CREATE;
  159. error = smb_sm_lookup(&vspec, NULL, scred, &vcp);
  160. smb_usr_vcspec_free(&vspec);
  161. return error;
  162. }
  163. int
  164. smb_usr_openshare(struct smb_vc *vcp, struct smbioc_oshare *dp,
  165. struct smb_cred *scred, struct smb_share **sspp)
  166. {
  167. struct smb_share *ssp;
  168. struct smb_sharespec shspec;
  169. int error;
  170. error = smb_usr_share2spec(dp, &shspec);
  171. if (error)
  172. return error;
  173. error = smb_vc_lookupshare(vcp, &shspec, scred, &ssp);
  174. if (error == 0) {
  175. *sspp = ssp;
  176. return 0;
  177. }
  178. if ((dp->ioc_opt & SMBSOPT_CREATE) == 0)
  179. return error;
  180. error = smb_share_create(vcp, &shspec, scred, &ssp);
  181. if (error)
  182. return error;
  183. error = smb_smb_treeconnect(ssp, scred);
  184. if (error) {
  185. smb_share_put(ssp, scred);
  186. } else
  187. *sspp = ssp;
  188. return error;
  189. }
  190. int
  191. smb_usr_simplerequest(struct smb_share *ssp, struct smbioc_rq *dp,
  192. struct smb_cred *scred)
  193. {
  194. struct smb_rq *rqp;
  195. struct mbchain *mbp;
  196. struct mdchain *mdp;
  197. u_int8_t wc;
  198. u_int16_t bc;
  199. int error;
  200. switch (dp->ioc_cmd) {
  201. case SMB_COM_TRANSACTION2:
  202. case SMB_COM_TRANSACTION2_SECONDARY:
  203. case SMB_COM_CLOSE_AND_TREE_DISC:
  204. case SMB_COM_TREE_CONNECT:
  205. case SMB_COM_TREE_DISCONNECT:
  206. case SMB_COM_NEGOTIATE:
  207. case SMB_COM_SESSION_SETUP_ANDX:
  208. case SMB_COM_LOGOFF_ANDX:
  209. case SMB_COM_TREE_CONNECT_ANDX:
  210. return EPERM;
  211. }
  212. rqp = malloc(sizeof(struct smb_rq), M_SMBTEMP, M_WAITOK);
  213. error = smb_rq_init(rqp, SSTOCP(ssp), dp->ioc_cmd, scred);
  214. if (error) {
  215. free(rqp, M_SMBTEMP);
  216. return error;
  217. }
  218. mbp = &rqp->sr_rq;
  219. smb_rq_wstart(rqp);
  220. error = mb_put_mem(mbp, dp->ioc_twords, dp->ioc_twc * 2, MB_MUSER);
  221. if (error)
  222. goto bad;
  223. smb_rq_wend(rqp);
  224. smb_rq_bstart(rqp);
  225. error = mb_put_mem(mbp, dp->ioc_tbytes, dp->ioc_tbc, MB_MUSER);
  226. if (error)
  227. goto bad;
  228. smb_rq_bend(rqp);
  229. error = smb_rq_simple(rqp);
  230. if (error)
  231. goto bad;
  232. mdp = &rqp->sr_rp;
  233. md_get_uint8(mdp, &wc);
  234. dp->ioc_rwc = wc;
  235. wc *= 2;
  236. if (wc > dp->ioc_rpbufsz) {
  237. error = EBADRPC;
  238. goto bad;
  239. }
  240. error = md_get_mem(mdp, dp->ioc_rpbuf, wc, MB_MUSER);
  241. if (error)
  242. goto bad;
  243. md_get_uint16le(mdp, &bc);
  244. if ((wc + bc) > dp->ioc_rpbufsz) {
  245. error = EBADRPC;
  246. goto bad;
  247. }
  248. dp->ioc_rbc = bc;
  249. error = md_get_mem(mdp, dp->ioc_rpbuf + wc, bc, MB_MUSER);
  250. bad:
  251. dp->ioc_errclass = rqp->sr_errclass;
  252. dp->ioc_serror = rqp->sr_serror;
  253. dp->ioc_error = rqp->sr_error;
  254. smb_rq_done(rqp);
  255. free(rqp, M_SMBTEMP);
  256. return error;
  257. }
  258. static int
  259. smb_cpdatain(struct mbchain *mbp, int len, caddr_t data)
  260. {
  261. int error;
  262. if (len == 0)
  263. return 0;
  264. error = mb_init(mbp);
  265. if (error)
  266. return error;
  267. return mb_put_mem(mbp, data, len, MB_MUSER);
  268. }
  269. int
  270. smb_usr_t2request(struct smb_share *ssp, struct smbioc_t2rq *dp,
  271. struct smb_cred *scred)
  272. {
  273. struct smb_t2rq *t2p;
  274. struct mdchain *mdp;
  275. int error, len;
  276. if (dp->ioc_setupcnt > 3)
  277. return EINVAL;
  278. t2p = malloc(sizeof(struct smb_t2rq), M_SMBTEMP, M_WAITOK);
  279. error = smb_t2_init(t2p, SSTOCP(ssp), dp->ioc_setup[0], scred);
  280. if (error) {
  281. free(t2p, M_SMBTEMP);
  282. return error;
  283. }
  284. len = t2p->t2_setupcount = dp->ioc_setupcnt;
  285. if (len > 1)
  286. t2p->t2_setupdata = dp->ioc_setup;
  287. if (dp->ioc_name) {
  288. t2p->t_name = smb_strdupin(dp->ioc_name, 128);
  289. if (t2p->t_name == NULL) {
  290. error = ENOMEM;
  291. goto bad;
  292. }
  293. }
  294. t2p->t2_maxscount = 0;
  295. t2p->t2_maxpcount = dp->ioc_rparamcnt;
  296. t2p->t2_maxdcount = dp->ioc_rdatacnt;
  297. error = smb_cpdatain(&t2p->t2_tparam, dp->ioc_tparamcnt, dp->ioc_tparam);
  298. if (error)
  299. goto bad;
  300. error = smb_cpdatain(&t2p->t2_tdata, dp->ioc_tdatacnt, dp->ioc_tdata);
  301. if (error)
  302. goto bad;
  303. error = smb_t2_request(t2p);
  304. if (error)
  305. goto bad;
  306. mdp = &t2p->t2_rparam;
  307. if (mdp->md_top) {
  308. len = m_fixhdr(mdp->md_top);
  309. if (len > dp->ioc_rparamcnt) {
  310. error = EMSGSIZE;
  311. goto bad;
  312. }
  313. dp->ioc_rparamcnt = len;
  314. error = md_get_mem(mdp, dp->ioc_rparam, len, MB_MUSER);
  315. if (error)
  316. goto bad;
  317. } else
  318. dp->ioc_rparamcnt = 0;
  319. mdp = &t2p->t2_rdata;
  320. if (mdp->md_top) {
  321. len = m_fixhdr(mdp->md_top);
  322. if (len > dp->ioc_rdatacnt) {
  323. error = EMSGSIZE;
  324. goto bad;
  325. }
  326. dp->ioc_rdatacnt = len;
  327. error = md_get_mem(mdp, dp->ioc_rdata, len, MB_MUSER);
  328. } else
  329. dp->ioc_rdatacnt = 0;
  330. bad:
  331. if (t2p->t_name)
  332. smb_strfree(t2p->t_name);
  333. smb_t2_done(t2p);
  334. free(t2p, M_SMBTEMP);
  335. return error;
  336. }