ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/doc/libptytty.3.pod
Revision: 1.25
Committed: Sat Jul 24 12:41:35 2021 UTC (5 years, 2 months ago) by sf-exg
Branch: MAIN
CVS Tags: HEAD
Changes since 1.24: +10 -0 lines
Error occurred while calculating annotation data.
Log Message:
doc: add installation section

File Contents

# Content
1 =head1 NAME
2
3 libptytty - OS independent and secure pty/tty and utmp/wtmp/lastlog handling
4
5 =head1 SYNOPSIS
6
7 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 See also the F<eg/> directory, which currently contains the F<c-sample.c>
42 file that spawns a login shell from C using libptytty.
43
44 =head1 DESCRIPTION
45
46 Libptytty is a small library that offers pseudo-tty management in an
47 OS-independent way. It was created out of frustration over the many
48 differences of pty/tty handling in different operating systems for the use
49 inside C<rxvt-unicode>.
50
51 In addition to offering mere pty/tty management, it also offers session
52 database support (utmp and optional wtmp/lastlog updates for login
53 shells).
54
55 It also supports fork'ing after startup and dropping privileges in the
56 calling process, so in case the calling process gets compromised by the
57 user starting the program there is less to gain, as only the helper
58 process runs with privileges (e.g. setuid/setgid), which reduces the area
59 of attack immensely.
60
61 Libptytty is written in C++, but it also offers a C-only API.
62
63 =head1 INSTALLATION
64
65 libptytty uses C<CMake> as build system. To build libptytty, install
66 C<CMake> and run the following commands from either the libptytty
67 source directory or a separate build directory:
68
69 cmake -DCMAKE_INSTALL_PREFIX=<prefix> -DBUILD_SHARED_LIBS=ON <path/to/libptytty>
70 cmake --build .
71 cmake --install .
72
73 =head1 SECURITY CONSIDERATIONS
74
75 I<< B<It is of paramount importance that you at least read the following
76 paragraph!> >>
77
78 If you write a typical terminal-like program that just wants one or more
79 ptys, you should call the C<ptytty::init ()> method (C: C<ptytty_init ()>
80 function) as the very first thing in your program:
81
82 int main (int argc, char *argv[])
83 {
84 // do nothing here
85 ptytty::init ();
86 // in C: ptytty_init ();
87
88 // initialise, parse arguments, etc.
89 }
90
91 This checks whether the program runs setuid or setgid. If yes then it will
92 fork a helper process and drop privileges.
93
94 Some programs need finer control over if and when this helper process
95 is started, and if and how to drop privileges. For those programs, the
96 methods C<ptytty::use_helper> and C<ptytty::drop_privileges> (and possibly
97 C<ptytty::sanitise_stdfd>) are more useful.
98
99 =head1 C++ INTERFACE: THE ptytty CLASS
100
101 =head2 STATIC METHODS
102
103 =over
104
105 =item ptytty::init ()
106
107 The default way to initialise libptytty. Must be called immediately as
108 the first thing in the C<main> function, or earlier e.g. during static
109 construction time. The earlier, the better.
110
111 This method calls C<sanitise_stdfd> and then checks whether the program runs
112 with setuid/setgid permissions and, if yes, spawns a helper process for
113 pty/tty management. It then drops the privileges completely, so the actual
114 program runs without setuid/setgid privileges.
115
116 On failure, this method throws a C<ptytty_error> exception.
117
118 =item ptytty::use_helper ()
119
120 Tries to start a helper process that retains privileges even when the
121 calling process does not. This is usually called from C<ptytty::init> when
122 it detects that the program is running setuid or setgid, but can be called
123 manually if it is inconvenient to drop privileges at startup, or when
124 you are not running setuid/setgid but want to drop privileges (e.g. when
125 running as a root-started daemon).
126
127 This method will try not to start more than one helper process. The same
128 helper process can usually be used both from the process starting it and
129 all its fork'ed (not exec'ed) children.
130
131 On failure, this method throws a C<ptytty_error> exception.
132
133 =item ptytty::drop_privileges ()
134
135 Drops privileges completely, i.e. sets real, effective and saved user
136 id to the real user id. Useful to make sure that the process doesn't
137 run with special privileges.
138
139 On failure, this method throws a C<ptytty_error> exception.
140
141 =item ptytty::sanitise_stdfd ()
142
143 Checks whether file descriptors 0, 1 and 2 (stdin, stdout and stderr)
144 are valid (open) and, if not, connects them to F</dev/tty> or
145 F</dev/null> if possible. This is necessary because libptytty might
146 want to output error messages to those descriptors, which at the time
147 of outputting the error message, might be connected to something
148 unsuitable opened by the unsuspecting program itself (this can be a
149 security issue).
150
151 On failure, this method throws a C<ptytty_error> exception.
152
153 =item bool success = ptytty::send_fd (int socket, int fd)
154
155 Utility method to send a file descriptor over a unix domain
156 socket. Returns true if successful, false otherwise. This method is only
157 exposed for your convenience and is not required for normal operation.
158
159 =item int fd = ptytty::recv_fd (int socket)
160
161 Utility method to receive a file descriptor over a unix domain
162 socket. Returns the fd if successful and C<-1> otherwise. This method
163 is only exposed for your convenience and is not required for normal
164 operation.
165
166 =item ptytty *pty = ptytty::create ()
167
168 Creates new ptytty object. Creation does not yet do anything besides
169 allocating the structure.
170
171 A static method is used because the actual ptytty implementation can
172 differ at runtime, so you need a dynamic object creation facility.
173
174 =back
175
176
177 =head2 DYNAMIC/SESSION-RELATED DATA MEMBERS AND METHODS
178
179 =over
180
181 =item int pty_fd = pty->pty
182
183 =item int tty_fd = pty->tty
184
185 These members contain the pty and tty file descriptors, respectively. They
186 initially contain C<-1> until a successful call to C<ptytty::get>.
187
188 =item bool success = pty->get ()
189
190 Tries to find, allocate and initialise a new pty/tty pair. Returns C<true>
191 when successful.
192
193 If the helper process is running and there is a protocol error, this
194 method throws a C<ptytty_error> exception.
195
196 =item pty->login (int cmd_pid, bool login_shell, const char *hostname)
197
198 Creates an entry in the systems session database(s) (utmp, wtmp, lastlog).
199 C<cmd_pid> must be the pid of the process representing the session
200 (such as the login shell), C<login_shell> defines whether the session is
201 associated with a login, which influences whether wtmp and lastlog entries
202 are created, and C<hostname> should identify the "hostname" the user logs
203 in from, which often is the value of the C<DISPLAY> variable or tty line
204 in case of local logins.
205
206 Calling this method is optional. A session starts at the time of the login
207 call and extends until the ptytty object is destroyed.
208
209 =item pty->close_tty ()
210
211 Closes the tty. Useful after forking in the parent/pty process.
212
213 =item bool success = pty->make_controlling_tty ()
214
215 Tries to make the pty/tty pair the controlling terminal of the current
216 process. Useful after forking in the child/tty process.
217
218 =item pty->set_utf8_mode (bool on)
219
220 On systems supporting special UTF-8 line disciplines (e.g. Linux), this
221 tries to enable this discipline for the given pty. Can be called at any
222 time to change the mode.
223
224 =back
225
226
227 =head1 C INTERFACE: THE ptytty FAMILY OF FUNCTIONS
228
229 =over
230
231 =item ptytty_init ()
232
233 See C<ptytty::init ()>.
234
235 =item PTYTTY ptytty_create ()
236
237 Creates a new opaque PTYTTY object and returns it. Do not try to access it
238 in any way except by testing it for truthness (e.g. C<if (pty) ....>). See
239 C<ptytty::create ()>.
240
241 =item int ptytty_pty (PTYTTY ptytty)
242
243 Return the pty file descriptor. See C<< pty->pty >>.
244
245 =item int ptytty_tty (PTYTTY ptytty)
246
247 Return the tty file descriptor. See C<< pty->tty >>.
248
249 =item void ptytty_delete (PTYTTY ptytty)
250
251 Destroys the PTYTTY object, freeing the pty/tty pair and cleaning up the
252 utmp/wtmp/lastlog databases, if initialised/used. Same as C<delete pty> in
253 C++.
254
255 =item int ptytty_get (PTYTTY ptytty)
256
257 See C<< pty->get >>, returns 0 in case of an error, non-zero otherwise.
258
259 =item void ptytty_login (PTYTTY ptytty, int cmd_pid, bool login_shell, const char *hostname)
260
261 See C<< pty->login >>.
262
263 =item void ptytty_close_tty (PTYTTY ptytty)
264
265 See C<< pty->close_tty >>.
266
267 =item int ptytty_make_controlling_tty (PTYTTY ptytty)
268
269 See C<< pty->make_controlling_tty >>.
270
271 =item void ptytty_set_utf8_mode (PTYTTY ptytty, int on)
272
273 See C<< pty->set_utf8_mode >>.
274
275 =item void ptytty_drop_privileges ()
276
277 See C<< ptytty::drop_privileges >>.
278
279 =item void ptytty_use_helper ()
280
281 See C<< ptytty::use_helper >>.
282
283 =back
284
285 =head1 PORTABILITY
286
287 To date, libptytty has been tested on the following platforms:
288
289 =over
290
291 =item GNU/Linux
292
293 =item FreeBSD
294
295 =item NetBSD
296
297 =item OpenBSD
298
299 =item macOS
300
301 =item Solaris
302
303 =item AIX
304
305 =back
306
307 =head1 BUGS
308
309 You kiddin'?
310
311 =head1 AUTHORS
312
313 Emanuele Giaquinta <emanuele.giaquinta@gmail.com>, Marc Alexander Lehmann
314 <rxvt-unicode@schmorp.de>.
315