ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/doc/libptytty.3
Revision: 1.3
Committed: Sat Jan 21 16:17:10 2012 UTC (12 years, 6 months ago) by sf-exg
Branch: MAIN
CVS Tags: rxvt-unicode-rel-9_19, rxvt-unicode-rel-9_18, rxvt-unicode-rel-9_17, rxvt-unicode-rel-9_16, rel-1_7, rxvt-unicode-rel-9_21, rxvt-unicode-rel-9_20, rel-1_6
Changes since 1.2: +1 -1 lines
Log Message:
1.6

File Contents

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