ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/proxy.C
Revision: 1.5
Committed: Tue Jan 16 18:25:51 2007 UTC (17 years, 5 months ago) by ayin
Content type: text/plain
Branch: MAIN
Changes since 1.4: +2 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 // This file is part of libptytty. Do not make local modifications.
2     // http://software.schmorp.de/pkg/libptytty
3    
4     /*----------------------------------------------------------------------*
5     * File: proxy.C
6     *----------------------------------------------------------------------*
7     *
8     * All portions of code are copyright by their respective author/s.
9     * Copyright (c) 2006 Marc Lehmann <pcg@goof.com>
10     *
11     * This program is free software; you can redistribute it and/or modify
12     * it under the terms of the GNU General Public License as published by
13     * the Free Software Foundation; either version 2 of the License, or
14     * (at your option) any later version.
15     *
16     * This program is distributed in the hope that it will be useful,
17     * but WITHOUT ANY WARRANTY; without even the implied warranty of
18     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19     * GNU General Public License for more details.
20     *
21     * You should have received a copy of the GNU General Public License
22     * along with this program; if not, write to the Free Software
23     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24     *---------------------------------------------------------------------*/
25    
26     #include "../config.h"
27    
28     #include "ptytty.h"
29    
30     #include <csignal>
31    
32     #include <sys/types.h>
33     #include <sys/socket.h>
34     #include <unistd.h>
35     #include <fcntl.h>
36    
37 root 1.4 // helper/proxy support
38    
39     #if PTYTTY_HELPER
40    
41 root 1.1 static int sock_fd = -1, lock_fd = -1;
42     static int helper_pid, owner_pid;
43    
44     struct command
45     {
46     enum { get, login, destroy } type;
47    
48     ptytty *id;
49    
50     bool login_shell;
51     int cmd_pid;
52     char hostname[512]; // arbitrary, but should be plenty
53     };
54    
55     struct ptytty_proxy : ptytty
56     {
57     ptytty *id;
58    
59     ptytty_proxy ()
60     : id(0)
61     {
62     }
63    
64     ~ptytty_proxy ();
65    
66     bool get ();
67     void login (int cmd_pid, bool login_shell, const char *hostname);
68     };
69    
70     #if PTYTTY_REENTRANT
71     # define NEED_TOKEN do { char ch; read (lock_fd, &ch, 1); } while (0)
72     # define GIVE_TOKEN do { char ch; write (lock_fd, &ch, 1); } while (0)
73     #else
74     # define NEED_TOKEN (void)0
75     # define GIVE_TOKEN (void)0
76     #endif
77    
78     bool
79     ptytty_proxy::get ()
80     {
81     NEED_TOKEN;
82    
83     command cmd;
84    
85     cmd.type = command::get;
86    
87     write (sock_fd, &cmd, sizeof (cmd));
88    
89     if (read (sock_fd, &id, sizeof (id)) != sizeof (id))
90     ptytty_fatal ("protocol error while creating pty using helper process, aborting.\n");
91    
92     if (!id)
93     {
94     GIVE_TOKEN;
95     return false;
96     }
97    
98     if ((pty = recv_fd (sock_fd)) < 0
99     || (tty = recv_fd (sock_fd)) < 0)
100     ptytty_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n");
101    
102     GIVE_TOKEN;
103     return true;
104     }
105    
106     void
107     ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname)
108     {
109     NEED_TOKEN;
110    
111     command cmd;
112    
113     cmd.type = command::login;
114     cmd.id = id;
115     cmd.cmd_pid = cmd_pid;
116     cmd.login_shell = login_shell;
117     strncpy (cmd.hostname, hostname, sizeof (cmd.hostname));
118    
119     write (sock_fd, &cmd, sizeof (cmd));
120    
121     GIVE_TOKEN;
122     }
123    
124     ptytty_proxy::~ptytty_proxy ()
125     {
126     if (id)
127     {
128 ayin 1.5 close (pty);
129    
130 root 1.1 NEED_TOKEN;
131    
132     command cmd;
133    
134     cmd.type = command::destroy;
135     cmd.id = id;
136    
137     write (sock_fd, &cmd, sizeof (cmd));
138    
139     GIVE_TOKEN;
140     }
141     }
142    
143     static
144     void serve ()
145     {
146     command cmd;
147     vector<ptytty *> ptys;
148    
149     for (;;)
150     {
151     GIVE_TOKEN;
152    
153     if (read (sock_fd, &cmd, sizeof (command)) != sizeof (command))
154     break;
155    
156     if (cmd.type == command::get)
157     {
158     // -> id ptyfd ttyfd
159     cmd.id = new ptytty_unix;
160    
161     if (cmd.id->get ())
162     {
163     write (sock_fd, &cmd.id, sizeof (cmd.id));
164     ptys.push_back (cmd.id);
165    
166     ptytty::send_fd (sock_fd, cmd.id->pty);
167     ptytty::send_fd (sock_fd, cmd.id->tty);
168     }
169     else
170     {
171     delete cmd.id;
172     cmd.id = 0;
173     write (sock_fd, &cmd.id, sizeof (cmd.id));
174     }
175     }
176     else if (cmd.type == command::login)
177     {
178     #if UTMP_SUPPORT
179     if (find (ptys.begin (), ptys.end (), cmd.id) != ptys.end ())
180     {
181     cmd.hostname[sizeof (cmd.hostname) - 1] = 0;
182     cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname);
183     }
184     #endif
185     }
186     else if (cmd.type == command::destroy)
187     {
188     vector<ptytty *>::iterator pty = find (ptys.begin (), ptys.end (), cmd.id);
189    
190     if (pty != ptys.end ())
191     {
192     delete *pty;
193     ptys.erase (pty);
194     }
195     }
196     else
197     break;
198    
199     NEED_TOKEN;
200     }
201    
202     // destroy all ptys
203     for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); )
204     delete *i;
205     }
206    
207     void
208     ptytty::use_helper ()
209     {
210     #ifndef PTYTTY_NO_PID_CHECK
211     int pid = getpid ();
212     #endif
213    
214     if (sock_fd >= 0
215     #ifndef PTYTTY_NO_PID_CHECK
216     && pid == owner_pid
217     #endif
218     )
219     return;
220    
221     #ifndef PTYTTY_NO_PID_CHECK
222     owner_pid = pid;
223     #endif
224    
225     int sv[2];
226    
227     if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
228     ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
229    
230     #ifdef PTYTTY_REENTRANT
231     int lv[2];
232    
233     if (socketpair (AF_UNIX, SOCK_STREAM, 0, lv))
234     ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
235     #endif
236    
237     helper_pid = fork ();
238    
239     if (helper_pid < 0)
240     ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n");
241    
242     if (helper_pid)
243     {
244     // client, process
245     sock_fd = sv[0];
246     close (sv[1]);
247     fcntl (sock_fd, F_SETFD, FD_CLOEXEC);
248     #ifdef PTYTTY_REENTRANT
249     lock_fd = lv[0];
250     close (lv[1]);
251     fcntl (lock_fd, F_SETFD, FD_CLOEXEC);
252     #endif
253     }
254     else
255     {
256     // server, pty-helper
257     sock_fd = sv[1];
258     #ifdef PTYTTY_REENTRANT
259     lock_fd = lv[1];
260     #endif
261    
262     chdir ("/");
263    
264     signal (SIGHUP, SIG_IGN);
265     signal (SIGTERM, SIG_IGN);
266     signal (SIGINT, SIG_IGN);
267     signal (SIGPIPE, SIG_IGN);
268    
269     for (int fd = 0; fd < 1023; fd++)
270     if (fd != sock_fd && fd != lock_fd)
271     close (fd);
272    
273     serve ();
274     _exit (EXIT_SUCCESS);
275     }
276     }
277    
278     #endif
279    
280     ptytty *
281     ptytty::create ()
282     {
283     #if PTYTTY_HELPER
284     if (helper_pid
285     # ifndef PTYTTY_NO_PID_CHECK
286     && getpid () == owner_pid
287     # endif
288     )
289     // use helper process
290     return new ptytty_proxy;
291     else
292     #endif
293     return new ptytty_unix;
294     }
295    
296     void
297     ptytty::init ()
298     {
299     uid_t uid = getuid ();
300     gid_t gid = getgid ();
301    
302     // before doing anything else, check for setuid/setgid operation,
303     // start the helper process and drop privileges
304     if (uid != geteuid ()
305     || gid != getegid ())
306     {
307     #if PTYTTY_HELPER
308     use_helper ();
309     #else
310 root 1.2 ptytty_warn ("running setuid/setgid without pty helper compiled in, continuing unprivileged.\n", 0);
311 root 1.1 #endif
312    
313     drop_privileges ();
314     }
315     }
316    
317     void
318     ptytty::drop_privileges ()
319     {
320     uid_t uid = getuid ();
321     gid_t gid = getgid ();
322    
323     // drop privileges
324     #if HAVE_SETRESUID
325     setresgid (gid, gid, gid);
326     setresuid (uid, uid, uid);
327     #elif HAVE_SETREUID
328     setregid (gid, gid);
329     setreuid (uid, uid);
330     #elif HAVE_SETUID
331     setgid (gid);
332     setuid (uid);
333 root 1.3 #else
334     # error no way to drop privileges, configure failed?
335 root 1.1 #endif
336    
337     if (uid != geteuid ()
338     || gid != getegid ())
339     ptytty_fatal ("unable to drop privileges, aborting.\n");
340     }
341