Remove unused argument iconv_convenience.
[amitay/samba.git] / source4 / auth / ntlmssp / ntlmssp_parse.c
index 9c711c0a140efeefbdb138bb866227ce1577ac88..d606b8d563e022ef01da0bdc9bdcb2ce01f1789f 100644 (file)
@@ -7,7 +7,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
-#include "smb.h"
-#include "pstring.h"
+#include "auth/ntlmssp/msrpc_parse.h"
 
 /*
   this is a tiny msrpc packet generator. I am only using this to
@@ -42,7 +40,8 @@
   d = word (4 bytes)
   C = constant ascii string
  */
-BOOL msrpc_gen(TALLOC_CTX *mem_ctx, DATA_BLOB *blob,
+bool msrpc_gen(TALLOC_CTX *mem_ctx, 
+              DATA_BLOB *blob,
               const char *format, ...)
 {
        int i;
@@ -68,7 +67,7 @@ BOOL msrpc_gen(TALLOC_CTX *mem_ctx, DATA_BLOB *blob,
                        head_size += 8;
                        n = push_ucs2_talloc(pointers, (void **)&pointers[i].data, s);
                        if (n == -1) {
-                               return False;
+                               return false;
                        }
                        pointers[i].length = n;
                        pointers[i].length -= 2;
@@ -79,7 +78,7 @@ BOOL msrpc_gen(TALLOC_CTX *mem_ctx, DATA_BLOB *blob,
                        head_size += 8;
                        n = push_ascii_talloc(pointers, (char **)&pointers[i].data, s);
                        if (n == -1) {
-                               return False;
+                               return false;
                        }
                        pointers[i].length = n;
                        pointers[i].length -= 1;
@@ -91,7 +90,7 @@ BOOL msrpc_gen(TALLOC_CTX *mem_ctx, DATA_BLOB *blob,
                        s = va_arg(ap, char *);
                        n = push_ucs2_talloc(pointers, (void **)&pointers[i].data, s);
                        if (n == -1) {
-                               return False;
+                               return false;
                        }
                        pointers[i].length = n;
                        pointers[i].length -= 2;
@@ -177,17 +176,17 @@ BOOL msrpc_gen(TALLOC_CTX *mem_ctx, DATA_BLOB *blob,
        
        talloc_free(pointers);
 
-       return True;
+       return true;
 }
 
 
 /* a helpful macro to avoid running over the end of our blob */
 #define NEED_DATA(amount) \
 if ((head_ofs + amount) > blob->length) { \
-        return False; \
+        return false; \
 }
 
-/*
+/**
   this is a tiny msrpc packet parser. This the the partner of msrpc_gen
 
   format specifiers are:
@@ -200,7 +199,8 @@ if ((head_ofs + amount) > blob->length) { \
   C = constant ascii string
  */
 
-BOOL msrpc_parse(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob,
+bool msrpc_parse(TALLOC_CTX *mem_ctx, 
+                const DATA_BLOB *blob,
                 const char *format, ...)
 {
        int i;
@@ -211,7 +211,9 @@ BOOL msrpc_parse(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob,
        uint16_t len1, len2;
        uint32_t ptr;
        uint32_t *v;
-       pstring p;
+       size_t p_len = 1024;
+       char *p = talloc_array(mem_ctx, char, p_len);
+       bool ret = true;
 
        va_start(ap, format);
        for (i=0; format[i]; i++) {
@@ -228,21 +230,27 @@ BOOL msrpc_parse(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob,
                        } else {
                                /* make sure its in the right format - be strict */
                                if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
-                                       return False;
+                                       ret = false;
+                                       goto cleanup;
                                }
                                if (len1 & 1) {
                                        /* if odd length and unicode */
-                                       return False;
+                                       ret = false;
+                                       goto cleanup;
+                               }
+                               if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
+                                               blob->data + ptr < blob->data) {
+                                       ret = false;
+                                       goto cleanup;
                                }
-                               if (blob->data + ptr < (uint8_t *)ptr || blob->data + ptr < blob->data)
-                                       return False;
 
                                if (0 < len1) {
-                                       pull_string(p, blob->data + ptr, sizeof(p)
+                                       pull_string(p, blob->data + ptr, p_len
                                                    len1, STR_UNICODE|STR_NOALIGN);
                                        (*ps) = talloc_strdup(mem_ctx, p);
                                        if (!(*ps)) {
-                                               return False;
+                                               ret = false;
+                                               goto cleanup;
                                        }
                                } else {
                                        (*ps) = "";
@@ -261,18 +269,23 @@ BOOL msrpc_parse(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob,
                                *ps = "";
                        } else {
                                if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
-                                       return False;
+                                       ret = false;
+                                       goto cleanup;
                                }
 
-                               if (blob->data + ptr < (uint8_t *)ptr || blob->data + ptr < blob->data)
-                                       return False;   
+                               if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
+                                               blob->data + ptr < blob->data) {
+                                       ret = false;
+                                       goto cleanup;
+                               }
 
                                if (0 < len1) {
-                                       pull_string(p, blob->data + ptr, sizeof(p)
+                                       pull_string(p, blob->data + ptr, p_len
                                                    len1, STR_ASCII|STR_NOALIGN);
                                        (*ps) = talloc_strdup(mem_ctx, p);
                                        if (!(*ps)) {
-                                               return False;
+                                               ret = false;
+                                               goto cleanup;
                                        }
                                } else {
                                        (*ps) = "";
@@ -291,12 +304,16 @@ BOOL msrpc_parse(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob,
                        } else {
                                /* make sure its in the right format - be strict */
                                if ((len1 != len2) || (ptr + len1 < ptr) || (ptr + len1 < len1) || (ptr + len1 > blob->length)) {
-                                       return False;
+                                       ret = false;
+                                       goto cleanup;
+                               }
+
+                               if (blob->data + ptr < (uint8_t *)(uintptr_t)ptr ||
+                                               blob->data + ptr < blob->data) {
+                                       ret = false;
+                                       goto cleanup;
                                }
 
-                               if (blob->data + ptr < (uint8_t *)ptr || blob->data + ptr < blob->data)
-                                       return False;   
-                       
                                *b = data_blob_talloc(mem_ctx, blob->data + ptr, len1);
                        }
                        break;
@@ -305,9 +322,12 @@ BOOL msrpc_parse(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob,
                        len1 = va_arg(ap, uint_t);
                        /* make sure its in the right format - be strict */
                        NEED_DATA(len1);
-                       if (blob->data + head_ofs < (uint8_t *)head_ofs || blob->data + head_ofs < blob->data)
-                               return False;   
-                       
+                       if (blob->data + head_ofs < (uint8_t *)head_ofs ||
+                                       blob->data + head_ofs < blob->data) {
+                               ret = false;
+                               goto cleanup;
+                       }
+
                        *b = data_blob_talloc(mem_ctx, blob->data + head_ofs, len1);
                        head_ofs += len1;
                        break;
@@ -319,19 +339,26 @@ BOOL msrpc_parse(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob,
                case 'C':
                        s = va_arg(ap, char *);
 
-                       if (blob->data + head_ofs < (uint8_t *)head_ofs || blob->data + head_ofs < blob->data)
-                               return False;   
-       
-                       head_ofs += pull_string(p, blob->data+head_ofs, sizeof(p), 
-                                               blob->length - head_ofs, 
-                                               STR_ASCII|STR_TERMINATE);
+                       if (blob->data + head_ofs < (uint8_t *)head_ofs ||
+                                       blob->data + head_ofs < blob->data) {
+                               ret = false;
+                               goto cleanup;
+                       }
+
+                       head_ofs += pull_string(p,
+                                       blob->data+head_ofs, p_len,
+                                       blob->length - head_ofs,
+                                       STR_ASCII|STR_TERMINATE);
                        if (strcmp(s, p) != 0) {
-                               return False;
+                               ret = false;
+                               goto cleanup;
                        }
                        break;
                }
        }
-       va_end(ap);
 
-       return True;
+cleanup:
+       va_end(ap);
+       talloc_free(p);
+       return ret;
 }