Back to index

lightning-sunbird  0.9+nobinonly
Defines | Enumerations | Functions | Variables
pwdecrypt.c File Reference
#include "nspr.h"
#include "string.h"
#include "nss.h"
#include "secutil.h"
#include "cert.h"
#include "pk11func.h"
#include "nssb64.h"
#include "plgetopt.h"
#include "pk11sdr.h"

Go to the source code of this file.

Defines

#define DEFAULT_VALUE   "Test"
#define MAX_STRING   256

Enumerations

enum  { false = 0, true = 1 }

Functions

static void synopsis (char *program_name)
static void short_usage (char *program_name)
static void long_usage (char *program_name)
int isatobchar (int c)
int getData (FILE *inFile, char **inString)
int main (int argc, char **argv)
 The Xalan testcases app.

Variables

static unsigned char b64 [256]
enum { ... }  bool

Define Documentation

#define DEFAULT_VALUE   "Test"

Definition at line 54 of file pwdecrypt.c.

#define MAX_STRING   256

Definition at line 142 of file pwdecrypt.c.


Enumeration Type Documentation

anonymous enum
Enumerator:
false 
true 

Definition at line 133 of file pwdecrypt.c.

     {
   false = 0,
   true = 1
} bool;

Function Documentation

int getData ( FILE inFile,
char **  inString 
)

Definition at line 144 of file pwdecrypt.c.

                                      {
    int len = 0;
    int space = MAX_STRING;
    int oneequal = false;
    int c;
    char *string = (char *) malloc(space);

    string[len++]='M';

    while ((c = getc(inFile)) != EOF) {
       if (len >= space) {
           char *newString;

           space *= 2;
           newString = (char *)realloc(string,space);
           if (newString == NULL) {
              ungetc(c,inFile);
              break;
           }
           string = newString;
       }
       string[len++] = c;
       if (!isatobchar(c)) {
          if (c == '=') {
              if (oneequal) {
                  break;
              }
              oneequal = true;
              continue;
          } else {
              ungetc(c,inFile);
              len--;
              break;
          }
       }
       if (oneequal) {
          ungetc(c,inFile);
          len--;
          break;
       }
    }
    if (len >= space) {
       space += 2;
       string = (char *)realloc(string,space);
    }
    string[len++] = 0;
    *inString = string;
    return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int isatobchar ( int  c)

Definition at line 139 of file pwdecrypt.c.

{ return b64[c] != 0; }

Here is the caller graph for this function:

static void long_usage ( char *  program_name) [static]

Definition at line 80 of file pwdecrypt.c.

{
    PRFileDesc *pr_stderr;

    pr_stderr = PR_STDERR;
    synopsis (program_name);
    PR_fprintf (pr_stderr, "\nDecode encrypted passwords (and other data).\n");
    PR_fprintf (pr_stderr, 
       "This program reads in standard configuration files looking\n"
       "for base 64 encoded data. Data that looks like it's base 64 encode\n"
       "is decoded an passed to the NSS SDR code. If the decode and decrypt\n"
       "is successful, then decrypted data is outputted in place of the\n"
       "original base 64 data. If the decode or decrypt fails, the original\n"
       "data is written and the reason for failure is logged to the \n"
       "optional logfile.\n");
    PR_fprintf (pr_stderr,
              "  %-13s Read stream including encrypted data from "
               "\"read_file\"\n",
              "-i read_file");
    PR_fprintf (pr_stderr,
              "  %-13s Write results to \"write_file\"\n",
              "-o write_file");
    PR_fprintf (pr_stderr,
              "  %-13s Find security databases in \"dbdir\"\n",
              "-d dbdir");
    PR_fprintf (pr_stderr,
              "  %-13s Log failed decrypt/decode attempts to \"log_file\"\n",
              "-l log_file");
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)

The Xalan testcases app.

Definition at line 195 of file pwdecrypt.c.

{
    int               retval = 0;  /* 0 - test succeeded.  -1 - test failed */
    SECStatus  rv;
    PLOptState       *optstate;
    char      *program_name;
    char  *input_file = NULL;      /* read encrypted data from here (or create) */
    char  *output_file = NULL;     /* write new encrypted data here */
    char  *log_file = NULL; /* write new encrypted data here */
    FILE      *inFile = stdin;
    FILE      *outFile = stdout;
    FILE      *logFile = NULL;
    PLOptStatus optstatus;
    SECItem   result;
    int              c;

    result.data = 0;

    program_name = PL_strrchr(argv[0], '/');
    program_name = program_name ? (program_name + 1) : argv[0];

    optstate = PL_CreateOptState (argc, argv, "Hd:i:o:l:?");
    if (optstate == NULL) {
       SECU_PrintError (program_name, "PL_CreateOptState failed");
       return 1;
    }

    while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
       switch (optstate->option) {
         case '?':
           short_usage (program_name);
           return 1;

         case 'H':
           long_usage (program_name);
           return 1;

         case 'd':
           SECU_ConfigDirectory(optstate->value);
           break;

          case 'i':
            input_file = PL_strdup(optstate->value);
            break;

          case 'o':
            output_file = PL_strdup(optstate->value);
            break;

          case 'l':
            log_file = PL_strdup(optstate->value);
            break;

       }
    }
    PL_DestroyOptState(optstate);
    if (optstatus == PL_OPT_BAD) {
       short_usage (program_name);
       return 1;
    }

    if (input_file) {
      inFile = fopen(input_file,"r");
      if (inFile == NULL) {
       perror(input_file);
       return 1;
      }
      PR_Free(input_file);
    }
    if (output_file) {
      outFile = fopen(output_file,"w+");
      if (outFile == NULL) {
       perror(output_file);
       return 1;
      }
      PR_Free(output_file);
    }
    if (log_file) {
      logFile = fopen(log_file,"w+");
      if (logFile == NULL) {
       perror(log_file);
       return 1;
      }
      PR_Free(log_file);
    }

    /*
     * Initialize the Security libraries.
     */
    PK11_SetPasswordFunc(SECU_GetModulePassword);
    rv = NSS_Init(SECU_ConfigDirectory(NULL));
    if (rv != SECSuccess) {
       SECU_PrintError (program_name, "NSS_Init failed");
       retval = 1;
       goto prdone;
    }

    /* Get the encrypted result, either from the input file
     * or from encrypting the plaintext value
     */

    while ((c = getc(inFile)) != EOF) {
       if (c == 'M') {
          char *dataString = NULL;
          SECItem *inText;

          rv = getData(inFile, &dataString);
          if (!rv) {
              fputs(dataString,outFile);
              free(dataString);
              continue;
          }
          inText = NSSBase64_DecodeBuffer(NULL, NULL, dataString,
                                                 strlen(dataString));
          if ((inText == NULL) || (inText->len == 0)) {
              if (logFile) {
                  fprintf(logFile,"Base 64 decode failed on <%s>\n",
                                                        dataString);
                  fprintf(logFile," Error %x: %s\n",PORT_GetError(),
                     SECU_Strerror(PORT_GetError()));
              }
              fputs(dataString,outFile);
              free(dataString);
              continue;
          }
          result.data = NULL;
          result.len  = 0;
          rv = PK11SDR_Decrypt(inText, &result, NULL);
          SECITEM_FreeItem(inText, PR_TRUE);
          if (rv != SECSuccess) {
              if (logFile) {
                  fprintf(logFile,"SDR decrypt failed on <%s>\n",
                                                        dataString);
                  fprintf(logFile," Error %x: %s\n",PORT_GetError(),
                     SECU_Strerror(PORT_GetError()));
              }
              fputs(dataString,outFile);
              free(dataString);
              SECITEM_ZfreeItem(&result, PR_FALSE);
              continue;
          }
          /* result buffer has no extra space for a NULL */
          fprintf(outFile, "%.*s", result.len, result.data);
          SECITEM_ZfreeItem(&result, PR_FALSE);
         } else {
          putc(c,outFile);
         }
    }

    fclose(outFile);
    fclose(inFile);
    if (logFile) {
       fclose(logFile);
    }

    if (NSS_Shutdown() != SECSuccess) {
       SECU_PrintError (program_name, "NSS_Shutdown failed");
       exit(1);
    }

prdone:
    PR_Cleanup ();
    return retval;
}

