/* Copyright (c) 1994,1995 Laishev Ruslan R. All Right reserved !!! */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "smbdef.h" #include #include #include "zcrypt.h" #include #include #include #include #include /* INET symbol definitions */ typedef unsigned int uint; typedef unsigned long ulong; typedef signed int sint; typedef signed long slong; typedef signed int sshort; typedef signed long ushort; extern "C" { int smb$initialize (...); int smb$check_for_message (...); int smb$read_message (...); int smb$read_message_item (...); int smb$send_to_jobctl (...); int sys$assign (...); int sys$qiow (...); } int Msg$Process (ulong); void log_error (const char *, int); int log_message (const char *); int VER_Write (const char *,const char *,int); char Host [512]; int Port; char File [512]; ulong stream = 0; ulong structure_level = SMBMSG$K_STRUCTURE_LEVEL ; ulong status; ulong request; char Msg_Buff [ 1024 ]; $DESCRIPTOR (Msg_Buff_dsc,Msg_Buff); #define DEBUG 1 int sock_1; /* socket */ static char message[16]; static struct sockaddr_in sock2_name; /* Address struct for socket2.*/ struct hostent hostentstruct; /* Storage for hostent data. */ struct hostent *hostentptr; /* Pointer to hostent data. */ void main (void) { int privs; privs = PRV$M_NETMBX | PRV$M_TMPMBX; status= sys$setprv(1,&privs,0,0); if ( !$VMS_STATUS_SUCCESS (status) ) { #ifdef DEBUG log_error ("sys$setprv",status); #endif lib$stop (status); } /* Init */ if ( SS$_NORMAL != (status = smb$initialize (&structure_level)) ) { log_error ("smb$initialize",status); lib$stop (status); } #ifdef DEBUG else log_message ("smb$initialize - Ok"); #endif while (1) { /* Wait for message */ if ( SS$_NORMAL != (status = smb$check_for_message ()) ) { #ifdef DEBUG log_message ("smb$check_for_message - None"); #endif sleep (16); continue; } #ifdef DEBUG else log_message ("smb$check_for_message - Ok"); #endif /* Process messages */ if ( SS$_NORMAL != (status = smb$read_message (&stream,&Msg_Buff_dsc,&request)) ) { log_error ("smb$read_message",status); lib$stop (status); } #ifdef DEBUG else log_message ("smb$read_message - Ok"); #endif Msg$Process (request); } sys$exit(SS$_NORMAL); } /* --------------------------------------------------------------------- */ int Msg$Process (ulong request) { ulong context, item_code; ushort size; char ports [ 16 ]; char buff [512]; $DESCRIPTOR(buff_dsc,buff); #ifdef DEBUG char buff_debug [1024]; #endif switch (request) { /* --------------------------------------------------------------------- */ case SMBMSG$K_START_STREAM: #ifdef DEBUG log_message("SMBMSG$K_START_STREAM"); #endif context = 0; item_code = SMBMSG$K_DEVICE_NAME; #ifdef DEBUG log_message("SMBMSG$K_DEVICE_NAME"); #endif if ( SS$_NORMAL != (status = smb$read_message_item (&Msg_Buff_dsc,&context,&item_code, &buff_dsc,&size)) ) { if (SMB$_NOMOREITEMS != status) { log_error ("smb$read_message_item",status); lib$stop (status); } } #ifdef DEBUG sprintf(buff_debug,"Size=%d",size); log_message (buff_debug); #endif buff [ size ] = 0; if ( sscanf ( buff, "\"%[^:]:%[^\"]",Host,ports) < 2 ) { log_error ("Please check value with /ON=..",SS$_IVDEVNAM); sys$exit (SS$_IVDEVNAM); } if ( gethostbyname (Host) == NULL) { log_error ("Checking device name",SS$_IVDEVNAM); sys$exit (SS$_IVDEVNAM); } Port = atoi (ports); if ( (Port <= 0) || (Port > 16000) ) { log_error ("Checking device name",SS$_IVDEVNAM); sys$exit (SS$_IVDEVNAM); } #ifdef DEBUG sprintf(buff,"Starting Host=%s, Port=%d",Host,Port); log_message (buff); #endif if ( SS$_NORMAL != (status = smb$send_to_jobctl (&stream,&request)) ) { log_error ("smb$send_to_jobctl",status); lib$stop (status); } #ifdef DEBUG log_message("SMBMSG$K_START_STREAM - Ok"); #endif break; /* --------------------------------------------------------------------- */ case SMBMSG$K_START_TASK: #ifdef DEBUG log_message("SMBMSG$K_START_TASK"); #endif context = 0; item_code = 0; while ( (item_code != SMBMSG$K_FILE_SPECIFICATION) && (status != SMB$_NOMOREITEMS) ) { if ( SS$_NORMAL != (status = smb$read_message_item (&Msg_Buff_dsc,&context,&item_code, &buff_dsc,&size)) ) { if (SMB$_NOMOREITEMS != status) { log_error ("smb$read_message_item",status); lib$stop (status); } } }; /* end while */ #ifdef DEBUG sprintf(buff_debug,"Size=%ld",size); log_message (buff_debug); #endif if (SMB$_NOMOREITEMS != status) { buff [ size ] = 0; strcpy (File,buff); #ifdef DEBUG sprintf(buff,"Starting send %s to %s:%d", File,Host,Port); log_message (buff); #endif if ( SS$_NORMAL != (status = smb$send_to_jobctl (&stream,&request)) ) { log_error ("smb$send_to_jobctl - SMBMSG$K_START_TASK",status); lib$stop (status); } #ifdef DEBUG else log_message ("smb$send_to_jobctl - SMBMSG$K_START_TASK"); #endif } VER_Write (File,Host,Port); request = SMBMSG$K_TASK_COMPLETE; if ( SS$_NORMAL != (status = smb$send_to_jobctl (&stream,&request)) ) { log_error ("smb$send_to_jobctl - SMBMSG$K_TASK_COMPLETE",status); lib$stop (status); } #ifdef DEBUG else log_message ("smb$send_to_jobctl - SMBMSG$K_TASK_COMPLETE"); #endif break; /* --------------------------------------------------------------------- */ case SMBMSG$K_STOP_STREAM: #ifdef DEBUG log_message("SMBMSG$K_STOP_STREAM"); #endif sys$exit(0); case SMBMSG$K_RESET_STREAM: #ifdef DEBUG log_message("SMBMSG$K_RESET_STREAM"); #endif sys$exit(0); case SMBMSG$K_STOP_TASK: #ifdef DEBUG log_message("SMBMSG$K_STOP_TASK"); #endif case SMBMSG$K_PAUSE_TASK: #ifdef DEBUG log_message("SMBMSG$K_PAUSE_TASK"); #endif sys$exit(0); case SMBMSG$K_RESUME_TASK: #ifdef DEBUG log_message("SMBMSG$K_RESUME_TASK"); #endif default: #ifdef DEBUG log_message("Undefined SMB$READ_MESSAGE:request"); #endif #ifdef DEBUG log_message ("smb$send_to_jobctl - "); #endif if ( SS$_NORMAL != (status = smb$send_to_jobctl (&stream,&request)) ) { log_error ("smb$send_to_jobctl",status); lib$stop (status); } #ifdef DEBUG else log_message ("smb$send_to_jobctl - Ok"); #endif } /* end switch */ return 0; } /* --------------------------------------------------------------------- */ void log_error ( const char *prefix, int vms_status ) { char msg[1024]; sprintf (msg, "%s,Error code=:%d",prefix,vms_status ); log_message ( msg ); if ( vms_status ) { sprintf (msg, "Error text=:%s", strerror ( EVMSERR, vms_status ) ); log_message ( msg ); } } int log_message ( const char *msg ) { FILE *logfile; unsigned long current_time; char *current_time_str; char *log_file_name; int time_len; if ( ! (log_file_name = getenv ("VER_IP_SYM_LOG")) ) return 0; /* log_file_name = "ver_ip_sym.log"; */ if ( ! (logfile = fopen ( log_file_name, "a+" )) ) { if ( ! (logfile = fopen ( log_file_name, "w" )) ) { perror ("Open Log file"); sys$exit (0); } } time ( ¤t_time ); current_time_str = ctime ( ¤t_time ); time_len = strlen ( current_time_str ); fprintf (logfile, "%.*s, %s\n", time_len - 1, current_time_str, msg); fclose (logfile); } /* --------------------------------------------------------------------- */ void VER_sock_shut (int sock) { if ( 0 > shutdown(sock,2) ) log_error ("shutdown",errno); if ( 0 > close (sock) ) log_error ("close",errno); } /* --------------------------------------------------------------------- */ int VER_sock_init (char *Host,int Port) { if ( 0 > (sock_1 = socket (AF_INET, SOCK_STREAM, 0)) ) { log_error( "socket",errno); sys$exit(0); } if ((hostentptr = gethostbyname (Host)) == NULL) { log_error( "gethostbyname",errno); VER_sock_shut(sock_1); sys$exit(0); } hostentstruct = *hostentptr; sock2_name.sin_family = hostentstruct.h_addrtype; sock2_name.sin_port = htons(Port); sock2_name.sin_addr = * ((struct in_addr *) hostentstruct.h_addr); if (0 > connect(sock_1,(struct sockaddr *) &sock2_name, sizeof (sock2_name)) ) { log_error("connect",errno); VER_sock_shut(sock_1); return -1; } return 0; } /*-----------------------------------------------------------*/ int VER_Write (const char *File,const char *Host,int Port) { char buff1 [16384]; char buff0 [ 512 ]; FILE *pinp; int linp,lout; TZCryptError s; const char Password [] = "gl5d9"; const char FingerPrint[]= "Ver"; int wait_f; /*-----------------------------------------------------------*/ #ifdef DEBUG log_message ("Open file"); #endif if ( NULL == (pinp = fopen(File,"r")) ) { log_error("fopen",errno); return -1; } #ifdef DEBUG log_message ("Read file"); #endif if ( 0 > (linp = fread (&buff1,1,sizeof(buff1),pinp)) ) { log_error("fread",errno); return -1; } fclose (pinp); if ( linp == sizeof(buff1) ) { log_error("file is too big",0); return -1; } if ( ceOk != (s = Encrypt (buff1,linp,&linp,sizeof(buff1),FingerPrint,Password)) ) { log_error(CryptError(s),0); return -1; } #ifdef DEBUG sprintf(buff0,"\n Size = %d",linp); log_message ("Send buffer to IP destination"); #endif wait_f = 0; while (1) { if (wait_f) { sleep (30); wait_f = 0; } #ifdef DEBUG log_message ("Init connection"); #endif if ( !VER_sock_init (Host,Port) ) { #ifdef DEBUG log_message ("Send to remote host"); #endif if ( 0 > send(sock_1,buff1,linp, 0) ) { log_error ("send",errno); VER_sock_shut(sock_1); wait_f = 1; continue; } #ifdef DEBUG log_message ("Wait receive message"); #endif buff0 [0 ] = 0; if ( 0 > (lout = recv(sock_1, buff0 ,sizeof (buff0), 0)) ) { log_error ("recv",errno); VER_sock_shut(sock_1); wait_f = 1; continue; } VER_sock_shut(sock_1); #ifdef DEBUG log_message ("Check 'OkMsg'"); #endif if ( ceOk != (s = Decrypt (buff0,lout,&lout,FingerPrint,Password)) ) { #ifdef DEBUG log_message (buff0); sprintf(buff0,"Size=%d,Decrypt error=%d",lout,s); log_message (buff0); #endif log_error(CryptError(s),0); wait_f = 1; continue; } if ( !strcmp(buff0,"OkMsg") ) { #ifdef DEBUG log_message ("Send successfull"); #endif return 0; } } else wait_f = 1; }; }