ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/doc/libptytty.3
Revision: 1.7
Committed: Tue Jul 27 08:43:41 2021 UTC (5 years, 2 months ago) by sf-exg
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +13 -2 lines
Log Message:
Regenerate libptytty.3 and README

File Contents

# User Rev Content
1 root 1.6 .\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35)
2 sf-exg 1.1 .\"
3     .\" Standard preamble:
4     .\" ========================================================================
5     .de Sp \" Vertical space (when we can't use .PP)
6     .if t .sp .5v
7     .if n .sp
8     ..
9     .de Vb \" Begin verbatim text
10     .ft CW
11     .nf
12     .ne \\$1
13     ..
14     .de Ve \" End verbatim text
15     .ft R
16     .fi
17     ..
18     .\" Set up some character translations and predefined strings. \*(-- will
19     .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
20     .\" double quote, and \*(R" will give a right double quote. \*(C+ will
21     .\" give a nicer C++. Capital omega is used to do unbreakable dashes and
22     .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
23     .\" nothing in troff, for use with C<>.
24     .tr \(*W-
25     .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
26     .ie n \{\
27     . ds -- \(*W-
28     . ds PI pi
29     . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
30     . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
31     . ds L" ""
32     . ds R" ""
33     . ds C` ""
34     . ds C' ""
35     'br\}
36     .el\{\
37     . ds -- \|\(em\|
38     . ds PI \(*p
39     . ds L" ``
40     . ds R" ''
41 root 1.4 . ds C`
42     . ds C'
43 sf-exg 1.1 'br\}
44     .\"
45 sf-exg 1.2 .\" Escape single quotes in literal strings from groff's Unicode transform.
46     .ie \n(.g .ds Aq \(aq
47     .el .ds Aq '
48     .\"
49 sf-exg 1.5 .\" If the F register is >0, we'll generate index entries on stderr for
50 sf-exg 1.2 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
51 sf-exg 1.1 .\" entries marked with X<> in POD. Of course, you'll have to process the
52     .\" output yourself in some meaningful fashion.
53 root 1.4 .\"
54     .\" Avoid warning from groff about undefined register 'F'.
55     .de IX
56 sf-exg 1.1 ..
57 root 1.4 .nr rF 0
58     .if \n(.g .if rF .nr rF 1
59 sf-exg 1.5 .if (\n(rF:(\n(.g==0)) \{\
60     . if \nF \{\
61 root 1.4 . de IX
62     . tm Index:\\$1\t\\n%\t"\\$2"
63 sf-exg 1.2 ..
64 sf-exg 1.5 . if !\nF==2 \{\
65 root 1.4 . nr % 0
66     . nr F 2
67     . \}
68     . \}
69 sf-exg 1.2 .\}
70 root 1.4 .rr rF
71 sf-exg 1.1 .\"
72     .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
73     .\" Fear. Run. Save yourself. No user-serviceable parts.
74     . \" fudge factors for nroff and troff
75     .if n \{\
76     . ds #H 0
77     . ds #V .8m
78     . ds #F .3m
79     . ds #[ \f1
80     . ds #] \fP
81     .\}
82     .if t \{\
83     . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
84     . ds #V .6m
85     . ds #F 0
86     . ds #[ \&
87     . ds #] \&
88     .\}
89     . \" simple accents for nroff and troff
90     .if n \{\
91     . ds ' \&
92     . ds ` \&
93     . ds ^ \&
94     . ds , \&
95     . ds ~ ~
96     . ds /
97     .\}
98     .if t \{\
99     . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
100     . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
101     . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
102     . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
103     . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
104     . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
105     .\}
106     . \" troff and (daisy-wheel) nroff accents
107     .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
108     .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
109     .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
110     .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
111     .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
112     .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
113     .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
114     .ds ae a\h'-(\w'a'u*4/10)'e
115     .ds Ae A\h'-(\w'A'u*4/10)'E
116     . \" corrections for vroff
117     .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
118     .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
119     . \" for low resolution devices (crt and lpr)
120     .if \n(.H>23 .if \n(.V>19 \
121     \{\
122     . ds : e
123     . ds 8 ss
124     . ds o a
125     . ds d- d\h'-1'\(ga
126     . ds D- D\h'-1'\(hy
127     . ds th \o'bp'
128     . ds Th \o'LP'
129     . ds ae ae
130     . ds Ae AE
131     .\}
132     .rm #[ #] #H #V #F C
133     .\" ========================================================================
134     .\"
135     .IX Title "libptytty 3"
136 sf-exg 1.7 .TH libptytty 3 "2021-07-27" "2.0" "LIBPTYTTY"
137 sf-exg 1.1 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
138     .\" way too many mistakes in technical documents.
139     .if n .ad l
140     .nh
141     .SH "NAME"
142     libptytty \- OS independent and secure pty/tty and utmp/wtmp/lastlog handling
143     .SH "SYNOPSIS"
144     .IX Header "SYNOPSIS"
145     .Vb 1
146     \& cc ... \-lptytty
147     \&
148     \& #include <libptytty.h>
149     \&
150     \&
151     \& // C++
152     \& ptytty *pty = ptytty::create ();
153     \&
154     \& if (!pty\->get ())
155     \& // error allocating pty
156     \&
157     \& if (we want utmp)
158     \& pty\->login (process_pid, 0, "remote.host");
159     \& else if (we want utmp AND wtmp/lastlog)
160     \& pty\->login (process_pid, 1, "remote.host");
161     \&
162     \& // we are done with it
163     \& delete pty;
164     \&
165     \&
166     \& // C
167     \& PTYTTY pty = ptytty_create ();
168     \&
169     \& if (!ptytty_get (pty))
170     \& // error allocating pty
171     \&
172     \& if (we want utmp)
173     \& ptytty_login (pty, process_pid, 0, "remote.host");
174     \& else if (we want utmp AND wtmp/lastlog)
175     \& ptytty_login (pty, process_pid, 1, "remote.host");
176     \&
177     \& // we are done with it
178     \& ptytty_delete (pty);
179     .Ve
180     .PP
181     See also the \fIeg/\fR directory, which currently contains the \fIc\-sample.c\fR
182 sf-exg 1.2 file that spawns a login shell from C using libptytty.
183 sf-exg 1.1 .SH "DESCRIPTION"
184     .IX Header "DESCRIPTION"
185     Libptytty is a small library that offers pseudo-tty management in an
186     OS-independent way. It was created out of frustration over the many
187     differences of pty/tty handling in different operating systems for the use
188     inside \f(CW\*(C`rxvt\-unicode\*(C'\fR.
189     .PP
190     In addition to offering mere pty/tty management, it also offers session
191     database support (utmp and optional wtmp/lastlog updates for login
192     shells).
193     .PP
194     It also supports fork'ing after startup and dropping privileges in the
195     calling process, so in case the calling process gets compromised by the
196     user starting the program there is less to gain, as only the helper
197     process runs with privileges (e.g. setuid/setgid), which reduces the area
198     of attack immensely.
199     .PP
200 root 1.4 Libptytty is written in \*(C+, but it also offers a C\-only \s-1API.\s0
201 sf-exg 1.7 .SH "INSTALLATION"
202     .IX Header "INSTALLATION"
203     libptytty uses \f(CW\*(C`CMake\*(C'\fR as build system. To build libptytty, install
204     \&\f(CW\*(C`CMake\*(C'\fR and run the following commands from either the libptytty
205     source directory or a separate build directory:
206     .PP
207     .Vb 3
208     \& cmake \-DCMAKE_INSTALL_PREFIX=<prefix> \-DBUILD_SHARED_LIBS=ON <path/to/libptytty>
209     \& cmake \-\-build .
210     \& cmake \-\-install .
211     .Ve
212 sf-exg 1.1 .SH "SECURITY CONSIDERATIONS"
213     .IX Header "SECURITY CONSIDERATIONS"
214     \&\fI\f(BIIt is of paramount importance that you at least read the following
215     paragraph!\fI\fR
216     .PP
217     If you write a typical terminal-like program that just wants one or more
218     ptys, you should call the \f(CW\*(C`ptytty::init ()\*(C'\fR method (C: \f(CW\*(C`ptytty_init ()\*(C'\fR
219     function) as the very first thing in your program:
220     .PP
221     .Vb 5
222     \& int main (int argc, char *argv[])
223     \& {
224     \& // do nothing here
225     \& ptytty::init ();
226     \& // in C: ptytty_init ();
227     \&
228     \& // initialise, parse arguments, etc.
229     \& }
230     .Ve
231     .PP
232 sf-exg 1.2 This checks whether the program runs setuid or setgid. If yes then it will
233 sf-exg 1.1 fork a helper process and drop privileges.
234     .PP
235     Some programs need finer control over if and when this helper process
236     is started, and if and how to drop privileges. For those programs, the
237     methods \f(CW\*(C`ptytty::use_helper\*(C'\fR and \f(CW\*(C`ptytty::drop_privileges\*(C'\fR (and possibly
238     \&\f(CW\*(C`ptytty::sanitise_stdfd\*(C'\fR) are more useful.
239     .SH "\*(C+ INTERFACE: THE ptytty CLASS"
240     .IX Header " INTERFACE: THE ptytty CLASS"
241 root 1.4 .SS "\s-1STATIC METHODS\s0"
242 sf-exg 1.1 .IX Subsection "STATIC METHODS"
243     .IP "ptytty::init ()" 4
244     .IX Item "ptytty::init ()"
245     The default way to initialise libptytty. Must be called immediately as
246     the first thing in the \f(CW\*(C`main\*(C'\fR function, or earlier e.g. during static
247     construction time. The earlier, the better.
248     .Sp
249 sf-exg 1.2 This method calls \f(CW\*(C`sanitise_stdfd\*(C'\fR and then checks whether the program runs
250 sf-exg 1.1 with setuid/setgid permissions and, if yes, spawns a helper process for
251     pty/tty management. It then drops the privileges completely, so the actual
252     program runs without setuid/setgid privileges.
253 root 1.4 .Sp
254 root 1.6 On failure, this method throws a \f(CW\*(C`ptytty_error\*(C'\fR exception.
255 sf-exg 1.1 .IP "ptytty::use_helper ()" 4
256     .IX Item "ptytty::use_helper ()"
257     Tries to start a helper process that retains privileges even when the
258     calling process does not. This is usually called from \f(CW\*(C`ptytty::init\*(C'\fR when
259     it detects that the program is running setuid or setgid, but can be called
260 sf-exg 1.2 manually if it is inconvenient to drop privileges at startup, or when
261 sf-exg 1.1 you are not running setuid/setgid but want to drop privileges (e.g. when
262     running as a root-started daemon).
263     .Sp
264     This method will try not to start more than one helper process. The same
265     helper process can usually be used both from the process starting it and
266     all its fork'ed (not exec'ed) children.
267 root 1.4 .Sp
268 root 1.6 On failure, this method throws a \f(CW\*(C`ptytty_error\*(C'\fR exception.
269 sf-exg 1.1 .IP "ptytty::drop_privileges ()" 4
270     .IX Item "ptytty::drop_privileges ()"
271 root 1.4 Drops privileges completely, i.e. sets real, effective and saved user
272     id to the real user id. Useful to make sure that the process doesn't
273     run with special privileges.
274     .Sp
275 root 1.6 On failure, this method throws a \f(CW\*(C`ptytty_error\*(C'\fR exception.
276 sf-exg 1.1 .IP "ptytty::sanitise_stdfd ()" 4
277     .IX Item "ptytty::sanitise_stdfd ()"
278 root 1.4 Checks whether file descriptors 0, 1 and 2 (stdin, stdout and stderr)
279     are valid (open) and, if not, connects them to \fI/dev/tty\fR or
280     \&\fI/dev/null\fR if possible. This is necessary because libptytty might
281     want to output error messages to those descriptors, which at the time
282     of outputting the error message, might be connected to something
283     unsuitable opened by the unsuspecting program itself (this can be a
284     security issue).
285     .Sp
286 root 1.6 On failure, this method throws a \f(CW\*(C`ptytty_error\*(C'\fR exception.
287 sf-exg 1.1 .IP "bool success = ptytty::send_fd (int socket, int fd)" 4
288     .IX Item "bool success = ptytty::send_fd (int socket, int fd)"
289     Utility method to send a file descriptor over a unix domain
290     socket. Returns true if successful, false otherwise. This method is only
291 sf-exg 1.2 exposed for your convenience and is not required for normal operation.
292 sf-exg 1.1 .IP "int fd = ptytty::recv_fd (int socket)" 4
293     .IX Item "int fd = ptytty::recv_fd (int socket)"
294     Utility method to receive a file descriptor over a unix domain
295 sf-exg 1.2 socket. Returns the fd if successful and \f(CW\*(C`\-1\*(C'\fR otherwise. This method
296     is only exposed for your convenience and is not required for normal
297 sf-exg 1.1 operation.
298     .IP "ptytty *pty = ptytty::create ()" 4
299     .IX Item "ptytty *pty = ptytty::create ()"
300     Creates new ptytty object. Creation does not yet do anything besides
301     allocating the structure.
302     .Sp
303     A static method is used because the actual ptytty implementation can
304     differ at runtime, so you need a dynamic object creation facility.
305 root 1.4 .SS "\s-1DYNAMIC/SESSION\-RELATED DATA MEMBERS AND METHODS\s0"
306 sf-exg 1.1 .IX Subsection "DYNAMIC/SESSION-RELATED DATA MEMBERS AND METHODS"
307     .IP "int pty_fd = pty\->pty" 4
308     .IX Item "int pty_fd = pty->pty"
309     .PD 0
310     .IP "int tty_fd = pty\->tty" 4
311     .IX Item "int tty_fd = pty->tty"
312     .PD
313     These members contain the pty and tty file descriptors, respectively. They
314 root 1.4 initially contain \f(CW\*(C`\-1\*(C'\fR until a successful call to \f(CW\*(C`ptytty::get\*(C'\fR.
315 sf-exg 1.1 .IP "bool success = pty\->get ()" 4
316     .IX Item "bool success = pty->get ()"
317     Tries to find, allocate and initialise a new pty/tty pair. Returns \f(CW\*(C`true\*(C'\fR
318     when successful.
319 root 1.4 .Sp
320     If the helper process is running and there is a protocol error, this
321 root 1.6 method throws a \f(CW\*(C`ptytty_error\*(C'\fR exception.
322 sf-exg 1.1 .IP "pty\->login (int cmd_pid, bool login_shell, const char *hostname)" 4
323     .IX Item "pty->login (int cmd_pid, bool login_shell, const char *hostname)"
324     Creates an entry in the systems session database(s) (utmp, wtmp, lastlog).
325     \&\f(CW\*(C`cmd_pid\*(C'\fR must be the pid of the process representing the session
326 sf-exg 1.2 (such as the login shell), \f(CW\*(C`login_shell\*(C'\fR defines whether the session is
327     associated with a login, which influences whether wtmp and lastlog entries
328 sf-exg 1.1 are created, and \f(CW\*(C`hostname\*(C'\fR should identify the \*(L"hostname\*(R" the user logs
329     in from, which often is the value of the \f(CW\*(C`DISPLAY\*(C'\fR variable or tty line
330     in case of local logins.
331     .Sp
332     Calling this method is optional. A session starts at the time of the login
333     call and extends until the ptytty object is destroyed.
334     .IP "pty\->close_tty ()" 4
335     .IX Item "pty->close_tty ()"
336     Closes the tty. Useful after forking in the parent/pty process.
337     .IP "bool success = pty\->make_controlling_tty ()" 4
338     .IX Item "bool success = pty->make_controlling_tty ()"
339     Tries to make the pty/tty pair the controlling terminal of the current
340     process. Useful after forking in the child/tty process.
341     .IP "pty\->set_utf8_mode (bool on)" 4
342     .IX Item "pty->set_utf8_mode (bool on)"
343     On systems supporting special \s-1UTF\-8\s0 line disciplines (e.g. Linux), this
344     tries to enable this discipline for the given pty. Can be called at any
345     time to change the mode.
346     .SH "C INTERFACE: THE ptytty FAMILY OF FUNCTIONS"
347     .IX Header "C INTERFACE: THE ptytty FAMILY OF FUNCTIONS"
348     .IP "ptytty_init ()" 4
349     .IX Item "ptytty_init ()"
350     See \f(CW\*(C`ptytty::init ()\*(C'\fR.
351     .IP "\s-1PTYTTY\s0 ptytty_create ()" 4
352     .IX Item "PTYTTY ptytty_create ()"
353     Creates a new opaque \s-1PTYTTY\s0 object and returns it. Do not try to access it
354     in any way except by testing it for truthness (e.g. \f(CW\*(C`if (pty) ....\*(C'\fR). See
355     \&\f(CW\*(C`ptytty::create ()\*(C'\fR.
356     .IP "int ptytty_pty (\s-1PTYTTY\s0 ptytty)" 4
357     .IX Item "int ptytty_pty (PTYTTY ptytty)"
358     Return the pty file descriptor. See \f(CW\*(C`pty\->pty\*(C'\fR.
359     .IP "int ptytty_tty (\s-1PTYTTY\s0 ptytty)" 4
360     .IX Item "int ptytty_tty (PTYTTY ptytty)"
361     Return the tty file descriptor. See \f(CW\*(C`pty\->tty\*(C'\fR.
362     .IP "void ptytty_delete (\s-1PTYTTY\s0 ptytty)" 4
363     .IX Item "void ptytty_delete (PTYTTY ptytty)"
364     Destroys the \s-1PTYTTY\s0 object, freeing the pty/tty pair and cleaning up the
365     utmp/wtmp/lastlog databases, if initialised/used. Same as \f(CW\*(C`delete pty\*(C'\fR in
366     \&\*(C+.
367     .IP "int ptytty_get (\s-1PTYTTY\s0 ptytty)" 4
368     .IX Item "int ptytty_get (PTYTTY ptytty)"
369     See \f(CW\*(C`pty\->get\*(C'\fR, returns 0 in case of an error, non-zero otherwise.
370     .IP "void ptytty_login (\s-1PTYTTY\s0 ptytty, int cmd_pid, bool login_shell, const char *hostname)" 4
371     .IX Item "void ptytty_login (PTYTTY ptytty, int cmd_pid, bool login_shell, const char *hostname)"
372     See \f(CW\*(C`pty\->login\*(C'\fR.
373     .IP "void ptytty_close_tty (\s-1PTYTTY\s0 ptytty)" 4
374     .IX Item "void ptytty_close_tty (PTYTTY ptytty)"
375     See \f(CW\*(C`pty\->close_tty\*(C'\fR.
376     .IP "int ptytty_make_controlling_tty (\s-1PTYTTY\s0 ptytty)" 4
377     .IX Item "int ptytty_make_controlling_tty (PTYTTY ptytty)"
378     See \f(CW\*(C`pty\->make_controlling_tty\*(C'\fR.
379     .IP "void ptytty_set_utf8_mode (\s-1PTYTTY\s0 ptytty, int on)" 4
380     .IX Item "void ptytty_set_utf8_mode (PTYTTY ptytty, int on)"
381     See \f(CW\*(C`pty\->set_utf8_mode\*(C'\fR.
382     .IP "void ptytty_drop_privileges ()" 4
383     .IX Item "void ptytty_drop_privileges ()"
384     See \f(CW\*(C`ptytty::drop_privileges\*(C'\fR.
385     .IP "void ptytty_use_helper ()" 4
386     .IX Item "void ptytty_use_helper ()"
387     See \f(CW\*(C`ptytty::use_helper\*(C'\fR.
388 sf-exg 1.5 .SH "PORTABILITY"
389     .IX Header "PORTABILITY"
390     To date, libptytty has been tested on the following platforms:
391     .IP "GNU/Linux" 4
392     .IX Item "GNU/Linux"
393     .PD 0
394     .IP "FreeBSD" 4
395     .IX Item "FreeBSD"
396     .IP "NetBSD" 4
397     .IX Item "NetBSD"
398     .IP "OpenBSD" 4
399     .IX Item "OpenBSD"
400     .IP "macOS" 4
401     .IX Item "macOS"
402     .IP "Solaris" 4
403     .IX Item "Solaris"
404     .IP "\s-1AIX\s0" 4
405     .IX Item "AIX"
406     .PD
407 sf-exg 1.1 .SH "BUGS"
408     .IX Header "BUGS"
409     You kiddin'?
410     .SH "AUTHORS"
411     .IX Header "AUTHORS"
412 sf-exg 1.7 Emanuele Giaquinta <emanuele.giaquinta@gmail.com>, Marc Alexander Lehmann
413 sf-exg 1.1 <rxvt\-unicode@schmorp.de>.