~ubuntu-branches/ubuntu/gutsy/vnc4/gutsy

« back to all changes in this revision

Viewing changes to unix/xc/programs/Xserver/hw/xfree86/os-support/linux/lnx_init.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2006-05-15 20:35:17 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060515203517-l4lre1ku942mn26k
Tags: 4.1.1+X4.3.0-10
* Correction of critical security issue. Thanks to Martin Kogler
  <e9925248@student.tuwien.ac.at> that informed me about the issue,
  and provided the patch.
  This flaw was originally found by Steve Wiseman of intelliadmin.com.
* Applied patch from Javier Kohen <jkohen@users.sourceforge.net> that
  inform the user that only 8 first characters of the password will
  actually be used when typing more than 8 characters, closes:
  #355619.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/lnx_init.c,v 3.14 2001/10/31 22:50:30 tsi Exp $ */
 
2
/*
 
3
 * Copyright 1992 by Orest Zborowski <obz@Kodak.com>
 
4
 * Copyright 1993 by David Wexelblat <dwex@goblin.org>
 
5
 *
 
6
 * Permission to use, copy, modify, distribute, and sell this software and its
 
7
 * documentation for any purpose is hereby granted without fee, provided that
 
8
 * the above copyright notice appear in all copies and that both that
 
9
 * copyright notice and this permission notice appear in supporting
 
10
 * documentation, and that the names of Orest Zborowski and David Wexelblat 
 
11
 * not be used in advertising or publicity pertaining to distribution of 
 
12
 * the software without specific, written prior permission.  Orest Zborowski
 
13
 * and David Wexelblat make no representations about the suitability of this 
 
14
 * software for any purpose.  It is provided "as is" without express or 
 
15
 * implied warranty.
 
16
 *
 
17
 * OREST ZBOROWSKI AND DAVID WEXELBLAT DISCLAIMS ALL WARRANTIES WITH REGARD 
 
18
 * TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 
 
19
 * FITNESS, IN NO EVENT SHALL OREST ZBOROWSKI OR DAVID WEXELBLAT BE LIABLE 
 
20
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
 
21
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
 
22
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
 
23
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
24
 *
 
25
 */
 
26
/* $XConsortium: lnx_init.c /main/7 1996/10/23 18:46:30 kaleb $ */
 
27
 
 
28
#include "X.h"
 
29
#include "Xmd.h"
 
30
 
 
31
#include "compiler.h"
 
32
 
 
33
#include "xf86.h"
 
34
#include "xf86Priv.h"
 
35
#include "xf86_OSlib.h"
 
36
#include "lnx.h"
 
37
 
 
38
#ifdef USE_DEV_FB
 
39
extern char *getenv(const char *);
 
40
#include <linux/fb.h>
 
41
char *fb_dev_name;
 
42
#endif
 
43
 
 
44
static Bool KeepTty = FALSE;
 
45
static int VTnum = -1;
 
46
static int activeVT = -1;
 
47
 
 
48
void
 
49
xf86OpenConsole(void)
 
