static int auth_callback(const char *prompt, char *buf, size_t len, int echo, int verify, void *userdata) { (void) verify; (void) userdata; return ssh_getpass(prompt, buf, len, echo, verify); } /* static int auth_callback */
static int auth_callback(const char *prompt, char *buf, size_t len, int echo, int verify, void *userdata) { char *answer = NULL; char *ptr; (void) verify; (void) userdata; if (echo) { while ((answer = fgets(buf, len, stdin)) == NULL); if ((ptr = strchr(buf, '\n'))) { *ptr = '\0'; } } else { if (ssh_getpass(prompt, buf, len, 0, 0) < 0) { return -1; } return 0; } if (answer == NULL) { return -1; } strncpy(buf, answer, len); return 0; }
int authenticate_kbdint(ssh_session session, const char *password) { int err; err = ssh_userauth_kbdint(session, NULL, NULL); while (err == SSH_AUTH_INFO) { const char *instruction; const char *name; char buffer[128]; int i, n; name = ssh_userauth_kbdint_getname(session); instruction = ssh_userauth_kbdint_getinstruction(session); n = ssh_userauth_kbdint_getnprompts(session); if (name && strlen(name) > 0) { printf("%s\n", name); } if (instruction && strlen(instruction) > 0) { printf("%s\n", instruction); } for (i = 0; i < n; i++) { const char *answer; const char *prompt; char echo; prompt = ssh_userauth_kbdint_getprompt(session, i, &echo); if (prompt == NULL) { break; } if (echo) { char *p; printf("%s", prompt); if (fgets(buffer, sizeof(buffer), stdin) == NULL) { return SSH_AUTH_ERROR; } buffer[sizeof(buffer) - 1] = '\0'; if ((p = strchr(buffer, '\n'))) { *p = '\0'; } if (ssh_userauth_kbdint_setanswer(session, i, buffer) < 0) { return SSH_AUTH_ERROR; } memset(buffer, 0, strlen(buffer)); } else { if (password && strstr(prompt, "Password:")) { answer = password; } else { buffer[0] = '\0'; if (ssh_getpass(prompt, buffer, sizeof(buffer), 0, 0) < 0) { return SSH_AUTH_ERROR; } answer = buffer; } if (ssh_userauth_kbdint_setanswer(session, i, answer) < 0) { return SSH_AUTH_ERROR; } } } err=ssh_userauth_kbdint(session,NULL,NULL); } return err; }
int authenticate(ssh_session session){ int rc; int method; char password[128] = {0}; char *banner; // Try to authenticate rc = ssh_userauth_none(session, NULL); if (rc == SSH_AUTH_ERROR) { error(session); return rc; } method = ssh_auth_list(session); while (rc != SSH_AUTH_SUCCESS) { // Try to authenticate with public key first if (method & SSH_AUTH_METHOD_PUBLICKEY) { rc = ssh_userauth_autopubkey(session, NULL); if (rc == SSH_AUTH_ERROR) { error(session); return rc; } else if (rc == SSH_AUTH_SUCCESS) { break; } } // Try to authenticate with keyboard interactive"; if (method & SSH_AUTH_METHOD_INTERACTIVE) { rc = authenticate_kbdint(session, NULL); if (rc == SSH_AUTH_ERROR) { error(session); return rc; } else if (rc == SSH_AUTH_SUCCESS) { break; } } if (ssh_getpass("Password: "******"%s\n",banner); ssh_string_free_char(banner); } return rc; }