Source

blake / libclient.cpp

/*
Copyright (C) 2001-2008 Daybo Logic.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. Neither the name of the project nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
/*
  This does nothing but run some tests on the Blake library to
  make sure we can link with it.  Whether it is built as a DLL or
  a shared object, or a static library.

  It is only a C++ file because this tells some Unices that we must
  use C++ as a linker (Blake still relies on some C++ components at
  time of writing, although they are all deprecated).
  - DDRP
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /*HAVE_CONFIG_H*/

#include <time.h>
#include <stdio.h>

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif /*HAVE_STDLIB_H*/

#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif /*HAVE_LIMITS_H*/

#ifdef HAVE_STRING_H
# include <string.h>
#endif /*HAVE_STRING_H*/

#ifdef BLAKE_HDRSTOP
# pragma hdrstop
#endif /*BLAKE_HDRSTOP*/

#ifdef HAVE_DLSTDINC_H
# include "dlstdinc.h"
#endif /*HAVE_DLSTDINC_H*/

#include "blake_threads.h"
#include "blake.h"
/*-------------------------------------------------------------------------*/
static const char *const rcsid =
  "$Id: libclient.cpp,v 1.9.1 2008/01/26 19:10:00 overlord Exp $";
/*-------------------------------------------------------------------------*/
static bool StartupTasks(void);
static void CleanupTasks(void);
static void InitMutants(void);
static void DestroyLocks(void);
static void PrintError(
  const char *const FunctionName,
  const enum blakeError ErrorNumber
);
static bool TestErrorPrint(void);
static bool TestCracker(void);
static bool TestCracker2(const char *HostName);
static bool TestDB(void);

static struct {
  int data;

