~kroq-gar78/ubuntu/precise/gnome-control-center/fix-885947

« back to all changes in this revision

Viewing changes to capplets/display/gnome-display-properties-install-systemwide.c

  • Committer: Bazaar Package Importer
  • Author(s): Rodrigo Moya
  • Date: 2011-05-17 10:47:27 UTC
  • mfrom: (0.1.11 experimental) (1.1.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20110517104727-lqel6m8vhfw5jby1
Tags: 1:3.0.1.1-1ubuntu1
* Rebase on Debian, remaining Ubuntu changes:
* debian/control:
  - Build-Depend on hardening-wrapper, dpkg-dev and dh-autoreconf
  - Add dependency on ubuntu-system-service
  - Remove dependency on gnome-icon-theme-symbolic
  - Move dependency on apg, gnome-icon-theme-symbolic and accountsservice to
    be a Recommends: until we get them in main
* debian/rules:
  - Use autoreconf
  - Add binary-post-install rule for gnome-control-center-data
  - Run dh-autoreconf
* debian/gnome-control-center.dirs:
* debian/gnome-control-center.links:
  - Add a link to the control center shell for indicators
* debian/patches/00_disable-nm.patch:
  - Temporary patch to disable building with NetworkManager until we get
    the new one in the archive
* debian/patches/01_git_remove_gettext_calls.patch:
  - Remove calls to AM_GNU_GETTEXT, IT_PROG_INTLTOOL should be enough
* debian/patches/01_git_kill_warning.patch:
  - Kill warning
* debian/patches/50_ubuntu_systemwide_prefs.patch:
  - Ubuntu specific proxy preferences
* debian/patches/51_ubuntu_system_keyboard.patch:
  - Implement the global keyboard spec at https://wiki.ubuntu.com/DefaultKeyboardSettings

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2
 
 *
3
 
 * gnome-display-properties-install-systemwide - Install a RANDR profile for the whole system
4
 
 *
5
 
 * Copyright (C) 2010 Novell, Inc.
6
 
 *
7
 
 * Authors: Federico Mena Quintero <federico@novell.com>
8
 
 *
9
 
 * Licensed under the GNU General Public License Version 2
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
 
 */
25
 
 
26
 
#include "config.h"
27
 
 
28
 
#include <errno.h>
29
 
#include <locale.h>
30
 
#include <stdio.h>
31
 
#include <stdlib.h>
32
 
#include <fcntl.h>
33
 
#include <sys/types.h>
34
 
#include <sys/stat.h>
35
 
#include <unistd.h>
36
 
#include <glib/gi18n.h>
37
 
#include <glib.h>
38
 
 
39
 
#define SYSTEM_RANDR_PATH "/etc/gnome-settings-daemon/xrandr"
40
 
 
41
 
static void
42
 
usage (const char *program_name)
43
 
{
44
 
        g_print (_("Usage: %s SOURCE_FILE DEST_NAME\n"
45
 
                   "\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"
49
 
                   "\n"
50
 
                   "SOURCE_FILE - a full pathname, typically /home/username/.config/monitors.xml\n"
51
 
                   "\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"),
55
 
                 program_name,
56
 
                 SYSTEM_RANDR_PATH);
57
 
}
58
 
 
59
 
static gboolean
60
 
is_basename (const char *filename)
61
 
{
62
 
        if (*filename == '\0')
63
 
                return FALSE; /* no empty strings, please */
64
 
 
65
 
        for (; *filename; filename++)
66
 
                if (G_IS_DIR_SEPARATOR (*filename))
67
 
                        return FALSE;
68
 
 
69
 
        return TRUE;
70
 
}
71
 
 
72
 
static gboolean
73
 
copy_file (int source_fd, int dest_fd)
74
 
{
75
 
        char buf[1024];
76
 
        int num_read;
77
 
        int num_written;
78
 
 
79
 
        while (TRUE) {
80
 
                char *p;
81
 
 
82
 
                num_read = read (source_fd, buf, sizeof (buf));
83
 
                if (num_read == 0)
84
 
                        break;
85
 
 
86
 
                if (num_read == -1) {
87
 
                        if (errno == EINTR)
88
 
                                continue;
89
 
                        else
90
 
                                return FALSE;
91
 
                }
92
 
 
93
 
                p = buf;
94
 
                while (num_read > 0) {
95
 
                        num_written = write (dest_fd, p, num_read);
96
 
                        if (num_written == -1) {
97
 
                                if (errno == EINTR)
98
 
                                        continue;
99
 
                                else
100
 
                                        return FALSE;
101
 
                        }
102
 
 
103
 
                        num_read -= num_written;
104
 
                        p += num_written;
105
 
                }
106
 
        }
107
 
 
108
 
        return TRUE;
109
 
}
110
 
 
111
 
/* This is essentially a "please copy a file to a privileged location" program.
112
 
 * We try to be paranoid in the following ways:
113
 
 *
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".
117
 
 *
118
 
 * - We copy only to a well-known directory.
119
 
 *
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
122
 
 *   that we have open.
123
 
 *
124
 
 * - We replace the destination file atomically.
125
 
 */
126
 
 
127
 
int
128
 
main (int argc, char **argv)
129
 
{
130
 
        uid_t uid, euid;
131
 
        const char *source_filename;
132
 
        const char *dest_name;
133
 
        const char *pkexec_uid_str;
134
 
        int pkexec_uid;
135
 
        struct stat statbuf;
136
 
        int err;
137
 
        int source_fd;
138
 
        int dest_fd;
139
 
        char template[100];
140
 
 
141
 
        setlocale (LC_ALL, "");
142
 
        bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
143
 
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
144
 
        textdomain (GETTEXT_PACKAGE);
145
 
 
146
 
        /* We only run as root */
147
 
        uid = getuid ();
148
 
        euid = geteuid ();
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"));
152
 
                return EXIT_FAILURE;
153
 
        }
154
 
 
155
 
        /* Usage: gsd-xrandr-install-systemwide SOURCE_FILE DEST_NAME */
156
 
 
157
 
        if (argc != 3) {
158
 
                usage (argv[0]);
159
 
                return EXIT_FAILURE;
160
 
        }
161
 
 
162
 
        source_filename = argv[1];
163
 
        dest_name = argv[2];
164
 
 
165
 
        /* Absolute source filenames only, please */
166
 
 
167
 
        if (!g_path_is_absolute (source_filename)) {
168
 
                g_print ("%s\n", _("The source filename must be absolute"));
169
 
                return EXIT_FAILURE;
170
 
        }
171
 
 
172
 
        /* We only copy regular files */
173
 
 
174
 
        source_fd = open (source_filename, O_RDONLY);
175
 
        if (source_fd == -1) {
176
 
                err = errno;
177
 
 
178
 
                /* Translators: first %s is a filename; second %s is an error message */
179
 
                g_print (_("Could not open %s: %s\n"),
180
 
                         source_filename,
181
 
                         g_strerror (err));
182
 
                return EXIT_FAILURE;
183
 
        }
184
 
 
185
 
        if (fstat (source_fd, &statbuf) != 0) {
186
 
                err = errno;
187
 
                /* Translators: first %s is a filename; second %s is an error message */
188
 
                g_print (_("Could not get information for %s: %s\n"),
189
 
                         source_filename,
190
 
                         g_strerror (err));
191
 
                return EXIT_FAILURE;
192
 
        }
193
 
 
194
 
        if (!S_ISREG (statbuf.st_mode)) {
195
 
                g_print (_("%s must be a regular file\n"),
196
 
                         source_filename);
197
 
                return EXIT_FAILURE;
198
 
        }
199
 
 
200
 
        /* We only copy files that are really owned by the calling user */
201
 
 
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)"));
205
 
                return EXIT_FAILURE;
