curl_ntlm_wb.c 12 KB

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