2
* NSS utility functions
4
* The contents of this file are subject to the Mozilla Public
5
* License Version 1.1 (the "License"); you may not use this file
6
* except in compliance with the License. You may obtain a copy of
7
* the License at http://www.mozilla.org/MPL/
9
* Software distributed under the License is distributed on an "AS
10
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11
* implied. See the License for the specific language governing
12
* rights and limitations under the License.
14
* The Original Code is the Netscape security libraries.
16
* The Initial Developer of the Original Code is Netscape
17
* Communications Corporation. Portions created by Netscape are
18
* Copyright (C) 1994-2000 Netscape Communications Corporation. All
23
* Alternatively, the contents of this file may be used under the
24
* terms of the GNU General Public License Version 2 or later (the
25
* "GPL"), in which case the provisions of the GPL are applicable
26
* instead of those above. If you wish to allow use of your
27
* version of this file only under the terms of the GPL and not to
28
* allow others to use your version of this file under the MPL,
29
* indicate your decision by deleting the provisions above and
30
* replace them with the notice and other provisions required by
31
* the GPL. If you do not delete the provisions above, a recipient
32
* may use your version of this file under either the MPL or the
35
# $Id: dbinit.c,v 1.20 2003/05/30 23:31:30 wtc%netscape.com Exp $
50
pk11_certdb_name_cb(void *arg, int dbVersion)
52
const char *configdir = (const char *)arg;
76
/* make sure we return something allocated with PORT_ so we have properly
77
* matched frees at the end */
78
smpname = PR_smprintf(CERT_DB_FMT, configdir, dbver);
80
dbname = PORT_Strdup(smpname);
81
PR_smprintf_free(smpname);
87
pk11_keydb_name_cb(void *arg, int dbVersion)
89
const char *configdir = (const char *)arg;
110
smpname = PR_smprintf(KEY_DB_FMT, configdir, dbver);
112
dbname = PORT_Strdup(smpname);
113
PR_smprintf_free(smpname);
118
/* for now... we need to define vendor specific codes here.
120
#define CKR_CERTDB_FAILED CKR_DEVICE_ERROR
121
#define CKR_KEYDB_FAILED CKR_DEVICE_ERROR
124
pk11_EvaluateConfigDir(const char *configdir,char **appName)
126
if (PORT_Strncmp(configdir, MULTIACCESS, sizeof(MULTIACCESS)-1) == 0) {
129
*appName = PORT_Strdup(configdir+sizeof(MULTIACCESS)-1);
130
if (*appName == NULL) {
134
while (*cdir && *cdir != ':') {
147
pk11_OpenCertDB(const char * configdir, const char *prefix, PRBool readOnly,
148
NSSLOWCERTCertDBHandle **certdbPtr)
150
NSSLOWCERTCertDBHandle *certdb = NULL;
151
CK_RV crv = CKR_CERTDB_FAILED;
154
char * appName = NULL;
156
if (prefix == NULL) {
160
configdir = pk11_EvaluateConfigDir(configdir, &appName);
162
name = PR_smprintf("%s" PATH_SEPARATOR "%s",configdir,prefix);
163
if (name == NULL) goto loser;
165
certdb = (NSSLOWCERTCertDBHandle*)PORT_ZAlloc(sizeof(NSSLOWCERTCertDBHandle));
169
/* fix when we get the DB in */
170
rv = nsslowcert_OpenCertDB(certdb, readOnly, appName, prefix,
171
pk11_certdb_name_cb, (void *)name, PR_FALSE);
172
if (rv == SECSuccess) {
178
if (certdb) PR_Free(certdb);
179
if (name) PR_smprintf_free(name);
180
if (appName) PORT_Free(appName);
185
pk11_OpenKeyDB(const char * configdir, const char *prefix, PRBool readOnly,
186
NSSLOWKEYDBHandle **keydbPtr)
188
NSSLOWKEYDBHandle *keydb;
190
char * appName = NULL;
192
if (prefix == NULL) {
195
configdir = pk11_EvaluateConfigDir(configdir, &appName);
197
name = PR_smprintf("%s" PATH_SEPARATOR "%s",configdir,prefix);
200
keydb = nsslowkey_OpenKeyDB(readOnly, appName, prefix,
201
pk11_keydb_name_cb, (void *)name);
202
PR_smprintf_free(name);
203
if (appName) PORT_Free(appName);
205
return CKR_KEYDB_FAILED;
213
* OK there are now lots of options here, lets go through them all:
215
* configdir - base directory where all the cert, key, and module datbases live.
216
* certPrefix - prefix added to the beginning of the cert database example: "
218
* keyPrefix - prefix added to the beginning of the key database example: "
220
* secmodName - name of the security module database (usually "secmod.db").
221
* readOnly - Boolean: true if the databases are to be openned read only.
222
* nocertdb - Don't open the cert DB and key DB's, just initialize the
224
* nomoddb - Don't open the security module DB, just initialize the
226
* forceOpen - Continue to force initializations even if the databases cannot
230
pk11_DBInit(const char *configdir, const char *certPrefix,
231
const char *keyPrefix, PRBool readOnly,
232
PRBool noCertDB, PRBool noKeyDB, PRBool forceOpen,
233
NSSLOWCERTCertDBHandle **certdbPtr, NSSLOWKEYDBHandle **keydbPtr)
239
crv = pk11_OpenCertDB(configdir, certPrefix, readOnly, certdbPtr);
241
if (!forceOpen) goto loser;
247
crv = pk11_OpenKeyDB(configdir, keyPrefix, readOnly, keydbPtr);
249
if (!forceOpen) goto loser;
261
pk11_DBShutdown(NSSLOWCERTCertDBHandle *certHandle,
262
NSSLOWKEYDBHandle *keyHandle)
265
nsslowcert_ClosePermCertDB(certHandle);
266
PORT_Free(certHandle);
271
nsslowkey_CloseKeyDB(keyHandle);
276
static int rdbmapflags(int flags);
277
static rdbfunc pk11_rdbfunc = NULL;
278
static rdbstatusfunc pk11_rdbstatusfunc = NULL;
280
/* NOTE: SHLIB_SUFFIX is defined on the command line */
281
#define RDBLIB SHLIB_PREFIX"rdb."SHLIB_SUFFIX
283
DB * rdbopen(const char *appName, const char *prefix,
284
const char *type, int flags, int *status)
290
db = (*pk11_rdbfunc)(appName,prefix,type,rdbmapflags(flags));
291
if (!db && status && pk11_rdbstatusfunc) {
292
*status = (*pk11_rdbstatusfunc)();
298
* try to open the library.
300
lib = PR_LoadLibrary(RDBLIB);
306
/* get the entry points */
307
pk11_rdbstatusfunc = (rdbstatusfunc) PR_FindSymbol(lib,"rdbstatus");
308
pk11_rdbfunc = (rdbfunc) PR_FindSymbol(lib,"rdbopen");
310
db = (*pk11_rdbfunc)(appName,prefix,type,rdbmapflags(flags));
311
if (!db && status && pk11_rdbstatusfunc) {
312
*status = (*pk11_rdbstatusfunc)();
317
/* couldn't find the entry point, unload the library and fail */
318
PR_UnloadLibrary(lib);
323
* the following data structures are from rdb.h.
327
int (*xactstart)(DB *db);
328
int (*xactdone)(DB *db, PRBool abort);
330
int (*dbinitcomplete)(DB *db);
333
#define DB_RDB ((DBTYPE) 0xff)
339
rdbmapflags(int flags) {
357
return (PRBool) db->type == DB_RDB;
361
db_BeginTransaction(DB *db)
363
struct RDBStr *rdb = (struct RDBStr *)db;
364
if (db->type != DB_RDB) {
368
return rdb->xactstart(db);
372
db_FinishTransaction(DB *db, PRBool abort)
374
struct RDBStr *rdb = (struct RDBStr *)db;
375
if (db->type != DB_RDB) {
379
return rdb->xactdone(db, abort);
383
db_InitComplete(DB *db)
385
struct RDBStr *rdb = (struct RDBStr *)db;
386
if (db->type != DB_RDB) {
389
/* we should have addes a version number to the RDBS structure. Since we
390
* didn't, we detect that we have and 'extended' structure if the rdbstatus
392
if (!pk11_rdbstatusfunc) {
396
return rdb->dbinitcomplete(db);
402
db_Copy(DB *dest,DB *src)
406
ret = (*src->seq)(src, &key, &data, R_FIRST);
412
(void)(*dest->put)(dest,&key,&data, R_NOOVERWRITE);
413
} while ( (*src->seq)(src, &key, &data, R_NEXT) == 0);
414
(void)(*dest->sync)(dest,0);