2
0

curl_ntlm_wb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, 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. /* The last 3 #include files should be in this order */
  51. #include "curl_printf.h"
  52. #include "curl_memory.h"
  53. #include "memdebug.h"
  54. #if DEBUG_ME
  55. # define DEBUG_OUT(x) x
  56. #else
  57. # define DEBUG_OUT(x) Curl_nop_stmt
  58. #endif
  59. /* Portable 'sclose_nolog' used only in child process instead of 'sclose'
  60. to avoid fooling the socket leak detector */
  61. #if defined(HAVE_CLOSESOCKET)
  62. # define sclose_nolog(x) closesocket((x))
  63. #elif defined(HAVE_CLOSESOCKET_CAMEL)
  64. # define sclose_nolog(x) CloseSocket((x))
  65. #else
  66. # define sclose_nolog(x) close((x))
  67. #endif
  68. void Curl_ntlm_wb_cleanup(struct connectdata *conn)
  69. {
  70. if(conn->ntlm_auth_hlpr_socket != CURL_SOCKET_BAD) {
  71. sclose(conn->ntlm_auth_hlpr_socket);
  72. conn->ntlm_auth_hlpr_socket = CURL_SOCKET_BAD;
  73. }
  74. if(conn->ntlm_auth_hlpr_pid) {
  75. int i;
  76. for(i = 0; i < 4; i++) {
  77. pid_t ret = waitpid(conn->ntlm_auth_hlpr_pid, NULL, WNOHANG);
  78. if(ret == conn->ntlm_auth_hlpr_pid || errno == ECHILD)
  79. break;
  80. switch(i) {
  81. case 0:
  82. kill(conn->ntlm_auth_hlpr_pid, SIGTERM);
  83. break;
  84. case 1:
  85. /* Give the process another moment to shut down cleanly before
  86. bringing down the axe */
  87. Curl_wait_ms(1);
  88. break;
  89. case 2:
  90. kill(conn->ntlm_auth_hlpr_pid, SIGKILL);
  91. break;
  92. case 3:
  93. break;
  94. }
  95. }
  96. conn->ntlm_auth_hlpr_pid = 0;
  97. }
  98. free(conn->challenge_header);
  99. conn->challenge_header = NULL;
  100. free(conn->response_header);
  101. conn->response_header = NULL;
  102. }
  103. static CURLcode ntlm_wb_init(struct connectdata *conn, 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. /* Return if communication with ntlm_auth already set up */
  117. if(conn->ntlm_auth_hlpr_socket != CURL_SOCKET_BAD ||
  118. conn->ntlm_auth_hlpr_pid)
  119. return CURLE_OK;
  120. username = userp;
  121. /* The real ntlm_auth really doesn't like being invoked with an
  122. empty username. It won't make inferences for itself, and expects
  123. the client to do so (mostly because it's really designed for
  124. servers like squid to use for auth, and client support is an
  125. afterthought for it). So try hard to provide a suitable username
  126. if we don't already have one. But if we can't, provide the
  127. empty one anyway. Perhaps they have an implementation of the
  128. ntlm_auth helper which *doesn't* need it so we might as well try */
  129. if(!username || !username[0]) {
  130. username = getenv("NTLMUSER");
  131. if(!username || !username[0])
  132. username = getenv("LOGNAME");
  133. if(!username || !username[0])
  134. username = getenv("USER");
  135. #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
  136. if((!username || !username[0]) &&
  137. !getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res) &&
  138. pw_res) {
  139. username = pw.pw_name;
  140. }
  141. #endif
  142. if(!username || !username[0])
  143. username = userp;
  144. }
  145. slash = strpbrk(username, "\\/");
  146. if(slash) {
  147. domain = strdup(username);
  148. if(!domain)
  149. return CURLE_OUT_OF_MEMORY;
  150. slash = domain + (slash - username);
  151. *slash = '\0';
  152. username = username + (slash - domain) + 1;
  153. }
  154. /* For testing purposes, when DEBUGBUILD is defined and environment
  155. variable CURL_NTLM_WB_FILE is set a fake_ntlm is used to perform
  156. NTLM challenge/response which only accepts commands and output
  157. strings pre-written in test case definitions */
  158. #ifdef DEBUGBUILD
  159. ntlm_auth_alloc = curl_getenv("CURL_NTLM_WB_FILE");
  160. if(ntlm_auth_alloc)
  161. ntlm_auth = ntlm_auth_alloc;
  162. else
  163. #endif
  164. ntlm_auth = NTLM_WB_FILE;
  165. if(access(ntlm_auth, X_OK) != 0) {
  166. failf(conn->data, "Could not access ntlm_auth: %s errno %d: %s",
  167. ntlm_auth, errno, Curl_strerror(errno, buffer, sizeof(buffer)));
  168. goto done;
  169. }
  170. if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds)) {
  171. failf(conn->data, "Could not open socket pair. errno %d: %s",
  172. errno, Curl_strerror(errno, buffer, sizeof(buffer)));
  173. goto done;
  174. }
  175. child_pid = fork();
  176. if(child_pid == -1) {
  177. sclose(sockfds[0]);
  178. sclose(sockfds[1]);
  179. failf(conn->data, "Could not fork. errno %d: %s",
  180. errno, Curl_strerror(errno, buffer, sizeof(buffer)));
  181. goto done;
  182. }
  183. else if(!child_pid) {
  184. /*
  185. * child process
  186. */
  187. /* Don't use sclose in the child since it fools the socket leak detector */
  188. sclose_nolog(sockfds[0]);
  189. if(dup2(sockfds[1], STDIN_FILENO) == -1) {
  190. failf(conn->data, "Could not redirect child stdin. errno %d: %s",
  191. errno, Curl_strerror(errno, buffer, sizeof(buffer)));
  192. exit(1);
  193. }
  194. if(dup2(sockfds[1], STDOUT_FILENO) == -1) {
  195. failf(conn->data, "Could not redirect child stdout. errno %d: %s",
  196. errno, Curl_strerror(errno, buffer, sizeof(buffer)));
  197. exit(1);
  198. }
  199. if(domain)
  200. execl(ntlm_auth, ntlm_auth,
  201. "--helper-protocol", "ntlmssp-client-1",
  202. "--use-cached-creds",
  203. "--username", username,
  204. "--domain", domain,
  205. NULL);
  206. else
  207. execl(ntlm_auth, ntlm_auth,
  208. "--helper-protocol", "ntlmssp-client-1",
  209. "--use-cached-creds",
  210. "--username", username,
  211. NULL);
  212. sclose_nolog(sockfds[1]);
  213. failf(conn->data, "Could not execl(). errno %d: %s",
  214. errno, Curl_strerror(errno, buffer, sizeof(buffer)));
  215. exit(1);
  216. }
  217. sclose(sockfds[1]);
  218. conn->ntlm_auth_hlpr_socket = sockfds[0];
  219. conn->ntlm_auth_hlpr_pid = child_pid;
  220. free(domain);
  221. free(ntlm_auth_alloc);
  222. return CURLE_OK;
  223. done:
  224. free(domain);
  225. free(ntlm_auth_alloc);
  226. return CURLE_REMOTE_ACCESS_DENIED;
  227. }
  228. /* if larger than this, something is seriously wrong */
  229. #define MAX_NTLM_WB_RESPONSE 100000
  230. static CURLcode ntlm_wb_response(struct connectdata *conn,
  231. const char *input, curlntlm state)
  232. {
  233. char *buf = malloc(NTLM_BUFSIZE);
  234. size_t len_in = strlen(input), len_out = 0;
  235. if(!buf)
  236. return CURLE_OUT_OF_MEMORY;
  237. while(len_in > 0) {
  238. ssize_t written = swrite(conn->ntlm_auth_hlpr_socket, input, len_in);
  239. if(written == -1) {
  240. /* Interrupted by a signal, retry it */
  241. if(errno == EINTR)
  242. continue;
  243. /* write failed if other errors happen */
  244. goto done;
  245. }
  246. input += written;
  247. len_in -= written;
  248. }
  249. /* Read one line */
  250. while(1) {
  251. ssize_t size;
  252. char *newbuf;
  253. size = sread(conn->ntlm_auth_hlpr_socket, buf + len_out, NTLM_BUFSIZE);
  254. if(size == -1) {
  255. if(errno == EINTR)
  256. continue;
  257. goto done;
  258. }
  259. else if(size == 0)
  260. goto done;
  261. len_out += size;
  262. if(buf[len_out - 1] == '\n') {
  263. buf[len_out - 1] = '\0';
  264. break;
  265. }
  266. if(len_out > MAX_NTLM_WB_RESPONSE) {
  267. failf(conn->data, "too large ntlm_wb response!");
  268. free(buf);
  269. return CURLE_OUT_OF_MEMORY;
  270. }
  271. newbuf = Curl_saferealloc(buf, len_out + NTLM_BUFSIZE);
  272. if(!newbuf)
  273. return CURLE_OUT_OF_MEMORY;
  274. buf = newbuf;
  275. }
  276. /* Samba/winbind installed but not configured */
  277. if(state == NTLMSTATE_TYPE1 &&
  278. len_out == 3 &&
  279. buf[0] == 'P' && buf[1] == 'W')
  280. goto done;
  281. /* invalid response */
  282. if(len_out < 4)
  283. goto done;
  284. if(state == NTLMSTATE_TYPE1 &&
  285. (buf[0]!='Y' || buf[1]!='R' || buf[2]!=' '))
  286. goto done;
  287. if(state == NTLMSTATE_TYPE2 &&
  288. (buf[0]!='K' || buf[1]!='K' || buf[2]!=' ') &&
  289. (buf[0]!='A' || buf[1]!='F' || buf[2]!=' '))
  290. goto done;
  291. conn->response_header = aprintf("NTLM %.*s", len_out - 4, buf + 3);
  292. free(buf);
  293. if(!conn->response_header)
  294. return CURLE_OUT_OF_MEMORY;
  295. return CURLE_OK;
  296. done:
  297. free(buf);
  298. return CURLE_REMOTE_ACCESS_DENIED;
  299. }
  300. /*
  301. * This is for creating ntlm header output by delegating challenge/response
  302. * to Samba's winbind daemon helper ntlm_auth.
  303. */
  304. CURLcode Curl_output_ntlm_wb(struct connectdata *conn,
  305. bool proxy)
  306. {
  307. /* point to the address of the pointer that holds the string to send to the
  308. server, which is for a plain host or for a HTTP proxy */
  309. char **allocuserpwd;
  310. /* point to the name and password for this */
  311. const char *userp;
  312. /* point to the correct struct with this */
  313. struct ntlmdata *ntlm;
  314. struct auth *authp;
  315. CURLcode res = CURLE_OK;
  316. char *input;
  317. DEBUGASSERT(conn);
  318. DEBUGASSERT(conn->data);
  319. if(proxy) {
  320. allocuserpwd = &conn->allocptr.proxyuserpwd;
  321. userp = conn->http_proxy.user;
  322. ntlm = &conn->proxyntlm;
  323. authp = &conn->data->state.authproxy;
  324. }
  325. else {
  326. allocuserpwd = &conn->allocptr.userpwd;
  327. userp = conn->user;
  328. ntlm = &conn->ntlm;
  329. authp = &conn->data->state.authhost;
  330. }
  331. authp->done = FALSE;
  332. /* not set means empty */
  333. if(!userp)
  334. userp = "";
  335. switch(ntlm->state) {
  336. case NTLMSTATE_TYPE1:
  337. default:
  338. /* Use Samba's 'winbind' daemon to support NTLM authentication,
  339. * by delegating the NTLM challenge/response protocol to a helper
  340. * in ntlm_auth.
  341. * http://devel.squid-cache.org/ntlm/squid_helper_protocol.html
  342. * https://www.samba.org/samba/docs/man/manpages-3/winbindd.8.html
  343. * https://www.samba.org/samba/docs/man/manpages-3/ntlm_auth.1.html
  344. * Preprocessor symbol 'NTLM_WB_ENABLED' is defined when this
  345. * feature is enabled and 'NTLM_WB_FILE' symbol holds absolute
  346. * filename of ntlm_auth helper.
  347. * If NTLM authentication using winbind fails, go back to original
  348. * request handling process.
  349. */
  350. /* Create communication with ntlm_auth */
  351. res = ntlm_wb_init(conn, userp);
  352. if(res)
  353. return res;
  354. res = ntlm_wb_response(conn, "YR\n", ntlm->state);
  355. if(res)
  356. return res;
  357. free(*allocuserpwd);
  358. *allocuserpwd = aprintf("%sAuthorization: %s\r\n",
  359. proxy ? "Proxy-" : "",
  360. conn->response_header);
  361. DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
  362. free(conn->response_header);
  363. if(!*allocuserpwd)
  364. return CURLE_OUT_OF_MEMORY;
  365. conn->response_header = NULL;
  366. break;
  367. case NTLMSTATE_TYPE2:
  368. input = aprintf("TT %s\n", conn->challenge_header);
  369. if(!input)
  370. return CURLE_OUT_OF_MEMORY;
  371. res = ntlm_wb_response(conn, input, ntlm->state);
  372. free(input);
  373. input = NULL;
  374. if(res)
  375. return res;
  376. free(*allocuserpwd);
  377. *allocuserpwd = aprintf("%sAuthorization: %s\r\n",
  378. proxy ? "Proxy-" : "",
  379. conn->response_header);
  380. DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
  381. ntlm->state = NTLMSTATE_TYPE3; /* we sent a type-3 */
  382. authp->done = TRUE;
  383. Curl_ntlm_wb_cleanup(conn);
  384. if(!*allocuserpwd)
  385. return CURLE_OUT_OF_MEMORY;
  386. break;
  387. case NTLMSTATE_TYPE3:
  388. /* connection is already authenticated,
  389. * don't send a header in future requests */
  390. free(*allocuserpwd);
  391. *allocuserpwd = NULL;
  392. authp->done = TRUE;
  393. break;
  394. }
  395. return CURLE_OK;
  396. }
  397. #endif /* !CURL_DISABLE_HTTP && USE_NTLM && NTLM_WB_ENABLED */