50
{
 
51
    int i, fd = -1;
 
52
    int result;
 
53
    struct vt_mode VT;
 
54
    char vtname[11];
 
55
    struct vt_stat vts;
 
56
    MessageType from = X_PROBED;
 
57
#ifdef USE_DEV_FB
 
58
    struct fb_var_screeninfo var;
 
59
    int fbfd;
 
60
#endif
 
61
    char *tty0[] = { "/dev/tty0", "/dev/vc/0", NULL };
 
62
    char *vcs[] = { "/dev/vc/%d", "/dev/tty%d", NULL };
 
63
 
 
64
    if (serverGeneration == 1) 
 
65
    {
 
66
        /* check if we're run with euid==0 */
 
67
        if (geteuid() != 0)
 
68
        {
 
69
            FatalError("xf86OpenConsole: Server must be suid root\n");
 
70
        }
 
71
 
 
72
        /*
 
73
         * setup the virtual terminal manager
 
74
         */
 
75
        if (VTnum != -1) {
 
76
            xf86Info.vtno = VTnum;
 
77
            from = X_CMDLINE;
 
78
        } else {
 
79
            i=0;
 
80
            while (tty0[i] != NULL)
 
81
            {
 
82
                if ((fd = open(tty0[i],O_WRONLY,0)) >= 0)
 
83
                  break;
 
84
                i++;
 
85
            }
 
86
            if (fd < 0)
 
87
                FatalError(
 
88
                    "xf86OpenConsole: Cannot open /dev/tty0 (%s)\n",
 
89
                    strerror(errno));
 
90
            if ((ioctl(fd, VT_OPENQRY, &xf86Info.vtno) < 0) ||
 
91
                (xf86Info.vtno == -1)) {
 
92
                FatalError("xf86OpenConsole: Cannot find a free VT\n");
 
93
            }
 
94
            close(fd);
 
95
        }
 
96
 
 
97
#ifdef USE_DEV_FB
 
98
        fb_dev_name=getenv("FRAMEBUFFER");
 
99
        if (!fb_dev_name)
 
100
            fb_dev_name="/dev/fb0current";
 
101
        if ((fbfd = open(fb_dev_name, O_RDONLY)) < 0)
 
102
            FatalError("xf86OpenConsole: Cannot open %s (%s)\n",
 
103
        fb_dev_name, strerror(errno));
 
104
        if (ioctl(fbfd, FBIOGET_VSCREENINFO, &var))
 
105
            FatalError("xf86OpenConsole: Unable to get screen info\n");
 
106
#endif
 
107
        xf86Msg(from, "using VT number %d\n\n", xf86Info.vtno);
 
108
 
 
109
        if (!KeepTty) {
 
110
            setpgrp();
 
111
        }
 
112
 
 
113
        i=0;
 
114
        while (vcs[i] != NULL)
 
115
        {
 
116
            sprintf(vtname, vcs[i], xf86Info.vtno); /* /dev/tty1-64 */
 
117
            if ((xf86Info.consoleFd = open(vtname, O_RDWR|O_NDELAY, 0)) >= 0)
 
118
                break;
 
119
            i++;
 
120
        }
 
121
 
 
122
        if (xf86Info.consoleFd < 0) {
 
123
            FatalError("xf86OpenConsole: Cannot open virtual console %d (%s)\n",
 
124
                       xf86Info.vtno, strerror(errno));
 
125
        }
 
126
 
 
127
        /* change ownership of the vt */
 
128
        chown(vtname, getuid(), getgid());
 
129
 
 
130
        /*
 
131
         * the current VT device we're running on is not "console", we want
 
132
         * to grab all consoles too
 
133
         *
 
134
         * Why is this needed??
 
135
         */
 
136
        chown("/dev/tty0", getuid(), getgid());
 
137
 
 
138
        /*
 
139
         * Linux doesn't switch to an active vt after the last close of a vt,
 
140
         * so we do this ourselves by remembering which is active now.
 
141
         */
 
142
        if (ioctl(xf86Info.consoleFd, VT_GETSTATE, &vts) == 0)
 
143
        {
 
144
            activeVT = vts.v_active;
 
145
        }
 
146
 
 
147
        if (!KeepTty)
 
148
        {
 
149
            /*
 
150
             * Detach from the controlling tty to avoid char loss
 
151
             */
 
152
            if ((i = open("/dev/tty",O_RDWR)) >= 0)
 
153
            {
 
154
                ioctl(i, TIOCNOTTY, 0);
 
155
                close(i);
 
156
            }
 
157
        }
 
158
 
 
159
        /*
 
160
         * now get the VT
 
161
         */
 
162
        SYSCALL(result = ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno));
 
163
        if (result != 0)
 
164
        {
 
165
            xf86Msg(X_WARNING, "xf86OpenConsole: VT_ACTIVATE failed\n");
 
166
        }
 
167
        SYSCALL(result =
 
168
                  ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno));
 
169
        if (result != 0)
 
170
        {
 
171
            xf86Msg(X_WARNING, "xf86OpenConsole: VT_WAITACTIVE failed\n");
 
172
        }
 
173
        SYSCALL(result = ioctl(xf86Info.consoleFd, VT_GETMODE, &VT));
 
174
        if (result < 0) 
 
175
        {
 
176
            FatalError("xf86OpenConsole: VT_GETMODE failed\n");
 
177
        }
 
178
 
 
179
        signal(SIGUSR1, xf86VTRequest);
 
