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