Пример #1
0
void bank_process_local_command(Bank *bank, char *command, size_t len)
{
    static char create_user[] = "create-user";
    static char deposit[] = "deposit";
    static char balance_command[] = "balance";
    static char invalid[] = "Invalid command";
    static char create_user_error[] = "Usage: create-user <user-name> <pin> <balance>";
    static char error_file[] = "Error creating card file for user";
    static char balance_error[] = "Usage: balance <user-name>";
    static char deposit_error[] = "Usage: deposit <user-name> <amt>";
    static char too_rich_error[] = "Too rich for this program";

    const char space[] = " ";
    char user_name[1024];
    char file_name[1024];
    char *token;
    strtok(command, "\n");
    token = strtok(command, space);

    /*
    while( token != NULL ) {
        printf( "%s\n", token );
    
        token = strtok(NULL, space);
    }
    */

    if (strcmp(token, create_user) == 0) {
        char pin[1024];
 	    char balbuf[1024];
        unsigned int balance;

        // get user name
        token = strtok(NULL, space);

        
        if (token != NULL && user_name_check(token)) {
            
            strcpy(user_name, token);
            
            // get user pin number
            token = strtok(NULL, space);

            
            if (token != NULL && pin_check(token)) {
                strcpy(pin, token);
                // get user balance
                token = strtok(NULL, space);
                
                if (token != NULL && deposit_check(token)) {

                    if (too_rich_check(token)) {
                        printf("%s\n", create_user_error);
                        return;
                    }

                    char *ptr;
                    balance = strtoul(token, &ptr, 10);
                    // printf("balance is: %d\n", balance);
                    token = strtok(NULL, space);

                    if (token != NULL) {
                        printf("%s\n", create_user_error);
                    } else {
                    
                        char *key;
                        key = (char *) malloc(sizeof(user_name) * sizeof(char));

                        strcpy(key, user_name);
                        if (list_find(bank->users, key) == NULL) {

                            unsigned int *balance_pt;
                            balance_pt = (unsigned int *) malloc(sizeof(unsigned int));
                            *balance_pt = balance;

                            list_add(bank->users, key, balance_pt);
                            
                            // create card file name
                            strcpy(file_name, user_name);
                            strcat(file_name, ".card");
                            
                            // write pin number and balance (with an empty space in between) into the file
			                char call[1024];
                            char buffer[1024];
			                sprintf(buffer, "%s %s", user_name, pin);

		                    //To encrypt and make system call to create .card file
			                sprintf(call,"echo %s |openssl enc -aes-256-cbc -e -a -pass file:%s -salt -out %s",buffer,bank->symm_key,file_name);
			                if (system(call) == -1)
                                printf("Error creating card file for user %s\n", user_name);
			                else {
    			                //flush call to get 
    			                memset(call,0x00,strlen(call));
                                printf("Created user %s\n", user_name);
                            }
                                                    
                        } else {
                            free(key);
                            printf("%s ", "Error: user");
                            printf("%s ", user_name);
                            printf("%s\n", "already exists");
                        }

                        // list_print(bank->users);
                    }
                } else 
                    printf("%s\n", create_user_error);
            } else 
                printf("%s\n", create_user_error); 
            

        } else
            printf("%s\n", create_user_error); 

            
    } else if (strcmp(token, deposit) == 0) {
        token = strtok(NULL, space);
        if (token != NULL && user_name_check(token)) {
            strcpy(user_name, token);
            if (list_find(bank->users, user_name) == NULL) 
                printf("%s\n", "No such user");
            else {
                token = strtok(NULL, space);
                if (token == NULL || !deposit_check(token))
                    printf("%s\n", deposit_error);
                else {
                    char *ptr;
                    unsigned int deposit = strtoul(token, &ptr, 10);
                    if (strtok(NULL, space) != NULL)
                        printf("%s\n", deposit_error);
                    else {
                        unsigned int *cur_b = list_find(bank->users, user_name);
                        unsigned int new_b = *cur_b + deposit;
                        if (new_b < *cur_b || new_b < deposit) {
                            printf("%s\n", too_rich_error);
                            return;
                        }
                        *cur_b = new_b;

                        printf("$%u %s %s's account\n", deposit, "added to", user_name);



                    }
                }
            }
        } else
            printf("%s\n", deposit_error);
    } else if (strcmp(token, balance_command) == 0) {
        // char user_name[1024];
        token = strtok(NULL, space);
        if (token != NULL && user_name_check(token)) {
            strcpy(user_name, token);

            if (strtok(NULL, space) != NULL) {
                printf("%s\n", balance_error);
            } else {

                if (list_find(bank->users, user_name) == NULL) {
                    printf("%s\n", "No such user");
                } else { 
                    unsigned int *b = list_find(bank->users, user_name);
                    printf("$%u\n", *b);
                    
                }
            }
        } else
            printf("%s\n", balance_error);
    } else {
        printf("%s\n", invalid);
    }
}
Пример #2
0
static PRESULT conaxpin_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8	unact,input,save;
	UINT8	id = OSD_GetObjID(pObj);

	switch(event)
	{		
	case EVN_FOCUS_PRE_GET:
		break;
	case EVN_FOCUS_PRE_LOSE:
		break;
	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16) ;
		if(unact <= VACT_NUM_9)
		{
			win_ca_input_number(id-ENTER_PIN_ID,(UINT8)(unact-VACT_NUM_0));
			win_ca_set_password_str(id-ENTER_PIN_ID);
			OSD_TrackObject(pObj,C_UPDATE_ALL);
		}
		else if(unact == VACT_ENTER)
		{
			if((g_ca_Input_Status==TRUE)&&(g_ca_Input_Bit_Number==4))
			{
				if(id == ENTER_NEWPIN_ID)		// step3. enter new pin
				{
					win_capin_switch_confirm(2, TRUE);
					g_ca_Input_Bit_Number=0;
					g_ca_Input_Status=FALSE;
					MEMSET(win_ca_password_value[2], 0, 4);
					win_ca_set_password_str(2);
					OSD_ChangeFocus((POBJECT_HEAD)&g_win_ca_pin,CONFIRM_NEWPIN_ID,C_UPDATE_ALL);
					OSD_DrawObject((POBJECT_HEAD)&capin_item_con2, C_UPDATE_ALL);
					OSD_TrackObject((POBJECT_HEAD)&capin_item_con3,C_UPDATE_ALL);
				}
				else if(id == ENTER_PIN_ID)	// step2. enter old pin
				{
					win_capin_switch_confirm(1, TRUE);
					MEMSET(win_ca_password_value[1], 0, 4);
					MEMSET(win_ca_password_value[2], 0, 4);
					OSD_ChangeFocus((POBJECT_HEAD)&g_win_ca_pin,ENTER_NEWPIN_ID,C_UPDATE_ALL);
					OSD_DrawObject((POBJECT_HEAD)&capin_item_con1, C_UPDATE_ALL);
					OSD_TrackObject((POBJECT_HEAD)&capin_item_con2,C_UPDATE_ALL);
					g_ca_Input_Bit_Number=0;
				}
				else		// id==CONFIRM_NEWPIN_ID
				{
					// step4. confirm new pin
					
					// step5. new pin comparison ok?
					if(MEMCMP(win_ca_password_value[1], win_ca_password_value[2], 4) != 0)
					{
						// step6a. pin confirmation does not match
						win_compopup_init(WIN_POPUP_TYPE_SMSG);
						win_compopup_set_msg(NULL,NULL,RS_CONAX_PIN_NOT_MATCH);
						win_compopup_open_ext(&save);
						osal_task_sleep(2000);
						win_compopup_smsg_restoreback();
						goto RESTART;
					}

					// step6b. change pin on card
					
					// step8. sw=wrong pin?
					if(pin_check(win_ca_password_value[0], 0)!=0)
					{
						//step9a. incorrect old pin
						win_compopup_init(WIN_POPUP_TYPE_SMSG);
						win_compopup_set_msg(NULL,NULL,RS_CONAX_INCORRECT_OLD_PIN);
						win_compopup_open_ext(&save);
						osal_task_sleep(2000);
						win_compopup_smsg_restoreback();
						goto RESTART;
					}

					// step7. change pin ok?
					if(conax_pin_change(win_ca_password_value[0], win_ca_password_value[1]) == 1)
					{
						win_compopup_init(WIN_POPUP_TYPE_SMSG);
						win_compopup_set_msg(NULL,NULL,RS_CONAX_BEEN_CHANGED);
						win_compopup_open_ext(&save);
						osal_task_sleep(2000);
						win_compopup_smsg_restoreback();
						return PROC_LEAVE;
					}
					else
					{
						// step9b. change ca pin failed
						win_compopup_init(WIN_POPUP_TYPE_SMSG);
						win_compopup_set_msg(NULL,NULL,RS_CONAX_CHANGE_PIN_FAILED);
						win_compopup_open_ext(&save);
						osal_task_sleep(2000);
						win_compopup_smsg_restoreback();
					}

RESTART:
					g_ca_Input_Bit_Number=0;
					g_ca_Input_Status=FALSE;
					MEMSET(win_ca_password_value[0], 0, 4);
					MEMSET(win_ca_password_value[1], 0, 4);
					MEMSET(win_ca_password_value[2], 0, 4);
					win_ca_set_password_str(0);
					win_ca_set_password_str(1);
					win_ca_set_password_str(2);
					win_capin_switch_confirm(1, FALSE);
					OSD_ChangeFocus((POBJECT_HEAD)&g_win_ca_pin,ENTER_PIN_ID,C_UPDATE_ALL);
					OSD_TrackObject((POBJECT_HEAD)&capin_item_con1,C_UPDATE_ALL);
					OSD_DrawObject((POBJECT_HEAD)&capin_item_con2, C_UPDATE_ALL);
					OSD_DrawObject((POBJECT_HEAD)&capin_item_con3, C_UPDATE_ALL);
				}
			}
		}
		else if(unact==VACT_DECREASE)
		{
			if(g_ca_Input_Bit_Number>0)
			{
				g_ca_Input_Bit_Number--;
				win_ca_set_password_str(id-ENTER_PIN_ID);
				OSD_TrackObject(pObj,C_UPDATE_ALL);
			}
		}
		ret = PROC_LOOP;
		break;
	}
	return ret;
}