LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-28-2005, 06:07 AM   #1
hari_sasidharan
LQ Newbie
 
Registered: Jan 2005
Posts: 4

Rep: Reputation: 0
Unable to ssh in this shell


This is the code for a shell
But iam unable to ssh

Tried googling but did not find any useful tips

Code:
void
set_keypress (int fd)
{

  struct termios new_settings;
  if (tcgetattr (fd, &new_settings) < 0)
    {
      perror ("error getting the attributes for terminal ");
      exit (-1);
    }
  new_settings.c_lflag &= ~ICANON;
  new_settings.c_cc[VTIME] = 0;
  new_settings.c_cc[VMIN] = 1;
  if (tcsetattr (fd, TCSANOW, &new_settings) < 0)
    {
      perror ("error setting the attributes for terminal  ");
      exit (-1);
    }
  return;
}

int
open_pty_pair (int *amaster, int *aslave)
{
  int master, slave;
  char *name;
  master = getpt ();
  if (master < 0)
    {
      perror ("The master pty couldn't be opend");
      return -1;
    }
  if (grantpt (master) < 0 || unlockpt (master) < 0)
    {
      perror ("Grant pt failed ");
      close (master);
      return -1;
    }
  name = ptsname (master);
  if (name == NULL)
    {
      perror ("Error getting the master name");
      close (master);
      return -1;
    }
  slave = open (name, O_RDWR);
  if (slave == -1)
    {
      perror ("Error opening the slave");
      close (master);
      return -1;
    }
  if (isastream (slave))
    {

      if (ioctl (slave, I_PUSH, "ptem") < 0
	  || ioctl (slave, I_PUSH, "ldterm") < 0)
	{
	  perror ("Streams file : ioctl failed");
	  close (master);
	  close (slave);
	  return -1;
	}
    }
  *amaster = master;
  *aslave = slave;
  return 1;
}

int
main ()
{
  int *amaster = malloc (4), *aslave = malloc (4);
  if (open_pty_pair (amaster, aslave) < 0)
    {
      printf ("Allocation of pseudo terminal failed");
      exit (0);
    }
  int pid = fork ();
  if (!pid)
    {
      close (*amaster);
      dup2 (*aslave, 0);
      dup2 (*aslave, 1);
      dup2 (*aslave, 2);
      execl ("/bin/bash", "/bin/bash", (char *) 0);
    }
  else
    {
      close (*aslave);
      set_keypress (*amaster);
      struct termios new_settings;
      if (tcgetattr (0, &new_settings) < 0)
	{
	  perror ("Error getting the attributes for terminal 0");
	  exit (0);
	}
      new_settings.c_lflag &= (~ECHO);
      if (tcsetattr (0, TCSANOW, &new_settings) < 0)
	{
	  perror ("Error unsetting echo for terminal 0");
	  exit (0);
	}

      int flags;
      if ((flags = fcntl (*amaster, F_GETFL, 0)) < 0)
	{
	  perror ("fcntl:getfl failed\n");
	  exit (-1);
	}
      flags |= O_NONBLOCK;
      if (fcntl (*amaster, F_SETFL, flags) < 0)
	{
	  perror ("Fcntl:setfl failed\n");
	  exit (-1);
	}

      int ppid = fork ();
      if (!ppid)
	{
	  int ch;
	  set_keypress (0);
	  while (1)
	    {
	      ch = fgetc (stdin);
	      if (ch > 0)
		{
		  if (write (*amaster, &ch, 1) < 0)
		    {
		      perror ("error writing to the master ");
		    }
		}
	    }
	}
      else
	{
	  int len;
	  char buffer;
	  dup2 (*amaster, 0);
	  while (1)
	    {
	      if ((buffer = fgetc (stdin)) != -1)
		{
		  fprintf (stdout, "%c", buffer);
		}
	    }
	}
    }
  waitpid (pid, NULL, 0);
  return 0;
}

Last edited by hari_sasidharan; 12-29-2005 at 02:51 AM.
 
Old 12-28-2005, 06:51 AM   #2
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
1) Code is best posted in [_code]...[_/code] blocks so it's actually readable.
2) What errors are you getting?
3) What language is that?
 
Old 12-29-2005, 02:53 AM   #3
hari_sasidharan
LQ Newbie
 
Registered: Jan 2005
Posts: 4

Original Poster
Rep: Reputation: 0
This is the c code for a shell
After compiling and executing the code i get a shell..... i am able to do most of the things including telnetting to a system
but when i ssh i am not probed for passwd
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
unable to change root shell sandin Slackware 3 04-17-2005 08:33 AM
Unable to get shell when using SSH/CHROOT/PAM_TACPLUS slug420 Linux - Security 0 11-16-2004 09:32 AM
ssh connetback shell? tubbay Linux - Software 1 06-14-2004 01:15 PM
SSH using shell scripts Alek Linux - General 2 08-28-2003 02:52 PM
Ssh shell Programming DenZ Linux - Security 4 07-08-2003 02:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration