📄 main.c
字号:
/* At which port is the card terminal? */static u16 port = 0; /* default: COM1 *//* Options for getopt_long(3) */static struct option long_options[] = { {"backup", required_argument, NULL, 0}, {"change-cardkey", no_argument, (int *)&change_cardkey_flg, 1}, {"cipher", required_argument, NULL, 0}, {"format-card", no_argument, (int *)&format_card_flg, 1}, {"help", no_argument, NULL, 0 }, {"list-password", required_argument, NULL, 0}, {"port", required_argument, NULL, 0}, {"save-password", no_argument, (int *)&save_password_flg, 1}, {"security", required_argument, NULL, 0}, {"remove-password", required_argument, NULL, 0}, {"restore", required_argument, NULL, 0}, {"verbose", no_argument, (int *)&verbose_flg, 1}, {"version", no_argument, NULL, 0}, {0, 0, 0, 0}};/****************************************************************************** * * Function : main * * Description : This function is the main body of the program. It will check * program options, checks whether they are valid, and will call * the appropriate sub-routines. * * Input : [1] argc (int) * Number of commandline arguments. * [2] argv (char) * Array containing commandline options. * * Return : POC_ERROR or POC_SUCCESS * *****************************************************************************/int main(int argc, char **argv) { /* used by getopt_long */ int ch; int option_index; /* Set progname. */ progname = argv[0]; card_crypt_ctx.cipher = NULL; card_crypt_ctx.sec_level = 0; /* Get command line arguments. */ while ((ch = getopt_long(argc, argv, "cfhl:p:sr:vV", long_options, &option_index)) != -1) switch (ch) { case 0: /* * Check long options. */ /* --backup: backup card */ if (strcmp(long_options[option_index].name, "backup") == 0) { strncpy(file, optarg, FILENAME_MAX); backup_card_flg = 1; break; } else /* --cipher: cipher to use */ if (strcmp(long_options[option_index].name, "cipher") == 0) { if ( (card_crypt_ctx.cipher = get_name(optarg, CIPHERS)) == NULL) { print_err(STR_WRONG_CIPHER); exit(1); } break; } else /* --help: output help message */ if (strcmp(long_options[option_index].name, "help") == 0) { usage(argv[0]); break; } else /* --list-password: list password which has the given descr. */ if (strcmp(long_options[option_index].name, "list-password") == 0) { strncpy(description, optarg, DESCR_LENGTH); list_password_flg = 1; break; } else /* --remove-password: remove password which has the given descr. */ if (strcmp(long_options[option_index].name, "remove-password") == 0) { strncpy(description, optarg, DESCR_LENGTH); remove_password_flg = 1; break; } else /* --restore: restore a backuped card. */ if (strcmp(long_options[option_index].name, "restore") == 0) { strncpy(file, optarg, FILENAME_MAX); restore_card_flg = 1; break; } else /* --security: which security-level to use */ if (strcmp(long_options[option_index].name, "security") == 0) { if (atoi(optarg) != SL_NORMAL && atoi(optarg) != SL_HIGH) { print_err(STR_WRONG_SEC_LEVEL); exit(1); } card_crypt_ctx.sec_level = atoi(optarg); break; } else /* --verbose: verbose output */ if (strcmp(long_options[option_index].name, "verbose") == 0) { verbose_flg = 1; break; } else /* --version: output program's version */ if (strcmp(long_options[option_index].name, "version") == 0) { print_version(); exit(0); break; } else /* --port: at which comport we can find the terminal. */ if (strcmp(long_options[option_index].name, "port") == 0) { port = atoi(optarg) - 1; break; } break; /* Short options ... */ case 'c': /* -c: change card's encryption key */ change_cardkey_flg = 1; break; case 'f': /* -f: format card */ format_card_flg = 1; break; case 'h': /* -h: help message */ usage(argv[0]); break; case 'l': /* -l: list password */ strncpy(description, optarg, DESCR_LENGTH); list_password_flg = 1; break; case 'p': /* -p: com port */ port = atoi(optarg) - 1; break; case 's': /* -s: save password */ save_password_flg = 1; break; case 'r': /* -r: remove password */ strncpy(description, optarg, DESCR_LENGTH); remove_password_flg = 1; break; case 'v': /* -v: be verbose */ verbose_flg = 1; break; case 'V': /* -V: print version information */ print_version(); exit(0); break; }#ifdef DEBUG /* If compiled with DEBUG we always print more information. */ verbose_flg = 1;#endif /* Check whether 'poc' was executed without a COMMAND (see help message for possible COMMANDS. */ if (!backup_card_flg && !change_cardkey_flg && !format_card_flg && !list_password_flg && !save_password_flg && !remove_password_flg && !restore_card_flg ) { print_err(STR_NO_COMMAND); return(POC_ERROR); } /* Multiple COMMANDS are not allowed. */ if ( (backup_card_flg + change_cardkey_flg + format_card_flg + list_password_flg + save_password_flg + remove_password_flg + restore_card_flg) > 1) { print_err(STR_NO_MULTIPLE_COMMANDS); return(POC_ERROR); } /* Check cipher/security-level settings and set to defaults if nothing was specified. */ check_crypto_settings(&card_crypt_ctx); /* * Check which flags are set and execute the appropiate functions. * * Depending on the return of each function we will output an error or * a success message/output a searched password and exit. */ /* Backup a card. */ if (backup_card_flg) { if (backup_create(file, port) != POC_SUCCESS) { print_err(STR_BACKUP_FAIL); return(POC_ERROR); } else { printf(STR_BACKUP_SUCCESS); return(POC_SUCCESS); } } /* Change the encryption key. */ if (change_cardkey_flg) { if (change_card_key(&card_crypt_ctx, port) != POC_SUCCESS) { print_err(STR_CHANGE_KEY_FAIL); return(POC_ERROR); } else { printf(STR_CHANGE_KEY_SUCCESS); return(POC_SUCCESS); } } /* Format a card. */ if (format_card_flg) { if (format_card(port) != POC_SUCCESS) { print_err(STR_FORMAT_FAIL); return(POC_ERROR); } else { printf(STR_FORMAT_SUCCESS); return(POC_SUCCESS); } } /* Print a stored password. */ if (list_password_flg) { if (password_list(&card_crypt_ctx, port, description) != POC_SUCCESS) { print_err(STR_LIST_PASS_FAIL); return(POC_ERROR); } return(POC_SUCCESS); } /* Save a password. */ if (save_password_flg) { if (password_save(&card_crypt_ctx, port) != POC_SUCCESS) { print_err(STR_SAVE_PASS_FAIL); return(POC_ERROR); } else { printf(STR_SAVE_PASS_SUCCESS); return(POC_SUCCESS); } } /* Remove a password. */ if (remove_password_flg) { if (password_remove(&card_crypt_ctx, port, description) != POC_SUCCESS) { print_err(STR_REM_PASS_FAIL); return(POC_ERROR); } else { printf(STR_REM_PASS_SUCCESS); return(POC_SUCCESS); } } /* Restore a backuped card. */ if (restore_card_flg) { if (backup_restore(file, port) != POC_SUCCESS) { print_err(STR_RESTORE_FAIL); return(POC_ERROR); } else { printf(STR_RESTORE_SUCCESS); return(POC_SUCCESS); } } return(POC_SUCCESS);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -