smb.c 34 KB

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