aboutsummaryrefslogtreecommitdiff
path: root/src/misc.c
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cynapses.org>2010-09-29 11:26:35 +0200
committerAndreas Schneider <asn@cynapses.org>2010-09-29 11:26:35 +0200
commit8b719e51cf3e1b0ee7bee93a006bfa517fba4926 (patch)
tree0db367ac509ec9d062093387fc0b6fd423f46c6c /src/misc.c
parent72b62d3064fb567015aeed72d800b9eb4ca22a79 (diff)
downloadlibssh-8b719e51cf3e1b0ee7bee93a006bfa517fba4926.tar.gz
libssh-8b719e51cf3e1b0ee7bee93a006bfa517fba4926.tar.xz
libssh-8b719e51cf3e1b0ee7bee93a006bfa517fba4926.zip
misc: Move ssh_analyze_banner to a common location.
Don't duplicate functions!
Diffstat (limited to 'src/misc.c')
-rw-r--r--src/misc.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/misc.c b/src/misc.c
index ee78d8b0..b19c6c9f 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -681,6 +681,69 @@ char *ssh_path_expand_escape(ssh_session session, const char *s) {
#undef MAX_BUF_SIZE
}
+/**
+ * @internal
+ *
+ * @brief Analyze the SSH banner to find out if we have a SSHv1 or SSHv2
+ * server.
+ *
+ * @param session The session to analyze the banner from.
+ * @param ssh1 The variable which is set if it is a SSHv1 server.
+ * @param ssh2 The variable which is set if it is a SSHv2 server.
+ *
+ * @return 0 on success, < 0 on error.
+ *
+ * @see ssh_get_banner()
+ */
+int ssh_analyze_banner(ssh_session session, int *ssh1, int *ssh2) {
+ const char *banner = session->clientbanner;
+ const char *openssh;
+
+ ssh_log(session, SSH_LOG_RARE, "Analyzing banner: %s", banner);
+
+ if (strncmp(banner, "SSH-", 4) != 0) {
+ ssh_set_error(session, SSH_FATAL, "Protocol mismatch: %s", banner);
+ return -1;
+ }
+
+ /*
+ * Typical banners e.g. are:
+ * SSH-1.5-blah
+ * SSH-1.99-blah
+ * SSH-2.0-blah
+ */
+ switch(banner[4]) {
+ case '1':
+ *ssh1 = 1;
+ if (banner[6] == '9') {
+ *ssh2 = 1;
+ } else {
+ *ssh2 = 0;
+ }
+ break;
+ case '2':
+ *ssh1 = 0;
+ *ssh2 = 1;
+ break;
+ default:
+ ssh_set_error(session, SSH_FATAL, "Protocol mismatch: %s", banner);
+ return -1;
+ }
+
+ openssh = strstr(banner, "OpenSSH");
+ if (openssh != NULL) {
+ int major, minor;
+ major = strtol(openssh + 8, (char **) NULL, 10);
+ minor = strtol(openssh + 10, (char **) NULL, 10);
+ session->openssh = SSH_VERSION_INT(major, minor, 0);
+ ssh_log(session, SSH_LOG_RARE,
+ "We are talking to an OpenSSH client version: %d.%d (%x)",
+ major, minor, session->openssh);
+ }
+
+ return 0;
+}
+
/** @} */
/* vim: set ts=4 sw=4 et cindent: */