]> Pileus Git - ~andy/fetchmail/blob - ntlmsubr.c
NTLM: cancel properly in case of failure.
[~andy/fetchmail] / ntlmsubr.c
1 #include "config.h"
2
3 #ifdef NTLM_ENABLE
4 #include "fetchmail.h"
5 #include "i18n.h"
6 #include "ntlm.h"
7 #include "socket.h"
8
9 #include <string.h>
10
11 int ntlm_helper(int sock, struct query *ctl, const char *proto)
12 {
13 /*
14  * NTLM support by Grant Edwards.
15  *
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
19  * network interface.
20  *
21  * Much source (ntlm.h, smb*.c smb*.h) was borrowed from Samba.
22  */
23     tSmbNtlmAuthRequest request;
24     tSmbNtlmAuthChallenge challenge;
25     tSmbNtlmAuthResponse response;
26
27     char msgbuf[2048];
28     int result;
29
30     if ((result = gen_recv(sock, msgbuf, sizeof msgbuf)))
31         return result;
32
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"));
36         }
37         goto cancelfail;
38     }
39
40     buildSmbNtlmAuthRequest(&request,ctl->remotename,NULL);
41
42     if (outlevel >= O_DEBUG)
43         dumpSmbNtlmAuthRequest(stdout, &request);
44
45     memset(msgbuf,0,sizeof msgbuf);
46     to64frombits (msgbuf, &request, SmbLength(&request));
47
48     if (outlevel >= O_MONITOR)
49         report(stdout, "%s> %s\n", proto, msgbuf);
50
51     strcat(msgbuf,"\r\n");
52     SockWrite (sock, msgbuf, strlen (msgbuf));
53
54     if ((result = gen_recv(sock, msgbuf, sizeof msgbuf)))
55         goto cancelfail;
56
57     (void)from64tobits (&challenge, msgbuf, sizeof(challenge));
58
59     if (outlevel >= O_DEBUG)
60         dumpSmbNtlmAuthChallenge(stdout, &challenge);
61
62     buildSmbNtlmAuthResponse(&challenge, &response,ctl->remotename,ctl->password);
63
64     if (outlevel >= O_DEBUG)
65         dumpSmbNtlmAuthResponse(stdout, &response);
66
67     memset(msgbuf,0,sizeof msgbuf);
68     to64frombits (msgbuf, &response, SmbLength(&response));
69
70     if (outlevel >= O_MONITOR)
71         report(stdout, "%s> %s\n", proto, msgbuf);
72
73     strcat(msgbuf,"\r\n");
74     SockWrite (sock, msgbuf, strlen (msgbuf));
75
76     return PS_SUCCESS;
77
78 cancelfail: /* cancel authentication and return failure */
79     {
80         if (outlevel >= O_MONITOR)
81             report(stdout, "%s> *\n", proto);
82         SockWrite(sock, "*\r\n", 3);
83         return result;
84     }
85 }
86
87 #endif /* NTLM_ENABLE */