Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Im writing a PAM module to handle authentication (server side - sshd) and I have to know if an ssh client is a tty (interactive) or not (non interactive).

Does anyone have a clue?

share|improve this question

You could try checking for $SSH_TTY, but that isn't reliable.

$ ssh -6 fedoraplug.local
Last login: ......
-$ echo "${!SSH*}"
SSH_CLIENT SSH_CONNECTION SSH_TTY
-$ exit
$ ssh -6 fedoraplug.local 'echo "${!SSH*}"'
SSH_CLIENT SSH_CONNECTION
$ ssh -t -6 fedoraplug.local 'echo "${!SSH*}"'
SSH_CLIENT SSH_CONNECTION SSH_TTY
Connection to fedoraplug.local closed.
share|improve this answer
    
isnt that on the client side? Im on the server side (sshd) – jontra volta Jul 15 '13 at 6:36
    
Uh, no, that's done on the server. – Ignacio Vazquez-Abrams Jul 15 '13 at 6:39

On the client side of things, you can use the tty command (manpage here). Assuming the session is interactive, this command would return something like:

breakthrough@lt0:~$ tty
/dev/pts/0

However, if the client is not interactive/tty-based, the command will return:

not a tty

For actually writing a PAM module, while tty does provide some exit codes that you can use, it's unlikely that you can use the tty command directly as a syscall; instead, you should take a look at the ttyname() (or ttyname_r()) function.

You can see a sample implementation of this function here which covers some general use cases. While it should be sufficient to see if a valid ttyname was returned, best practice would be to check if the error ENOTTY was raised, indicating that the passed file descriptor does not refer to a terminal device.

share|improve this answer
    
Unless -t is passed. – Ignacio Vazquez-Abrams Jul 15 '13 at 6:39
    
@IgnacioVazquez-Abrams even a pseudo-terminal emulates a full text terminal, so one would expect even a non-interactive session invoked with this should indicate that the session is TTY regardless... – Breakthrough Jul 15 '13 at 6:46

If you are writing in C:

man isatty

DESCRIPTION The isatty() function tests whether fd is an open file descriptor referring to a terminal.

RETURN VALUE isatty() returns 1 if fd is an open file descriptor referring to a terminal; otherwise 0 is returned, and errno is set to indicate the error.

share|improve this answer
1  
While this command may answer the question, it is better to include the essential parts of the answer here and provide the link to documentation for reference. – Jawa Aug 11 '13 at 16:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .