Back to index

lightning-sunbird  0.9+nobinonly
argpin.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 Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /******************************************************
00039  *
00040  *  argpin.c - Returns pin for token specified in a
00041  *  command line paramenter.
00042  *
00043  ******************************************************/
00044 
00045 #include <stdio.h>
00046 #include <string.h>
00047 
00048 #include "argpin.h"
00049 
00050 struct SVRCOREArgPinObj
00051 {
00052   SVRCOREPinObj base;
00053 
00054   char *tokenName;
00055   char *password;
00056   SVRCOREPinObj *alt;
00057 };
00058 static const struct SVRCOREPinMethods vtable;
00059 
00060 /*  XXXceb these are two hacks to fix a problem with the debug builds
00061  *  of svrcore.  With the optimizer turned off, there is a situation
00062  *  in user.c, where these two functions need to be available for the
00063  *  linker (they are imported, and no lib exports them, since they are
00064  *  declared static on XP_UNIX platforms) The short term hack solution
00065  *  is to define them here.  Yeah, it is ugly but, it will need to be
00066  *  here, until a new version of svrcore is done. 
00067  */
00068 
00069 
00070 void echoOff(int fd)
00071 {
00072 }
00073  
00074 void echoOn(int fd)
00075 {
00076 }
00077 
00078 
00079 /* ------------------------------------------------------------ */
00080 SVRCOREError
00081 SVRCORE_CreateArgPinObj(SVRCOREArgPinObj **out, const char * tokenName, const char *password, SVRCOREPinObj *pinObj)
00082 {
00083   SVRCOREError err = 0;
00084   SVRCOREArgPinObj *obj = 0;
00085 
00086   do {
00087     obj = (SVRCOREArgPinObj*)malloc(sizeof (SVRCOREArgPinObj));
00088     if (!obj) { err = 1; break; }
00089 
00090     obj->base.methods = &vtable;
00091     obj->tokenName=NULL;
00092     obj->password=NULL;
00093     obj->alt=pinObj;
00094 
00095     if ( tokenName == NULL) {
00096       PK11SlotInfo *slot = PK11_GetInternalKeySlot();
00097 
00098       obj->tokenName = strdup(PK11_GetTokenName(slot));
00099       PK11_FreeSlot(slot);
00100     }
00101     else
00102     {
00103       obj->tokenName = strdup(tokenName);
00104     }
00105     if (obj->tokenName == NULL) { err = 1; break; }
00106 
00107     obj->password = strdup(password);
00108     if (obj->password == NULL) { err = 1; break; }
00109   } while(0);
00110 
00111   if (err)
00112   {
00113     SVRCORE_DestroyArgPinObj(obj);
00114     obj = 0;
00115   }
00116 
00117   *out = obj;
00118   return err;
00119 }
00120 
00121 void
00122 SVRCORE_DestroyArgPinObj(SVRCOREArgPinObj *obj)
00123 {
00124   if (obj->tokenName) free(obj->tokenName);
00125   if (obj->password)
00126   {
00127     memset(obj->password, 0, strlen(obj->password));
00128     free(obj->password);
00129   }
00130   if (obj) free(obj);
00131 }
00132 
00133 static void destroyObject(SVRCOREPinObj *obj)
00134 {
00135   SVRCORE_DestroyArgPinObj((SVRCOREArgPinObj*)obj);
00136 }
00137 
00138 static char *getPin(SVRCOREPinObj *obj, const char *tokenName, PRBool retry)
00139 {
00140     SVRCOREArgPinObj *p = (SVRCOREArgPinObj*)obj;
00141 
00142     if (tokenName == NULL) return NULL;
00143 
00144     /* On first attempt, return the password if the token name
00145      * matches.
00146      */
00147     if (!retry && strcmp(p->tokenName, tokenName) == 0)
00148     {
00149       return strdup(p->password);
00150     }
00151 
00152     if (p->alt != NULL) return SVRCORE_GetPin(p->alt, tokenName, retry);
00153 
00154     return (NULL);
00155 }
00156 
00157 /*
00158  * VTable
00159  */
00160 static const SVRCOREPinMethods vtable =
00161 { 0, 0, destroyObject, getPin };
00162