ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/doc/libptytty.3
Revision: 1.4
Committed: Thu Feb 25 20:21:49 2016 UTC (8 years, 4 months ago) by root
Branch: MAIN
CVS Tags: rel-1_8, rxvt-unicode-rel-9_26, rxvt-unicode-rel-9_25, rxvt-unicode-rel-9_22
Changes since 1.3: +44 -23 lines
Log Message:
1.8

File Contents

# User Rev Content
1 root 1.4 .\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.30)
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.1 .\" If the F register is turned on, 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     .if (\n(rF:(\n(.g==0)) \{
60     . if \nF \{
61     . de IX
62     . tm Index:\\$1\t\\n%\t"\\$2"
63 sf-exg 1.2 ..
64 root 1.4 . if !\nF==2 \{
65     . 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 root 1.4 .TH libptytty 3 "2016-02-25" "1.8" "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.1 .SH "SECURITY CONSIDERATIONS"
202     .IX Header "SECURITY CONSIDERATIONS"
203     \&\fI\f(BIIt is of paramount importance that you at least read the following
204     paragraph!\fI\fR
205     .PP
206     If you write a typical terminal-like program that just wants one or more
207     ptys, you should call the \f(CW\*(C`ptytty::init ()\*(C'\fR method (C: \f(CW\*(C`ptytty_init ()\*(C'\fR
208     function) as the very first thing in your program:
209     .PP
210     .Vb 5
211     \& int main (int argc, char *argv[])
212     \& {
213     \& // do nothing here
214     \& ptytty::init ();
215     \& // in C: ptytty_init ();
216     \&
217     \& // initialise, parse arguments, etc.
218     \& }
219     .Ve
220     .PP
221 sf-exg 1.2 This checks whether the program runs setuid or setgid. If yes then it will
222 sf-exg 1.1 fork a helper process and drop privileges.
223     .PP
224     Some programs need finer control over if and when this helper process
225     is started, and if and how to drop privileges. For those programs, the
226     methods \f(CW\*(C`ptytty::use_helper\*(C'\fR and \f(CW\*(C`ptytty::drop_privileges\*(C'\fR (and possibly
227     \&\f(CW\*(C`ptytty::sanitise_stdfd\*(C'\fR) are more useful.
228     .SH "\*(C+ INTERFACE: THE ptytty CLASS"
229     .IX Header " INTERFACE: THE ptytty CLASS"
230 root 1.4 .SS "\s-1STATIC METHODS\s0"
231 sf-exg 1.1 .IX Subsection "STATIC METHODS"
232     .IP "ptytty::init ()" 4
233     .IX Item "ptytty::init ()"
234     The default way to initialise libptytty. Must be called immediately as
235     the first thing in the \f(CW\*(C`main\*(C'\fR function, or earlier e.g. during static
236     construction time. The earlier, the better.
237     .Sp
238 sf-exg 1.2 This method calls \f(CW\*(C`sanitise_stdfd\*(C'\fR and then checks whether the program runs
239 sf-exg 1.1 with setuid/setgid permissions and, if yes, spawns a helper process for
240     pty/tty management. It then drops the privileges completely, so the actual
241     program runs without setuid/setgid privileges.
242 root 1.4 .Sp
243     On failure, this method terminates the process.
244 sf-exg 1.1 .IP "ptytty::use_helper ()" 4
245     .IX Item "ptytty::use_helper ()"
246     Tries to start a helper process that retains privileges even when the
247     calling process does not. This is usually called from \f(CW\*(C`ptytty::init\*(C'\fR when
248     it detects that the program is running setuid or setgid, but can be called
249 sf-exg 1.2 manually if it is inconvenient to drop privileges at startup, or when
250 sf-exg 1.1 you are not running setuid/setgid but want to drop privileges (e.g. when
251     running as a root-started daemon).
252     .Sp
253     This method will try not to start more than one helper process. The same
254     helper process can usually be used both from the process starting it and
255     all its fork'ed (not exec'ed) children.
256 root 1.4 .Sp
257     On failure, this method terminates the process.
258 sf-exg 1.1 .IP "ptytty::drop_privileges ()" 4
259     .IX Item "ptytty::drop_privileges ()"
260 root 1.4 Drops privileges completely, i.e. sets real, effective and saved user
261     id to the real user id. Useful to make sure that the process doesn't
262     run with special privileges.
263     .Sp
264     On failure, this method terminates the process.
265 sf-exg 1.1 .IP "ptytty::sanitise_stdfd ()" 4
266     .IX Item "ptytty::sanitise_stdfd ()"
267 root 1.4 Checks whether file descriptors 0, 1 and 2 (stdin, stdout and stderr)
268     are valid (open) and, if not, connects them to \fI/dev/tty\fR or
269     \&\fI/dev/null\fR if possible. This is necessary because libptytty might
270     want to output error messages to those descriptors, which at the time
271     of outputting the error message, might be connected to something
272     unsuitable opened by the unsuspecting program itself (this can be a
273     security issue).
274     .Sp
275     On failure, this method terminates the process.
276 sf-exg 1.1 .IP "bool success = ptytty::send_fd (int socket, int fd)" 4
277     .IX Item "bool success = ptytty::send_fd (int socket, int fd)"
278     Utility method to send a file descriptor over a unix domain
279     socket. Returns true if successful, false otherwise. This method is only
280 sf-exg 1.2 exposed for your convenience and is not required for normal operation.
281 sf-exg 1.1 .IP "int fd = ptytty::recv_fd (int socket)" 4
282     .IX Item "int fd = ptytty::recv_fd (int socket)"
283     Utility method to receive a file descriptor over a unix domain
284 sf-exg 1.2 socket. Returns the fd if successful and \f(CW\*(C`\-1\*(C'\fR otherwise. This method
285     is only exposed for your convenience and is not required for normal
286 sf-exg 1.1 operation.
287     .IP "ptytty *pty = ptytty::create ()" 4
288     .IX Item "ptytty *pty = ptytty::create ()"
289     Creates new ptytty object. Creation does not yet do anything besides
290     allocating the structure.
291     .Sp
292     A static method is used because the actual ptytty implementation can
293     differ at runtime, so you need a dynamic object creation facility.
294 root 1.4 .SS "\s-1DYNAMIC/SESSION\-RELATED DATA MEMBERS AND METHODS\s0"
295 sf-exg 1.1 .IX Subsection "DYNAMIC/SESSION-RELATED DATA MEMBERS AND METHODS"
296     .IP "int pty_fd = pty\->pty" 4
297     .IX Item "int pty_fd = pty->pty"
298     .PD 0
299     .IP "int tty_fd = pty\->tty" 4
300     .IX Item "int tty_fd = pty->tty"
301     .PD
302     These members contain the pty and tty file descriptors, respectively. They
303 root 1.4 initially contain \f(CW\*(C`\-1\*(C'\fR until a successful call to \f(CW\*(C`ptytty::get\*(C'\fR.
304 sf-exg 1.1 .IP "bool success = pty\->get ()" 4
305     .IX Item "bool success = pty->get ()"
306     Tries to find, allocate and initialise a new pty/tty pair. Returns \f(CW\*(C`true\*(C'\fR
307     when successful.
308 root 1.4 .Sp
309     If the helper process is running and there is a protocol error, this
310     method terminates the process.
311 sf-exg 1.1 .IP "pty\->login (int cmd_pid, bool login_shell, const char *hostname)" 4
312     .IX Item "pty->login (int cmd_pid, bool login_shell, const char *hostname)"
313     Creates an entry in the systems session database(s) (utmp, wtmp, lastlog).
314     \&\f(CW\*(C`cmd_pid\*(C'\fR must be the pid of the process representing the session
315 sf-exg 1.2 (such as the login shell), \f(CW\*(C`login_shell\*(C'\fR defines whether the session is
316     associated with a login, which influences whether wtmp and lastlog entries
317 sf-exg 1.1 are created, and \f(CW\*(C`hostname\*(C'\fR should identify the \*(L"hostname\*(R" the user logs
318     in from, which often is the value of the \f(CW\*(C`DISPLAY\*(C'\fR variable or tty line
319     in case of local logins.
320     .Sp
321     Calling this method is optional. A session starts at the time of the login
322     call and extends until the ptytty object is destroyed.
323     .IP "pty\->close_tty ()" 4
324     .IX Item "pty->close_tty ()"
325     Closes the tty. Useful after forking in the parent/pty process.
326     .IP "bool success = pty\->make_controlling_tty ()" 4
327     .IX Item "bool success = pty->make_controlling_tty ()"
328     Tries to make the pty/tty pair the controlling terminal of the current
329     process. Useful after forking in the child/tty process.
330     .IP "pty\->set_utf8_mode (bool on)" 4
331     .IX Item "pty->set_utf8_mode (bool on)"
332     On systems supporting special \s-1UTF\-8\s0 line disciplines (e.g. Linux), this
333     tries to enable this discipline for the given pty. Can be called at any
334     time to change the mode.
335     .SH "C INTERFACE: THE ptytty FAMILY OF FUNCTIONS"
336     .IX Header "C INTERFACE: THE ptytty FAMILY OF FUNCTIONS"
337     .IP "ptytty_init ()" 4
338     .IX Item "ptytty_init ()"
339     See \f(CW\*(C`ptytty::init ()\*(C'\fR.
340     .IP "\s-1PTYTTY\s0 ptytty_create ()" 4
341     .IX Item "PTYTTY ptytty_create ()"
342     Creates a new opaque \s-1PTYTTY\s0 object and returns it. Do not try to access it
343     in any way except by testing it for truthness (e.g. \f(CW\*(C`if (pty) ....\*(C'\fR). See
344     \&\f(CW\*(C`ptytty::create ()\*(C'\fR.
345     .IP "int ptytty_pty (\s-1PTYTTY\s0 ptytty)" 4
346     .IX Item "int ptytty_pty (PTYTTY ptytty)"
347     Return the pty file descriptor. See \f(CW\*(C`pty\->pty\*(C'\fR.
348     .IP "int ptytty_tty (\s-1PTYTTY\s0 ptytty)" 4
349     .IX Item "int ptytty_tty (PTYTTY ptytty)"
350     Return the tty file descriptor. See \f(CW\*(C`pty\->tty\*(C'\fR.
351     .IP "void ptytty_delete (\s-1PTYTTY\s0 ptytty)" 4
352     .IX Item "void ptytty_delete (PTYTTY ptytty)"
353     Destroys the \s-1PTYTTY\s0 object, freeing the pty/tty pair and cleaning up the
354     utmp/wtmp/lastlog databases, if initialised/used. Same as \f(CW\*(C`delete pty\*(C'\fR in
355     \&\*(C+.
356     .IP "int ptytty_get (\s-1PTYTTY\s0 ptytty)" 4
357     .IX Item "int ptytty_get (PTYTTY ptytty)"
358     See \f(CW\*(C`pty\->get\*(C'\fR, returns 0 in case of an error, non-zero otherwise.
359     .IP "void ptytty_login (\s-1PTYTTY\s0 ptytty, int cmd_pid, bool login_shell, const char *hostname)" 4
360     .IX Item "void ptytty_login (PTYTTY ptytty, int cmd_pid, bool login_shell, const char *hostname)"
361     See \f(CW\*(C`pty\->login\*(C'\fR.
362     .IP "void ptytty_close_tty (\s-1PTYTTY\s0 ptytty)" 4
363     .IX Item "void ptytty_close_tty (PTYTTY ptytty)"
364     See \f(CW\*(C`pty\->close_tty\*(C'\fR.
365     .IP "int ptytty_make_controlling_tty (\s-1PTYTTY\s0 ptytty)" 4
366     .IX Item "int ptytty_make_controlling_tty (PTYTTY ptytty)"
367     See \f(CW\*(C`pty\->make_controlling_tty\*(C'\fR.
368     .IP "void ptytty_set_utf8_mode (\s-1PTYTTY\s0 ptytty, int on)" 4
369     .IX Item "void ptytty_set_utf8_mode (PTYTTY ptytty, int on)"
370     See \f(CW\*(C`pty\->set_utf8_mode\*(C'\fR.
371     .IP "void ptytty_drop_privileges ()" 4
372     .IX Item "void ptytty_drop_privileges ()"
373     See \f(CW\*(C`ptytty::drop_privileges\*(C'\fR.
374     .IP "void ptytty_use_helper ()" 4
375     .IX Item "void ptytty_use_helper ()"
376     See \f(CW\*(C`ptytty::use_helper\*(C'\fR.
377     .SH "BUGS"
378     .IX Header "BUGS"
379     You kiddin'?
380     .SH "AUTHORS"
381     .IX Header "AUTHORS"
382     Emanuele Giaquinta <e.giaquinta@glauco.it>, Marc Alexander Lehmann
383     <rxvt\-unicode@schmorp.de>.