chiark / gitweb /
mechanisms: add mechanisms to change system locale and clock
[elogind.git] / src / localed.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2011 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <dbus/dbus.h>
23
24 #include <errno.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "util.h"
29 #include "strv.h"
30 #include "dbus-common.h"
31 #include "polkit.h"
32
33 #define INTROSPECTION                                                   \
34         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
35         "<node>\n"                                                      \
36         " <interface name=\"org.freedesktop.locale1\">\n"               \
37         "  <property name=\"Locale\" type=\"as\" access=\"read\"/>\n"   \
38         "  <method name=\"SetLocale\">\n"                               \
39         "   <arg name=\"locale\" type=\"as\" direction=\"in\"/>\n"      \
40         "   <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
41         "  </method>\n"                                                 \
42         " </interface>\n"                                               \
43         BUS_PROPERTIES_INTERFACE                                        \
44         BUS_INTROSPECTABLE_INTERFACE                                    \
45         BUS_PEER_INTERFACE                                              \
46         "</node>\n"
47
48 #define INTERFACES_LIST                         \
49         BUS_GENERIC_INTERFACES_LIST             \
50         "org.freedesktop.locale1\0"
51
52 enum {
53         /* We don't list LC_ALL here on purpose. People should be
54          * using LANG instead. */
55
56         PROP_LANG,
57         PROP_LC_CTYPE,
58         PROP_LC_NUMERIC,
59         PROP_LC_TIME,
60         PROP_LC_COLLATE,
61         PROP_LC_MONETARY,
62         PROP_LC_MESSAGES,
63         PROP_LC_PAPER,
64         PROP_LC_NAME,
65         PROP_LC_ADDRESS,
66         PROP_LC_TELEPHONE,
67         PROP_LC_MEASUREMENT,
68         PROP_LC_IDENTIFICATION,
69         _PROP_MAX
70 };
71
72 static const char * const names[_PROP_MAX] = {
73         [PROP_LANG] = "LANG",
74         [PROP_LC_CTYPE] = "LC_CTYPE",
75         [PROP_LC_NUMERIC] = "LC_NUMERIC",
76         [PROP_LC_TIME] = "LC_TIME",
77         [PROP_LC_COLLATE] = "LC_COLLATE",
78         [PROP_LC_MONETARY] = "LC_MONETARY",
79         [PROP_LC_MESSAGES] = "LC_MESSAGES",
80         [PROP_LC_PAPER] = "LC_PAPER",
81         [PROP_LC_NAME] = "LC_NAME",
82         [PROP_LC_ADDRESS] = "LC_ADDRESS",
83         [PROP_LC_TELEPHONE] = "LC_TELEPHONE",
84         [PROP_LC_MEASUREMENT] = "LC_MEASUREMENT",
85         [PROP_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
86 };
87
88 static char *data[_PROP_MAX] = {
89         NULL,
90         NULL,
91         NULL,
92         NULL,
93         NULL,
94         NULL,
95         NULL,
96         NULL,
97         NULL,
98         NULL,
99         NULL,
100         NULL,
101         NULL
102 };
103
104 static void free_data(void) {
105         int p;
106
107         for (p = 0; p < _PROP_MAX; p++) {
108                 free(data[p]);
109                 data[p] = NULL;
110         }
111 }
112
113 static void simplify(void) {
114         int p;
115
116         for (p = 1; p < _PROP_MAX; p++)
117                 if (isempty(data[p]) || streq_ptr(data[PROP_LANG], data[p])) {
118                         free(data[p]);
119                         data[p] = NULL;
120                 }
121 }
122
123 static int read_data(void) {
124         int r;
125
126         free_data();
127
128         r = parse_env_file("/etc/locale.conf", NEWLINE,
129                            "LANG",              &data[PROP_LANG],
130                            "LC_CTYPE",          &data[PROP_LC_CTYPE],
131                            "LC_NUMERIC",        &data[PROP_LC_NUMERIC],
132                            "LC_TIME",           &data[PROP_LC_TIME],
133                            "LC_COLLATE",        &data[PROP_LC_COLLATE],
134                            "LC_MONETARY",       &data[PROP_LC_MONETARY],
135                            "LC_MESSAGES",       &data[PROP_LC_MESSAGES],
136                            "LC_PAPER",          &data[PROP_LC_PAPER],
137                            "LC_NAME",           &data[PROP_LC_NAME],
138                            "LC_ADDRESS",        &data[PROP_LC_ADDRESS],
139                            "LC_TELEPHONE",      &data[PROP_LC_TELEPHONE],
140                            "LC_MEASUREMENT",    &data[PROP_LC_MEASUREMENT],
141                            "LC_IDENTIFICATION", &data[PROP_LC_IDENTIFICATION],
142                            NULL);
143
144         if (r == -ENOENT) {
145                 int p;
146
147                 /* Fill in what we got passed from systemd. */
148
149                 for (p = 0; p < _PROP_MAX; p++) {
150                         char *e, *d;
151
152                         assert(names[p]);
153
154                         e = getenv(names[p]);
155                         if (e) {
156                                 d = strdup(e);
157                                 if (!d)
158                                         return -ENOMEM;
159                         } else
160                                 d = NULL;
161
162                         free(data[p]);
163                         data[p] = d;
164                 }
165
166                 r = 0;
167         }
168
169         simplify();
170         return r;
171 }
172
173 static int write_data(void) {
174         int r, p;
175         char **l = NULL;
176
177         r = load_env_file("/etc/locale.conf", &l);
178         if (r < 0 && r != -ENOENT)
179                 return r;
180
181         for (p = 0; p < _PROP_MAX; p++) {
182                 char *t, **u;
183
184                 assert(names[p]);
185
186                 if (isempty(data[p])) {
187                         l = strv_env_unset(l, names[p]);
188                         continue;
189                 }
190
191                 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
192                         strv_free(l);
193                         return -ENOMEM;
194                 }
195
196                 u = strv_env_set(l, t);
197                 free(t);
198                 strv_free(l);
199
200                 if (!u)
201                         return -ENOMEM;
202
203                 l = u;
204         }
205
206         if (strv_isempty(l)) {
207
208                 if (unlink("/etc/locale.conf") < 0)
209                         return errno == ENOENT ? 0 : -errno;
210
211                 return 0;
212         }
213
214         r = write_env_file("/etc/locale.conf", l);
215         strv_free(l);
216
217         return r;
218 }
219
220 static void push_data(DBusConnection *bus) {
221         char **l_set = NULL, **l_unset = NULL, **t;
222         int c_set = 0, c_unset = 0, p;
223         DBusError error;
224         DBusMessage *m = NULL, *reply = NULL;
225         DBusMessageIter iter, sub;
226
227         dbus_error_init(&error);
228
229         assert(bus);
230
231         l_set = new0(char*, _PROP_MAX);
232         l_unset = new0(char*, _PROP_MAX);
233         if (!l_set || !l_unset) {
234                 log_error("Out of memory");
235                 goto finish;
236         }
237
238         for (p = 0; p < _PROP_MAX; p++) {
239                 assert(names[p]);
240
241                 if (isempty(data[p]))
242                         l_unset[c_set++] = (char*) names[p];
243                 else {
244                         char *s;
245
246                         if (asprintf(&s, "%s=%s", names[p], data[p]) < 0) {
247                                 log_error("Out of memory");
248                                 goto finish;
249                         }
250
251                         l_set[c_unset++] = s;
252                 }
253         }
254
255         assert(c_set + c_unset == _PROP_MAX);
256         m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment");
257         if (!m) {
258                 log_error("Could not allocate message.");
259                 goto finish;
260         }
261
262         dbus_message_iter_init_append(m, &iter);
263
264         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
265                 log_error("Out of memory.");
266                 goto finish;
267         }
268
269         STRV_FOREACH(t, l_unset)
270                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
271                         log_error("Out of memory.");
272                         goto finish;
273                 }
274
275         if (!dbus_message_iter_close_container(&iter, &sub) ||
276             !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
277                 log_error("Out of memory.");
278                 goto finish;
279         }
280
281         STRV_FOREACH(t, l_set)
282                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
283                         log_error("Out of memory.");
284                         goto finish;
285                 }
286
287         if (!dbus_message_iter_close_container(&iter, &sub)) {
288                 log_error("Out of memory.");
289                 goto finish;
290         }
291
292         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
293         if (!reply) {
294                 log_error("Failed to set locale information: %s", bus_error_message(&error));
295                 goto finish;
296         }
297
298 finish:
299         if (m)
300                 dbus_message_unref(m);
301
302         if (reply)
303                 dbus_message_unref(reply);
304
305         dbus_error_free(&error);
306
307         strv_free(l_set);
308         free(l_unset);
309 }
310
311 static int append_locale(DBusMessageIter *i, const char *property, void *userdata) {
312         int r, c = 0, p;
313         char **l;
314
315         l = new0(char*, _PROP_MAX+1);
316         if (!l)
317                 return -ENOMEM;
318
319         for (p = 0; p < _PROP_MAX; p++) {
320                 char *t;
321
322                 if (isempty(data[p]))
323                         continue;
324
325                 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
326                         strv_free(l);
327                         return -ENOMEM;
328                 }
329
330                 l[c++] = t;
331         }
332
333         r = bus_property_append_strv(i, property, (void*) l);
334         strv_free(l);
335
336         return r;
337 }
338
339 static DBusHandlerResult locale_message_handler(
340                 DBusConnection *connection,
341                 DBusMessage *message,
342                 void *userdata) {
343
344         const BusProperty properties[] = {
345                 { "org.freedesktop.locale1", "Locale", append_locale, "as", NULL},
346                 { NULL, NULL, NULL, NULL, NULL }
347         };
348
349         DBusMessage *reply = NULL, *changed = NULL;
350         DBusError error;
351         int r;
352
353         assert(connection);
354         assert(message);
355
356         dbus_error_init(&error);
357
358         if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetLocale")) {
359                 char **l = NULL, **i;
360                 dbus_bool_t interactive;
361                 DBusMessageIter iter;
362                 bool modified = false;
363                 bool passed[_PROP_MAX];
364                 int p;
365
366                 if (!dbus_message_iter_init(message, &iter))
367                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
368
369                 r = bus_parse_strv_iter(&iter, &l);
370                 if (r < 0) {
371                         if (r == -ENOMEM)
372                                 goto oom;
373
374                         return bus_send_error_reply(connection, message, NULL, r);
375                 }
376
377                 if (!dbus_message_iter_next(&iter) ||
378                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)  {
379                         strv_free(l);
380                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
381                 }
382
383                 dbus_message_iter_get_basic(&iter, &interactive);
384
385                 zero(passed);
386
387                 /* Check whether a variable changed and if so valid */
388                 STRV_FOREACH(i, l) {
389                         bool valid = false;
390
391                         for (p = 0; p < _PROP_MAX; p++) {
392                                 size_t k;
393
394                                 k = strlen(names[p]);
395                                 if (startswith(*i, names[p]) && (*i)[k] == '=') {
396                                         valid = true;
397                                         passed[p] = true;
398
399                                         if (!streq_ptr(*i + k + 1, data[p]))
400                                                 modified = true;
401
402                                         break;
403                                 }
404                         }
405
406                         if (!valid) {
407                                 strv_free(l);
408                                 return bus_send_error_reply(connection, message, NULL, -EINVAL);
409                         }
410                 }
411
412                 /* Check whether a variable is unset */
413                 if (!modified)  {
414                         for (p = 0; p < _PROP_MAX; p++)
415                                 if (!isempty(data[p]) && !passed[p]) {
416                                         modified = true;
417                                         break;
418                                 }
419                 }
420
421                 if (modified) {
422
423                         r = verify_polkit(connection, message, "org.freedesktop.locale1.set-locale", interactive, &error);
424                         if (r < 0) {
425                                 strv_free(l);
426                                 return bus_send_error_reply(connection, message, &error, r);
427                         }
428
429                         STRV_FOREACH(i, l) {
430                                 for (p = 0; p < _PROP_MAX; p++) {
431                                         size_t k;
432
433                                         k = strlen(names[p]);
434                                         if (startswith(*i, names[p]) && (*i)[k] == '=') {
435                                                 char *t;
436
437                                                 t = strdup(*i + k + 1);
438                                                 if (!t) {
439                                                         strv_free(l);
440                                                         goto oom;
441                                                 }
442
443                                                 free(data[p]);
444                                                 data[p] = t;
445
446                                                 break;
447                                         }
448                                 }
449                         }
450
451                         for (p = 0; p < _PROP_MAX; p++) {
452                                 if (passed[p])
453                                         continue;
454
455                                 free(data[p]);
456                                 data[p] = NULL;
457                         }
458
459                         simplify();
460
461                         r = write_data();
462                         if (r < 0) {
463                                 log_error("Failed to set locale: %s", strerror(-r));
464                                 return bus_send_error_reply(connection, message, NULL, r);
465                         }
466
467                         push_data(connection);
468
469                         log_info("Changed locale information.");
470
471                         changed = bus_properties_changed_new(
472                                         "/org/freedesktop/locale1",
473                                         "org.freedesktop.locale1",
474                                         "Locale\0");
475                         if (!changed)
476                                 goto oom;
477                 }
478
479         } else
480                 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
481
482         if (!(reply = dbus_message_new_method_return(message)))
483                 goto oom;
484
485         if (!dbus_connection_send(connection, reply, NULL))
486                 goto oom;
487
488         dbus_message_unref(reply);
489         reply = NULL;
490
491         if (changed) {
492
493                 if (!dbus_connection_send(connection, changed, NULL))
494                         goto oom;
495
496                 dbus_message_unref(changed);
497         }
498
499         return DBUS_HANDLER_RESULT_HANDLED;
500
501 oom:
502         if (reply)
503                 dbus_message_unref(reply);
504
505         if (changed)
506                 dbus_message_unref(changed);
507
508         dbus_error_free(&error);
509
510         return DBUS_HANDLER_RESULT_NEED_MEMORY;
511 }
512
513 static int connect_bus(DBusConnection **_bus) {
514         static const DBusObjectPathVTable locale_vtable = {
515                 .message_function = locale_message_handler
516         };
517         DBusError error;
518         DBusConnection *bus = NULL;
519         int r;
520
521         assert(_bus);
522
523         dbus_error_init(&error);
524
525         bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
526         if (!bus) {
527                 log_error("Failed to get system D-Bus connection: %s", error.message);
528                 r = -ECONNREFUSED;
529                 goto fail;
530         }
531
532         if (!dbus_connection_register_object_path(bus, "/org/freedesktop/locale1", &locale_vtable, NULL)) {
533                 log_error("Not enough memory");
534                 r = -ENOMEM;
535                 goto fail;
536         }
537
538         if (dbus_bus_request_name(bus, "org.freedesktop.locale1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) < 0) {
539                 log_error("Failed to register name on bus: %s", error.message);
540                 r = -EEXIST;
541                 goto fail;
542         }
543
544         if (_bus)
545                 *_bus = bus;
546
547         return 0;
548
549 fail:
550         dbus_connection_close(bus);
551         dbus_connection_unref(bus);
552
553         dbus_error_free(&error);
554
555         return r;
556 }
557
558 int main(int argc, char *argv[]) {
559         int r;
560         DBusConnection *bus = NULL;
561
562         log_set_target(LOG_TARGET_AUTO);
563         log_parse_environment();
564         log_open();
565
566         if (argc != 1) {
567                 log_error("This program takes no arguments.");
568                 r = -EINVAL;
569                 goto finish;
570         }
571
572         umask(0022);
573
574         r = read_data();
575         if (r < 0) {
576                 log_error("Failed to read locale data: %s", strerror(-r));
577                 goto finish;
578         }
579
580         r = connect_bus(&bus);
581         if (r < 0)
582                 goto finish;
583
584         while (dbus_connection_read_write_dispatch(bus, -1))
585                 ;
586
587         r = 0;
588
589 finish:
590         free_data();
591
592         if (bus) {
593                 dbus_connection_flush(bus);
594                 dbus_connection_close(bus);
595                 dbus_connection_unref(bus);
596         }
597
598         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
599 }