PageRenderTime 204ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/curl_ntlm_wb.c

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