2
0

smb.c 29 KB

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