2
0

smb.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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->set.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->set.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->set.upload) {
  700. data->req.size = data->state.infilesize;
  701. Curl_pgrsSetUploadSize(data, data->req.size);
  702. next_state = SMB_UPLOAD;
  703. }
  704. else {
  705. smb_m = (const struct smb_nt_create_response*) msg;
  706. data->req.size = smb_swap64(smb_m->end_of_file);
  707. if(data->req.size < 0) {
  708. req->result = CURLE_WEIRD_SERVER_REPLY;
  709. next_state = SMB_CLOSE;
  710. }
  711. else {
  712. Curl_pgrsSetDownloadSize(data, data->req.size);
  713. if(data->set.get_filetime)
  714. get_posix_time(&data->info.filetime, smb_m->last_change_time);
  715. next_state = SMB_DOWNLOAD;
  716. }
  717. }
  718. break;
  719. case SMB_DOWNLOAD:
  720. if(h->status || smbc->got < sizeof(struct smb_header) + 14) {
  721. req->result = CURLE_RECV_ERROR;
  722. next_state = SMB_CLOSE;
  723. break;
  724. }
  725. len = Curl_read16_le(((const unsigned char *) msg) +
  726. sizeof(struct smb_header) + 11);
  727. off = Curl_read16_le(((const unsigned char *) msg) +
  728. sizeof(struct smb_header) + 13);
  729. if(len > 0) {
  730. if(off + sizeof(unsigned int) + len > smbc->got) {
  731. failf(data, "Invalid input packet");
  732. result = CURLE_RECV_ERROR;
  733. }
  734. else
  735. result = Curl_client_write(data, CLIENTWRITE_BODY,
  736. (char *)msg + off + sizeof(unsigned int),
  737. len);
  738. if(result) {
  739. req->result = result;
  740. next_state = SMB_CLOSE;
  741. break;
  742. }
  743. }
  744. data->req.bytecount += len;
  745. data->req.offset += len;
  746. Curl_pgrsSetDownloadCounter(data, data->req.bytecount);
  747. next_state = (len < MAX_PAYLOAD_SIZE) ? SMB_CLOSE : SMB_DOWNLOAD;
  748. break;
  749. case SMB_UPLOAD:
  750. if(h->status || smbc->got < sizeof(struct smb_header) + 6) {
  751. req->result = CURLE_UPLOAD_FAILED;
  752. next_state = SMB_CLOSE;
  753. break;
  754. }
  755. len = Curl_read16_le(((const unsigned char *) msg) +
  756. sizeof(struct smb_header) + 5);
  757. data->req.bytecount += len;
  758. data->req.offset += len;
  759. Curl_pgrsSetUploadCounter(data, data->req.bytecount);
  760. if(data->req.bytecount >= data->req.size)
  761. next_state = SMB_CLOSE;
  762. else
  763. next_state = SMB_UPLOAD;
  764. break;
  765. case SMB_CLOSE:
  766. /* We don't care if the close failed, proceed to tree disconnect anyway */
  767. next_state = SMB_TREE_DISCONNECT;
  768. break;
  769. case SMB_TREE_DISCONNECT:
  770. next_state = SMB_DONE;
  771. break;
  772. default:
  773. smb_pop_message(conn);
  774. return CURLE_OK; /* ignore */
  775. }
  776. smb_pop_message(conn);
  777. switch(next_state) {
  778. case SMB_OPEN:
  779. result = smb_send_open(data);
  780. break;
  781. case SMB_DOWNLOAD:
  782. result = smb_send_read(data);
  783. break;
  784. case SMB_UPLOAD:
  785. result = smb_send_write(data);
  786. break;
  787. case SMB_CLOSE:
  788. result = smb_send_close(data);
  789. break;
  790. case SMB_TREE_DISCONNECT:
  791. result = smb_send_tree_disconnect(data);
  792. break;
  793. case SMB_DONE:
  794. result = req->result;
  795. *done = true;
  796. break;
  797. default:
  798. break;
  799. }
  800. if(result) {
  801. connclose(conn, "SMB: failed to send message");
  802. return result;
  803. }
  804. request_state(data, next_state);
  805. return CURLE_OK;
  806. }
  807. static CURLcode smb_disconnect(struct Curl_easy *data,
  808. struct connectdata *conn, bool dead)
  809. {
  810. struct smb_conn *smbc = &conn->proto.smbc;
  811. (void) dead;
  812. (void) data;
  813. Curl_safefree(smbc->share);
  814. Curl_safefree(smbc->domain);
  815. Curl_safefree(smbc->recv_buf);
  816. return CURLE_OK;
  817. }
  818. static int smb_getsock(struct Curl_easy *data,
  819. struct connectdata *conn, curl_socket_t *socks)
  820. {
  821. (void)data;
  822. socks[0] = conn->sock[FIRSTSOCKET];
  823. return GETSOCK_READSOCK(0) | GETSOCK_WRITESOCK(0);
  824. }
  825. static CURLcode smb_do(struct Curl_easy *data, bool *done)
  826. {
  827. struct connectdata *conn = data->conn;
  828. struct smb_conn *smbc = &conn->proto.smbc;
  829. *done = FALSE;
  830. if(smbc->share) {
  831. return CURLE_OK;
  832. }
  833. return CURLE_URL_MALFORMAT;
  834. }
  835. static CURLcode smb_parse_url_path(struct Curl_easy *data,
  836. struct connectdata *conn)
  837. {
  838. struct smb_request *req = data->req.p.smb;
  839. struct smb_conn *smbc = &conn->proto.smbc;
  840. char *path;
  841. char *slash;
  842. /* URL decode the path */
  843. CURLcode result = Curl_urldecode(data->state.up.path, 0, &path, NULL,
  844. REJECT_CTRL);
  845. if(result)
  846. return result;
  847. /* Parse the path for the share */
  848. smbc->share = strdup((*path == '/' || *path == '\\') ? path + 1 : path);
  849. free(path);
  850. if(!smbc->share)
  851. return CURLE_OUT_OF_MEMORY;
  852. slash = strchr(smbc->share, '/');
  853. if(!slash)
  854. slash = strchr(smbc->share, '\\');
  855. /* The share must be present */
  856. if(!slash) {
  857. Curl_safefree(smbc->share);
  858. failf(data, "missing share in URL path for SMB");
  859. return CURLE_URL_MALFORMAT;
  860. }
  861. /* Parse the path for the file path converting any forward slashes into
  862. backslashes */
  863. *slash++ = 0;
  864. req->path = slash;
  865. for(; *slash; slash++) {
  866. if(*slash == '/')
  867. *slash = '\\';
  868. }
  869. return CURLE_OK;
  870. }
  871. #endif /* CURL_DISABLE_SMB && USE_CURL_NTLM_CORE &&
  872. SIZEOF_CURL_OFF_T > 4 */