smb.c 28 KB

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