smb.c 29 KB

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