smb.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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, /* write_resp_hd */
  251. ZERO_NULL, /* connection_check */
  252. ZERO_NULL, /* attach connection */
  253. PORT_SMB, /* defport */
  254. CURLPROTO_SMB, /* protocol */
  255. CURLPROTO_SMB, /* family */
  256. PROTOPT_NONE /* flags */
  257. };
  258. #ifdef USE_SSL
  259. /*
  260. * SMBS handler interface
  261. */
  262. const struct Curl_handler Curl_handler_smbs = {
  263. "smbs", /* scheme */
  264. smb_setup_connection, /* setup_connection */
  265. smb_do, /* do_it */
  266. ZERO_NULL, /* done */
  267. ZERO_NULL, /* do_more */
  268. smb_connect, /* connect_it */
  269. smb_connection_state, /* connecting */
  270. smb_request_state, /* doing */
  271. smb_getsock, /* proto_getsock */
  272. smb_getsock, /* doing_getsock */
  273. ZERO_NULL, /* domore_getsock */
  274. ZERO_NULL, /* perform_getsock */
  275. smb_disconnect, /* disconnect */
  276. ZERO_NULL, /* write_resp */
  277. ZERO_NULL, /* write_resp_hd */
  278. ZERO_NULL, /* connection_check */
  279. ZERO_NULL, /* attach connection */
  280. PORT_SMBS, /* defport */
  281. CURLPROTO_SMBS, /* protocol */
  282. CURLPROTO_SMB, /* family */
  283. PROTOPT_SSL /* flags */
  284. };
  285. #endif
  286. #define MAX_PAYLOAD_SIZE 0x8000
  287. #define MAX_MESSAGE_SIZE (MAX_PAYLOAD_SIZE + 0x1000)
  288. #define CLIENTNAME "curl"
  289. #define SERVICENAME "?????"
  290. /* Append a string to an SMB message */
  291. #define MSGCAT(str) \
  292. do { \
  293. strcpy(p, (str)); \
  294. p += strlen(str); \
  295. } while(0)
  296. /* Append a null-terminated string to an SMB message */
  297. #define MSGCATNULL(str) \
  298. do { \
  299. strcpy(p, (str)); \
  300. p += strlen(str) + 1; \
  301. } while(0)
  302. /* SMB is mostly little endian */
  303. #if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
  304. defined(__OS400__)
  305. static unsigned short smb_swap16(unsigned short x)
  306. {
  307. return (unsigned short) ((x << 8) | ((x >> 8) & 0xff));
  308. }
  309. static unsigned int smb_swap32(unsigned int x)
  310. {
  311. return (x << 24) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) |
  312. ((x >> 24) & 0xff);
  313. }
  314. static curl_off_t smb_swap64(curl_off_t x)
  315. {
  316. return ((curl_off_t) smb_swap32((unsigned int) x) << 32) |
  317. smb_swap32((unsigned int) (x >> 32));
  318. }
  319. #else
  320. # define smb_swap16(x) (x)
  321. # define smb_swap32(x) (x)
  322. # define smb_swap64(x) (x)
  323. #endif
  324. /* SMB request state */
  325. enum smb_req_state {
  326. SMB_REQUESTING,
  327. SMB_TREE_CONNECT,
  328. SMB_OPEN,
  329. SMB_DOWNLOAD,
  330. SMB_UPLOAD,
  331. SMB_CLOSE,
  332. SMB_TREE_DISCONNECT,
  333. SMB_DONE
  334. };
  335. /* SMB request data */
  336. struct smb_request {
  337. enum smb_req_state state;
  338. char *path;
  339. unsigned short tid; /* Even if we connect to the same tree as another */
  340. unsigned short fid; /* request, the tid will be different */
  341. CURLcode result;
  342. };
  343. static void conn_state(struct Curl_easy *data, enum smb_conn_state newstate)
  344. {
  345. struct smb_conn *smbc = &data->conn->proto.smbc;
  346. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  347. /* For debug purposes */
  348. static const char * const names[] = {
  349. "SMB_NOT_CONNECTED",
  350. "SMB_CONNECTING",
  351. "SMB_NEGOTIATE",
  352. "SMB_SETUP",
  353. "SMB_CONNECTED",
  354. /* LAST */
  355. };
  356. if(smbc->state != newstate)
  357. infof(data, "SMB conn %p state change from %s to %s",
  358. (void *)smbc, names[smbc->state], names[newstate]);
  359. #endif
  360. smbc->state = newstate;
  361. }
  362. static void request_state(struct Curl_easy *data,
  363. enum smb_req_state newstate)
  364. {
  365. struct smb_request *req = data->req.p.smb;
  366. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  367. /* For debug purposes */
  368. static const char * const names[] = {
  369. "SMB_REQUESTING",
  370. "SMB_TREE_CONNECT",
  371. "SMB_OPEN",
  372. "SMB_DOWNLOAD",
  373. "SMB_UPLOAD",
  374. "SMB_CLOSE",
  375. "SMB_TREE_DISCONNECT",
  376. "SMB_DONE",
  377. /* LAST */
  378. };
  379. if(req->state != newstate)
  380. infof(data, "SMB request %p state change from %s to %s",
  381. (void *)req, names[req->state], names[newstate]);
  382. #endif
  383. req->state = newstate;
  384. }
  385. /* this should setup things in the connection, not in the easy
  386. handle */
  387. static CURLcode smb_setup_connection(struct Curl_easy *data,
  388. struct connectdata *conn)
  389. {
  390. struct smb_request *req;
  391. /* Initialize the request state */
  392. data->req.p.smb = req = calloc(1, sizeof(struct smb_request));
  393. if(!req)
  394. return CURLE_OUT_OF_MEMORY;
  395. /* Parse the URL path */
  396. return smb_parse_url_path(data, conn);
  397. }
  398. static CURLcode smb_connect(struct Curl_easy *data, bool *done)
  399. {
  400. struct connectdata *conn = data->conn;
  401. struct smb_conn *smbc = &conn->proto.smbc;
  402. char *slash;
  403. (void) done;
  404. /* Check we have a username and password to authenticate with */
  405. if(!data->state.aptr.user)
  406. return CURLE_LOGIN_DENIED;
  407. /* Initialize the connection state */
  408. smbc->state = SMB_CONNECTING;
  409. smbc->recv_buf = malloc(MAX_MESSAGE_SIZE);
  410. if(!smbc->recv_buf)
  411. return CURLE_OUT_OF_MEMORY;
  412. smbc->send_buf = malloc(MAX_MESSAGE_SIZE);
  413. if(!smbc->send_buf)
  414. return CURLE_OUT_OF_MEMORY;
  415. /* Multiple requests are allowed with this connection */
  416. connkeep(conn, "SMB default");
  417. /* Parse the username, domain, and password */
  418. slash = strchr(conn->user, '/');
  419. if(!slash)
  420. slash = strchr(conn->user, '\\');
  421. if(slash) {
  422. smbc->user = slash + 1;
  423. smbc->domain = strdup(conn->user);
  424. if(!smbc->domain)
  425. return CURLE_OUT_OF_MEMORY;
  426. smbc->domain[slash - conn->user] = 0;
  427. }
  428. else {
  429. smbc->user = conn->user;
  430. smbc->domain = strdup(conn->host.name);
  431. if(!smbc->domain)
  432. return CURLE_OUT_OF_MEMORY;
  433. }
  434. return CURLE_OK;
  435. }
  436. static CURLcode smb_recv_message(struct Curl_easy *data, void **msg)
  437. {
  438. struct connectdata *conn = data->conn;
  439. struct smb_conn *smbc = &conn->proto.smbc;
  440. char *buf = smbc->recv_buf;
  441. ssize_t bytes_read;
  442. size_t nbt_size;
  443. size_t msg_size;
  444. size_t len = MAX_MESSAGE_SIZE - smbc->got;
  445. CURLcode result;
  446. result = Curl_xfer_recv(data, buf + smbc->got, len, &bytes_read);
  447. if(result)
  448. return result;
  449. if(!bytes_read)
  450. return CURLE_OK;
  451. smbc->got += bytes_read;
  452. /* Check for a 32-bit nbt header */
  453. if(smbc->got < sizeof(unsigned int))
  454. return CURLE_OK;
  455. nbt_size = Curl_read16_be((const unsigned char *)
  456. (buf + sizeof(unsigned short))) +
  457. sizeof(unsigned int);
  458. if(smbc->got < nbt_size)
  459. return CURLE_OK;
  460. msg_size = sizeof(struct smb_header);
  461. if(nbt_size >= msg_size + 1) {
  462. /* Add the word count */
  463. msg_size += 1 + ((unsigned char) buf[msg_size]) * sizeof(unsigned short);
  464. if(nbt_size >= msg_size + sizeof(unsigned short)) {
  465. /* Add the byte count */
  466. msg_size += sizeof(unsigned short) +
  467. Curl_read16_le((const unsigned char *)&buf[msg_size]);
  468. if(nbt_size < msg_size)
  469. return CURLE_READ_ERROR;
  470. }
  471. }
  472. *msg = buf;
  473. return CURLE_OK;
  474. }
  475. static void smb_pop_message(struct connectdata *conn)
  476. {
  477. struct smb_conn *smbc = &conn->proto.smbc;
  478. smbc->got = 0;
  479. }
  480. static void smb_format_message(struct Curl_easy *data, struct smb_header *h,
  481. unsigned char cmd, size_t len)
  482. {
  483. struct connectdata *conn = data->conn;
  484. struct smb_conn *smbc = &conn->proto.smbc;
  485. struct smb_request *req = data->req.p.smb;
  486. unsigned int pid;
  487. memset(h, 0, sizeof(*h));
  488. h->nbt_length = htons((unsigned short) (sizeof(*h) - sizeof(unsigned int) +
  489. len));
  490. memcpy((char *)h->magic, "\xffSMB", 4);
  491. h->command = cmd;
  492. h->flags = SMB_FLAGS_CANONICAL_PATHNAMES | SMB_FLAGS_CASELESS_PATHNAMES;
  493. h->flags2 = smb_swap16(SMB_FLAGS2_IS_LONG_NAME | SMB_FLAGS2_KNOWS_LONG_NAME);
  494. h->uid = smb_swap16(smbc->uid);
  495. h->tid = smb_swap16(req->tid);
  496. pid = (unsigned int)getpid();
  497. h->pid_high = smb_swap16((unsigned short)(pid >> 16));
  498. h->pid = smb_swap16((unsigned short) pid);
  499. }
  500. static CURLcode smb_send(struct Curl_easy *data, size_t len,
  501. size_t upload_size)
  502. {
  503. struct connectdata *conn = data->conn;
  504. struct smb_conn *smbc = &conn->proto.smbc;
  505. size_t bytes_written;
  506. CURLcode result;
  507. result = Curl_xfer_send(data, smbc->send_buf, len, FALSE, &bytes_written);
  508. if(result)
  509. return result;
  510. if(bytes_written != len) {
  511. smbc->send_size = len;
  512. smbc->sent = bytes_written;
  513. }
  514. smbc->upload_size = upload_size;
  515. return CURLE_OK;
  516. }
  517. static CURLcode smb_flush(struct Curl_easy *data)
  518. {
  519. struct connectdata *conn = data->conn;
  520. struct smb_conn *smbc = &conn->proto.smbc;
  521. size_t bytes_written;
  522. size_t len = smbc->send_size - smbc->sent;
  523. CURLcode result;
  524. if(!smbc->send_size)
  525. return CURLE_OK;
  526. result = Curl_xfer_send(data, smbc->send_buf + smbc->sent, len, FALSE,
  527. &bytes_written);
  528. if(result)
  529. return result;
  530. if(bytes_written != len)
  531. smbc->sent += bytes_written;
  532. else
  533. smbc->send_size = 0;
  534. return CURLE_OK;
  535. }
  536. static CURLcode smb_send_message(struct Curl_easy *data, unsigned char cmd,
  537. const void *msg, size_t msg_len)
  538. {
  539. struct connectdata *conn = data->conn;
  540. struct smb_conn *smbc = &conn->proto.smbc;
  541. smb_format_message(data, (struct smb_header *)smbc->send_buf,
  542. cmd, msg_len);
  543. DEBUGASSERT((sizeof(struct smb_header) + msg_len) <= MAX_MESSAGE_SIZE);
  544. memcpy(smbc->send_buf + sizeof(struct smb_header), msg, msg_len);
  545. return smb_send(data, sizeof(struct smb_header) + msg_len, 0);
  546. }
  547. static CURLcode smb_send_negotiate(struct Curl_easy *data)
  548. {
  549. const char *msg = "\x00\x0c\x00\x02NT LM 0.12";
  550. return smb_send_message(data, SMB_COM_NEGOTIATE, msg, 15);
  551. }
  552. static CURLcode smb_send_setup(struct Curl_easy *data)
  553. {
  554. struct connectdata *conn = data->conn;
  555. struct smb_conn *smbc = &conn->proto.smbc;
  556. struct smb_setup msg;
  557. char *p = msg.bytes;
  558. unsigned char lm_hash[21];
  559. unsigned char lm[24];
  560. unsigned char nt_hash[21];
  561. unsigned char nt[24];
  562. const size_t byte_count = sizeof(lm) + sizeof(nt) +
  563. strlen(smbc->user) + strlen(smbc->domain) +
  564. strlen(OS) + strlen(CLIENTNAME) + 4; /* 4 null chars */
  565. if(byte_count > sizeof(msg.bytes))
  566. return CURLE_FILESIZE_EXCEEDED;
  567. Curl_ntlm_core_mk_lm_hash(conn->passwd, lm_hash);
  568. Curl_ntlm_core_lm_resp(lm_hash, smbc->challenge, lm);
  569. Curl_ntlm_core_mk_nt_hash(conn->passwd, nt_hash);
  570. Curl_ntlm_core_lm_resp(nt_hash, smbc->challenge, nt);
  571. memset(&msg, 0, sizeof(msg) - sizeof(msg.bytes));
  572. msg.word_count = SMB_WC_SETUP_ANDX;
  573. msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
  574. msg.max_buffer_size = smb_swap16(MAX_MESSAGE_SIZE);
  575. msg.max_mpx_count = smb_swap16(1);
  576. msg.vc_number = smb_swap16(1);
  577. msg.session_key = smb_swap32(smbc->session_key);
  578. msg.capabilities = smb_swap32(SMB_CAP_LARGE_FILES);
  579. msg.lengths[0] = smb_swap16(sizeof(lm));
  580. msg.lengths[1] = smb_swap16(sizeof(nt));
  581. memcpy(p, lm, sizeof(lm));
  582. p += sizeof(lm);
  583. memcpy(p, nt, sizeof(nt));
  584. p += sizeof(nt);
  585. MSGCATNULL(smbc->user);
  586. MSGCATNULL(smbc->domain);
  587. MSGCATNULL(OS);
  588. MSGCATNULL(CLIENTNAME);
  589. DEBUGASSERT(byte_count == (size_t)(p - msg.bytes));
  590. msg.byte_count = smb_swap16((unsigned short)byte_count);
  591. return smb_send_message(data, SMB_COM_SETUP_ANDX, &msg,
  592. sizeof(msg) - sizeof(msg.bytes) + byte_count);
  593. }
  594. static CURLcode smb_send_tree_connect(struct Curl_easy *data)
  595. {
  596. struct smb_tree_connect msg;
  597. struct connectdata *conn = data->conn;
  598. struct smb_conn *smbc = &conn->proto.smbc;
  599. char *p = msg.bytes;
  600. const size_t byte_count = strlen(conn->host.name) + strlen(smbc->share) +
  601. strlen(SERVICENAME) + 5; /* 2 nulls and 3 backslashes */
  602. if(byte_count > sizeof(msg.bytes))
  603. return CURLE_FILESIZE_EXCEEDED;
  604. memset(&msg, 0, sizeof(msg) - sizeof(msg.bytes));
  605. msg.word_count = SMB_WC_TREE_CONNECT_ANDX;
  606. msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
  607. msg.pw_len = 0;
  608. MSGCAT("\\\\");
  609. MSGCAT(conn->host.name);
  610. MSGCAT("\\");
  611. MSGCATNULL(smbc->share);
  612. MSGCATNULL(SERVICENAME); /* Match any type of service */
  613. DEBUGASSERT(byte_count == (size_t)(p - msg.bytes));
  614. msg.byte_count = smb_swap16((unsigned short)byte_count);
  615. return smb_send_message(data, SMB_COM_TREE_CONNECT_ANDX, &msg,
  616. sizeof(msg) - sizeof(msg.bytes) + byte_count);
  617. }
  618. static CURLcode smb_send_open(struct Curl_easy *data)
  619. {
  620. struct smb_request *req = data->req.p.smb;
  621. struct smb_nt_create msg;
  622. const size_t byte_count = strlen(req->path) + 1;
  623. if(byte_count > sizeof(msg.bytes))
  624. return CURLE_FILESIZE_EXCEEDED;
  625. memset(&msg, 0, sizeof(msg) - sizeof(msg.bytes));
  626. msg.word_count = SMB_WC_NT_CREATE_ANDX;
  627. msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
  628. msg.name_length = smb_swap16((unsigned short)(byte_count - 1));
  629. msg.share_access = smb_swap32(SMB_FILE_SHARE_ALL);
  630. if(data->state.upload) {
  631. msg.access = smb_swap32(SMB_GENERIC_READ | SMB_GENERIC_WRITE);
  632. msg.create_disposition = smb_swap32(SMB_FILE_OVERWRITE_IF);
  633. }
  634. else {
  635. msg.access = smb_swap32(SMB_GENERIC_READ);
  636. msg.create_disposition = smb_swap32(SMB_FILE_OPEN);
  637. }
  638. msg.byte_count = smb_swap16((unsigned short) byte_count);
  639. strcpy(msg.bytes, req->path);
  640. return smb_send_message(data, SMB_COM_NT_CREATE_ANDX, &msg,
  641. sizeof(msg) - sizeof(msg.bytes) + byte_count);
  642. }
  643. static CURLcode smb_send_close(struct Curl_easy *data)
  644. {
  645. struct smb_request *req = data->req.p.smb;
  646. struct smb_close msg;
  647. memset(&msg, 0, sizeof(msg));
  648. msg.word_count = SMB_WC_CLOSE;
  649. msg.fid = smb_swap16(req->fid);
  650. return smb_send_message(data, SMB_COM_CLOSE, &msg, sizeof(msg));
  651. }
  652. static CURLcode smb_send_tree_disconnect(struct Curl_easy *data)
  653. {
  654. struct smb_tree_disconnect msg;
  655. memset(&msg, 0, sizeof(msg));
  656. return smb_send_message(data, SMB_COM_TREE_DISCONNECT, &msg, sizeof(msg));
  657. }
  658. static CURLcode smb_send_read(struct Curl_easy *data)
  659. {
  660. struct smb_request *req = data->req.p.smb;
  661. curl_off_t offset = data->req.offset;
  662. struct smb_read msg;
  663. memset(&msg, 0, sizeof(msg));
  664. msg.word_count = SMB_WC_READ_ANDX;
  665. msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
  666. msg.fid = smb_swap16(req->fid);
  667. msg.offset = smb_swap32((unsigned int) offset);
  668. msg.offset_high = smb_swap32((unsigned int) (offset >> 32));
  669. msg.min_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
  670. msg.max_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
  671. return smb_send_message(data, SMB_COM_READ_ANDX, &msg, sizeof(msg));
  672. }
  673. static CURLcode smb_send_write(struct Curl_easy *data)
  674. {
  675. struct connectdata *conn = data->conn;
  676. struct smb_conn *smbc = &conn->proto.smbc;
  677. struct smb_write *msg;
  678. struct smb_request *req = data->req.p.smb;
  679. curl_off_t offset = data->req.offset;
  680. curl_off_t upload_size = data->req.size - data->req.bytecount;
  681. msg = (struct smb_write *)smbc->send_buf;
  682. if(upload_size >= MAX_PAYLOAD_SIZE - 1) /* There is one byte of padding */
  683. upload_size = MAX_PAYLOAD_SIZE - 1;
  684. memset(msg, 0, sizeof(*msg));
  685. msg->word_count = SMB_WC_WRITE_ANDX;
  686. msg->andx.command = SMB_COM_NO_ANDX_COMMAND;
  687. msg->fid = smb_swap16(req->fid);
  688. msg->offset = smb_swap32((unsigned int) offset);
  689. msg->offset_high = smb_swap32((unsigned int) (offset >> 32));
  690. msg->data_length = smb_swap16((unsigned short) upload_size);
  691. msg->data_offset = smb_swap16(sizeof(*msg) - sizeof(unsigned int));
  692. msg->byte_count = smb_swap16((unsigned short) (upload_size + 1));
  693. smb_format_message(data, &msg->h, SMB_COM_WRITE_ANDX,
  694. sizeof(*msg) - sizeof(msg->h) + (size_t) upload_size);
  695. return smb_send(data, sizeof(*msg), (size_t) upload_size);
  696. }
  697. static CURLcode smb_send_and_recv(struct Curl_easy *data, void **msg)
  698. {
  699. struct connectdata *conn = data->conn;
  700. struct smb_conn *smbc = &conn->proto.smbc;
  701. CURLcode result;
  702. *msg = NULL; /* if it returns early */
  703. /* Check if there is data in the transfer buffer */
  704. if(!smbc->send_size && smbc->upload_size) {
  705. size_t nread = smbc->upload_size > (size_t)MAX_MESSAGE_SIZE ?
  706. (size_t)MAX_MESSAGE_SIZE : smbc->upload_size;
  707. bool eos;
  708. result = Curl_client_read(data, smbc->send_buf, nread, &nread, &eos);
  709. if(result && result != CURLE_AGAIN)
  710. return result;
  711. if(!nread)
  712. return CURLE_OK;
  713. smbc->upload_size -= nread;
  714. smbc->send_size = nread;
  715. smbc->sent = 0;
  716. }
  717. /* Check if there is data to send */
  718. if(smbc->send_size) {
  719. result = smb_flush(data);
  720. if(result)
  721. return result;
  722. }
  723. /* Check if there is still data to be sent */
  724. if(smbc->send_size || smbc->upload_size)
  725. return CURLE_AGAIN;
  726. return smb_recv_message(data, msg);
  727. }
  728. static CURLcode smb_connection_state(struct Curl_easy *data, bool *done)
  729. {
  730. struct connectdata *conn = data->conn;
  731. struct smb_conn *smbc = &conn->proto.smbc;
  732. struct smb_negotiate_response *nrsp;
  733. struct smb_header *h;
  734. CURLcode result;
  735. void *msg = NULL;
  736. if(smbc->state == SMB_CONNECTING) {
  737. #ifdef USE_SSL
  738. if((conn->handler->flags & PROTOPT_SSL)) {
  739. bool ssl_done = FALSE;
  740. result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssl_done);
  741. if(result && result != CURLE_AGAIN)
  742. return result;
  743. if(!ssl_done)
  744. return CURLE_OK;
  745. }
  746. #endif
  747. result = smb_send_negotiate(data);
  748. if(result) {
  749. connclose(conn, "SMB: failed to send negotiate message");
  750. return result;
  751. }
  752. conn_state(data, SMB_NEGOTIATE);
  753. }
  754. /* Send the previous message and check for a response */
  755. result = smb_send_and_recv(data, &msg);
  756. if(result && result != CURLE_AGAIN) {
  757. connclose(conn, "SMB: failed to communicate");
  758. return result;
  759. }
  760. if(!msg)
  761. return CURLE_OK;
  762. h = msg;
  763. switch(smbc->state) {
  764. case SMB_NEGOTIATE:
  765. if((smbc->got < sizeof(*nrsp) + sizeof(smbc->challenge) - 1) ||
  766. h->status) {
  767. connclose(conn, "SMB: negotiation failed");
  768. return CURLE_COULDNT_CONNECT;
  769. }
  770. nrsp = msg;
  771. memcpy(smbc->challenge, nrsp->bytes, sizeof(smbc->challenge));
  772. smbc->session_key = smb_swap32(nrsp->session_key);
  773. result = smb_send_setup(data);
  774. if(result) {
  775. connclose(conn, "SMB: failed to send setup message");
  776. return result;
  777. }
  778. conn_state(data, SMB_SETUP);
  779. break;
  780. case SMB_SETUP:
  781. if(h->status) {
  782. connclose(conn, "SMB: authentication failed");
  783. return CURLE_LOGIN_DENIED;
  784. }
  785. smbc->uid = smb_swap16(h->uid);
  786. conn_state(data, SMB_CONNECTED);
  787. *done = true;
  788. break;
  789. default:
  790. smb_pop_message(conn);
  791. return CURLE_OK; /* ignore */
  792. }
  793. smb_pop_message(conn);
  794. return CURLE_OK;
  795. }
  796. /*
  797. * Convert a timestamp from the Windows world (100 nsec units from 1 Jan 1601)
  798. * to POSIX time. Cap the output to fit within a time_t.
  799. */
  800. static void get_posix_time(time_t *out, curl_off_t timestamp)
  801. {
  802. timestamp -= 116444736000000000;
  803. timestamp /= 10000000;
  804. #if SIZEOF_TIME_T < SIZEOF_CURL_OFF_T
  805. if(timestamp > TIME_T_MAX)
  806. *out = TIME_T_MAX;
  807. else if(timestamp < TIME_T_MIN)
  808. *out = TIME_T_MIN;
  809. else
  810. #endif
  811. *out = (time_t) timestamp;
  812. }
  813. static CURLcode smb_request_state(struct Curl_easy *data, bool *done)
  814. {
  815. struct connectdata *conn = data->conn;
  816. struct smb_request *req = data->req.p.smb;
  817. struct smb_header *h;
  818. struct smb_conn *smbc = &conn->proto.smbc;
  819. enum smb_req_state next_state = SMB_DONE;
  820. unsigned short len;
  821. unsigned short off;
  822. CURLcode result;
  823. void *msg = NULL;
  824. const struct smb_nt_create_response *smb_m;
  825. if(data->state.upload && (data->state.infilesize < 0)) {
  826. failf(data, "SMB upload needs to know the size up front");
  827. return CURLE_SEND_ERROR;
  828. }
  829. /* Start the request */
  830. if(req->state == SMB_REQUESTING) {
  831. result = smb_send_tree_connect(data);
  832. if(result) {
  833. connclose(conn, "SMB: failed to send tree connect message");
  834. return result;
  835. }
  836. request_state(data, SMB_TREE_CONNECT);
  837. }
  838. /* Send the previous message and check for a response */
  839. result = smb_send_and_recv(data, &msg);
  840. if(result && result != CURLE_AGAIN) {
  841. connclose(conn, "SMB: failed to communicate");
  842. return result;
  843. }
  844. if(!msg)
  845. return CURLE_OK;
  846. h = msg;
  847. switch(req->state) {
  848. case SMB_TREE_CONNECT:
  849. if(h->status) {
  850. req->result = CURLE_REMOTE_FILE_NOT_FOUND;
  851. if(h->status == smb_swap32(SMB_ERR_NOACCESS))
  852. req->result = CURLE_REMOTE_ACCESS_DENIED;
  853. break;
  854. }
  855. req->tid = smb_swap16(h->tid);
  856. next_state = SMB_OPEN;
  857. break;
  858. case SMB_OPEN:
  859. if(h->status || smbc->got < sizeof(struct smb_nt_create_response)) {
  860. req->result = CURLE_REMOTE_FILE_NOT_FOUND;
  861. if(h->status == smb_swap32(SMB_ERR_NOACCESS))
  862. req->result = CURLE_REMOTE_ACCESS_DENIED;
  863. next_state = SMB_TREE_DISCONNECT;
  864. break;
  865. }
  866. smb_m = (const struct smb_nt_create_response*) msg;
  867. req->fid = smb_swap16(smb_m->fid);
  868. data->req.offset = 0;
  869. if(data->state.upload) {
  870. data->req.size = data->state.infilesize;
  871. Curl_pgrsSetUploadSize(data, data->req.size);
  872. next_state = SMB_UPLOAD;
  873. }
  874. else {
  875. data->req.size = smb_swap64(smb_m->end_of_file);
  876. if(data->req.size < 0) {
  877. req->result = CURLE_WEIRD_SERVER_REPLY;
  878. next_state = SMB_CLOSE;
  879. }
  880. else {
  881. Curl_pgrsSetDownloadSize(data, data->req.size);
  882. if(data->set.get_filetime)
  883. get_posix_time(&data->info.filetime, smb_m->last_change_time);
  884. next_state = SMB_DOWNLOAD;
  885. }
  886. }
  887. break;
  888. case SMB_DOWNLOAD:
  889. if(h->status || smbc->got < sizeof(struct smb_header) + 14) {
  890. req->result = CURLE_RECV_ERROR;
  891. next_state = SMB_CLOSE;
  892. break;
  893. }
  894. len = Curl_read16_le(((const unsigned char *) msg) +
  895. sizeof(struct smb_header) + 11);
  896. off = Curl_read16_le(((const unsigned char *) msg) +
  897. sizeof(struct smb_header) + 13);
  898. if(len > 0) {
  899. if(off + sizeof(unsigned int) + len > smbc->got) {
  900. failf(data, "Invalid input packet");
  901. result = CURLE_RECV_ERROR;
  902. }
  903. else
  904. result = Curl_client_write(data, CLIENTWRITE_BODY,
  905. (char *)msg + off + sizeof(unsigned int),
  906. len);
  907. if(result) {
  908. req->result = result;
  909. next_state = SMB_CLOSE;
  910. break;
  911. }
  912. }
  913. data->req.offset += len;
  914. next_state = (len < MAX_PAYLOAD_SIZE) ? SMB_CLOSE : SMB_DOWNLOAD;
  915. break;
  916. case SMB_UPLOAD:
  917. if(h->status || smbc->got < sizeof(struct smb_header) + 6) {
  918. req->result = CURLE_UPLOAD_FAILED;
  919. next_state = SMB_CLOSE;
  920. break;
  921. }
  922. len = Curl_read16_le(((const unsigned char *) msg) +
  923. sizeof(struct smb_header) + 5);
  924. data->req.bytecount += len;
  925. data->req.offset += len;
  926. Curl_pgrsSetUploadCounter(data, data->req.bytecount);
  927. if(data->req.bytecount >= data->req.size)
  928. next_state = SMB_CLOSE;
  929. else
  930. next_state = SMB_UPLOAD;
  931. break;
  932. case SMB_CLOSE:
  933. /* We do not care if the close failed, proceed to tree disconnect anyway */
  934. next_state = SMB_TREE_DISCONNECT;
  935. break;
  936. case SMB_TREE_DISCONNECT:
  937. next_state = SMB_DONE;
  938. break;
  939. default:
  940. smb_pop_message(conn);
  941. return CURLE_OK; /* ignore */
  942. }
  943. smb_pop_message(conn);
  944. switch(next_state) {
  945. case SMB_OPEN:
  946. result = smb_send_open(data);
  947. break;
  948. case SMB_DOWNLOAD:
  949. result = smb_send_read(data);
  950. break;
  951. case SMB_UPLOAD:
  952. result = smb_send_write(data);
  953. break;
  954. case SMB_CLOSE:
  955. result = smb_send_close(data);
  956. break;
  957. case SMB_TREE_DISCONNECT:
  958. result = smb_send_tree_disconnect(data);
  959. break;
  960. case SMB_DONE:
  961. result = req->result;
  962. *done = true;
  963. break;
  964. default:
  965. break;
  966. }
  967. if(result) {
  968. connclose(conn, "SMB: failed to send message");
  969. return result;
  970. }
  971. request_state(data, next_state);
  972. return CURLE_OK;
  973. }
  974. static CURLcode smb_disconnect(struct Curl_easy *data,
  975. struct connectdata *conn, bool dead)
  976. {
  977. struct smb_conn *smbc = &conn->proto.smbc;
  978. (void) dead;
  979. (void) data;
  980. Curl_safefree(smbc->share);
  981. Curl_safefree(smbc->domain);
  982. Curl_safefree(smbc->recv_buf);
  983. Curl_safefree(smbc->send_buf);
  984. return CURLE_OK;
  985. }
  986. static int smb_getsock(struct Curl_easy *data,
  987. struct connectdata *conn, curl_socket_t *socks)
  988. {
  989. (void)data;
  990. socks[0] = conn->sock[FIRSTSOCKET];
  991. return GETSOCK_READSOCK(0) | GETSOCK_WRITESOCK(0);
  992. }
  993. static CURLcode smb_do(struct Curl_easy *data, bool *done)
  994. {
  995. struct connectdata *conn = data->conn;
  996. struct smb_conn *smbc = &conn->proto.smbc;
  997. *done = FALSE;
  998. if(smbc->share) {
  999. return CURLE_OK;
  1000. }
  1001. return CURLE_URL_MALFORMAT;
  1002. }
  1003. static CURLcode smb_parse_url_path(struct Curl_easy *data,
  1004. struct connectdata *conn)
  1005. {
  1006. struct smb_request *req = data->req.p.smb;
  1007. struct smb_conn *smbc = &conn->proto.smbc;
  1008. char *path;
  1009. char *slash;
  1010. /* URL decode the path */
  1011. CURLcode result = Curl_urldecode(data->state.up.path, 0, &path, NULL,
  1012. REJECT_CTRL);
  1013. if(result)
  1014. return result;
  1015. /* Parse the path for the share */
  1016. smbc->share = strdup((*path == '/' || *path == '\\') ? path + 1 : path);
  1017. free(path);
  1018. if(!smbc->share)
  1019. return CURLE_OUT_OF_MEMORY;
  1020. slash = strchr(smbc->share, '/');
  1021. if(!slash)
  1022. slash = strchr(smbc->share, '\\');
  1023. /* The share must be present */
  1024. if(!slash) {
  1025. Curl_safefree(smbc->share);
  1026. failf(data, "missing share in URL path for SMB");
  1027. return CURLE_URL_MALFORMAT;
  1028. }
  1029. /* Parse the path for the file path converting any forward slashes into
  1030. backslashes */
  1031. *slash++ = 0;
  1032. req->path = slash;
  1033. for(; *slash; slash++) {
  1034. if(*slash == '/')
  1035. *slash = '\\';
  1036. }
  1037. return CURLE_OK;
  1038. }
  1039. #endif /* CURL_DISABLE_SMB && USE_CURL_NTLM_CORE &&
  1040. SIZEOF_CURL_OFF_T > 4 */