aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAndreas Schneider <mail@cynapses.org>2009-10-15 17:56:03 +0200
committerAndreas Schneider <mail@cynapses.org>2009-10-15 17:56:03 +0200
commit86620fc9914b7a2a558bdc20148fd4b898bbda4b (patch)
treed8343ef031579fed34d6223d3396737531abcff8 /examples
parentb3dfd5cffcb44b0e432aad0f2eaf7738711f0865 (diff)
downloadlibssh-86620fc9914b7a2a558bdc20148fd4b898bbda4b.tar.gz
libssh-86620fc9914b7a2a558bdc20148fd4b898bbda4b.tar.xz
libssh-86620fc9914b7a2a558bdc20148fd4b898bbda4b.zip
Improve example authentication.
Diffstat (limited to 'examples')
-rw-r--r--examples/authentication.c85
1 files changed, 48 insertions, 37 deletions
diff --git a/examples/authentication.c b/examples/authentication.c
index 06a0b71..29b83ea 100644
--- a/examples/authentication.c
+++ b/examples/authentication.c
@@ -65,50 +65,61 @@ int authenticate_kbdint(ssh_session session){
}
int authenticate_console(ssh_session session){
- int auth;
- int methods;
+ int rc;
+ int method;
char *password;
char *banner;
- ssh_userauth_none(session, NULL);
- methods = ssh_auth_list(session);
- printf("supported auth methods: ");
- if (methods & SSH_AUTH_METHOD_PUBLICKEY) {
- printf("publickey");
+ // Try to authenticate
+ rc = ssh_userauth_none(session, NULL);
+ if (rc == SSH_AUTH_ERROR) {
+ perror("Authentication failed.");
+ return rc;
}
- if (methods & SSH_AUTH_METHOD_INTERACTIVE) {
- printf(", keyboard-interactive");
- }
- if (methods & SSH_AUTH_METHOD_PASSWORD) {
- printf(", password");
- }
- printf("\n");
- auth=ssh_userauth_autopubkey(session, NULL);
- if(auth==SSH_AUTH_ERROR){
- return auth;
- }
- banner=ssh_get_issue_banner(session);
- if(banner){
- printf("%s\n",banner);
- free(banner);
- }
- if(auth!=SSH_AUTH_SUCCESS && (methods & SSH_AUTH_METHOD_INTERACTIVE)){
- auth=authenticate_kbdint(session);
- if(auth==SSH_AUTH_ERROR){
- return auth;
+ 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) {
+ perror("Authentication failed.");
+ return rc;
+ } else if (rc == SSH_AUTH_SUCCESS) {
+ break;
+ }
}
- }
- if(auth!=SSH_AUTH_SUCCESS && (methods & SSH_AUTH_METHOD_PASSWORD)){
+
+ // Try to authenticate with keyboard interactive";
+ if (method & SSH_AUTH_METHOD_INTERACTIVE) {
+ rc = authenticate_kbdint(session);
+ if (rc == SSH_AUTH_ERROR) {
+ perror("Authentication failed.");
+ return rc;
+ } else if (rc == SSH_AUTH_SUCCESS) {
+ break;
+ }
+ }
+
password=getpass("Password: ");
- if(ssh_userauth_password(session,NULL,password) != SSH_AUTH_SUCCESS){
- return auth;
+ // Try to authenticate with password
+ if (method & SSH_AUTH_METHOD_PASSWORD) {
+ rc = ssh_userauth_password(session, NULL, password);
+ if (rc == SSH_AUTH_ERROR) {
+ perror("Authentication failed.");
+ return rc;
+ } else if (rc == SSH_AUTH_SUCCESS) {
+ break;
+ }
}
- memset(password,0,strlen(password));
}
- if(auth==SSH_AUTH_SUCCESS)
- ssh_log(session, SSH_LOG_FUNCTIONS, "Authentication success");
- if(auth==SSH_AUTH_PARTIAL)
- return SSH_AUTH_DENIED;
- return auth;
+
+ banner = ssh_get_issue_banner(session);
+ if (banner) {
+ printf("%s\n",banner);
+ free(banner);
+ }
+
+ return rc;
}