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