Here is the call graph for this function:

static void short_usage ( char *  program_name) [static]

Definition at line 70 of file pwdecrypt.c.

{
    PR_fprintf (PR_STDERR,
              "Type %s -H for more detailed descriptions\n",
              program_name);
    synopsis (program_name);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void synopsis ( char *  program_name) [static]

Definition at line 57 of file pwdecrypt.c.

{
    PRFileDesc *pr_stderr;

    pr_stderr = PR_STDERR;
    PR_fprintf (pr_stderr, "Usage:");
    PR_fprintf (pr_stderr,
       "\t%s [-i <input-file>] [-o <output-file>] [-d <dir>] [-l logfile]\n",
              program_name);
}

Here is the caller graph for this function:


Variable Documentation

unsigned char b64[256] [static]
Initial value:
 {
        0,      0,      0,      0,      0,      0,      0,      0,
        0,      0,      0,      0,      0,      0,      0,      0,
        0,      0,      0,      0,      0,      0,      0,      0,
        0,      0,      0,      0,      0,      0,      0,      0,
        0,      0,      0,      0,      0,      0,      0,      0,
        0,      0,      0,      1,      0,      0,      0,      1,
        1,      1,      1,      1,      1,      1,      1,      1,
        1,      1,      0,      0,      0,      0,      0,      0,
        0,      1,      1,      1,      1,      1,      1,      1,
        1,      1,      1,      1,      1,      1,      1,      1,
        1,      1,      1,      1,      1,      1,      1,      1,
        1,      1,      1,      0,      0,      0,      0,      0,
        0,      1,      1,      1,      1,      1,      1,      1,
        1,      1,      1,      1,      1,      1,      1,      1,
        1,      1,      1,      1,      1,      1,      1,      1,
        1,      1,      1,      0,      0,      0,      0,      0,
        0,      0,      0,      0,      0,      0,      0,      0
}

Definition at line 113 of file pwdecrypt.c.

enum { ... } bool