Back to index

lightning-sunbird  0.9+nobinonly
slot.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #ifdef DEBUG
00038 static const char CVS_ID[] = "@(#) $RCSfile: slot.c,v $ $Revision: 1.2.28.1 $ $Date: 2006/03/02 22:45:14 $";
00039 #endif /* DEBUG */
00040 
00041 #include "ckdbm.h"
00042 
00043 static CK_RV
00044 nss_dbm_mdSlot_Initialize
00045 (
00046   NSSCKMDSlot *mdSlot,
00047   NSSCKFWSlot *fwSlot,
00048   NSSCKMDInstance *mdInstance,                                    
00049   NSSCKFWInstance *fwInstance
00050 )
00051 {
00052   nss_dbm_slot_t *slot = (nss_dbm_slot_t *)mdSlot->etc;
00053   nss_dbm_instance_t *instance = (nss_dbm_instance_t *)mdInstance->etc;
00054   CK_RV rv = CKR_OK;
00055 
00056   slot->token_db = nss_dbm_db_open(instance->arena, fwInstance, slot->filename, 
00057                                    slot->flags, &rv);
00058   if( (nss_dbm_db_t *)NULL == slot->token_db ) {
00059     if( CKR_TOKEN_NOT_PRESENT == rv ) {
00060       /* This is not an error-- just means "the token isn't there" */
00061       rv = CKR_OK;
00062     }
00063   }
00064 
00065   return rv;
00066 }
00067 
00068 static void
00069 nss_dbm_mdSlot_Destroy
00070 (
00071   NSSCKMDSlot *mdSlot,
00072   NSSCKFWSlot *fwSlot,
00073   NSSCKMDInstance *mdInstance,                                    
00074   NSSCKFWInstance *fwInstance
00075 )
00076 {
00077   nss_dbm_slot_t *slot = (nss_dbm_slot_t *)mdSlot->etc;
00078 
00079   if( (nss_dbm_db_t *)NULL != slot->token_db ) {
00080     nss_dbm_db_close(slot->token_db);
00081     slot->token_db = (nss_dbm_db_t *)NULL;
00082   }
00083 }
00084 
00085 static NSSUTF8 *
00086 nss_dbm_mdSlot_GetSlotDescription
00087 (
00088   NSSCKMDSlot *mdSlot,
00089   NSSCKFWSlot *fwSlot,
00090   NSSCKMDInstance *mdInstance,                                    
00091   NSSCKFWInstance *fwInstance,
00092   CK_RV *pError
00093 )
00094 {
00095   return "Database";
00096 }
00097 
00098 static NSSUTF8 *
00099 nss_dbm_mdSlot_GetManufacturerID
00100 (
00101   NSSCKMDSlot *mdSlot,
00102   NSSCKFWSlot *fwSlot,
00103   NSSCKMDInstance *mdInstance,                                    
00104   NSSCKFWInstance *fwInstance,
00105   CK_RV *pError
00106 )
00107 {
00108   return "Berkeley";
00109 }
00110 
00111 static CK_BBOOL
00112 nss_dbm_mdSlot_GetTokenPresent
00113 (
00114   NSSCKMDSlot *mdSlot,
00115   NSSCKFWSlot *fwSlot,
00116   NSSCKMDInstance *mdInstance,                                    
00117   NSSCKFWInstance *fwInstance
00118 )
00119 {
00120   nss_dbm_slot_t *slot = (nss_dbm_slot_t *)mdSlot->etc;
00121 
00122   if( (nss_dbm_db_t *)NULL == slot->token_db ) {
00123     return CK_FALSE;
00124   } else {
00125     return CK_TRUE;
00126   }
00127 }
00128 
00129 static CK_BBOOL
00130 nss_dbm_mdSlot_GetRemovableDevice
00131 (
00132   NSSCKMDSlot *mdSlot,
00133   NSSCKFWSlot *fwSlot,
00134   NSSCKMDInstance *mdInstance,                                    
00135   NSSCKFWInstance *fwInstance
00136 )
00137 {
00138   /*
00139    * Well, this supports "tokens" (databases) that aren't there, so in
00140    * that sense they're removable.  It'd be nice to handle databases
00141    * that suddenly disappear (NFS-mounted home directories and network
00142    * errors, for instance) but that's a harder problem.  We'll say
00143    * we support removable devices, badly.
00144    */
00145 
00146   return CK_TRUE;
00147 }
00148 
00149 /* nss_dbm_mdSlot_GetHardwareSlot defaults to CK_FALSE */
00150 /* 
00151  * nss_dbm_mdSlot_GetHardwareVersion
00152  * nss_dbm_mdSlot_GetFirmwareVersion
00153  *
00154  * These are kinda fuzzy concepts here.  I suppose we could return the
00155  * Berkeley DB version for one of them, if we had an actual number we
00156  * were confident in.  But mcom's "dbm" has been hacked enough that I
00157  * don't really know from what "real" version it stems..
00158  */
00159 
00160 static NSSCKMDToken *
00161 nss_dbm_mdSlot_GetToken
00162 (
00163   NSSCKMDSlot *mdSlot,
00164   NSSCKFWSlot *fwSlot,
00165   NSSCKMDInstance *mdInstance,                                    
00166   NSSCKFWInstance *fwInstance,
00167   CK_RV *pError
00168 )
00169 {
00170   nss_dbm_slot_t *slot = (nss_dbm_slot_t *)mdSlot->etc;
00171   return nss_dbm_mdToken_factory(slot, pError);
00172 }
00173 
00174 NSS_IMPLEMENT NSSCKMDSlot *
00175 nss_dbm_mdSlot_factory
00176 (
00177   nss_dbm_instance_t *instance,
00178   char *filename,
00179   int flags,
00180   CK_RV *pError
00181 )
00182 {
00183   nss_dbm_slot_t *slot;
00184   NSSCKMDSlot *rv;
00185 
00186   slot = nss_ZNEW(instance->arena, nss_dbm_slot_t);
00187   if( (nss_dbm_slot_t *)NULL == slot ) {
00188     *pError = CKR_HOST_MEMORY;
00189     return (NSSCKMDSlot *)NULL;
00190   }
00191 
00192   slot->instance = instance;
00193   slot->filename = filename;
00194   slot->flags = flags;
00195   slot->token_db = (nss_dbm_db_t *)NULL;
00196 
00197   rv = nss_ZNEW(instance->arena, NSSCKMDSlot);
00198   if( (NSSCKMDSlot *)NULL == rv ) {
00199     *pError = CKR_HOST_MEMORY;
00200     return (NSSCKMDSlot *)NULL;
00201   }
00202 
00203   rv->etc = (void *)slot;
00204   rv->Initialize = nss_dbm_mdSlot_Initialize;
00205   rv->Destroy = nss_dbm_mdSlot_Destroy;
00206   rv->GetSlotDescription = nss_dbm_mdSlot_GetSlotDescription;
00207   rv->GetManufacturerID = nss_dbm_mdSlot_GetManufacturerID;
00208   rv->GetTokenPresent = nss_dbm_mdSlot_GetTokenPresent;
00209   rv->GetRemovableDevice = nss_dbm_mdSlot_GetRemovableDevice;
00210   /*  GetHardwareSlot */
00211   /*  GetHardwareVersion */
00212   /*  GetFirmwareVersion */
00213   rv->GetToken = nss_dbm_mdSlot_GetToken;
00214   rv->null = (void *)NULL;
00215 
00216   return rv;
00217 }