⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 no_odm.c

📁 IBM的Linux上的PKCS#11实现
💻 C
📖 第 1 页 / 共 2 页
字号:
  /* Allocate Len bytes */  if ( (s = (char *) malloc(Len)) == NULL ) {    return FALSE;  }  /* Fill with blanks */  memset(s, ' ', Len);  /* Copy the string, but not the trailing NULL */  memcpy(s, Src, ( ( strlen(Src) < Len ) ? strlen(Src) : Len ) );  /* Copy Len bytes into Dest */  memcpy(Dest, s, Len);  /* Free storage */  free(s);  return TRUE;}// Need to eliminate need for ODM here...  BOOL ReadSlotInfoDB ( void ) {  unsigned char          Index     = 0;  char                   StartDir[PATH_MAX + 1];  enum element_type { Present = 1, SlotNumber, SlotDescription, ManufacturerID, TokenPresent, RemovableToken, HardwareSlot, HwVersionMajor, HwVersionMinor, FwVersionMajor, FwVersionMinor, Correlator, DLLLocation, SlotInitFcn };  enum element_type element_num;  char slot_entry[PATH_MAX], *slot_element;  char separator[] = "|\n";  //char separator[] = "|";    BOOL                   Res      = FALSE;#ifdef PKCS64    CK_SLOT_INFO_64       *pSlot    = NULL;#else    CK_SLOT_INFO          *pSlot    = NULL;#endif#ifdef PKCS64    Slot_Info_t_64	sinfo_struct;#else    Slot_Info_t		sinfo_struct;#endif  FILE *fp;  sprintf(StartDir,"%s/%s", ODM_DIRECTORY, "pk_config_data");    fp=fopen(StartDir, "r");  if (fp == NULL) {    fprintf(stderr, "\nCannot open file %s\n", StartDir);    fprintf(stderr, "Please run /usr/lib/pkcs11/methods/pkcs11_startup\n");    return FALSE;  }  /* first checking if the end of file hasn't been reached */  // For some reason (strange)  on linux the FP gets nulled when the end of the file  // is reached....SO, we just check for a valid FP first..  while( fp && !feof(fp) ) {    if( Index >= NUMBER_SLOTS_MANAGED )  // making sure all slots aren't filled in the sinfo array      break;#ifdef ALLOCATE    slot_entry = (char *)malloc(sizeof(char)*(PATH_MAX));#else    bzero(slot_entry,PATH_MAX);#endif    fgets(slot_entry, PATH_MAX, fp);    while( ( (slot_entry[0] == '#') ||	     (slot_entry[0] == '\n') || 	     (slot_entry[0] == (char)NULL) ) 	   && (!feof(fp)) ) {      // skipping all blank lines, and lines beginning with a #, at all times      // AM I FORGETTING ANY OTHER CONDITIONS FOR BLANK LINES ??      // SAB XXX Any one of these causes an abort      fgets(slot_entry, PATH_MAX, fp);    }    // Check for null string...    if ( slot_entry[0] == '\0')	    break;  //  if( feof(fp) )  //    break;    bzero(&sinfo_struct,sizeof(sinfo_struct));  // for good measure zero it out before use each time    sinfo_struct.global_sessions = 0;	// initializing to zero    element_num = Present;    /* Present */    slot_element = strtok(slot_entry, separator);    if ( ! BooleanVal( slot_element, strlen(slot_element), &Res ) )	// BooleanVal(), correct value for 2nd parameter ??      continue;    if (Res)      sinfo_struct.present = (CK_BOOL) Res;    else      break;	// skipping this entry if it's not "Present" ??    element_num++;    while( (slot_element = strtok(NULL, separator)) != NULL ) {	// continually parsing line, extracting the tokens separated by |								// in variable string, slot_entry, and putting them in their								// appropriate fields in the Slot_Info_t(_64) struct, sinfo_struct    pSlot = &(sinfo_struct.pk_slot);    switch(element_num) {    /* Switch Statement to deal with each element of the slot, placing the elements in appropriate member of sinfo_struct */    /* SlotNumber */    case SlotNumber:#if 0#ifdef PKCS64      sinfo_struct.slot_number = (CK_SLOT_ID_64) atoi(slot_element);#else      sinfo_struct.slot_number = (CK_SLOT_ID) atoi(slot_element);#endif#else      sinfo_struct.slot_number = Index; // slot_element in config file is deprecated#endif      element_num++;      break;    /* SlotDescription */    case SlotDescription:      strncpy( (pSlot->slotDescription), slot_element, 	( (strlen(slot_element) > sizeof(pSlot->slotDescription)) 	  ? sizeof(pSlot->slotDescription) 	  : (strlen(slot_element) ) ) );      /* Set the last character in string to NULL since strncpy may mor may not copy the trailing NULL */      pSlot->slotDescription[sizeof(pSlot->slotDescription) - 1] = (CK_CHAR)NULL;      element_num++;      break;    /* ManufacturerID */    case ManufacturerID:      strncpy( (pSlot->manufacturerID), slot_element, 	( (strlen(slot_element) > sizeof(pSlot->manufacturerID)) 	  ? (sizeof(pSlot->manufacturerID)) 	  : (strlen(slot_element) ) ) );      /* Set the last character in string to NULL since strncpy may mor may not copy the trailing NULL */      pSlot->manufacturerID[sizeof(pSlot->manufacturerID) - 1] = (CK_CHAR)NULL;      element_num++;      break;    /* TokenPresent */    case TokenPresent:      pSlot->flags = 0x0; //NULL;	// initially setting to NULL      Res = FALSE;		// resetting Res to FALSE before determining token type using BooleanVal() function      if( BooleanVal(slot_element, strlen(slot_element), &Res) )        if(Res == TRUE)          pSlot->flags |= (CKF_TOKEN_PRESENT);#if 0         // SAB XXX  Since tokens can be removable, it is concievable that an         // entry exists but the token is not present.... So the else clause is         // not needed  Also all over the code you are changing the present         // flag... probably not what you intended      else        sinfo_struct.present = (CK_BOOL) FALSE;#endif        /* fprintf(stderr, "\nUnrecognized data\n");  // not exactly sure about this. */      element_num++;      break;    /* RemovableToken */    case RemovableToken:      if( BooleanVal(slot_element, strlen(slot_element), &Res) )        if(Res == TRUE)          pSlot->flags |= (CKF_REMOVABLE_DEVICE);#if 0         // SAB XXX  Since tokens can be removable, it is concievable that an         // entry exists but the token is not present.... So the else clause is         // not needed      else        sinfo_struct.present = (CK_BOOL) FALSE;        /* fprintf(stderr, "\nUnrecognized data\n");  // not exactly sure about this. */#endif      element_num++;      break;    /* HardwareSlot */    case HardwareSlot:      if( BooleanVal(slot_element, strlen(slot_element), &Res) )        if(Res == TRUE)          pSlot->flags |= (CKF_HW_SLOT);#if 0      else         // SAB XXX  Since tokens can be removable, it is concievable that an         // entry exists but the token is not present.... So the else clause is         // not needed        sinfo_struct.present = (CK_BOOL) FALSE;#endif        /* fprintf(stderr, "\nUnrecognized data\n");  // not exactly sure about this. */      if(pSlot->flags == 0x0  /*NULL*/ )		// making sure after all possible types have flags have been checked for        fprintf(stderr, "\nUnrecognized token flag\n");	// the token has been labeled as one of them      element_num++;      break;    /* HardwareVersion major and minor */    case HwVersionMajor:      pSlot->hardwareVersion.major = (CK_BYTE) atoi(slot_element);      element_num++;      break;    case HwVersionMinor:      pSlot->hardwareVersion.minor = (CK_BYTE) atoi(slot_element);      element_num++;      break;    /*FirmwareVersion major and minor */    case FwVersionMajor:      pSlot->firmwareVersion.major = (CK_BYTE) atoi(slot_element);      element_num++;      break;    case FwVersionMinor:      pSlot->firmwareVersion.minor = (CK_BYTE) atoi(slot_element);      element_num++;      break;    /* Correlator */    case Correlator:      // SAB XXX FIXME... be able to handle NULL correlator      // for tokens which don't need this...  XXX      if(slot_element[0] ==  0x0 /*NULL */ ) {        sinfo_struct.correlator[0] = '\0';      }      else {        strncpy( (sinfo_struct.correlator), slot_element, 	  ( (strlen(slot_element) > sizeof(sinfo_struct.correlator)) 	    ? (sizeof(sinfo_struct.correlator)) 	    : (strlen(slot_element)) ) );            /* Set the last character in string to NULL since strncpy may mor may not copy the trailing NULL */      sinfo_struct.correlator[sizeof(sinfo_struct.correlator) - 1] = (char) NULL;      }      element_num++;      break;    /* DLLLocation */    case DLLLocation:      if(slot_element[0] == 0x0 /*NULL */) {	//finish later...        sinfo_struct.dll_location[0] = (char)NULL;      }      else {        strncpy( (sinfo_struct.dll_location), slot_element,           ( (strlen(slot_element) > sizeof(sinfo_struct.dll_location))             ? (sizeof(sinfo_struct.dll_location)) 		  : (strlen(slot_element)) ) );	// took away an ")"      /* Set the last character in string to NULL since strncpy may mor may not copy the trailing NULL */      sinfo_struct.dll_location[sizeof(sinfo_struct.dll_location) - 1] = (char) NULL;      {        /* check for file existance */#if (AIX)        struct stat64 statbuf;#elif (LINUX)        struct stat statbuf;#endif        int Err;#if (AIX)        if ( stat64( sinfo_struct.dll_location, &statbuf ) < 0 ) {#elif (LINUX)        if ( stat( sinfo_struct.dll_location, &statbuf ) < 0 ) {#endif	  /* File not found, or other error */	  #pragma info(none)	    Err = errno;	  #pragma info(restore)	  if ( Err == ENOENT ) {            fprintf(stderr, "\nReading Slot Info: %s: file not found (%s).  Skipping slot entry.", sinfo_struct.dll_location, SysError(Err) );	    /* WarnLog ( "***** ReadSlotInfoDB: %s: file not found (%s).  Skipping DB entry.", sinfo[Index].dll_location, SysError(Err) ); */	  }          else {            fprintf(stderr, "\nReading Slot Info: %s: looking at %s, stat64() returned %s (%d; %#x)", sinfo_struct.dll_location, SysError(Err), Err, Err);	    /* DbgLog (DL0, "***** ReadSlotInfoDB: looking at %s, stat64() returned %s (%d; %#x)", sinfo[Index].dll_location, SysError(Err), Err, Err); */	  }	  memset( pSlot, '\0', sizeof(*pSlot) );	  sinfo_struct.present = (CK_BOOL) FALSE;	  continue;        }      } /* end DLLLocation unconditional block */      }      element_num++;      break;    /* SlotInitFcn */    case SlotInitFcn:      if(slot_element[0] == 0x0 /*NULL */) {     // finish later...	sinfo_struct.slot_init_fcn[0] = (char)NULL;    }    else {      // SAB  if there is not an initfcn then this entry is not used      strncpy( (sinfo_struct.slot_init_fcn), 	        slot_element, 	     ( (strlen(slot_element) > sizeof(sinfo_struct.slot_init_fcn))           ? (sizeof(sinfo_struct.slot_init_fcn)) 	  : strlen(slot_element)) );      /* Set the last character in string to NULL since strncpy may mor may not copy the trailing NULL */      sinfo_struct.slot_init_fcn[sizeof(sinfo_struct.slot_init_fcn) - 1] =(char) NULL;      {        /* stripping off trailing new line character, '\n' from init function name if necessary */        int i;        for(i = 0; i < sizeof(sinfo_struct.slot_init_fcn); i++){          if(sinfo_struct.slot_init_fcn[i] == '\n')            sinfo_struct.slot_init_fcn[i] = (char)NULL;        }      }    }      element_num++;      break;    default:      //  SAB...   since this is a daemon we reallly don't want to      //  be doing printf's ....         fprintf(stderr, "\nIncorrect field present.\n");    }    }    /*      *copying the contents of sinfo_struct into the array of similar structures if it's a valid entry      *(i.e. init fcn & dll filename are non-NULL...and the latter exists and is valid?)     */    if( (sinfo_struct.dll_location != NULL) &&	(sinfo_struct.slot_init_fcn != NULL) ) {      bcopy(&sinfo_struct,&sinfo[Index],sizeof(sinfo_struct));  // similar to sinfo[Index] = &sinfo_struct;      PrintSlotInfo( &(sinfo[Index]) );      Index++;    }#ifdef ALLOCATE    if (slot_entry) {      free(slot_entry);      slot_entry = NULL;    }#endif  }  if( Index >= NUMBER_SLOTS_MANAGED )    fprintf(stderr, "\nThe maximum number of slots supported has been reached\n");  if (fp)	  fclose( fp );  NumberSlotsInDB = Index;	// This is required since it sets the count in  				// the shared memory segment  return TRUE;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -