2004-06-28 06:46:27 +00:00
/*
Unix SMB / CIFS implementation .
simple kerberos5 / SPNEGO routines
Copyright ( C ) Andrew Tridgell 2001
Copyright ( C ) Jim McDonough < jmcd @ us . ibm . com > 2002
Copyright ( C ) Andrew Bartlett 2002 - 2003
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
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
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 0213 9 , USA .
*/
# include "includes.h"
2005-10-01 01:04:34 +00:00
# include "pstring.h"
2004-06-28 06:46:27 +00:00
/*
this is a tiny msrpc packet generator . I am only using this to
avoid tying this code to a particular varient of our rpc code . This
generator is not general enough for all our rpc needs , its just
enough for the spnego / ntlmssp code
format specifiers are :
U = unicode string ( input is unix string )
a = address ( input is char * unix_string )
( 1 byte type , 1 byte length , unicode / ASCII string , all inline )
A = ASCII string ( input is unix string )
B = data blob ( pointer + length )
b = data blob in header ( pointer + length )
D
d = word ( 4 bytes )
C = constant ascii string
*/
BOOL msrpc_gen ( TALLOC_CTX * mem_ctx , DATA_BLOB * blob ,
const char * format , . . . )
{
2004-09-22 21:45:52 +00:00
int i ;
ssize_t n ;
2004-06-28 06:46:27 +00:00
va_list ap ;
char * s ;
uint8_t * b ;
int head_size = 0 , data_size = 0 ;
int head_ofs , data_ofs ;
2004-09-22 21:45:52 +00:00
int * intargs ;
DATA_BLOB * pointers ;
2005-01-27 07:08:20 +00:00
pointers = talloc_array ( mem_ctx , DATA_BLOB , strlen ( format ) ) ;
intargs = talloc_array ( pointers , int , strlen ( format ) ) ;
2004-06-28 06:46:27 +00:00
/* first scan the format to work out the header and body size */
va_start ( ap , format ) ;
for ( i = 0 ; format [ i ] ; i + + ) {
switch ( format [ i ] ) {
case ' U ' :
s = va_arg ( ap , char * ) ;
head_size + = 8 ;
2004-10-28 12:46:59 +00:00
n = push_ucs2_talloc ( pointers , ( void * * ) & pointers [ i ] . data , s ) ;
2004-09-22 21:45:52 +00:00
if ( n = = - 1 ) {
return False ;
}
pointers [ i ] . length = n ;
pointers [ i ] . length - = 2 ;
data_size + = pointers [ i ] . length ;
2004-06-28 06:46:27 +00:00
break ;
case ' A ' :
s = va_arg ( ap , char * ) ;
head_size + = 8 ;
2004-09-22 21:45:52 +00:00
n = push_ascii_talloc ( pointers , ( char * * ) & pointers [ i ] . data , s ) ;
if ( n = = - 1 ) {
return False ;
}
pointers [ i ] . length = n ;
pointers [ i ] . length - = 1 ;
data_size + = pointers [ i ] . length ;
2004-06-28 06:46:27 +00:00
break ;
case ' a ' :
n = va_arg ( ap , int ) ;
2004-09-22 21:45:52 +00:00
intargs [ i ] = n ;
2004-06-28 06:46:27 +00:00
s = va_arg ( ap , char * ) ;
2004-10-28 12:46:59 +00:00
n = push_ucs2_talloc ( pointers , ( void * * ) & pointers [ i ] . data , s ) ;
2004-09-22 21:45:52 +00:00
if ( n = = - 1 ) {
return False ;
}
pointers [ i ] . length = n ;
pointers [ i ] . length - = 2 ;
data_size + = pointers [ i ] . length + 4 ;
2004-06-28 06:46:27 +00:00
break ;
case ' B ' :
b = va_arg ( ap , uint8_t * ) ;
head_size + = 8 ;
2004-09-22 21:45:52 +00:00
pointers [ i ] . data = b ;
pointers [ i ] . length = va_arg ( ap , int ) ;
data_size + = pointers [ i ] . length ;
2004-06-28 06:46:27 +00:00
break ;
case ' b ' :
b = va_arg ( ap , uint8_t * ) ;
2004-09-22 21:45:52 +00:00
pointers [ i ] . data = b ;
pointers [ i ] . length = va_arg ( ap , int ) ;
head_size + = pointers [ i ] . length ;
2004-06-28 06:46:27 +00:00
break ;
case ' d ' :
n = va_arg ( ap , int ) ;
2004-09-22 21:45:52 +00:00
intargs [ i ] = n ;
2004-06-28 06:46:27 +00:00
head_size + = 4 ;
break ;
case ' C ' :
s = va_arg ( ap , char * ) ;
2004-11-29 12:01:46 +00:00
pointers [ i ] . data = ( uint8_t * ) s ;
2004-09-22 21:45:52 +00:00
pointers [ i ] . length = strlen ( s ) + 1 ;
head_size + = pointers [ i ] . length ;
2004-06-28 06:46:27 +00:00
break ;
}
}
va_end ( ap ) ;
/* allocate the space, then scan the format again to fill in the values */
* blob = data_blob_talloc ( mem_ctx , NULL , head_size + data_size ) ;
head_ofs = 0 ;
data_ofs = head_size ;
va_start ( ap , format ) ;
for ( i = 0 ; format [ i ] ; i + + ) {
switch ( format [ i ] ) {
case ' U ' :
case ' A ' :
2004-09-22 21:45:52 +00:00
case ' B ' :
n = pointers [ i ] . length ;
2004-06-28 06:46:27 +00:00
SSVAL ( blob - > data , head_ofs , n ) ; head_ofs + = 2 ;
SSVAL ( blob - > data , head_ofs , n ) ; head_ofs + = 2 ;
SIVAL ( blob - > data , head_ofs , data_ofs ) ; head_ofs + = 4 ;
2004-09-22 21:45:52 +00:00
if ( pointers [ i ] . data & & n ) /* don't follow null pointers... */
memcpy ( blob - > data + data_ofs , pointers [ i ] . data , n ) ;
2004-06-28 06:46:27 +00:00
data_ofs + = n ;
break ;
case ' a ' :
2004-09-22 21:45:52 +00:00
n = intargs [ i ] ;
2004-06-28 06:46:27 +00:00
SSVAL ( blob - > data , data_ofs , n ) ; data_ofs + = 2 ;
2004-09-22 21:45:52 +00:00
n = pointers [ i ] . length ;
SSVAL ( blob - > data , data_ofs , n ) ; data_ofs + = 2 ;
if ( n > = 0 ) {
memcpy ( blob - > data + data_ofs , pointers [ i ] . data , n ) ;
}
2004-06-28 06:46:27 +00:00
data_ofs + = n ;
break ;
case ' d ' :
2004-09-22 21:45:52 +00:00
n = intargs [ i ] ;
SIVAL ( blob - > data , head_ofs , n ) ;
head_ofs + = 4 ;
2004-06-28 06:46:27 +00:00
break ;
case ' b ' :
2004-09-22 21:45:52 +00:00
n = pointers [ i ] . length ;
memcpy ( blob - > data + head_ofs , pointers [ i ] . data , n ) ;
2004-06-28 06:46:27 +00:00
head_ofs + = n ;
break ;
case ' C ' :
2004-09-22 21:45:52 +00:00
n = pointers [ i ] . length ;
memcpy ( blob - > data + head_ofs , pointers [ i ] . data , n ) ;
head_ofs + = n ;
2004-06-28 06:46:27 +00:00
break ;
}
}
va_end ( ap ) ;
2004-09-22 21:45:52 +00:00
talloc_free ( pointers ) ;
2004-06-28 06:46:27 +00:00
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 ; \
}
/*
this is a tiny msrpc packet parser . This the the partner of msrpc_gen
format specifiers are :
U = unicode string ( output is unix string )
A = ascii string
B = data blob
b = data blob in header
d = word ( 4 bytes )
C = constant ascii string
*/
BOOL msrpc_parse ( TALLOC_CTX * mem_ctx , const DATA_BLOB * blob ,
const char * format , . . . )
{
int i ;
va_list ap ;
const char * * ps , * s ;
DATA_BLOB * b ;
size_t head_ofs = 0 ;
uint16_t len1 , len2 ;
uint32_t ptr ;
uint32_t * v ;
2005-10-01 01:04:34 +00:00
pstring p ;
2004-06-28 06:46:27 +00:00
va_start ( ap , format ) ;
for ( i = 0 ; format [ i ] ; i + + ) {
switch ( format [ i ] ) {
case ' U ' :
NEED_DATA ( 8 ) ;
len1 = SVAL ( blob - > data , head_ofs ) ; head_ofs + = 2 ;
len2 = SVAL ( blob - > data , head_ofs ) ; head_ofs + = 2 ;
ptr = IVAL ( blob - > data , head_ofs ) ; head_ofs + = 4 ;
2004-08-24 15:48:14 +00:00
ps = ( const char * * ) va_arg ( ap , char * * ) ;
2004-06-28 06:46:27 +00:00
if ( len1 = = 0 & & len2 = = 0 ) {
* ps = " " ;
} 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 ;
}
if ( len1 & 1 ) {
/* if odd length and unicode */
return False ;
}
if ( blob - > data + ptr < ( uint8_t * ) ptr | | blob - > data + ptr < blob - > data )
return False ;
if ( 0 < len1 ) {
2006-06-27 17:55:02 +00:00
pull_string ( p , blob - > data + ptr , sizeof ( p ) ,
len1 , STR_UNICODE | STR_NOALIGN ) ;
2005-10-01 01:04:34 +00:00
( * ps ) = talloc_strdup ( mem_ctx , p ) ;
if ( ! ( * ps ) ) {
2004-06-28 06:46:27 +00:00
return False ;
}
} else {
( * ps ) = " " ;
}
}
break ;
case ' A ' :
NEED_DATA ( 8 ) ;
len1 = SVAL ( blob - > data , head_ofs ) ; head_ofs + = 2 ;
len2 = SVAL ( blob - > data , head_ofs ) ; head_ofs + = 2 ;
ptr = IVAL ( blob - > data , head_ofs ) ; head_ofs + = 4 ;
2004-08-24 15:48:14 +00:00
ps = ( const char * * ) va_arg ( ap , char * * ) ;
2004-06-28 06:46:27 +00:00
/* make sure its in the right format - be strict */
if ( len1 = = 0 & & len2 = = 0 ) {
* ps = " " ;
} else {
if ( ( len1 ! = len2 ) | | ( ptr + len1 < ptr ) | | ( ptr + len1 < len1 ) | | ( ptr + len1 > blob - > length ) ) {
return False ;
}
if ( blob - > data + ptr < ( uint8_t * ) ptr | | blob - > data + ptr < blob - > data )
return False ;
if ( 0 < len1 ) {
2006-06-27 17:55:02 +00:00
pull_string ( p , blob - > data + ptr , sizeof ( p ) ,
len1 , STR_ASCII | STR_NOALIGN ) ;
2004-06-28 06:46:27 +00:00
( * ps ) = talloc_strdup ( mem_ctx , p ) ;
if ( ! ( * ps ) ) {
return False ;
}
} else {
( * ps ) = " " ;
}
}
break ;
case ' B ' :
NEED_DATA ( 8 ) ;
len1 = SVAL ( blob - > data , head_ofs ) ; head_ofs + = 2 ;
len2 = SVAL ( blob - > data , head_ofs ) ; head_ofs + = 2 ;
ptr = IVAL ( blob - > data , head_ofs ) ; head_ofs + = 4 ;
b = ( DATA_BLOB * ) va_arg ( ap , void * ) ;
if ( len1 = = 0 & & len2 = = 0 ) {
* b = data_blob_talloc ( mem_ctx , NULL , 0 ) ;
} 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 ;
}
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 ;
case ' b ' :
b = ( DATA_BLOB * ) va_arg ( ap , void * ) ;
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 ;
* b = data_blob_talloc ( mem_ctx , blob - > data + head_ofs , len1 ) ;
head_ofs + = len1 ;
break ;
case ' d ' :
v = va_arg ( ap , uint32_t * ) ;
NEED_DATA ( 4 ) ;
* v = IVAL ( blob - > data , head_ofs ) ; head_ofs + = 4 ;
break ;
case ' C ' :
s = va_arg ( ap , char * ) ;
if ( blob - > data + head_ofs < ( uint8_t * ) head_ofs | | blob - > data + head_ofs < blob - > data )
return False ;
r2552: Character set conversion and string handling updates.
The intial motivation for this commit was to merge in some of the
bugfixes present in Samba3's chrcnv and string handling code into
Samba4. However, along the way I found a lot of unused functions, and
decided to do a bit more...
The strlen_m code now does not use a fixed buffer, but more work is
needed to finish off other functions in str_util.c. These fixed
length buffers hav caused very nasty, hard to chase down bugs at some
sites.
The strupper_m() function has a strupper_talloc() to replace it (we
need to go around and fix more uses, but it's a start). Use of these
new functions will avoid bugs where the upper or lowercase version of
a string is a different length.
I have removed the push_*_allocate functions, which are replaced by
calls to push_*_talloc. Likewise, pstring and other 'fixed length'
wrappers are removed, where possible.
I have removed the first ('base pointer') argument, used by push_ucs2,
as the Samba4 way of doing things ensures that this is always on an
even boundary anyway. (It was used in only one place, in any case).
(This used to be commit dfecb0150627b500cb026b8a4932fe87902ca392)
2004-09-23 00:51:45 +00:00
head_ofs + = pull_string ( p , blob - > data + head_ofs , sizeof ( p ) ,
2004-06-28 06:46:27 +00:00
blob - > length - head_ofs ,
STR_ASCII | STR_TERMINATE ) ;
if ( strcmp ( s , p ) ! = 0 ) {
return False ;
}
break ;
}
}
va_end ( ap ) ;
return True ;
}