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