Skip to content

Commit

Permalink
tidy-up: fix more nits
Browse files Browse the repository at this point in the history
- fix indentation errors.
- reformat `cmake/FindmbedTLS.cmake`
- replace a macro with a variable in `example/sftp_RW_nonblock.c`.
- delete macOS macro `_DARWIN_USE_64_BIT_INODE` from the
  OS/400 config header, `os400/libssh2_config.h`.
- fix other minor nits.

Closes libssh2#983
  • Loading branch information
vszakats committed Apr 18, 2023
1 parent 2213352 commit 17801d2
Show file tree
Hide file tree
Showing 23 changed files with 160 additions and 182 deletions.
88 changes: 43 additions & 45 deletions cmake/FindmbedTLS.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,53 @@
# MBEDX509_LIBRARY - path to mbedTLS X.509 library
# MBEDCRYPTO_LIBRARY - path to mbedTLS Crypto library

FIND_PATH(MBEDTLS_INCLUDE_DIR mbedtls/version.h)
find_path(MBEDTLS_INCLUDE_DIR mbedtls/version.h)

IF(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARIES)
# Already in cache, be silent
SET(MBEDTLS_FIND_QUIETLY TRUE)
ENDIF()
if(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARIES)
# Already in cache, be silent
set(MBEDTLS_FIND_QUIETLY TRUE)
endif()

FIND_LIBRARY(MBEDTLS_LIBRARY NAMES mbedtls libmbedtls libmbedx509)
FIND_LIBRARY(MBEDX509_LIBRARY NAMES mbedx509 libmbedx509)
FIND_LIBRARY(MBEDCRYPTO_LIBRARY NAMES mbedcrypto libmbedcrypto)
find_library(MBEDTLS_LIBRARY NAMES mbedtls libmbedtls libmbedx509)
find_library(MBEDX509_LIBRARY NAMES mbedx509 libmbedx509)
find_library(MBEDCRYPTO_LIBRARY NAMES mbedcrypto libmbedcrypto)

IF(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARY AND MBEDX509_LIBRARY AND MBEDCRYPTO_LIBRARY)
SET(MBEDTLS_FOUND TRUE)
ENDIF()
if(MBEDTLS_INCLUDE_DIR AND MBEDTLS_LIBRARY AND MBEDX509_LIBRARY AND MBEDCRYPTO_LIBRARY)
set(MBEDTLS_FOUND TRUE)
endif()

IF(MBEDTLS_FOUND)
# split mbedTLS into -L and -l linker options, so we can set them for pkg-config
GET_FILENAME_COMPONENT(MBEDTLS_LIBRARY_DIR ${MBEDTLS_LIBRARY} PATH)
GET_FILENAME_COMPONENT(MBEDTLS_LIBRARY_FILE ${MBEDTLS_LIBRARY} NAME_WE)
GET_FILENAME_COMPONENT(MBEDX509_LIBRARY_FILE ${MBEDX509_LIBRARY} NAME_WE)
GET_FILENAME_COMPONENT(MBEDCRYPTO_LIBRARY_FILE ${MBEDCRYPTO_LIBRARY} NAME_WE)
STRING(REGEX REPLACE "^lib" "" MBEDTLS_LIBRARY_FILE ${MBEDTLS_LIBRARY_FILE})
STRING(REGEX REPLACE "^lib" "" MBEDX509_LIBRARY_FILE ${MBEDX509_LIBRARY_FILE})
STRING(REGEX REPLACE "^lib" "" MBEDCRYPTO_LIBRARY_FILE ${MBEDCRYPTO_LIBRARY_FILE})
SET(MBEDTLS_LIBRARIES "-L${MBEDTLS_LIBRARY_DIR} -l${MBEDTLS_LIBRARY_FILE} -l${MBEDX509_LIBRARY_FILE} -l${MBEDCRYPTO_LIBRARY_FILE}")
if(MBEDTLS_FOUND)
# split mbedTLS into -L and -l linker options, so we can set them for pkg-config
get_filename_component(MBEDTLS_LIBRARY_DIR ${MBEDTLS_LIBRARY} PATH)
get_filename_component(MBEDTLS_LIBRARY_FILE ${MBEDTLS_LIBRARY} NAME_WE)
get_filename_component(MBEDX509_LIBRARY_FILE ${MBEDX509_LIBRARY} NAME_WE)
get_filename_component(MBEDCRYPTO_LIBRARY_FILE ${MBEDCRYPTO_LIBRARY} NAME_WE)
string(REGEX REPLACE "^lib" "" MBEDTLS_LIBRARY_FILE ${MBEDTLS_LIBRARY_FILE})
string(REGEX REPLACE "^lib" "" MBEDX509_LIBRARY_FILE ${MBEDX509_LIBRARY_FILE})
string(REGEX REPLACE "^lib" "" MBEDCRYPTO_LIBRARY_FILE ${MBEDCRYPTO_LIBRARY_FILE})
set(MBEDTLS_LIBRARIES "-L${MBEDTLS_LIBRARY_DIR} -l${MBEDTLS_LIBRARY_FILE} -l${MBEDX509_LIBRARY_FILE} -l${MBEDCRYPTO_LIBRARY_FILE}")

