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