~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/security/nss/cmd/dbtest/dbtest.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

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