IF(NOT MBEDTLS_FIND_QUIETLY)
MESSAGE(STATUS "Found mbedTLS:")
FILE(READ ${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h MBEDTLSCONTENT)
STRING(REGEX MATCH "MBEDTLS_VERSION_STRING +\"[0-9|.]+\"" MBEDTLSMATCH ${MBEDTLSCONTENT})
IF (MBEDTLSMATCH)
STRING(REGEX REPLACE "MBEDTLS_VERSION_STRING +\"([0-9|.]+)\"" "\\1" MBEDTLS_VERSION ${MBEDTLSMATCH})
MESSAGE(STATUS " version ${MBEDTLS_VERSION}")
ENDIF(MBEDTLSMATCH)
MESSAGE(STATUS " TLS: ${MBEDTLS_LIBRARY}")
MESSAGE(STATUS " X509: ${MBEDX509_LIBRARY}")
MESSAGE(STATUS " Crypto: ${MBEDCRYPTO_LIBRARY}")
ENDIF(NOT MBEDTLS_FIND_QUIETLY)
ELSE(MBEDTLS_FOUND)
IF(MBEDTLS_FIND_REQUIRED)
MESSAGE(FATAL_ERROR "Could not find mbedTLS")
ENDIF(MBEDTLS_FIND_REQUIRED)
ENDIF(MBEDTLS_FOUND)
if(NOT MBEDTLS_FIND_QUIETLY)
message(STATUS "Found mbedTLS:")
file(READ ${MBEDTLS_INCLUDE_DIR}/mbedtls/version.h MBEDTLSCONTENT)
string(REGEX MATCH "MBEDTLS_VERSION_STRING +\"[0-9|.]+\"" MBEDTLSMATCH ${MBEDTLSCONTENT})
if(MBEDTLSMATCH)
string(REGEX REPLACE "MBEDTLS_VERSION_STRING +\"([0-9|.]+)\"" "\\1" MBEDTLS_VERSION ${MBEDTLSMATCH})
message(STATUS " version ${MBEDTLS_VERSION}")
endif()
message(STATUS " TLS: ${MBEDTLS_LIBRARY}")
message(STATUS " X509: ${MBEDX509_LIBRARY}")
message(STATUS " Crypto: ${MBEDCRYPTO_LIBRARY}")
endif()
elseif(MBEDTLS_FIND_REQUIRED)
message(FATAL_ERROR "Could not find mbedTLS")
endif()

MARK_AS_ADVANCED(
MBEDTLS_INCLUDE_DIR
MBEDTLS_LIBRARY_DIR
MBEDTLS_LIBRARIES
MBEDTLS_LIBRARY
MBEDX509_LIBRARY
MBEDCRYPTO_LIBRARY
mark_as_advanced(
MBEDTLS_INCLUDE_DIR
MBEDTLS_LIBRARY_DIR
MBEDTLS_LIBRARIES
MBEDTLS_LIBRARY
MBEDX509_LIBRARY
MBEDCRYPTO_LIBRARY
)
3 changes: 1 addition & 2 deletions docs/libssh2_sftp_realpath.3
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ libssh2_sftp_realpath - convenience macro for \fIlibssh2_sftp_symlink_ex(3)\fP
#include <libssh2_sftp.h>

#define libssh2_sftp_realpath(sftp, path, target, maxlen) \\
libssh2_sftp_symlink_ex((sftp), \\
(path), strlen(path), \\
libssh2_sftp_symlink_ex((sftp), (path), strlen(path), \\
(target), (maxlen), \\
LIBSSH2_SFTP_REALPATH)
.fi
Expand Down
8 changes: 5 additions & 3 deletions docs/libssh2_version.3
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ string or NULL if the \fIrequired_version\fP isn't fulfilled.
To make sure you run with the correct libssh2 version:

.nf
if (!libssh2_version(LIBSSH2_VERSION_NUM)) {
fprintf (stderr, \&"Runtime libssh2 version too old!\&");
if(!libssh2_version(LIBSSH2_VERSION_NUM)) {
fprintf(stderr, \&"Runtime libssh2 version too old!\&");
exit(1);
}
.fi

Unconditionally get the version number:

printf(\&"libssh2 version: %s\&", libssh2_version(0) );
.nf
printf(\&"libssh2 version: %s\&", libssh2_version(0));
.fi
.SH AVAILABILITY
This function was added in libssh2 1.1, in previous versions there way no way
to extract this info in run-time.
14 changes: 6 additions & 8 deletions example/sftp_RW_nonblock.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@
#include <stdio.h>
#include <ctype.h>

#define STORAGE "/tmp/sftp-storage" /* this is the local file name this
example uses to store the downloaded
file in */

static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
static const char *privkey = "/home/username/.ssh/id_rsa";
static const char *username = "username";
static const char *password = "password";
static const char *sftppath = "/tmp/TEST"; /* source path */
static const char *dest = "/tmp/TEST2"; /* destination path */
static const char *storage = "/tmp/sftp-storage"; /* local file name to store
the downloaded file in */

static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
{
Expand Down Expand Up @@ -170,9 +168,9 @@ int main(int argc, char *argv[])
}
fprintf(stderr, "\n");

tempstorage = fopen(STORAGE, "wb");
tempstorage = fopen(storage, "wb");
if(!tempstorage) {
fprintf(stderr, "Can't open temp storage file %s\n", STORAGE);
fprintf(stderr, "Can't open temp storage file %s\n", storage);
goto shutdown;
}

Expand Down Expand Up @@ -274,10 +272,10 @@ int main(int argc, char *argv[])
libssh2_sftp_close(sftp_handle);
fclose(tempstorage);

tempstorage = fopen(STORAGE, "rb");
tempstorage = fopen(storage, "rb");
if(!tempstorage) {
/* weird, we can't read the file we just wrote to... */
fprintf(stderr, "can't open %s for reading\n", STORAGE);
fprintf(stderr, "can't open %s for reading\n", storage);
goto shutdown;
}

Expand Down
4 changes: 2 additions & 2 deletions example/sftp_nonblock.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ static const char *sftppath = "/tmp/TEST";
/* diff in ms */
static long tvdiff(struct timeval newer, struct timeval older)
{
return (newer.tv_sec - older.tv_sec) * 1000 +
(newer.tv_usec - older.tv_usec) / 1000;
return (newer.tv_sec - older.tv_sec) * 1000 +
(newer.tv_usec - older.tv_usec) / 1000;
}
#endif

Expand Down
12 changes: 6 additions & 6 deletions example/ssh2.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,12 @@ int main(int argc, char *argv[])
/* Instead of just running a single command with libssh2_channel_exec,
* a shell can be opened on the channel instead, for interactive use.
* You usually want a pty allocated first in that case (see above). */
#if 0
if(libssh2_channel_shell(channel)) {
fprintf(stderr, "Unable to request shell on allocated pty\n");
goto shutdown;
}
#endif
#if 0
if(libssh2_channel_shell(channel)) {
fprintf(stderr, "Unable to request shell on allocated pty\n");
goto shutdown;
}
#endif

/* At this point the shell can be interacted with using
* libssh2_channel_read()
Expand Down
5 changes: 0 additions & 5 deletions os400/libssh2_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,6 @@
significant byte first (like Motorola and SPARC, unlike Intel). */
#define WORDS_BIGENDIAN 1

/* Enable large inode numbers on Mac OS X 10.5. */
#ifndef _DARWIN_USE_64_BIT_INODE
# define _DARWIN_USE_64_BIT_INODE 1
#endif

/* Number of bits in a file offset, on hosts where this is settable. */
#undef _FILE_OFFSET_BITS

Expand Down
4 changes: 2 additions & 2 deletions src/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -1821,7 +1821,7 @@ libssh2_channel_get_exit_signal(LIBSSH2_CHANNEL *channel,
*exitsignal = LIBSSH2_ALLOC(session, namelen + 1);
if(!*exitsignal) {
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for signal name");
"Unable to allocate memory for signal name");
}
memcpy(*exitsignal, channel->exit_signal, namelen);
(*exitsignal)[namelen] = '\0';
Expand Down Expand Up @@ -2660,7 +2660,7 @@ int _libssh2_channel_close(LIBSSH2_CHANNEL * channel)
return rc;
}
_libssh2_error(session, rc,
"Unable to send EOF, but closing channel anyway");
"Unable to send EOF, but closing channel anyway");
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,9 @@ static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_3des_cbc = {
towards the start are chosen in preference to ones further down the list. */
static const LIBSSH2_CRYPT_METHOD *_libssh2_crypt_methods[] = {
#if LIBSSH2_AES_CTR
&libssh2_crypt_method_aes256_ctr,
&libssh2_crypt_method_aes192_ctr,
&libssh2_crypt_method_aes128_ctr,
&libssh2_crypt_method_aes256_ctr,
&libssh2_crypt_method_aes192_ctr,
&libssh2_crypt_method_aes128_ctr,
#endif /* LIBSSH2_AES */
#if LIBSSH2_AES
&libssh2_crypt_method_aes256_cbc,
Expand Down
10 changes: 5 additions & 5 deletions src/hostkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ hostkey_method_ssh_rsa_sha2_256_sig_verify(LIBSSH2_SESSION * session,
(void)session;

/* Skip past keyname_len(4) + keyname(12){"rsa-sha2-256"} +
signature_len(4) */
signature_len(4) */
if(sig_len < 20)
return -1;

Expand Down Expand Up @@ -339,7 +339,7 @@ hostkey_method_ssh_rsa_sha2_512_sig_verify(LIBSSH2_SESSION * session,
(void)session;

/* Skip past keyname_len(4) + keyname(12){"rsa-sha2-512"} +
signature_len(4) */
signature_len(4) */
if(sig_len < 20)
return -1;

Expand Down Expand Up @@ -510,7 +510,7 @@ hostkey_method_ssh_dss_init(LIBSSH2_SESSION * session,
return -1;

if(_libssh2_get_string(&buf, &p, &p_len))
return -1;
return -1;

if(_libssh2_get_string(&buf, &q, &q_len))
return -1;
Expand Down Expand Up @@ -877,14 +877,14 @@ hostkey_method_ssh_ecdsa_sig_verify(LIBSSH2_SESSION * session,
buf.dataptr = buf.data;
buf.len = sig_len;

if(_libssh2_get_string(&buf, &name, &name_len) || name_len != 19)
if(_libssh2_get_string(&buf, &name, &name_len) || name_len != 19)
return -1;

if(_libssh2_get_u32(&buf, &len) != 0 || len < 8)
return -1;

if(_libssh2_get_string(&buf, &r, &r_len))
return -1;
return -1;

if(_libssh2_get_string(&buf, &s, &s_len))
return -1;
Expand Down
32 changes: 14 additions & 18 deletions src/kex.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@ static int diffie_hellman_sha_algo(LIBSSH2_SESSION *session,
{
char fingerprint[64], *fprint = fingerprint;
int i;

for(i = 0; i < 20; i++, fprint += 3) {
snprintf(fprint, 4, "%02x:", session->server_hostkey_sha1[i]);
}
Expand Down Expand Up @@ -620,9 +619,9 @@ static int diffie_hellman_sha_algo(LIBSSH2_SESSION *session,
exchange_state->h_sig_comp);

if(session->hostkey->
sig_verify(session, exchange_state->h_sig,
exchange_state->h_sig_len, exchange_state->h_sig_comp,
digest_len, &session->server_hostkey_abstract)) {
sig_verify(session, exchange_state->h_sig,
exchange_state->h_sig_len, exchange_state->h_sig_comp,
digest_len, &session->server_hostkey_abstract)) {
ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_SIGN,
"Unable to verify hostkey signature");
goto clean_exit;
Expand Down Expand Up @@ -1718,7 +1717,7 @@ static int ecdh_sha2_nistp(LIBSSH2_SESSION *session, libssh2_curve_type type,

if(data_len < 5) {
ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT,
"Host key data is too short");
"Host key data is too short");
return ret;
}

Expand All @@ -1745,9 +1744,9 @@ static int ecdh_sha2_nistp(LIBSSH2_SESSION *session, libssh2_curve_type type,

if(_libssh2_copy_string(session, &buf, &(session->server_hostkey),
&server_public_key_len)) {
ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for a copy "
"of the host key");
ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for a copy "
"of the host key");
goto clean_exit;
}

Expand Down Expand Up @@ -1800,7 +1799,6 @@ static int ecdh_sha2_nistp(LIBSSH2_SESSION *session, libssh2_curve_type type,
{
char fingerprint[64], *fprint = fingerprint;
int i;

for(i = 0; i < 20; i++, fprint += 3) {
snprintf(fprint, 4, "%02x:", session->server_hostkey_sha1[i]);
}
Expand Down Expand Up @@ -1853,7 +1851,7 @@ static int ecdh_sha2_nistp(LIBSSH2_SESSION *session, libssh2_curve_type type,
if(_libssh2_get_string(&buf, &server_public_key,
&server_public_key_len)) {
ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO,
"Unexpected key length");
"Unexpected key length");
goto clean_exit;
}

Expand Down Expand Up @@ -1949,13 +1947,13 @@ static int ecdh_sha2_nistp(LIBSSH2_SESSION *session, libssh2_curve_type type,
}

/* The first key exchange has been performed,
switch to active crypt/comp/mac mode */
switch to active crypt/comp/mac mode */
session->state |= LIBSSH2_STATE_NEWKEYS;
_libssh2_debug((session, LIBSSH2_TRACE_KEX,
"Received NEWKEYS message"));

/* This will actually end up being just packet_type(1)
for this packet type anyway */
for this packet type anyway */
LIBSSH2_FREE(session, exchange_state->tmp);

if(!session->session_id) {
Expand Down Expand Up @@ -2430,7 +2428,6 @@ curve25519_sha256(LIBSSH2_SESSION *session, unsigned char *data,
{
char fingerprint[64], *fprint = fingerprint;
int i;

for(i = 0; i < 20; i++, fprint += 3) {
snprintf(fprint, 4, "%02x:", session->server_hostkey_sha1[i]);
}
Expand Down Expand Up @@ -2573,15 +2570,14 @@ curve25519_sha256(LIBSSH2_SESSION *session, unsigned char *data,
goto clean_exit;
}

/* The first key exchange has been performed, switch to active
crypt/comp/mac mode */

/* The first key exchange has been performed,
switch to active crypt/comp/mac mode */
session->state |= LIBSSH2_STATE_NEWKEYS;
_libssh2_debug((session, LIBSSH2_TRACE_KEX,
"Received NEWKEYS message"));

/* This will actually end up being just packet_type(1) for this packet
type anyway */
/* This will actually end up being just packet_type(1)
for this packet type anyway */
LIBSSH2_FREE(session, exchange_state->tmp);

if(!session->session_id) {
Expand Down
Loading

0 comments on commit 17801d2

Please sign in to comment.