11 int ntlm_helper(int sock, struct query *ctl, const char *proto)
14 * NTLM support by Grant Edwards.
16 * Handle MS-Exchange NTLM authentication method. This is the same
17 * as the NTLM auth used by Samba for SMB related services. We just
18 * encode the packets in base64 instead of sending them out via a
21 * Much source (ntlm.h, smb*.c smb*.h) was borrowed from Samba.
23 tSmbNtlmAuthRequest request;
24 tSmbNtlmAuthChallenge challenge;
25 tSmbNtlmAuthResponse response;
30 if ((result = gen_recv(sock, msgbuf, sizeof msgbuf)))
33 if (msgbuf[0] != '+' && strspn(msgbuf+1, " \t") < strlen(msgbuf+1)) {
34 if (outlevel >= O_VERBOSE) {
35 report(stdout, GT_("Warning: received malformed challenge to \"AUTH(ENTICATE) NTLM\"!\n"));
41 buildSmbNtlmAuthRequest(&request,ctl->remotename,NULL);
43 if (outlevel >= O_DEBUG)
44 dumpSmbNtlmAuthRequest(stdout, &request);
46 memset(msgbuf,0,sizeof msgbuf);
47 to64frombits (msgbuf, &request, SmbLength(&request));
49 if (outlevel >= O_MONITOR)
50 report(stdout, "%s> %s\n", proto, msgbuf);
52 strcat(msgbuf,"\r\n");
53 SockWrite (sock, msgbuf, strlen (msgbuf));
55 if ((result = gen_recv(sock, msgbuf, sizeof msgbuf)))
58 if ((result = from64tobits (&challenge, msgbuf, sizeof(challenge))) < 0
59 || result < ((void *)&challenge.context - (void *)&challenge))
61 report (stderr, GT_("could not decode BASE64 challenge\n"));
62 /* We do not goto cancelfail; the server has already sent the
63 * tagged reply, so the protocol exchange has ended, no need
64 * for us to send the asterisk. */
68 /* validate challenge:
71 * - that offset points into buffer
72 * - that offset + length does not wrap
73 * - that offset + length is not bigger than buffer */
74 if (0 != memcmp("NTLMSSP", challenge.ident, 8)
75 || challenge.msgType != 2
76 || challenge.uDomain.offset > result
77 || challenge.uDomain.offset + challenge.uDomain.len < challenge.uDomain.offset
78 || challenge.uDomain.offset + challenge.uDomain.len > result)
80 report (stderr, GT_("NTLM challenge contains invalid data.\n"));
85 if (outlevel >= O_DEBUG)
86 dumpSmbNtlmAuthChallenge(stdout, &challenge);
88 buildSmbNtlmAuthResponse(&challenge, &response,ctl->remotename,ctl->password);
90 if (outlevel >= O_DEBUG)
91 dumpSmbNtlmAuthResponse(stdout, &response);
93 memset(msgbuf,0,sizeof msgbuf);
94 to64frombits (msgbuf, &response, SmbLength(&response));
96 if (outlevel >= O_MONITOR)
97 report(stdout, "%s> %s\n", proto, msgbuf);
99 strcat(msgbuf,"\r\n");
100 SockWrite (sock, msgbuf, strlen (msgbuf));
104 cancelfail: /* cancel authentication and return failure */
106 if (outlevel >= O_MONITOR)
107 report(stdout, "%s> *\n", proto);
108 SockWrite(sock, "*\r\n", 3);
113 #endif /* NTLM_ENABLE */