smb.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. * Copyright (C) Bill Nagel <wnagel@tycoint.com>, Exacq Technologies
  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.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. * SPDX-License-Identifier: curl
  23. *
  24. ***************************************************************************/
  25. #include "curl_setup.h"
  26. #if !defined(CURL_DISABLE_SMB) && defined(USE_CURL_NTLM_CORE)
  27. #ifdef _WIN32
  28. #define getpid GetCurrentProcessId
  29. #endif
  30. #include "smb.h"
  31. #include "urldata.h"
  32. #include "sendf.h"
  33. #include "multiif.h"
  34. #include "cfilters.h"
  35. #include "connect.h"
  36. #include "progress.h"
  37. #include "transfer.h"
  38. #include "vtls/vtls.h"
  39. #include "curl_ntlm_core.h"
  40. #include "escape.h"
  41. #include "curl_endian.h"
  42. /* The last #include files should be: */
  43. #include "curl_memory.h"
  44. #include "memdebug.h"
  45. /*
  46. * Definitions for SMB protocol data structures
  47. */
  48. #if defined(_MSC_VER) || defined(__ILEC400__)
  49. # define PACK
  50. # pragma pack(push)
  51. # pragma pack(1)
  52. #elif defined(__GNUC__)
  53. # define PACK __attribute__((packed))
  54. #else
  55. # define PACK
  56. #endif
  57. #define SMB_COM_CLOSE 0x04
  58. #define SMB_COM_READ_ANDX 0x2e
  59. #define SMB_COM_WRITE_ANDX 0x2f
  60. #define SMB_COM_TREE_DISCONNECT 0x71
  61. #define SMB_COM_NEGOTIATE 0x72
  62. #define SMB_COM_SETUP_ANDX 0x73
  63. #define SMB_COM_TREE_CONNECT_ANDX 0x75
  64. #define SMB_COM_NT_CREATE_ANDX 0xa2
  65. #define SMB_COM_NO_ANDX_COMMAND 0xff
  66. #define SMB_WC_CLOSE 0x03
  67. #define SMB_WC_READ_ANDX 0x0c
  68. #define SMB_WC_WRITE_ANDX 0x0e
  69. #define SMB_WC_SETUP_ANDX 0x0d
  70. #define SMB_WC_TREE_CONNECT_ANDX 0x04
  71. #define SMB_WC_NT_CREATE_ANDX 0x18
  72. #define SMB_FLAGS_CANONICAL_PATHNAMES 0x10
  73. #define SMB_FLAGS_CASELESS_PATHNAMES 0x08
  74. #define SMB_FLAGS2_UNICODE_STRINGS 0x8000
  75. #define SMB_FLAGS2_IS_LONG_NAME 0x0040
  76. #define SMB_FLAGS2_KNOWS_LONG_NAME 0x0001
  77. #define SMB_CAP_LARGE_FILES 0x08
  78. #define SMB_GENERIC_WRITE 0x40000000
  79. #define SMB_GENERIC_READ 0x80000000
  80. #define SMB_FILE_SHARE_ALL 0x07
  81. #define SMB_FILE_OPEN 0x01
  82. #define SMB_FILE_OVERWRITE_IF 0x05
  83. #define SMB_ERR_NOACCESS 0x00050001
  84. struct smb_header {
  85. unsigned char nbt_type;
  86. unsigned char nbt_flags;
  87. unsigned short nbt_length;
  88. unsigned char magic[4];
  89. unsigned char command;
  90. unsigned int status;
  91. unsigned char flags;
  92. unsigned short flags2;
  93. unsigned short pid_high;
  94. unsigned char signature[8];
  95. unsigned short pad;
  96. unsigned short tid;
  97. unsigned short pid;
  98. unsigned short uid;
  99. unsigned short mid;
  100. } PACK;
  101. struct smb_negotiate_response {
  102. struct smb_header h;
  103. unsigned char word_count;
  104. unsigned short dialect_index;
  105. unsigned char security_mode;
  106. unsigned short max_mpx_count;
  107. unsigned short max_number_vcs;
  108. unsigned int max_buffer_size;
  109. unsigned int max_raw_size;
  110. unsigned int session_key;
  111. unsigned int capabilities;
  112. unsigned int system_time_low;
  113. unsigned int system_time_high;
  114. unsigned short server_time_zone;
  115. unsigned char encryption_key_length;
  116. unsigned short byte_count;
  117. char bytes[1];
  118. } PACK;
  119. struct andx {
  120. unsigned char command;
  121. unsigned char pad;
  122. unsigned short offset;
  123. } PACK;
  124. struct smb_setup {
  125. unsigned char word_count;
  126. struct andx andx;
  127. unsigned short max_buffer_size;
  128. unsigned short max_mpx_count;
  129. unsigned short vc_number;
  130. unsigned int session_key;
  131. unsigned short lengths[2];
  132. unsigned int pad;
  133. unsigned int capabilities;
  134. unsigned short byte_count;
  135. char bytes[1024];
  136. } PACK;
  137. struct smb_tree_connect {
  138. unsigned char word_count;
  139. struct andx andx;
  140. unsigned short flags;
  141. unsigned short pw_len;
  142. unsigned short byte_count;
  143. char bytes[1024];
  144. } PACK;
  145. struct smb_nt_create {
  146. unsigned char word_count;
  147. struct andx andx;
  148. unsigned char pad;
  149. unsigned short name_length;
  150. unsigned int flags;
  151. unsigned int root_fid;
  152. unsigned int access;
  153. curl_off_t allocation_size;
  154. unsigned int ext_file_attributes;
  155. unsigned int share_access;
  156. unsigned int create_disposition;
  157. unsigned int create_options;
  158. unsigned int impersonation_level;
  159. unsigned char security_flags;
  160. unsigned short byte_count;
  161. char bytes[1024];
  162. } PACK;
  163. struct smb_nt_create_response {
  164. struct smb_header h;
  165. unsigned char word_count;
  166. struct andx andx;
  167. unsigned char op_lock_level;
  168. unsigned short fid;
  169. unsigned int create_disposition;
  170. curl_off_t create_time;
  171. curl_off_t last_access_time;
  172. curl_off_t last_write_time;
  173. curl_off_t last_change_time;
  174. unsigned int ext_file_attributes;
  175. curl_off_t allocation_size;
  176. curl_off_t end_of_file;
  177. } PACK;
  178. struct smb_read {
  179. unsigned char word_count;
  180. struct andx andx;
  181. unsigned short fid;
  182. unsigned int offset;
  183. unsigned short max_bytes;
  184. unsigned short min_bytes;
  185. unsigned int timeout;
  186. unsigned short remaining;
  187. unsigned int offset_high;
  188. unsigned short byte_count;
  189. } PACK;
  190. struct smb_write {
  191. struct smb_header h;
  192. unsigned char word_count;
  193. struct andx andx;
  194. unsigned short fid;
  195. unsigned int offset;
  196. unsigned int timeout;
  197. unsigned short write_mode;
  198. unsigned short remaining;
  199. unsigned short pad;
  200. unsigned short data_length;
  201. unsigned short data_offset;
  202. unsigned int offset_high;
  203. unsigned short byte_count;
  204. unsigned char pad2;
  205. } PACK;
  206. struct smb_close {
  207. unsigned char word_count;
  208. unsigned short fid;
  209. unsigned int last_mtime;
  210. unsigned short byte_count;
  211. } PACK;
  212. struct smb_tree_disconnect {
  213. unsigned char word_count;
  214. unsigned short byte_count;
  215. } PACK;
  216. #if defined(_MSC_VER) || defined(__ILEC400__)
  217. # pragma pack(pop)
  218. #endif
  219. /* Local API functions */
  220. static CURLcode smb_setup_connection(struct Curl_easy *data,
  221. struct connectdata *conn);
  222. static CURLcode smb_connect(struct Curl_easy *data, bool *done);
  223. static CURLcode smb_connection_state(struct Curl_easy *data, bool *done);
  224. static CURLcode smb_do(struct Curl_easy *data, bool *done);
  225. static CURLcode smb_request_state(struct Curl_easy *data, bool *done);
  226. static CURLcode smb_disconnect(struct Curl_easy *data,
  227. struct connectdata *conn, bool dead);
  228. static int smb_getsock(struct Curl_easy *data, struct connectdata *conn,
  229. curl_socket_t *socks);
  230. static CURLcode smb_parse_url_path(struct Curl_easy *data,
  231. struct connectdata *conn);
  232. /*
  233. * SMB handler interface
  234. */
  235. const struct Curl_handler Curl_handler_smb = {
  236. "SMB", /* scheme */
  237. smb_setup_connection, /* setup_connection */
  238. smb_do, /* do_it */
  239. ZERO_NULL, /* done */
  240. ZERO_NULL, /* do_more */
  241. smb_connect, /* connect_it */
  242. smb_connection_state, /* connecting */
  243. smb_request_state, /* doing */
  244. smb_getsock, /* proto_getsock */
  245. smb_getsock, /* doing_getsock */
  246. ZERO_NULL, /* domore_getsock */
  247. ZERO_NULL, /* perform_getsock */
  248. smb_disconnect, /* disconnect */
  249. ZERO_NULL, /* write_resp */
  250. ZERO_NULL, /* connection_check */
  251. ZERO_NULL, /* attach connection */
  252. PORT_SMB, /* defport */
  253. CURLPROTO_SMB, /* protocol */
  254. CURLPROTO_SMB, /* family */
  255. PROTOPT_NONE /* flags */
  256. };
  257. #ifdef USE_SSL
  258. /*
  259. * SMBS handler interface
  260. */
  261. const struct Curl_handler Curl_handler_smbs = {
  262. "SMBS", /* scheme */
  263. smb_setup_connection, /* setup_connection */
  264. smb_do, /* do_it */
  265. ZERO_NULL, /* done */
  266. ZERO_NULL, /* do_more */
  267. smb_connect, /* connect_it */
  268. smb_connection_state, /* connecting */
  269. smb_request_state, /* doing */
  270. smb_getsock, /* proto_getsock */
  271. smb_getsock, /* doing_getsock */
  272. ZERO_NULL, /* domore_getsock */
  273. ZERO_NULL, /* perform_getsock */
  274. smb_disconnect, /* disconnect */
  275. ZERO_NULL, /* write_resp */
  276. ZERO_NULL, /* connection_check */
  277. ZERO_NULL, /* attach connection */
  278. PORT_SMBS, /* defport */
  279. CURLPROTO_SMBS, /* protocol */
  280. CURLPROTO_SMB, /* family */
  281. PROTOPT_SSL /* flags */
  282. };
  283. #endif
  284. #define MAX_PAYLOAD_SIZE 0x8000
  285. #define MAX_MESSAGE_SIZE (MAX_PAYLOAD_SIZE + 0x1000)
  286. #define CLIENTNAME "curl"
  287. #define SERVICENAME "?????"
  288. /* Append a string to an SMB message */
  289. #define MSGCAT(str) \
  290. do { \
  291. strcpy(p, (str)); \
  292. p += strlen(str); \
  293. } while(0)
  294. /* Append a null-terminated string to an SMB message */
  295. #define MSGCATNULL(str) \
  296. do { \
  297. strcpy(p, (str)); \
  298. p += strlen(str) + 1; \
  299. } while(0)
  300. /* SMB is mostly little endian */
  301. #if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
  302. defined(__OS400__)
  303. static unsigned short smb_swap16(unsigned short x)
  304. {
  305. return (unsigned short) ((x << 8) | ((x >> 8) & 0xff));
  306. }
  307. static unsigned int smb_swap32(unsigned int x)
  308. {
  309. return (x << 24) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) |
  310. ((x >> 24) & 0xff);
  311. }
  312. static curl_off_t smb_swap64(curl_off_t x)
  313. {
  314. return ((curl_off_t) smb_swap32((unsigned int) x) << 32) |
  315. smb_swap32((unsigned int) (x >> 32));
  316. }
  317. #else
  318. # define smb_swap16(x) (x)
  319. # define smb_swap32(x) (x)
  320. # define smb_swap64(x) (x)
  321. #endif
  322. /* SMB request state */
  323. enum smb_req_state {
  324. SMB_REQUESTING,
  325. SMB_TREE_CONNECT,
  326. SMB_OPEN,
  327. SMB_DOWNLOAD,
  328. SMB_UPLOAD,
  329. SMB_CLOSE,
  330. SMB_TREE_DISCONNECT,
  331. SMB_DONE
  332. };
  333. /* SMB request data */
  334. struct smb_request {
  335. enum smb_req_state state;
  336. char *path;
  337. unsigned short tid; /* Even if we connect to the same tree as another */
  338. unsigned short fid; /* request, the tid will be different */
  339. CURLcode result;
  340. };
  341. static void conn_state(struct Curl_easy *data, enum smb_conn_state newstate)
  342. {
  343. struct smb_conn *smbc = &data->conn->proto.smbc;
  344. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  345. /* For debug purposes */
  346. static const char * const names[] = {
  347. "SMB_NOT_CONNECTED",
  348. "SMB_CONNECTING",
  349. "SMB_NEGOTIATE",
  350. "SMB_SETUP",
  351. "SMB_CONNECTED",
  352. /* LAST */
  353. };
  354. if(smbc->state != newstate)
  355. infof(data, "SMB conn %p state change from %s to %s",
  356. (void *)smbc, names[smbc->state], names[newstate]);
  357. #endif
  358. smbc->state = newstate;
  359. }
  360. static void request_state(struct Curl_easy *data,
  361. enum smb_req_state newstate)
  362. {
  363. struct smb_request *req = data->req.p.smb;
  364. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  365. /* For debug purposes */
  366. static const char * const names[] = {
  367. "SMB_REQUESTING",
  368. "SMB_TREE_CONNECT",
  369. "SMB_OPEN",
  370. "SMB_DOWNLOAD",
  371. "SMB_UPLOAD",
  372. "SMB_CLOSE",
  373. "SMB_TREE_DISCONNECT",
  374. "SMB_DONE",
  375. /* LAST */
  376. };
  377. if(req->state != newstate)
  378. infof(data, "SMB request %p state change from %s to %s",
  379. (void *)req, names[req->state], names[newstate]);
  380. #endif
  381. req->state = newstate;
  382. }
  383. /* this should setup things in the connection, not in the easy
  384. handle */
  385. static CURLcode smb_setup_connection(struct Curl_easy *data,
  386. struct connectdata *conn)
  387. {
  388. struct smb_request *req;
  389. /* Initialize the request state */
  390. data->req.p.smb = req = calloc(1, sizeof(struct smb_request));
  391. if(!req)
  392. return CURLE_OUT_OF_MEMORY;
  393. /* Parse the URL path */
  394. return smb_parse_url_path(data, conn);
  395. }
  396. static CURLcode smb_connect(struct Curl_easy *data, bool *done)
  397. {
  398. struct connectdata *conn = data->conn;
  399. struct smb_conn *smbc = &conn->proto.smbc;
  400. char *slash;
  401. (void) done;
  402. /* Check we have a username and password to authenticate with */
  403. if(!data->state.aptr.user)
  404. return CURLE_LOGIN_DENIED;
  405. /* Initialize the connection state */
  406. smbc->state = SMB_CONNECTING;
  407. smbc->recv_buf = malloc(MAX_MESSAGE_SIZE);
  408. if(!smbc->recv_buf)
  409. return CURLE_OUT_OF_MEMORY;
  410. /* Multiple requests are allowed with this connection */
  411. connkeep(conn, "SMB default");
  412. /* Parse the username, domain, and password */
  413. slash = strchr(conn->user, '/');
  414. if(!slash)
  415. slash = strchr(conn->user, '\\');
  416. if(slash) {
  417. smbc->user = slash + 1;
  418. smbc->domain = strdup(conn->user);
  419. if(!smbc->domain)
  420. return CURLE_OUT_OF_MEMORY;
  421. smbc->domain[slash - conn->user] = 0;
  422. }
  423. else {
  424. smbc->user = conn->user;
  425. smbc->domain = strdup(conn->host.name);
  426. if(!smbc->domain)
  427. return CURLE_OUT_OF_MEMORY;
  428. }
  429. return CURLE_OK;
  430. }
  431. static CURLcode smb_recv_message(struct Curl_easy *data, void **msg)
  432. {
  433. struct connectdata *conn = data->conn;
  434. struct smb_conn *smbc = &conn->proto.smbc;
  435. char *buf = smbc->recv_buf;
  436. ssize_t bytes_read;
  437. size_t nbt_size;
  438. size_t msg_size;
  439. size_t len = MAX_MESSAGE_SIZE - smbc->got;
  440. CURLcode result;
  441. result = Curl_xfer_recv(data, buf + smbc->got, len, &bytes_read);
  442. if(result)
  443. return result;
  444. if(!bytes_read)
  445. return CURLE_OK;
  446. smbc->got += bytes_read;
  447. /* Check for a 32-bit nbt header */
  448. if(smbc->got < sizeof(unsigned int))
  449. return CURLE_OK;
  450. nbt_size = Curl_read16_be((const unsigned char *)
  451. (buf + sizeof(unsigned short))) +
  452. sizeof(unsigned int);
  453. if(smbc->got < nbt_size)
  454. return CURLE_OK;
  455. msg_size = sizeof(struct smb_header);
  456. if(nbt_size >= msg_size + 1) {
  457. /* Add the word count */
  458. msg_size += 1 + ((unsigned char) buf[msg_size]) * sizeof(unsigned short);
  459. if(nbt_size >= msg_size + sizeof(unsigned short)) {
  460. /* Add the byte count */
  461. msg_size += sizeof(unsigned short) +
  462. Curl_read16_le((const unsigned char *)&buf[msg_size]);
  463. if(nbt_size < msg_size)
  464. return CURLE_READ_ERROR;
  465. }
  466. }
  467. *msg = buf;
  468. return CURLE_OK;
  469. }
  470. static void smb_pop_message(struct connectdata *conn)
  471. {
  472. struct smb_conn *smbc = &conn->proto.smbc;
  473. smbc->got = 0;
  474. }
  475. static void smb_format_message(struct Curl_easy *data, struct smb_header *h,
  476. unsigned char cmd, size_t len)
  477. {
  478. struct connectdata *conn = data->conn;
  479. struct smb_conn *smbc = &conn->proto.smbc;
  480. struct smb_request *req = data->req.p.smb;
  481. unsigned int pid;
  482. memset(h, 0, sizeof(*h));
  483. h->nbt_length = htons((unsigned short) (sizeof(*h) - sizeof(unsigned int) +
  484. len));
  485. memcpy((char *)h->magic, "\xffSMB", 4);
  486. h->command = cmd;
  487. h->flags = SMB_FLAGS_CANONICAL_PATHNAMES | SMB_FLAGS_CASELESS_PATHNAMES;
  488. h->flags2 = smb_swap16(SMB_FLAGS2_IS_LONG_NAME | SMB_FLAGS2_KNOWS_LONG_NAME);
  489. h->uid = smb_swap16(smbc->uid);
  490. h->tid = smb_swap16(req->tid);
  491. pid = getpid();
  492. h->pid_high = smb_swap16((unsigned short)(pid >> 16));
  493. h->pid = smb_swap16((unsigned short) pid);
  494. }
  495. static CURLcode smb_send(struct Curl_easy *data, size_t len,
  496. size_t upload_size)
  497. {
  498. struct connectdata *conn = data->conn;
  499. struct smb_conn *smbc = &conn->proto.smbc;
  500. size_t bytes_written;
  501. CURLcode result;
  502. result = Curl_xfer_send(data, data->state.ulbuf, len, &bytes_written);
  503. if(result)
  504. return result;
  505. if(bytes_written != len) {
  506. smbc->send_size = len;
  507. smbc->sent = bytes_written;
  508. }
  509. smbc->upload_size = upload_size;
  510. return CURLE_OK;
  511. }
  512. static CURLcode smb_flush(struct Curl_easy *data)
  513. {
  514. struct connectdata *conn = data->conn;
  515. struct smb_conn *smbc = &conn->proto.smbc;
  516. size_t bytes_written;
  517. size_t len = smbc->send_size - smbc->sent;
  518. CURLcode result;
  519. if(!smbc->send_size)
  520. return CURLE_OK;
  521. result = Curl_xfer_send(data, data->state.ulbuf + smbc->sent, len,
  522. &bytes_written);
  523. if(result)
  524. return result;
  525. if(bytes_written != len)
  526. smbc->sent += bytes_written;
  527. else
  528. smbc->send_size = 0;
  529. return CURLE_OK;
  530. }
  531. static CURLcode smb_send_message(struct Curl_easy *data, unsigned char cmd,
  532. const void *msg, size_t msg_len)
  533. {
  534. CURLcode result = Curl_get_upload_buffer(data);
  535. if(result)
  536. return result;
  537. smb_format_message(data, (struct smb_header *)data->state.ulbuf,
  538. cmd, msg_len);
  539. memcpy(data->state.ulbuf + sizeof(struct smb_header),
  540. msg, msg_len);
  541. return smb_send(data, sizeof(struct smb_header) + msg_len, 0);
  542. }
  543. static CURLcode smb_send_negotiate(struct Curl_easy *data)
  544. {
  545. const char *msg = "\x00\x0c\x00\x02NT LM 0.12";
  546. return smb_send_message(data, SMB_COM_NEGOTIATE, msg, 15);
  547. }
  548. static CURLcode smb_send_setup(struct Curl_easy *data)
  549. {
  550. struct connectdata *conn = data->conn;
  551. struct smb_conn *smbc = &conn->proto.smbc;
  552. struct smb_setup msg;
  553. char *p = msg.bytes;
  554. unsigned char lm_hash[21];
  555. unsigned char lm[24];
  556. unsigned char nt_hash[21];
  557. unsigned char nt[24];
  558. size_t byte_count = sizeof(lm) + sizeof(nt);
  559. byte_count += strlen(smbc->user) + strlen(smbc->domain);
  560. byte_count += strlen(OS) + strlen(CLIENTNAME) + 4; /* 4 null chars */
  561. if(byte_count > sizeof(msg.bytes))
  562. return CURLE_FILESIZE_EXCEEDED;
  563. Curl_ntlm_core_mk_lm_hash(conn->passwd, lm_hash);
  564. Curl_ntlm_core_lm_resp(lm_hash, smbc->challenge, lm);
  565. Curl_ntlm_core_mk_nt_hash(conn->passwd, nt_hash);
  566. Curl_ntlm_core_lm_resp(nt_hash, smbc->challenge, nt);
  567. memset(&msg, 0, sizeof(msg));
  568. msg.word_count = SMB_WC_SETUP_ANDX;
  569. msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
  570. msg.max_buffer_size = smb_swap16(MAX_MESSAGE_SIZE);
  571. msg.max_mpx_count = smb_swap16(1);
  572. msg.vc_number = smb_swap16(1);
  573. msg.session_key = smb_swap32(smbc->session_key);
  574. msg.capabilities = smb_swap32(SMB_CAP_LARGE_FILES);
  575. msg.lengths[0] = smb_swap16(sizeof(lm));
  576. msg.lengths[1] = smb_swap16(sizeof(nt));
  577. memcpy(p, lm, sizeof(lm));
  578. p += sizeof(lm);
  579. memcpy(p, nt, sizeof(nt));
  580. p += sizeof(nt);
  581. MSGCATNULL(smbc->user);
  582. MSGCATNULL(smbc->domain);
  583. MSGCATNULL(OS);
  584. MSGCATNULL(CLIENTNAME);
  585. byte_count = p - msg.bytes;
  586. msg.byte_count = smb_swap16((unsigned short)byte_count);
  587. return smb_send_message(data, SMB_COM_SETUP_ANDX, &msg,
  588. sizeof(msg) - sizeof(msg.bytes) + byte_count);
  589. }
  590. static CURLcode smb_send_tree_connect(struct Curl_easy *data)
  591. {
  592. struct smb_tree_connect msg;
  593. struct connectdata *conn = data->conn;
  594. struct smb_conn *smbc = &conn->proto.smbc;
  595. char *p = msg.bytes;
  596. size_t byte_count = strlen(conn->host.name) + strlen(smbc->share);
  597. byte_count += strlen(SERVICENAME) + 5; /* 2 nulls and 3 backslashes */
  598. if(byte_count > sizeof(msg.bytes))
  599. return CURLE_FILESIZE_EXCEEDED;
  600. memset(&msg, 0, sizeof(msg));
  601. msg.word_count = SMB_WC_TREE_CONNECT_ANDX;
  602. msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
  603. msg.pw_len = 0;
  604. MSGCAT("\\\\");
  605. MSGCAT(conn->host.name);
  606. MSGCAT("\\");
  607. MSGCATNULL(smbc->share);
  608. MSGCATNULL(SERVICENAME); /* Match any type of service */
  609. byte_count = p - msg.bytes;
  610. msg.byte_count = smb_swap16((unsigned short)byte_count);
  611. return smb_send_message(data, SMB_COM_TREE_CONNECT_ANDX, &msg,
  612. sizeof(msg) - sizeof(msg.bytes) + byte_count);
  613. }
  614. static CURLcode smb_send_open(struct Curl_easy *data)
  615. {
  616. struct smb_request *req = data->req.p.smb;
  617. struct smb_nt_create msg;
  618. size_t byte_count;
  619. if((strlen(req->path) + 1) > sizeof(msg.bytes))
  620. return CURLE_FILESIZE_EXCEEDED;
  621. memset(&msg, 0, sizeof(msg));
  622. msg.word_count = SMB_WC_NT_CREATE_ANDX;
  623. msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
  624. byte_count = strlen(req->path);
  625. msg.name_length = smb_swap16((unsigned short)byte_count);
  626. msg.share_access = smb_swap32(SMB_FILE_SHARE_ALL);
  627. if(data->state.upload) {
  628. msg.access = smb_swap32(SMB_GENERIC_READ | SMB_GENERIC_WRITE);
  629. msg.create_disposition = smb_swap32(SMB_FILE_OVERWRITE_IF);
  630. }
  631. else {
  632. msg.access = smb_swap32(SMB_GENERIC_READ);
  633. msg.create_disposition = smb_swap32(SMB_FILE_OPEN);
  634. }
  635. msg.byte_count = smb_swap16((unsigned short) ++byte_count);
  636. strcpy(msg.bytes, req->path);
  637. return smb_send_message(data, SMB_COM_NT_CREATE_ANDX, &msg,
  638. sizeof(msg) - sizeof(msg.bytes) + byte_count);
  639. }
  640. static CURLcode smb_send_close(struct Curl_easy *data)
  641. {
  642. struct smb_request *req = data->req.p.smb;
  643. struct smb_close msg;
  644. memset(&msg, 0, sizeof(msg));
  645. msg.word_count = SMB_WC_CLOSE;
  646. msg.fid = smb_swap16(req->fid);
  647. return smb_send_message(data, SMB_COM_CLOSE, &msg, sizeof(msg));
  648. }
  649. static CURLcode smb_send_tree_disconnect(struct Curl_easy *data)
  650. {
  651. struct smb_tree_disconnect msg;
  652. memset(&msg, 0, sizeof(msg));
  653. return smb_send_message(data, SMB_COM_TREE_DISCONNECT, &msg, sizeof(msg));
  654. }
  655. static CURLcode smb_send_read(struct Curl_easy *data)
  656. {
  657. struct smb_request *req = data->req.p.smb;
  658. curl_off_t offset = data->req.offset;
  659. struct smb_read msg;
  660. memset(&msg, 0, sizeof(msg));
  661. msg.word_count = SMB_WC_READ_ANDX;
  662. msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
  663. msg.fid = smb_swap16(req->fid);
  664. msg.offset = smb_swap32((unsigned int) offset);
  665. msg.offset_high = smb_swap32((unsigned int) (offset >> 32));
  666. msg.min_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
  667. msg.max_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
  668. return smb_send_message(data, SMB_COM_READ_ANDX, &msg, sizeof(msg));
  669. }
  670. static CURLcode smb_send_write(struct Curl_easy *data)
  671. {
  672. struct smb_write *msg;
  673. struct smb_request *req = data->req.p.smb;
  674. curl_off_t offset = data->req.offset;
  675. curl_off_t upload_size = data->req.size - data->req.bytecount;
  676. CURLcode result = Curl_get_upload_buffer(data);
  677. if(result)
  678. return result;
  679. msg = (struct smb_write *)data->state.ulbuf;
  680. if(upload_size >= MAX_PAYLOAD_SIZE - 1) /* There is one byte of padding */
  681. upload_size = MAX_PAYLOAD_SIZE - 1;
  682. memset(msg, 0, sizeof(*msg));
  683. msg->word_count = SMB_WC_WRITE_ANDX;
  684. msg->andx.command = SMB_COM_NO_ANDX_COMMAND;
  685. msg->fid = smb_swap16(req->fid);
  686. msg->offset = smb_swap32((unsigned int) offset);
  687. msg->offset_high = smb_swap32((unsigned int) (offset >> 32));
  688. msg->data_length = smb_swap16((unsigned short) upload_size);
  689. msg->data_offset = smb_swap16(sizeof(*msg) - sizeof(unsigned int));
  690. msg->byte_count = smb_swap16((unsigned short) (upload_size + 1));
  691. smb_format_message(data, &msg->h, SMB_COM_WRITE_ANDX,
  692. sizeof(*msg) - sizeof(msg->h) + (size_t) upload_size);
  693. return smb_send(data, sizeof(*msg), (size_t) upload_size);
  694. }
  695. static CURLcode smb_send_and_recv(struct Curl_easy *data, void **msg)
  696. {
  697. struct connectdata *conn = data->conn;
  698. struct smb_conn *smbc = &conn->proto.smbc;
  699. CURLcode result;
  700. *msg = NULL; /* if it returns early */
  701. /* Check if there is data in the transfer buffer */
  702. if(!smbc->send_size && smbc->upload_size) {
  703. size_t nread = smbc->upload_size > (size_t)data->set.upload_buffer_size ?
  704. (size_t)data->set.upload_buffer_size : smbc->upload_size;
  705. data->req.upload_fromhere = data->state.ulbuf;
  706. result = Curl_fillreadbuffer(data, nread, &nread);
  707. if(result && result != CURLE_AGAIN)
  708. return result;
  709. if(!nread)
  710. return CURLE_OK;
  711. smbc->upload_size -= nread;
  712. smbc->send_size = nread;
  713. smbc->sent = 0;
  714. }
  715. /* Check if there is data to send */
  716. if(smbc->send_size) {
  717. result = smb_flush(data);
  718. if(result)
  719. return result;
  720. }
  721. /* Check if there is still data to be sent */
  722. if(smbc->send_size || smbc->upload_size)
  723. return CURLE_AGAIN;
  724. return smb_recv_message(data, msg);
  725. }
  726. static CURLcode smb_connection_state(struct Curl_easy *data, bool *done)
  727. {
  728. struct connectdata *conn = data->conn;
  729. struct smb_conn *smbc = &conn->proto.smbc;
  730. struct smb_negotiate_response *nrsp;
  731. struct smb_header *h;
  732. CURLcode result;
  733. void *msg = NULL;
  734. if(smbc->state == SMB_CONNECTING) {
  735. #ifdef USE_SSL
  736. if((conn->handler->flags & PROTOPT_SSL)) {
  737. bool ssl_done = FALSE;
  738. result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssl_done);
  739. if(result && result != CURLE_AGAIN)
  740. return result;
  741. if(!ssl_done)
  742. return CURLE_OK;
  743. }
  744. #endif
  745. result = smb_send_negotiate(data);
  746. if(result) {
  747. connclose(conn, "SMB: failed to send negotiate message");
  748. return result;
  749. }
  750. conn_state(data, SMB_NEGOTIATE);
  751. }
  752. /* Send the previous message and check for a response */
  753. result = smb_send_and_recv(data, &msg);
  754. if(result && result != CURLE_AGAIN) {
  755. connclose(conn, "SMB: failed to communicate");
  756. return result;
  757. }
  758. if(!msg)
  759. return CURLE_OK;
  760. h = msg;
  761. switch(smbc->state) {
  762. case SMB_NEGOTIATE:
  763. if((smbc->got < sizeof(*nrsp) + sizeof(smbc->challenge) - 1) ||
  764. h->status) {
  765. connclose(conn, "SMB: negotiation failed");
  766. return CURLE_COULDNT_CONNECT;
  767. }
  768. nrsp = msg;
  769. memcpy(smbc->challenge, nrsp->bytes, sizeof(smbc->challenge));
  770. smbc->session_key = smb_swap32(nrsp->session_key);
  771. result = smb_send_setup(data);
  772. if(result) {
  773. connclose(conn, "SMB: failed to send setup message");
  774. return result;
  775. }
  776. conn_state(data, SMB_SETUP);
  777. break;
  778. case SMB_SETUP:
  779. if(h->status) {
  780. connclose(conn, "SMB: authentication failed");
  781. return CURLE_LOGIN_DENIED;
  782. }
  783. smbc->uid = smb_swap16(h->uid);
  784. conn_state(data, SMB_CONNECTED);
  785. *done = true;
  786. break;
  787. default:
  788. smb_pop_message(conn);
  789. return CURLE_OK; /* ignore */
  790. }
  791. smb_pop_message(conn);
  792. return CURLE_OK;
  793. }
  794. /*
  795. * Convert a timestamp from the Windows world (100 nsec units from 1 Jan 1601)
  796. * to Posix time. Cap the output to fit within a time_t.
  797. */
  798. static void get_posix_time(time_t *out, curl_off_t timestamp)
  799. {
  800. timestamp -= 116444736000000000;
  801. timestamp /= 10000000;
  802. #if SIZEOF_TIME_T < SIZEOF_CURL_OFF_T
  803. if(timestamp > TIME_T_MAX)
  804. *out = TIME_T_MAX;
  805. else if(timestamp < TIME_T_MIN)
  806. *out = TIME_T_MIN;
  807. else
  808. #endif
  809. *out = (time_t) timestamp;
  810. }
  811. static CURLcode smb_request_state(struct Curl_easy *data, bool *done)
  812. {
  813. struct connectdata *conn = data->conn;
  814. struct smb_request *req = data->req.p.smb;
  815. struct smb_header *h;
  816. struct smb_conn *smbc = &conn->proto.smbc;
  817. enum smb_req_state next_state = SMB_DONE;
  818. unsigned short len;
  819. unsigned short off;
  820. CURLcode result;
  821. void *msg = NULL;
  822. const struct smb_nt_create_response *smb_m;
  823. if(data->state.upload && (data->state.infilesize < 0)) {
  824. failf(data, "SMB upload needs to know the size up front");
  825. return CURLE_SEND_ERROR;
  826. }
  827. /* Start the request */
  828. if(req->state == SMB_REQUESTING) {
  829. result = smb_send_tree_connect(data);
  830. if(result) {
  831. connclose(conn, "SMB: failed to send tree connect message");
  832. return result;
  833. }
  834. request_state(data, SMB_TREE_CONNECT);
  835. }
  836. /* Send the previous message and check for a response */
  837. result = smb_send_and_recv(data, &msg);
  838. if(result && result != CURLE_AGAIN) {
  839. connclose(conn, "SMB: failed to communicate");
  840. return result;
  841. }
  842. if(!msg)
  843. return CURLE_OK;
  844. h = msg;
  845. switch(req->state) {
  846. case SMB_TREE_CONNECT:
  847. if(h->status) {
  848. req->result = CURLE_REMOTE_FILE_NOT_FOUND;
  849. if(h->status == smb_swap32(SMB_ERR_NOACCESS))
  850. req->result = CURLE_REMOTE_ACCESS_DENIED;
  851. break;
  852. }
  853. req->tid = smb_swap16(h->tid);
  854. next_state = SMB_OPEN;
  855. break;
  856. case SMB_OPEN:
  857. if(h->status || smbc->got < sizeof(struct smb_nt_create_response)) {
  858. req->result = CURLE_REMOTE_FILE_NOT_FOUND;
  859. if(h->status == smb_swap32(SMB_ERR_NOACCESS))
  860. req->result = CURLE_REMOTE_ACCESS_DENIED;
  861. next_state = SMB_TREE_DISCONNECT;
  862. break;
  863. }
  864. smb_m = (const struct smb_nt_create_response*) msg;
  865. req->fid = smb_swap16(smb_m->fid);
  866. data->req.offset = 0;
  867. if(data->state.upload) {
  868. data->req.size = data->state.infilesize;
  869. Curl_pgrsSetUploadSize(data, data->req.size);
  870. next_state = SMB_UPLOAD;
  871. }
  872. else {
  873. data->req.size = smb_swap64(smb_m->end_of_file);
  874. if(data->req.size < 0) {
  875. req->result = CURLE_WEIRD_SERVER_REPLY;
  876. next_state = SMB_CLOSE;
  877. }
  878. else {
  879. Curl_pgrsSetDownloadSize(data, data->req.size);
  880. if(data->set.get_filetime)
  881. get_posix_time(&data->info.filetime, smb_m->last_change_time);
  882. next_state = SMB_DOWNLOAD;
  883. }
  884. }
  885. break;
  886. case SMB_DOWNLOAD:
  887. if(h->status || smbc->got < sizeof(struct smb_header) + 14) {
  888. req->result = CURLE_RECV_ERROR;
  889. next_state = SMB_CLOSE;
  890. break;
  891. }
  892. len = Curl_read16_le(((const unsigned char *) msg) +
  893. sizeof(struct smb_header) + 11);
  894. off = Curl_read16_le(((const unsigned char *) msg) +
  895. sizeof(struct smb_header) + 13);
  896. if(len > 0) {
  897. if(off + sizeof(unsigned int) + len > smbc->got) {
  898. failf(data, "Invalid input packet");
  899. result = CURLE_RECV_ERROR;
  900. }
  901. else
  902. result = Curl_client_write(data, CLIENTWRITE_BODY,
  903. (char *)msg + off + sizeof(unsigned int),
  904. len);
  905. if(result) {
  906. req->result = result;
  907. next_state = SMB_CLOSE;
  908. break;
  909. }
  910. }
  911. data->req.offset += len;
  912. next_state = (len < MAX_PAYLOAD_SIZE) ? SMB_CLOSE : SMB_DOWNLOAD;
  913. break;
  914. case SMB_UPLOAD:
  915. if(h->status || smbc->got < sizeof(struct smb_header) + 6) {
  916. req->result = CURLE_UPLOAD_FAILED;
  917. next_state = SMB_CLOSE;
  918. break;
  919. }
  920. len = Curl_read16_le(((const unsigned char *) msg) +
  921. sizeof(struct smb_header) + 5);
  922. data->req.bytecount += len;
  923. data->req.offset += len;
  924. Curl_pgrsSetUploadCounter(data, data->req.bytecount);
  925. if(data->req.bytecount >= data->req.size)
  926. next_state = SMB_CLOSE;
  927. else
  928. next_state = SMB_UPLOAD;
  929. break;
  930. case SMB_CLOSE:
  931. /* We don't care if the close failed, proceed to tree disconnect anyway */
  932. next_state = SMB_TREE_DISCONNECT;
  933. break;
  934. case SMB_TREE_DISCONNECT:
  935. next_state = SMB_DONE;
  936. break;
  937. default:
  938. smb_pop_message(conn);
  939. return CURLE_OK; /* ignore */
  940. }
  941. smb_pop_message(conn);
  942. switch(next_state) {
  943. case SMB_OPEN:
  944. result = smb_send_open(data);
  945. break;
  946. case SMB_DOWNLOAD:
  947. result = smb_send_read(data);
  948. break;
  949. case SMB_UPLOAD:
  950. result = smb_send_write(data);
  951. break;
  952. case SMB_CLOSE:
  953. result = smb_send_close(data);
  954. break;
  955. case SMB_TREE_DISCONNECT:
  956. result = smb_send_tree_disconnect(data);
  957. break;
  958. case SMB_DONE:
  959. result = req->result;
  960. *done = true;
  961. break;
  962. default:
  963. break;
  964. }
  965. if(result) {
  966. connclose(conn, "SMB: failed to send message");
  967. return result;
  968. }
  969. request_state(data, next_state);
  970. return CURLE_OK;
  971. }
  972. static CURLcode smb_disconnect(struct Curl_easy *data,
  973. struct connectdata *conn, bool dead)
  974. {
  975. struct smb_conn *smbc = &conn->proto.smbc;
  976. (void) dead;
  977. (void) data;
  978. Curl_safefree(smbc->share);
  979. Curl_safefree(smbc->domain);
  980. Curl_safefree(smbc->recv_buf);
  981. return CURLE_OK;
  982. }
  983. static int smb_getsock(struct Curl_easy *data,
  984. struct connectdata *conn, curl_socket_t *socks)
  985. {
  986. (void)data;
  987. socks[0] = conn->sock[FIRSTSOCKET];
  988. return GETSOCK_READSOCK(0) | GETSOCK_WRITESOCK(0);
  989. }
  990. static CURLcode smb_do(struct Curl_easy *data, bool *done)
  991. {
  992. struct connectdata *conn = data->conn;
  993. struct smb_conn *smbc = &conn->proto.smbc;
  994. *done = FALSE;
  995. if(smbc->share) {
  996. return CURLE_OK;
  997. }
  998. return CURLE_URL_MALFORMAT;
  999. }
  1000. static CURLcode smb_parse_url_path(struct Curl_easy *data,
  1001. struct connectdata *conn)
  1002. {
  1003. struct smb_request *req = data->req.p.smb;
  1004. struct smb_conn *smbc = &conn->proto.smbc;
  1005. char *path;
  1006. char *slash;
  1007. /* URL decode the path */
  1008. CURLcode result = Curl_urldecode(data->state.up.path, 0, &path, NULL,
  1009. REJECT_CTRL);
  1010. if(result)
  1011. return result;
  1012. /* Parse the path for the share */
  1013. smbc->share = strdup((*path == '/' || *path == '\\') ? path + 1 : path);
  1014. free(path);
  1015. if(!smbc->share)
  1016. return CURLE_OUT_OF_MEMORY;
  1017. slash = strchr(smbc->share, '/');
  1018. if(!slash)
  1019. slash = strchr(smbc->share, '\\');
  1020. /* The share must be present */
  1021. if(!slash) {
  1022. Curl_safefree(smbc->share);
  1023. failf(data, "missing share in URL path for SMB");
  1024. return CURLE_URL_MALFORMAT;
  1025. }
  1026. /* Parse the path for the file path converting any forward slashes into
  1027. backslashes */
  1028. *slash++ = 0;
  1029. req->path = slash;
  1030. for(; *slash; slash++) {
  1031. if(*slash == '/')
  1032. *slash = '\\';
  1033. }
  1034. return CURLE_OK;
  1035. }
  1036. #endif /* CURL_DISABLE_SMB && USE_CURL_NTLM_CORE &&
  1037. SIZEOF_CURL_OFF_T > 4 */