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