]> Pileus Git - ~andy/fetchmail/blob - smbutil.c
cbe18b991db697fdca0f3323572cfc969fbfd338
[~andy/fetchmail] / smbutil.c
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <ctype.h>
5 #include <assert.h>
6 #include <string.h>
7 #include "ntlm.h"
8 #include "smbencrypt.h"
9 #include "smbbyteorder.h"
10 #include "fetchmail.h"
11
12 char versionString[] ="libntlm version 0.21";
13
14 /* Utility routines that handle NTLM auth structures. */
15
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.
19  */
20
21
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
25  */
26
27 #define AddBytes(ptr, header, buf, count) \
28 { \
29 if (buf && count) \
30   { \
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; \
36   } \
37 else \
38   { \
39   ptr->header.len = \
40   ptr->header.maxlen = 0; \
41   SIVAL(&ptr->header.offset,0,ptr->bufIndex); \
42   } \
43 }
44
45 #define AddString(ptr, header, string) \
46 { \
47 char *p = string; \
48 int len = 0; \
49 if (p) len = strlen(p); \
50 AddBytes(ptr, header, ((unsigned char*)p), len); \
51 }
52
53 #define AddUnicodeString(ptr, header, string) \
54 { \
55 char *p = string; \
56 unsigned char *b = NULL; \
57 int len = 0; \
58 if (p) \
59   { \
60   len = strlen(p); \
61   b = strToUnicode(p); \
62   } \
63 AddBytes(ptr, header, b, len*2); \
64 }
65
66
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))
73
74
75 static void dumpRaw(FILE *fp, unsigned char *buf, size_t len)
76   {
77   int i;
78   
79   for (i=0; i<len; ++i)
80     fprintf(fp,"%02x ",buf[i]);
81     
82     fprintf(fp,"\n");
83   }
84
85 static char *unicodeToString(char *p, size_t len)
86   {
87   int i;
88   static char buf[1024];
89
90   assert(len+1 < sizeof buf);
91   
92   for (i=0; i<len; ++i)
93     {  
94     buf[i] = *p & 0x7f;
95     p += 2;
96     }
97
98   buf[i] = '\0';
99   return buf;
100   }
101
102 static unsigned char *strToUnicode(char *p)
103   {
104   static unsigned char buf[1024];
105   size_t l = strlen(p);
106   int i = 0;
107   
108   assert(l*2 < sizeof buf);
109   
110   while (l--)
111     {
112     buf[i++] = *p++;
113     buf[i++] = 0;
114     }
115   
116   return buf;
117   }
118
119 static unsigned char *toString(char *p, size_t len)
120   {
121   static unsigned char buf[1024];
122   
123   assert(len+1 < sizeof buf);
124   
125   memcpy(buf,p,len);
126   buf[len] = 0;
127   return buf;
128   }
129
130 void dumpSmbNtlmAuthRequest(FILE *fp, tSmbNtlmAuthRequest *request)
131   {
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));
138   }
139
140 void dumpSmbNtlmAuthChallenge(FILE *fp, tSmbNtlmAuthChallenge *challenge)
141   {
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);
148   }
149
150 void dumpSmbNtlmAuthResponse(FILE *fp, tSmbNtlmAuthResponse *response)
151   {
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));
162   }
163
164 void buildSmbNtlmAuthRequest(tSmbNtlmAuthRequest *request, char *user, char *domain)
165   {
166     char *u = xstrdup(user);
167     char *p = strchr(u,'@');
168     
169     if (p)
170       {
171         if (!domain) 
172           domain = p+1;
173         *p = '\0';
174       }
175     
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);
182     free(u);
183   }
184
185 void buildSmbNtlmAuthResponse(tSmbNtlmAuthChallenge *challenge, tSmbNtlmAuthResponse *response, char *user, char *password)
186   {
187     uint8 lmRespData[24];
188     uint8 ntRespData[24];
189     char *d = xstrdup(GetUnicodeString(challenge,uDomain));
190     char *domain = d;
191     char *u = xstrdup(user);
192     char *p = strchr(u,'@');
193     
194     if (p)
195       {
196         domain = p+1;
197         *p = '\0';
198       }
199     
200     SMBencrypt(password,   challenge->challengeData, lmRespData);
201     SMBNTencrypt(password, challenge->challengeData, ntRespData);
202     
203     response->bufIndex = 0;
204     memcpy(response->ident,"NTLMSSP\0\0\0",8);
205     SIVAL(&response->msgType,0,3);
206     
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);
213   
214     response->flags = challenge->flags;
215     
216     free(d);
217     free(u);
218   }
219