smb.c 29 KB

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