~ubuntu-branches/ubuntu/lucid/seamonkey/lucid-security

« back to all changes in this revision

Viewing changes to security/nss-fips/cmd/dbtest/dbtest.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabien Tassin
  • Date: 2008-07-29 21:29:02 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080729212902-spm9kpvchp9udwbw
Tags: 1.1.11+nobinonly-0ubuntu1
* New security upstream release: 1.1.11 (LP: #218534)
  Fixes USN-602-1, USN-619-1, USN-623-1 and USN-629-1
* Refresh diverged patch:
  - update debian/patches/80_security_build.patch
* Fix FTBFS with missing -lfontconfig
  - add debian/patches/11_fix_ftbfs_with_fontconfig.patch
  - update debian/patches/series
* Build with default gcc (hardy: 4.2, intrepid: 4.3)
  - update debian/rules
  - update debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
3
 *
 
4
 * The contents of this file are subject to the Mozilla Public License Version
 
5
 * 1.1 (the "License"); you may not use this file except in compliance with
 
6
 * the License. You may obtain a copy of the License at
 
7
 * http://www.mozilla.org/MPL/
 
8
 *
 
9
 * Software distributed under the License is distributed on an "AS IS" basis,
 
10
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
11
 * for the specific language governing rights and limitations under the
 
12
 * License.
 
13
 *
 
14
 * The Original Code is the Netscape security libraries.
 
15
 *
 
16
 * The Initial Developer of the Original Code is
 
17
 * Netscape Communications Corporation.
 
18
 * Portions created by the Initial Developer are Copyright (C) 1994-2000
 
19
 * the Initial Developer. All Rights Reserved.
 
20
 *
 
21
 * Contributor(s):
 
22
 *   Sonja Mirtitsch Sun Microsystems
 
23
 *
 
24
 * Alternatively, the contents of this file may be used under the terms of
 
25
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
26
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
27
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
28
 * of those above. If you wish to allow use of your version of this file only
 
29
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
30
 * use your version of this file under the terms of the MPL, indicate your
 
31
 * decision by deleting the provisions above and replace them with the notice
 
32
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
33
 * the provisions above, a recipient may use your version of this file under
 
34
 * the terms of any one of the MPL, the GPL or the LGPL.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
38
/*
 
39
** dbtest.c
 
40
**
 
41
** QA test for cert and key databases, especially to open
 
42
** database readonly (NSS_INIT_READONLY) and force initializations
 
43
** even if the databases cannot be opened (NSS_INIT_FORCEOPEN)
 
44
**
 
45
*/
 
46
#include <stdio.h>
 
47
#include <string.h>
 
48
 
 
49
#if defined(WIN32)
 
50
#include "fcntl.h"
 
51
#include "io.h"
 
52
#endif
 
53
 
 
54
#include "secutil.h"
 
55
 
 
56
#if defined(XP_UNIX)
 
57
#include <unistd.h>
 
58
#endif
 
59
 
 
60
#include "nspr.h"
 
61
#include "prtypes.h"
 
62
#include "certdb.h"
 
63
#include "nss.h"
 
64
#include "../modutil/modutil.h"
 
65
 
 
66
#include "plgetopt.h"
 
67
 
 
68
static char *progName;
 
69
 
 
70
char *dbDir  =  NULL;
 
71
 
 
72
static char *dbName[]={"secmod.db", "cert8.db", "key3.db"}; 
 
73
static char* dbprefix = "";
 
74
static char* secmodName = "secmod.db";
 
75
PRBool verbose;
 
76
 
 
77
 
 
78
static void Usage(const char *progName)
 
79
{
 
80
    printf("Usage:  %s [-r] [-f] [-d dbdir ] \n",
 
81
         progName);
 
82
    printf("%-20s open database readonly (NSS_INIT_READONLY)\n", "-r");
 
83
    printf("%-20s Continue to force initializations even if the\n", "-f");
 
84
    printf("%-20s databases cannot be opened (NSS_INIT_FORCEOPEN)\n", " ");
 
85
    printf("%-20s Directory with cert database (default is .\n",
 
86
          "-d certdir");
 
87
    exit(1);
 
88
}
 
89
 
 
90
int main(int argc, char **argv)
 
91
{
 
92
    PLOptState *optstate;
 
93
    PLOptStatus optstatus;
 
94
 
 
95
    PRUint32 flags = 0;
 
96
    Error ret;
 
97
    SECStatus rv;
 
98
    char * dbString = NULL;
 
99
    int i;
 
100
 
 
101
    progName = strrchr(argv[0], '/');
 
102
    if (!progName)
 
103
        progName = strrchr(argv[0], '\\');
 
104
    progName = progName ? progName+1 : argv[0];
 
105
 
 
106
    optstate = PL_CreateOptState(argc, argv, "rfd:h");
 
107
 
 
108
    while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
 
109
        switch (optstate->option) {
 
110
          case 'h':
 
111
          default : Usage(progName);                    break;
 
112
 
 
113
          case 'r': flags |= NSS_INIT_READONLY;         break;
 
114
 
 
115
          case 'f': flags |= NSS_INIT_FORCEOPEN;        break;
 
116
 
 
117
          case 'd':
 
118
                dbDir = PORT_Strdup(optstate->value);
 
119
                break;
 
120
 
 
121
        }
 
122
    }
 
123
    if (optstatus == PL_OPT_BAD)
 
124
        Usage(progName);
 
125
 
 
126
    if (!dbDir) {
 
127
        dbDir = SECU_DefaultSSLDir(); /* Look in $SSL_DIR */
 
128
    }
 
129
    dbDir = SECU_ConfigDirectory(dbDir);
 
130
    PR_fprintf(PR_STDERR, "dbdir selected is %s\n\n", dbDir);
 
131
 
 
132
    if( dbDir[0] == '\0') {
 
133
        PR_fprintf(PR_STDERR, errStrings[DIR_DOESNT_EXIST_ERR], dbDir);
 
134
        ret= DIR_DOESNT_EXIST_ERR;
 
135
        goto loser;
 
136
    }
 
137
 
 
138
 
 
139
    PR_Init( PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
 
140
 
 
141
    /* get the status of the directory and databases and output message */
 
142
    if(PR_Access(dbDir, PR_ACCESS_EXISTS) != PR_SUCCESS) {
 
143
        PR_fprintf(PR_STDERR, errStrings[DIR_DOESNT_EXIST_ERR], dbDir);
 
144
    } else if(PR_Access(dbDir, PR_ACCESS_READ_OK) != PR_SUCCESS) {
 
145
        PR_fprintf(PR_STDERR, errStrings[DIR_NOT_READABLE_ERR], dbDir);
 
146
    } else {
 
147
        if( !( flags & NSS_INIT_READONLY ) &&
 
148
                PR_Access(dbDir, PR_ACCESS_WRITE_OK) != PR_SUCCESS) {
 
149
            PR_fprintf(PR_STDERR, errStrings[DIR_NOT_WRITEABLE_ERR], dbDir);
 
150
        }
 
151
        for (i=0;i<3;i++) {
 
152
            dbString=PR_smprintf("%s/%s",dbDir,dbName[i]);
 
153
            PR_fprintf(PR_STDOUT, "database checked is %s\n",dbString);
 
154
            if(PR_Access(dbString, PR_ACCESS_EXISTS) != PR_SUCCESS) {
 
155
                PR_fprintf(PR_STDERR, errStrings[FILE_DOESNT_EXIST_ERR], 
 
156
                                      dbString);
 
157
            } else if(PR_Access(dbString, PR_ACCESS_READ_OK) != PR_SUCCESS) {
 
158
                PR_fprintf(PR_STDERR, errStrings[FILE_NOT_READABLE_ERR], 
 
159
                                      dbString);
 
160
            } else if( !( flags & NSS_INIT_READONLY ) &&
 
161
                    PR_Access(dbString, PR_ACCESS_WRITE_OK) != PR_SUCCESS) {
 
162
                PR_fprintf(PR_STDERR, errStrings[FILE_NOT_WRITEABLE_ERR], 
 
163
                                      dbString);
 
164
            }
 
165
        }
 
166
    }
 
167
 
 
168
    rv = NSS_Initialize(SECU_ConfigDirectory(dbDir), dbprefix, dbprefix,
 
169
                   secmodName, flags);
 
170
    if (rv != SECSuccess) {
 
171
        SECU_PrintPRandOSError(progName);
 
172
        ret=NSS_INITIALIZE_FAILED_ERR;
 
173
    } else {
 
174
        if (NSS_Shutdown() != SECSuccess) {
 
175
            exit(1);
 
176
        }
 
177
        ret=SUCCESS;
 
178
    }
 
179
 
 
180
loser:
 
181
    return ret;
 
182
}
 
183