ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libptytty/doc/libptytty.html
Revision: 1.3
Committed: Mon Jan 23 12:37:09 2006 UTC (20 years, 8 months ago) by root
Content type: text/html
Branch: MAIN
CVS Tags: rel-0_1, rel-0_2
Changes since 1.2: +44 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <title>libptytty - OS independent and secure pty/tty and utmp/wtmp/lastlog handling</title>
5 <link rev="made" href="mailto:perl-binary@plan9.de" />
6 </head>
7
8 <body style="background-color: white">
9
10 <p><a name="__index__"></a></p>
11 <!-- INDEX BEGIN -->
12
13 <ul>
14
15 <li><a href="#name">NAME</a></li>
16 <li><a href="#synopsis">SYNOPSIS</a></li>
17 <li><a href="#description">DESCRIPTION</a></li>
18 <li><a href="#security_considerations">SECURITY CONSIDERATIONS</a></li>
19 <li><a href="#c___interface__the_ptytty_class">C++ INTERFACE: THE ptytty CLASS</a></li>
20 <ul>
21
22 <li><a href="#static_methods">STATIC METHODS</a></li>
23 <li><a href="#dynamic_sessionrelated_data_members_and_methods">DYNAMIC/SESSION-RELATED DATA MEMBERS AND METHODS</a></li>
24 </ul>
25
26 <li><a href="#c_interface__the_ptytty_family_of_functions">C INTERFACE: THE ptytty FAMILY OF FUNCTIONS</a></li>
27 <li><a href="#bugs">BUGS</a></li>
28 <li><a href="#authors">AUTHORS</a></li>
29 </ul>
30 <!-- INDEX END -->
31
32 <hr />
33 <p>
34 </p>
35 <h1><a name="name">NAME</a></h1>
36 <p>libptytty - OS independent and secure pty/tty and utmp/wtmp/lastlog handling</p>
37 <p>
38 </p>
39 <hr />
40 <h1><a name="synopsis">SYNOPSIS</a></h1>
41 <pre>
42 cc ... -lptytty</pre>
43 <pre>
44 #include &lt;libptytty.h&gt;</pre>
45 <pre>
46 // C++
47 ptytty *pty = ptytty::create ();</pre>
48 <pre>
49 if (!pty-&gt;get ())
50 // error allocating pty</pre>
51 <pre>
52 if (we want utmp)
53 pty-&gt;login (process_pid, 0, &quot;remote.host&quot;);
54 else if (we want utmp AND wtmp/lastlog)
55 pty-&gt;login (process_pid, 1, &quot;remote.host&quot;);</pre>
56 <pre>
57 // we are done with it
58 delete pty;</pre>
59 <pre>
60 // C
61 PTYTTY pty = ptytty_create ();</pre>
62 <pre>
63 if (!ptytty_get (pty))
64 // error allocating pty</pre>
65 <pre>
66 if (we want utmp)
67 ptytty_login (pty, process_pid, 0, &quot;remote.host&quot;);
68 else if (we want utmp AND wtmp/lastlog)
69 ptytty_login (pty, process_pid, 1, &quot;remote.host&quot;);</pre>
70 <pre>
71 // we are done with it
72 ptytty_delete (pty);</pre>
73 <p>
74 </p>
75 <hr />
76 <h1><a name="description">DESCRIPTION</a></h1>
77 <p>Libptytty is a small library that offers pseudo-tty management in an
78 OS-independent way. It was created out of frustration over the many
79 differences of pty/tty handling in different operating systems for the use
80 inside <code>rxvt-unicode</code>.</p>
81 <p>In addition to offering mere pty/tty management, it also offers session
82 database support (utmp and optional wtmp/lastlog updates for login
83 shells).</p>
84 <p>It also supports fork'ing after startup and dropping privileges in the
85 calling process, so in case the calling process gets compromised by the
86 user starting the program there is less to gain, as only the helper
87 process runs with privileges (e.g. setuid/setgid), which reduces the area
88 of attack immensely.</p>
89 <p>Libptytty is written in C++, but it also offers a C-only API.</p>
90 <p>
91 </p>
92 <hr />
93 <h1><a name="security_considerations">SECURITY CONSIDERATIONS</a></h1>
94 <p><em><strong>It is of paramount importance that you at least read the following
95 paragraph!</strong> </em>&gt;</p>
96 <p>If you are a typical terminal-like program that just wants one or more
97 ptys, you should call the <a href="#item_init"><code>ptytty::init ()</code></a> method (C: <a href="#item_ptytty_init"><code>ptytty_init ()</code></a>
98 function) as the very first thing in your program:</p>
99 <pre>
100 int main (int argc, char *argv[])
101 {
102 // do nothing here
103 ptytty::init ();
104 // in C: ptytty_init ();</pre>
105 <pre>
106 // initialise, parse arguments, etc.
107 }</pre>
108 <p>This checks wether the program runs setuid or setgid. If yes then it will
109 fork a helper process and drop privileges.</p>
110 <p>Some programs need finer control over if and when this helper process
111 is started, and if and how to drop privileges. For those programs, the
112 methods <code>ptytty::use_helper</code> and <code>ptytty::drop_privileges</code> are more
113 useful.</p>
114 <p>
115 </p>
116 <hr />
117 <h1><a name="c___interface__the_ptytty_class">C++ INTERFACE: THE ptytty CLASS</a></h1>
118 <p>
119 </p>
120 <h2><a name="static_methods">STATIC METHODS</a></h2>
121 <dl>
122 <dt><strong><a name="item_init">ptytty::init ()</a></strong><br />
123 </dt>
124 <dd>
125 The default way to initialise libptytty. Must be called imemdiately as
126 the first thing in the <code>main</code> function, or earlier e.g. during static
127 construction time. The earlier, the better.
128 </dd>
129 <dd>
130 <p>This method checks wether the program runs with setuid/setgid permissions
131 and, if yes, spawns a helper process for pty/tty management. IT then
132 drops the privileges completely, so the actual program runs without
133 setuid/setgid privileges.</p>
134 </dd>
135 <p></p>
136 <dt><strong><a name="item_use_helper">ptytty::use_helper ()</a></strong><br />
137 </dt>
138 <dd>
139 Tries to start a helper process that retains privileges even when the
140 calling process does not. This is usually called from <code>ptytty::init</code> when
141 it detects that the program is running setuid or setgid, but can be called
142 manually if it is inconvinient to drop privileges at startup, or when
143 you are not running setuid/setgid but want to drop privileges (e.g. when
144 running as a root-started daemon).
145 </dd>
146 <dd>
147 <p>This method will try not to start more than one helper process. The same
148 helper process cna usually be used form the process starting it an all its
149 fork'ed (not exec'ed) children</p>
150 </dd>
151 <p></p>
152 <dt><strong><a name="item_drop_privileges">ptytty::drop_privileges ()</a></strong><br />
153 </dt>
154 <dd>
155 Drops privileges completely, i.e. sets real, effective and saved user id
156 to the real user id. Also aborts if this cnanot be achieved. Useful to
157 make sure that the process doesn't run with special privileges.
158 </dd>
159 <p></p>
160 <dt><strong><a name="item_send_fd">bool success = ptytty::send_fd (int socket, int fd)</a></strong><br />
161 </dt>
162 <dd>
163 Utility method to send a file descriptor over a unix domain
164 socket. Returns true if successful, false otherwise. This method is only
165 exposed for your convinience and is not required for normal operation.
166 </dd>
167 <p></p>
168 <dt><strong><a name="item_recv_fd">int fd = ptytty::recv_fd (int socket)</a></strong><br />
169 </dt>
170 <dd>
171 Utility method to receive a file descriptor over a unix domain
172 socket. Returns the fd if sucecssful and <code>-1</code> otherwise. This method
173 is only exposed for your convinience and is not required for normal
174 operation.
175 </dd>
176 <p></p>
177 <dt><strong><a name="item_create">ptytty *pty = ptytty::create ()</a></strong><br />
178 </dt>
179 <dd>
180 Creates new ptytty object. Creation does not yet do anything besides
181 allocating the structure.
182 </dd>
183 <dd>
184 <p>A static method is used because the actual ptytty implementation can
185 differ at runtime, so you need a dynamic object creation facility.</p>
186 </dd>
187 <p></p></dl>
188 <p>
189 </p>
190 <h2><a name="dynamic_sessionrelated_data_members_and_methods">DYNAMIC/SESSION-RELATED DATA MEMBERS AND METHODS</a></h2>
191 <dl>
192 <dt><strong><a name="item_pty">int pty_fd = pty-&gt;pty</a></strong><br />
193 </dt>
194 <dt><strong><a name="item_tty">int tty_fd = pty-&gt;tty</a></strong><br />
195 </dt>
196 <dd>
197 These members contain the pty and tty file descriptors, respectively. They
198 initially contain <code>-1</code> until a successful to <code>ptytty::get</code>.
199 </dd>
200 <p></p>
201 <dt><strong><a name="item_get">bool success = pty-&gt;get ()</a></strong><br />
202 </dt>
203 <dd>
204 Tries to find, allocate and initialise a new pty/tty pair. Returns <code>true</code>
205 when successful.
206 </dd>
207 <p></p>
208 <dt><strong><a name="item_login">pty-&gt;login (int cmd_pid, bool login_shell, const char *hostname)</a></strong><br />
209 </dt>
210 <dd>
211 Creates an entry in the systems session <code>database(s)</code> (utmp, wtmp, lastlog).
212 <code>cmd_pid</code> must be the pid of the process representing the session
213 (such as the login shell), <code>login_shell</code> defines wether the session is
214 associated with a login, which influences wether wtmp and lastlog entries
215 are created, and <code>hostname</code> should identify the ``hostname'' the user logs
216 in from, which often is the value of the <code>DISPLAY</code> variable or tty line
217 in case of local logins.
218 </dd>
219 <dd>
220 <p>Calling this method is optional. A session starts at the time of the login
221 call and extends until the ptytty object is destroyed.</p>
222 </dd>
223 <p></p>
224 <dt><strong><a name="item_close_tty">pty-&gt;close_tty ()</a></strong><br />
225 </dt>
226 <dd>
227 Closes the tty. Useful after forking in the parent/pty process.
228 </dd>
229 <p></p>
230 <dt><strong><a name="item_make_controlling_tty">bool success = pty-&gt;make_controlling_tty ()</a></strong><br />
231 </dt>
232 <dd>
233 Tries to make the pty/tty pair the controlling terminal of the current
234 process. Useful after forking in the child/tty process.
235 </dd>
236 <p></p>
237 <dt><strong><a name="item_set_utf8_mode">pty-&gt;set_utf8_mode (bool on)</a></strong><br />
238 </dt>
239 <dd>
240 On systems supporting special UTF-8 line disciplines (e.g. Linux), tries
241 to enable it for the given pty. Can be called at any time to change the
242 mode.
243 </dd>
244 <p></p></dl>
245 <p>
246 </p>
247 <hr />
248 <h1><a name="c_interface__the_ptytty_family_of_functions">C INTERFACE: THE ptytty FAMILY OF FUNCTIONS</a></h1>
249 <dl>
250 <dt><strong><a name="item_ptytty_init">ptytty_init ()</a></strong><br />
251 </dt>
252 <dd>
253 See <a href="#item_init"><code>ptytty::init ()</code></a>.
254
255 </dd>
256 <dd>
257 <pre>
258
259 =item PTYTTY ptytty_create ()</pre>
260 </dd>
261 <dd>
262 <p>Creates a new opaque PTYTTY object and returns it. Do not try to access it
263 in any way excecp by testing it for truthness (e.g. <code>if (pty) ....</code>). See
264 <a href="#item_create"><code>ptytty::create ()</code></a>.</p>
265 </dd>
266 <p></p>
267 <dt><strong><a name="item_ptytty_pty">int ptytty_pty (PTYTTY ptytty)</a></strong><br />
268 </dt>
269 <dd>
270 Return the pty file descriptor. See <a href="#item_pty"><code>pty-&gt;pty</code></a>.
271
272 </dd>
273 <dd>
274 <pre>
275
276 =item int ptytty_tty (PTYTTY ptytty)</pre>
277 </dd>
278 <dd>
279 <p>Return the tty file descriptor. See <a href="#item_tty"><code>pty-&gt;tty</code></a>.
280 </p>
281 </dd>
282 <dd>
283 <pre>
284
285 =item void ptytty_delete (PTYTTY ptytty)</pre>
286 </dd>
287 <dd>
288 <p>Destroys the PTYTTY object, freeing the pty/tty pair and cleaning up the
289 utmp/wtmp/lastlog databases, if initialised/used. Same as <code>delete pty</code> in
290 C++.</p>
291 </dd>
292 <p></p>
293 <dt><strong><a name="item_ptytty_get">int ptytty_get (PTYTTY ptytty)</a></strong><br />
294 </dt>
295 <dd>
296 See <a href="#item_get"><code>pty-&gt;get</code></a>, returns 0 in case of an error, non-zero otherwise.
297 </dd>
298 <p></p>
299 <dt><strong><a name="item_ptytty_login">void ptytty_login (PTYTTY ptytty, int cmd_pid, bool login_shell, const char *hostname)</a></strong><br />
300 </dt>
301 <dd>
302 See <a href="#item_login"><code>pty-&gt;login</code></a>.
303 </dd>
304 <p></p>
305 <dt><strong><a name="item_ptytty_close_tty">void ptytty_close_tty (PTYTTY ptytty)</a></strong><br />
306 </dt>
307 <dd>
308 See <a href="#item_close_tty"><code>pty-&gt;close_tty</code></a>.
309
310 </dd>
311 <dd>
312 <pre>
313
314 =item int ptytty_make_controlling_tty (PTYTTY ptytty)</pre>
315 </dd>
316 <dd>
317 <p>See <a href="#item_make_controlling_tty"><code>pty-&gt;make_controlling_tty</code></a>.
318 </p>
319 </dd>
320 <dd>
321 <pre>
322
323 =item void ptytty_set_utf8_mode (PTYTTY ptytty, int on)</pre>
324 </dd>
325 <dd>
326 <p>See <a href="#item_set_utf8_mode"><code>pty-&gt;set_utf8_mode</code></a>.</p>
327 </dd>
328 <p></p>
329 <dt><strong><a name="item_ptytty_drop_privileges">void ptytty_drop_privileges ()</a></strong><br />
330 </dt>
331 <dd>
332 See <code>ptytty::drop_privileges</code>.
333
334 </dd>
335 <dd>
336 <pre>
337
338 =item void ptytty_use_helper ()</pre>
339 </dd>
340 <dd>
341 <p>See <code>ptytty::use_helper</code>.
342
343 </p>
344 </dd>
345 <p></p></dl>
346 <p>
347 </p>
348 <hr />
349 <h1><a name="bugs">BUGS</a></h1>
350 <p>You kiddin'?
351
352 </p>
353 <p>
354 </p>
355 <hr />
356 <h1><a name="authors">AUTHORS</a></h1>
357 <p>Emanuele Giaquinta <em><a href="mailto:<e.giaquinta@glauco.it"><e.giaquinta@glauco.it</a></em>&gt;, Marc Alexander Lehmann
358 <em><a href="mailto:<rxvt-unicode@schmorp.de"><rxvt-unicode@schmorp.de</a></em>&gt;.
359 </p>
360
361 </body>
362
363 </html>