8 #include "smbencrypt.h"
9 #include "smbbyteorder.h"
15 char versionString[] = PACKAGE_STRING;
17 /* Utility routines that handle NTLM auth structures. */
19 /* The [IS]VAL macros are to take care of byte order for non-Intel
20 * Machines -- I think this file is OK, but it hasn't been tested.
21 * The other files (the ones stolen from Samba) should be OK.
25 /* I am not crazy about these macros -- they seem to have gotten
26 * a bit complex. A new scheme for handling string/buffer fields
27 * in the structures probably needs to be designed
30 #define AddBytes(ptr, header, buf, count) \
34 SSVAL(&ptr->header.len,0,count); \
35 SSVAL(&ptr->header.maxlen,0,count); \
36 SIVAL(&ptr->header.offset,0,((ptr->buffer - ((uint8*)ptr)) + ptr->bufIndex)); \
37 memcpy(ptr->buffer+ptr->bufIndex, buf, count); \
38 ptr->bufIndex += count; \
43 ptr->header.maxlen = 0; \
44 SIVAL(&ptr->header.offset,0,ptr->bufIndex); \
48 #define AddString(ptr, header, string) \
52 if (p) len = strlen(p); \
53 AddBytes(ptr, header, ((unsigned char*)p), len); \
56 #define AddUnicodeString(ptr, header, string) \
59 unsigned char *b = NULL; \
64 b = strToUnicode(p); \
66 AddBytes(ptr, header, b, len*2); \
70 #define GetUnicodeString(structPtr, header) \
71 unicodeToString(((char*)structPtr) + IVAL(&structPtr->header.offset,0) , SVAL(&structPtr->header.len,0)/2)
72 #define GetString(structPtr, header) \
73 toString((((char *)structPtr) + IVAL(&structPtr->header.offset,0)), SVAL(&structPtr->header.len,0))
74 #define DumpBuffer(fp, structPtr, header) \
75 dumpRaw(fp,((unsigned char*)structPtr)+IVAL(&structPtr->header.offset,0),SVAL(&structPtr->header.len,0))
78 static void dumpRaw(FILE *fp, unsigned char *buf, size_t len)
83 fprintf(fp,"%02x ",buf[i]);
88 static char *unicodeToString(char *p, size_t len)
91 static char buf[1024];
93 /* quick hack to work around invalid NTLM challenge */
94 if (len + 1 < sizeof(buf)) return "";
96 // assert(len+1 < sizeof buf);
108 static unsigned char *strToUnicode(char *p)
110 static unsigned char buf[1024];
111 size_t l = strlen(p);
114 assert(l*2 < sizeof buf);
125 static unsigned char *toString(char *p, size_t len)
127 static unsigned char buf[1024];
129 assert(len+1 < sizeof buf);
136 void dumpSmbNtlmAuthRequest(FILE *fp, tSmbNtlmAuthRequest *request)
138 fprintf(fp,"NTLM Request:\n");
139 fprintf(fp," Ident = %s\n",request->ident);
140 fprintf(fp," mType = %d\n",IVAL(&request->msgType,0));
141 fprintf(fp," Flags = %08x\n",IVAL(&request->flags,0));
142 fprintf(fp," User = %s\n",GetString(request,user));
143 fprintf(fp," Domain = %s\n",GetString(request,domain));
146 void dumpSmbNtlmAuthChallenge(FILE *fp, tSmbNtlmAuthChallenge *challenge)
148 fprintf(fp,"NTLM Challenge:\n");
149 fprintf(fp," Ident = %s\n",challenge->ident);
150 fprintf(fp," mType = %d\n",IVAL(&challenge->msgType,0));
151 fprintf(fp," Domain = %s\n",GetUnicodeString(challenge,uDomain));
152 fprintf(fp," Flags = %08x\n",IVAL(&challenge->flags,0));
153 fprintf(fp," Challenge = "); dumpRaw(fp, challenge->challengeData,8);
156 void dumpSmbNtlmAuthResponse(FILE *fp, tSmbNtlmAuthResponse *response)
158 fprintf(fp,"NTLM Response:\n");
159 fprintf(fp," Ident = %s\n",response->ident);
160 fprintf(fp," mType = %d\n",IVAL(&response->msgType,0));
161 fprintf(fp," LmResp = "); DumpBuffer(fp,response,lmResponse);
162 fprintf(fp," NTResp = "); DumpBuffer(fp,response,ntResponse);
163 fprintf(fp," Domain = %s\n",GetUnicodeString(response,uDomain));
164 fprintf(fp," User = %s\n",GetUnicodeString(response,uUser));
165 fprintf(fp," Wks = %s\n",GetUnicodeString(response,uWks));
166 fprintf(fp," sKey = "); DumpBuffer(fp, response,sessionKey);
167 fprintf(fp," Flags = %08x\n",IVAL(&response->flags,0));
170 void buildSmbNtlmAuthRequest(tSmbNtlmAuthRequest *request, char *user, char *domain)
172 char *u = strdup(user);
173 char *p = strchr(u,'@');
182 request->bufIndex = 0;
183 memcpy(request->ident,"NTLMSSP\0\0\0",8);
184 SIVAL(&request->msgType,0,1);
185 SIVAL(&request->flags,0,0x0000b207); /* have to figure out what these mean */
186 AddString(request,user,u);
187 AddString(request,domain,domain);
191 void buildSmbNtlmAuthResponse(tSmbNtlmAuthChallenge *challenge, tSmbNtlmAuthResponse *response, char *user, char *password)
193 uint8 lmRespData[24];
194 uint8 ntRespData[24];
195 char *d = strdup(GetUnicodeString(challenge,uDomain));
197 char *u = strdup(user);
198 char *p = strchr(u,'@');
206 SMBencrypt(password, challenge->challengeData, lmRespData);
207 SMBNTencrypt(password, challenge->challengeData, ntRespData);
209 response->bufIndex = 0;
210 memcpy(response->ident,"NTLMSSP\0\0\0",8);
211 SIVAL(&response->msgType,0,3);
213 AddBytes(response,lmResponse,lmRespData,24);
214 AddBytes(response,ntResponse,ntRespData,24);
215 AddUnicodeString(response,uDomain,domain);
216 AddUnicodeString(response,uUser,u);
217 AddUnicodeString(response,uWks,u);
218 AddString(response,sessionKey,NULL);
220 response->flags = challenge->flags;