smb.c 28 KB

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