1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #if defined(USE_NTLM) && defined(NTLM_WB_ENABLED)
30 * http://davenport.sourceforge.net/ntlm.html
31 * http://www.innovation.ch/java/ntlm.html
36 #ifdef HAVE_SYS_WAIT_H
46 #include "curl_ntlm_wb.h"
49 #include "curl_memory.h"
51 #define _MPRINTF_REPLACE /* use our functions only */
52 #include <curl/mprintf.h>
54 /* The last #include file should be: */
58 # define DEBUG_OUT(x) x
60 # define DEBUG_OUT(x) Curl_nop_stmt
63 /* Portable 'sclose_nolog' used only in child process instead of 'sclose'
64 to avoid fooling the socket leak detector */
65 #if defined(HAVE_CLOSESOCKET)
66 # define sclose_nolog(x) closesocket((x))
67 #elif defined(HAVE_CLOSESOCKET_CAMEL)
68 # define sclose_nolog(x) CloseSocket((x))
70 # define sclose_nolog(x) close((x))
73 void Curl_ntlm_wb_cleanup(struct connectdata *conn)
75 if(conn->ntlm_auth_hlpr_socket != CURL_SOCKET_BAD) {
76 sclose(conn->ntlm_auth_hlpr_socket);
77 conn->ntlm_auth_hlpr_socket = CURL_SOCKET_BAD;
80 if(conn->ntlm_auth_hlpr_pid) {
82 for(i = 0; i < 4; i++) {
83 pid_t ret = waitpid(conn->ntlm_auth_hlpr_pid, NULL, WNOHANG);
84 if(ret == conn->ntlm_auth_hlpr_pid || errno == ECHILD)
88 kill(conn->ntlm_auth_hlpr_pid, SIGTERM);
91 /* Give the process another moment to shut down cleanly before
92 bringing down the axe */
96 kill(conn->ntlm_auth_hlpr_pid, SIGKILL);
102 conn->ntlm_auth_hlpr_pid = 0;
105 Curl_safefree(conn->challenge_header);
106 conn->challenge_header = NULL;
107 Curl_safefree(conn->response_header);
108 conn->response_header = NULL;
111 static CURLcode ntlm_wb_init(struct connectdata *conn, const char *userp)
113 curl_socket_t sockfds[2];
115 const char *username;
116 char *slash, *domain = NULL;
117 const char *ntlm_auth = NULL;
118 char *ntlm_auth_alloc = NULL;
121 /* Return if communication with ntlm_auth already set up */
122 if(conn->ntlm_auth_hlpr_socket != CURL_SOCKET_BAD ||
123 conn->ntlm_auth_hlpr_pid)
127 slash = strpbrk(username, "\\/");
129 if((domain = strdup(username)) == NULL)
130 return CURLE_OUT_OF_MEMORY;
131 slash = domain + (slash - username);
133 username = username + (slash - domain) + 1;
136 /* For testing purposes, when DEBUGBUILD is defined and environment
137 variable CURL_NTLM_WB_FILE is set a fake_ntlm is used to perform
138 NTLM challenge/response which only accepts commands and output
139 strings pre-written in test case definitions */
141 ntlm_auth_alloc = curl_getenv("CURL_NTLM_WB_FILE");
143 ntlm_auth = ntlm_auth_alloc;
146 ntlm_auth = NTLM_WB_FILE;
148 if(access(ntlm_auth, X_OK) != 0) {
150 failf(conn->data, "Could not access ntlm_auth: %s errno %d: %s",
151 ntlm_auth, error, Curl_strerror(conn, error));
155 if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds)) {
157 failf(conn->data, "Could not open socket pair. errno %d: %s",
158 error, Curl_strerror(conn, error));
163 if(child_pid == -1) {
167 failf(conn->data, "Could not fork. errno %d: %s",
168 error, Curl_strerror(conn, error));
171 else if(!child_pid) {
176 /* Don't use sclose in the child since it fools the socket leak detector */
177 sclose_nolog(sockfds[0]);
178 if(dup2(sockfds[1], STDIN_FILENO) == -1) {
180 failf(conn->data, "Could not redirect child stdin. errno %d: %s",
181 error, Curl_strerror(conn, error));
185 if(dup2(sockfds[1], STDOUT_FILENO) == -1) {
187 failf(conn->data, "Could not redirect child stdout. errno %d: %s",
188 error, Curl_strerror(conn, error));
193 execl(ntlm_auth, ntlm_auth,
194 "--helper-protocol", "ntlmssp-client-1",
195 "--use-cached-creds",
196 "--username", username,
200 execl(ntlm_auth, ntlm_auth,
201 "--helper-protocol", "ntlmssp-client-1",
202 "--use-cached-creds",
203 "--username", username,
207 sclose_nolog(sockfds[1]);
208 failf(conn->data, "Could not execl(). errno %d: %s",
209 error, Curl_strerror(conn, error));
214 conn->ntlm_auth_hlpr_socket = sockfds[0];
215 conn->ntlm_auth_hlpr_pid = child_pid;
216 Curl_safefree(domain);
217 Curl_safefree(ntlm_auth_alloc);
221 Curl_safefree(domain);
222 Curl_safefree(ntlm_auth_alloc);
223 return CURLE_REMOTE_ACCESS_DENIED;
226 static CURLcode ntlm_wb_response(struct connectdata *conn,
227 const char *input, curlntlm state)
230 char buf[200]; /* enough, type 1, 3 message length is less then 200 */
232 size_t len_in = strlen(input), len_out = sizeof(buf);
235 ssize_t written = swrite(conn->ntlm_auth_hlpr_socket, input, len_in);
237 /* Interrupted by a signal, retry it */
240 /* write failed if other errors happen */
248 size = sread(conn->ntlm_auth_hlpr_socket, tmpbuf, len_out);
256 else if(tmpbuf[size - 1] == '\n') {
257 tmpbuf[size - 1] = '\0';
265 /* Samba/winbind installed but not configured */
266 if(state == NTLMSTATE_TYPE1 &&
268 buf[0] == 'P' && buf[1] == 'W')
269 return CURLE_REMOTE_ACCESS_DENIED;
270 /* invalid response */
273 if(state == NTLMSTATE_TYPE1 &&
274 (buf[0]!='Y' || buf[1]!='R' || buf[2]!=' '))
276 if(state == NTLMSTATE_TYPE2 &&
277 (buf[0]!='K' || buf[1]!='K' || buf[2]!=' ') &&
278 (buf[0]!='A' || buf[1]!='F' || buf[2]!=' '))
281 conn->response_header = aprintf("NTLM %.*s", size - 4, buf + 3);
284 return CURLE_REMOTE_ACCESS_DENIED;
288 * This is for creating ntlm header output by delegating challenge/response
289 * to Samba's winbind daemon helper ntlm_auth.
291 CURLcode Curl_output_ntlm_wb(struct connectdata *conn,
294 /* point to the address of the pointer that holds the string to send to the
295 server, which is for a plain host or for a HTTP proxy */
297 /* point to the name and password for this */
299 /* point to the correct struct with this */
300 struct ntlmdata *ntlm;
303 CURLcode res = CURLE_OK;
307 DEBUGASSERT(conn->data);
310 allocuserpwd = &conn->allocptr.proxyuserpwd;
311 userp = conn->proxyuser;
312 ntlm = &conn->proxyntlm;
313 authp = &conn->data->state.authproxy;
316 allocuserpwd = &conn->allocptr.userpwd;
319 authp = &conn->data->state.authhost;
323 /* not set means empty */
327 switch(ntlm->state) {
328 case NTLMSTATE_TYPE1:
330 /* Use Samba's 'winbind' daemon to support NTLM authentication,
331 * by delegating the NTLM challenge/response protocal to a helper
333 * http://devel.squid-cache.org/ntlm/squid_helper_protocol.html
334 * http://www.samba.org/samba/docs/man/manpages-3/winbindd.8.html
335 * http://www.samba.org/samba/docs/man/manpages-3/ntlm_auth.1.html
336 * Preprocessor symbol 'NTLM_WB_ENABLED' is defined when this
337 * feature is enabled and 'NTLM_WB_FILE' symbol holds absolute
338 * filename of ntlm_auth helper.
339 * If NTLM authentication using winbind fails, go back to original
340 * request handling process.
342 /* Create communication with ntlm_auth */
343 res = ntlm_wb_init(conn, userp);
346 res = ntlm_wb_response(conn, "YR\n", ntlm->state);
350 Curl_safefree(*allocuserpwd);
351 *allocuserpwd = aprintf("%sAuthorization: %s\r\n",
352 proxy ? "Proxy-" : "",
353 conn->response_header);
354 DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
355 Curl_safefree(conn->response_header);
356 conn->response_header = NULL;
358 case NTLMSTATE_TYPE2:
359 input = aprintf("TT %s\n", conn->challenge_header);
361 return CURLE_OUT_OF_MEMORY;
362 res = ntlm_wb_response(conn, input, ntlm->state);
368 Curl_safefree(*allocuserpwd);
369 *allocuserpwd = aprintf("%sAuthorization: %s\r\n",
370 proxy ? "Proxy-" : "",
371 conn->response_header);
372 DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
373 ntlm->state = NTLMSTATE_TYPE3; /* we sent a type-3 */
375 Curl_ntlm_wb_cleanup(conn);
377 case NTLMSTATE_TYPE3:
378 /* connection is already authenticated,
379 * don't send a header in future requests */
391 #endif /* USE_NTLM && NTLM_WB_ENABLED */