smb.c 29 KB

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