simo / rpms / softhsm

Forked from rpms/softhsm 5 years ago
Clone
903844f
/* ***** BEGIN COPYRIGHT BLOCK *****
903844f
 * Copyright (C) 2006 Red Hat, Inc.
903844f
 * All rights reserved.
903844f
 *
903844f
 * This library is free software; you can redistribute it and/or
903844f
 * modify it under the terms of the GNU Lesser General Public
903844f
 * License as published by the Free Software Foundation version
903844f
 * 2.1 of the License.
903844f
 *
903844f
 * This library is distributed in the hope that it will be useful,
903844f
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
903844f
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
903844f
 * Lesser General Public License for more details.
903844f
 *
903844f
 * You should have received a copy of the GNU Lesser General Public
903844f
 * License along with this library; if not, write to the Free Software
903844f
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
903844f
 * ***** END COPYRIGHT BLOCK ***** */
903844f
903844f
#include <stdio.h>
903844f
#include <string.h>
903844f
#include "pkcs11.h"
903844f
#include "pkcs11n.h"
903844f
903844f
/*
903844f
 * windows specific globing search
903844f
 */
903844f
#ifdef WIN32 
903844f
#include <windows.h>
903844f
#include <winver.h>
903844f
#include <winreg.h>
903844f
#include <direct.h>
903844f
#include <shlobj.h>
903844f
903844f
#define PINST_FILE_DATA WIN32_FIND_DATA
903844f
#define PINST_ITERATOR  HANDLE
903844f
#define PINST_FIRST(pattern, data) FindFirstFile(pattern, &data)
903844f
#define PINST_PATH(iter, data)  (data).cFileName
903844f
#define PINST_NEXT(iter, data)  FindNextFile(iter, &data)
903844f
#define PINST_FREE_ITER(iter, data)	FindClose(iter)
903844f
#define PINST_INVALID_ITERATOR INVALID_HANDLE_VALUE
903844f
#define PINST_IS_DIRECTORY(iter, data) \
903844f
	((data).dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
903844f
#define PINST_IS_HIDDEN(iter, data) \
903844f
	((data).dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) 
903844f
#define PINST_FULLPATH(tempPath,path) tempPath
903844f
#define PINST_ERROR DWORD
903844f
#define PINST_NO_MORE ERROR_NO_MORE_FILES
903844f
#define PINST_SET_ERROR(x) SetLastError(x)
903844f
#define PINST_GET_ERROR() GetLastError()
903844f
#define PINST_FS "\\"
903844f
903844f
903844f
/*#define NETSCAPE_KEY "Software\\Netscape\\Netscape Navigator\\Main" */
903844f
#define NETSCAPE_KEY "Software\\Netscape\\Netscape Navigator"
903844f
#define NETSCAPE_SUBKEY_1 "Main"
903844f
#define NETSCAPE_SUBKEY_2 "Install Directory"
903844f
903844f
/* capture the window's error string */
903844f
static void
903844f
winPerror(FILE *outFile, DWORD error, const char *msgString)
903844f
{
903844f
     char buffer[256];
903844f
     char *cp;
903844f
     DWORD ret;
903844f
903844f
     fprintf(outFile,"*** %s: ",msgString);
903844f
     sprintf(buffer,"Format message problem, error = %d (0x%x)\n", error, error);
903844f
     ret=FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, buffer, 
903844f
							sizeof(buffer), NULL);
903844f
     for (cp=buffer; *cp; cp++) {
903844f
	if (*cp == '\r') *cp = ' ';
903844f
     }
903844f
     fprintf(outFile, buffer);
903844f
}
903844f
#endif
903844f
903844f
/*
903844f
 * otherwise we are assuming unix (posix)
903844f
 */
903844f
#ifndef PINST_FILE_DATA
903844f
#define UNIX
903844f
#include <stdlib.h>
903844f
#include <limits.h>
903844f
#include <glob.h>
903844f
#define PINST_FILE_DATA glob_t
903844f
#define PINST_ITERATOR  int
903844f
#define PINST_INVALID_ITERATOR  -1
903844f
#define PINST_FIRST(pattern, data) \
903844f
   ((glob(pattern, GLOB_MARK, NULL, &data) == 0) ? 0 : PINST_INVALID_ITERATOR)
903844f
#define PINST_PATH(iter, data) \
903844f
       (((data).gl_pathv == NULL) ? 0 : (data).gl_pathv[iter] )
