2
0

smb.c 34 KB

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