smb.c 37 KB

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