SSH_SESSION *ssh_bind_accept(SSH_BIND *ssh_bind){ SSH_SESSION *session; PRIVATE_KEY *dsa=NULL, *rsa=NULL; if(ssh_bind->bindfd<0){ ssh_set_error(ssh_bind,SSH_FATAL,"Can't accept new clients on a " "not bound socket."); return NULL; } if(!ssh_bind->options->dsakey && !ssh_bind->options->rsakey){ ssh_set_error(ssh_bind,SSH_FATAL,"DSA or RSA host key file must be set before accept()"); return NULL; } if(ssh_bind->options->dsakey){ dsa=_privatekey_from_file(ssh_bind,ssh_bind->options->dsakey,TYPE_DSS); if(!dsa) return NULL; ssh_say(2,"Dsa private key read successfuly\n"); } if(ssh_bind->options->rsakey){ rsa=_privatekey_from_file(ssh_bind,ssh_bind->options->rsakey,TYPE_RSA); if(!rsa){ if(dsa) private_key_free(dsa); return NULL; } ssh_say(2,"RSA private key read successfuly\n"); } int fd=accept(ssh_bind->bindfd,NULL,NULL); if(fd<0){ ssh_set_error(ssh_bind,SSH_FATAL,"Accepting a new connection: %s", strerror(errno)); if(dsa) private_key_free(dsa); if(rsa) private_key_free(rsa); return NULL; } session=ssh_new(); session->server=1; session->version=2; session->fd=fd; session->options=ssh_options_copy(ssh_bind->options); session->dsa_key=dsa; session->rsa_key=rsa; return session; }
SSH_SESSION *ssh_bind_accept(SSH_BIND *ssh_bind) { SSH_SESSION *session; PRIVATE_KEY *dsa = NULL; PRIVATE_KEY *rsa = NULL; int fd = -1; if (ssh_bind->bindfd < 0) { ssh_set_error(ssh_bind, SSH_FATAL, "Can't accept new clients on a not bound socket."); return NULL; } if (ssh_bind->options->dsakey == NULL || ssh_bind->options->rsakey == NULL) { ssh_set_error(ssh_bind, SSH_FATAL, "DSA or RSA host key file must be set before accept()"); return NULL; } if (ssh_bind->options->dsakey) { dsa = _privatekey_from_file(ssh_bind, ssh_bind->options->dsakey, TYPE_DSS); if (dsa == NULL) { return NULL; } } if (ssh_bind->options->rsakey) { rsa = _privatekey_from_file(ssh_bind, ssh_bind->options->rsakey, TYPE_RSA); if (rsa == NULL) { privatekey_free(dsa); return NULL; } } fd = accept(ssh_bind->bindfd, NULL, NULL); if (fd < 0) { ssh_set_error(ssh_bind, SSH_FATAL, "Accepting a new connection: %s", strerror(errno)); privatekey_free(dsa); privatekey_free(rsa); return NULL; } session = ssh_new(); if (session == NULL) { ssh_set_error(ssh_bind, SSH_FATAL, "Not enough space"); privatekey_free(dsa); privatekey_free(rsa); return NULL; } session->server = 1; session->version = 2; session->options = ssh_options_copy(ssh_bind->options); if (session->options == NULL) { ssh_set_error(ssh_bind, SSH_FATAL, "No space left"); privatekey_free(dsa); privatekey_free(rsa); ssh_cleanup(session); return NULL; } ssh_socket_free(session->socket); session->socket = ssh_socket_new(session); if (session->socket == NULL) { privatekey_free(dsa); privatekey_free(rsa); ssh_cleanup(session); return NULL; } ssh_socket_set_fd(session->socket,fd); session->dsa_key = dsa; session->rsa_key = rsa; return session; }
int ssh_bind_listen(ssh_bind sshbind) { const char *host; socket_t fd; sshbind->dsa = NULL; sshbind->rsa = NULL; if (ssh_init() < 0) { ssh_set_error(sshbind, SSH_FATAL, "ssh_init() failed"); return -1; } if (sshbind->dsakey) { if (sshbind->dsakey == NULL && sshbind->rsakey == NULL) { ssh_set_error(sshbind, SSH_FATAL, "DSA or RSA host key file must be set before listen()"); return SSH_ERROR; } sshbind->dsa = _privatekey_from_file( sshbind, sshbind->dsakey, SSH_KEYTYPE_DSS ); if (sshbind->dsa == NULL) { return -1; } } if (sshbind->rsakey) { sshbind->rsa = _privatekey_from_file( sshbind, sshbind->rsakey, SSH_KEYTYPE_RSA ); if (sshbind->rsa == NULL) { privatekey_free(sshbind->dsa); return -1; } } host = sshbind->bindaddr; if (host == NULL) { host = "0.0.0.0"; } fd = bind_socket(sshbind, host, sshbind->bindport); if (fd == SSH_INVALID_SOCKET) { privatekey_free(sshbind->dsa); privatekey_free(sshbind->rsa); return -1; } sshbind->bindfd = fd; if (listen(fd, 10) < 0) { ssh_set_error(sshbind, SSH_FATAL, "Listening to socket %d: %s", fd, strerror(errno)); close(fd); privatekey_free(sshbind->dsa); privatekey_free(sshbind->rsa); return -1; } return 0; }
int ssh_bind_accept(ssh_bind sshbind, ssh_session session) { ssh_private_key dsa = NULL; ssh_private_key rsa = NULL; socket_t fd = SSH_INVALID_SOCKET; int i; if (sshbind->bindfd == SSH_INVALID_SOCKET) { ssh_set_error(sshbind, SSH_FATAL, "Can't accept new clients on a not bound socket."); return SSH_ERROR; } if(session == NULL){ ssh_set_error(sshbind, SSH_FATAL,"session is null"); return SSH_ERROR; } if (sshbind->dsakey == NULL && sshbind->rsakey == NULL) { ssh_set_error(sshbind, SSH_FATAL, "DSA or RSA host key file must be set before accept()"); return SSH_ERROR; } if (sshbind->dsakey) { dsa = _privatekey_from_file(sshbind, sshbind->dsakey, SSH_KEYTYPE_DSS); if (dsa == NULL) { return SSH_ERROR; } } if (sshbind->rsakey) { rsa = _privatekey_from_file(sshbind, sshbind->rsakey, SSH_KEYTYPE_RSA); if (rsa == NULL) { privatekey_free(dsa); return SSH_ERROR; } } fd = accept(sshbind->bindfd, NULL, NULL); if (fd == SSH_INVALID_SOCKET) { ssh_set_error(sshbind, SSH_FATAL, "Accepting a new connection: %s", strerror(errno)); privatekey_free(dsa); privatekey_free(rsa); return SSH_ERROR; } session->server = 1; session->version = 2; /* copy options */ for (i = 0; i < 10; ++i) { if (sshbind->wanted_methods[i]) { session->wanted_methods[i] = strdup(sshbind->wanted_methods[i]); if (session->wanted_methods[i] == NULL) { privatekey_free(dsa); privatekey_free(rsa); return SSH_ERROR; } } } if (sshbind->bindaddr == NULL) session->bindaddr = NULL; else { SAFE_FREE(session->bindaddr); session->bindaddr = strdup(sshbind->bindaddr); if (session->bindaddr == NULL) { privatekey_free(dsa); privatekey_free(rsa); return SSH_ERROR; } } session->log_verbosity = sshbind->log_verbosity; ssh_socket_free(session->socket); session->socket = ssh_socket_new(session); if (session->socket == NULL) { /* perhaps it may be better to copy the error from session to sshbind */ ssh_set_error_oom(sshbind); privatekey_free(dsa); privatekey_free(rsa); return SSH_ERROR; } ssh_socket_set_fd(session->socket, fd); ssh_socket_get_poll_handle_out(session->socket); session->dsa_key = dsa; session->rsa_key = rsa; return SSH_OK; }