8 #include "smbencrypt.h"
9 #include "smbbyteorder.h"
10 #include "fetchmail.h"
12 char versionString[] ="libntlm version 0.21";
14 /* Utility routines that handle NTLM auth structures. */
16 /* The [IS]VAL macros are to take care of byte order for non-Intel
17 * Machines -- I think this file is OK, but it hasn't been tested.
18 * The other files (the ones stolen from Samba) should be OK.
22 /* I am not crazy about these macros -- they seem to have gotten
23 * a bit complex. A new scheme for handling string/buffer fields
24 * in the structures probably needs to be designed
27 #define AddBytes(ptr, header, buf, count) \
31 SSVAL(&ptr->header.len,0,count); \
32 SSVAL(&ptr->header.maxlen,0,count); \
33 SIVAL(&ptr->header.offset,0,((ptr->buffer - ((uint8*)ptr)) + ptr->bufIndex)); \
34 memcpy(ptr->buffer+ptr->bufIndex, buf, count); \
35 ptr->bufIndex += count; \
40 ptr->header.maxlen = 0; \
41 SIVAL(&ptr->header.offset,0,ptr->bufIndex); \
45 #define AddString(ptr, header, string) \
49 if (p) len = strlen(p); \
50 AddBytes(ptr, header, ((unsigned char*)p), len); \
53 #define AddUnicodeString(ptr, header, string) \
56 unsigned char *b = NULL; \
61 b = strToUnicode(p); \
63 AddBytes(ptr, header, b, len*2); \
67 #define GetUnicodeString(structPtr, header) \
68 unicodeToString(((char*)structPtr) + IVAL(&structPtr->header.offset,0) , SVAL(&structPtr->header.len,0)/2)
69 #define GetString(structPtr, header) \
70 toString((((char *)structPtr) + IVAL(&structPtr->header.offset,0)), SVAL(&structPtr->header.len,0))
71 #define DumpBuffer(fp, structPtr, header) \
72 dumpRaw(fp,((unsigned char*)structPtr)+IVAL(&structPtr->header.offset,0),SVAL(&structPtr->header.len,0))
75 static void dumpRaw(FILE *fp, unsigned char *buf, size_t len)
80 fprintf(fp,"%02x ",buf[i]);
85 static char *unicodeToString(char *p, size_t len)
88 static char buf[1024];
90 assert(len+1 < sizeof buf);
102 static unsigned char *strToUnicode(char *p)
104 static unsigned char buf[1024];
105 size_t l = strlen(p);
108 assert(l*2 < sizeof buf);
119 static unsigned char *toString(char *p, size_t len)
121 static unsigned char buf[1024];
123 assert(len+1 < sizeof buf);
130 void dumpSmbNtlmAuthRequest(FILE *fp, tSmbNtlmAuthRequest *request)
132 fprintf(fp,"NTLM Request:\n");
133 fprintf(fp," Ident = %s\n",request->ident);
134 fprintf(fp," mType = %d\n",IVAL(&request->msgType,0));
135 fprintf(fp," Flags = %08x\n",IVAL(&request->flags,0));
136 fprintf(fp," User = %s\n",GetString(request,user));
137 fprintf(fp," Domain = %s\n",GetString(request,domain));
140 void dumpSmbNtlmAuthChallenge(FILE *fp, tSmbNtlmAuthChallenge *challenge)
142 fprintf(fp,"NTLM Challenge:\n");
143 fprintf(fp," Ident = %s\n",challenge->ident);
144 fprintf(fp," mType = %d\n",IVAL(&challenge->msgType,0));
145 fprintf(fp," Domain = %s\n",GetUnicodeString(challenge,uDomain));
146 fprintf(fp," Flags = %08x\n",IVAL(&challenge->flags,0));
147 fprintf(fp," Challenge = "); dumpRaw(fp, challenge->challengeData,8);
150 void dumpSmbNtlmAuthResponse(FILE *fp, tSmbNtlmAuthResponse *response)
152 fprintf(fp,"NTLM Response:\n");
153 fprintf(fp," Ident = %s\n",response->ident);
154 fprintf(fp," mType = %d\n",IVAL(&response->msgType,0));
155 fprintf(fp," LmResp = "); DumpBuffer(fp,response,lmResponse);
156 fprintf(fp," NTResp = "); DumpBuffer(fp,response,ntResponse);
157 fprintf(fp," Domain = %s\n",GetUnicodeString(response,uDomain));
158 fprintf(fp," User = %s\n",GetUnicodeString(response,uUser));
159 fprintf(fp," Wks = %s\n",GetUnicodeString(response,uWks));
160 fprintf(fp," sKey = "); DumpBuffer(fp, response,sessionKey);
161 fprintf(fp," Flags = %08x\n",IVAL(&response->flags,0));
164 void buildSmbNtlmAuthRequest(tSmbNtlmAuthRequest *request, char *user, char *domain)
166 char *u = xstrdup(user);
167 char *p = strchr(u,'@');
176 request->bufIndex = 0;
177 memcpy(request->ident,"NTLMSSP\0\0\0",8);
178 SIVAL(&request->msgType,0,1);
179 SIVAL(&request->flags,0,0x0000b207); /* have to figure out what these mean */
180 AddString(request,user,u);
181 AddString(request,domain,domain);
185 void buildSmbNtlmAuthResponse(tSmbNtlmAuthChallenge *challenge, tSmbNtlmAuthResponse *response, char *user, char *password)
187 uint8 lmRespData[24];
188 uint8 ntRespData[24];
189 char *d = xstrdup(GetUnicodeString(challenge,uDomain));
191 char *u = xstrdup(user);
192 char *p = strchr(u,'@');
200 SMBencrypt(password, challenge->challengeData, lmRespData);
201 SMBNTencrypt(password, challenge->challengeData, ntRespData);
203 response->bufIndex = 0;
204 memcpy(response->ident,"NTLMSSP\0\0\0",8);
205 SIVAL(&response->msgType,0,3);
207 AddBytes(response,lmResponse,lmRespData,24);
208 AddBytes(response,ntResponse,ntRespData,24);
209 AddUnicodeString(response,uDomain,domain);
210 AddUnicodeString(response,uUser,u);
211 AddUnicodeString(response,uWks,u);
212 AddString(response,sessionKey,NULL);
214 response->flags = challenge->flags;