| 1 |
root |
1.1 |
=head1 NAME |
| 2 |
|
|
|
| 3 |
root |
1.2 |
libptytty - OS independent and secure pty/tty and utmp/wtmp/lastlog handling |
| 4 |
root |
1.1 |
|
| 5 |
|
|
=head1 SYNOPSIS |
| 6 |
|
|
|
| 7 |
root |
1.9 |
cc ... -lptytty |
| 8 |
|
|
|
| 9 |
|
|
#include <libptytty.h> |
| 10 |
|
|
|
| 11 |
|
|
|
| 12 |
|
|
// C++ |
| 13 |
|
|
ptytty *pty = ptytty::create (); |
| 14 |
|
|
|
| 15 |
|
|
if (!pty->get ()) |
| 16 |
|
|
// error allocating pty |
| 17 |
|
|
|
| 18 |
|
|
if (we want utmp) |
| 19 |
|
|
pty->login (process_pid, 0, "remote.host"); |
| 20 |
|
|
else if (we want utmp AND wtmp/lastlog) |
| 21 |
|
|
pty->login (process_pid, 1, "remote.host"); |
| 22 |
|
|
|
| 23 |
|
|
// we are done with it |
| 24 |
|
|
delete pty; |
| 25 |
|
|
|
| 26 |
|
|
|
| 27 |
|
|
// C |
| 28 |
|
|
PTYTTY pty = ptytty_create (); |
| 29 |
|
|
|
| 30 |
|
|
if (!ptytty_get (pty)) |
| 31 |
|
|
// error allocating pty |
| 32 |
|
|
|
| 33 |
|
|
if (we want utmp) |
| 34 |
|
|
ptytty_login (pty, process_pid, 0, "remote.host"); |
| 35 |
|
|
else if (we want utmp AND wtmp/lastlog) |
| 36 |
|
|
ptytty_login (pty, process_pid, 1, "remote.host"); |
| 37 |
|
|
|
| 38 |
|
|
// we are done with it |
| 39 |
|
|
ptytty_delete (pty); |
| 40 |
|
|
|
| 41 |
|
|
|
| 42 |
root |
1.1 |
|
| 43 |
|
|
=head1 DESCRIPTION |
| 44 |
|
|
|
| 45 |
root |
1.9 |
Libptytty is a small library that offers pseudo-tty management in an |
| 46 |
|
|
OS-independent way. It was created out of frustration over the many |
| 47 |
|
|
differences of pty/tty handling in different operating systems for the use |
| 48 |
|
|
inside C<rxvt-unicode>. |
| 49 |
|
|
|
| 50 |
|
|
In addition to offering mere pty/tty management, it also offers session |
| 51 |
|
|
database support (utmp and optional wtmp/lastlog updates for login |
| 52 |
|
|
shells). |
| 53 |
|
|
|
| 54 |
|
|
It also supports fork'ing after startup and dropping privileges in the |
| 55 |
|
|
calling process, so in case the calling process gets compromised by the |
| 56 |
|
|
user starting the program there is less to gain, as only the helper |
| 57 |
|
|
process runs with privileges (e.g. setuid/setgid), which reduces the area |
| 58 |
|
|
of attack immensely. |
| 59 |
|
|
|
| 60 |
|
|
Libptytty is written in C++, but it also offers a C-only API. |
| 61 |
root |
1.6 |
|
| 62 |
root |
1.4 |
=head1 SECURITY CONSIDERATIONS |
| 63 |
root |
1.3 |
|
| 64 |
ayin |
1.5 |
I<< B<It is of paramount importance that you at least read the following |
| 65 |
root |
1.4 |
paragraph!> >> |
| 66 |
|
|
|
| 67 |
|
|
If you are a typical terminal-like program that just wants one or more |
| 68 |
root |
1.8 |
ptys, you should call the C<ptytty::init ()> method (C: C<ptytty_init ()> |
| 69 |
root |
1.7 |
function) as the very first thing in your program: |
| 70 |
root |
1.4 |
|
| 71 |
|
|
int main (int argc, char *argv[]) |
| 72 |
|
|
{ |
| 73 |
|
|
// do nothing here |
| 74 |
|
|
ptytty::init (); |
| 75 |
root |
1.7 |
// in C: ptytty_init (); |
| 76 |
root |
1.4 |
|
| 77 |
|
|
// initialise, parse arguments, etc. |
| 78 |
|
|
} |
| 79 |
|
|
|
| 80 |
|
|
This checks wether the program runs setuid or setgid. If yes then it will |
| 81 |
|
|
fork a helper process and drop privileges. |
| 82 |
|
|
|
| 83 |
root |
1.6 |
Some programs need finer control over if and when this helper process |
| 84 |
|
|
is started, and if and how to drop privileges. For those programs, the |
| 85 |
|
|
methods C<ptytty::use_helper> and C<ptytty::drop_privileges> are more |
| 86 |
|
|
useful. |
| 87 |
|
|
|
| 88 |
root |
1.7 |
=head1 C++ INTERFACE: THE ptytty CLASS |
| 89 |
root |
1.6 |
|
| 90 |
|
|
=head2 STATIC METHODS |
| 91 |
|
|
|
| 92 |
|
|
=over 4 |
| 93 |
|
|
|
| 94 |
|
|
=item ptytty::init () |
| 95 |
|
|
|
| 96 |
|
|
The default way to initialise libptytty. Must be called imemdiately as |
| 97 |
|
|
the first thing in the C<main> function, or earlier e.g. during static |
| 98 |
|
|
construction time. The earlier, the better. |
| 99 |
|
|
|
| 100 |
|
|
This method checks wether the program runs with setuid/setgid permissions |
| 101 |
|
|
and, if yes, spawns a helper process for pty/tty management. IT then |
| 102 |
|
|
drops the privileges completely, so the actual program runs without |
| 103 |
|
|
setuid/setgid privileges. |
| 104 |
|
|
|
| 105 |
|
|
=item ptytty::use_helper () |
| 106 |
|
|
|
| 107 |
root |
1.7 |
Tries to start a helper process that retains privileges even when the |
| 108 |
|
|
calling process does not. This is usually called from C<ptytty::init> when |
| 109 |
|
|
it detects that the program is running setuid or setgid, but can be called |
| 110 |
|
|
manually if it is inconvinient to drop privileges at startup, or when |
| 111 |
|
|
you are not running setuid/setgid but want to drop privileges (e.g. when |
| 112 |
|
|
running as a root-started daemon). |
| 113 |
|
|
|
| 114 |
|
|
This method will try not to start more than one helper process. The same |
| 115 |
|
|
helper process cna usually be used form the process starting it an all its |
| 116 |
|
|
fork'ed (not exec'ed) children |
| 117 |
root |
1.6 |
|
| 118 |
|
|
=item ptytty::drop_privileges () |
| 119 |
|
|
|
| 120 |
|
|
Drops privileges completely, i.e. sets real, effective and saved user id |
| 121 |
|
|
to the real user id. Also aborts if this cnanot be achieved. Useful to |
| 122 |
|
|
make sure that the process doesn't run with special privileges. |
| 123 |
|
|
|
| 124 |
|
|
=item bool success = ptytty::send_fd (int socket, int fd) |
| 125 |
|
|
|
| 126 |
|
|
Utility method to send a file descriptor over a unix domain |
| 127 |
|
|
socket. Returns true if successful, false otherwise. This method is only |
| 128 |
|
|
exposed for your convinience and is not required for normal operation. |
| 129 |
|
|
|
| 130 |
|
|
=item int fd = ptytty::recv_fd (int socket) |
| 131 |
|
|
|
| 132 |
|
|
Utility method to receive a file descriptor over a unix domain |
| 133 |
|
|
socket. Returns the fd if sucecssful and C<-1> otherwise. This method |
| 134 |
|
|
is only exposed for your convinience and is not required for normal |
| 135 |
|
|
operation. |
| 136 |
root |
1.4 |
|
| 137 |
root |
1.6 |
=item ptytty *pty = ptytty::create () |
| 138 |
root |
1.3 |
|
| 139 |
root |
1.6 |
Creates new ptytty object. Creation does not yet do anything besides |
| 140 |
|
|
allocating the structure. |
| 141 |
|
|
|
| 142 |
|
|
A static method is used because the actual ptytty implementation can |
| 143 |
|
|
differ at runtime, so you need a dynamic object creation facility. |
| 144 |
|
|
|
| 145 |
|
|
=back |
| 146 |
|
|
|
| 147 |
root |
1.7 |
|
| 148 |
root |
1.6 |
=head2 DYNAMIC/SESSION-RELATED DATA MEMBERS AND METHODS |
| 149 |
root |
1.1 |
|
| 150 |
|
|
=over 4 |
| 151 |
|
|
|
| 152 |
root |
1.6 |
=item int pty_fd = pty->pty |
| 153 |
|
|
|
| 154 |
|
|
=item int tty_fd = pty->tty |
| 155 |
|
|
|
| 156 |
|
|
These members contain the pty and tty file descriptors, respectively. They |
| 157 |
|
|
initially contain C<-1> until a successful to C<ptytty::get>. |
| 158 |
|
|
|
| 159 |
|
|
=item bool success = pty->get () |
| 160 |
|
|
|
| 161 |
|
|
Tries to find, allocate and initialise a new pty/tty pair. Returns C<true> |
| 162 |
|
|
when successful. |
| 163 |
|
|
|
| 164 |
|
|
=item pty->login (int cmd_pid, bool login_shell, const char *hostname) |
| 165 |
|
|
|
| 166 |
|
|
Creates an entry in the systems session database(s) (utmp, wtmp, lastlog). |
| 167 |
|
|
C<cmd_pid> must be the pid of the process representing the session |
| 168 |
|
|
(such as the login shell), C<login_shell> defines wether the session is |
| 169 |
|
|
associated with a login, which influences wether wtmp and lastlog entries |
| 170 |
|
|
are created, and C<hostname> should identify the "hostname" the user logs |
| 171 |
|
|
in from, which often is the value of the C<DISPLAY> variable or tty line |
| 172 |
|
|
in case of local logins. |
| 173 |
|
|
|
| 174 |
|
|
Calling this method is optional. A session starts at the time of the login |
| 175 |
|
|
call and extends until the ptytty object is destroyed. |
| 176 |
|
|
|
| 177 |
|
|
=item pty->close_tty () |
| 178 |
|
|
|
| 179 |
|
|
Closes the tty. Useful after forking in the parent/pty process. |
| 180 |
|
|
|
| 181 |
|
|
=item bool success = pty->make_controlling_tty () |
| 182 |
|
|
|
| 183 |
|
|
Tries to make the pty/tty pair the controlling terminal of the current |
| 184 |
|
|
process. Useful after forking in the child/tty process. |
| 185 |
|
|
|
| 186 |
|
|
=item pty->set_utf8_mode (bool on) |
| 187 |
root |
1.1 |
|
| 188 |
root |
1.6 |
On systems supporting special UTF-8 line disciplines (e.g. Linux), tries |
| 189 |
|
|
to enable it for the given pty. Can be called at any time to change the |
| 190 |
|
|
mode. |
| 191 |
root |
1.1 |
|
| 192 |
|
|
=back |
| 193 |
|
|
|
| 194 |
root |
1.7 |
|
| 195 |
|
|
=head1 C INTERFACE: THE ptytty FAMILY OF FUNCTIONS |
| 196 |
|
|
|
| 197 |
|
|
=over 4 |
| 198 |
|
|
|
| 199 |
|
|
=item ptytty_init () |
| 200 |
|
|
|
| 201 |
|
|
See C<ptytty::init ()>. |
| 202 |
|
|
|
| 203 |
|
|
=item PTYTTY ptytty_create () |
| 204 |
|
|
|
| 205 |
|
|
Creates a new opaque PTYTTY object and returns it. Do not try to access it |
| 206 |
|
|
in any way excecp by testing it for truthness (e.g. C<if (pty) ....>). See |
| 207 |
|
|
C<ptytty::create ()>. |
| 208 |
|
|
|
| 209 |
|
|
=item int ptytty_pty (PTYTTY ptytty) |
| 210 |
|
|
|
| 211 |
|
|
Return the pty file descriptor. See C<< pty->pty >>. |
| 212 |
|
|
|
| 213 |
|
|
=item int ptytty_tty (PTYTTY ptytty) |
| 214 |
|
|
|
| 215 |
|
|
Return the tty file descriptor. See C<< pty->tty >>. |
| 216 |
|
|
|
| 217 |
|
|
=item void ptytty_delete (PTYTTY ptytty) |
| 218 |
|
|
|
| 219 |
|
|
Destroys the PTYTTY object, freeing the pty/tty pair and cleaning up the |
| 220 |
|
|
utmp/wtmp/lastlog databases, if initialised/used. Same as C<delete pty> in |
| 221 |
|
|
C++. |
| 222 |
|
|
|
| 223 |
|
|
=item int ptytty_get (PTYTTY ptytty) |
| 224 |
|
|
|
| 225 |
|
|
See C<< pty->get >>, returns 0 in case of an error, non-zero otherwise. |
| 226 |
|
|
|
| 227 |
|
|
=item void ptytty_login (PTYTTY ptytty, int cmd_pid, bool login_shell, const char *hostname) |
| 228 |
|
|
|
| 229 |
|
|
See C<< pty->login >>. |
| 230 |
|
|
|
| 231 |
|
|
=item void ptytty_close_tty (PTYTTY ptytty) |
| 232 |
|
|
|
| 233 |
|
|
See C<< pty->close_tty >>. |
| 234 |
|
|
|
| 235 |
|
|
=item int ptytty_make_controlling_tty (PTYTTY ptytty) |
| 236 |
|
|
|
| 237 |
|
|
See C<< pty->make_controlling_tty >>. |
| 238 |
|
|
|
| 239 |
|
|
=item void ptytty_set_utf8_mode (PTYTTY ptytty, int on) |
| 240 |
|
|
|
| 241 |
|
|
See C<< pty->set_utf8_mode >>. |
| 242 |
|
|
|
| 243 |
|
|
=item void ptytty_drop_privileges () |
| 244 |
|
|
|
| 245 |
|
|
See C<< ptytty::drop_privileges >>. |
| 246 |
|
|
|
| 247 |
|
|
=item void ptytty_use_helper () |
| 248 |
|
|
|
| 249 |
|
|
See C<< ptytty::use_helper >>. |
| 250 |
|
|
|
| 251 |
|
|
=back |
| 252 |
|
|
|
| 253 |
|
|
|
| 254 |
root |
1.1 |
=head1 BUGS |
| 255 |
|
|
|
| 256 |
|
|
You kiddin'? |
| 257 |
|
|
|
| 258 |
|
|
=head1 AUTHORS |
| 259 |
|
|
|
| 260 |
|
|
Emanuele Giaquinta L<< <e.giaquinta@glauco.it> >>, Marc Alexander Lehmann |
| 261 |
|
|
L<< <rxvt-unicode@schmorp.de> >>. |