smb.c 29 KB

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