206
 
        }
207
 
 
208
 
        if (sscanf (pkexec_uid_str, "%d", &pkexec_uid) != 1) {
209
 
                g_print ("%s\n", _("PKEXEC_UID must be set to an integer value"));
210
 
                return EXIT_FAILURE;
211
 
        }
212
 
 
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);
216
 
                return EXIT_FAILURE;
217
 
        }
218
 
 
219
 
        /* We only accept basenames for the destination */
220
 
 
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"),
224
 
                         dest_name);
225
 
                return EXIT_FAILURE;
226
 
        }
227
 
 
228
 
        /* Chdir to the destination directory to keep it open... */
229
 
 
230
 
        if (chdir (SYSTEM_RANDR_PATH) != 0) {
231
 
                g_print (_("%s must be a directory\n"), SYSTEM_RANDR_PATH);
232
 
                return EXIT_FAILURE;
233
 
        }
234
 
 
235
 
        /* ... and open our temporary destination file right there */
236
 
 
237
 
        strcpy (template, "gsd-XXXXXX");
238
 
        dest_fd = g_mkstemp_full (template, O_WRONLY, 0644);
239
 
        if (dest_fd == -1) {
240
 
                err = errno;
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"),
243
 
                         SYSTEM_RANDR_PATH,
244
 
                         template,
245
 
                         g_strerror (err));
246
 
                return EXIT_FAILURE;
247
 
        }
248
 
 
249
 
        /* Do the copy */
250
 
 
251
 
        if (!copy_file (source_fd, dest_fd)) {
252
 
                /* If something went wrong, remove the destination file to avoid leaving trash around */
253
 
                unlink (template);
254
 
                return EXIT_FAILURE;
255
 
        }
256
 
 
257
 
        /* Rename to the final filename */
258
 
 
259
 
        if (rename (template, dest_name) != 0) {
260
 
                err = errno;
261
 
                unlink (template);
262
 
                g_print (_("Could not rename %s to %s: %s\n"),
263
 
                         template,
264
 
                         dest_name,
265
 
                         g_strerror (err));
266
 
                return EXIT_FAILURE;
267
 
        }
268
 
 
269
 
        /* Whew!  We'll leave the final closing of the files to the almighty kernel. */
270
 
 
271
 
        return EXIT_SUCCESS;
272
 
}