903844f
#define PINST_NEXT(iter, data) (((data).gl_pathc > ++iter) ?  iter : 0)
903844f
#define PINST_FREE_ITER(iter, data) globfree(&data)
903844f
#define PINST_IS_DIRECTORY(iter, data) pinst_isdir(PINST_PATH(iter,data))
903844f
#define PINST_IS_HIDDEN(iter, data) (0)
903844f
#define PINST_FULLPATH(tempPath,path) path
903844f
#define PINST_ERROR int
903844f
#define NO_ERROR 0
903844f
#define PINST_NO_MORE NO_ERROR
903844f
#define PINST_SET_ERROR(x)
903844f
#define PINST_GET_ERROR() NO_ERROR
903844f
#define PINST_FS "/"
903844f
903844f
903844f
#define MAX_PATH PATH_MAX 
903844f
903844f
static int
903844f
pinst_isdir(const char *path)
903844f
{
903844f
    int len = strlen(path);
903844f
903844f
903844f
    return (len > 0) && (path[len-1] == '/');
903844f
}
903844f
903844f
#endif
903844f
	
903844f
903844f
typedef enum _InstType {
903844f
    Install,
903844f
    UnInstall,
903844f
} InstType;
903844f
903844f
typedef enum _DirType {
903844f
   AppDataDir = 0,
903844f
   HomeDir,
903844f
   NetscapeInstallDir,
903844f
   MaxDirType,
903844f
} DirType;
903844f
903844f
char *dirPaths[MaxDirType] = { NULL };
903844f
903844f
typedef struct _DirList {
903844f
    DirType dirType;
903844f
    char *search;
903844f
    char *tail;
903844f
} DirList;
903844f
903844f
DirList dirList[] = {
903844f
#ifdef WIN32 
903844f
    { AppDataDir, "Mozilla\\Profiles\\*", "*.slt" },
903844f
    { AppDataDir, "Mozilla\\Firefox\\Profiles\\*", NULL },
903844f
    { AppDataDir, "Thunderbird\\Profiles\\*", NULL },
903844f
    { NetscapeInstallDir, "..\\Users\\*", NULL },
903844f
#endif
903844f
#ifndef MAC 
903844f
#ifdef UNIX
903844f
    { HomeDir, ".mozilla/firefox/*", NULL },
903844f
    { HomeDir, ".mozilla/*", NULL },
903844f
    { HomeDir, ".thunderbird/*", NULL },
903844f
    { HomeDir, ".netscape", NULL },
903844f
#endif
903844f
#endif
903844f
#ifdef MAC 
903844f
903844f
    { HomeDir, "Library/Mozilla/Profiles/*", "*.slt"},
903844f
    { HomeDir, "Library/Application Support/Firefox/Profiles/*", NULL },
903844f
    { HomeDir, "Library/Thunderbird/Profiles/*", NULL },
903844f
903844f
903844f
#endif
903844f
903844f
903844f
};
903844f
903844f
int verbose = 0;
903844f
903844f
int dirListCount = sizeof(dirList)/sizeof(dirList[0]);
903844f
903844f
static void
903844f
usage(char *prog)
903844f
{
903844f
    fprintf(stderr,"usage: %s [-u][-v] [-p path] module\n", prog);
903844f
    return;
903844f
}
903844f
903844f
/* Utility printing functions */
903844f
903844f
903844f
903844f
#define CONFIG_TAG "configDir="
903844f
int
903844f
installPKCS11(char *dirPath, InstType type, char *module)
903844f
{
903844f
    char *paramString = (char *)malloc(strlen(dirPath)+sizeof(CONFIG_TAG)+3);
903844f
    char *cp;
903844f
    char **rc;
903844f
903844f
    if (paramString == NULL) {
903844f
	PINST_SET_ERROR(ERROR_NOT_ENOUGH_MEMORY);
903844f
	return 0;
903844f
    }
903844f
    sprintf(paramString,CONFIG_TAG"\"%s\" ",dirPath);
903844f
903844f
    /* translate all the \'s to /'s */
903844f
    for (cp=paramString; *cp; cp++) {
903844f
	if (*cp == '\\') *cp='/';
903844f
    }
903844f
903844f
    /* don't call this if you have NSS initialized!!, use SECMOD_AddModule
903844f
     * or SECMOD_AddUserModule instead */
903844f
    rc = (char **) NSC_ModuleDBFunc(type == Install ? 
903844f
			SECMOD_MODULE_DB_FUNCTION_ADD :
903844f
			SECMOD_MODULE_DB_FUNCTION_DEL, paramString, module); 
903844f
    if (verbose) {
903844f
	fprintf(stderr, "Install \"%s\" in %s : %s\n", module, dirPath, 
903844f
							rc ? *rc : "Fail" );
903844f
    }
903844f
	
903844f
    free(paramString);
903844f
    return 1;
903844f
}
903844f
903844f
903844f
int
903844f
installAllPKCS11(char *dirPath, char *search, char *tail,
903844f
					InstType type, char *module)
