Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 1 | #include <errno.h> |
| 2 | #include <unistd.h> |
| 3 | |
| 4 | #include <osmocom/core/select.h> |
| 5 | |
| 6 | #include "client.h" |
| 7 | |
| 8 | /* This is a remsim-client with an interactive 'shell', where the user |
| 9 | * can type in C-APDUs in hex formats, which will be sent to the bankd / |
| 10 | * SIM-card. Responses received from SIM Card via bankd will be printed |
| 11 | * in return. */ |
| 12 | |
| 13 | |
| 14 | /*********************************************************************** |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 15 | * stdin frontend code to remsim-client |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 16 | ***********************************************************************/ |
| 17 | |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 18 | int frontend_request_card_insert(struct bankd_client *bc) |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 19 | { |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 20 | return 0; |
| 21 | } |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 22 | |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 23 | int frontend_request_sim_remote(struct bankd_client *bc) |
| 24 | { |
| 25 | return 0; |
| 26 | } |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 27 | |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 28 | int frontend_request_modem_reset(struct bankd_client *bc) |
| 29 | { |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | int frontend_handle_card2modem(struct bankd_client *bc, const uint8_t *data, size_t len) |
| 34 | { |
| 35 | OSMO_ASSERT(data); |
| 36 | printf("R-APDU: %s\n", osmo_hexdump(data, len)); |
Harald Welte | 8236840 | 2020-02-18 22:59:37 +0100 | [diff] [blame] | 37 | fflush(stdout); |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 42 | int frontend_handle_set_atr(struct bankd_client *bc, const uint8_t *data, size_t len) |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 43 | { |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 44 | OSMO_ASSERT(data); |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 45 | |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 46 | printf("SET_ATR: %s\n", osmo_hexdump(data, len)); |
Harald Welte | 8236840 | 2020-02-18 22:59:37 +0100 | [diff] [blame] | 47 | fflush(stdout); |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 48 | |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 49 | return 0; |
| 50 | } |
| 51 | |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 52 | int frontend_handle_slot_status(struct bankd_client *bc, const SlotPhysStatus_t *sts) |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 53 | { |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 57 | int frontend_append_script_env(struct bankd_client *bc, char **env, size_t max_env) |
| 58 | { |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 63 | /*********************************************************************** |
| 64 | * Incoming command from the user application (stdin shell in our case) |
| 65 | ***********************************************************************/ |
| 66 | |
| 67 | struct stdin_state { |
| 68 | struct osmo_fd ofd; |
| 69 | struct msgb *rx_msg; |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 70 | struct bankd_client *bc; |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | /* called every time a command on stdin was received */ |
| 74 | static void handle_stdin_command(struct stdin_state *ss, char *cmd) |
| 75 | { |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 76 | struct bankd_client *bc = ss->bc; |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 77 | RsproPDU_t *pdu; |
| 78 | BankSlot_t bslot; |
| 79 | uint8_t buf[1024]; |
| 80 | int rc; |
| 81 | |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 82 | bank_slot2rspro(&bslot, &bc->bankd_slot); |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 83 | |
| 84 | OSMO_ASSERT(ss->rx_msg); |
| 85 | |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 86 | if (!strcasecmp(cmd, "RESET")) { |
| 87 | /* reset the [remote] card */ |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 88 | pdu = rspro_gen_ClientSlotStatusInd(bc->srv_conn.clslot, &bslot, |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 89 | true, false, false, true); |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 90 | server_conn_send_rspro(&bc->bankd_conn, pdu); |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 91 | } else { |
| 92 | /* we assume the user has entered a C-APDU as hex string. parse + send */ |
| 93 | rc = osmo_hexparse(cmd, buf, sizeof(buf)); |
| 94 | if (rc < 0) { |
| 95 | fprintf(stderr, "ERROR parsing C-APDU `%s'!\n", cmd); |
| 96 | return; |
| 97 | } |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 98 | if (!bc->srv_conn.clslot) { |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 99 | fprintf(stderr, "Cannot send command; no client slot\n"); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | /* Send CMD APDU to [remote] card */ |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 104 | pdu = rspro_gen_TpduModem2Card(bc->srv_conn.clslot, &bslot, buf, rc); |
| 105 | server_conn_send_rspro(&bc->bankd_conn, pdu); |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | /* call-back function for stdin read. Gather bytes in buffer until CR/LF received */ |
| 110 | static int stdin_fd_cb(struct osmo_fd *ofd, unsigned int what) |
| 111 | { |
| 112 | struct stdin_state *ss = ofd->data; |
| 113 | char *cur; |
| 114 | int rc, i; |
| 115 | |
| 116 | OSMO_ASSERT(what & OSMO_FD_READ); |
| 117 | |
| 118 | if (!ss->rx_msg) { |
| 119 | ss->rx_msg = msgb_alloc(1024, "stdin"); |
| 120 | OSMO_ASSERT(ss->rx_msg); |
| 121 | } |
| 122 | |
| 123 | cur = (char *) ss->rx_msg->tail; |
| 124 | rc = read(ofd->fd, cur, msgb_tailroom(ss->rx_msg)); |
| 125 | if (rc < 0) |
| 126 | return rc; |
| 127 | msgb_put(ss->rx_msg, rc); |
| 128 | |
| 129 | for (i = 0; i < rc; i++) { |
| 130 | if (cur[i] == '\r' || cur[i] == '\n') { |
| 131 | cur[i] = '\0'; |
| 132 | /* dispatch the command */ |
| 133 | handle_stdin_command(ss, cur); |
| 134 | /* FIXME: possibly other commands */ |
| 135 | msgb_free(ss->rx_msg); |
| 136 | ss->rx_msg = NULL; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | |
| 145 | /* main function */ |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 146 | int client_user_main(struct bankd_client *bc) |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 147 | { |
| 148 | struct stdin_state ss; |
| 149 | |
| 150 | /* register stdin file descriptor with osmocom select loop abstraction */ |
| 151 | memset(&ss, 0, sizeof(ss)); |
| 152 | osmo_fd_setup(&ss.ofd, fileno(stdin), OSMO_FD_READ, &stdin_fd_cb, &ss, 0); |
| 153 | osmo_fd_register(&ss.ofd); |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 154 | ss.bc = bc; |
Harald Welte | 1200c82 | 2020-02-13 20:43:27 +0100 | [diff] [blame] | 155 | |
| 156 | while (1) { |
| 157 | osmo_select_main(0); |
| 158 | } |
| 159 | } |