aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/samplesshd.c104
-rw-r--r--include/libssh/server.h1
-rw-r--r--libssh/options.c20
3 files changed, 121 insertions, 4 deletions
diff --git a/examples/samplesshd.c b/examples/samplesshd.c
index e9757f20..5c03192e 100644
--- a/examples/samplesshd.c
+++ b/examples/samplesshd.c
@@ -14,9 +14,11 @@ clients must be made or how a client should react.
#include <libssh/libssh.h>
#include <libssh/server.h>
-#include <unistd.h>
+#include <argp.h>
+#include <stdlib.h>
#include <string.h>
#include <stdio.h>
+#include <unistd.h>
#ifndef KEYS_FOLDER
#ifdef _WIN32
#define KEYS_FOLDER
@@ -33,6 +35,98 @@ static int auth_password(char *user, char *password){
return 1; // authenticated
}
+const char *argp_program_version = "libssh server example "
+ SSH_STRINGIFY(LIBSSH_VERSION);
+const char *argp_program_bug_address = "<libssh@libssh.org>";
+
+/* Program documentation. */
+static char doc[] = "libssh -- a Secure Shell protocol implementation";
+
+/* A description of the arguments we accept. */
+static char args_doc[] = "BINDADDR";
+
+/* The options we understand. */
+static struct argp_option options[] = {
+ {
+ .name = "port",
+ .key = 'p',
+ .arg = "PORT",
+ .flags = 0,
+ .doc = "Set the port to bind.",
+ .group = 0
+ },
+ {
+ .name = "hostkey",
+ .key = 'k',
+ .arg = "FILE",
+ .flags = 0,
+ .doc = "Set the host key.",
+ .group = 0
+ },
+ {
+ .name = "dsakey",
+ .key = 'd',
+ .arg = "FILE",
+ .flags = 0,
+ .doc = "Set the dsa key.",
+ .group = 0
+ },
+ {
+ .name = "rsakey",
+ .key = 'r',
+ .arg = "FILE",
+ .flags = 0,
+ .doc = "Set the rsa key.",
+ .group = 0
+ },
+ {NULL, 0, 0, 0, NULL, 0}
+};
+
+/* Parse a single option. */
+static error_t parse_opt (int key, char *arg, struct argp_state *state) {
+ int i;
+ /* Get the input argument from argp_parse, which we
+ * know is a pointer to our arguments structure.
+ */
+ ssh_bind sshbind = state->input;
+
+ switch (key) {
+ case 'p':
+ i = atoi(arg);
+ ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &i);
+ break;
+ case 'd':
+ ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_DSAKEY, arg);
+ break;
+ case 'k':
+ ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, arg);
+ break;
+ case 'r':
+ ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, arg);
+ break;
+ case ARGP_KEY_ARG:
+ if (state->arg_num >= 1) {
+ /* Too many arguments. */
+ argp_usage (state);
+ }
+ ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, arg);
+ break;
+ case ARGP_KEY_END:
+ if (state->arg_num < 1) {
+ /* Not enough arguments. */
+ argp_usage (state);
+ }
+ break;
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+
+ return 0;
+}
+
+/* Our argp parser. */
+static struct argp argp = {options, parse_opt, args_doc, doc, NULL, NULL, NULL};
+
int main(int argc, char **argv){
ssh_session session;
ssh_bind sshbind;
@@ -46,10 +140,16 @@ int main(int argc, char **argv){
sshbind=ssh_bind_new();
session=ssh_new();
- ssh_options_getopt(session,&argc,argv);
+
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_DSAKEY, KEYS_FOLDER "ssh_host_dsa_key");
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, KEYS_FOLDER "ssh_host_rsa_key");
+ /*
+ * Parse our arguments; every option seen by parse_opt will
+ * be reflected in arguments.
+ */
+ argp_parse (&argp, argc, argv, 0, 0, sshbind);
+
if(ssh_bind_listen(sshbind)<0){
printf("Error listening to socket: %s\n",ssh_get_error(sshbind));
return 1;
diff --git a/include/libssh/server.h b/include/libssh/server.h
index c53f4bf2..db7d402e 100644
--- a/include/libssh/server.h
+++ b/include/libssh/server.h
@@ -39,6 +39,7 @@ extern "C" {
enum ssh_bind_options_e {
SSH_BIND_OPTIONS_BINDADDR,
SSH_BIND_OPTIONS_BINDPORT,
+ SSH_BIND_OPTIONS_BINDPORT_STR,
SSH_BIND_OPTIONS_HOSTKEY,
SSH_BIND_OPTIONS_DSAKEY,
SSH_BIND_OPTIONS_RSAKEY,
diff --git a/libssh/options.c b/libssh/options.c
index d5069813..31a5232a 100644
--- a/libssh/options.c
+++ b/libssh/options.c
@@ -621,10 +621,8 @@ static int ssh_bind_options_set_algo(ssh_bind sshbind, int algo,
int ssh_bind_options_set(ssh_bind sshbind, enum ssh_bind_options_e type,
const void *value) {
-#if 0
char *p, *q;
int i;
-#endif
if (sshbind == NULL) {
return -1;
@@ -661,6 +659,24 @@ int ssh_bind_options_set(ssh_bind sshbind, enum ssh_bind_options_e type,
sshbind->bindport = *x & 0xffff;
}
break;
+ case SSH_BIND_OPTIONS_BINDPORT_STR:
+ if (value == NULL) {
+ sshbind->bindport = 22 & 0xffff;
+ } else {
+ q = strdup(value);
+ if (q == NULL) {
+ ssh_set_error_oom(sshbind);
+ return -1;
+ }
+ i = strtol(q, &p, 10);
+ if (q == p) {
+ SAFE_FREE(q);
+ }
+ SAFE_FREE(q);
+
+ sshbind->bindport = i & 0xffff;
+ }
+ break;
case SSH_BIND_OPTIONS_DSAKEY:
if (value == NULL) {
ssh_set_error_invalid(sshbind, __FUNCTION__);