1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* gnome-display-properties-install-systemwide - Install a RANDR profile for the whole system
5
* Copyright (C) 2010 Novell, Inc.
7
* Authors: Federico Mena Quintero <federico@novell.com>
9
* Licensed under the GNU General Public License Version 2
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33
#include <sys/types.h>
36
#include <glib/gi18n.h>
39
#define SYSTEM_RANDR_PATH "/etc/gnome-settings-daemon/xrandr"
42
usage (const char *program_name)
44
g_print (_("Usage: %s SOURCE_FILE DEST_NAME\n"
46
"This program installs a RANDR profile for multi-monitor setups into\n"
47
"a systemwide location. The resulting profile will get used when\n"
48
"the RANDR plug-in gets run in gnome-settings-daemon.\n"
50
"SOURCE_FILE - a full pathname, typically /home/username/.config/monitors.xml\n"
52
"DEST_NAME - relative name for the installed file. This will get put in\n"
53
" the systemwide directory for RANDR configurations,\n"
54
" so the result will typically be %s/DEST_NAME\n"),
60
is_basename (const char *filename)
62
if (*filename == '\0')
63
return FALSE; /* no empty strings, please */
65
for (; *filename; filename++)
66
if (G_IS_DIR_SEPARATOR (*filename))
73
copy_file (int source_fd, int dest_fd)
82
num_read = read (source_fd, buf, sizeof (buf));
94
while (num_read > 0) {
95
num_written = write (dest_fd, p, num_read);
96
if (num_written == -1) {
103
num_read -= num_written;
111
/* This is essentially a "please copy a file to a privileged location" program.
112
* We try to be paranoid in the following ways:
114
* - We copy only regular files, owned by the user who called pkexec(1), to
115
* avoid attacks like "copy a file that I'm not allowed to read into a
116
* world-readable location".
118
* - We copy only to a well-known directory.
120
* - We try to avoid race conditions. We only fstat() files that we have open
121
* to avoid files moving under our feet. We only create files in directories
124
* - We replace the destination file atomically.
128
main (int argc, char **argv)
131
const char *source_filename;
132
const char *dest_name;
133
const char *pkexec_uid_str;
141
setlocale (LC_ALL, "");
142
bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
143
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
144
textdomain (GETTEXT_PACKAGE);
146
/* We only run as root */
149
if (uid != 0 || euid != 0) {
150
/* Translators: only able to install RANDR profiles as root */
151
g_print ("%s\n", _("This program can only be used by the root user"));
155
/* Usage: gsd-xrandr-install-systemwide SOURCE_FILE DEST_NAME */
162
source_filename = argv[1];
165
/* Absolute source filenames only, please */
167
if (!g_path_is_absolute (source_filename)) {
168
g_print ("%s\n", _("The source filename must be absolute"));
172
/* We only copy regular files */
174
source_fd = open (source_filename, O_RDONLY);
175
if (source_fd == -1) {
178
/* Translators: first %s is a filename; second %s is an error message */
179
g_print (_("Could not open %s: %s\n"),
185
if (fstat (source_fd, &statbuf) != 0) {
187
/* Translators: first %s is a filename; second %s is an error message */
188
g_print (_("Could not get information for %s: %s\n"),
194
if (!S_ISREG (statbuf.st_mode)) {
195
g_print (_("%s must be a regular file\n"),
200
/* We only copy files that are really owned by the calling user */
202
pkexec_uid_str = g_getenv ("PKEXEC_UID");
203
if (pkexec_uid_str == NULL) {
204
g_print ("%s\n", _("This program must only be run through pkexec(1)"));
208
if (sscanf (pkexec_uid_str, "%d", &pkexec_uid) != 1) {
209
g_print ("%s\n", _("PKEXEC_UID must be set to an integer value"));
213
if (statbuf.st_uid != pkexec_uid) {
214
/* Translators: we are complaining that a file must be really owned by the user who called this program */
215
g_print (_("%s must be owned by you\n"), source_filename);
219
/* We only accept basenames for the destination */
221
if (!is_basename (dest_name)) {
222
/* Translators: here we are saying that a plain filename must look like "filename", not like "some_dir/filename" */
223
g_print (_("%s must not have any directory components\n"),
228
/* Chdir to the destination directory to keep it open... */
230
if (chdir (SYSTEM_RANDR_PATH) != 0) {
231
g_print (_("%s must be a directory\n"), SYSTEM_RANDR_PATH);
235
/* ... and open our temporary destination file right there */
237
strcpy (template, "gsd-XXXXXX");
238
dest_fd = g_mkstemp_full (template, O_WRONLY, 0644);
241
/* Translators: the first %s/%s is a directory/filename; the last %s is an error message */
242
g_print (_("Could not open %s/%s: %s\n"),
251
if (!copy_file (source_fd, dest_fd)) {
252
/* If something went wrong, remove the destination file to avoid leaving trash around */
257
/* Rename to the final filename */
259
if (rename (template, dest_name) != 0) {
262
g_print (_("Could not rename %s to %s: %s\n"),
269
/* Whew! We'll leave the final closing of the files to the almighty kernel. */