smb.c 27 KB

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