PageRenderTime 75ms CodeModel.GetById 44ms RepoModel.GetById 0ms app.codeStats 0ms

/freebsd5/sys/netncp/ncp_ncp.c

https://github.com/deathmaker1/kame
C | 495 lines | 405 code | 37 blank | 53 comment | 76 complexity | 0551b15457de0ea7662d4b13bf6aa4db MD5 | raw file
  1. /*
  2. * Copyright (c) 1999, 2000, 2001 Boris Popov
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by Boris Popov.
  16. * 4. Neither the name of the author nor the names of any co-contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * $FreeBSD: src/sys/netncp/ncp_ncp.c,v 1.12 2002/10/11 14:58:30 mike Exp $
  33. *
  34. * Core of NCP protocol
  35. */
  36. #include <sys/param.h>
  37. #include <sys/errno.h>
  38. #include <sys/systm.h>
  39. #include <sys/proc.h>
  40. #include <sys/signalvar.h>
  41. #include <sys/sysctl.h>
  42. #include <sys/mbuf.h>
  43. #include <sys/uio.h>
  44. #include <netipx/ipx.h>
  45. #include <netipx/ipx_var.h>
  46. #include <netncp/ncp.h>
  47. #include <netncp/ncp_conn.h>
  48. #include <netncp/ncp_sock.h>
  49. #include <netncp/ncp_subr.h>
  50. #include <netncp/ncp_ncp.h>
  51. #include <netncp/ncp_rq.h>
  52. #include <netncp/nwerror.h>
  53. #ifdef NCP_DATA_DEBUG
  54. static
  55. void m_dumpm(struct mbuf *m) {
  56. char *p;
  57. int len;
  58. printf("d=");
  59. while(m) {
  60. p=mtod(m,char *);
  61. len=m->m_len;
  62. printf("(%d)",len);
  63. while(len--){
  64. printf("%02x ",((int)*(p++)) & 0xff);
  65. }
  66. m=m->m_next;
  67. };
  68. printf("\n");
  69. }
  70. #endif /* NCP_DATA_DEBUG */
  71. int
  72. ncp_chkintr(struct ncp_conn *conn, struct proc *p)
  73. {
  74. sigset_t tmpset;
  75. if (p == NULL)
  76. return 0;
  77. tmpset = p->p_siglist;
  78. SIGSETNAND(tmpset, p->p_sigmask);
  79. SIGSETNAND(tmpset, p->p_sigignore);
  80. if (SIGNOTEMPTY(p->p_siglist) && NCP_SIGMASK(tmpset))
  81. return EINTR;
  82. return 0;
  83. }
  84. /*
  85. * Process initial NCP handshake (attach)
  86. * NOTE: Since all functions below may change conn attributes, they
  87. * should be called with LOCKED connection, also they use procp & ucred
  88. */
  89. int
  90. ncp_ncp_connect(struct ncp_conn *conn)
  91. {
  92. struct ncp_rq *rqp;
  93. struct ncp_rphdr *rp;
  94. int error;
  95. error = ncp_rq_alloc_any(NCP_ALLOC_SLOT, 0, conn, conn->procp, conn->ucred, &rqp);
  96. if (error)
  97. return error;
  98. conn->flags &= ~(NCPFL_SIGNACTIVE | NCPFL_SIGNWANTED |
  99. NCPFL_ATTACHED | NCPFL_LOGGED | NCPFL_INVALID);
  100. conn->seq = 0;
  101. error = ncp_request_int(rqp);
  102. if (!error) {
  103. rp = mtod(rqp->rp.md_top, struct ncp_rphdr*);
  104. conn->connid = rp->conn_low + (rp->conn_high << 8);
  105. }
  106. ncp_rq_done(rqp);
  107. if (error)
  108. return error;
  109. conn->flags |= NCPFL_ATTACHED | NCPFL_WASATTACHED;
  110. return 0;
  111. }
  112. int
  113. ncp_ncp_disconnect(struct ncp_conn *conn)
  114. {
  115. struct ncp_rq *rqp;
  116. int error;
  117. NCPSDEBUG("for connid=%d\n",conn->nc_id);
  118. #ifdef NCPBURST
  119. ncp_burst_disconnect(conn);
  120. #endif
  121. if (conn->flags & NCPFL_ATTACHED) {
  122. error = ncp_rq_alloc_any(NCP_FREE_SLOT, 0, conn, conn->procp, conn->ucred, &rqp);
  123. if (!error) {
  124. ncp_request_int(rqp);
  125. ncp_rq_done(rqp);
  126. }
  127. }
  128. ncp_conn_invalidate(conn);
  129. ncp_sock_disconnect(conn);
  130. return 0;
  131. }
  132. /*
  133. * All negotiation functions expect a locked connection
  134. */
  135. int
  136. ncp_negotiate_buffersize(struct ncp_conn *conn, int size, int *target)
  137. {
  138. struct ncp_rq *rqp;
  139. u_int16_t bsize;
  140. int error;
  141. error = ncp_rq_alloc(0x21, conn, conn->procp, conn->ucred, &rqp);
  142. if (error)
  143. return error;
  144. mb_put_uint16be(&rqp->rq, size);
  145. error = ncp_request(rqp);
  146. if (error)
  147. return error;
  148. md_get_uint16be(&rqp->rp, &bsize);
  149. *target = min(bsize, size);
  150. ncp_rq_done(rqp);
  151. return error;
  152. }
  153. static int
  154. ncp_negotiate_size_and_options(struct ncp_conn *conn, int size, int options,
  155. int *ret_size, u_int8_t *ret_options)
  156. {
  157. struct ncp_rq *rqp;
  158. u_int16_t rs;
  159. int error;
  160. error = ncp_rq_alloc(0x61, conn, conn->procp, conn->ucred, &rqp);
  161. if (error)
  162. return error;
  163. mb_put_uint16be(&rqp->rq, size);
  164. mb_put_uint8(&rqp->rq, options);
  165. rqp->nr_minrplen = 2 + 2 + 1;
  166. error = ncp_request(rqp);
  167. if (error)
  168. return error;
  169. md_get_uint16be(&rqp->rp, &rs);
  170. *ret_size = (rs == 0) ? size : min(rs, size);
  171. md_get_uint16be(&rqp->rp, &rs); /* skip echo socket */
  172. md_get_uint8(&rqp->rp, ret_options);
  173. ncp_rq_done(rqp);
  174. return error;
  175. }
  176. int
  177. ncp_renegotiate_connparam(struct ncp_conn *conn, int buffsize, u_int8_t in_options)
  178. {
  179. u_int8_t options;
  180. int neg_buffsize, error, sl, ckslevel, ilen;
  181. sl = conn->li.sig_level;
  182. if (sl >= 2)
  183. in_options |= NCP_SECURITY_LEVEL_SIGN_HEADERS;
  184. if (conn->li.saddr.sa_family == AF_IPX) {
  185. ilen = sizeof(ckslevel);
  186. error = kernel_sysctlbyname(curproc, "net.ipx.ipx.checksum",
  187. &ckslevel, &ilen, NULL, 0, NULL);
  188. if (error)
  189. return error;
  190. if (ckslevel == 2)
  191. in_options |= NCP_IPX_CHECKSUM;
  192. }
  193. error = ncp_negotiate_size_and_options(conn, buffsize, in_options,
  194. &neg_buffsize, &options);
  195. if (!error) {
  196. if (conn->li.saddr.sa_family == AF_IPX &&
  197. ((options ^ in_options) & NCP_IPX_CHECKSUM)) {
  198. if (ckslevel == 2) {
  199. printf("Server refuses to support IPX checksums\n");
  200. return NWE_REQUESTER_FAILURE;
  201. }
  202. in_options |= NCP_IPX_CHECKSUM;
  203. error = 1;
  204. }
  205. if ((options ^ in_options) & 2) {
  206. if (sl == 0 || sl == 3)
  207. return NWE_SIGNATURE_LEVEL_CONFLICT;
  208. if (sl == 1) {
  209. in_options |= NCP_SECURITY_LEVEL_SIGN_HEADERS;
  210. error = 1;
  211. }
  212. }
  213. if (error) {
  214. error = ncp_negotiate_size_and_options(conn,
  215. buffsize, in_options, &neg_buffsize, &options);
  216. if ((options ^ in_options) & 3) {
  217. return NWE_SIGNATURE_LEVEL_CONFLICT;
  218. }
  219. }
  220. } else {
  221. in_options &= ~NCP_SECURITY_LEVEL_SIGN_HEADERS;
  222. error = ncp_negotiate_buffersize(conn, NCP_DEFAULT_BUFSIZE,
  223. &neg_buffsize);
  224. }
  225. if (error) return error;
  226. if ((neg_buffsize < 512) || (neg_buffsize > NCP_MAX_BUFSIZE))
  227. return EINVAL;
  228. conn->buffer_size = neg_buffsize;
  229. if (in_options & NCP_SECURITY_LEVEL_SIGN_HEADERS)
  230. conn->flags |= NCPFL_SIGNWANTED;
  231. if (conn->li.saddr.sa_family == AF_IPX)
  232. ncp_sock_checksum(conn, in_options & NCP_IPX_CHECKSUM);
  233. return 0;
  234. }
  235. void
  236. ncp_check_rq(struct ncp_conn *conn){
  237. return;
  238. if (conn->flags & NCPFL_INTR) return;
  239. /* first, check for signals */
  240. if (ncp_chkintr(conn,conn->procp)) {
  241. conn->flags |= NCPFL_INTR;
  242. }
  243. return;
  244. }
  245. int
  246. ncp_get_bindery_object_id(struct ncp_conn *conn,
  247. u_int16_t object_type, char *object_name,
  248. struct ncp_bindery_object *target,
  249. struct proc *p,struct ucred *cred)
  250. {
  251. struct ncp_rq *rqp;
  252. int error;
  253. error = ncp_rq_alloc_subfn(23, 53, conn, conn->procp, conn->ucred, &rqp);
  254. mb_put_uint16be(&rqp->rq, object_type);
  255. ncp_rq_pstring(rqp, object_name);
  256. rqp->nr_minrplen = 54;
  257. error = ncp_request(rqp);
  258. if (error)
  259. return error;
  260. md_get_uint32be(&rqp->rp, &target->object_id);
  261. md_get_uint16be(&rqp->rp, &target->object_type);
  262. md_get_mem(&rqp->rp, (caddr_t)target->object_name, 48, MB_MSYSTEM);
  263. ncp_rq_done(rqp);
  264. return 0;
  265. }
  266. /*
  267. * target is a 8-byte buffer
  268. */
  269. int
  270. ncp_get_encryption_key(struct ncp_conn *conn, char *target)
  271. {
  272. struct ncp_rq *rqp;
  273. int error;
  274. error = ncp_rq_alloc_subfn(23, 23, conn, conn->procp, conn->ucred, &rqp);
  275. if (error)
  276. return error;
  277. rqp->nr_minrplen = 8;
  278. error = ncp_request(rqp);
  279. if (error)
  280. return error;
  281. md_get_mem(&rqp->rp, target, 8, MB_MSYSTEM);
  282. ncp_rq_done(rqp);
  283. return error;
  284. }
  285. /*
  286. * Initialize packet signatures. They a slightly modified MD4.
  287. * The first 16 bytes of logindata are the shuffled password,
  288. * the last 8 bytes the encryption key as received from the server.
  289. */
  290. static int
  291. ncp_sign_start(struct ncp_conn *conn, char *logindata)
  292. {
  293. char msg[64];
  294. u_int32_t state[4];
  295. memcpy(msg, logindata, 24);
  296. memcpy(msg + 24, "Authorized NetWare Client", 25);
  297. bzero(msg + 24 + 25, sizeof(msg) - 24 - 25);
  298. conn->sign_state[0] = 0x67452301;
  299. conn->sign_state[1] = 0xefcdab89;
  300. conn->sign_state[2] = 0x98badcfe;
  301. conn->sign_state[3] = 0x10325476;
  302. ncp_sign(conn->sign_state, msg, state);
  303. conn->sign_root[0] = state[0];
  304. conn->sign_root[1] = state[1];
  305. conn->flags |= NCPFL_SIGNACTIVE;
  306. return 0;
  307. }
  308. int
  309. ncp_login_encrypted(struct ncp_conn *conn, struct ncp_bindery_object *object,
  310. const u_char *key, const u_char *passwd,
  311. struct proc *p, struct ucred *cred)
  312. {
  313. struct ncp_rq *rqp;
  314. struct mbchain *mbp;
  315. u_int32_t tmpID = htonl(object->object_id);
  316. u_char buf[16 + 8];
  317. u_char encrypted[8];
  318. int error;
  319. nw_keyhash((u_char*)&tmpID, passwd, strlen(passwd), buf);
  320. nw_encrypt(key, buf, encrypted);
  321. error = ncp_rq_alloc_subfn(23, 24, conn, p, cred, &rqp);
  322. if (error)
  323. return error;
  324. mbp = &rqp->rq;
  325. mb_put_mem(mbp, encrypted, 8, MB_MSYSTEM);
  326. mb_put_uint16be(mbp, object->object_type);
  327. ncp_rq_pstring(rqp, object->object_name);
  328. error = ncp_request(rqp);
  329. if (!error)
  330. ncp_rq_done(rqp);
  331. if ((conn->flags & NCPFL_SIGNWANTED) &&
  332. (error == 0 || error == NWE_PASSWORD_EXPIRED)) {
  333. bcopy(key, buf + 16, 8);
  334. error = ncp_sign_start(conn, buf);
  335. }
  336. return error;
  337. }
  338. int
  339. ncp_login_unencrypted(struct ncp_conn *conn, u_int16_t object_type,
  340. const char *object_name, const u_char *passwd,
  341. struct proc *p, struct ucred *cred)
  342. {
  343. struct ncp_rq *rqp;
  344. int error;
  345. error = ncp_rq_alloc_subfn(23, 20, conn, p, cred, &rqp);
  346. if (error)
  347. return error;
  348. mb_put_uint16be(&rqp->rq, object_type);
  349. ncp_rq_pstring(rqp, object_name);
  350. ncp_rq_pstring(rqp, passwd);
  351. error = ncp_request(rqp);
  352. if (!error)
  353. ncp_rq_done(rqp);
  354. return error;
  355. }
  356. int
  357. ncp_read(struct ncp_conn *conn, ncp_fh *file, struct uio *uiop, struct ucred *cred)
  358. {
  359. struct ncp_rq *rqp;
  360. struct mbchain *mbp;
  361. u_int16_t retlen = 0 ;
  362. int error = 0, len = 0, tsiz, burstio;
  363. tsiz = uiop->uio_resid;
  364. #ifdef NCPBURST
  365. burstio = (ncp_burst_enabled && tsiz > conn->buffer_size);
  366. #else
  367. burstio = 0;
  368. #endif
  369. while (tsiz > 0) {
  370. if (!burstio) {
  371. len = min(4096 - (uiop->uio_offset % 4096), tsiz);
  372. len = min(len, conn->buffer_size);
  373. error = ncp_rq_alloc(72, conn, uiop->uio_procp, cred, &rqp);
  374. if (error)
  375. break;
  376. mbp = &rqp->rq;
  377. mb_put_uint8(mbp, 0);
  378. mb_put_mem(mbp, (caddr_t)file, 6, MB_MSYSTEM);
  379. mb_put_uint32be(mbp, uiop->uio_offset);
  380. mb_put_uint16be(mbp, len);
  381. rqp->nr_minrplen = 2;
  382. error = ncp_request(rqp);
  383. if (error)
  384. break;
  385. md_get_uint16be(&rqp->rp, &retlen);
  386. if (uiop->uio_offset & 1)
  387. md_get_mem(&rqp->rp, NULL, 1, MB_MSYSTEM);
  388. error = md_get_uio(&rqp->rp, uiop, retlen);
  389. ncp_rq_done(rqp);
  390. } else {
  391. #ifdef NCPBURST
  392. error = ncp_burst_read(conn, file, tsiz, &len, &retlen, uiop, cred);
  393. #endif
  394. }
  395. if (error)
  396. break;
  397. tsiz -= retlen;
  398. if (retlen < len)
  399. break;
  400. }
  401. return (error);
  402. }
  403. int
  404. ncp_write(struct ncp_conn *conn, ncp_fh *file, struct uio *uiop, struct ucred *cred)
  405. {
  406. struct ncp_rq *rqp;
  407. struct mbchain *mbp;
  408. int error = 0, len, tsiz, backup;
  409. if (uiop->uio_iovcnt != 1) {
  410. printf("%s: can't handle iovcnt>1 !!!\n", __func__);
  411. return EIO;
  412. }
  413. tsiz = uiop->uio_resid;
  414. while (tsiz > 0) {
  415. len = min(4096 - (uiop->uio_offset % 4096), tsiz);
  416. len = min(len, conn->buffer_size);
  417. if (len == 0) {
  418. printf("gotcha!\n");
  419. }
  420. /* rq head */
  421. error = ncp_rq_alloc(73, conn, uiop->uio_procp, cred, &rqp);
  422. if (error)
  423. break;
  424. mbp = &rqp->rq;
  425. mb_put_uint8(mbp, 0);
  426. mb_put_mem(mbp, (caddr_t)file, 6, MB_MSYSTEM);
  427. mb_put_uint32be(mbp, uiop->uio_offset);
  428. mb_put_uint16be(mbp, len);
  429. error = mb_put_uio(mbp, uiop, len);
  430. if (error) {
  431. ncp_rq_done(rqp);
  432. break;
  433. }
  434. error = ncp_request(rqp);
  435. if (!error)
  436. ncp_rq_done(rqp);
  437. if (len == 0)
  438. break;
  439. if (error) {
  440. backup = len;
  441. uiop->uio_iov->iov_base =
  442. (char *)uiop->uio_iov->iov_base - backup;
  443. uiop->uio_iov->iov_len += backup;
  444. uiop->uio_offset -= backup;
  445. uiop->uio_resid += backup;
  446. break;
  447. }
  448. tsiz -= len;
  449. }
  450. if (error)
  451. uiop->uio_resid = tsiz;
  452. switch (error) {
  453. case NWE_INSUFFICIENT_SPACE:
  454. error = ENOSPC;
  455. break;
  456. }
  457. return (error);
  458. }