smb.c 28 KB

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