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

File Contents

# Content
1 NAME
2 libptytty - OS independent and secure pty/tty and utmp/wtmp/lastlog
3 handling
4
5 SYNOPSIS
6 cc ... -lptytty
7
8 #include <libptytty.h>
9
10
11 // C++
12 ptytty *pty = ptytty::create ();
13
14 if (!pty->get ())
15 // error allocating pty
16
17 if (we want utmp)
18 pty->login (process_pid, 0, "remote.host");
19 else if (we want utmp AND wtmp/lastlog)
20 pty->login (process_pid, 1, "remote.host");
21
22 // we are done with it
23 delete pty;
24
25
26 // C
27 PTYTTY pty = ptytty_create ();
28
29 if (!ptytty_get (pty))
30 // error allocating pty
31
32 if (we want utmp)
33 ptytty_login (pty, process_pid, 0, "remote.host");
34 else if (we want utmp AND wtmp/lastlog)
35 ptytty_login (pty, process_pid, 1, "remote.host");
36
37 // we are done with it
38 ptytty_delete (pty);
39
40 See also the eg/ directory, which currently contains the c-sample.c file
41 that spawns a login shell from C using libptytty.
42
43 DESCRIPTION
44 Libptytty is a small library that offers pseudo-tty management in an
45 OS-independent way. It was created out of frustration over the many
46 differences of pty/tty handling in different operating systems for the
47 use inside "rxvt-unicode".
48
49 In addition to offering mere pty/tty management, it also offers session
50 database support (utmp and optional wtmp/lastlog updates for login
51 shells).
52
53 It also supports fork'ing after startup and dropping privileges in the
54 calling process, so in case the calling process gets compromised by the
55 user starting the program there is less to gain, as only the helper
56 process runs with privileges (e.g. setuid/setgid), which reduces the
57 area of attack immensely.
58
59 Libptytty is written in C++, but it also offers a C-only API.
60
61 SECURITY CONSIDERATIONS
62 *It is of paramount importance that you at least read the following
63 paragraph!*
64
65 If you write a typical terminal-like program that just wants one or more
66 ptys, you should call the "ptytty::init ()" method (C: "ptytty_init ()"
67 function) as the very first thing in your program:
68
69 int main (int argc, char *argv[])
70 {
71 // do nothing here
72 ptytty::init ();
73 // in C: ptytty_init ();
74
75 // initialise, parse arguments, etc.
76 }
77
78 This checks whether the program runs setuid or setgid. If yes then it
79 will fork a helper process and drop privileges.
80
81 Some programs need finer control over if and when this helper process is
82 started, and if and how to drop privileges. For those programs, the
83 methods "ptytty::use_helper" and "ptytty::drop_privileges" (and possibly
84 "ptytty::sanitise_stdfd") are more useful.
85
86 C++ INTERFACE: THE ptytty CLASS
87 STATIC METHODS
88 ptytty::init ()
89 The default way to initialise libptytty. Must be called immediately
90 as the first thing in the "main" function, or earlier e.g. during
91 static construction time. The earlier, the better.
92
93 This method calls "sanitise_stdfd" and then checks whether the
94 program runs with setuid/setgid permissions and, if yes, spawns a
95 helper process for pty/tty management. It then drops the privileges
96 completely, so the actual program runs without setuid/setgid
97 privileges.
98
99 On failure, this method terminates the process.
100
101 ptytty::use_helper ()
102 Tries to start a helper process that retains privileges even when
103 the calling process does not. This is usually called from
104 "ptytty::init" when it detects that the program is running setuid or
105 setgid, but can be called manually if it is inconvenient to drop
106 privileges at startup, or when you are not running setuid/setgid but
107 want to drop privileges (e.g. when running as a root-started
108 daemon).
109
110 This method will try not to start more than one helper process. The
111 same helper process can usually be used both from the process
112 starting it and all its fork'ed (not exec'ed) children.
113
114 On failure, this method terminates the process.
115
116 ptytty::drop_privileges ()
117 Drops privileges completely, i.e. sets real, effective and saved
118 user id to the real user id. Useful to make sure that the process
119 doesn't run with special privileges.
120
121 On failure, this method terminates the process.
122
123 ptytty::sanitise_stdfd ()
124 Checks whether file descriptors 0, 1 and 2 (stdin, stdout and
125 stderr) are valid (open) and, if not, connects them to /dev/tty or
126 /dev/null if possible. This is necessary because libptytty might
127 want to output error messages to those descriptors, which at the
128 time of outputting the error message, might be connected to
129 something unsuitable opened by the unsuspecting program itself (this
130 can be a security issue).
131
132 On failure, this method terminates the process.
133
134 bool success = ptytty::send_fd (int socket, int fd)
135 Utility method to send a file descriptor over a unix domain socket.
136 Returns true if successful, false otherwise. This method is only
137 exposed for your convenience and is not required for normal
138 operation.
139
140 int fd = ptytty::recv_fd (int socket)
141 Utility method to receive a file descriptor over a unix domain
142 socket. Returns the fd if successful and -1 otherwise. This method
143 is only exposed for your convenience and is not required for normal
144 operation.
145
146 ptytty *pty = ptytty::create ()
147 Creates new ptytty object. Creation does not yet do anything besides
148 allocating the structure.
149
150 A static method is used because the actual ptytty implementation can
151 differ at runtime, so you need a dynamic object creation facility.
152
153 DYNAMIC/SESSION-RELATED DATA MEMBERS AND METHODS
154 int pty_fd = pty->pty
155 int tty_fd = pty->tty
156 These members contain the pty and tty file descriptors,
157 respectively. They initially contain -1 until a successful call to
158 "ptytty::get".
159
160 bool success = pty->get ()
161 Tries to find, allocate and initialise a new pty/tty pair. Returns
162 "true" when successful.
163
164 If the helper process is running and there is a protocol error, this
165 method terminates the process.
166
167 pty->login (int cmd_pid, bool login_shell, const char *hostname)
168 Creates an entry in the systems session database(s) (utmp, wtmp,
169 lastlog). "cmd_pid" must be the pid of the process representing the
170 session (such as the login shell), "login_shell" defines whether the
171 session is associated with a login, which influences whether wtmp
172 and lastlog entries are created, and "hostname" should identify the
173 "hostname" the user logs in from, which often is the value of the
174 "DISPLAY" variable or tty line in case of local logins.
175
176 Calling this method is optional. A session starts at the time of the
177 login call and extends until the ptytty object is destroyed.
178
179 pty->close_tty ()
180 Closes the tty. Useful after forking in the parent/pty process.
181
182 bool success = pty->make_controlling_tty ()
183 Tries to make the pty/tty pair the controlling terminal of the
184 current process. Useful after forking in the child/tty process.
185
186 pty->set_utf8_mode (bool on)
187 On systems supporting special UTF-8 line disciplines (e.g. Linux),
188 this tries to enable this discipline for the given pty. Can be
189 called at any time to change the mode.
190
191 C INTERFACE: THE ptytty FAMILY OF FUNCTIONS
192 ptytty_init ()
193 See "ptytty::init ()".
194
195 PTYTTY ptytty_create ()
196 Creates a new opaque PTYTTY object and returns it. Do not try to
197 access it in any way except by testing it for truthness (e.g. "if
198 (pty) ...."). See "ptytty::create ()".
199
200 int ptytty_pty (PTYTTY ptytty)
201 Return the pty file descriptor. See "pty->pty".
202
203 int ptytty_tty (PTYTTY ptytty)
204 Return the tty file descriptor. See "pty->tty".
205
206 void ptytty_delete (PTYTTY ptytty)
207 Destroys the PTYTTY object, freeing the pty/tty pair and cleaning up
208 the utmp/wtmp/lastlog databases, if initialised/used. Same as
209 "delete pty" in C++.
210
211 int ptytty_get (PTYTTY ptytty)
212 See "pty->get", returns 0 in case of an error, non-zero otherwise.
213
214 void ptytty_login (PTYTTY ptytty, int cmd_pid, bool login_shell, const
215 char *hostname)
216 See "pty->login".
217
218 void ptytty_close_tty (PTYTTY ptytty)
219 See "pty->close_tty".
220
221 int ptytty_make_controlling_tty (PTYTTY ptytty)
222 See "pty->make_controlling_tty".
223
224 void ptytty_set_utf8_mode (PTYTTY ptytty, int on)
225 See "pty->set_utf8_mode".
226
227 void ptytty_drop_privileges ()
228 See "ptytty::drop_privileges".
229
230 void ptytty_use_helper ()
231 See "ptytty::use_helper".
232
233 BUGS
234 You kiddin'?
235
236 AUTHORS
237 Emanuele Giaquinta <e.giaquinta@glauco.it>, Marc Alexander Lehmann
238 <rxvt-unicode@schmorp.de>.
239