Exemplo n.º 1
0
/*!
 * \brief   ハッシュ計算実行
 */
void SHA256::execute(const char* message, int32_t size)
{
#if defined(_WINDOWS)
    HCRYPTPROV hProv = NULL;
    HCRYPTHASH hHash = NULL;

    do
    {
        if (CryptAcquireContextW(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT) == FALSE)
            break;

        if (CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash) == FALSE)
            break;

        if (CryptHashData(hHash, (uint8_t*)message, size, 0) == FALSE)
            break;

        int32_t hashSize = getHashSize();
        CryptGetHashParam(hHash, HP_HASHVAL, mData->mMessageDigest, (DWORD*)&hashSize, 0);
    }
    while (false);

    if (hHash)
        CryptDestroyHash(hHash);

    if (hProv)
        CryptReleaseContext(hProv, 0);
#else
    SHA256_CTX context;

    SHA256_Init(  &context);
    SHA256_Update(&context, message, size);
    SHA256_Final(mData->mMessageDigest, &context);
#endif
}
Exemplo n.º 2
0
int main(int argc, char *argv[]) {
  PRIVATE int pid = 0;
  PRIVATE int c = 0, i = 0, fds = 0, status = 0;
  int digit_optind = 0;
  PRIVATE struct passwd *pwd_ent;
  PRIVATE struct group *grp_ent;
  PRIVATE char **ptr;
  char *tmp_ptr = NULL;
  char *pid_file = NULL;
  char *user = NULL;
  char *group = NULL;
#ifdef LINUX
  struct rlimit rlim;

  getrlimit( RLIMIT_CORE, &rlim );
#ifdef DEBUG
  rlim.rlim_cur = rlim.rlim_max;
  printf( "DEBUG - RLIMIT_CORE: %ld\n", rlim.rlim_cur );
#else
  rlim.rlim_cur = 0; 
#endif
  setrlimit( RLIMIT_CORE, &rlim );
#endif

  /* setup config */
  config = ( Config_t * )XMALLOC( sizeof( Config_t ) );
  XMEMSET( config, 0, sizeof( Config_t ) );

  /* get real uid and gid, we may want to drop privs */
  config->gid = getgid();
  config->uid = getuid();

  while (1) {
    int this_option_optind = optind ? optind : 1;
#ifdef HAVE_GETOPT_LONG
    int option_index = 0;
    static struct option long_options[] = {
      {"atime", no_argument, 0, 'a' },
      {"debug", required_argument, 0, 'd' },
      {"exdir", required_argument, 0, 'e' },
      {"exfile", required_argument, 0, 'E' },
      {"help", no_argument, 0, 'h' },
      {"md5", no_argument, 0, 'm' },
      {"preserve", no_argument, 0, 'p' },
      {"quick", no_argument, 0, 'q' },
      {"sha256", no_argument, 0, 's' },
      {"version", no_argument, 0, 'v' },
      {"write", required_argument, 0, 'w' },
      {0, no_argument, 0, 0}
    };
    c = getopt_long(argc, argv, "ad:e:E:hmpqsvw:", long_options, &option_index);
#else
    c = getopt( argc, argv, "ad:e:E:hmpqsvw:" );
#endif

    if (c EQ -1)
      break;

    switch (c) {
      case 'a':
      /* enable atime change reporting */
      config->show_atime = TRUE;
      
    case 'p':
      if ( config->uid != 0 ) {
        fprintf( stderr, "ERR - Insufficient priviledges to preserve ATIME, aborting\n" );
        print_help();
        return( EXIT_FAILURE );
      }
      config->preserve_atime = TRUE;
      
      break;
        
      
    case 'd':
      /* show debig info */
      config->debug = atoi( optarg );
      config->mode = MODE_INTERACTIVE;
      break;

    case 'e':
      /* exclude a specific directory from the diff */
      if ( ( config->exclusions = (char **)XMALLOC( sizeof( char * ) * 2 ) ) EQ NULL ) {
        fprintf( stderr, "ERR - Unable to allocate memory for exclusion list\n" );
        return( EXIT_FAILURE );
      }
      if ( ( config->exclusions[0] = XMALLOC( MAXPATHLEN + 1 ) ) EQ NULL ) {
        fprintf( stderr, "ERR - Unable to allocate memory for exclusion list\n" );
        XFREE( config->exclusions );
        return( EXIT_FAILURE );
      }
      if ( optarg[0] != '/' ) {
        config->exclusions[0][0] = '/';
        XSTRNCPY( config->exclusions[0]+1, optarg, MAXPATHLEN - 1 );
      } else
        XSTRNCPY( config->exclusions[0], optarg, MAXPATHLEN );
      config->exclusions[1] = 0;
      break;
        
    case 'E':
      /* exclude a list of directories in the specific file */
      //if ( loadExclusions( optarg ) != TRUE )
      //  return( EXIT_FAILURE );
      //break;
      fprintf( stderr, "ERR - Feature not currently supported\n" );
      print_help();
      return( EXIT_SUCCESS );
      
    case 'h':
      /* show help info */
      print_help();
      return( EXIT_SUCCESS );

    case 'm':
      /* md5 hash files */
      config->hash = TRUE;
      config->md5_hash = TRUE;
      config->digest_size = 16;
      config->sha256_hash = FALSE;
      break;

    case 'q':
      /* do quick checks only */
      config->quick = TRUE;
      break;
      
    case 's':
      /* sha256 hash files */
      config->hash = TRUE;
      config->sha256_hash = TRUE;
      config->digest_size = 32;
      config->md5_hash = FALSE;
      break;

    case 'v':
      /* show the version */
      print_version();
      return( EXIT_SUCCESS );


    case 'w':
      /* define the dir to store logs in */
      if ( ( config->outfile = ( char * )XMALLOC( MAXPATHLEN + 1 ) ) EQ NULL ) {
        /* XXX problem */
      }
      XMEMSET( config->outfile, 0, MAXPATHLEN + 1 );
      XSTRNCPY( config->outfile, optarg, MAXPATHLEN );
      break;
      
    default:
      fprintf( stderr, "Unknown option code [0%o]\n", c);
    }
  }

  /* turn off quick mode if hash mode is enabled */
  if ( config->hash )
    config->quick = FALSE;

  /* check dirs and files for danger */

  if ( time( &config->current_time ) EQ -1 ) {
    fprintf( stderr, "ERR - Unable to get current time\n" );
    
    /* cleanup buffers */
    cleanup();
    return EXIT_FAILURE;
  }

  /* initialize program wide config options */
  config->hostname = (char *)XMALLOC( MAXHOSTNAMELEN+1 );

  /* get processor hostname */
  if ( gethostname( config->hostname, MAXHOSTNAMELEN ) != 0 ) {
    fprintf( stderr, "Unable to get hostname\n" );
    strncpy( config->hostname, "unknown", MAXHOSTNAMELEN );
  }

  /* setup gracefull shutdown */
  signal( SIGINT, sigint_handler );
  signal( SIGTERM, sigterm_handler );
  signal( SIGFPE, sigfpe_handler );
  signal( SIGILL, sigill_handler );
  signal( SIGSEGV, sigsegv_handler );
#ifndef MINGW
  signal( SIGHUP, sighup_handler );
  signal( SIGBUS, sigbus_handler );
#endif  

  /****
   *
   * lets get this party started
   *
   ****/

  show_info();
  if ( ( baseDir = (char *)XMALLOC( PATH_MAX ) ) EQ NULL ) {
    fprintf( stderr, "ERR - Unable to allocate memory for baseDir string\n" );
    cleanup();
    return( EXIT_FAILURE );
  }
  if ( ( compDir = (char *)XMALLOC( PATH_MAX ) ) EQ NULL ) {
    fprintf( stderr, "ERR - Unable to allocate memory for compDir string\n" );
    cleanup();
    return( EXIT_FAILURE );
  }

  compDirHash = initHash( 52 );

  while (optind < argc ) {
    if ( ( compDirLen = strlen( argv[optind] ) ) >= PATH_MAX ) {
      fprintf( stderr, "ERR - Argument too long\n" );
      if ( baseDirHash != NULL )
	freeHash( baseDirHash );
      freeHash( compDirHash );
      cleanup();
      return( EXIT_FAILURE );
    } else {
      strncpy( compDir, argv[optind++], PATH_MAX-1 );
      /* process directory tree */
      if ( processDir( compDir ) EQ FAILED ) {
	if ( baseDirHash != NULL  )
	  freeHash( baseDirHash );
        freeHash( compDirHash );
	cleanup();
	return( EXIT_FAILURE );
      }

      if ( baseDirHash != NULL ) {
	/* compare the old tree to the new tree to find missing files */
	if ( traverseHash( baseDirHash, findMissingFiles ) != TRUE ) {
	  freeHash( baseDirHash );
	  freeHash( compDirHash );
	  cleanup();
	  return( EXIT_FAILURE );
	}
      }

      /* Prep for next dir to compare */
      if ( baseDirHash != NULL )
	freeHash( baseDirHash );
      baseDirHash = compDirHash;
      compDirHash = initHash( getHashSize( baseDirHash ) );
      baseDirLen = compDirLen;
      strncpy( baseDir, compDir, compDirLen );
    }
  }

  if ( baseDirHash != NULL )
    freeHash( baseDirHash );
  if ( compDirHash != NULL )
    freeHash( compDirHash );

  /****
   *
   * we are done
   *
   ****/

  cleanup();

  return( EXIT_SUCCESS );
}