1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright (C) 2006 William Jon McCann <mccann@jhu.edu>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29
#include <sys/types.h>
31
#include <sys/socket.h>
32
#include <sys/ioctl.h>
43
#include <sys/consio.h>
46
#ifdef HAVE_GETPEERUCRED
50
#include "ck-sysdeps.h"
56
/* Adapted from dbus-sysdeps-unix.c:_dbus_read_credentials_socket() */
58
ck_get_socket_peer_credentials (int socket_fd,
77
if (getsockopt (socket_fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) == 0 && cr_len == sizeof (cr)) {
82
g_warning ("Failed to getsockopt() credentials, returned len %d/%d: %s\n",
87
#elif defined(HAVE_GETPEERUCRED)
91
if (getpeerucred (socket_fd, &ucred) == 0) {
92
pid_read = ucred_getpid (ucred);
93
uid_read = ucred_geteuid (ucred);
96
g_warning ("Failed to getpeerucred() credentials: %s\n",
102
#else /* !SO_PEERCRED && !HAVE_GETPEERUCRED */
103
g_warning ("Socket credentials not supported on this OS\n");
121
* Get an fd for use with kbd/console ioctls.
122
* We try several things because opening /dev/console will fail
123
* if someone else used X (which does a chown on /dev/console).
127
ck_fd_is_a_console (int fd)
131
#elif defined(__FreeBSD__)
138
kb_ok = (ioctl (fd, VT_GETSTATE, &vts) == 0);
139
#elif defined(__FreeBSD__)
140
kb_ok = (ioctl (fd, CONS_GETVERS, &vers) == 0);
145
return (isatty (fd) && kb_ok);
149
open_a_console (char *fnam)
153
fd = open (fnam, O_RDONLY | O_NOCTTY);
154
if (fd < 0 && errno == EACCES)
155
fd = open (fnam, O_WRONLY | O_NOCTTY);
160
if (! ck_fd_is_a_console (fd)) {
169
ck_get_a_console_fd (void)
176
/* On Solaris, first try Sun VT device. */
177
fd = open_a_console ("/dev/vt/active");
181
fd = open_a_console ("/dev/vt/0");
188
fd = open_a_console (_PATH_TTY);
194
fd = open_a_console ("/dev/tty");
199
fd = open_a_console ("/dev/tty0");
205
fd = open_a_console (_PATH_CONSOLE);
211
fd = open_a_console ("/dev/console");
216
for (fd = 0; fd < 3; fd++) {
217
if (ck_fd_is_a_console (fd)) {
226
ck_is_root_user (void)
229
uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
230
gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
232
#ifdef HAVE_GETRESUID
233
if (getresuid (&ruid, &euid, &suid) != 0 ||
234
getresgid (&rgid, &egid, &sgid) != 0)
235
#endif /* HAVE_GETRESUID */
237
suid = ruid = getuid ();
238
sgid = rgid = getgid ();
252
ck_wait_for_active_console_num (int console_fd,
258
g_assert (console_fd != -1);
265
g_debug ("VT_WAITACTIVE for vt %d", num);
266
res = ioctl (console_fd, VT_WAITACTIVE, num);
267
g_debug ("VT_WAITACTIVE for vt %d returned %d", num, res);
276
errmsg = g_strerror (errno);
278
if (errno == EINTR) {
279
g_debug ("Interrupted waiting for native console %d activation: %s",
283
} else if (errno == ENOTSUP) {
284
g_debug ("Console activation not supported on this system");
286
g_warning ("Error waiting for native console %d activation: %s",
300
ck_activate_console_num (int console_fd,
306
g_assert (console_fd != -1);
312
res = ioctl (console_fd, VT_ACTIVATE, num);
321
if (errno == ENOTSUP) {
322
g_debug ("Console activation not supported on this system");
324
g_warning ("Unable to activate console: %s", g_strerror (errno));