Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Right way to offer a login prompt inside pty
Dear Unix Experts,

I have been developing a "terminal emulator on a web page" called
Anyterm.  It uses an apache module and Javascript to give you shell
access to your server from almost anywhere, even if there are firewalls
in the way and where an ssh client can't be installed (internet cafes
etc).  Read more at http://chezphil.org/anyterm/.

Today's problem is how to present a login: prompt inside the terminal
and to correctly set up the user's environment.  This seems to be more
complex than I had hoped.  My code uses forkpty() which creates a new
pseudo-terminal and runs a process with its slave side connected to
stdin/stdout.  I had hoped that I could simply run /bin/login, but this
doesn't work: it says that it must be run from getty.  I think that the
only reason for this is utmp.  So perhaps I should run getty?  But
getty takes a terminal device as a command-line parameter, rather than
connecting to stdin/stdout, and may want to do serial line stuff.

I have had a quick look at the telnetd source code and it seems rather
complicated.  Surely this has been done before.  Can someone point me
at an example?

Currently I am using the following script:

#!/bin/sh
while /bin/true
do
echo -n "login: "
if read U
then
/bin/true
else
exit 0
fi
su - "$U"
echo
done

This superficially does what I want, but it doesn't do anything with
utmp so who doesn't report the login, and peculiar things go wrong
inside the session e.g. "man" doesn't work properly (it fails when it
spawns g(un)zip) and sometimes ctrl-C and ctrl-Z don't work.

I suppose that an equivalent question is " how do I display a login:
prompt inside an xterm using 'xterm -e ....' ? ".

I'm developing this on Linux but would like the code to be as general
as possible.

Any suggestions would be much appreciated.

Regards,

Phil Endecott.


Report this thread to moderator Post Follow-up to this message
Old Post
phil_gg04@treefic.com
04-14-05 08:58 PM


Re: Right way to offer a login prompt inside pty
<phil_gg04@treefic.com> wrote in message
news:1113479656.038980.183450@g14g2000cwa.googlegroups.com...
> Dear Unix Experts,
>
> I have been developing a "terminal emulator on a web page" called
> Anyterm.  It uses an apache module and Javascript to give you shell
> access to your server from almost anywhere, even if there are firewalls
> in the way and where an ssh client can't be installed (internet cafes
> etc).  Read more at http://chezphil.org/anyterm/.
>
> Today's problem is how to present a login: prompt inside the terminal
> and to correctly set up the user's environment.  This seems to be more
> complex than I had hoped.  My code uses forkpty() which creates a new
> pseudo-terminal and runs a process with its slave side connected to
> stdin/stdout.  I had hoped that I could simply run /bin/login, but this
> doesn't work: it says that it must be run from getty.  I think that the
> only reason for this is utmp.  So perhaps I should run getty?  But
> getty takes a terminal device as a command-line parameter, rather than
> connecting to stdin/stdout, and may want to do serial line stuff.
>
> I have had a quick look at the telnetd source code and it seems rather
> complicated.  Surely this has been done before.  Can someone point me
> at an example?
>
> Currently I am using the following script:
>
> #!/bin/sh
> while /bin/true
> do
>  echo -n "login: "
>  if read U
>  then
>    /bin/true
>  else
>    exit 0
>  fi
>  su - "$U"
>  echo
> done
>
> This superficially does what I want, but it doesn't do anything with
> utmp so who doesn't report the login, and peculiar things go wrong
> inside the session e.g. "man" doesn't work properly (it fails when it
> spawns g(un)zip) and sometimes ctrl-C and ctrl-Z don't work.
>
> I suppose that an equivalent question is " how do I display a login:
> prompt inside an xterm using 'xterm -e ....' ? ".
>
> I'm developing this on Linux but would like the code to be as general
> as possible.
>
> Any suggestions would be much appreciated.
>
> Regards,
>
> Phil Endecott.
>

Security considerations cause most systems to absolutely insist that a login
originate
from a terminal or a pseudo-tty.  If you already are logged into the system,
it's trivial to start a pty master-slave pair so that login will work.

--

Fletcher Glenn



Report this thread to moderator Post Follow-up to this message
Old Post
Fletcher Glenn
04-15-05 01:58 AM


Re: Right way to offer a login prompt inside pty
> If you already are logged into the system,
> it's trivial to start a pty master-slave pair so that login will
work.

