smb.c 27 KB

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