1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-11-29 01:03:57 +03:00

options: Allocate dest in ssh_options_copy().

This commit is contained in:
Andreas Schneider
2011-08-11 12:53:02 +02:00
parent 1c30c2a510
commit a4a1af5bbf

View File

@@ -53,101 +53,116 @@
* *
* @param src The session to use to copy the options. * @param src The session to use to copy the options.
* *
* @param dest The session to copy the options to. * @param dest A pointer to store the allocated session with duplicated
* options. You have to free the memory.
* *
* @returns 0 on sucess, -1 on error with errno set. * @returns 0 on sucess, -1 on error with errno set.
* *
* @see ssh_session_connect() * @see ssh_session_connect()
*/ */
int ssh_options_copy(ssh_session src, ssh_session *dest) { int ssh_options_copy(ssh_session src, ssh_session *dest) {
ssh_session new; ssh_session new;
int i; int i;
if (src == NULL || dest == NULL || *dest == NULL) { if (src == NULL || dest == NULL) {
return -1;
}
new = *dest;
if (src->username) {
new->username = strdup(src->username);
if (new->username == NULL) {
return -1;
}
}
if (src->host) {
new->host = strdup(src->host);
if (new->host == NULL) {
return -1;
}
}
if (src->identity) {
struct ssh_iterator *it;
new->identity = ssh_list_new();
if (new->identity == NULL) {
return -1;
}
it = ssh_list_get_iterator(src->identity);
while (it) {
char *id;
int rc;
id = strdup((char *) it->data);
if (id == NULL) {
return -1; return -1;
} }
rc = ssh_list_append(new->identity, id); new = ssh_new();
if (rc < 0) { if (new == NULL) {
return -1; return -1;
}
it = it->next;
} }
}
if (src->sshdir) { if (src->username) {
new->sshdir = strdup(src->sshdir); new->username = strdup(src->username);
if (new->sshdir == NULL) { if (new->username == NULL) {
return -1; ssh_free(new);
return -1;
}
} }
}
if (src->knownhosts) { if (src->host) {
new->knownhosts = strdup(src->knownhosts); new->host = strdup(src->host);
if (new->knownhosts == NULL) { if (new->host == NULL) {
return -1; ssh_free(new);
return -1;
}
} }
}
for (i = 0; i < 10; ++i) { if (src->identity) {
if (src->wanted_methods[i]) { struct ssh_iterator *it;
new->wanted_methods[i] = strdup(src->wanted_methods[i]);
if (new->wanted_methods[i] == NULL) { new->identity = ssh_list_new();
return -1; if (new->identity == NULL) {
} ssh_free(new);
return -1;
}
it = ssh_list_get_iterator(src->identity);
while (it) {
char *id;
int rc;
id = strdup((char *) it->data);
if (id == NULL) {
ssh_free(new);
return -1;
}
rc = ssh_list_append(new->identity, id);
if (rc < 0) {
ssh_free(new);
return -1;
}
it = it->next;
}
} }
}
if(src->ProxyCommand) { if (src->sshdir) {
new->ProxyCommand = strdup(src->ProxyCommand); new->sshdir = strdup(src->sshdir);
if(new->ProxyCommand == NULL) if (new->sshdir == NULL) {
return -1; ssh_free(new);
} return -1;
new->fd = src->fd; }
new->port = src->port; }
new->common.callbacks = src->common.callbacks;
new->timeout = src->timeout;
new->timeout_usec = src->timeout_usec;
new->ssh2 = src->ssh2;
new->ssh1 = src->ssh1;
new->common.log_verbosity = src->common.log_verbosity;
new->compressionlevel = src->compressionlevel;
return 0; if (src->knownhosts) {
new->knownhosts = strdup(src->knownhosts);
if (new->knownhosts == NULL) {
ssh_free(new);
return -1;
}
}
for (i = 0; i < 10; ++i) {
if (src->wanted_methods[i]) {
new->wanted_methods[i] = strdup(src->wanted_methods[i]);
if (new->wanted_methods[i] == NULL) {
ssh_free(new);
return -1;
}
}
}
if(src->ProxyCommand) {
new->ProxyCommand = strdup(src->ProxyCommand);
if(new->ProxyCommand == NULL)
ssh_free(new);
return -1;
}
new->fd = src->fd;
new->port = src->port;
new->common.callbacks = src->common.callbacks;
new->timeout = src->timeout;
new->timeout_usec = src->timeout_usec;
new->ssh2 = src->ssh2;
new->ssh1 = src->ssh1;
new->common.log_verbosity = src->common.log_verbosity;
new->compressionlevel = src->compressionlevel;
*dest = new;
return 0;
} }
int ssh_options_set_algo(ssh_session session, int algo, int ssh_options_set_algo(ssh_session session, int algo,