summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/libssh/libssh.h2
-rw-r--r--libssh/options.c29
-rw-r--r--sample.c10
3 files changed, 30 insertions, 11 deletions
diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h
index 20b2868..322b0e0 100644
--- a/include/libssh/libssh.h
+++ b/include/libssh/libssh.h
@@ -308,7 +308,7 @@ SSH_OPTIONS *ssh_options_new(void);
SSH_OPTIONS *ssh_options_copy(SSH_OPTIONS *opt);
void ssh_options_free(SSH_OPTIONS *opt);
int ssh_options_set_wanted_algos(SSH_OPTIONS *opt, int algo, const char *list);
-void ssh_options_set_username(SSH_OPTIONS *opt, const char *username);
+int ssh_options_set_username(SSH_OPTIONS *opt, const char *username);
void ssh_options_set_port(SSH_OPTIONS *opt, unsigned int port);
int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **argv);
int ssh_options_set_host(SSH_OPTIONS *opt, const char *host);
diff --git a/libssh/options.c b/libssh/options.c
index e12a235..2bdc589 100644
--- a/libssh/options.c
+++ b/libssh/options.c
@@ -257,15 +257,28 @@ int ssh_options_set_host(SSH_OPTIONS *opt, const char *hostname){
return 0;
}
-/** \brief set username for authentication
- * \bug this should not be set at options time
- * \param opt options structure
- * \param username user name to authenticate
+/**
+ * @brief Set the username for authentication
+ *
+ * @param opt The options structure to use.
+ *
+ * @param username The username to authenticate.
+ *
+ * @return 0 on success, -1 on error.
+ *
+ * @bug this should not be set at options time
*/
-void ssh_options_set_username(SSH_OPTIONS *opt, const char *username){
- if(opt->username)
- free(opt->username);
- opt->username=strdup(username);
+int ssh_options_set_username(SSH_OPTIONS *opt, const char *username) {
+ if (opt->username) {
+ SAFE_FREE(opt->username);
+ }
+
+ opt->username = strdup(username);
+ if (opt->username == NULL) {
+ return -1;
+ }
+
+ return 0;
}
/** If you wish to open the socket yourself for a reason
diff --git a/sample.c b/sample.c
index 543bcfc..debb09a 100644
--- a/sample.c
+++ b/sample.c
@@ -420,8 +420,14 @@ int main(int argc, char **argv){
}
opts(argc,argv);
signal(SIGTERM, do_exit);
- if(user)
- ssh_options_set_username(options,user);
+
+ if (user) {
+ if (ssh_options_set_username(options,user) < 0) {
+ ssh_options_free(options);
+ return 1;
+ }
+ }
+
if (ssh_options_set_host(options,host) < 0) {
ssh_options_free(options);
return 1;