8 #include "smbencrypt.h"
9 #include "smbbyteorder.h"
11 char versionString[] ="libntlm version 0.21";
13 /* Utility routines that handle NTLM auth structures. */
15 /* The [IS]VAL macros are to take care of byte order for non-Intel
16 * Machines -- I think this file is OK, but it hasn't been tested.
17 * The other files (the ones stolen from Samba) should be OK.
21 /* I am not crazy about these macros -- they seem to have gotten
22 * a bit complex. A new scheme for handling string/buffer fields
23 * in the structures probably needs to be designed
26 #define AddBytes(ptr, header, buf, count) \
30 SSVAL(&ptr->header.len,0,count); \
31 SSVAL(&ptr->header.maxlen,0,count); \
32 SIVAL(&ptr->header.offset,0,((ptr->buffer - ((uint8*)ptr)) + ptr->bufIndex)); \
33 memcpy(ptr->buffer+ptr->bufIndex, buf, count); \
34 ptr->bufIndex += count; \
39 ptr->header.maxlen = 0; \
40 SIVAL(&ptr->header.offset,0,ptr->bufIndex); \
44 #define AddString(ptr, header, string) \
48 if (p) len = strlen(p); \
49 AddBytes(ptr, header, ((unsigned char*)p), len); \
52 #define AddUnicodeString(ptr, header, string) \
55 unsigned char *b = NULL; \
60 b = strToUnicode(p); \
62 AddBytes(ptr, header, b, len*2); \
66 #define GetUnicodeString(structPtr, header) \
67 unicodeToString(((char*)structPtr) + IVAL(&structPtr->header.offset,0) , SVAL(&structPtr->header.len,0)/2)
68 #define GetString(structPtr, header) \
69 toString((((char *)structPtr) + IVAL(&structPtr->header.offset,0)), SVAL(&structPtr->header.len,0))
70 #define DumpBuffer(fp, structPtr, header) \
71 dumpRaw(fp,((unsigned char*)structPtr)+IVAL(&structPtr->header.offset,0),SVAL(&structPtr->header.len,0))
74 static void dumpRaw(FILE *fp, unsigned char *buf, size_t len)
79 fprintf(fp,"%02x ",buf[i]);
84 static char *unicodeToString(char *p, size_t len)
87 static char buf[1024];
89 assert(len+1 < sizeof buf);
101 static unsigned char *strToUnicode(char *p)
103 static unsigned char buf[1024];
104 size_t l = strlen(p);
107 assert(l*2 < sizeof buf);
118 static unsigned char *toString(char *p, size_t len)
120 static unsigned char buf[1024];
122 assert(len+1 < sizeof buf);
129 void dumpSmbNtlmAuthRequest(FILE *fp, tSmbNtlmAuthRequest *request)
131 fprintf(fp,"NTLM Request:\n");
132 fprintf(fp," Ident = %s\n",request->ident);
133 fprintf(fp," mType = %d\n",IVAL(&request->msgType,0));
134 fprintf(fp," Flags = %08x\n",IVAL(&request->flags,0));
135 fprintf(fp," User = %s\n",GetString(request,user));
136 fprintf(fp," Domain = %s\n",GetString(request,domain));
139 void dumpSmbNtlmAuthChallenge(FILE *fp, tSmbNtlmAuthChallenge *challenge)
141 fprintf(fp,"NTLM Challenge:\n");
142 fprintf(fp," Ident = %s\n",challenge->ident);
143 fprintf(fp," mType = %d\n",IVAL(&challenge->msgType,0));
144 fprintf(fp," Domain = %s\n",GetUnicodeString(challenge,uDomain));
145 fprintf(fp," Flags = %08x\n",IVAL(&challenge->flags,0));
146 fprintf(fp," Challenge = "); dumpRaw(fp, challenge->challengeData,8);
149 void dumpSmbNtlmAuthResponse(FILE *fp, tSmbNtlmAuthResponse *response)
151 fprintf(fp,"NTLM Response:\n");
152 fprintf(fp," Ident = %s\n",response->ident);
153 fprintf(fp," mType = %d\n",IVAL(&response->msgType,0));
154 fprintf(fp," LmResp = "); DumpBuffer(fp,response,lmResponse);
155 fprintf(fp," NTResp = "); DumpBuffer(fp,response,ntResponse);
156 fprintf(fp," Domain = %s\n",GetUnicodeString(response,uDomain));
157 fprintf(fp," User = %s\n",GetUnicodeString(response,uUser));
158 fprintf(fp," Wks = %s\n",GetUnicodeString(response,uWks));
159 fprintf(fp," sKey = "); DumpBuffer(fp, response,sessionKey);
160 fprintf(fp," Flags = %08x\n",IVAL(&response->flags,0));
163 void buildSmbNtlmAuthRequest(tSmbNtlmAuthRequest *request, char *user, char *domain)
165 char *u = strdup(user);
166 char *p = strchr(u,'@');
175 request->bufIndex = 0;
176 memcpy(request->ident,"NTLMSSP\0\0\0",8);
177 SIVAL(&request->msgType,0,1);
178 SIVAL(&request->flags,0,0x0000b207); /* have to figure out what these mean */
179 AddString(request,user,u);
180 AddString(request,domain,domain);
184 void buildSmbNtlmAuthResponse(tSmbNtlmAuthChallenge *challenge, tSmbNtlmAuthResponse *response, char *user, char *password)
186 uint8 lmRespData[24];
187 uint8 ntRespData[24];
188 char *d = strdup(GetUnicodeString(challenge,uDomain));
190 char *u = strdup(user);
191 char *p = strchr(u,'@');
199 SMBencrypt(password, challenge->challengeData, lmRespData);
200 SMBNTencrypt(password, challenge->challengeData, ntRespData);
202 response->bufIndex = 0;
203 memcpy(response->ident,"NTLMSSP\0\0\0",8);
204 SIVAL(&response->msgType,0,3);
206 AddBytes(response,lmResponse,lmRespData,24);
207 AddBytes(response,ntResponse,ntRespData,24);
208 AddUnicodeString(response,uDomain,domain);
209 AddUnicodeString(response,uUser,u);
210 AddUnicodeString(response,uWks,u);
211 AddString(response,sessionKey,NULL);
213 response->flags = challenge->flags;