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