2
0

smb.c 29 KB

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