  #ifdef BLAKE_THREADS
  dtlock_t lock;
  #endif /*BLAKE_THREADS*/

} protect[3];
/*-------------------------------------------------------------------------*/
int main(void)
{
  int status = EXIT_FAILURE;

  if ( !StartupTasks() ) {
    printf("Oh no!  Something didn't start\n");
    return EXIT_FAILURE;
  }
  blake_Unused(rcsid);
  atexit(CleanupTasks);

  printf("Debugging program for using Blake features.\n");
  if ( TestErrorPrint() && TestCracker() && TestDB() )
    status = EXIT_SUCCESS;

  printf("Finished.\n");
  return status;
}
/*-------------------------------------------------------------------------*/
static bool StartupTasks()
{
  if ( !daybothreads_Startup() )
    return false;
  if ( !blake_Init() )
    return false;
  InitMutants();
  return true;
}
/*-------------------------------------------------------------------------*/
static void CleanupTasks()
{
  DestroyLocks();
  blake_Cleanup();
}
/*-------------------------------------------------------------------------*/
static void InitMutants()
{
  unsigned int i;

  for ( i = 0; i < sizeof(protect)/sizeof(protect[0]); i++ )
    daybothreads_CreateLock(&protect[i].lock);
}
/*-------------------------------------------------------------------------*/
static void DestroyLocks()
{
  unsigned int i;

  for ( i = 0; i < sizeof(protect)/sizeof(protect[0]); i++ )
    daybothreads_DestroyLock(&protect[i].lock);
}
/*-------------------------------------------------------------------------*/
static void PrintError(
  const char *const FunctionName,
  const enum blakeError ErrorNumber
)
{
  char *msg;
  unsigned int len =
    blake_GetErrorMessage(ErrorNumber, NULL, 0, langEnglish);

  msg =
#ifdef __cplusplus
  (char*)
#endif
  malloc(++len);

  if ( msg ) {
    blake_GetErrorMessage(ErrorNumber, msg, len, langEnglish);
    if ( FunctionName )
      fprintf(stdout, "%s(): %s\n", FunctionName, msg);
    else
      fprintf(stdout, "%s\n", msg);
    free(msg);
  }
}
/*-------------------------------------------------------------------------*/
static bool TestErrorPrint()
{
  enum blakeError err;
  unsigned int i, c;

  printf("Printing all error messages on stdout...\n");
  c = blake_GetNumberOfErrorMessages();
  for ( i = 0U; i < c; i++ ) {
    err = (enum blakeError)i;
    PrintError(NULL, err);
  }

  return true;
}
/*-------------------------------------------------------------------------*/
static bool TestCracker()
{
  unsigned int i;
  /* Out of this lot, one must work! */
  static const char* hostNames[] = {
    "127.0.0.1",
    "localhost",
    "cyathus.daybologic.com",
    "static.daybologic.com",
    "www.daybologic.co.uk",
    "login.overchat.org",
    "www.overchat.org",
    "www.ripe.net",
    "www.arin.net",
    "www.slashdot.org",
    "www.internic.net",
    "www.iana.org",
    "www.royal.gov.uk",
    "www.parliament.uk",
    "irc.brad-x.com",
    "relay.gradwell.net",
    "www.digg.com",
    "www.theregister.co.uk",
    "shell.gradwell.net",
    "kestrel.daybologic.com",
    "oberon.daybologic.com",
    "grable.daybologic.com",
    "www.gradwell.com",
    "lilac.gradwell.net",
    "red.gradwell.net",
    "sip.gradwell.net",
    "newsip.gradwell.net",
    "www.r6.gsa.gov",
    "www.police.uk",
    "www.nhs.uk",
    "www.linx.net",
    "en.wikipedia.org",
    "a.root-servers.net",
    "b.root-servers.net",
    "c.root-servers.net",
    "d.root-servers.net",
    "e.root-servers.net",
    "f.root-servers.net",
    "g.root-servers.net",
    "h.root-servers.net",
    "i.root-servers.net",
    "j.root-servers.net",
    "k.root-servers.net",
    "l.root-servers.net",
    "m.root-servers.net"
  };

  printf("\n\nCracker test/resolver test\n"
             "--------------------------\n");
  for ( i = 0U; i < sizeof(hostNames)/sizeof(hostNames[0]); i++ ) {
    if ( !TestCracker2(hostNames[i]) ) {
      return false;
    }
  }
  return true;
}
/*-------------------------------------------------------------------------*/
static bool TestCracker2(
  const char *HostName
)
{
  enum blakeError err;
  OVERCHAT_IP ip;
  DWORD compact;

  printf("Looking up %s : ", HostName);
  err = network_Resolve(HostName, &ip);
  if ( err != blakeSuccess ) {
    PrintError("network_Resolve", err);
    return false;
  }
  if ( network_IsZeroIP(&ip) ) {
    printf("Failed DNS resolution\n");
    return false;
  }
  printf("[");
  network_PrintIP(&ip);
  printf("] - ");
  err = network_DecrackIPv4(&ip, &compact);
  if ( err != blakeSuccess )
    PrintError("network_DecrackIPv4", err);
  printf("%lX - ", compact);
  err = network_CrackIPv4(compact, &ip);
  if ( err != blakeSuccess )
    PrintError("network_CrackIPv4", err);
  network_PrintIP(&ip);
  printf("\n");

  return true;
}
/*-------------------------------------------------------------------------*/
static bool TestDB()
{
  const char *host = "sql.overchat.org";
  const char *name = "daybologic";
  const char *user = "overchat";
  const char *pass = "Qg3t44M6r";
  const char *table = "overchat_users";
  BLAKE_DATABASE_HANDLE dbh;
  enum blakeError err;
  bool status = false;

  err = blake_db_CreateHandle(host, name, user, pass, table, &dbh);
  if ( err == blakeSuccess ) {
    if ( dbh != BLAKE_INVALID_HANDLE ) {
      err == blake_db_LockTable(dbh);
      if ( err == blakeSuccess ) {
        struct blake_db_user userRec;
	unsigned int uid, records = 0U;
	
	for ( uid = 1U; uid <= UINT_MAX; uid++ ) {
	  blake_db_InitUser(&userRec, sizeof(userRec));
	  err = blake_db_GetUserData(dbh, uid, &userRec, sizeof(userRec));
	  if ( err != blakeSuccess ) {
	    PrintError("blake_db_GetUserData", err);
	    break;
	  }
	  
	  if ( !userRec.time_reg ) break;
	  if ( !userRec.deleted ) {
	    records++;
	    printf("User(%u) found: \"%s\"\n", userRec.uid, userRec.username);
	    printf(
	      "Password: \"%s\", email address: \"%s\"\n",
	      blake_PassMask(userRec.password), userRec.email
	    );
	    printf("Clearance level: %u\n", userRec.clearance);
	    printf("Real name: \"%s\"\n", userRec.realname);
	    printf(
	      "Secret question and answer: \"%s\", \"%s\"\n",
	      userRec.secretq, userRec.secreta
	    );
	    printf("Record creation: %s", ctime(&userRec.time_rec));
	    printf("User registration: %s", ctime(&userRec.time_reg));
	    printf("Last seen: %s", ctime(&userRec.time_last));
            printf("User enabled: %s\n", ((userRec.enabled) ? ("yes") : ("no")));
	  }
	}	      
        blake_db_UnlockTable(dbh);
	printf("Number of records: %u\n", records);
      }
      else
        PrintError("blake_db_LockTable", err);
	
      err = blake_db_DestroyHandle(dbh);
      if ( err != blakeSuccess )
        PrintError("blake_db_DestroyHandle", err);
      else
        status = true;
    }
    else {
      puts(
        "blake_db_CreateHandle() was successful "
        "but returned BLAKE_INVALID_HANDLE"
      );
    }
  }
  else {
    PrintError("blake_db_CreateHandle", err);
  }
  return status;
}
/*-------------------------------------------------------------------------*/