2
0

smb.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2016 - 2021, 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(!conn->bits.user_passwd)
  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. struct smb_conn *smbc = &conn->proto.smbc;
  267. char *buf = smbc->recv_buf;
  268. ssize_t bytes_read;
  269. size_t nbt_size;
  270. size_t msg_size;
  271. size_t len = MAX_MESSAGE_SIZE - smbc->got;
  272. CURLcode result;
  273. result = Curl_read(data, FIRSTSOCKET, buf + smbc->got, len, &bytes_read);
  274. if(result)
  275. return result;
  276. if(!bytes_read)
  277. return CURLE_OK;
  278. smbc->got += bytes_read;
  279. /* Check for a 32-bit nbt header */
  280. if(smbc->got < sizeof(unsigned int))
  281. return CURLE_OK;
  282. nbt_size = Curl_read16_be((const unsigned char *)
  283. (buf + sizeof(unsigned short))) +
  284. sizeof(unsigned int);
  285. if(smbc->got < nbt_size)
  286. return CURLE_OK;
  287. msg_size = sizeof(struct smb_header);
  288. if(nbt_size >= msg_size + 1) {
  289. /* Add the word count */
  290. msg_size += 1 + ((unsigned char) buf[msg_size]) * sizeof(unsigned short);
  291. if(nbt_size >= msg_size + sizeof(unsigned short)) {
  292. /* Add the byte count */
  293. msg_size += sizeof(unsigned short) +
  294. Curl_read16_le((const unsigned char *)&buf[msg_size]);
  295. if(nbt_size < msg_size)
  296. return CURLE_READ_ERROR;
  297. }
  298. }
  299. *msg = buf;
  300. return CURLE_OK;
  301. }
  302. static void smb_pop_message(struct connectdata *conn)
  303. {
  304. struct smb_conn *smbc = &conn->proto.smbc;
  305. smbc->got = 0;
  306. }
  307. static void smb_format_message(struct Curl_easy *data, struct smb_header *h,
  308. unsigned char cmd, size_t len)
  309. {
  310. struct connectdata *conn = data->conn;
  311. struct smb_conn *smbc = &conn->proto.smbc;
  312. struct smb_request *req = data->req.p.smb;
  313. unsigned int pid;
  314. memset(h, 0, sizeof(*h));
  315. h->nbt_length = htons((unsigned short) (sizeof(*h) - sizeof(unsigned int) +
  316. len));
  317. memcpy((char *)h->magic, "\xffSMB", 4);
  318. h->command = cmd;
  319. h->flags = SMB_FLAGS_CANONICAL_PATHNAMES | SMB_FLAGS_CASELESS_PATHNAMES;
  320. h->flags2 = smb_swap16(SMB_FLAGS2_IS_LONG_NAME | SMB_FLAGS2_KNOWS_LONG_NAME);
  321. h->uid = smb_swap16(smbc->uid);
  322. h->tid = smb_swap16(req->tid);
  323. pid = getpid();
  324. h->pid_high = smb_swap16((unsigned short)(pid >> 16));
  325. h->pid = smb_swap16((unsigned short) pid);
  326. }
  327. static CURLcode smb_send(struct Curl_easy *data, ssize_t len,
  328. size_t upload_size)
  329. {
  330. struct connectdata *conn = data->conn;
  331. struct smb_conn *smbc = &conn->proto.smbc;
  332. ssize_t bytes_written;
  333. CURLcode result;
  334. result = Curl_write(data, FIRSTSOCKET, data->state.ulbuf,
  335. len, &bytes_written);
  336. if(result)
  337. return result;
  338. if(bytes_written != len) {
  339. smbc->send_size = len;
  340. smbc->sent = bytes_written;
  341. }
  342. smbc->upload_size = upload_size;
  343. return CURLE_OK;
  344. }
  345. static CURLcode smb_flush(struct Curl_easy *data)
  346. {
  347. struct connectdata *conn = data->conn;
  348. struct smb_conn *smbc = &conn->proto.smbc;
  349. ssize_t bytes_written;
  350. ssize_t len = smbc->send_size - smbc->sent;
  351. CURLcode result;
  352. if(!smbc->send_size)
  353. return CURLE_OK;
  354. result = Curl_write(data, FIRSTSOCKET,
  355. data->state.ulbuf + smbc->sent,
  356. len, &bytes_written);
  357. if(result)
  358. return result;
  359. if(bytes_written != len)
  360. smbc->sent += bytes_written;
  361. else
  362. smbc->send_size = 0;
  363. return CURLE_OK;
  364. }
  365. static CURLcode smb_send_message(struct Curl_easy *data, unsigned char cmd,
  366. const void *msg, size_t msg_len)
  367. {
  368. CURLcode result = Curl_get_upload_buffer(data);
  369. if(result)
  370. return result;
  371. smb_format_message(data, (struct smb_header *)data->state.ulbuf,
  372. cmd, msg_len);
  373. memcpy(data->state.ulbuf + sizeof(struct smb_header),
  374. msg, msg_len);
  375. return smb_send(data, sizeof(struct smb_header) + msg_len, 0);
  376. }
  377. static CURLcode smb_send_negotiate(struct Curl_easy *data)
  378. {
  379. const char *msg = "\x00\x0c\x00\x02NT LM 0.12";
  380. return smb_send_message(data, SMB_COM_NEGOTIATE, msg, 15);
  381. }
  382. static CURLcode smb_send_setup(struct Curl_easy *data)
  383. {
  384. struct connectdata *conn = data->conn;
  385. struct smb_conn *smbc = &conn->proto.smbc;
  386. struct smb_setup msg;
  387. char *p = msg.bytes;
  388. unsigned char lm_hash[21];
  389. unsigned char lm[24];
  390. unsigned char nt_hash[21];
  391. unsigned char nt[24];
  392. size_t byte_count = sizeof(lm) + sizeof(nt);
  393. byte_count += strlen(smbc->user) + strlen(smbc->domain);
  394. byte_count += strlen(OS) + strlen(CLIENTNAME) + 4; /* 4 null chars */
  395. if(byte_count > sizeof(msg.bytes))
  396. return CURLE_FILESIZE_EXCEEDED;
  397. Curl_ntlm_core_mk_lm_hash(data, conn->passwd, lm_hash);
  398. Curl_ntlm_core_lm_resp(lm_hash, smbc->challenge, lm);
  399. #ifdef USE_NTRESPONSES
  400. Curl_ntlm_core_mk_nt_hash(data, conn->passwd, nt_hash);
  401. Curl_ntlm_core_lm_resp(nt_hash, smbc->challenge, nt);
  402. #else
  403. memset(nt, 0, sizeof(nt));
  404. #endif
  405. memset(&msg, 0, sizeof(msg));
  406. msg.word_count = SMB_WC_SETUP_ANDX;
  407. msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
  408. msg.max_buffer_size = smb_swap16(MAX_MESSAGE_SIZE);
  409. msg.max_mpx_count = smb_swap16(1);
  410. msg.vc_number = smb_swap16(1);
  411. msg.session_key = smb_swap32(smbc->session_key);
  412. msg.capabilities = smb_swap32(SMB_CAP_LARGE_FILES);
  413. msg.lengths[0] = smb_swap16(sizeof(lm));
  414. msg.lengths[1] = smb_swap16(sizeof(nt));
  415. memcpy(p, lm, sizeof(lm));
  416. p += sizeof(lm);
  417. memcpy(p, nt, sizeof(nt));
  418. p += sizeof(nt);
  419. MSGCATNULL(smbc->user);
  420. MSGCATNULL(smbc->domain);
  421. MSGCATNULL(OS);
  422. MSGCATNULL(CLIENTNAME);
  423. byte_count = p - msg.bytes;
  424. msg.byte_count = smb_swap16((unsigned short)byte_count);
  425. return smb_send_message(data, SMB_COM_SETUP_ANDX, &msg,
  426. sizeof(msg) - sizeof(msg.bytes) + byte_count);
  427. }
  428. static CURLcode smb_send_tree_connect(struct Curl_easy *data)
  429. {
  430. struct smb_tree_connect msg;
  431. struct connectdata *conn = data->conn;
  432. struct smb_conn *smbc = &conn->proto.smbc;
  433. char *p = msg.bytes;
  434. size_t byte_count = strlen(conn->host.name) + strlen(smbc->share);
  435. byte_count += strlen(SERVICENAME) + 5; /* 2 nulls and 3 backslashes */
  436. if(byte_count > sizeof(msg.bytes))
  437. return CURLE_FILESIZE_EXCEEDED;
  438. memset(&msg, 0, sizeof(msg));
  439. msg.word_count = SMB_WC_TREE_CONNECT_ANDX;
  440. msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
  441. msg.pw_len = 0;
  442. MSGCAT("\\\\");
  443. MSGCAT(conn->host.name);
  444. MSGCAT("\\");
  445. MSGCATNULL(smbc->share);
  446. MSGCATNULL(SERVICENAME); /* Match any type of service */
  447. byte_count = p - msg.bytes;
  448. msg.byte_count = smb_swap16((unsigned short)byte_count);
  449. return smb_send_message(data, SMB_COM_TREE_CONNECT_ANDX, &msg,
  450. sizeof(msg) - sizeof(msg.bytes) + byte_count);
  451. }
  452. static CURLcode smb_send_open(struct Curl_easy *data)
  453. {
  454. struct smb_request *req = data->req.p.smb;
  455. struct smb_nt_create msg;
  456. size_t byte_count;
  457. if((strlen(req->path) + 1) > sizeof(msg.bytes))
  458. return CURLE_FILESIZE_EXCEEDED;
  459. memset(&msg, 0, sizeof(msg));
  460. msg.word_count = SMB_WC_NT_CREATE_ANDX;
  461. msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
  462. byte_count = strlen(req->path);
  463. msg.name_length = smb_swap16((unsigned short)byte_count);
  464. msg.share_access = smb_swap32(SMB_FILE_SHARE_ALL);
  465. if(data->set.upload) {
  466. msg.access = smb_swap32(SMB_GENERIC_READ | SMB_GENERIC_WRITE);
  467. msg.create_disposition = smb_swap32(SMB_FILE_OVERWRITE_IF);
  468. }
  469. else {
  470. msg.access = smb_swap32(SMB_GENERIC_READ);
  471. msg.create_disposition = smb_swap32(SMB_FILE_OPEN);
  472. }
  473. msg.byte_count = smb_swap16((unsigned short) ++byte_count);
  474. strcpy(msg.bytes, req->path);
  475. return smb_send_message(data, SMB_COM_NT_CREATE_ANDX, &msg,
  476. sizeof(msg) - sizeof(msg.bytes) + byte_count);
  477. }
  478. static CURLcode smb_send_close(struct Curl_easy *data)
  479. {
  480. struct smb_request *req = data->req.p.smb;
  481. struct smb_close msg;
  482. memset(&msg, 0, sizeof(msg));
  483. msg.word_count = SMB_WC_CLOSE;
  484. msg.fid = smb_swap16(req->fid);
  485. return smb_send_message(data, SMB_COM_CLOSE, &msg, sizeof(msg));
  486. }
  487. static CURLcode smb_send_tree_disconnect(struct Curl_easy *data)
  488. {
  489. struct smb_tree_disconnect msg;
  490. memset(&msg, 0, sizeof(msg));
  491. return smb_send_message(data, SMB_COM_TREE_DISCONNECT, &msg, sizeof(msg));
  492. }
  493. static CURLcode smb_send_read(struct Curl_easy *data)
  494. {
  495. struct smb_request *req = data->req.p.smb;
  496. curl_off_t offset = data->req.offset;
  497. struct smb_read msg;
  498. memset(&msg, 0, sizeof(msg));
  499. msg.word_count = SMB_WC_READ_ANDX;
  500. msg.andx.command = SMB_COM_NO_ANDX_COMMAND;
  501. msg.fid = smb_swap16(req->fid);
  502. msg.offset = smb_swap32((unsigned int) offset);
  503. msg.offset_high = smb_swap32((unsigned int) (offset >> 32));
  504. msg.min_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
  505. msg.max_bytes = smb_swap16(MAX_PAYLOAD_SIZE);
  506. return smb_send_message(data, SMB_COM_READ_ANDX, &msg, sizeof(msg));
  507. }
  508. static CURLcode smb_send_write(struct Curl_easy *data)
  509. {
  510. struct smb_write *msg;
  511. struct smb_request *req = data->req.p.smb;
  512. curl_off_t offset = data->req.offset;
  513. curl_off_t upload_size = data->req.size - data->req.bytecount;
  514. CURLcode result = Curl_get_upload_buffer(data);
  515. if(result)
  516. return result;
  517. msg = (struct smb_write *)data->state.ulbuf;
  518. if(upload_size >= MAX_PAYLOAD_SIZE - 1) /* There is one byte of padding */
  519. upload_size = MAX_PAYLOAD_SIZE - 1;
  520. memset(msg, 0, sizeof(*msg));
  521. msg->word_count = SMB_WC_WRITE_ANDX;
  522. msg->andx.command = SMB_COM_NO_ANDX_COMMAND;
  523. msg->fid = smb_swap16(req->fid);
  524. msg->offset = smb_swap32((unsigned int) offset);
  525. msg->offset_high = smb_swap32((unsigned int) (offset >> 32));
  526. msg->data_length = smb_swap16((unsigned short) upload_size);
  527. msg->data_offset = smb_swap16(sizeof(*msg) - sizeof(unsigned int));
  528. msg->byte_count = smb_swap16((unsigned short) (upload_size + 1));
  529. smb_format_message(data, &msg->h, SMB_COM_WRITE_ANDX,
  530. sizeof(*msg) - sizeof(msg->h) + (size_t) upload_size);
  531. return smb_send(data, sizeof(*msg), (size_t) upload_size);
  532. }
  533. static CURLcode smb_send_and_recv(struct Curl_easy *data, void **msg)
  534. {
  535. struct connectdata *conn = data->conn;
  536. struct smb_conn *smbc = &conn->proto.smbc;
  537. CURLcode result;
  538. *msg = NULL; /* if it returns early */
  539. /* Check if there is data in the transfer buffer */
  540. if(!smbc->send_size && smbc->upload_size) {
  541. size_t nread = smbc->upload_size > (size_t)data->set.upload_buffer_size ?
  542. (size_t)data->set.upload_buffer_size : smbc->upload_size;
  543. data->req.upload_fromhere = data->state.ulbuf;
  544. result = Curl_fillreadbuffer(data, nread, &nread);
  545. if(result && result != CURLE_AGAIN)
  546. return result;
  547. if(!nread)
  548. return CURLE_OK;
  549. smbc->upload_size -= nread;
  550. smbc->send_size = nread;
  551. smbc->sent = 0;
  552. }
  553. /* Check if there is data to send */
  554. if(smbc->send_size) {
  555. result = smb_flush(data);
  556. if(result)
  557. return result;
  558. }
  559. /* Check if there is still data to be sent */
  560. if(smbc->send_size || smbc->upload_size)
  561. return CURLE_AGAIN;
  562. return smb_recv_message(data, msg);
  563. }
  564. static CURLcode smb_connection_state(struct Curl_easy *data, bool *done)
  565. {
  566. struct connectdata *conn = data->conn;
  567. struct smb_conn *smbc = &conn->proto.smbc;
  568. struct smb_negotiate_response *nrsp;
  569. struct smb_header *h;
  570. CURLcode result;
  571. void *msg = NULL;
  572. if(smbc->state == SMB_CONNECTING) {
  573. #ifdef USE_SSL
  574. if((conn->handler->flags & PROTOPT_SSL)) {
  575. bool ssl_done = FALSE;
  576. result = Curl_ssl_connect_nonblocking(data, conn, FALSE,
  577. FIRSTSOCKET, &ssl_done);
  578. if(result && result != CURLE_AGAIN)
  579. return result;
  580. if(!ssl_done)
  581. return CURLE_OK;
  582. }
  583. #endif
  584. result = smb_send_negotiate(data);
  585. if(result) {
  586. connclose(conn, "SMB: failed to send negotiate message");
  587. return result;
  588. }
  589. conn_state(data, SMB_NEGOTIATE);
  590. }
  591. /* Send the previous message and check for a response */
  592. result = smb_send_and_recv(data, &msg);
  593. if(result && result != CURLE_AGAIN) {
  594. connclose(conn, "SMB: failed to communicate");
  595. return result;
  596. }
  597. if(!msg)
  598. return CURLE_OK;
  599. h = msg;
  600. switch(smbc->state) {
  601. case SMB_NEGOTIATE:
  602. if((smbc->got < sizeof(*nrsp) + sizeof(smbc->challenge) - 1) ||
  603. h->status) {
  604. connclose(conn, "SMB: negotiation failed");
  605. return CURLE_COULDNT_CONNECT;
  606. }
  607. nrsp = msg;
  608. memcpy(smbc->challenge, nrsp->bytes, sizeof(smbc->challenge));
  609. smbc->session_key = smb_swap32(nrsp->session_key);
  610. result = smb_send_setup(data);
  611. if(result) {
  612. connclose(conn, "SMB: failed to send setup message");
  613. return result;
  614. }
  615. conn_state(data, SMB_SETUP);
  616. break;
  617. case SMB_SETUP:
  618. if(h->status) {
  619. connclose(conn, "SMB: authentication failed");
  620. return CURLE_LOGIN_DENIED;
  621. }
  622. smbc->uid = smb_swap16(h->uid);
  623. conn_state(data, SMB_CONNECTED);
  624. *done = true;
  625. break;
  626. default:
  627. smb_pop_message(conn);
  628. return CURLE_OK; /* ignore */
  629. }
  630. smb_pop_message(conn);
  631. return CURLE_OK;
  632. }
  633. /*
  634. * Convert a timestamp from the Windows world (100 nsec units from 1 Jan 1601)
  635. * to Posix time. Cap the output to fit within a time_t.
  636. */
  637. static void get_posix_time(time_t *out, curl_off_t timestamp)
  638. {
  639. timestamp -= 116444736000000000;
  640. timestamp /= 10000000;
  641. #if SIZEOF_TIME_T < SIZEOF_CURL_OFF_T
  642. if(timestamp > TIME_T_MAX)
  643. *out = TIME_T_MAX;
  644. else if(timestamp < TIME_T_MIN)
  645. *out = TIME_T_MIN;
  646. else
  647. #endif
  648. *out = (time_t) timestamp;
  649. }
  650. static CURLcode smb_request_state(struct Curl_easy *data, bool *done)
  651. {
  652. struct connectdata *conn = data->conn;
  653. struct smb_request *req = data->req.p.smb;
  654. struct smb_header *h;
  655. struct smb_conn *smbc = &conn->proto.smbc;
  656. enum smb_req_state next_state = SMB_DONE;
  657. unsigned short len;
  658. unsigned short off;
  659. CURLcode result;
  660. void *msg = NULL;
  661. const struct smb_nt_create_response *smb_m;
  662. /* Start the request */
  663. if(req->state == SMB_REQUESTING) {
  664. result = smb_send_tree_connect(data);
  665. if(result) {
  666. connclose(conn, "SMB: failed to send tree connect message");
  667. return result;
  668. }
  669. request_state(data, SMB_TREE_CONNECT);
  670. }
  671. /* Send the previous message and check for a response */
  672. result = smb_send_and_recv(data, &msg);
  673. if(result && result != CURLE_AGAIN) {
  674. connclose(conn, "SMB: failed to communicate");
  675. return result;
  676. }
  677. if(!msg)
  678. return CURLE_OK;
  679. h = msg;
  680. switch(req->state) {
  681. case SMB_TREE_CONNECT:
  682. if(h->status) {
  683. req->result = CURLE_REMOTE_FILE_NOT_FOUND;
  684. if(h->status == smb_swap32(SMB_ERR_NOACCESS))
  685. req->result = CURLE_REMOTE_ACCESS_DENIED;
  686. break;
  687. }
  688. req->tid = smb_swap16(h->tid);
  689. next_state = SMB_OPEN;
  690. break;
  691. case SMB_OPEN:
  692. if(h->status || smbc->got < sizeof(struct smb_nt_create_response)) {
  693. req->result = CURLE_REMOTE_FILE_NOT_FOUND;
  694. if(h->status == smb_swap32(SMB_ERR_NOACCESS))
  695. req->result = CURLE_REMOTE_ACCESS_DENIED;
  696. next_state = SMB_TREE_DISCONNECT;
  697. break;
  698. }
  699. smb_m = (const struct smb_nt_create_response*) msg;
  700. req->fid = smb_swap16(smb_m->fid);
  701. data->req.offset = 0;
  702. if(data->set.upload) {
  703. data->req.size = data->state.infilesize;
  704. Curl_pgrsSetUploadSize(data, data->req.size);
  705. next_state = SMB_UPLOAD;
  706. }
  707. else {
  708. smb_m = (const struct smb_nt_create_response*) msg;
  709. data->req.size = smb_swap64(smb_m->end_of_file);
  710. if(data->req.size < 0) {
  711. req->result = CURLE_WEIRD_SERVER_REPLY;
  712. next_state = SMB_CLOSE;
  713. }
  714. else {
  715. Curl_pgrsSetDownloadSize(data, data->req.size);
  716. if(data->set.get_filetime)
  717. get_posix_time(&data->info.filetime, smb_m->last_change_time);
  718. next_state = SMB_DOWNLOAD;
  719. }
  720. }
  721. break;
  722. case SMB_DOWNLOAD:
  723. if(h->status || smbc->got < sizeof(struct smb_header) + 14) {
  724. req->result = CURLE_RECV_ERROR;
  725. next_state = SMB_CLOSE;
  726. break;
  727. }
  728. len = Curl_read16_le(((const unsigned char *) msg) +
  729. sizeof(struct smb_header) + 11);
  730. off = Curl_read16_le(((const unsigned char *) msg) +
  731. sizeof(struct smb_header) + 13);
  732. if(len > 0) {
  733. if(off + sizeof(unsigned int) + len > smbc->got) {
  734. failf(data, "Invalid input packet");
  735. result = CURLE_RECV_ERROR;
  736. }
  737. else
  738. result = Curl_client_write(data, CLIENTWRITE_BODY,
  739. (char *)msg + off + sizeof(unsigned int),
  740. len);
  741. if(result) {
  742. req->result = result;
  743. next_state = SMB_CLOSE;
  744. break;
  745. }
  746. }
  747. data->req.bytecount += len;
  748. data->req.offset += len;
  749. Curl_pgrsSetDownloadCounter(data, data->req.bytecount);
  750. next_state = (len < MAX_PAYLOAD_SIZE) ? SMB_CLOSE : SMB_DOWNLOAD;
  751. break;
  752. case SMB_UPLOAD:
  753. if(h->status || smbc->got < sizeof(struct smb_header) + 6) {
  754. req->result = CURLE_UPLOAD_FAILED;
  755. next_state = SMB_CLOSE;
  756. break;
  757. }
  758. len = Curl_read16_le(((const unsigned char *) msg) +
  759. sizeof(struct smb_header) + 5);
  760. data->req.bytecount += len;
  761. data->req.offset += len;
  762. Curl_pgrsSetUploadCounter(data, data->req.bytecount);
  763. if(data->req.bytecount >= data->req.size)
  764. next_state = SMB_CLOSE;
  765. else
  766. next_state = SMB_UPLOAD;
  767. break;
  768. case SMB_CLOSE:
  769. /* We don't care if the close failed, proceed to tree disconnect anyway */
  770. next_state = SMB_TREE_DISCONNECT;
  771. break;
  772. case SMB_TREE_DISCONNECT:
  773. next_state = SMB_DONE;
  774. break;
  775. default:
  776. smb_pop_message(conn);
  777. return CURLE_OK; /* ignore */
  778. }
  779. smb_pop_message(conn);
  780. switch(next_state) {
  781. case SMB_OPEN:
  782. result = smb_send_open(data);
  783. break;
  784. case SMB_DOWNLOAD:
  785. result = smb_send_read(data);
  786. break;
  787. case SMB_UPLOAD:
  788. result = smb_send_write(data);
  789. break;
  790. case SMB_CLOSE:
  791. result = smb_send_close(data);
  792. break;
  793. case SMB_TREE_DISCONNECT:
  794. result = smb_send_tree_disconnect(data);
  795. break;
  796. case SMB_DONE:
  797. result = req->result;
  798. *done = true;
  799. break;
  800. default:
  801. break;
  802. }
  803. if(result) {
  804. connclose(conn, "SMB: failed to send message");
  805. return result;
  806. }
  807. request_state(data, next_state);
  808. return CURLE_OK;
  809. }
  810. static CURLcode smb_done(struct Curl_easy *data, CURLcode status,
  811. bool premature)
  812. {
  813. (void) premature;
  814. Curl_safefree(data->req.p.smb);
  815. return status;
  816. }
  817. static CURLcode smb_disconnect(struct Curl_easy *data,
  818. struct connectdata *conn, bool dead)
  819. {
  820. struct smb_conn *smbc = &conn->proto.smbc;
  821. (void) dead;
  822. (void) data;
  823. Curl_safefree(smbc->share);
  824. Curl_safefree(smbc->domain);
  825. Curl_safefree(smbc->recv_buf);
  826. return CURLE_OK;
  827. }
  828. static int smb_getsock(struct Curl_easy *data,
  829. struct connectdata *conn, curl_socket_t *socks)
  830. {
  831. (void)data;
  832. socks[0] = conn->sock[FIRSTSOCKET];
  833. return GETSOCK_READSOCK(0) | GETSOCK_WRITESOCK(0);
  834. }
  835. static CURLcode smb_do(struct Curl_easy *data, bool *done)
  836. {
  837. struct connectdata *conn = data->conn;
  838. struct smb_conn *smbc = &conn->proto.smbc;
  839. *done = FALSE;
  840. if(smbc->share) {
  841. return CURLE_OK;
  842. }
  843. return CURLE_URL_MALFORMAT;
  844. }
  845. static CURLcode smb_parse_url_path(struct Curl_easy *data,
  846. struct connectdata *conn)
  847. {
  848. struct smb_request *req = data->req.p.smb;
  849. struct smb_conn *smbc = &conn->proto.smbc;
  850. char *path;
  851. char *slash;
  852. /* URL decode the path */
  853. CURLcode result = Curl_urldecode(data, data->state.up.path, 0, &path, NULL,
  854. REJECT_CTRL);
  855. if(result)
  856. return result;
  857. /* Parse the path for the share */
  858. smbc->share = strdup((*path == '/' || *path == '\\') ? path + 1 : path);
  859. free(path);
  860. if(!smbc->share)
  861. return CURLE_OUT_OF_MEMORY;
  862. slash = strchr(smbc->share, '/');
  863. if(!slash)
  864. slash = strchr(smbc->share, '\\');
  865. /* The share must be present */
  866. if(!slash) {
  867. Curl_safefree(smbc->share);
  868. return CURLE_URL_MALFORMAT;
  869. }
  870. /* Parse the path for the file path converting any forward slashes into
  871. backslashes */
  872. *slash++ = 0;
  873. req->path = slash;
  874. for(; *slash; slash++) {
  875. if(*slash == '/')
  876. *slash = '\\';
  877. }
  878. return CURLE_OK;
  879. }
  880. #endif /* CURL_DISABLE_SMB && USE_CURL_NTLM_CORE &&
  881. SIZEOF_CURL_OFF_T > 4 */