2
0

curl_ntlm_wb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
  24. defined(NTLM_WB_ENABLED)
  25. /*
  26. * NTLM details:
  27. *
  28. * https://davenport.sourceforge.io/ntlm.html
  29. * https://www.innovation.ch/java/ntlm.html
  30. */
  31. #define DEBUG_ME 0
  32. #ifdef HAVE_SYS_WAIT_H
  33. #include <sys/wait.h>
  34. #endif
  35. #ifdef HAVE_SIGNAL_H
  36. #include <signal.h>
  37. #endif
  38. #ifdef HAVE_PWD_H
  39. #include <pwd.h>
  40. #endif
  41. #include "urldata.h"
  42. #include "sendf.h"
  43. #include "select.h"
  44. #include "vauth/ntlm.h"
  45. #include "curl_ntlm_core.h"
  46. #include "curl_ntlm_wb.h"
  47. #include "url.h"
  48. #include "strerror.h"
  49. #include "strdup.h"
  50. #include "strcase.h"
  51. /* The last 3 #include files should be in this order */
  52. #include "curl_printf.h"
  53. #include "curl_memory.h"
  54. #include "memdebug.h"
  55. #if DEBUG_ME
  56. # define DEBUG_OUT(x) x
  57. #else
  58. # define DEBUG_OUT(x) Curl_nop_stmt
  59. #endif
  60. /* Portable 'sclose_nolog' used only in child process instead of 'sclose'
  61. to avoid fooling the socket leak detector */
  62. #if defined(HAVE_CLOSESOCKET)
  63. # define sclose_nolog(x) closesocket((x))
  64. #elif defined(HAVE_CLOSESOCKET_CAMEL)
  65. # define sclose_nolog(x) CloseSocket((x))
  66. #else
  67. # define sclose_nolog(x) close((x))
  68. #endif
  69. static void ntlm_wb_cleanup(struct ntlmdata *ntlm)
  70. {
  71. if(ntlm->ntlm_auth_hlpr_socket != CURL_SOCKET_BAD) {
  72. sclose(ntlm->ntlm_auth_hlpr_socket);
  73. ntlm->ntlm_auth_hlpr_socket = CURL_SOCKET_BAD;
  74. }
  75. if(ntlm->ntlm_auth_hlpr_pid) {
  76. int i;
  77. for(i = 0; i < 4; i++) {
  78. pid_t ret = waitpid(ntlm->ntlm_auth_hlpr_pid, NULL, WNOHANG);
  79. if(ret == ntlm->ntlm_auth_hlpr_pid || errno == ECHILD)
  80. break;
  81. switch(i) {
  82. case 0:
  83. kill(ntlm->ntlm_auth_hlpr_pid, SIGTERM);
  84. break;
  85. case 1:
  86. /* Give the process another moment to shut down cleanly before
  87. bringing down the axe */
  88. Curl_wait_ms(1);
  89. break;
  90. case 2:
  91. kill(ntlm->ntlm_auth_hlpr_pid, SIGKILL);
  92. break;
  93. case 3:
  94. break;
  95. }
  96. }
  97. ntlm->ntlm_auth_hlpr_pid = 0;
  98. }
  99. Curl_safefree(ntlm->challenge);
  100. Curl_safefree(ntlm->response);
  101. }
  102. static CURLcode ntlm_wb_init(struct Curl_easy *data, struct ntlmdata *ntlm,
  103. const char *userp)
  104. {
  105. curl_socket_t sockfds[2];
  106. pid_t child_pid;
  107. const char *username;
  108. char *slash, *domain = NULL;
  109. const char *ntlm_auth = NULL;
  110. char *ntlm_auth_alloc = NULL;
  111. #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
  112. struct passwd pw, *pw_res;
  113. char pwbuf[1024];
  114. #endif
  115. char buffer[STRERROR_LEN];
  116. #if defined(CURL_DISABLE_VERBOSE_STRINGS)
  117. (void) data;
  118. #endif
  119. /* Return if communication with ntlm_auth already set up */
  120. if(ntlm->ntlm_auth_hlpr_socket != CURL_SOCKET_BAD ||
  121. ntlm->ntlm_auth_hlpr_pid)
  122. return CURLE_OK;
  123. username = userp;
  124. /* The real ntlm_auth really doesn't like being invoked with an
  125. empty username. It won't make inferences for itself, and expects
  126. the client to do so (mostly because it's really designed for
  127. servers like squid to use for auth, and client support is an
  128. afterthought for it). So try hard to provide a suitable username
  129. if we don't already have one. But if we can't, provide the
  130. empty one anyway. Perhaps they have an implementation of the
  131. ntlm_auth helper which *doesn't* need it so we might as well try */
  132. if(!username || !username[0]) {
  133. username = getenv("NTLMUSER");
  134. if(!username || !username[0])
  135. username = getenv("LOGNAME");
  136. if(!username || !username[0])
  137. username = getenv("USER");
  138. #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
  139. if((!username || !username[0]) &&
  140. !getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res) &&
  141. pw_res) {
  142. username = pw.pw_name;
  143. }
  144. #endif
  145. if(!username || !username[0])
  146. username = userp;
  147. }
  148. slash = strpbrk(username, "\\/");
  149. if(slash) {
  150. domain = strdup(username);
  151. if(!domain)
  152. return CURLE_OUT_OF_MEMORY;
  153. slash = domain + (slash - username);
  154. *slash = '\0';
  155. username = username + (slash - domain) + 1;
  156. }
  157. /* For testing purposes, when DEBUGBUILD is defined and environment
  158. variable CURL_NTLM_WB_FILE is set a fake_ntlm is used to perform
  159. NTLM challenge/response which only accepts commands and output
  160. strings pre-written in test case definitions */
  161. #ifdef DEBUGBUILD
  162. ntlm_auth_alloc = curl_getenv("CURL_NTLM_WB_FILE");
  163. if(ntlm_auth_alloc)
  164. ntlm_auth = ntlm_auth_alloc;
  165. else
  166. #endif
  167. ntlm_auth = NTLM_WB_FILE;
  168. if(access(ntlm_auth, X_OK) != 0) {
  169. failf(data, "Could not access ntlm_auth: %s errno %d: %s",
  170. ntlm_auth, errno, Curl_strerror(errno, buffer, sizeof(buffer)));
  171. goto done;
  172. }
  173. if(Curl_socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds)) {
  174. failf(data, "Could not open socket pair. errno %d: %s",
  175. errno, Curl_strerror(errno, buffer, sizeof(buffer)));
  176. goto done;
  177. }
  178. child_pid = fork();
  179. if(child_pid == -1) {
  180. sclose(sockfds[0]);
  181. sclose(sockfds[1]);
  182. failf(data, "Could not fork. errno %d: %s",
  183. errno, Curl_strerror(errno, buffer, sizeof(buffer)));
  184. goto done;
  185. }
  186. else if(!child_pid) {
  187. /*
  188. * child process
  189. */
  190. /* Don't use sclose in the child since it fools the socket leak detector */
  191. sclose_nolog(sockfds[0]);
  192. if(dup2(sockfds[1], STDIN_FILENO) == -1) {
  193. failf(data, "Could not redirect child stdin. errno %d: %s",
  194. errno, Curl_strerror(errno, buffer, sizeof(buffer)));
  195. exit(1);
  196. }
  197. if(dup2(sockfds[1], STDOUT_FILENO) == -1) {
  198. failf(data, "Could not redirect child stdout. errno %d: %s",
  199. errno, Curl_strerror(errno, buffer, sizeof(buffer)));
  200. exit(1);
  201. }
  202. if(domain)
  203. execl(ntlm_auth, ntlm_auth,
  204. "--helper-protocol", "ntlmssp-client-1",
  205. "--use-cached-creds",
  206. "--username", username,
  207. "--domain", domain,
  208. NULL);
  209. else
  210. execl(ntlm_auth, ntlm_auth,
  211. "--helper-protocol", "ntlmssp-client-1",
  212. "--use-cached-creds",
  213. "--username", username,
  214. NULL);
  215. sclose_nolog(sockfds[1]);
  216. failf(data, "Could not execl(). errno %d: %s",
  217. errno, Curl_strerror(errno, buffer, sizeof(buffer)));
  218. exit(1);
  219. }
  220. sclose(sockfds[1]);
  221. ntlm->ntlm_auth_hlpr_socket = sockfds[0];
  222. ntlm->ntlm_auth_hlpr_pid = child_pid;
  223. free(domain);
  224. free(ntlm_auth_alloc);
  225. return CURLE_OK;
  226. done:
  227. free(domain);
  228. free(ntlm_auth_alloc);
  229. return CURLE_REMOTE_ACCESS_DENIED;
  230. }
  231. /* if larger than this, something is seriously wrong */
  232. #define MAX_NTLM_WB_RESPONSE 100000
  233. static CURLcode ntlm_wb_response(struct Curl_easy *data, struct ntlmdata *ntlm,
  234. const char *input, curlntlm state)
  235. {
  236. size_t len_in = strlen(input), len_out = 0;
  237. struct dynbuf b;
  238. char *ptr = NULL;
  239. unsigned char *buf = (unsigned char *)data->state.buffer;
  240. Curl_dyn_init(&b, MAX_NTLM_WB_RESPONSE);
  241. while(len_in > 0) {
  242. ssize_t written = swrite(ntlm->ntlm_auth_hlpr_socket, input, len_in);
  243. if(written == -1) {
  244. /* Interrupted by a signal, retry it */
  245. if(errno == EINTR)
  246. continue;
  247. /* write failed if other errors happen */
  248. goto done;
  249. }
  250. input += written;
  251. len_in -= written;
  252. }
  253. /* Read one line */
  254. while(1) {
  255. ssize_t size =
  256. sread(ntlm->ntlm_auth_hlpr_socket, buf, data->set.buffer_size);
  257. if(size == -1) {
  258. if(errno == EINTR)
  259. continue;
  260. goto done;
  261. }
  262. else if(size == 0)
  263. goto done;
  264. if(Curl_dyn_addn(&b, buf, size))
  265. goto done;
  266. len_out = Curl_dyn_len(&b);
  267. ptr = Curl_dyn_ptr(&b);
  268. if(len_out && ptr[len_out - 1] == '\n') {
  269. ptr[len_out - 1] = '\0';
  270. break; /* done! */
  271. }
  272. /* loop */
  273. }
  274. /* Samba/winbind installed but not configured */
  275. if(state == NTLMSTATE_TYPE1 &&
  276. len_out == 3 &&
  277. ptr[0] == 'P' && ptr[1] == 'W')
  278. goto done;
  279. /* invalid response */
  280. if(len_out < 4)
  281. goto done;
  282. if(state == NTLMSTATE_TYPE1 &&
  283. (ptr[0]!='Y' || ptr[1]!='R' || ptr[2]!=' '))
  284. goto done;
  285. if(state == NTLMSTATE_TYPE2 &&
  286. (ptr[0]!='K' || ptr[1]!='K' || ptr[2]!=' ') &&
  287. (ptr[0]!='A' || ptr[1]!='F' || ptr[2]!=' '))
  288. goto done;
  289. ntlm->response = strdup(ptr + 3);
  290. Curl_dyn_free(&b);
  291. if(!ntlm->response)
  292. return CURLE_OUT_OF_MEMORY;
  293. return CURLE_OK;
  294. done:
  295. Curl_dyn_free(&b);
  296. return CURLE_REMOTE_ACCESS_DENIED;
  297. }
  298. CURLcode Curl_input_ntlm_wb(struct connectdata *conn,
  299. bool proxy,
  300. const char *header)
  301. {
  302. struct ntlmdata *ntlm = proxy ? &conn->proxyntlm : &conn->ntlm;
  303. curlntlm *state = proxy ? &conn->proxy_ntlm_state : &conn->http_ntlm_state;
  304. if(!checkprefix("NTLM", header))
  305. return CURLE_BAD_CONTENT_ENCODING;
  306. header += strlen("NTLM");
  307. while(*header && ISSPACE(*header))
  308. header++;
  309. if(*header) {
  310. ntlm->challenge = strdup(header);
  311. if(!ntlm->challenge)
  312. return CURLE_OUT_OF_MEMORY;
  313. *state = NTLMSTATE_TYPE2; /* We got a type-2 message */
  314. }
  315. else {
  316. if(*state == NTLMSTATE_LAST) {
  317. infof(conn->data, "NTLM auth restarted\n");
  318. Curl_http_auth_cleanup_ntlm_wb(conn);
  319. }
  320. else if(*state == NTLMSTATE_TYPE3) {
  321. infof(conn->data, "NTLM handshake rejected\n");
  322. Curl_http_auth_cleanup_ntlm_wb(conn);
  323. *state = NTLMSTATE_NONE;
  324. return CURLE_REMOTE_ACCESS_DENIED;
  325. }
  326. else if(*state >= NTLMSTATE_TYPE1) {
  327. infof(conn->data, "NTLM handshake failure (internal error)\n");
  328. return CURLE_REMOTE_ACCESS_DENIED;
  329. }
  330. *state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
  331. }
  332. return CURLE_OK;
  333. }
  334. /*
  335. * This is for creating ntlm header output by delegating challenge/response
  336. * to Samba's winbind daemon helper ntlm_auth.
  337. */
  338. CURLcode Curl_output_ntlm_wb(struct connectdata *conn, bool proxy)
  339. {
  340. /* point to the address of the pointer that holds the string to send to the
  341. server, which is for a plain host or for a HTTP proxy */
  342. char **allocuserpwd;
  343. /* point to the name and password for this */
  344. const char *userp;
  345. struct ntlmdata *ntlm;
  346. curlntlm *state;
  347. struct auth *authp;
  348. struct Curl_easy *data = conn->data;
  349. CURLcode res = CURLE_OK;
  350. DEBUGASSERT(conn);
  351. DEBUGASSERT(conn->data);
  352. if(proxy) {
  353. #ifndef CURL_DISABLE_PROXY
  354. allocuserpwd = &data->state.aptr.proxyuserpwd;
  355. userp = conn->http_proxy.user;
  356. ntlm = &conn->proxyntlm;
  357. state = &conn->proxy_ntlm_state;
  358. authp = &conn->data->state.authproxy;
  359. #else
  360. return CURLE_NOT_BUILT_IN;
  361. #endif
  362. }
  363. else {
  364. allocuserpwd = &data->state.aptr.userpwd;
  365. userp = conn->user;
  366. ntlm = &conn->ntlm;
  367. state = &conn->http_ntlm_state;
  368. authp = &conn->data->state.authhost;
  369. }
  370. authp->done = FALSE;
  371. /* not set means empty */
  372. if(!userp)
  373. userp = "";
  374. switch(*state) {
  375. case NTLMSTATE_TYPE1:
  376. default:
  377. /* Use Samba's 'winbind' daemon to support NTLM authentication,
  378. * by delegating the NTLM challenge/response protocol to a helper
  379. * in ntlm_auth.
  380. * http://devel.squid-cache.org/ntlm/squid_helper_protocol.html
  381. * https://www.samba.org/samba/docs/man/manpages-3/winbindd.8.html
  382. * https://www.samba.org/samba/docs/man/manpages-3/ntlm_auth.1.html
  383. * Preprocessor symbol 'NTLM_WB_ENABLED' is defined when this
  384. * feature is enabled and 'NTLM_WB_FILE' symbol holds absolute
  385. * filename of ntlm_auth helper.
  386. * If NTLM authentication using winbind fails, go back to original
  387. * request handling process.
  388. */
  389. /* Create communication with ntlm_auth */
  390. res = ntlm_wb_init(conn->data, ntlm, userp);
  391. if(res)
  392. return res;
  393. res = ntlm_wb_response(conn->data, ntlm, "YR\n", *state);
  394. if(res)
  395. return res;
  396. free(*allocuserpwd);
  397. *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
  398. proxy ? "Proxy-" : "",
  399. ntlm->response);
  400. DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
  401. Curl_safefree(ntlm->response);
  402. if(!*allocuserpwd)
  403. return CURLE_OUT_OF_MEMORY;
  404. break;
  405. case NTLMSTATE_TYPE2: {
  406. char *input = aprintf("TT %s\n", ntlm->challenge);
  407. if(!input)
  408. return CURLE_OUT_OF_MEMORY;
  409. res = ntlm_wb_response(conn->data, ntlm, input, *state);
  410. free(input);
  411. if(res)
  412. return res;
  413. free(*allocuserpwd);
  414. *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
  415. proxy ? "Proxy-" : "",
  416. ntlm->response);
  417. DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
  418. *state = NTLMSTATE_TYPE3; /* we sent a type-3 */
  419. authp->done = TRUE;
  420. Curl_http_auth_cleanup_ntlm_wb(conn);
  421. if(!*allocuserpwd)
  422. return CURLE_OUT_OF_MEMORY;
  423. break;
  424. }
  425. case NTLMSTATE_TYPE3:
  426. /* connection is already authenticated,
  427. * don't send a header in future requests */
  428. *state = NTLMSTATE_LAST;
  429. /* FALLTHROUGH */
  430. case NTLMSTATE_LAST:
  431. Curl_safefree(*allocuserpwd);
  432. authp->done = TRUE;
  433. break;
  434. }
  435. return CURLE_OK;
  436. }
  437. void Curl_http_auth_cleanup_ntlm_wb(struct connectdata *conn)
  438. {
  439. ntlm_wb_cleanup(&conn->ntlm);
  440. ntlm_wb_cleanup(&conn->proxyntlm);
  441. }
  442. #endif /* !CURL_DISABLE_HTTP && USE_NTLM && NTLM_WB_ENABLED */