]> Pileus Git - ~andy/fetchmail/blobdiff - smbutil.c
Note Earl's regression fix for SSL_CTX_clear_options() on older OpenSSL.
[~andy/fetchmail] / smbutil.c
index 74bd5ba278dcae5c9fa5087c5c2839bb0c7ea405..78041ae969180f950f29cb6d5d682181079969da 100644 (file)
--- a/smbutil.c
+++ b/smbutil.c
@@ -26,7 +26,7 @@ char versionString[] ="libntlm version 0.21";
 
 #define AddBytes(ptr, header, buf, count) \
 { \
-if (buf && count) \
+if (buf != NULL && count != 0) \
   { \
   SSVAL(&ptr->header.len,0,count); \
   SSVAL(&ptr->header.maxlen,0,count); \
@@ -44,23 +44,23 @@ else \
 
 #define AddString(ptr, header, string) \
 { \
-char *p = string; \
-int len = 0; \
-if (p) len = strlen(p); \
-AddBytes(ptr, header, ((unsigned char*)p), len); \
+char *p_ = string; \
+int len_ = 0; \
+if (p_) len_ = strlen(p_); \
+AddBytes(ptr, header, ((unsigned char*)p_), len_); \
 }
 
 #define AddUnicodeString(ptr, header, string) \
 { \
-char *p = string; \
-unsigned char *b = NULL; \
-int len = 0; \
-if (p) \
+char *p_ = string; \
+unsigned char *b_ = NULL; \
+int len_ = 0; \
+if (p_) \
   { \
-  len = strlen(p); \
-  b = strToUnicode(p); \
+  len_ = strlen(p_); \
+  b_ = strToUnicode(p_); \
   } \
-AddBytes(ptr, header, b, len*2); \
+AddBytes(ptr, header, b_, len_*2); \
 }
 
 
@@ -82,15 +82,29 @@ static void dumpRaw(FILE *fp, unsigned char *buf, size_t len)
     fprintf(fp,"\n");
   }
 
+/* helper macro to destructively resize buffers; assumes that bufsiz
+ * is initialized to 0 if buf is unallocated! */
+#define allocbuf(buf, bufsiz, need, type) do { \
+  if (!buf || (need) > (bufsiz)) \
+    { \
+    (bufsiz) = ((need) < 1024) ? 1024 : (need); \
+    xfree(buf); \
+    (buf) = (type)xmalloc(bufsiz); \
+    } \
+  } while (0);
+
+/* this is a brute-force conversion from UCS-2LE to US-ASCII, discarding
+ * the upper 9 bits */
 static char *unicodeToString(char *p, size_t len)
   {
   size_t i;
-  static char buf[1024];
+  static char *buf;
+  static size_t bufsiz;
+
+  allocbuf(buf, bufsiz, len + 1, char *);
 
-  assert(len+1 < sizeof buf);
-  
   for (i=0; i<len; ++i)
-    {  
+    {
     buf[i] = *p & 0x7f;
     p += 2;
     }
@@ -99,29 +113,32 @@ static char *unicodeToString(char *p, size_t len)
   return buf;
   }
 
+/* This is a brute-force conversion from US-ASCII to UCS-2LE */
 static unsigned char *strToUnicode(char *p)
   {
-  static unsigned char buf[1024];
+  static unsigned char *buf;
+  static size_t bufsiz;
   size_t l = strlen(p);
   int i = 0;
-  
-  assert(l*2 < sizeof buf);
-  
+
+  allocbuf(buf, bufsiz, l * 2, unsigned char *);
+
   while (l--)
     {
     buf[i++] = *p++;
     buf[i++] = 0;
     }
-  
+
   return buf;
   }
 
 static unsigned char *toString(char *p, size_t len)
   {
-  static unsigned char buf[1024];
-  
-  assert(len+1 < sizeof buf);
-  
+  static unsigned char *buf;
+  static size_t bufsiz;
+
+  allocbuf(buf, bufsiz, len + 1, unsigned char *);
+
   memcpy(buf,p,len);
   buf[len] = 0;
   return buf;
@@ -131,17 +148,17 @@ void dumpSmbNtlmAuthRequest(FILE *fp, tSmbNtlmAuthRequest *request)
   {
   fprintf(fp,"NTLM Request:\n");
   fprintf(fp,"      Ident = %s\n",request->ident);
-  fprintf(fp,"      mType = %d\n",IVAL(&request->msgType,0));
+  fprintf(fp,"      mType = %ld\n",(long int)IVAL(&request->msgType,0));
   fprintf(fp,"      Flags = %08x\n",IVAL(&request->flags,0));
-  fprintf(fp,"       User = %s\n",GetString(request,user));
-  fprintf(fp,"     Domain = %s\n",GetString(request,domain));
+  fprintf(fp,"       User = %s\n",(char *)GetString(request,user));
+  fprintf(fp,"     Domain = %s\n",(char *)GetString(request,domain));
   }
 
 void dumpSmbNtlmAuthChallenge(FILE *fp, tSmbNtlmAuthChallenge *challenge)
   {
   fprintf(fp,"NTLM Challenge:\n");
   fprintf(fp,"      Ident = %s\n",challenge->ident);
-  fprintf(fp,"      mType = %d\n",IVAL(&challenge->msgType,0));
+  fprintf(fp,"      mType = %ld\n",(long int)IVAL(&challenge->msgType,0));
   fprintf(fp,"     Domain = %s\n",GetUnicodeString(challenge,uDomain));
   fprintf(fp,"      Flags = %08x\n",IVAL(&challenge->flags,0));
   fprintf(fp,"  Challenge = "); dumpRaw(fp, challenge->challengeData,8);
@@ -151,7 +168,7 @@ void dumpSmbNtlmAuthResponse(FILE *fp, tSmbNtlmAuthResponse *response)
   {
   fprintf(fp,"NTLM Response:\n");
   fprintf(fp,"      Ident = %s\n",response->ident);
-  fprintf(fp,"      mType = %d\n",IVAL(&response->msgType,0));
+  fprintf(fp,"      mType = %ld\n",(long int)IVAL(&response->msgType,0));
   fprintf(fp,"     LmResp = "); DumpBuffer(fp,response,lmResponse);
   fprintf(fp,"     NTResp = "); DumpBuffer(fp,response,ntResponse);
   fprintf(fp,"     Domain = %s\n",GetUnicodeString(response,uDomain));
@@ -197,8 +214,8 @@ void buildSmbNtlmAuthResponse(tSmbNtlmAuthChallenge *challenge, tSmbNtlmAuthResp
         *p = '\0';
       }
     
-    SMBencrypt(password,   challenge->challengeData, lmRespData);
-    SMBNTencrypt(password, challenge->challengeData, ntRespData);
+    SMBencrypt((uint8*)password,   challenge->challengeData, lmRespData);
+    SMBNTencrypt((uint8*)password, challenge->challengeData, ntRespData);
     
     response->bufIndex = 0;
     memcpy(response->ident,"NTLMSSP\0\0\0",8);