smb.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2014, Bill Nagel <[email protected]>, Exacq Technologies
  9. * Copyright (C) 2015, Daniel Stenberg, <[email protected]>, et al.
  10. *
  11. * This software is licensed as described in the file COPYING, which
  12. * you should have received as part of this distribution. The terms
  13. * are also available at https://curl.haxx.se/docs/copyright.html.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the COPYING file.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. ***************************************************************************/
  23. #include "curl_setup.h"
  24. #if !defined(CURL_DISABLE_SMB) && defined(USE_NTLM) && \
  25. (CURL_SIZEOF_CURL_OFF_T > 4)
  26. #if !defined(USE_WINDOWS_SSPI) || defined(USE_WIN32_CRYPTO)
  27. #define BUILDING_CURL_SMB_C
  28. #ifdef HAVE_PROCESS_H
  29. #include <process.h>
  30. #define getpid _getpid
  31. #endif
  32. #include "smb.h"
  33. #include "urldata.h"
  34. #include "sendf.h"
  35. #include "multiif.h"
  36. #include "connect.h"
  37. #include "progress.h"
  38. #include "transfer.h"
  39. #include "vtls/vtls.h"
  40. #include "curl_ntlm_core.h"
  41. #include "escape.h"
  42. #include "curl_endian.h"
  43. /* The last #include files should be: */
  44. #include "curl_memory.h"
  45. #include "memdebug.h"
  46. /* Local API functions */
  47. static CURLcode smb_setup_connection(struct connectdata *conn);
  48. static CURLcode smb_connect(struct connectdata *conn, bool *done);
  49. static CURLcode smb_connection_state(struct connectdata *conn, bool *done);
  50. static CURLcode smb_request_state(struct connectdata *conn, bool *done);
  51. static CURLcode smb_done(struct connectdata *conn, CURLcode status,
  52. bool premature);
  53. static CURLcode smb_disconnect(struct connectdata *conn, bool dead);
  54. static int smb_getsock(struct connectdata *conn, curl_socket_t *socks,
  55. int numsocks);
  56. static CURLcode smb_parse_url_path(struct connectdata *conn);
  57. /*
  58. * SMB handler interface
  59. */
  60. const struct Curl_handler Curl_handler_smb = {
  61. "SMB", /* scheme */
  62. smb_setup_connection, /* setup_connection */
  63. ZERO_NULL, /* do_it */
  64. smb_done, /* done */
  65. ZERO_NULL, /* do_more */
  66. smb_connect, /* connect_it */
  67. smb_connection_state, /* connecting */
  68. smb_request_state, /* doing */
  69. smb_getsock, /* proto_getsock */
  70. smb_getsock, /* doing_getsock */
  71. ZERO_NULL, /* domore_getsock */
  72. ZERO_NULL, /* perform_getsock */
  73. smb_disconnect, /* disconnect */
  74. ZERO_NULL, /* readwrite */
  75. PORT_SMB, /* defport */
  76. CURLPROTO_SMB, /* protocol */
  77. PROTOPT_NONE /* flags */
  78. };
  79. #ifdef USE_SSL
  80. /*
  81. * SMBS handler interface
  82. */
  83. const struct Curl_handler Curl_handler_smbs = {
  84. "SMBS", /* scheme */
  85. smb_setup_connection, /* setup_connection */
  86. ZERO_NULL, /* do_it */
  87. smb_done, /* done */
  88. ZERO_NULL, /* do_more */
  89. smb_connect, /* connect_it */
  90. smb_connection_state, /* connecting */
  91. smb_request_state, /* doing */
  92. smb_getsock, /* proto_getsock */
  93. smb_getsock, /* doing_getsock */
  94. ZERO_NULL, /* domore_getsock */
  95. ZERO_NULL, /* perform_getsock */
  96. smb_disconnect, /* disconnect */
  97. ZERO_NULL, /* readwrite */
  98. PORT_SMBS, /* defport */
  99. CURLPROTO_SMBS, /* protocol */
  100. PROTOPT_SSL /* flags */
  101. };
  102. #endif
  103. #define MAX_PAYLOAD_SIZE 0x8000
  104. #define MAX_MESSAGE_SIZE (MAX_PAYLOAD_SIZE + 0x1000)
  105. #define CLIENTNAME "curl"
  106. #define SERVICENAME "?????"
  107. /* Append a string to an SMB message */
  108. #define MSGCAT(str) \
  109. strcpy(p, (str)); \
  110. p += strlen(str);
  111. /* Append a null-terminated string to an SMB message */
  112. #define MSGCATNULL(str) \
  113. strcpy(p, (str)); \
  114. p += strlen(str) + 1;
  115. /* SMB is mostly little endian */
  116. #if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
  117. defined(__OS400__)
  118. static unsigned short smb_swap16(unsigned short x)
  119. {
  120. return (unsigned short) ((x << 8) | ((x >> 8) & 0xff));
  121. }
  122. static unsigned int smb_swap32(unsigned int x)
  123. {
  124. return (x << 24) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) |
  125. ((x >> 24) & 0xff);
  126. }
  127. #ifdef HAVE_LONGLONG
  128. static unsigned long long smb_swap64(unsigned long long x)
  129. {
  130. return ((unsigned long long) smb_swap32((unsigned int) x) << 32) |
  131. smb_swap32((unsigned int) (x >> 32));
  132. }
  133. #else
  134. static unsigned __int64 smb_swap64(unsigned __int64 x)
  135. {
  136. return ((unsigned __int64) smb_swap32((unsigned int) x) << 32) |
  137. smb_swap32((unsigned int) (x >> 32));
  138. }
  139. #endif
  140. #else
  141. # define smb_swap16(x) (x)
  142. # define smb_swap32(x) (x)
  143. # define smb_swap64(x) (x)
  144. #endif
  145. /* SMB request state */
  146. enum smb_req_state {
  147. SMB_REQUESTING,
  148. SMB_TREE_CONNECT,
  149. SMB_OPEN,
  150. SMB_DOWNLOAD,
  151. SMB_UPLOAD,
  152. SMB_CLOSE,
  153. SMB_TREE_DISCONNECT,
  154. SMB_DONE
  155. };
  156. /* SMB request data */
  157. struct smb_request {
  158. enum smb_req_state state;
  159. char *share;
  160. char *path;
  161. unsigned short tid; /* Even if we connect to the same tree as another */
  162. unsigned short fid; /* request, the tid will be different */
  163. CURLcode result;
  164. };
  165. static void conn_state(struct connectdata *conn, enum smb_conn_state newstate)
  166. {
  167. struct smb_conn *smb = &conn->proto.smbc;
  168. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  169. /* For debug purposes */
  170. static const char * const names[] = {
  171. "SMB_NOT_CONNECTED",
  172. "SMB_CONNECTING",
  173. "SMB_NEGOTIATE",
  174. "SMB_SETUP",
  175. "SMB_CONNECTED",
  176. /* LAST */
  177. };
  178. if(smb->state != newstate)
  179. infof(conn->data, "SMB conn %p state change from %s to %s\n",
  180. (void *)smb, names[smb->state], names[newstate]);
  181. #endif
  182. smb->state = newstate;
  183. }
  184. static void request_state(struct connectdata *conn,
  185. enum smb_req_state newstate)
  186. {
  187. struct smb_request *req = conn->data->req.protop;
  188. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  189. /* For debug purposes */
  190. static const char * const names[] = {
  191. "SMB_REQUESTING",
  192. "SMB_TREE_CONNECT",
  193. "SMB_OPEN",
  194. "SMB_DOWNLOAD",
  195. "SMB_UPLOAD",
  196. "SMB_CLOSE",
  197. "SMB_TREE_DISCONNECT",
  198. "SMB_DONE",
  199. /* LAST */
  200. };
  201. if(req->state != newstate)
  202. infof(conn->data, "SMB request %p state change from %s to %s\n",
  203. (void *)req, names[req->state], names[newstate]);
  204. #endif
  205. req->state = newstate;
  206. }
  207. static CURLcode smb_setup_connection(struct connectdata *conn)
  208. {
  209. struct smb_request *req;
  210. /* Initialize the request state */
  211. conn->data->req.protop = req = calloc(1, sizeof(struct smb_request));
  212. if(!req)
  213. return CURLE_OUT_OF_MEMORY;
  214. /* Parse the URL path */
  215. return smb_parse_url_path(conn);
  216. }
  217. static CURLcode smb_connect(struct connectdata *conn, bool *done)
  218. {
  219. struct smb_conn *smbc = &conn->proto.smbc;
  220. char *slash;
  221. (void) done;
  222. /* Check we have a username and password to authenticate with */
  223. if(!conn->bits.user_passwd)
  224. return CURLE_LOGIN_DENIED;
  225. /* Initialize the connection state */
  226. memset(smbc, 0, sizeof(*smbc));
  227. smbc->state = SMB_CONNECTING;
  228. smbc->recv_buf = malloc(MAX_MESSAGE_SIZE);
  229. if(!smbc->recv_buf)
  230. return CURLE_OUT_OF_MEMORY;
  231. /* Multiple requests are allowed with this connection */
  232. connkeep(conn, "SMB default");
  233. /* Parse the username, domain, and password */
  234. slash = strchr(conn->user, '/');
  235. if(!slash)
  236. slash = strchr(conn->user, '\\');
  237. if(slash) {
  238. smbc->user = slash + 1;
  239. smbc->domain = strdup(conn->user);
  240. if(!smbc->domain)
  241. return CURLE_OUT_OF_MEMORY;
  242. smbc->domain[slash - conn->user] = 0;
  243. }
  244. else {
  245. smbc->user = conn->user;
  246. smbc->domain = strdup(conn->host.name);
  247. if(!smbc->domain)
  248. return CURLE_OUT_OF_MEMORY;
  249. }
  250. return CURLE_OK;
  251. }
  252. static CURLcode smb_recv_message(struct connectdata *conn, void **msg)
  253. {
  254. struct smb_conn *smbc = &conn->proto.smbc;
  255. char *buf = smbc->recv_buf;
  256. ssize_t bytes_read;
  257. size_t nbt_size;
  258. size_t msg_size;
  259. size_t len = MAX_MESSAGE_SIZE - smbc->got;
  260. CURLcode result;
  261. result = Curl_read(conn, FIRSTSOCKET, buf + smbc->got, len, &bytes_read);
  262. if(result)
  263. return result;
  264. if(!bytes_read)
  265. return CURLE_OK;
  266. smbc->got += bytes_read;
  267. /* Check for a 32-bit nbt header */
  268. if(smbc->got < sizeof(unsigned int))
  269. return CURLE_OK;
  270. nbt_size = Curl_read16_be((unsigned char *)(buf + sizeof(unsigned short))) +
  271. sizeof(unsigned int);
  272. if(smbc->got < nbt_size)
  273. return CURLE_OK;
  274. msg_size = sizeof(struct smb_header);
  275. if(nbt_size >= msg_size + 1) {
  276. /* Add the word count */
  277. msg_size += 1 + ((unsigned char) buf[msg_size]) * sizeof(unsigned short);
  278. if(nbt_size >= msg_size + sizeof(unsigned short)) {
  279. /* Add the byte count */
  280. msg_size += sizeof(unsigned short) +
  281. Curl_read16_le((unsigned char *)&buf[msg_size]);
  282. if(nbt_size < msg_size)
  283. return CURLE_READ_ERROR;
  284. }
  285. }
  286. *msg = buf;
  287. return CURLE_OK;
  288. }
  289. static void smb_pop_message(struct connectdata *conn)
  290. {
  291. struct smb_conn *smbc = &conn->proto.smbc;
  292. smbc->got = 0;
  293. }
  294. static void smb_format_message(struct connectdata *conn, struct smb_header *h,
  295. unsigned char cmd, size_t len)
  296. {
  297. struct smb_conn *smbc = &conn->proto.smbc;
  298. struct smb_request *req = conn->data->req.protop;
  299. unsigned int pid;
  300. memset(h, 0, sizeof(*h));
  301. h->nbt_length = htons((unsigned short) (sizeof(*h) - sizeof(unsigned int) +
  302. len));
  303. memcpy((char *)h->magic, "\xffSMB", 4);
  304. h->command = cmd;
  305. h->flags = SMB_FLAGS_CANONICAL_PATHNAMES | SMB_FLAGS_CASELESS_PATHNAMES;
  306. h->flags2 = smb_swap16(SMB_FLAGS2_IS_LONG_NAME | SMB_FLAGS2_KNOWS_LONG_NAME);
  307. h->uid = smb_swap16(smbc->uid);
  308. h->tid = smb_swap16(req->tid);
  309. pid = getpid();
  310. h->pid_high = smb_swap16((unsigned short)(pid >> 16));
  311. h->pid = smb_swap16((unsigned short) pid);
  312. }
  313. static CURLcode smb_send(struct connectdata *conn, ssize_t len,
  314. size_t upload_size)
  315. {
  316. struct smb_conn *smbc = &conn->proto.smbc;
  317. ssize_t bytes_written;
  318. CURLcode result;
  319. result = Curl_write(conn, FIRSTSOCKET, conn->data->state.uploadbuffer,
  320. len, &bytes_written);
  321. if(result)
  322. return result;
  323. if(bytes_written != len) {
  324. smbc->send_size = len;
  325. smbc->sent = bytes_written;
  326. }
  327. smbc->upload_size = upload_size;
  328. return CURLE_OK;
  329. }
  330. static CURLcode smb_flush(struct connectdata *conn)
  331. {
  332. struct smb_conn *smbc = &conn->proto.smbc;
  333. ssize_t bytes_written;
  334. ssize_t len = smbc->send_size - smbc->sent;
  335. CURLcode result;
  336. if(!smbc->send_size)
  337. return CURLE_OK;
  338. result = Curl_write(conn, FIRSTSOCKET,
  339. conn->data->state.uploadbuffer + smbc->sent,
  340. len, &bytes_written);
  341. if(result)
  342. return result;
  343. if(bytes_written != len)
  344. smbc->sent += bytes_written;
  345. else
  346. smbc->send_size = 0;
  347. return CURLE_OK;
  348. }
  349. static CURLcode smb_send_message(struct connectdata *conn, unsigned char cmd,
  350. const void *msg, size_t msg_len)
  351. {
  352. smb_format_message(conn, (struct smb_header *)conn->data->state.uploadbuffer,
  353. cmd, msg_len);
  354. memcpy(conn->data->state.uploadbuffer + sizeof(struct smb_header),
  355. msg, msg_len);
  356. return smb_send(conn, sizeof(struct smb_header) + msg_len, 0);
  357. }
  358. static CURLcode smb_send_negotiate(struct connectdata *conn)
  359. {
  360. const char *msg = "\x00\x0c\x00\x02NT LM 0.12";
  361. return smb_send_message(conn, SMB_COM_NEGOTIATE, msg, 15);
  362. }
  363. static CURLcode smb_send_setup(struct connectdata *conn)
  364. {
  365. struct smb_conn *smbc = &conn->proto.smbc;
  366. struct smb_setup msg;
  367. char *p = msg.bytes;
  368. unsigned char lm_hash[21];
  369. unsigned char lm[24];
  370. unsigned char nt_hash[21];
  371. unsigned char nt[24];
  372. size_t byte_count = sizeof(lm) + sizeof(nt);
  373. byte_count += strlen(smbc->user) + strlen(smbc->domain);
  374. byte_count += strlen(OS) + strlen(CLIENTNAME) + 4; /* 4 null chars */
  375. if(byte_count > sizeof(msg.bytes))
  376. return CURLE_FILESIZE_EXCEEDED;
  377. Curl_ntlm_core_mk_lm_hash(conn->data, conn->passwd, lm_hash);
  378. Curl_ntlm_core_lm_resp(lm_hash, smbc->challenge, lm);
  379. #if USE_NTRESPONSES
  380. Curl_ntlm_core_mk_nt_hash(conn->data, conn->passwd, nt_hash);
  381. Curl_ntlm_core_lm_resp(nt_hash, smbc->challenge, nt);
  382. #else
  383. memset(nt, 0, sizeof(nt));
  384. #endif
  385. memset(&msg, 0, sizeof(msg));
  386. msg.word_count = SMB_WC_SETUP_ANDX;
  387. msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
  388. msg.max_buffer_size = smb_swap16(MAX_MESSAGE_SIZE);
  389. msg.max_mpx_count = smb_swap16(1);
  390. msg.vc_number = smb_swap16(1);
  391. msg.session_key = smb_swap32(smbc->session_key);
  392. msg.capabilities = smb_swap32(SMB_CAP_LARGE_FILES);
  393. msg.lengths[0] = smb_swap16(sizeof(lm));
  394. msg.lengths[1] = smb_swap16(sizeof(nt));
  395. memcpy(p, lm, sizeof(lm));
  396. p += sizeof(lm);
  397. memcpy(p, nt, sizeof(nt));
  398. p += sizeof(nt);
  399. MSGCATNULL(smbc->user);
  400. MSGCATNULL(smbc->domain);
  401. MSGCATNULL(OS);
  402. MSGCATNULL(CLIENTNAME);
  403. byte_count = p - msg.bytes;
  404. msg.byte_count = smb_swap16((unsigned short)byte_count);
  405. return smb_send_message(conn, SMB_COM_SETUP_ANDX, &msg,
  406. sizeof(msg) - sizeof(msg.bytes) + byte_count);
  407. }
  408. static CURLcode smb_send_tree_connect(struct connectdata *conn)
  409. {
  410. struct smb_request *req = conn->data->req.protop;
  411. struct smb_tree_connect msg;
  412. char *p = msg.bytes;
  413. size_t byte_count = strlen(conn->host.name) + strlen(req->share);
  414. byte_count += strlen(SERVICENAME) + 5; /* 2 nulls and 3 backslashes */
  415. if(byte_count > sizeof(msg.bytes))
  416. return CURLE_FILESIZE_EXCEEDED;
  417. memset(&msg, 0, sizeof(msg));
  418. msg.word_count = SMB_WC_TREE_CONNECT_ANDX;
  419. msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
  420. msg.pw_len = 0;
  421. MSGCAT("\\\\");
  422. MSGCAT(conn->host.name);
  423. MSGCAT("\\");
  424. MSGCATNULL(req->share);
  425. MSGCATNULL(SERVICENAME); /* Match any type of service */
  426. byte_count = p - msg.bytes;
  427. msg.byte_count = smb_swap16((unsigned short)byte_count);
  428. return smb_send_message(conn, SMB_COM_TREE_CONNECT_ANDX, &msg,
  429. sizeof(msg) - sizeof(msg.bytes) + byte_count);
  430. }
  431. static CURLcode smb_send_open(struct connectdata *conn)
  432. {
  433. struct smb_request *req = conn->data->req.protop;
  434. struct smb_nt_create msg;
  435. size_t byte_count;
  436. if((strlen(req->path) + 1) > sizeof(msg.bytes))
  437. return CURLE_FILESIZE_EXCEEDED;
  438. memset(&msg, 0, sizeof(msg));
  439. msg.word_count = SMB_WC_NT_CREATE_ANDX;
  440. msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
  441. byte_count = strlen(req->path);
  442. msg.name_length = smb_swap16((unsigned short)byte_count);
  443. msg.share_access = smb_swap32(SMB_FILE_SHARE_ALL);
  444. if(conn->data->set.upload) {
  445. msg.access = smb_swap32(SMB_GENERIC_READ | SMB_GENERIC_WRITE);
  446. msg.create_disposition = smb_swap32(SMB_FILE_OVERWRITE_IF);
  447. }
  448. else {
  449. msg.access = smb_swap32(SMB_GENERIC_READ);
  450. msg.create_disposition = smb_swap32(SMB_FILE_OPEN);
  451. }
  452. msg.byte_count = smb_swap16((unsigned short) ++byte_count);
  453. strcpy(msg.bytes, req->path);
  454. return smb_send_message(conn, SMB_COM_NT_CREATE_ANDX, &msg,
  455. sizeof(msg) - sizeof(msg.bytes) + byte_count);
  456. }
  457. static CURLcode smb_send_close(struct connectdata *conn)
  458. {
  459. struct smb_request *req = conn->data->req.protop;
  460. struct smb_close msg;
  461. memset(&msg, 0, sizeof(msg));
  462. msg.word_count = SMB_WC_CLOSE;
  463. msg.fid = smb_swap16(req->fid);
  464. return smb_send_message(conn, SMB_COM_CLOSE, &msg, sizeof(msg));
  465. }
  466. static CURLcode smb_send_tree_disconnect(struct connectdata *conn)
  467. {
  468. struct smb_tree_disconnect msg;
  469. memset(&msg, 0, sizeof(msg));
  470. return smb_send_message(conn, SMB_COM_TREE_DISCONNECT, &msg, sizeof(msg));
  471. }
  472. static CURLcode smb_send_read(struct connectdata *conn)
  473. {
  474. struct smb_request *req = conn->data->req.protop;
  475. curl_off_t offset = conn->data->req.offset;
  476. struct smb_read msg;
  477. memset(&msg, 0, sizeof(msg));
  478. msg.word_count = SMB_WC_READ_ANDX;
  479. msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
  480. msg.fid = smb_swap16(req->fid);
  481. msg.offset = smb_swap32((unsigned int) offset);
  482. msg.offset_high = smb_swap32((unsigned int) (offset >> 32));
  483. msg.min_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
  484. msg.max_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
  485. return smb_send_message(conn, SMB_COM_READ_ANDX, &msg, sizeof(msg));
  486. }
  487. static CURLcode smb_send_write(struct connectdata *conn)
  488. {
  489. struct smb_write *msg = (struct smb_write *)conn->data->state.uploadbuffer;
  490. struct smb_request *req = conn->data->req.protop;
  491. curl_off_t offset = conn->data->req.offset;
  492. curl_off_t upload_size = conn->data->req.size - conn->data->req.bytecount;
  493. if(upload_size >= MAX_PAYLOAD_SIZE - 1) /* There is one byte of padding */
  494. upload_size = MAX_PAYLOAD_SIZE - 1;
  495. memset(msg, 0, sizeof(*msg));
  496. msg->word_count = SMB_WC_WRITE_ANDX;
  497. msg->andx.command = SMB_COM_NO_ANDX_COMMAND;
  498. msg->fid = smb_swap16(req->fid);
  499. msg->offset = smb_swap32((unsigned int) offset);
  500. msg->offset_high = smb_swap32((unsigned int) (offset >> 32));
  501. msg->data_length = smb_swap16((unsigned short) upload_size);
  502. msg->data_offset = smb_swap16(sizeof(*msg) - sizeof(unsigned int));
  503. msg->byte_count = smb_swap16((unsigned short) (upload_size + 1));
  504. smb_format_message(conn, &msg->h, SMB_COM_WRITE_ANDX,
  505. sizeof(*msg) - sizeof(msg->h) + (size_t) upload_size);
  506. return smb_send(conn, sizeof(*msg), (size_t) upload_size);
  507. }
  508. static CURLcode smb_send_and_recv(struct connectdata *conn, void **msg)
  509. {
  510. struct smb_conn *smbc = &conn->proto.smbc;
  511. CURLcode result;
  512. /* Check if there is data in the transfer buffer */
  513. if(!smbc->send_size && smbc->upload_size) {
  514. int nread = smbc->upload_size > BUFSIZE ? BUFSIZE :
  515. (int) smbc->upload_size;
  516. conn->data->req.upload_fromhere = conn->data->state.uploadbuffer;
  517. result = Curl_fillreadbuffer(conn, nread, &nread);
  518. if(result && result != CURLE_AGAIN)
  519. return result;
  520. if(!nread)
  521. return CURLE_OK;
  522. smbc->upload_size -= nread;
  523. smbc->send_size = nread;
  524. smbc->sent = 0;
  525. }
  526. /* Check if there is data to send */
  527. if(smbc->send_size) {
  528. result = smb_flush(conn);
  529. if(result)
  530. return result;
  531. }
  532. /* Check if there is still data to be sent */
  533. if(smbc->send_size || smbc->upload_size)
  534. return CURLE_AGAIN;
  535. return smb_recv_message(conn, msg);
  536. }
  537. static CURLcode smb_connection_state(struct connectdata *conn, bool *done)
  538. {
  539. struct smb_conn *smbc = &conn->proto.smbc;
  540. struct smb_negotiate_response *nrsp;
  541. struct smb_header *h;
  542. CURLcode result;
  543. void *msg = NULL;
  544. if(smbc->state == SMB_CONNECTING) {
  545. #ifdef USE_SSL
  546. if((conn->handler->flags & PROTOPT_SSL)) {
  547. bool ssl_done;
  548. result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &ssl_done);
  549. if(result && result != CURLE_AGAIN)
  550. return result;
  551. if(!ssl_done)
  552. return CURLE_OK;
  553. }
  554. #endif
  555. result = smb_send_negotiate(conn);
  556. if(result) {
  557. connclose(conn, "SMB: failed to send negotiate message");
  558. return result;
  559. }
  560. conn_state(conn, SMB_NEGOTIATE);
  561. }
  562. /* Send the previous message and check for a response */
  563. result = smb_send_and_recv(conn, &msg);
  564. if(result && result != CURLE_AGAIN) {
  565. connclose(conn, "SMB: failed to communicate");
  566. return result;
  567. }
  568. if(!msg)
  569. return CURLE_OK;
  570. h = msg;
  571. switch(smbc->state) {
  572. case SMB_NEGOTIATE:
  573. if(h->status || smbc->got < sizeof(*nrsp) + sizeof(smbc->challenge) - 1) {
  574. connclose(conn, "SMB: negotiation failed");
  575. return CURLE_COULDNT_CONNECT;
  576. }
  577. nrsp = msg;
  578. memcpy(smbc->challenge, nrsp->bytes, sizeof(smbc->challenge));
  579. smbc->session_key = smb_swap32(nrsp->session_key);
  580. result = smb_send_setup(conn);
  581. if(result) {
  582. connclose(conn, "SMB: failed to send setup message");
  583. return result;
  584. }
  585. conn_state(conn, SMB_SETUP);
  586. break;
  587. case SMB_SETUP:
  588. if(h->status) {
  589. connclose(conn, "SMB: authentication failed");
  590. return CURLE_LOGIN_DENIED;
  591. }
  592. smbc->uid = smb_swap16(h->uid);
  593. conn_state(conn, SMB_CONNECTED);
  594. *done = true;
  595. break;
  596. default:
  597. smb_pop_message(conn);
  598. return CURLE_OK; /* ignore */
  599. }
  600. smb_pop_message(conn);
  601. return CURLE_OK;
  602. }
  603. static CURLcode smb_request_state(struct connectdata *conn, bool *done)
  604. {
  605. struct smb_request *req = conn->data->req.protop;
  606. struct smb_header *h;
  607. struct smb_conn *smbc = &conn->proto.smbc;
  608. enum smb_req_state next_state = SMB_DONE;
  609. unsigned short len;
  610. unsigned short off;
  611. CURLcode result;
  612. void *msg = NULL;
  613. /* Start the request */
  614. if(req->state == SMB_REQUESTING) {
  615. result = smb_send_tree_connect(conn);
  616. if(result) {
  617. connclose(conn, "SMB: failed to send tree connect message");
  618. return result;
  619. }
  620. request_state(conn, SMB_TREE_CONNECT);
  621. }
  622. /* Send the previous message and check for a response */
  623. result = smb_send_and_recv(conn, &msg);
  624. if(result && result != CURLE_AGAIN) {
  625. connclose(conn, "SMB: failed to communicate");
  626. return result;
  627. }
  628. if(!msg)
  629. return CURLE_OK;
  630. h = msg;
  631. switch(req->state) {
  632. case SMB_TREE_CONNECT:
  633. if(h->status) {
  634. req->result = CURLE_REMOTE_FILE_NOT_FOUND;
  635. if(h->status == smb_swap32(SMB_ERR_NOACCESS))
  636. req->result = CURLE_REMOTE_ACCESS_DENIED;
  637. break;
  638. }
  639. req->tid = smb_swap16(h->tid);
  640. next_state = SMB_OPEN;
  641. break;
  642. case SMB_OPEN:
  643. if(h->status || smbc->got < sizeof(struct smb_nt_create_response)) {
  644. req->result = CURLE_REMOTE_FILE_NOT_FOUND;
  645. next_state = SMB_TREE_DISCONNECT;
  646. break;
  647. }
  648. req->fid = smb_swap16(((struct smb_nt_create_response *)msg)->fid);
  649. conn->data->req.offset = 0;
  650. if(conn->data->set.upload) {
  651. conn->data->req.size = conn->data->state.infilesize;
  652. Curl_pgrsSetUploadSize(conn->data, conn->data->req.size);
  653. next_state = SMB_UPLOAD;
  654. }
  655. else {
  656. conn->data->req.size =
  657. smb_swap64(((struct smb_nt_create_response *)msg)->end_of_file);
  658. Curl_pgrsSetDownloadSize(conn->data, conn->data->req.size);
  659. next_state = SMB_DOWNLOAD;
  660. }
  661. break;
  662. case SMB_DOWNLOAD:
  663. if(h->status || smbc->got < sizeof(struct smb_header) + 14) {
  664. req->result = CURLE_RECV_ERROR;
  665. next_state = SMB_CLOSE;
  666. break;
  667. }
  668. len = Curl_read16_le(((unsigned char *) msg) +
  669. sizeof(struct smb_header) + 11);
  670. off = Curl_read16_le(((unsigned char *) msg) +
  671. sizeof(struct smb_header) + 13);
  672. if(len > 0) {
  673. if(off + sizeof(unsigned int) + len > smbc->got) {
  674. failf(conn->data, "Invalid input packet");
  675. result = CURLE_RECV_ERROR;
  676. }
  677. else
  678. result = Curl_client_write(conn, CLIENTWRITE_BODY,
  679. (char *)msg + off + sizeof(unsigned int),
  680. len);
  681. if(result) {
  682. req->result = result;
  683. next_state = SMB_CLOSE;
  684. break;
  685. }
  686. }
  687. conn->data->req.bytecount += len;
  688. conn->data->req.offset += len;
  689. Curl_pgrsSetDownloadCounter(conn->data, conn->data->req.bytecount);
  690. next_state = (len < MAX_PAYLOAD_SIZE) ? SMB_CLOSE : SMB_DOWNLOAD;
  691. break;
  692. case SMB_UPLOAD:
  693. if(h->status || smbc->got < sizeof(struct smb_header) + 6) {
  694. req->result = CURLE_UPLOAD_FAILED;
  695. next_state = SMB_CLOSE;
  696. break;
  697. }
  698. len = Curl_read16_le(((unsigned char *) msg) +
  699. sizeof(struct smb_header) + 5);
  700. conn->data->req.bytecount += len;
  701. conn->data->req.offset += len;
  702. Curl_pgrsSetUploadCounter(conn->data, conn->data->req.bytecount);
  703. if(conn->data->req.bytecount >= conn->data->req.size)
  704. next_state = SMB_CLOSE;
  705. else
  706. next_state = SMB_UPLOAD;
  707. break;
  708. case SMB_CLOSE:
  709. /* We don't care if the close failed, proceed to tree disconnect anyway */
  710. next_state = SMB_TREE_DISCONNECT;
  711. break;
  712. case SMB_TREE_DISCONNECT:
  713. next_state = SMB_DONE;
  714. break;
  715. default:
  716. smb_pop_message(conn);
  717. return CURLE_OK; /* ignore */
  718. }
  719. smb_pop_message(conn);
  720. switch(next_state) {
  721. case SMB_OPEN:
  722. result = smb_send_open(conn);
  723. break;
  724. case SMB_DOWNLOAD:
  725. result = smb_send_read(conn);
  726. break;
  727. case SMB_UPLOAD:
  728. result = smb_send_write(conn);
  729. break;
  730. case SMB_CLOSE:
  731. result = smb_send_close(conn);
  732. break;
  733. case SMB_TREE_DISCONNECT:
  734. result = smb_send_tree_disconnect(conn);
  735. break;
  736. case SMB_DONE:
  737. result = req->result;
  738. *done = true;
  739. break;
  740. default:
  741. break;
  742. }
  743. if(result) {
  744. connclose(conn, "SMB: failed to send message");
  745. return result;
  746. }
  747. request_state(conn, next_state);
  748. return CURLE_OK;
  749. }
  750. static CURLcode smb_done(struct connectdata *conn, CURLcode status,
  751. bool premature)
  752. {
  753. struct smb_request *req = conn->data->req.protop;
  754. (void) premature;
  755. Curl_safefree(req->share);
  756. Curl_safefree(conn->data->req.protop);
  757. return status;
  758. }
  759. static CURLcode smb_disconnect(struct connectdata *conn, bool dead)
  760. {
  761. struct smb_conn *smbc = &conn->proto.smbc;
  762. struct smb_request *req = conn->data->req.protop;
  763. (void) dead;
  764. Curl_safefree(smbc->domain);
  765. Curl_safefree(smbc->recv_buf);
  766. /* smb_done is not always called, so cleanup the request */
  767. if(req) {
  768. Curl_safefree(req->share);
  769. }
  770. return CURLE_OK;
  771. }
  772. static int smb_getsock(struct connectdata *conn, curl_socket_t *socks,
  773. int numsocks)
  774. {
  775. struct smb_conn *smbc = &conn->proto.smbc;
  776. if(!numsocks)
  777. return GETSOCK_BLANK;
  778. socks[0] = conn->sock[FIRSTSOCKET];
  779. if(smbc->send_size || smbc->upload_size)
  780. return GETSOCK_WRITESOCK(0);
  781. return GETSOCK_READSOCK(0);
  782. }
  783. static CURLcode smb_parse_url_path(struct connectdata *conn)
  784. {
  785. CURLcode result = CURLE_OK;
  786. struct Curl_easy *data = conn->data;
  787. struct smb_request *req = data->req.protop;
  788. char *path;
  789. char *slash;
  790. /* URL decode the path */
  791. result = Curl_urldecode(data, data->state.path, 0, &path, NULL, TRUE);
  792. if(result)
  793. return result;
  794. /* Parse the path for the share */
  795. req->share = strdup((*path == '/' || *path == '\\') ? path + 1 : path);
  796. if(!req->share) {
  797. free(path);
  798. return CURLE_OUT_OF_MEMORY;
  799. }
  800. slash = strchr(req->share, '/');
  801. if(!slash)
  802. slash = strchr(req->share, '\\');
  803. /* The share must be present */
  804. if(!slash) {
  805. free(path);
  806. return CURLE_URL_MALFORMAT;
  807. }
  808. /* Parse the path for the file path converting any forward slashes into
  809. backslashes */
  810. *slash++ = 0;
  811. req->path = slash;
  812. for(; *slash; slash++) {
  813. if(*slash == '/')
  814. *slash = '\\';
  815. }
  816. free(path);
  817. return CURLE_OK;
  818. }
  819. #endif /* !USE_WINDOWS_SSPI || USE_WIN32_CRYPTO */
  820. #endif /* CURL_DISABLE_SMB && USE_NTLM && CURL_SIZEOF_CURL_OFF_T > 4 */