curl_ntlm_wb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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.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 Curl_easy *data,
  299. struct connectdata *conn,
  300. bool proxy,
  301. const char *header)
  302. {
  303. struct ntlmdata *ntlm = proxy ? &conn->proxyntlm : &conn->ntlm;
  304. curlntlm *state = proxy ? &conn->proxy_ntlm_state : &conn->http_ntlm_state;
  305. (void) data; /* In case it gets unused by nop log macros. */
  306. if(!checkprefix("NTLM", header))
  307. return CURLE_BAD_CONTENT_ENCODING;
  308. header += strlen("NTLM");
  309. while(*header && ISSPACE(*header))
  310. header++;
  311. if(*header) {
  312. ntlm->challenge = strdup(header);
  313. if(!ntlm->challenge)
  314. return CURLE_OUT_OF_MEMORY;
  315. *state = NTLMSTATE_TYPE2; /* We got a type-2 message */
  316. }
  317. else {
  318. if(*state == NTLMSTATE_LAST) {
  319. infof(data, "NTLM auth restarted");
  320. Curl_http_auth_cleanup_ntlm_wb(conn);
  321. }
  322. else if(*state == NTLMSTATE_TYPE3) {
  323. infof(data, "NTLM handshake rejected");
  324. Curl_http_auth_cleanup_ntlm_wb(conn);
  325. *state = NTLMSTATE_NONE;
  326. return CURLE_REMOTE_ACCESS_DENIED;
  327. }
  328. else if(*state >= NTLMSTATE_TYPE1) {
  329. infof(data, "NTLM handshake failure (internal error)");
  330. return CURLE_REMOTE_ACCESS_DENIED;
  331. }
  332. *state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
  333. }
  334. return CURLE_OK;
  335. }
  336. /*
  337. * This is for creating ntlm header output by delegating challenge/response
  338. * to Samba's winbind daemon helper ntlm_auth.
  339. */
  340. CURLcode Curl_output_ntlm_wb(struct Curl_easy *data, struct connectdata *conn,
  341. bool proxy)
  342. {
  343. /* point to the address of the pointer that holds the string to send to the
  344. server, which is for a plain host or for a HTTP proxy */
  345. char **allocuserpwd;
  346. /* point to the name and password for this */
  347. const char *userp;
  348. struct ntlmdata *ntlm;
  349. curlntlm *state;
  350. struct auth *authp;
  351. CURLcode res = CURLE_OK;
  352. DEBUGASSERT(conn);
  353. DEBUGASSERT(data);
  354. if(proxy) {
  355. #ifndef CURL_DISABLE_PROXY
  356. allocuserpwd = &data->state.aptr.proxyuserpwd;
  357. userp = conn->http_proxy.user;
  358. ntlm = &conn->proxyntlm;
  359. state = &conn->proxy_ntlm_state;
  360. authp = &data->state.authproxy;
  361. #else
  362. return CURLE_NOT_BUILT_IN;
  363. #endif
  364. }
  365. else {
  366. allocuserpwd = &data->state.aptr.userpwd;
  367. userp = conn->user;
  368. ntlm = &conn->ntlm;
  369. state = &conn->http_ntlm_state;
  370. authp = &data->state.authhost;
  371. }
  372. authp->done = FALSE;
  373. /* not set means empty */
  374. if(!userp)
  375. userp = "";
  376. switch(*state) {
  377. case NTLMSTATE_TYPE1:
  378. default:
  379. /* Use Samba's 'winbind' daemon to support NTLM authentication,
  380. * by delegating the NTLM challenge/response protocol to a helper
  381. * in ntlm_auth.
  382. * https://web.archive.org/web/20190925164737
  383. * /devel.squid-cache.org/ntlm/squid_helper_protocol.html
  384. * https://www.samba.org/samba/docs/man/manpages-3/winbindd.8.html
  385. * https://www.samba.org/samba/docs/man/manpages-3/ntlm_auth.1.html
  386. * Preprocessor symbol 'NTLM_WB_ENABLED' is defined when this
  387. * feature is enabled and 'NTLM_WB_FILE' symbol holds absolute
  388. * filename of ntlm_auth helper.
  389. * If NTLM authentication using winbind fails, go back to original
  390. * request handling process.
  391. */
  392. /* Create communication with ntlm_auth */
  393. res = ntlm_wb_init(data, ntlm, userp);
  394. if(res)
  395. return res;
  396. res = ntlm_wb_response(data, ntlm, "YR\n", *state);
  397. if(res)
  398. return res;
  399. free(*allocuserpwd);
  400. *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
  401. proxy ? "Proxy-" : "",
  402. ntlm->response);
  403. DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
  404. Curl_safefree(ntlm->response);
  405. if(!*allocuserpwd)
  406. return CURLE_OUT_OF_MEMORY;
  407. break;
  408. case NTLMSTATE_TYPE2: {
  409. char *input = aprintf("TT %s\n", ntlm->challenge);
  410. if(!input)
  411. return CURLE_OUT_OF_MEMORY;
  412. res = ntlm_wb_response(data, ntlm, input, *state);
  413. free(input);
  414. if(res)
  415. return res;
  416. free(*allocuserpwd);
  417. *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
  418. proxy ? "Proxy-" : "",
  419. ntlm->response);
  420. DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
  421. *state = NTLMSTATE_TYPE3; /* we sent a type-3 */
  422. authp->done = TRUE;
  423. Curl_http_auth_cleanup_ntlm_wb(conn);
  424. if(!*allocuserpwd)
  425. return CURLE_OUT_OF_MEMORY;
  426. break;
  427. }
  428. case NTLMSTATE_TYPE3:
  429. /* connection is already authenticated,
  430. * don't send a header in future requests */
  431. *state = NTLMSTATE_LAST;
  432. /* FALLTHROUGH */
  433. case NTLMSTATE_LAST:
  434. Curl_safefree(*allocuserpwd);
  435. authp->done = TRUE;
  436. break;
  437. }
  438. return CURLE_OK;
  439. }
  440. void Curl_http_auth_cleanup_ntlm_wb(struct connectdata *conn)
  441. {
  442. ntlm_wb_cleanup(&conn->ntlm);
  443. ntlm_wb_cleanup(&conn->proxyntlm);
  444. }
  445. #endif /* !CURL_DISABLE_HTTP && USE_NTLM && NTLM_WB_ENABLED */