180
 
 
181
        VT.mode = VT_PROCESS;
 
182
        VT.relsig = SIGUSR1;
 
183
        VT.acqsig = SIGUSR1;
 
184
        if (ioctl(xf86Info.consoleFd, VT_SETMODE, &VT) < 0) 
 
185
        {
 
186
            FatalError("xf86OpenConsole: VT_SETMODE VT_PROCESS failed\n");
 
187
        }
 
188
        if (ioctl(xf86Info.consoleFd, KDSETMODE, KD_GRAPHICS) < 0)
 
189
        {
 
190
            FatalError("xf86OpenConsole: KDSETMODE KD_GRAPHICS failed\n");
 
191
        }
 
192
 
 
193
        /* we really should have a InitOSInputDevices() function instead
 
194
         * of Init?$#*&Device(). So I just place it here */
 
195
        
 
196
#ifdef USE_DEV_FB
 
197
        /* copy info to new console */
 
198
        var.yoffset=0;
 
199
        var.xoffset=0;
 
200
        if (ioctl(fbfd, FBIOPUT_VSCREENINFO, &var))
 
201
            FatalError("Unable to set screen info\n");
 
202
        close(fbfd);
 
203
#endif
 
204
    }
 
205
    else 
 
206
    {
 
207
        /* serverGeneration != 1 */
 
208
        /*
 
209
         * now get the VT
 
210
         */
 
211
        SYSCALL(result = ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno));
 
212
        if (result != 0)
 
213
        {
 
214
            xf86Msg(X_WARNING, "xf86OpenConsole: VT_ACTIVATE failed\n");
 
215
        }
 
216
        SYSCALL(result =
 
217
                ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno));
 
218
        if (result != 0)
 
219
        {
 
220
            xf86Msg(X_WARNING, "xf86OpenConsole: VT_WAITACTIVE failed\n");
 
221
        }
 
222
    }
 
223
    return;
 
224
}
 
225
 
 
226
void
 
227
xf86CloseConsole()
 
228
{
 
229
    struct vt_mode   VT;
 
230
 
 
231
#if 0
 
232
    ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno);
 
233
    ioctl(xf86Info.consoleFd, VT_WAITACTIVE, 0);
 
234
#endif
 
235
    ioctl(xf86Info.consoleFd, KDSETMODE, KD_TEXT);  /* Back to text mode ... */
 
236
    if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) != -1)
 
237
    {
 
238
        VT.mode = VT_AUTO;
 
239
        ioctl(xf86Info.consoleFd, VT_SETMODE, &VT); /* set dflt vt handling */
 
240
    }
 
241
    /*
 
242
     * Perform a switch back to the active VT when we were started
 
243
     */
 
244
    if (activeVT >= 0)
 
245
    {
 
246
        ioctl(xf86Info.consoleFd, VT_ACTIVATE, activeVT);
 
247
        activeVT = -1;
 
248
    }
 
249
    close(xf86Info.consoleFd);                /* make the vt-manager happy */
 
250
    return;
 
251
}
 
252
 
 
253
int
 
254
xf86ProcessArgument(int argc, char *argv[], int i)
 
255
{
 
256
        /*
 
257
         * Keep server from detaching from controlling tty.  This is useful 
 
258
         * when debugging (so the server can receive keyboard signals.
 
259
         */
 
260
        if (!strcmp(argv[i], "-keeptty"))
 
261
        {
 
262
                KeepTty = TRUE;
 
263
                return(1);
 
264
        }
 
265
        if ((argv[i][0] == 'v') && (argv[i][1] == 't'))
 
266
        {
 
267
                if (sscanf(argv[i], "vt%2d", &VTnum) == 0)
 
268
                {
 
269
                        UseMsg();
 
270
                        VTnum = -1;
 
271
                        return(0);
 
272
                }
 
273
                return(1);
 
274
        }
 
275
        return(0);
 
276
}
 
277
 
 
278
void
 
279
xf86UseMsg()
 
280
{
 
281
        ErrorF("vtXX                   use the specified VT number\n");
 
282
        ErrorF("-keeptty               ");
 
283
        ErrorF("don't detach controlling tty (for debugging only)\n");
 
284
        return;
 
285
}