Back to index

lightning-sunbird  0.9+nobinonly
remtest.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 /*
00038 **
00039 ** Sample client side test program that uses SSL and libsec
00040 **
00041 */
00042 
00043 #include "secutil.h"
00044 
00045 #if defined(XP_UNIX)
00046 #include <unistd.h>
00047 #else
00048 #include "ctype.h"   /* for isalpha() */
00049 #endif
00050 
00051 #include <stdio.h>
00052 #include <string.h>
00053 #include <stdlib.h>
00054 #include <errno.h>
00055 #include <fcntl.h>
00056 #include <stdarg.h>
00057 
00058 #include "nspr.h"
00059 #include "prio.h"
00060 #include "prnetdb.h"
00061 #include "nss.h"
00062 #include "pk11func.h"
00063 #include "plgetopt.h"
00064 
00065 void
00066 Usage(char *progName) 
00067 {
00068     fprintf(stderr,"usage: %s [-d profiledir] -t tokenName [-r]\n", progName);
00069     exit(1);
00070 }
00071 
00072 int main(int argc, char **argv)
00073 {
00074     char *             certDir  =  NULL;
00075     PLOptState *optstate;
00076     PLOptStatus optstatus;
00077     SECStatus rv;
00078     char * tokenName = NULL;
00079     PRBool cont=PR_TRUE;
00080     PK11TokenEvent event = PK11TokenPresentEvent;
00081     PK11TokenStatus status;
00082     char *progName;
00083     PK11SlotInfo *slot;
00084 
00085     progName = strrchr(argv[0], '/');
00086     if (!progName)
00087        progName = strrchr(argv[0], '\\');
00088     progName = progName ? progName+1 : argv[0];
00089 
00090     optstate = PL_CreateOptState(argc, argv, "rd:t:");
00091     while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
00092        switch (optstate->option) {
00093 
00094          case 'd':
00095            certDir = strdup(optstate->value);
00096            certDir = SECU_ConfigDirectory(certDir);
00097            break;
00098          case 't':
00099            tokenName = strdup(optstate->value);
00100            break;
00101          case 'r':
00102            event = PK11TokenRemovedOrChangedEvent;
00103            break;
00104        }
00105     }
00106     if (optstatus == PL_OPT_BAD)
00107        Usage(progName);
00108 
00109     if (tokenName == NULL) {
00110        Usage(progName);
00111     }
00112 
00113     if (!certDir) {
00114        certDir = SECU_DefaultSSLDir();    /* Look in $SSL_DIR */
00115        certDir = SECU_ConfigDirectory(certDir); /* call even if it's NULL */
00116     }
00117 
00118     PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
00119 
00120     PK11_SetPasswordFunc(SECU_GetModulePassword);
00121 
00122     /* open the cert DB, the key DB, and the secmod DB. */
00123     rv = NSS_Init(certDir);
00124     if (rv != SECSuccess) {
00125        SECU_PrintError(progName, "unable to open cert database");
00126        return 1;
00127     }
00128 
00129     printf("Looking up tokenNamed: <%s>\n",tokenName);
00130     slot = PK11_FindSlotByName(tokenName);
00131     if (slot == NULL) {
00132        SECU_PrintError(progName, "unable to find token");
00133        return 1;
00134     }
00135 
00136     do {
00137        status = 
00138           PK11_WaitForTokenEvent(slot,event,PR_INTERVAL_NO_TIMEOUT, 0, 0);
00139 
00140        switch (status) {
00141        case PK11TokenNotRemovable:
00142            cont = PR_FALSE;
00143            printf("%s Token Not Removable\n",tokenName);
00144            break;
00145        case PK11TokenChanged:
00146            event = PK11TokenRemovedOrChangedEvent;
00147            printf("%s Token Changed\n", tokenName);
00148            break;
00149        case PK11TokenRemoved:
00150            event = PK11TokenPresentEvent;
00151            printf("%s Token Removed\n", tokenName);
00152            break;
00153        case PK11TokenPresent:
00154            event = PK11TokenRemovedOrChangedEvent;
00155            printf("%s Token Present\n", tokenName);
00156            break;
00157        }
00158     } while (cont);
00159 
00160     if (NSS_Shutdown() != SECSuccess) {
00161         exit(1);
00162     }
00163     PR_Cleanup();
00164     return 0;
00165 }