예제 #1
0
파일: pk11.c 프로젝트: emaldona/nss
Error
LoadMechanismList(void)
{
    int i;

    if (pk11_DefaultArray == NULL) {
        pk11_DefaultArray = PK11_GetDefaultArray(&pk11_DefaultArraySize);
        if (pk11_DefaultArray == NULL) {
            /* should assert. This shouldn't happen */
            return UNSPECIFIED_ERR;
        }
    }
    if (mechanismStrings != NULL) {
        return SUCCESS;
    }

    /* build the mechanismStrings array */
    mechanismStrings = PORT_NewArray(MaskString, pk11_DefaultArraySize);
    if (mechanismStrings == NULL) {
        return OUT_OF_MEM_ERR;
    }
    numMechanismStrings = pk11_DefaultArraySize;
    for (i = 0; i < numMechanismStrings; i++) {
        const char *name = pk11_DefaultArray[i].name;
        unsigned long flag = pk11_DefaultArray[i].flag;
        /* map new name to old */
        switch (flag) {
            case SECMOD_FORTEZZA_FLAG:
                name = "FORTEZZA";
                break;
            case SECMOD_SHA1_FLAG:
                name = "SHA1";
                break;
            case SECMOD_CAMELLIA_FLAG:
                name = "CAMELLIA";
                break;
            case SECMOD_RANDOM_FLAG:
                name = "RANDOM";
                break;
            case SECMOD_FRIENDLY_FLAG:
                name = "FRIENDLY";
                break;
            default:
                break;
        }
        mechanismStrings[i].name = name;
        mechanismStrings[i].mask = SECMOD_InternaltoPubMechFlags(flag);
    }
    return SUCCESS;
}
/************************************************************************
 *
 * U n s e t D e f a u l t M o d u l e
 */
Error
UnsetDefaultModule(char *moduleName, char *slotName, char *mechanisms)
{
    SECMODModule * module = NULL;
    PK11SlotInfo *slot;
    int s, i;
    unsigned long mechFlags = getFlagsFromString(mechanisms,
	mechanismStrings, numMechanismStrings);
    PRBool found = PR_FALSE;
    Error rv;

    if (pk11_DefaultArray == NULL) {
	pk11_DefaultArray = PK11_GetDefaultArray(&pk11_DefaultArraySize);
	if (pk11_DefaultArray == NULL) {
	    /* should assert. This shouldn't happen */
	    rv = UNSPECIFIED_ERR;
            goto loser;
	}
    }

    mechFlags =  SECMOD_PubMechFlagstoInternal(mechFlags);

    module = SECMOD_FindModule(moduleName);
    if(!module) {
	PR_fprintf(PR_STDERR, errStrings[NO_SUCH_MODULE_ERR], moduleName);
	rv = NO_SUCH_MODULE_ERR;
        goto loser;
    }

    for(s=0; s < module->slotCount; s++) {
	slot = module->slots[s];
	if ((slotName != NULL) &&
	    !((strcmp(PK11_GetSlotName(slot),slotName) == 0) ||
	    (strcmp(PK11_GetTokenName(slot),slotName) == 0)) ) {
	    /* we are only interested in changing the one slot */
	    continue;
	}
	for(i=0; i < pk11_DefaultArraySize ; i++) {
	    if(pk11_DefaultArray[i].flag & mechFlags) {
		PK11_UpdateSlotAttribute(slot, &(pk11_DefaultArray[i]),
		    PR_FALSE);
	    }
	}
    }
    if (slotName && !found) {
	PR_fprintf(PR_STDERR, errStrings[NO_SUCH_SLOT_ERR], slotName);
	rv = NO_SUCH_SLOT_ERR;
        goto loser;
    }

    /* Delete and re-add module to save changes */
    if( SECMOD_UpdateModule(module) != SECSuccess ) {
	PR_fprintf(PR_STDERR, errStrings[UNDEFAULT_FAILED_ERR],
	  moduleName);
	rv = UNDEFAULT_FAILED_ERR;
        goto loser;
    }

    PR_fprintf(PR_STDOUT, msgStrings[UNDEFAULT_SUCCESS_MSG]);
    rv = SUCCESS;
loser:
    if (module) {
        SECMOD_DestroyModule(module);
    }
    return rv;
}