summaryrefslogtreecommitdiff
path: root/src/nu_server.c
blob: 942be1b96777273674cb85908cee462aafc70bf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/ioctl.h>

#include "nu_header.h"

static void bailout( char *reason );
static mainsock = -1;  
static childsock = -1; 

static void netbios_read( SMB_HEADER **buf) {
  BYTE    bytes[4];
  ssize_t bytesread, bytestoread;

  if( read( childsock, bytes, 4) < 4 )
    bailout( "Short read." );
  bytestoread = htons(*(WORD*)(2+bytes));
  if( (*buf = (SMB_HEADER*)realloc( *buf, 4 + bytestoread )) == NULL)
    bailout( "Out of memory");
  *(DWORD*)*buf = *(DWORD*)bytes;
  bytesread = read( childsock, ((BYTE*)buf) + 4, bytestoread);
  if( bytesread != bytestoread )
    bailout( "Short read." );
}

static void netbios_write( BYTE command, BYTE *buf, WORD size ) {
  BYTE netbios_header[4] = { command, 0, size >> 8, size & 255 };
  if( write( childsock, netbios_header, 4 ) <= 0 ||
      write( childsock, buf, size )         <  0 )
    bailout( "Write failed." );
}

static void child( ) {
  SMB_HEADER *inpacket = NULL;
  DWORD       bytesread;

  /* I should spare that code... */
  if( mainsock != -1 ) { close( mainsock ); mainsock = -1; }

  /* Try to answer first netbios packet */
  netbios_read( &inpacket );
  if( inpacket->netbios_command != 0x81 )
    bailout( "No session request");
  netbios_write( 0x82, NULL, 0 );

  while( 1 ) {
    netbios_read( &inpacket );
    if( inpacket->netbios_command != 0 )
      bailout( "Unhandled netbios command" );
    if( inpacket->Protocol != SMB_HEADER_PROTOCOL_MAGIC )
      bailout( "Protocol identifier mismatch");

    switch( inpacket->Command ) {
      case SMB_COM_NEGOTIATE:
      {
        BYTE outblock[5] = { 0xff,0,0,0,0 };
        netbios_write( 0, outblock, sizeof( outblock ));
        break;
      }
      default:
      {
        fprintf( stderr, "Got message: %02X\n", inpacket->Command );
        break;
      }
    }

  } /* End main loop */
}

void sigint( int reason ) { bailout( "User interrupt." ); }

int main()
{
  struct sockaddr_in sa;
  int    l=1;
         
  signal( SIGINT, sigint);

  bzero( &sa, sizeof( sa));
  sa.sin_family      = PF_INET;
  sa.sin_port        = htons( 139 );
  sa.sin_addr.s_addr = INADDR_ANY;

  if( ( mainsock = socket( PF_INET, SOCK_STREAM, 0) ) == -1)
    bailout( "Could not open socket");
  setsockopt( mainsock, SOL_SOCKET, SO_REUSEPORT, &l, sizeof(l));
  if( bind( mainsock, (struct sockaddr *)&sa, sizeof( sa)) != 0)
    bailout( "Could not bind socket");
  if( listen( mainsock, 1024) != 0 )
    bailout( "Could not make socket listen");
 
  while( 1 ) {
    struct sockaddr otherend;
    int             size = sizeof( otherend );
     
    if( ( childsock = accept( mainsock, &otherend, &size) ) == -1)
      bailout( "Socket Broke.");
    if (!fork()) child( );
  }
}

/* Graceful exit. */
static void bailout( char *reason) {
    fputs( reason, stderr);
    fputs( "\nCleaning up.\n", stderr);
    if( mainsock != -1 )
        close( mainsock );
    if( childsock != -1 ) {
        shutdown( childsock, SHUT_RDWR);
        close( childsock );
    }
    exit( 0 );
}