ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/libptytty.h
(Generate patch)

Comparing rxvt-unicode/src/libptytty.h (file contents):
Revision 1.1 by root, Sun Jan 22 09:57:48 2006 UTC vs.
Revision 1.4 by root, Mon Jan 23 12:37:59 2006 UTC

1// This file is part of libptytty. Do not make local modifications. 1=head1 NAME
2// http://software.schmorp.de/pkg/libptytty
3 2
4#ifndef LIBPTYTTY_H_ /* public libptytty header file */ 3libptytty - OS independent and secure pty/tty and utmp/wtmp/lastlog handling
5#define LIBPTYTTY_H_
6 4
7struct ptytty { 5=head1 SYNOPSIS
8 int pty; // pty file descriptor; connected to rxvt
9 int tty; // tty file descriptor; connected to child
10 6
11 virtual ~ptytty () 7 -lptytty
8
9=head1 DESCRIPTION
10
11TODO
12
13=head1 SECURITY CONSIDERATIONS
14
15I<< B<It is of paramount importance that you at least read the following
16paragraph!> >>
17
18If you are a typical terminal-like program that just wants one or more
19ptys, you should call the C<ptytty::init ()> method (C: C<ptytty_init ()
20function) as the very first thing in your program:
21
22 int main (int argc, char *argv[])
12 { 23 {
24 // do nothing here
25 ptytty::init ();
26 // in C: ptytty_init ();
27
28 // initialise, parse arguments, etc.
13 } 29 }
14 30
15 virtual bool get () = 0; 31This checks wether the program runs setuid or setgid. If yes then it will
32fork a helper process and drop privileges.
33
34Some programs need finer control over if and when this helper process
35is started, and if and how to drop privileges. For those programs, the
36methods C<ptytty::use_helper> and C<ptytty::drop_privileges> are more
37useful.
38
39=head1 C++ INTERFACE: THE ptytty CLASS
40
41=head2 STATIC METHODS
42
43=over 4
44
45=item ptytty::init ()
46
47The default way to initialise libptytty. Must be called imemdiately as
48the first thing in the C<main> function, or earlier e.g. during static
49construction time. The earlier, the better.
50
51This method checks wether the program runs with setuid/setgid permissions
52and, if yes, spawns a helper process for pty/tty management. IT then
53drops the privileges completely, so the actual program runs without
54setuid/setgid privileges.
55
56=item ptytty::use_helper ()
57
58Tries to start a helper process that retains privileges even when the
59calling process does not. This is usually called from C<ptytty::init> when
60it detects that the program is running setuid or setgid, but can be called
61manually if it is inconvinient to drop privileges at startup, or when
62you are not running setuid/setgid but want to drop privileges (e.g. when
63running as a root-started daemon).
64
65This method will try not to start more than one helper process. The same
66helper process can usually be used from the process starting it an all its
67fork'ed (not exec'ed) children.
68
69Please note that starting a helper process after dropping privileges makes
70no sense.
71
72=item ptytty::drop_privileges ()
73
74Drops privileges completely, i.e. sets real, effective and saved user id
75to the real user id. Also aborts if this cnanot be achieved. Useful to
76make sure that the process doesn't run with special privileges.
77
78=item bool success = ptytty::send_fd (int socket, int fd)
79
80Utility method to send a file descriptor over a unix domain
81socket. Returns true if successful, false otherwise. This method is only
82exposed for your convinience and is not required for normal operation.
83
84=item int fd = ptytty::recv_fd (int socket)
85
86Utility method to receive a file descriptor over a unix domain
87socket. Returns the fd if sucecssful and C<-1> otherwise. This method
88is only exposed for your convinience and is not required for normal
89operation.
90
91=item ptytty *pty = ptytty::create ()
92
93Creates new ptytty object. Creation does not yet do anything besides
94allocating the structure.
95
96A static method is used because the actual ptytty implementation can
97differ at runtime, so you need a dynamic object creation facility.
98
99=back
100
101
102=head2 DYNAMIC/SESSION-RELATED DATA MEMBERS AND METHODS
103
104=over 4
105
106=item int pty_fd = pty->pty
107
108=item int tty_fd = pty->tty
109
110These members contain the pty and tty file descriptors, respectively. They
111initially contain C<-1> until a successful to C<ptytty::get>.
112
113=item bool success = pty->get ()
114
115Tries to find, allocate and initialise a new pty/tty pair. Returns C<true>
116when successful.
117
16 virtual void login (int cmd_pid, bool login_shell, const char *hostname) = 0; 118=item pty->login (int cmd_pid, bool login_shell, const char *hostname)
17 119
18 void close_tty (); 120Creates an entry in the systems session database(s) (utmp, wtmp, lastlog).
19 bool make_controlling_tty (); 121C<cmd_pid> must be the pid of the process representing the session
20 void set_utf8_mode (bool on); 122(such as the login shell), C<login_shell> defines wether the session is
123associated with a login, which influences wether wtmp and lastlog entries
124are created, and C<hostname> should identify the "hostname" the user logs
125in from, which often is the value of the C<DISPLAY> variable or tty line
126in case of local logins.
21 127
22 static void init (); 128Calling this method is optional. A session starts at the time of the login
23 static ptytty *create (); // create a new pty object 129call and extends until the ptytty object is destroyed.
24 130
25 static void drop_privileges (); 131=item pty->close_tty ()
26 static void use_helper ();
27 132
28 static int send_fd (int socket, int fd); 133Closes the tty. Useful after forking in the parent/pty process.
29 static int recv_fd (int socket);
30 134
31protected: 135=item bool success = pty->make_controlling_tty ()
32 136
33 ptytty () 137Tries to make the pty/tty pair the controlling terminal of the current
34 : pty(-1), tty(-1) 138process. Useful after forking in the child/tty process.
35 {
36 }
37};
38 139
39#endif 140=item pty->set_utf8_mode (bool on)
40 141
142On systems supporting special UTF-8 line disciplines (e.g. Linux), tries
143to enable it for the given pty. Can be called at any time to change the
144mode.
145
146=back
147
148
149=head1 C INTERFACE: THE ptytty FAMILY OF FUNCTIONS
150
151=over 4
152
153=item ptytty_init ()
154
155See C<ptytty::init ()>.
156
157=item PTYTTY ptytty_create ()
158
159Creates a new opaque PTYTTY object and returns it. Do not try to access it
160in any way excecp by testing it for truthness (e.g. C<if (pty) ....>). See
161C<ptytty::create ()>.
162
163=item int ptytty_pty (PTYTTY ptytty)
164
165Return the pty file descriptor. See C<< pty->pty >>.
166
167=item int ptytty_tty (PTYTTY ptytty)
168
169Return the tty file descriptor. See C<< pty->tty >>.
170
171=item void ptytty_delete (PTYTTY ptytty)
172
173Destroys the PTYTTY object, freeing the pty/tty pair and cleaning up the
174utmp/wtmp/lastlog databases, if initialised/used. Same as C<delete pty> in
175C++.
176
177=item int ptytty_get (PTYTTY ptytty)
178
179See C<< pty->get >>, returns 0 in case of an error, non-zero otherwise.
180
181=item void ptytty_login (PTYTTY ptytty, int cmd_pid, bool login_shell, const char *hostname)
182
183See C<< pty->login >>.
184
185=item void ptytty_close_tty (PTYTTY ptytty)
186
187See C<< pty->close_tty >>.
188
189=item int ptytty_make_controlling_tty (PTYTTY ptytty)
190
191See C<< pty->make_controlling_tty >>.
192
193=item void ptytty_set_utf8_mode (PTYTTY ptytty, int on)
194
195See C<< pty->set_utf8_mode >>.
196
197=item void ptytty_drop_privileges ()
198
199See C<< ptytty::drop_privileges >>.
200
201=item void ptytty_use_helper ()
202
203See C<< ptytty::use_helper >>.
204
205=back
206
207
208=head1 BUGS
209
210You kiddin'?
211
212=head1 AUTHORS
213
214Emanuele Giaquinta L<< <e.giaquinta@glauco.it> >>, Marc Alexander Lehmann
215L<< <rxvt-unicode@schmorp.de> >>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines