static gint update_tokencode(gpointer data) { time_t now = time(NULL); struct tm *tm; char str[128], *formatted; tm = gmtime(&now); if ((tm->tm_sec >= 30 && last_sec < 30) || (tm->tm_sec < 30 && last_sec >= 30) || last_sec == -1) { last_sec = tm->tm_sec; securid_compute_tokencode(current_token, now, tokencode_str); securid_compute_tokencode(current_token, now + token_interval, next_tokencode_str); } token_sec = token_interval - (tm->tm_sec % token_interval) - 1; gtk_widget_queue_draw(GTK_WIDGET(progress_bar)); formatted = stoken_format_tokencode(tokencode_str); if (!formatted) die("out of memory\n"); snprintf(str, sizeof(str), "<span size=\"xx-large\" weight=\"bold\">%s</span>", formatted); gtk_label_set_markup(GTK_LABEL(tokencode_text), str); free(formatted); if (next_tokencode_text) { formatted = stoken_format_tokencode(next_tokencode_str); if (!formatted) die("out of memory\n"); gtk_label_set_text(GTK_LABEL(next_tokencode_text), formatted); free(formatted); } return TRUE; }
static gint update_tokencode(gpointer data) { time_t now = time(NULL); struct tm *tm; int sec, i, j, code_len; char str[16]; tm = gmtime(&now); if ((tm->tm_sec >= 30 && last_sec < 30) || (tm->tm_sec < 30 && last_sec >= 30) || last_sec == -1) { last_sec = tm->tm_sec; securid_compute_tokencode(current_token, now, tokencode_str); } sec = interval - (tm->tm_sec % interval) - 1; /* inject a space in the middle of the code, e.g. "1234 5678" */ code_len = strlen(tokencode_str); for (i = 0, j = 0; i < code_len; i++) { if (i == code_len / 2) str[j++] = ' '; str[j++] = tokencode_str[i]; } str[j] = 0; gtk_label_set_text(GTK_LABEL(tokencode_text), str); gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress_bar), (double)sec / (interval - 1)); if (!opt_small) { sprintf(str, "00:%02d", sec); gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar), str); } return TRUE; }
int main(int argc, char **argv) { char *cmd = parse_cmdline(argc, argv, NOT_GUI); int rc; char buf[BUFLEN]; struct securid_token *t; rc = common_init(cmd); if (rc != ERR_NONE) die("can't initialize: %s\n", stoken_errstr[rc]); t = current_token; if (!t) die("error: no token present. Use 'stoken import' to add one.\n"); terminal_init(); if (!strcmp(cmd, "tokencode")) { int days_left = securid_check_exp(t, adjusted_time()); if (days_left < 0 && !opt_force) die("error: token has expired; use --force to override\n"); unlock_token(t, 1, NULL); securid_compute_tokencode(t, adjusted_time(), buf); puts(buf); if (days_left < 14 && !opt_force) warn("warning: token expires in %d day%s\n", days_left, days_left == 1 ? "" : "s"); } else if (!strcmp(cmd, "import")) { char *pass; unlock_token(t, 0, &pass); if (!opt_keep_password) { pass = xmalloc(BUFLEN); request_new_pass(pass); } t->is_smartphone = 0; securid_encode_token(t, pass, opt_new_devid, buf); rc = write_token_and_pin(buf, NULL, pass); if (rc != ERR_NONE) die("rcfile: error writing new token: %s\n", stoken_errstr[rc]); } else if (!strcmp(cmd, "export")) { char *pass; unlock_token(t, 0, &pass); if (opt_new_password) pass = opt_new_password; else if (!opt_keep_password) pass = NULL; t->is_smartphone = opt_iphone || opt_android; securid_encode_token(t, pass, opt_new_devid, buf); print_formatted(buf); } else if (!strcmp(cmd, "show")) { unlock_token(t, 0, NULL); securid_token_info(t, &print_token_info_line); } else if (!strcmp(cmd, "setpin")) { char *pass = NULL, pin[BUFLEN], confirm_pin[BUFLEN]; int len; if (opt_file || opt_token) die("error: setpin only operates on the rcfile token\n"); unlock_token(t, 0, &pass); if (opt_new_pin) { if (securid_pin_format_ok(opt_new_pin) != ERR_NONE) die("error: invalid --new-pin format\n"); strncpy(pin, opt_new_pin, BUFLEN); len = strlen(pin); } else { prompt("Enter new PIN: "); len = read_user_input(pin, BUFLEN, 1); if (len > 0 && securid_pin_format_ok(pin) != ERR_NONE) die("error: PIN must be 4-8 digits\n"); prompt("Confirm new PIN: "); read_user_input(confirm_pin, BUFLEN, 1); if (strcmp(pin, confirm_pin) != 0) die("error: PINs do not match\n"); } securid_encode_token(t, pass, NULL, buf); rc = write_token_and_pin(buf, len ? pin : NULL, pass); free(pass); if (rc != ERR_NONE) die("error: can't set PIN: %s\n", stoken_errstr[rc]); } else if (!strcmp(cmd, "setpass")) { char pass[BUFLEN]; unlock_token(t, 0, NULL); request_new_pass(pass); securid_encode_token(t, pass, NULL, buf); /* just print to stdout if it didn't come from the rcfile */ if (opt_file || opt_token) print_formatted(buf); else { rc = write_token_and_pin(buf, strlen(t->pin) ? t->pin : NULL, strlen(pass) ? pass : NULL); if (rc != ERR_NONE) die("error: can't set password: %s\n", stoken_errstr[rc]); } } else die("error: invalid command '%s'\n", cmd); return 0; }