home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / reg.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  13KB  |  456 lines

  1. #include <windows.h>
  2.  
  3. #define _PROTOTYP( func, parms ) func parms
  4. #include "ckoetc.h"
  5. #include "ckcxla.h"
  6. #include "ckuxla.h"
  7. #include <time.h>
  8.  
  9. static long crcta[16] = { 0L, 010201L, 020402L, 030603L, 041004L,
  10.   051205L, 061406L, 071607L, 0102010L, 0112211L, 0122412L, 0132613L, 0143014L,
  11.   0153215L, 0163416L, 0173617L };
  12.  
  13. static long crctb[16] = { 0L, 010611L, 021422L, 031233L, 043044L,
  14.   053655L, 062466L, 072277L, 0106110L, 0116701L, 0127532L, 0137323L, 0145154L,
  15.   0155745L, 0164576L, 0174367L };
  16.  
  17. /* 16-bit CRC */
  18.  
  19. unsigned int
  20. chk3(register CHAR *pkt) {
  21.     register long c, crc;
  22.     for (crc = 0; *pkt != '\0'; pkt++) {
  23.     c = crc ^ (long)(*pkt);
  24.     crc = (crc >> 8) ^ (crcta[(c & 0xF0) >> 4] ^ crctb[c & 0x0F]);
  25.     }
  26.     return((unsigned int) (crc & 0xFFFF));
  27. }
  28.  
  29. #define FILECOUNT 2
  30.  
  31. CHAR * FileName[FILECOUNT] ;
  32. HANDLE hFile[FILECOUNT] ;
  33. HANDLE hFileMapping[FILECOUNT] ;
  34. CHAR * pView[FILECOUNT] ;
  35. struct ck_registration * RegBlock[FILECOUNT] ;
  36. FILETIME ftLastAccess[FILECOUNT], ftCreation[FILECOUNT], ftLastWrite[FILECOUNT] ;
  37. struct ck_registration NewRegBlock;
  38. char *zinptr=NULL;
  39. int zincnt=0, zoutcnt=0;
  40. int byteorder=0;
  41. char *zoutptr=NULL;
  42. void zdstuff(char c) {};
  43.  
  44. /* The following is to avoid warnings from ckuxla.obj during linking */
  45. int deblog = 0;
  46. int dodebug(int flag, char * s1, char * s2, int n) {
  47.     return(0);
  48. }
  49.  
  50.  
  51. int
  52. ckOpenFile(int i)
  53. {
  54.     hFile[i] = CreateFile(
  55.                    FileName[i],    // address of name of the file 
  56.                    GENERIC_READ | GENERIC_WRITE,
  57.                    0,     // share mode 
  58.                    NULL,// address of security descriptor 
  59.                    OPEN_EXISTING,    // how to create 
  60.                    FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,    // file attributes 
  61.                    NULL     // handle of file with attributes to copy  
  62.    );
  63.  
  64.     if ( !hFile[i] || (ULONG) hFile[i] == -1L) {
  65.         switch ( i ) {
  66.         case 0:
  67.             printf("switching to k2.exe\n");
  68.             FileName[i] = "k2.exe";
  69.             break;
  70.         case 1:
  71.             printf("switching to k2dial.exe\n");
  72.             FileName[i] = "k2dial.exe";
  73.             break;
  74.         }
  75.         hFile[i] = CreateFile(FileName[i],    // address of name of the file 
  76.                                GENERIC_READ | GENERIC_WRITE,
  77.                                0,     // share mode 
  78.                                NULL,// address of security descriptor 
  79.                                OPEN_EXISTING,    // how to create 
  80.                                FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,    // file attributes 
  81.                                NULL     // handle of file with attributes to copy  
  82.                                );                              
  83.  
  84.         if ( !hFile[i] || (ULONG) hFile[i] == -1L ) {
  85.             return 0;
  86.         }
  87.     }
  88.     
  89.    GetFileTime( hFile[i], &ftCreation[i], &ftLastAccess[i], &ftLastWrite[i] ) ;
  90.    hFileMapping[i] = CreateFileMapping( hFile[i],
  91.                                             NULL,
  92.                                             PAGE_READWRITE,
  93.                                             0,
  94.                                             0,
  95.                                             NULL );
  96.  
  97.    pView[i] = (CHAR *) MapViewOfFile( hFileMapping[i],
  98.                                  FILE_MAP_READ | FILE_MAP_WRITE,
  99.                                  0,0,0);
  100.  
  101.     if ( !pView[i] ) {
  102.         return 0;
  103.     }
  104.    return 1;
  105. }
  106.  
  107. int 
  108. ckCloseFile( int i ) 
  109. {
  110.    if (pView[i])
  111.       UnmapViewOfFile( pView[i] ) ;
  112.    if (hFileMapping[i])
  113.       CloseHandle( hFileMapping[i] ) ;
  114.    if (hFile[i])
  115.    {
  116.       SetFileTime( hFile[i], &ftCreation[i], &ftLastAccess[i], &ftLastWrite[i] ) ;
  117.       CloseHandle( hFile[i] ) ;
  118.    }
  119.    return 1;
  120. }
  121.  
  122. int 
  123. SearchForRegBlock( int i ) 
  124. {
  125.    CHAR * SearchString =   "====758914360207174068160528073\0\0\0\0\0^H^H^H^H^H^H^_{({({({(^^^^^^^^^\0^C^C^C____^_^_^_^____)})})})})}\0$(((((((((((())))))))))))^@^@^@" ;
  126.    int    SearchStringLength = 136 ;
  127.    int SearchLength = strlen(SearchString) ;
  128.    BY_HANDLE_FILE_INFORMATION fileInfo ;
  129.    CHAR * start = NULL ;
  130.    CHAR * p = NULL ;
  131.    int found = 0 ;
  132.    ULONG k = 0 ;
  133.  
  134.    GetFileInformationByHandle( hFile[i], &fileInfo ) ;
  135.  
  136.    //  We only search for the first null terminated portion of the search string 
  137.    //  because we want to be able to find a pre-registered copy
  138.  
  139.    if ( !pView[i] )
  140.       return 0 ;
  141.  
  142.    start = pView[i] ;
  143.    p = pView[i] ;
  144.    found = 0 ;
  145.  
  146.    while ( !found && k < fileInfo.nFileSizeLow )
  147.    {
  148.          LONG j = 0 ;
  149.       if ( p[k] == SearchString[0] )
  150.       {
  151.          start = &p[k] ;
  152.          for ( j = 0 ; j < SearchLength ; j++ )
  153.          {
  154.             if ( start[j] != SearchString[j] )
  155.                break;
  156.          }
  157.          if ( j == SearchLength )
  158.             found = 1 ;
  159.       }
  160.       k++ ;
  161.    }
  162.  
  163.    if ( found )
  164.    {
  165.        RegBlock[i] = (struct ck_registration *) start ;
  166.        printf("Registration Block of %d bytes starts at %d (%X)\n", 
  167.                SearchStringLength, start - pView[i] ,start - pView[i] ) ;
  168.        printf("Pre-byte: 0x%X   Post-byte: 0x%X\n",*(start-1),*(start+SearchStringLength));
  169.    }
  170.     
  171.    return found;
  172. }
  173.  
  174. int
  175. RegisterExe( struct ck_registration * ck_reg, int i )
  176. {
  177.    memcpy( RegBlock[i], ck_reg, sizeof(struct ck_registration) ) ;
  178.    return (1) ;
  179. }
  180.  
  181. void
  182. usage(char * program) {
  183.     printf("Usage: %s [ -n <name> {-b | -a | -d | -o <org>} -c <count> -s <serial> \\\n",program);
  184.     printf("            [-p <prefix>] [-v <version>]] [-h]\n\n");
  185.     printf(" If no options are given the current registration information is displayed.\n");
  186.     printf(" Otherwise, the following options must be given:\n");
  187.     printf("   -n followed by the Registree's name;\n");
  188.     printf("   -o followed by the organization, or\n");
  189.     printf("   -b which is short-hand for Bulk Right to Copy License, or\n");
  190.     printf("   -a which is short-hand for Academic Site License;\n");
  191.     printf("   -d which is short-hand for 30-Day Demo;\n");
  192.     printf("   -c followed by the license count;\n");
  193.     printf("   -s followed by the unencoded serial number.\n\n");
  194.     printf(" If necessary, the following options are also available:\n");
  195.     printf("   -p followed by the product prefix, otherwise K95\n");
  196.     printf("   -v followed by the version, otherwise 1.1\n\n");
  197.     printf("   -h is this usage text\n\n");
  198.     exit(1);
  199. }
  200.  
  201. int bulk=0;
  202. int academic=0;
  203. int demo=0;
  204. char name[32]="";
  205. char org[32]="";
  206. int  license=0, count=0;
  207. char prefix[5]="K95-";
  208. char version[5]="-1.1";
  209. int reg_upd = 0;
  210. unsigned long reg_t=0;
  211.  
  212. void
  213. ParseCmdLine( int argc, char ** argv )
  214. {
  215.     int i=0,j=0,n=0;
  216.     char * p=NULL;
  217.  
  218.     i = 1;                      /* start after program name */
  219.     while ( i < argc ) {
  220.         if ( argv[i][0] == '-' || argv[i][0] == '/' ) {
  221.             switch ( argv[i][1] ) {
  222.             case 'n':           /* Name of licensee */
  223.             case 'N':
  224.                 i++;            /* get next argument (if any) and use a name */
  225.                 if ( i >= argc || name[0]) 
  226.                     usage(argv[0]);
  227.  
  228.                 strncpy(name,argv[i],31);
  229.                 name[31]='\0';
  230.                 break;
  231.             case 'o':           /* Name of Organization */
  232.             case 'O':
  233.                 i++;            /* get next argument (if any) and use a name */
  234.                 if ( i >= argc ||
  235.                      demo || bulk || academic || org[0] )
  236.                     usage(argv[0]);
  237.  
  238.                 strncpy(org,argv[i],31);
  239.                 org[31]='\0';
  240.                 break;
  241.             case 'b':
  242.             case 'B':
  243.                 if ( demo || bulk || academic || org[0] )
  244.                     usage(argv[0]);
  245.                 bulk = 1;
  246.                 strcpy(org,"Bulk Right to Copy License");
  247.                 break;
  248.             case 'a':
  249.             case 'A':
  250.                 if ( demo || bulk || academic || org[0] )
  251.                     usage(argv[0]);
  252.                 academic = 1;
  253.                 strcpy(org,"Academic Site License");
  254.                 break;
  255.             case 'd':
  256.             case 'D':
  257.                 if ( demo || bulk || academic || org[0] )
  258.                     usage(argv[0]);
  259.                 demo = 1;
  260.                 strcpy(org,"For Evaluation Only");
  261.                 break;
  262.  
  263.             case 'c':
  264.             case 'C':
  265.                 i++;
  266.                 if ( i >= argc || count )
  267.                     usage(argv[0]);
  268.                 count = atol(argv[i]);
  269.                 break;
  270.             case 's':
  271.             case 'S':
  272.                 i++;
  273.                 if ( i >= argc || license )
  274.                     usage(argv[0]);
  275.                 license = atol(argv[i]);
  276.                 if ( license == 98 )
  277.                     reg_t = time(NULL);
  278.                 break;
  279.             case 'v':
  280.             case 'V':
  281.                 i++;
  282.                 if ( i >= argc )
  283.                     usage(argv[0]);
  284.                 strncpy(&version[1],argv[i],3);
  285.                 version[4]='\0';
  286.                 break;
  287.             case 'p':
  288.             case 'P':
  289.                 i++;
  290.                 if ( i >= argc )
  291.                     usage(argv[0]);
  292.                 strncpy(prefix,argv[i],3);
  293.                 prefix[3]='-';
  294.                 prefix[4]='\0';
  295.                 break;
  296.             case 'h':
  297.             case 'H':
  298.             default:
  299.                 usage(argv[0]);
  300.             }
  301.         }
  302.         else {
  303.             usage(argv[0]);
  304.         }
  305.         i++;
  306.     }
  307.  
  308.     /* Verify contents */
  309.     if ( name[0] || org[0] || license || count ) {
  310.         reg_upd = 1;
  311.         if ( !name[0] || !org[0] || !license )
  312.             usage(argv[0]);
  313.         if ( !count )
  314.             count = 1;
  315.     }
  316.  
  317.     /* Create the Registration Block */
  318.     if ( reg_upd )
  319.     {
  320.         char * serial = NULL;
  321.  
  322.         ck_encrypt(name);
  323.         strncpy( NewRegBlock.name, name, 31 );
  324.         NewRegBlock.name[31]='\0';
  325.         j = strlen(NewRegBlock.name);
  326.         for ( ;j < 31 ; j++ )
  327.             NewRegBlock.name[j] = '\0';
  328.  
  329.         ck_encrypt( org ) ;
  330.         strncpy( NewRegBlock.corp, org, 31 ) ;
  331.         NewRegBlock.corp[31] = '\0' ;
  332.         j = strlen(NewRegBlock.corp) ;
  333.         for ( ; j < 31 ; j++ )
  334.             NewRegBlock.corp[j] = '\0' ;
  335.         NewRegBlock.corp[31] = '\0' ;
  336.  
  337.         strncpy( NewRegBlock.ident, "====758914360207174068160528073", 32 ) ;
  338.  
  339.         serial = maksn( prefix, license, version ) ;
  340.  
  341.         if ( chksn( serial ) )
  342.         {
  343.             ck_encrypt( serial ) ;
  344.             strncpy( NewRegBlock.serial, serial, 31 ) ;
  345.             j = strlen(NewRegBlock.serial) ;
  346.             for ( ; j < 31 ; j++ )
  347.                 NewRegBlock.serial[j] = '\0' ;
  348.             NewRegBlock.serial[31] = '\0' ;
  349.  
  350.             NewRegBlock.is_set = count ? count : 1 ;
  351.             setcrc( &NewRegBlock );
  352.         }
  353.  
  354.         NewRegBlock.time = reg_t;
  355.     }
  356. }
  357.  
  358.  
  359. int
  360. main( int argc, char ** argv ) 
  361. {
  362.     int dsn = 0 ;
  363.     int i ;
  364.     extern int tcsl;
  365.  
  366.     /* Parse command line and use it to register the files */
  367.     ParseCmdLine(argc,argv);
  368.  
  369.     /* Get current registration information */
  370.  
  371.     tcsl = FC_CP850;
  372.  
  373. #ifdef OS2
  374.     FileName[0] = "k2.exe" ;
  375.     FileName[1] = "k2dial.exe" ;
  376. #else
  377.     FileName[0] = "k95.exe" ;
  378.     FileName[1] = "k95dial.exe" ;
  379. #endif
  380.  
  381.     for ( i=0 ; i < FILECOUNT ; i++ )
  382.     {
  383.         hFile[i]        = NULL ;
  384.         hFileMapping[i] = NULL ;
  385.         pView[i]        = NULL ;
  386.         RegBlock[i] = NULL ;
  387.  
  388.         if ( !ckOpenFile(i) )
  389.         {
  390.             printf("Unable to open %s\n", FileName[i]) ;
  391.             return 1 ;
  392.         }
  393.  
  394.         printf("\n%s\n++++++++++++\n",FileName[i]);
  395.         SearchForRegBlock(i) ;
  396.         if ( !RegBlock[i] )
  397.         {
  398.             // Registration Block not found.  Invalid K95.EXE.
  399.             printf( "Invalid %s file.\n", FileName[i] ) ;
  400.             return 2 ;
  401.         }
  402.  
  403.         if ( (dsn = isregistered( RegBlock[i] )) > 0 )
  404.         {
  405.             char temp[32] ;
  406.             strcpy( temp, RegBlock[i]->name ) ;
  407.             ck_decrypt( temp ) ;
  408.             printf("Name   : %s\n",temp ) ;
  409.             strcpy( temp, RegBlock[i]->corp ) ;
  410.             ck_decrypt( temp ) ;
  411.             printf("Corp   : %s\n",temp ) ;
  412.  
  413.             printf("Count  : %d\n",RegBlock[i]->is_set ) ;
  414.  
  415.             strcpy( temp, RegBlock[i]->serial ) ;
  416.             ck_decrypt( temp ) ;
  417.             printf("Encoded: %s\n",temp);
  418.             temp[3] = '\0' ;
  419.             printf("Product: %s\n",temp ) ;
  420.             printf("Serial : %d\n",dsn ) ;
  421.             printf("Version: %s\n",&temp[16] ) ;
  422.         }
  423.         printf("\n");
  424.  
  425.         if ( reg_upd ) {
  426.             /* Create the new regblock and store it */
  427.             RegisterExe(&NewRegBlock, i);
  428.         }
  429.  
  430.         ckCloseFile(i) ;
  431.     }
  432.  
  433.     if ( reg_upd ) {
  434.         char temp[32];
  435.         printf("\nRegistration Information set to:\n");
  436.         ck_decrypt(name);
  437.         printf("Name   : %s\n",name );
  438.         ck_decrypt(org);
  439.         printf("Corp   : %s\n",org );
  440.  
  441.         printf("Count  : %d\n",count ) ;
  442.  
  443.         strcpy( temp, NewRegBlock.serial ) ;
  444.         ck_decrypt( temp ) ;
  445.         printf("Encoded: %s\n",temp);
  446.  
  447.         prefix[3]='\0';
  448.         printf("Product: %s\n",prefix ) ;
  449.         printf("Serial : %d\n",license ) ;
  450.         printf("Version: %s\n",&version[1] ) ;
  451.         printf("Time:    %u\n",reg_t);
  452.     }
  453.     return(0);
  454. }
  455.  
  456.