libptytty(3) | LIBPTYTTY | libptytty(3) |
libptytty - OS independent and secure pty/tty and utmp/wtmp/lastlog handling
cc ... -lptytty #include <libptytty.h> // C++ ptytty *pty = ptytty::create (); if (!pty->get ()) // error allocating pty if (we want utmp) pty->login (process_pid, 0, "remote.host"); else if (we want utmp AND wtmp/lastlog) pty->login (process_pid, 1, "remote.host"); // we are done with it delete pty; // C PTYTTY pty = ptytty_create (); if (!ptytty_get (pty)) // error allocating pty if (we want utmp) ptytty_login (pty, process_pid, 0, "remote.host"); else if (we want utmp AND wtmp/lastlog) ptytty_login (pty, process_pid, 1, "remote.host"); // we are done with it ptytty_delete (pty);
See also the eg/ directory, which currently contains the c-sample.c file that spawns a login shell from C using libptytty.
Libptytty is a small library that offers pseudo-tty management in an OS-independent way. It was created out of frustration over the many differences of pty/tty handling in different operating systems for the use inside "rxvt-unicode".
In addition to offering mere pty/tty management, it also offers session database support (utmp and optional wtmp/lastlog updates for login shells).
It also supports fork'ing after startup and dropping privileges in the calling process, so in case the calling process gets compromised by the user starting the program there is less to gain, as only the helper process runs with privileges (e.g. setuid/setgid), which reduces the area of attack immensely.
Libptytty is written in C++, but it also offers a C-only API.
libptytty uses "CMake" as build system. To build libptytty, install "CMake" and run the following commands from either the libptytty source directory or a separate build directory:
cmake -DCMAKE_INSTALL_PREFIX=<prefix> -DBUILD_SHARED_LIBS=ON <path/to/libptytty> cmake --build . cmake --install .
It is of paramount importance that you at least read the following paragraph!
If you write a typical terminal-like program that just wants one or more ptys, you should call the "ptytty::init ()" method (C: "ptytty_init ()" function) as the very first thing in your program:
int main (int argc, char *argv[]) { // do nothing here ptytty::init (); // in C: ptytty_init (); // initialise, parse arguments, etc. }
This checks whether the program runs setuid or setgid. If yes then it will fork a helper process and drop privileges.
Some programs need finer control over if and when this helper process is started, and if and how to drop privileges. For those programs, the methods "ptytty::use_helper" and "ptytty::drop_privileges" (and possibly "ptytty::sanitise_stdfd") are more useful.
This method calls "sanitise_stdfd" and then checks whether the program runs with setuid/setgid permissions and, if yes, spawns a helper process for pty/tty management. It then drops the privileges completely, so the actual program runs without setuid/setgid privileges.
On failure, this method throws a "ptytty_error" exception.
This method will try not to start more than one helper process. The same helper process can usually be used both from the process starting it and all its fork'ed (not exec'ed) children.
On failure, this method throws a "ptytty_error" exception.
On failure, this method throws a "ptytty_error" exception.
On failure, this method throws a "ptytty_error" exception.
A static method is used because the actual ptytty implementation can differ at runtime, so you need a dynamic object creation facility.
If the helper process is running and there is a protocol error, this method throws a "ptytty_error" exception.
Calling this method is optional. A session starts at the time of the login call and extends until the ptytty object is destroyed.
To date, libptytty has been tested on the following platforms:
You kiddin'?
Emanuele Giaquinta <emanuele.giaquinta@gmail.com>, Marc Alexander Lehmann <rxvt-unicode@schmorp.de>.
2021-07-27 | 2.0 |