Code Comments
Programming Forum and web based access to our favorite programming groups.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.
Post Follow-up to this message<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
Post Follow-up to this message> 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().
Post Follow-up to this message<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
Post Follow-up to this message>>>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.
Post Follow-up to this messagephil_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.
Post Follow-up to this messagephil_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.
Post Follow-up to this message> 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.
Post Follow-up to this messagephil_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.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.