Good, I was hoping that it would be easy!  But I haven't worked out
what this trivial method is.  Please can you explain?

Thanks.

--Phil.

p.s. I have resolved the problems with "man" and ctrl-C mentioned in my
original post, it's all to do with setting the right signal behaviour
between forkpty() and exec().


Report this thread to moderator Post Follow-up to this message
Old Post
phil_gg04@treefic.com
04-15-05 01:58 AM


Re: Right way to offer a login prompt inside pty
<phil_gg04@treefic.com> wrote in message
news:1113512128.759553.270510@g14g2000cwa.googlegroups.com... 
> work.
>
> Good, I was hoping that it would be easy!  But I haven't worked out
> what this trivial method is.  Please can you explain?
>
> Thanks.
>
> --Phil.
>
> p.s. I have resolved the problems with "man" and ctrl-C mentioned in my
> original post, it's all to do with setting the right signal behaviour
> between forkpty() and exec().
>

You could learn a lot by visiting www.freebsd.org.  This website
has the source to many UNIX utilities that offers code hints
to just the sort of thing you want.  Try looking at rlogind for
example.

--

Fletcher Glenn



Report this thread to moderator Post Follow-up to this message
Old Post
Fletcher Glenn
04-15-05 08:58 AM


Re: Right way to offer a login prompt inside pty
>>>it's trivial to start a pty master-slave pair so that login will
work 
> Try looking at [the source to] rlogind

Yes.  I've already looked at telnetd, which is similar.  I was really
hoping for an easier way to do it.  Oh well, I guess different people
have different ideas of "trivial"!

--Phil.


Report this thread to moderator Post Follow-up to this message
Old Post
phil_gg04@treefic.com
04-15-05 08:58 AM


Re: Right way to offer a login prompt inside pty
phil_gg04@treefic.com writes:
 
> work 
>
> Yes.  I've already looked at telnetd, which is similar.  I was really
> hoping for an easier way to do it.  Oh well, I guess different people
> have different ideas of "trivial"!

Some systems (like *BSD and Gnu/Linux but not Solaris or HP-UX, for
example) have the utility functions openpty, forkpty and login_tty,
which might be useful.

mkb.

Report this thread to moderator Post Follow-up to this message
Old Post
Matthias Buelow
04-15-05 08:59 PM


Re: Right way to offer a login prompt inside pty
phil_gg04@treefic.com wrote:
 
>
> work.
>
> Good, I was hoping that it would be easy!  But I haven't worked out
> what this trivial method is.  Please can you explain?
>
> Thanks.
>
> --Phil.
>
> p.s. I have resolved the problems with "man" and ctrl-C mentioned in my
> original post, it's all to do with setting the right signal behaviour
> between forkpty() and exec().
>

You might want to look at expect and/or libexpect at
http://expect.nist.gov.  If abstracts away the pty details including
many portability problems.

-- ced

--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.

Report this thread to moderator Post Follow-up to this message
Old Post
Chuck Dillon
04-15-05 08:59 PM


Re: Right way to offer a login prompt inside pty
> openpty, forkpty and login_tty might be useful.

Yes, I'm using those.  I'm actually using the ROTE library which calls
forkpty().  The hard bit is the next step.
(BTW, can anyone decode what "creating a new session" means in the
login_tty man page?)

Anyway, I think that I have solved the problem by hacking something
based on mingetty.

Thanks for the suggestions.

--Phil.


Report this thread to moderator Post Follow-up to this message
Old Post
phil_gg04@treefic.com
04-15-05 08:59 PM


Re: Right way to offer a login prompt inside pty
phil_gg04@treefic.com writes:

> (BTW, can anyone decode what "creating a new session" means in the
> login_tty man page?)

From APUE(*):
``A session is a collection of one or more process groups.''
Sessions usually have a single controlling process and a controlling
terminal (for example your login shell).

If you do more Unix programming, I really recommend getting a good
book that describes the Unix API.  One of my favourites is W. Richard
Stevens, "Advanced Programming in the UNIX environment"(*), which is
also recommended in the FAQ for this newsgroup, IIRC.

mkb.

Report this thread to moderator Post Follow-up to this message
Old Post
Matthias Buelow
04-16-05 01:58 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:08 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.