903844f
{
903844f
    char *searchString;
903844f
    unsigned long searchStringLen;
903844f
    int len;
903844f
    char *tempPath, *fileStart;
903844f
    PINST_FILE_DATA fileData;
903844f
    PINST_ITERATOR iter;
903844f
    PINST_ERROR err = NO_ERROR;
903844f
903844f
    char *myPath = NULL;
903844f
903844f
    searchString = (char *)malloc(strlen(dirPath)+2+strlen(search));
903844f
903844f
    if (searchString == NULL) {
903844f
	PINST_SET_ERROR(ERROR_NOT_ENOUGH_MEMORY);
903844f
	return 0;
903844f
    }
903844f
    sprintf(searchString,"%s" PINST_FS "%s",dirPath,search);
903844f
903844f
    searchStringLen = strlen(searchString);
903844f
    tempPath=malloc(searchStringLen+MAX_PATH+1);
903844f
    if (tempPath == NULL) {
903844f
	free(searchString);
903844f
	PINST_SET_ERROR(ERROR_NOT_ENOUGH_MEMORY);
903844f
	return 0;
903844f
    }
903844f
    strcpy(tempPath, searchString);
903844f
    fileStart = strrchr(tempPath, *PINST_FS);
903844f
    if (fileStart == NULL) {
903844f
	tempPath[searchStringLen] = *PINST_FS;
903844f
	fileStart = &tempPath[searchStringLen];
903844f
    }
903844f
    fileStart++;
903844f
903844f
    iter = PINST_FIRST(searchString, fileData);
903844f
    free(searchString);
903844f
    if (iter == PINST_INVALID_ITERATOR) {
903844f
	/* error set by PINST_FIRST */
903844f
	free(tempPath);
903844f
	return 0;
903844f
    }
903844f
903844f
    len=1;
903844f
903844f
    do {
903844f
	char *path = PINST_PATH(iter, fileData);
903844f
        if(!path)
903844f
        {
903844f
            break;
903844f
        }
903844f
903844f
	if (!PINST_IS_DIRECTORY(iter, fileData)) {
903844f
	    continue;
903844f
	}
903844f
	if (PINST_IS_HIDDEN(iter, fileData)) {
903844f
	    continue;
903844f
	}
903844f
 	/* skip . and .. */
903844f
	if ((path[0] == '.') && ((path[1] == 0) || 
903844f
				 (path[1] == '.' && path[2] == 0)) ) {
903844f
	    continue;
903844f
	}
903844f
	strcpy(fileStart,path);
903844f
903844f
	myPath=PINST_FULLPATH(tempPath,path);
903844f
	if (tail) {
903844f
	    installAllPKCS11(myPath, tail, NULL, type, module);
903844f
	} else {
903844f
	    installPKCS11(myPath, type, module);
903844f
	}
903844f
    } while (PINST_NEXT(iter, fileData));
903844f
    free(tempPath);
903844f
903844f
    err = PINST_GET_ERROR();
903844f
    PINST_FREE_ITER(iter,fileData);
903844f
903844f
    if (err != PINST_NO_MORE) {
903844f
	/* restore the previous error (in case FindClose trashes it) */
903844f
	PINST_SET_ERROR(err);
903844f
	return 0;
903844f
    }
903844f
    return 1;
903844f
}
903844f
	
