smb.c 28 KB

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