summaryrefslogtreecommitdiff
path: root/src/nu_server.c
diff options
context:
space:
mode:
authorerdgeist <>2003-12-03 17:17:23 +0000
committererdgeist <>2003-12-03 17:17:23 +0000
commit243841f9486c8b922b8e071ca4b96125808353fe (patch)
tree18eebff0bca0cd63694df1be35463916e067912e /src/nu_server.c
parentff183bd51957027765d8930c4eccca418f26d1ad (diff)
Negotiation Packet handling works
Diffstat (limited to 'src/nu_server.c')
-rwxr-xr-xsrc/nu_server.c99
1 files changed, 83 insertions, 16 deletions
diff --git a/src/nu_server.c b/src/nu_server.c
index 942be1b..29819ee 100755
--- a/src/nu_server.c
+++ b/src/nu_server.c
@@ -1,3 +1,5 @@
1#include <time.h>
2#include <fcntl.h>
1#include <signal.h> 3#include <signal.h>
2#include <sys/types.h> 4#include <sys/types.h>
3#include <sys/socket.h> 5#include <sys/socket.h>
@@ -8,29 +10,64 @@
8#include "nu_header.h" 10#include "nu_header.h"
9 11
10static void bailout( char *reason ); 12static void bailout( char *reason );
13static void sigint( int reason ) { bailout( "User interrupt." ); }
14static void packet_dump( SMB_HEADER *buf );
11static mainsock = -1; 15static mainsock = -1;
12static childsock = -1; 16static childsock = -1;
13 17
18static QWORD getnttime( struct timeval *t ) {
19 return 10000000ll * ( t->tv_sec + 11644473600ll ) + t->tv_usec * 10ll;
20}
21
14static void netbios_read( SMB_HEADER **buf) { 22static void netbios_read( SMB_HEADER **buf) {
15 BYTE bytes[4]; 23 BYTE bytes[4];
16 ssize_t bytesread, bytestoread; 24 ssize_t bytestoread;
17 25
18 if( read( childsock, bytes, 4) < 4 ) 26 if( read( childsock, bytes, 4) < 4 )
19 bailout( "Short read." ); 27 bailout( "Short read." );
20 bytestoread = htons(*(WORD*)(2+bytes)); 28 bytestoread = htons(*(WORD*)(bytes+2));
21 if( (*buf = (SMB_HEADER*)realloc( *buf, 4 + bytestoread )) == NULL) 29 if( (*buf = (SMB_HEADER*)realloc( *buf, 4 + bytestoread )) == NULL)
22 bailout( "Out of memory"); 30 bailout( "Out of memory.");
23 *(DWORD*)*buf = *(DWORD*)bytes; 31 *(DWORD*)*buf = *(DWORD*)bytes;
24 bytesread = read( childsock, ((BYTE*)buf) + 4, bytestoread); 32 if( read( childsock, ((BYTE*)*buf) + 4, bytestoread) != bytestoread )
25 if( bytesread != bytestoread )
26 bailout( "Short read." ); 33 bailout( "Short read." );
27} 34}
28 35
29static void netbios_write( BYTE command, BYTE *buf, WORD size ) { 36static void netbios_write( BYTE cmd, SMB_HEADER *buf, SMB_HEADER2 *buf2 ) {
30 BYTE netbios_header[4] = { command, 0, size >> 8, size & 255 }; 37 const BYTE buf2_[2] = { 0, 0 };
31 if( write( childsock, netbios_header, 4 ) <= 0 || 38 if(!buf2 ) buf2 = (SMB_HEADER2*)buf2_;
32 write( childsock, buf, size ) < 0 ) 39 if( buf ) {
33 bailout( "Write failed." ); 40 struct iovec iov[2] = { {buf, SIZEOF_SMB_HEADER + 2*buf->WordCount},
41 {buf2, 2 + buf2->ByteCount} };
42
43 buf->netbios_command = cmd;
44 buf->netbios_flags = 0;
45 buf->netbios_size = htons( SIZEOF_SMB_HEADER - 4 +
46 2 * buf->WordCount +
47 2 + buf2->ByteCount );
48 buf->Flags = 0x88;
49 buf->Flags2 = 0x4001;
50
51 if( writev( childsock, iov, 2 ) < htons( buf->netbios_size ) + 4 )
52 bailout( "Write failed." );
53 } else {
54 const BYTE buf_[4] = { cmd, 0, 0, 0 };
55 if( write( childsock, buf_, 4 ) < 4)
56 bailout( "Write failed." );
57 }
58}
59
60static void packet_dump( SMB_HEADER *buf ) {
61 fprintf( stderr, "netbios_cmd, flag, size = %02X, %02X, %04X\n", buf->netbios_command, buf->netbios_flags, buf->netbios_size );
62 fprintf( stderr, "Protocol = %08X\n", *(DWORD*)&buf->Protocol);
63 fprintf( stderr, "Command = %02X\n", buf->Command);
64 fprintf( stderr, "Status = %08X\n", *(DWORD*)&buf->Status);
65 fprintf( stderr, "Flags, Flags2 = %02X, %04X\n", buf->Flags, buf->Flags2);
66 fprintf( stderr, "Pad = %04X %04X %04X %04X %04X %04X\n",
67 buf->Pad[0], buf->Pad[1], buf->Pad[2],
68 buf->Pad[3], buf->Pad[4], buf->Pad[5] );
69 fprintf( stderr, "TreeID,ProcessID,UserID = %04X, %04X, %04X\n", buf->TreeID, buf->ProcessID, buf->UserID);
70 fprintf( stderr, "MultiplexID, WordCount = %04X, %02X\n", buf->MultiplexID, buf->WordCount);
34} 71}
35 72
36static void child( ) { 73static void child( ) {
@@ -44,25 +81,53 @@ static void child( ) {
44 netbios_read( &inpacket ); 81 netbios_read( &inpacket );
45 if( inpacket->netbios_command != 0x81 ) 82 if( inpacket->netbios_command != 0x81 )
46 bailout( "No session request"); 83 bailout( "No session request");
47 netbios_write( 0x82, NULL, 0 ); 84 netbios_write( 0x82, NULL, NULL );
48 85
49 while( 1 ) { 86 while( 1 ) {
87 WORD *ParameterWords;
50 netbios_read( &inpacket ); 88 netbios_read( &inpacket );
89 packet_dump( inpacket );
90 ParameterWords = (WORD*)(((BYTE*)inpacket)+SIZEOF_SMB_HEADER);
91
51 if( inpacket->netbios_command != 0 ) 92 if( inpacket->netbios_command != 0 )
52 bailout( "Unhandled netbios command" ); 93 bailout( "Unhandled netbios command" );
53 if( inpacket->Protocol != SMB_HEADER_PROTOCOL_MAGIC ) 94 if( *(DWORD*)&inpacket->Protocol != SMB_HEADER_PROTOCOL_MAGIC )
54 bailout( "Protocol identifier mismatch"); 95 bailout( "Protocol identifier mismatch");
55 96
56 switch( inpacket->Command ) { 97 switch( inpacket->Command ) {
57 case SMB_COM_NEGOTIATE: 98 case SMB_COM_NEGOTIATE:
58 { 99 {
59 BYTE outblock[5] = { 0xff,0,0,0,0 }; 100 BYTE myself[] = { 8,0,0x67,0x61,0x74,0x6c,0x69,0x6e,0x67,0x00 };
60 netbios_write( 0, outblock, sizeof( outblock )); 101 struct timeval t; gettimeofday( &t, NULL );
102
103 inpacket = (SMB_HEADER*)realloc( inpacket, SIZEOF_SMB_HEADER + 17 * 2 );
104 *(DWORD*)&inpacket->Status = STATUS_SUCCESS;
105
106 ParameterWords = (WORD*)(((BYTE*)inpacket)+SIZEOF_SMB_HEADER-1);
107 ParameterWords[0] = 0x0511; /* Protocol Version 5, 17 bytes */
108 ParameterWords[1] = 0; /* security mode: share, no c/r */
109 ParameterWords[2] = 1; /* Max pending */
110 ParameterWords[3] = 1; /* Only one VC */
111 ParameterWords[4] = 0; /* Max Buffer Size */
112 ParameterWords[5] = 0x100; /* Max Buffer Size #2 */
113 ParameterWords[6] = 0; /* Max Raw Size */
114 ParameterWords[7] = 0x100; /* Max Raw Size #2 */
115 ParameterWords[8] = getpid(); /* unique id */
116 ParameterWords[9] = getppid(); /* unique id #2 */
117 ParameterWords[10] = 0; /* Capabilities */
118 ParameterWords[11] = 0; /* Capabilities #2 */
119*(QWORD*)&ParameterWords[12] = getnttime( &t );
120 ParameterWords[16] = 0;
121*(BYTE *)&ParameterWords[17] = 0;
122 netbios_write( 0, inpacket, (SMB_HEADER2*)myself);
61 break; 123 break;
62 } 124 }
63 default: 125 default:
64 { 126 {
65 fprintf( stderr, "Got message: %02X\n", inpacket->Command ); 127 fprintf( stderr, "Got message: %02X\n", inpacket->Command );
128 inpacket->WordCount = 0;
129*(DWORD*)&inpacket->Status = 0x00400002;
130 netbios_write( 0, inpacket, NULL );
66 break; 131 break;
67 } 132 }
68 } 133 }
@@ -70,8 +135,6 @@ static void child( ) {
70 } /* End main loop */ 135 } /* End main loop */
71} 136}
72 137
73void sigint( int reason ) { bailout( "User interrupt." ); }
74
75int main() 138int main()
76{ 139{
77 struct sockaddr_in sa; 140 struct sockaddr_in sa;
@@ -86,7 +149,11 @@ int main()
86 149
87 if( ( mainsock = socket( PF_INET, SOCK_STREAM, 0) ) == -1) 150 if( ( mainsock = socket( PF_INET, SOCK_STREAM, 0) ) == -1)
88 bailout( "Could not open socket"); 151 bailout( "Could not open socket");
152#ifdef SO_REUSEPORT
89 setsockopt( mainsock, SOL_SOCKET, SO_REUSEPORT, &l, sizeof(l)); 153 setsockopt( mainsock, SOL_SOCKET, SO_REUSEPORT, &l, sizeof(l));
154#else
155 setsockopt( mainsock, SOL_SOCKET, SO_REUSEADDR, &l, sizeof(l));
156#endif
90 if( bind( mainsock, (struct sockaddr *)&sa, sizeof( sa)) != 0) 157 if( bind( mainsock, (struct sockaddr *)&sa, sizeof( sa)) != 0)
91 bailout( "Could not bind socket"); 158 bailout( "Could not bind socket");
92 if( listen( mainsock, 1024) != 0 ) 159 if( listen( mainsock, 1024) != 0 )