903844f
int main(int argc, char **argv)
903844f
{
903844f
    char *module = NULL;
903844f
    char *prog = *argv++;
903844f
    char *cp;
903844f
    int argCount = 0;
903844f
    int i;
903844f
    InstType type = Install;
903844f
    char * path = NULL;
903844f
#ifdef WIN32
903844f
    BOOL brc;
903844f
    HKEY regKey;
903844f
    unsigned long lrc;
903844f
    TCHAR appData[MAX_PATH];
903844f
    char netscapeInstall[MAX_PATH];
903844f
    unsigned long nsInstallSize = MAX_PATH;
903844f
#endif
903844f
903844f
    /*
903844f
     * parse the arglist;
903844f
     */
903844f
    while ((cp = *argv++) != 0) {
903844f
	if (*cp == '-') {
903844f
	    while (*++cp) switch (*cp) {
903844f
	    case 'i':
903844f
		type = Install;
903844f
		break;
903844f
	    case 'u':
903844f
		type = UnInstall;
903844f
		break;
903844f
	    case 'v':
903844f
		verbose = 1;
903844f
		break;
903844f
	    case 'p':
903844f
		path = *argv++;
903844f
		if (path == NULL) {
903844f
		    usage(prog);
903844f
		    return 2;
903844f
		}
903844f
		break;
903844f
	    default:
903844f
		usage(prog);
903844f
		return 2;
903844f
	    }
903844f
	} else switch (argCount++) {
903844f
	case 0:
903844f
	    module = cp;
903844f
	    break;
903844f
	default:
903844f
	    usage(prog);
903844f
	    return 2;
903844f
	}
903844f
    }
903844f
903844f
    if (module == NULL) {
903844f
	usage(prog);
903844f
    }
903844f
903844f
    if (path) {
903844f
	installAllPKCS11(path, "", NULL, type, module);
903844f
	return 0;
903844f
    }
903844f
903844f
#ifdef WIN32 
903844f
    /* App Data Dir */
903844f
    brc = SHGetSpecialFolderPath(NULL, appData, CSIDL_APPDATA, FALSE);
903844f
    if (brc) {
903844f
	dirPaths[AppDataDir] = appData;
903844f
    } else {
903844f
	if (verbose) {
903844f
	    winPerror(stderr, GetLastError(), "Reading App Directory");
903844f
	}
903844f
    }
903844f
903844f
    /* Netscape Install Dir */
903844f
    lrc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETSCAPE_KEY, 0, 
903844f
					KEY_ENUMERATE_SUB_KEYS, &regKey);
903844f
    if (lrc == ERROR_SUCCESS) {
903844f
	int i = 0;
903844f
	TCHAR productName[255];
903844f
	HKEY prodKey;
903844f
	HKEY mainKey;
903844f
903844f
	while ((lrc = RegEnumKey(regKey, i, productName, sizeof(productName)))
903844f
							 == ERROR_SUCCESS) {
903844f
	    i++;
903844f
	    lrc = RegOpenKeyEx(regKey, productName, 0, 
903844f
					KEY_ENUMERATE_SUB_KEYS, &prodKey);
903844f
	    if (lrc != ERROR_SUCCESS) {
903844f
		if (verbose) {
903844f
		    winPerror(stderr, GetLastError(), 
903844f
					"Reading Netscape 4.0 prodkey");
903844f
		    fprintf(stderr,"Product = %s\n",productName);
903844f
		}
903844f
		continue;
903844f
	    }
903844f
	    lrc = RegOpenKeyEx(prodKey, NETSCAPE_SUBKEY_1, 0, 
903844f
						KEY_QUERY_VALUE, &mainKey);
903844f
	    if (lrc != ERROR_SUCCESS) {
903844f
	        RegCloseKey(prodKey);
903844f
		continue;
903844f
	    }
903844f
	    /* open main */
903844f
	    lrc = RegQueryValueEx(mainKey, NETSCAPE_SUBKEY_2, NULL, NULL, 
903844f
					netscapeInstall, &nsInstallSize);
903844f
	    RegCloseKey(mainKey);
903844f
	    RegCloseKey(prodKey);
903844f
	    if (lrc == ERROR_SUCCESS)  {
903844f
		if (netscapeInstall[nsInstallSize-1] == 0) {
903844f
		    if (verbose) {
903844f
		        fprintf(stderr, 
903844f
		   	   "Found Netscape 4.0 Install directory\n");
903844f
		    }
903844f
		    dirPaths[NetscapeInstallDir] = netscapeInstall;
903844f
		    break;
903844f
		} else {
903844f
		    fprintf(stderr, 
903844f
			"Reading Netscape 4.0 key: Value too large\n");
903844f
		}
903844f
	    } else {
903844f
		if (verbose) {
903844f
		    winPerror(stderr, lrc, "Reading Netscape 4.0 key");
903844f
		}
903844f
	   }
903844f
	}
903844f
	if ((lrc != ERROR_SUCCESS) && (lrc != ERROR_NO_MORE_ITEMS)) {
903844f
	    winPerror(stderr, lrc, "EnumKey on Netscape Registry Key failed");
903844f
	}
903844f
    } else {
903844f
	if (verbose) {
903844f
	    winPerror(stderr, lrc, "Openning Netscape 4.0 key");
903844f
	}
903844f
    }
903844f
#endif
903844f
#ifdef UNIX
903844f
    dirPaths[HomeDir] = getenv("HOME");
903844f
#endif
903844f
903844f
    /* OK, now search the directories and complete the Install */
903844f
    for (i=0; i < dirListCount; i++) {
903844f
	char *dirPath = dirPaths[dirList[i].dirType];
903844f
	if (!dirPath) {
903844f
	    continue;
903844f
	}
903844f
	installAllPKCS11(dirPath, dirList[i].search, dirList[i].tail, 
903844f
								type, module);
903844f
    }
903844f
903844f
    return 0;
903844f
}