ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/doc/libptytty.3.pod
Revision: 1.21
Committed: Mon May 24 13:52:05 2021 UTC (5 years, 4 months ago) by sf-exg
Branch: MAIN
Changes since 1.20: +21 -0 lines
Log Message:
doc: add portability section

File Contents

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