For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > April 2005 > Right way to offer a login prompt inside pty









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Right way to offer a login prompt inside pty
phil_gg04@treefic.com

2005-04-14, 3:58 pm

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.

Fletcher Glenn

2005-04-14, 8:58 pm


<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


phil_gg04@treefic.com

2005-04-14, 8:58 pm

> 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().

Fletcher Glenn

2005-04-15, 3:58 am


<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


phil_gg04@treefic.com

2005-04-15, 3:58 am

>>>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.

Matthias Buelow

2005-04-15, 3:59 pm

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.
Chuck Dillon

2005-04-15, 3:59 pm

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.
phil_gg04@treefic.com

2005-04-15, 3:59 pm

> 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.

Matthias Buelow

2005-04-15, 8:58 pm

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.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com