andre@3: /* This Source Code Form is subject to the terms of the Mozilla Public andre@3: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@3: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@3: andre@3: /* andre@3: * Berkeley DB 1.85 Shim code to handle blobs. andre@3: */ andre@3: #include "mcom_db.h" andre@3: #include "secitem.h" andre@3: #include "nssb64.h" andre@3: #include "blapi.h" andre@3: #include "secerr.h" andre@3: andre@3: #include "lgdb.h" andre@3: andre@3: /* andre@3: * Blob block: andre@3: * Byte 0 CERTDB Version -+ -+ andre@3: * Byte 1 certDBEntryTypeBlob | BLOB_HEAD_LEN | andre@3: * Byte 2 flags (always '0'); | | andre@3: * Byte 3 reserved (always '0'); -+ | andre@3: * Byte 4 LSB length | <--BLOB_LENGTH_START | BLOB_BUF_LEN andre@3: * Byte 5 . | | andre@3: * Byte 6 . | BLOB_LENGTH_LEN | andre@3: * Byte 7 MSB length | | andre@3: * Byte 8 blob_filename -+ -+ <-- BLOB_NAME_START | andre@3: * Byte 9 . | BLOB_NAME_LEN | andre@3: * . . | | andre@3: * Byte 37 . -+ -+ andre@3: */ andre@3: #define DBS_BLOCK_SIZE (16*1024) /* 16 k */ andre@3: #define DBS_MAX_ENTRY_SIZE (DBS_BLOCK_SIZE - (2048)) /* 14 k */ andre@3: #define DBS_CACHE_SIZE DBS_BLOCK_SIZE*8 andre@3: #define ROUNDDIV(x,y) (x+(y-1))/y andre@3: #define BLOB_HEAD_LEN 4 andre@3: #define BLOB_LENGTH_START BLOB_HEAD_LEN andre@3: #define BLOB_LENGTH_LEN 4 andre@3: #define BLOB_NAME_START BLOB_LENGTH_START+BLOB_LENGTH_LEN andre@3: #define BLOB_NAME_LEN 1+ROUNDDIV(SHA1_LENGTH,3)*4+1 andre@3: #define BLOB_BUF_LEN BLOB_HEAD_LEN+BLOB_LENGTH_LEN+BLOB_NAME_LEN andre@3: andre@3: /* a Shim data structure. This data structure has a db built into it. */ andre@3: typedef struct DBSStr DBS; andre@3: andre@3: struct DBSStr { andre@3: DB db; andre@3: char *blobdir; andre@3: int mode; andre@3: PRBool readOnly; andre@3: PRFileMap *dbs_mapfile; andre@3: unsigned char *dbs_addr; andre@3: PRUint32 dbs_len; andre@3: char staticBlobArea[BLOB_BUF_LEN]; andre@3: }; andre@3: andre@3: andre@3: andre@3: /* andre@3: * return true if the Datablock contains a blobtype andre@3: */ andre@3: static PRBool andre@3: dbs_IsBlob(DBT *blobData) andre@3: { andre@3: unsigned char *addr = (unsigned char *)blobData->data; andre@3: if (blobData->size < BLOB_BUF_LEN) { andre@3: return PR_FALSE; andre@3: } andre@3: return addr && ((certDBEntryType) addr[1] == certDBEntryTypeBlob); andre@3: } andre@3: andre@3: /* andre@3: * extract the filename in the blob of the real data set. andre@3: * This value is not malloced (does not need to be freed by the caller. andre@3: */ andre@3: static const char * andre@3: dbs_getBlobFileName(DBT *blobData) andre@3: { andre@3: char *addr = (char *)blobData->data; andre@3: andre@3: return &addr[BLOB_NAME_START]; andre@3: } andre@3: andre@3: /* andre@3: * extract the size of the actual blob from the blob record andre@3: */ andre@3: static PRUint32 andre@3: dbs_getBlobSize(DBT *blobData) andre@3: { andre@3: unsigned char *addr = (unsigned char *)blobData->data; andre@3: andre@3: return (PRUint32)(addr[BLOB_LENGTH_START+3] << 24) | andre@3: (addr[BLOB_LENGTH_START+2] << 16) | andre@3: (addr[BLOB_LENGTH_START+1] << 8) | andre@3: addr[BLOB_LENGTH_START]; andre@3: } andre@3: andre@3: andre@3: /* We are using base64 data for the filename, but base64 data can include a andre@3: * '/' which is interpreted as a path separator on many platforms. Replace it andre@3: * with an inocuous '-'. We don't need to convert back because we never actual andre@3: * decode the filename. andre@3: */ andre@3: andre@3: static void andre@3: dbs_replaceSlash(char *cp, int len) andre@3: { andre@3: while (len--) { andre@3: if (*cp == '/') *cp = '-'; andre@3: cp++; andre@3: } andre@3: } andre@3: andre@3: /* andre@3: * create a blob record from a key, data and return it in blobData. andre@3: * NOTE: The data element is static data (keeping with the dbm model). andre@3: */ andre@3: static void andre@3: dbs_mkBlob(DBS *dbsp,const DBT *key, const DBT *data, DBT *blobData) andre@3: { andre@3: unsigned char sha1_data[SHA1_LENGTH]; andre@3: char *b = dbsp->staticBlobArea; andre@3: PRUint32 length = data->size; andre@3: SECItem sha1Item; andre@3: andre@3: b[0] = CERT_DB_FILE_VERSION; /* certdb version number */ andre@3: b[1] = (char) certDBEntryTypeBlob; /* type */ andre@3: b[2] = 0; /* flags */ andre@3: b[3] = 0; /* reserved */ andre@3: b[BLOB_LENGTH_START] = length & 0xff; andre@3: b[BLOB_LENGTH_START+1] = (length >> 8) & 0xff; andre@3: b[BLOB_LENGTH_START+2] = (length >> 16) & 0xff; andre@3: b[BLOB_LENGTH_START+3] = (length >> 24) & 0xff; andre@3: sha1Item.data = sha1_data; andre@3: sha1Item.len = SHA1_LENGTH; andre@3: SHA1_HashBuf(sha1_data,key->data,key->size); andre@3: b[BLOB_NAME_START]='b'; /* Make sure we start with a alpha */ andre@3: NSSBase64_EncodeItem(NULL,&b[BLOB_NAME_START+1],BLOB_NAME_LEN-1,&sha1Item); andre@3: b[BLOB_BUF_LEN-1] = 0; andre@3: dbs_replaceSlash(&b[BLOB_NAME_START+1],BLOB_NAME_LEN-1); andre@3: blobData->data = b; andre@3: blobData->size = BLOB_BUF_LEN; andre@3: return; andre@3: } andre@3: andre@3: andre@3: /* andre@3: * construct a path to the actual blob. The string returned must be andre@3: * freed by the caller with PR_smprintf_free. andre@3: * andre@3: * Note: this file does lots of consistancy checks on the DBT. The andre@3: * routines that call this depend on these checks, so they don't worry andre@3: * about them (success of this routine implies a good blobdata record). andre@3: */ andre@3: static char * andre@3: dbs_getBlobFilePath(char *blobdir,DBT *blobData) andre@3: { andre@3: const char *name; andre@3: andre@3: if (blobdir == NULL) { andre@3: PR_SetError(SEC_ERROR_BAD_DATABASE,0); andre@3: return NULL; andre@3: } andre@3: if (!dbs_IsBlob(blobData)) { andre@3: PR_SetError(SEC_ERROR_BAD_DATABASE,0); andre@3: return NULL; andre@3: } andre@3: name = dbs_getBlobFileName(blobData); andre@3: if (!name || *name == 0) { andre@3: PR_SetError(SEC_ERROR_BAD_DATABASE,0); andre@3: return NULL; andre@3: } andre@3: return PR_smprintf("%s" PATH_SEPARATOR "%s", blobdir, name); andre@3: } andre@3: andre@3: /* andre@3: * Delete a blob file pointed to by the blob record. andre@3: */ andre@3: static void andre@3: dbs_removeBlob(DBS *dbsp, DBT *blobData) andre@3: { andre@3: char *file; andre@3: andre@3: file = dbs_getBlobFilePath(dbsp->blobdir, blobData); andre@3: if (!file) { andre@3: return; andre@3: } andre@3: PR_Delete(file); andre@3: PR_smprintf_free(file); andre@3: } andre@3: andre@3: /* andre@3: * Directory modes are slightly different, the 'x' bit needs to be on to andre@3: * access them. Copy all the read bits to 'x' bits andre@3: */ andre@3: static int andre@3: dbs_DirMode(int mode) andre@3: { andre@3: int x_bits = (mode >> 2) & 0111; andre@3: return mode | x_bits; andre@3: } andre@3: andre@3: /* andre@3: * write a data blob to it's file. blobdData is the blob record that will be andre@3: * stored in the database. data is the actual data to go out on disk. andre@3: */ andre@3: static int andre@3: dbs_writeBlob(DBS *dbsp, int mode, DBT *blobData, const DBT *data) andre@3: { andre@3: char *file = NULL; andre@3: PRFileDesc *filed; andre@3: PRStatus status; andre@3: int len; andre@3: int error = 0; andre@3: andre@3: file = dbs_getBlobFilePath(dbsp->blobdir, blobData); andre@3: if (!file) { andre@3: goto loser; andre@3: } andre@3: if (PR_Access(dbsp->blobdir, PR_ACCESS_EXISTS) != PR_SUCCESS) { andre@3: status = PR_MkDir(dbsp->blobdir,dbs_DirMode(mode)); andre@3: if (status != PR_SUCCESS) { andre@3: goto loser; andre@3: } andre@3: } andre@3: filed = PR_OpenFile(file,PR_CREATE_FILE|PR_TRUNCATE|PR_WRONLY, mode); andre@3: if (filed == NULL) { andre@3: error = PR_GetError(); andre@3: goto loser; andre@3: } andre@3: len = PR_Write(filed,data->data,data->size); andre@3: error = PR_GetError(); andre@3: PR_Close(filed); andre@3: if (len < (int)data->size) { andre@3: goto loser; andre@3: } andre@3: PR_smprintf_free(file); andre@3: return 0; andre@3: andre@3: loser: andre@3: if (file) { andre@3: PR_Delete(file); andre@3: PR_smprintf_free(file); andre@3: } andre@3: /* don't let close or delete reset the error */ andre@3: PR_SetError(error,0); andre@3: return -1; andre@3: } andre@3: andre@3: andre@3: /* andre@3: * we need to keep a address map in memory between calls to DBM. andre@3: * remember what we have mapped can close it when we get another dbm andre@3: * call. andre@3: * andre@3: * NOTE: Not all platforms support mapped files. This code is designed to andre@3: * detect this at runtime. If map files aren't supported the OS will indicate andre@3: * this by failing the PR_Memmap call. In this case we emulate mapped files andre@3: * by just reading in the file into regular memory. We signal this state by andre@3: * making dbs_mapfile NULL and dbs_addr non-NULL. andre@3: */ andre@3: andre@3: static void andre@3: dbs_freemap(DBS *dbsp) andre@3: { andre@3: if (dbsp->dbs_mapfile) { andre@3: PR_MemUnmap(dbsp->dbs_addr,dbsp->dbs_len); andre@3: PR_CloseFileMap(dbsp->dbs_mapfile); andre@3: dbsp->dbs_mapfile = NULL; andre@3: dbsp->dbs_addr = NULL; andre@3: dbsp->dbs_len = 0; andre@3: } else if (dbsp->dbs_addr) { andre@3: PORT_Free(dbsp->dbs_addr); andre@3: dbsp->dbs_addr = NULL; andre@3: dbsp->dbs_len = 0; andre@3: } andre@3: return; andre@3: } andre@3: andre@3: static void andre@3: dbs_setmap(DBS *dbsp, PRFileMap *mapfile, unsigned char *addr, PRUint32 len) andre@3: { andre@3: dbsp->dbs_mapfile = mapfile; andre@3: dbsp->dbs_addr = addr; andre@3: dbsp->dbs_len = len; andre@3: } andre@3: andre@3: /* andre@3: * platforms that cannot map the file need to read it into a temp buffer. andre@3: */ andre@3: static unsigned char * andre@3: dbs_EmulateMap(PRFileDesc *filed, int len) andre@3: { andre@3: unsigned char *addr; andre@3: PRInt32 dataRead; andre@3: andre@3: addr = PORT_Alloc(len); andre@3: if (addr == NULL) { andre@3: return NULL; andre@3: } andre@3: andre@3: dataRead = PR_Read(filed,addr,len); andre@3: if (dataRead != len) { andre@3: PORT_Free(addr); andre@3: if (dataRead > 0) { andre@3: /* PR_Read didn't set an error, we need to */ andre@3: PR_SetError(SEC_ERROR_BAD_DATABASE,0); andre@3: } andre@3: return NULL; andre@3: } andre@3: andre@3: return addr; andre@3: } andre@3: andre@3: andre@3: /* andre@3: * pull a database record off the disk andre@3: * data points to the blob record on input and the real record (if we could andre@3: * read it) on output. if there is an error data is not modified. andre@3: */ andre@3: static int andre@3: dbs_readBlob(DBS *dbsp, DBT *data) andre@3: { andre@3: char *file = NULL; andre@3: PRFileDesc *filed = NULL; andre@3: PRFileMap *mapfile = NULL; andre@3: unsigned char *addr = NULL; andre@3: int error; andre@3: int len = -1; andre@3: andre@3: file = dbs_getBlobFilePath(dbsp->blobdir, data); andre@3: if (!file) { andre@3: goto loser; andre@3: } andre@3: filed = PR_OpenFile(file,PR_RDONLY,0); andre@3: PR_smprintf_free(file); file = NULL; andre@3: if (filed == NULL) { andre@3: goto loser; andre@3: } andre@3: andre@3: len = dbs_getBlobSize(data); andre@3: mapfile = PR_CreateFileMap(filed, len, PR_PROT_READONLY); andre@3: if (mapfile == NULL) { andre@3: /* USE PR_GetError instead of PORT_GetError here andre@3: * because we are getting the error from PR_xxx andre@3: * function */ andre@3: if (PR_GetError() != PR_NOT_IMPLEMENTED_ERROR) { andre@3: goto loser; andre@3: } andre@3: addr = dbs_EmulateMap(filed, len); andre@3: } else { andre@3: addr = PR_MemMap(mapfile, 0, len); andre@3: } andre@3: if (addr == NULL) { andre@3: goto loser; andre@3: } andre@3: PR_Close(filed); andre@3: dbs_setmap(dbsp,mapfile,addr,len); andre@3: andre@3: data->data = addr; andre@3: data->size = len; andre@3: return 0; andre@3: andre@3: loser: andre@3: /* preserve the error code */ andre@3: error = PR_GetError(); andre@3: if (mapfile) { andre@3: PR_CloseFileMap(mapfile); andre@3: } andre@3: if (filed) { andre@3: PR_Close(filed); andre@3: } andre@3: PR_SetError(error,0); andre@3: return -1; andre@3: } andre@3: andre@3: /* andre@3: * actual DBM shims andre@3: */ andre@3: static int andre@3: dbs_get(const DB *dbs, const DBT *key, DBT *data, unsigned int flags) andre@3: { andre@3: int ret; andre@3: DBS *dbsp = (DBS *)dbs; andre@3: DB *db = (DB *)dbs->internal; andre@3: andre@3: andre@3: dbs_freemap(dbsp); andre@3: andre@3: ret = (* db->get)(db, key, data, flags); andre@3: if ((ret == 0) && dbs_IsBlob(data)) { andre@3: ret = dbs_readBlob(dbsp,data); andre@3: } andre@3: andre@3: return(ret); andre@3: } andre@3: andre@3: static int andre@3: dbs_put(const DB *dbs, DBT *key, const DBT *data, unsigned int flags) andre@3: { andre@3: DBT blob; andre@3: int ret = 0; andre@3: DBS *dbsp = (DBS *)dbs; andre@3: DB *db = (DB *)dbs->internal; andre@3: andre@3: dbs_freemap(dbsp); andre@3: andre@3: /* If the db is readonly, just pass the data down to rdb and let it fail */ andre@3: if (!dbsp->readOnly) { andre@3: DBT oldData; andre@3: int ret1; andre@3: andre@3: /* make sure the current record is deleted if it's a blob */ andre@3: ret1 = (*db->get)(db,key,&oldData,0); andre@3: if ((ret1 == 0) && flags == R_NOOVERWRITE) { andre@3: /* let DBM return the error to maintain consistancy */ andre@3: return (* db->put)(db, key, data, flags); andre@3: } andre@3: if ((ret1 == 0) && dbs_IsBlob(&oldData)) { andre@3: dbs_removeBlob(dbsp, &oldData); andre@3: } andre@3: andre@3: if (data->size > DBS_MAX_ENTRY_SIZE) { andre@3: dbs_mkBlob(dbsp,key,data,&blob); andre@3: ret = dbs_writeBlob(dbsp, dbsp->mode, &blob, data); andre@3: data = &blob; andre@3: } andre@3: } andre@3: andre@3: if (ret == 0) { andre@3: ret = (* db->put)(db, key, data, flags); andre@3: } andre@3: return(ret); andre@3: } andre@3: andre@3: static int andre@3: dbs_sync(const DB *dbs, unsigned int flags) andre@3: { andre@3: DB *db = (DB *)dbs->internal; andre@3: DBS *dbsp = (DBS *)dbs; andre@3: andre@3: dbs_freemap(dbsp); andre@3: andre@3: return (* db->sync)(db, flags); andre@3: } andre@3: andre@3: static int andre@3: dbs_del(const DB *dbs, const DBT *key, unsigned int flags) andre@3: { andre@3: int ret; andre@3: DBS *dbsp = (DBS *)dbs; andre@3: DB *db = (DB *)dbs->internal; andre@3: andre@3: dbs_freemap(dbsp); andre@3: andre@3: if (!dbsp->readOnly) { andre@3: DBT oldData; andre@3: ret = (*db->get)(db,key,&oldData,0); andre@3: if ((ret == 0) && dbs_IsBlob(&oldData)) { andre@3: dbs_removeBlob(dbsp,&oldData); andre@3: } andre@3: } andre@3: andre@3: return (* db->del)(db, key, flags); andre@3: } andre@3: andre@3: static int andre@3: dbs_seq(const DB *dbs, DBT *key, DBT *data, unsigned int flags) andre@3: { andre@3: int ret; andre@3: DBS *dbsp = (DBS *)dbs; andre@3: DB *db = (DB *)dbs->internal; andre@3: andre@3: dbs_freemap(dbsp); andre@3: andre@3: ret = (* db->seq)(db, key, data, flags); andre@3: if ((ret == 0) && dbs_IsBlob(data)) { andre@3: /* don't return a blob read as an error so traversals keep going */ andre@3: (void) dbs_readBlob(dbsp,data); andre@3: } andre@3: andre@3: return(ret); andre@3: } andre@3: andre@3: static int andre@3: dbs_close(DB *dbs) andre@3: { andre@3: DBS *dbsp = (DBS *)dbs; andre@3: DB *db = (DB *)dbs->internal; andre@3: int ret; andre@3: andre@3: dbs_freemap(dbsp); andre@3: ret = (* db->close)(db); andre@3: PORT_Free(dbsp->blobdir); andre@3: PORT_Free(dbsp); andre@3: return ret; andre@3: } andre@3: andre@3: static int andre@3: dbs_fd(const DB *dbs) andre@3: { andre@3: DB *db = (DB *)dbs->internal; andre@3: andre@3: return (* db->fd)(db); andre@3: } andre@3: andre@3: /* andre@3: * the naming convention we use is andre@3: * change the .xxx into .dir. (for nss it's always .db); andre@3: * if no .extension exists or is equal to .dir, add a .dir andre@3: * the returned data must be freed. andre@3: */ andre@3: #define DIRSUFFIX ".dir" andre@3: static char * andre@3: dbs_mkBlobDirName(const char *dbname) andre@3: { andre@3: int dbname_len = PORT_Strlen(dbname); andre@3: int dbname_end = dbname_len; andre@3: const char *cp; andre@3: char *blobDir = NULL; andre@3: andre@3: /* scan back from the end looking for either a directory separator, a '.', andre@3: * or the end of the string. NOTE: Windows should check for both separators andre@3: * here. For now this is safe because we know NSS always uses a '.' andre@3: */ andre@3: for (cp = &dbname[dbname_len]; andre@3: (cp > dbname) && (*cp != '.') && (*cp != *PATH_SEPARATOR) ; andre@3: cp--) andre@3: /* Empty */ ; andre@3: if (*cp == '.') { andre@3: dbname_end = cp - dbname; andre@3: if (PORT_Strcmp(cp,DIRSUFFIX) == 0) { andre@3: dbname_end = dbname_len; andre@3: } andre@3: } andre@3: blobDir = PORT_ZAlloc(dbname_end+sizeof(DIRSUFFIX)); andre@3: if (blobDir == NULL) { andre@3: return NULL; andre@3: } andre@3: PORT_Memcpy(blobDir,dbname,dbname_end); andre@3: PORT_Memcpy(&blobDir[dbname_end],DIRSUFFIX,sizeof(DIRSUFFIX)); andre@3: return blobDir; andre@3: } andre@3: andre@3: #define DBM_DEFAULT 0 andre@3: static const HASHINFO dbs_hashInfo = { andre@3: DBS_BLOCK_SIZE, /* bucket size, must be greater than = to andre@3: * or maximum entry size (+ header) andre@3: * we allow before blobing */ andre@3: DBM_DEFAULT, /* Fill Factor */ andre@3: DBM_DEFAULT, /* number of elements */ andre@3: DBS_CACHE_SIZE, /* cache size */ andre@3: DBM_DEFAULT, /* hash function */ andre@3: DBM_DEFAULT, /* byte order */ andre@3: }; andre@3: andre@3: /* andre@3: * the open function. NOTE: this is the only exposed function in this file. andre@3: * everything else is called through the function table pointer. andre@3: */ andre@3: DB * andre@3: dbsopen(const char *dbname, int flags, int mode, DBTYPE type, andre@3: const void *userData) andre@3: { andre@3: DB *db = NULL,*dbs = NULL; andre@3: DBS *dbsp = NULL; andre@3: andre@3: /* NOTE: we are overriding userData with dbs_hashInfo. since all known andre@3: * callers pass 0, this is ok, otherwise we should merge the two */ andre@3: andre@3: dbsp = (DBS *)PORT_ZAlloc(sizeof(DBS)); andre@3: if (!dbsp) { andre@3: return NULL; andre@3: } andre@3: dbs = &dbsp->db; andre@3: andre@3: dbsp->blobdir=dbs_mkBlobDirName(dbname); andre@3: if (dbsp->blobdir == NULL) { andre@3: goto loser; andre@3: } andre@3: dbsp->mode = mode; andre@3: dbsp->readOnly = (PRBool)(flags == NO_RDONLY); andre@3: dbsp->dbs_mapfile = NULL; andre@3: dbsp->dbs_addr = NULL; andre@3: dbsp->dbs_len = 0; andre@3: andre@3: /* the real dbm call */ andre@3: db = dbopen(dbname, flags, mode, type, &dbs_hashInfo); andre@3: if (db == NULL) { andre@3: goto loser; andre@3: } andre@3: dbs->internal = (void *) db; andre@3: dbs->type = type; andre@3: dbs->close = dbs_close; andre@3: dbs->get = dbs_get; andre@3: dbs->del = dbs_del; andre@3: dbs->put = dbs_put; andre@3: dbs->seq = dbs_seq; andre@3: dbs->sync = dbs_sync; andre@3: dbs->fd = dbs_fd; andre@3: andre@3: return dbs; andre@3: loser: andre@3: if (db) { andre@3: (*db->close)(db); andre@3: } andre@3: if (dbsp) { andre@3: if (dbsp->blobdir) { andre@3: PORT_Free(dbsp->blobdir); andre@3: } andre@3: PORT_Free(dbsp); andre@3: } andre@3: return NULL; andre@3: }