The SSH helper functions.

Overview

Different helper functions used in the SSH Library. More…

// global functions

char*
ssh_basename(const char* path);

char*
ssh_dirname(const char* path);

int
ssh_getpass(
    const char* prompt,
    char* buf,
    size_t len,
    int echo,
    int verify
);

int
ssh_mkdir(
    const char* pathname,
    mode_t mode
);

const char*
ssh_version(int req_version);

char*
ssh_path_expand_tilde(const char* d);

int
ssh_timeout_update(
    struct ssh_timestamp* ts,
    int timeout
);

const void*
_ssh_list_pop_head(struct ssh_list* list);

int
ssh_analyze_banner(
    ssh_session session,
    int server,
    int* ssh1,
    int* ssh2
);

int
ssh_file_readaccess_ok(const char* file);

char*
ssh_get_local_username(void);

char*
ssh_get_user_home_dir(void);

char*
ssh_hostport(
    const char* host,
    int port
);

int
ssh_is_ipaddr(const char* str);

int
ssh_is_ipaddr_v4(const char* str);

static
struct ssh_iterator*
ssh_iterator_new(const void* data);

int
ssh_list_append(
    struct ssh_list* list,
    const void* data
);

struct ssh_iterator*
ssh_list_find(
    const struct ssh_list* list,
    void* value
);

void
ssh_list_free(struct ssh_list* list);

struct ssh_iterator*
ssh_list_get_iterator(const struct ssh_list* list);

struct ssh_list*
ssh_list_new(void);

int
ssh_list_prepend(
    struct ssh_list* list,
    const void* data
);

void
ssh_list_remove(
    struct ssh_list* list,
    struct ssh_iterator* iterator
);

char*
ssh_lowercase(const char* str);

int
ssh_make_milliseconds(
    long sec,
    long usec
);

int
ssh_match_group(
    const char* group,
    const char* object
);

char*
ssh_path_expand_escape(
    ssh_session session,
    const char* s
);

int
ssh_timeout_elapsed(
    struct ssh_timestamp* ts,
    int timeout
);

static
int
ssh_timestamp_difference(
    struct ssh_timestamp* old,
    struct ssh_timestamp* new
);

void
ssh_timestamp_init(struct ssh_timestamp* ts);

// macros

#define CLOCK
#define NSS_BUFLEN_PASSWD

Detailed Documentation

Different helper functions used in the SSH Library.

Global Functions

char*
ssh_basename(const char* path)

basename - parse filename component.

basename breaks a null-terminated pathname string into a filename component. ssh_basename() returns the component following the final ‘/’. Trailing ‘/’ characters are not counted as part of the pathname.

Parameters:

path

The path to parse.

Returns:

The filename of path or NULL if we can’t allocate memory. If path is a the string “/”, basename returns the string “/”. If path is NULL or an empty string, “.” is returned.

char*
ssh_dirname(const char* path)

Parse directory component.

dirname breaks a null-terminated pathname string into a directory component. In the usual case, ssh_dirname() returns the string up to, but not including, the final ‘/’. Trailing ‘/’ characters are not counted as part of the pathname. The caller must free the memory.

Parameters:

path

The path to parse.

Returns:

The dirname of path or NULL if we can’t allocate memory. If path does not contain a slash, c_dirname() returns the string “.”. If path is the string “/”, it returns the string “/”. If path is NULL or an empty string, “.” is returned.

int
ssh_getpass(
    const char* prompt,
    char* buf,
    size_t len,
    int echo,
    int verify
)

Get a password from the console.

You should make sure that the buffer is an empty string!

You can also use this function to ask for a username. Then you can fill the buffer with the username and it is shows to the users. If the users just presses enter the buffer will be untouched.

char username[128];

snprintf(username, sizeof(username), "john");

ssh_getpass("Username:", username, sizeof(username), 1, 0);

The prompt will look like this:

Username: [john]

If you press enter then john is used as the username, or you can type it in to change it.

Parameters:

prompt

The prompt to show to ask for the password.

buf

The buffer the password should be stored. It NEEDS to be empty or filled out.

len

The length of the buffer.

echo

Should we echo what you type.

verify

Should we ask for the password twice.

Returns:

0 on success, -1 on error.

int
ssh_mkdir(
    const char* pathname,
    mode_t mode
)

Attempts to create a directory with the given pathname.

This is the portable version of mkdir, mode is ignored on Windows systems.

Parameters:

pathname

The path name to create the directory.

mode

The permissions to use.

Returns:

0 on success, < 0 on error with errno set.

const char*
ssh_version(int req_version)

Check if libssh is the required version or get the version string.

Example:

if (ssh_version(SSH_VERSION_INT(0,2,1)) == NULL) {
  fprintf(stderr, "libssh version is too old!\n");
  exit(1);
}

if (debug) {
  printf("libssh %s\n", ssh_version(0));
}

Parameters:

req_version

The version required.

Returns:

If the version of libssh is newer than the version required it will return a version string. NULL if the version is older.

char*
ssh_path_expand_tilde(const char* d)

Expand a directory starting with a tilde ‘~’.

Parameters:

d

The directory to expand.

Returns:

The expanded directory, NULL on error.

int
ssh_timeout_update(
    struct ssh_timestamp* ts,
    int timeout
)

updates a timeout value so it reflects the remaining time

Parameters:

ts

pointer to an existing timestamp

timeout

timeout in milliseconds. Negative values mean infinite timeout

Returns:

remaining time in milliseconds, 0 if elapsed, -1 if never.