smb.c 28 KB

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