aboutsummaryrefslogtreecommitdiff
path: root/src/pty.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pty.c')
-rw-r--r--src/pty.c128
1 files changed, 128 insertions, 0 deletions
diff --git a/src/pty.c b/src/pty.c
new file mode 100644
index 0000000..c888df3
--- /dev/null
+++ b/src/pty.c
@@ -0,0 +1,128 @@
+#include <pty.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <utmp.h>
+
+#include "pty.h"
+
+// callback function for SSH channel PTY request from a client
+int pty_request(ssh_session session, ssh_channel channel,
+ const char *term, int cols, int rows, int py, int px,
+ void *userdata) {
+ struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
+
+ (void) session;
+ (void) channel;
+ (void) term;
+
+ cdata->winsize->ws_row = rows;
+ cdata->winsize->ws_col = cols;
+ cdata->winsize->ws_xpixel = px;
+ cdata->winsize->ws_ypixel = py;
+
+ if (openpty(&cdata->pty_master, &cdata->pty_slave, NULL, NULL,
+ cdata->winsize) != 0) {
+ fprintf(stderr, "Failed to open pty\n");
+ return SSH_ERROR;
+ }
+ return SSH_OK;
+}
+
+// callback function for SSH channel PTY resize from a client
+int pty_resize(ssh_session session, ssh_channel channel, int cols,
+ int rows, int py, int px, void *userdata) {
+ struct channel_data_struct *cdata = (struct channel_data_struct *)userdata;
+
+ (void) session;
+ (void) channel;
+
+ cdata->winsize->ws_row = rows;
+ cdata->winsize->ws_col = cols;
+ cdata->winsize->ws_xpixel = px;
+ cdata->winsize->ws_ypixel = py;
+
+ if (cdata->pty_master != -1) {
+ return ioctl(cdata->pty_master, TIOCSWINSZ, cdata->winsize);
+ }
+
+ return SSH_ERROR;
+}
+
+int exec_pty(const char *mode, const char *command,
+ struct channel_data_struct *cdata) {
+ switch(cdata->pid = fork()) {
+ case -1:
+ close(cdata->pty_master);
+ close(cdata->pty_slave);
+ fprintf(stderr, "Failed to fork\n");
+ return SSH_ERROR;
+ case 0:
+ close(cdata->pty_master);
+ if (login_tty(cdata->pty_slave) != 0) {
+ exit(1);
+ }
+ execl("/bin/sh", "sh", mode, command, NULL);
+ exit(0);
+ default:
+ close(cdata->pty_slave);
+ /* pty fd is bi-directional */
+ cdata->child_stdout = cdata->child_stdin = cdata->pty_master;
+ }
+ return SSH_OK;
+}
+
+int exec_nopty(const char *command, struct channel_data_struct *cdata) {
+ int in[2], out[2], err[2];
+
+ /* Do the plumbing to be able to talk with the child process. */
+ if (pipe(in) != 0) {
+ goto stdin_failed;
+ }
+ if (pipe(out) != 0) {
+ goto stdout_failed;
+ }
+ if (pipe(err) != 0) {
+ goto stderr_failed;
+ }
+
+ switch(cdata->pid = fork()) {
+ case -1:
+ goto fork_failed;
+ case 0:
+ /* Finish the plumbing in the child process. */
+ close(in[1]);
+ close(out[0]);
+ close(err[0]);
+ dup2(in[0], STDIN_FILENO);
+ dup2(out[1], STDOUT_FILENO);
+ dup2(err[1], STDERR_FILENO);
+ close(in[0]);
+ close(out[1]);
+ close(err[1]);
+ /* exec the requested command. */
+ execl("/bin/sh", "sh", "-c", command, NULL);
+ exit(0);
+ }
+
+ close(in[0]);
+ close(out[1]);
+ close(err[1]);
+
+ cdata->child_stdin = in[1];
+ cdata->child_stdout = out[0];
+ cdata->child_stderr = err[0];
+
+ return SSH_OK;
+
+fork_failed:
+ close(err[0]);
+ close(err[1]);
+stderr_failed:
+ close(out[0]);
+ close(out[1]);
+stdout_failed:
+ close(in[0]);
+ close(in[1]);
+stdin_failed:
+ return SSH_ERROR;
+}