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