From 1cff5e480a046c3efa1b2ceae5a271ec5aa1e021 Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Wed, 3 Dec 2003 18:46:56 +0000 Subject: Did somemassive tidying --- src/nu_header.h | 12 ++++---- src/nu_server.c | 86 +++++++++++++++++++++------------------------------------ 2 files changed, 38 insertions(+), 60 deletions(-) diff --git a/src/nu_header.h b/src/nu_header.h index de43a08..378268c 100755 --- a/src/nu_header.h +++ b/src/nu_header.h @@ -7,7 +7,7 @@ typedef struct { BYTE netbios_command; BYTE netbios_flags; WORD netbios_size; - BYTE Protocol[4]; /* Protocol identifier 0xFF,"SMB" */ + DWORD Protocol; /* Protocol identifier 0xFF,"SMB" */ BYTE Command; /* Command Code, look below */ BYTE Status[4]; BYTE Flags; @@ -17,15 +17,17 @@ typedef struct { WORD ProcessID; WORD UserID; WORD MultiplexID; - BYTE WordCount; } SMB_HEADER; -#define SIZEOF_SMB_HEADER 37 +typedef struct { + BYTE WordCount; + WORD Buffer[0]; +} SMB_PARAMS; typedef struct { WORD ByteCount; BYTE Buffer[0]; -} SMB_HEADER2; +} SMB_BYTES; /* This is the protocol identifier, each smb request must begin with this double word @@ -40,7 +42,7 @@ typedef struct { a read-only subset of this. */ -enum { +typedef enum { SMB_COM_CREATE_DIRECTORY = 0x00, SMB_COM_DELETE_DIRECTORY = 0x01, SMB_COM_OPEN = 0x02, diff --git a/src/nu_server.c b/src/nu_server.c index 29819ee..64eecfe 100755 --- a/src/nu_server.c +++ b/src/nu_server.c @@ -11,7 +11,6 @@ static void bailout( char *reason ); static void sigint( int reason ) { bailout( "User interrupt." ); } -static void packet_dump( SMB_HEADER *buf ); static mainsock = -1; static childsock = -1; @@ -20,56 +19,48 @@ static QWORD getnttime( struct timeval *t ) { } static void netbios_read( SMB_HEADER **buf) { - BYTE bytes[4]; + DWORD bytes; ssize_t bytestoread; if( read( childsock, bytes, 4) < 4 ) bailout( "Short read." ); - bytestoread = htons(*(WORD*)(bytes+2)); + bytestoread = htons(((WORD*)bytes)[1]); if( (*buf = (SMB_HEADER*)realloc( *buf, 4 + bytestoread )) == NULL) bailout( "Out of memory."); - *(DWORD*)*buf = *(DWORD*)bytes; + *(DWORD*)*buf = bytes; if( read( childsock, ((BYTE*)*buf) + 4, bytestoread) != bytestoread ) bailout( "Short read." ); } -static void netbios_write( BYTE cmd, SMB_HEADER *buf, SMB_HEADER2 *buf2 ) { - const BYTE buf2_[2] = { 0, 0 }; - if(!buf2 ) buf2 = (SMB_HEADER2*)buf2_; +static void netbios_write( SMB_COMMAND cmd, + SMB_HEADER *buf, + SMB_PARAMS *buf2, + SMB_BYTES *buf3 ) { + BYTE buf_[4] = { 0, 0, 0, 0 }; + if(!buf2 ) buf2 = (SMB_PARAMS*)buf_; + if(!buf3 ) buf3 = (SMB_BYTES*)buf_; if( buf ) { - struct iovec iov[2] = { {buf, SIZEOF_SMB_HEADER + 2*buf->WordCount}, - {buf2, 2 + buf2->ByteCount} }; + struct iovec iov[3] = { {buf , sizeof(SMB_HEADER) }, + {buf2, 1 + buf2->WordCount * 2}, + {buf3, 2 + buf3->ByteCount } }; buf->netbios_command = cmd; buf->netbios_flags = 0; - buf->netbios_size = htons( SIZEOF_SMB_HEADER - 4 + - 2 * buf->WordCount + - 2 + buf2->ByteCount ); + buf->netbios_size = htons( sizeof(SMB_HEADER) - 4 + + 1 + buf2->WordCount * 2 + + 2 + buf3->ByteCount ); buf->Flags = 0x88; buf->Flags2 = 0x4001; - if( writev( childsock, iov, 2 ) < htons( buf->netbios_size ) + 4 ) + if( writev( childsock, iov, 3 ) < htons( buf->netbios_size ) + 4 ) bailout( "Write failed." ); } else { - const BYTE buf_[4] = { cmd, 0, 0, 0 }; + *buf_ = cmd; if( write( childsock, buf_, 4 ) < 4) bailout( "Write failed." ); } } -static void packet_dump( SMB_HEADER *buf ) { - fprintf( stderr, "netbios_cmd, flag, size = %02X, %02X, %04X\n", buf->netbios_command, buf->netbios_flags, buf->netbios_size ); - fprintf( stderr, "Protocol = %08X\n", *(DWORD*)&buf->Protocol); - fprintf( stderr, "Command = %02X\n", buf->Command); - fprintf( stderr, "Status = %08X\n", *(DWORD*)&buf->Status); - fprintf( stderr, "Flags, Flags2 = %02X, %04X\n", buf->Flags, buf->Flags2); - fprintf( stderr, "Pad = %04X %04X %04X %04X %04X %04X\n", - buf->Pad[0], buf->Pad[1], buf->Pad[2], - buf->Pad[3], buf->Pad[4], buf->Pad[5] ); - fprintf( stderr, "TreeID,ProcessID,UserID = %04X, %04X, %04X\n", buf->TreeID, buf->ProcessID, buf->UserID); - fprintf( stderr, "MultiplexID, WordCount = %04X, %02X\n", buf->MultiplexID, buf->WordCount); -} - static void child( ) { SMB_HEADER *inpacket = NULL; DWORD bytesread; @@ -81,53 +72,38 @@ static void child( ) { netbios_read( &inpacket ); if( inpacket->netbios_command != 0x81 ) bailout( "No session request"); - netbios_write( 0x82, NULL, NULL ); + netbios_write( 0x82, NULL, NULL, NULL ); while( 1 ) { - WORD *ParameterWords; netbios_read( &inpacket ); - packet_dump( inpacket ); - ParameterWords = (WORD*)(((BYTE*)inpacket)+SIZEOF_SMB_HEADER); if( inpacket->netbios_command != 0 ) bailout( "Unhandled netbios command" ); - if( *(DWORD*)&inpacket->Protocol != SMB_HEADER_PROTOCOL_MAGIC ) + if( inpacket->Protocol != SMB_HEADER_PROTOCOL_MAGIC ) bailout( "Protocol identifier mismatch"); switch( inpacket->Command ) { case SMB_COM_NEGOTIATE: { - BYTE myself[] = { 8,0,0x67,0x61,0x74,0x6c,0x69,0x6e,0x67,0x00 }; + const BYTE bytes[] = { 8,0,0x67,0x61,0x74,0x6c,0x69,0x6e,0x67,0 }; + WORD params[] = { 0x0511, 0x0000, 0x0001, 0x0001, + 0x0000, 0x0100, 0x0000, 0x0100, + 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000 }; struct timeval t; gettimeofday( &t, NULL ); - inpacket = (SMB_HEADER*)realloc( inpacket, SIZEOF_SMB_HEADER + 17 * 2 ); *(DWORD*)&inpacket->Status = STATUS_SUCCESS; - - ParameterWords = (WORD*)(((BYTE*)inpacket)+SIZEOF_SMB_HEADER-1); - ParameterWords[0] = 0x0511; /* Protocol Version 5, 17 bytes */ - ParameterWords[1] = 0; /* security mode: share, no c/r */ - ParameterWords[2] = 1; /* Max pending */ - ParameterWords[3] = 1; /* Only one VC */ - ParameterWords[4] = 0; /* Max Buffer Size */ - ParameterWords[5] = 0x100; /* Max Buffer Size #2 */ - ParameterWords[6] = 0; /* Max Raw Size */ - ParameterWords[7] = 0x100; /* Max Raw Size #2 */ - ParameterWords[8] = getpid(); /* unique id */ - ParameterWords[9] = getppid(); /* unique id #2 */ - ParameterWords[10] = 0; /* Capabilities */ - ParameterWords[11] = 0; /* Capabilities #2 */ -*(QWORD*)&ParameterWords[12] = getnttime( &t ); - ParameterWords[16] = 0; -*(BYTE *)&ParameterWords[17] = 0; - netbios_write( 0, inpacket, (SMB_HEADER2*)myself); + params[8] = getpid(); params[9] = getppid(); + *(QWORD*)¶ms[12] = getnttime( &t ); + netbios_write( 0, inpacket, (SMB_PARAMS*)params ,(SMB_BYTES*)bytes); break; } default: { fprintf( stderr, "Got message: %02X\n", inpacket->Command ); - inpacket->WordCount = 0; -*(DWORD*)&inpacket->Status = 0x00400002; - netbios_write( 0, inpacket, NULL ); + *(DWORD*)&inpacket->Status = 0x00400002; + netbios_write( 0, inpacket, NULL, NULL ); break; } } -- cgit v1.2.3