chiark / gitweb /
audit: smaller fixes to audit hookup
[elogind.git] / src / update-utmp.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 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 <assert.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <libaudit.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <dbus/dbus.h>
30
31 #ifdef HAVE_AUDIT
32 #include <libaudit.h>
33 #endif
34
35 #include "log.h"
36 #include "macro.h"
37 #include "util.h"
38 #include "special.h"
39 #include "utmp-wtmp.h"
40 #include "dbus-common.h"
41
42 typedef struct Context {
43         DBusConnection *bus;
44 #ifdef HAVE_AUDIT
45         int audit_fd;
46 #endif
47 } Context;
48
49 static usec_t get_startup_time(Context *c) {
50         const char
51                 *interface = "org.freedesktop.systemd1.Manager",
52                 *property = "StartupTimestamp";
53
54         DBusError error;
55         usec_t t = 0;
56         DBusMessage *m = NULL, *reply = NULL;
57         DBusMessageIter iter, sub;
58
59         dbus_error_init(&error);
60
61         assert(c);
62
63         if (!(m = dbus_message_new_method_call(
64                               "org.freedesktop.systemd1",
65                               "/org/freedesktop/systemd1",
66                               "org.freedesktop.DBus.Properties",
67                               "Get"))) {
68                 log_error("Could not allocate message.");
69                 goto finish;
70         }
71
72         if (!dbus_message_append_args(m,
73                                       DBUS_TYPE_STRING, &interface,
74                                       DBUS_TYPE_STRING, &property,
75                                       DBUS_TYPE_INVALID)) {
76                 log_error("Could not append arguments to message.");
77                 goto finish;
78         }
79
80         if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
81                 log_error("Failed to send command: %s", error.message);
82                 goto finish;
83         }
84
85         if (!dbus_message_iter_init(reply, &iter) ||
86             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)  {
87                 log_error("Failed to parse reply.");
88                 goto finish;
89         }
90
91         dbus_message_iter_recurse(&iter, &sub);
92
93         if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT64)  {
94                 log_error("Failed to parse reply.");
95                 goto finish;
96         }
97
98         dbus_message_iter_get_basic(&sub, &t);
99
100 finish:
101         if (m)
102                 dbus_message_unref(m);
103
104         if (reply)
105                 dbus_message_unref(reply);
106
107         dbus_error_free(&error);
108
109         return t;
110 }
111
112 static int get_current_runlevel(Context *c) {
113         static const struct {
114                 const int runlevel;
115                 const char *special;
116         } table[] = {
117                 /* The first target of this list that is active or has
118                  * a job scheduled wins */
119                 { '5', SPECIAL_RUNLEVEL5_TARGET },
120                 { '4', SPECIAL_RUNLEVEL4_TARGET },
121                 { '3', SPECIAL_RUNLEVEL3_TARGET },
122                 { '2', SPECIAL_RUNLEVEL2_TARGET },
123                 { '1', SPECIAL_RESCUE_TARGET },
124         };
125         const char
126                 *interface = "org.freedesktop.systemd1.Unit",
127                 *property = "ActiveState";
128
129         DBusMessage *m = NULL, *reply = NULL;
130         int r = 0;
131         unsigned i;
132         DBusError error;
133
134         assert(c);
135
136         dbus_error_init(&error);
137
138         for (i = 0; i < ELEMENTSOF(table); i++) {
139                 const char *path = NULL, *state;
140                 DBusMessageIter iter, sub;
141
142                 if (!(m = dbus_message_new_method_call(
143                                       "org.freedesktop.systemd1",
144                                       "/org/freedesktop/systemd1",
145                                       "org.freedesktop.systemd1.Manager",
146                                       "GetUnit"))) {
147                         log_error("Could not allocate message.");
148                         r = -ENOMEM;
149                         goto finish;
150                 }
151
152                 if (!dbus_message_append_args(m,
153                                               DBUS_TYPE_STRING, &table[i].special,
154                                               DBUS_TYPE_INVALID)) {
155                         log_error("Could not append arguments to message.");
156                         r = -ENOMEM;
157                         goto finish;
158                 }
159
160                 if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
161                         dbus_error_free(&error);
162                         continue;
163                 }
164
165                 if (!dbus_message_get_args(reply, &error,
166                                            DBUS_TYPE_OBJECT_PATH, &path,
167                                            DBUS_TYPE_INVALID)) {
168                         log_error("Failed to parse reply: %s", error.message);
169                         r = -EIO;
170                         goto finish;
171                 }
172
173                 dbus_message_unref(m);
174                 if (!(m = dbus_message_new_method_call(
175                                       "org.freedesktop.systemd1",
176                                       path,
177                                       "org.freedesktop.DBus.Properties",
178                                       "Get"))) {
179                         log_error("Could not allocate message.");
180                         r = -ENOMEM;
181                         goto finish;
182                 }
183
184                 if (!dbus_message_append_args(m,
185                                               DBUS_TYPE_STRING, &interface,
186                                               DBUS_TYPE_STRING, &property,
187                                               DBUS_TYPE_INVALID)) {
188                         log_error("Could not append arguments to message.");
189                         r = -ENOMEM;
190                         goto finish;
191                 }
192
193                 dbus_message_unref(reply);
194                 if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
195                         log_error("Failed to send command: %s", error.message);
196                         r = -EIO;
197                         goto finish;
198                 }
199
200                 if (!dbus_message_iter_init(reply, &iter) ||
201                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)  {
202                         log_error("Failed to parse reply.");
203                         r = -EIO;
204                         goto finish;
205                 }
206
207                 dbus_message_iter_recurse(&iter, &sub);
208
209                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)  {
210                         log_error("Failed to parse reply.");
211                         r = -EIO;
212                         goto finish;
213                 }
214
215                 dbus_message_iter_get_basic(&sub, &state);
216
217                 if (streq(state, "active") || streq(state, "reloading"))
218                         r = table[i].runlevel;
219
220                 dbus_message_unref(m);
221                 dbus_message_unref(reply);
222                 m = reply = NULL;
223
224                 if (r)
225                         break;
226         }
227
228 finish:
229         if (m)
230                 dbus_message_unref(m);
231
232         if (reply)
233                 dbus_message_unref(reply);
234
235         dbus_error_free(&error);
236
237         return r;
238 }
239
240 static int on_reboot(Context *c) {
241         int r = 0, q;
242         usec_t t;
243
244         assert(c);
245
246         /* We finished start-up, so let's write the utmp
247          * record and send the audit msg */
248
249 #ifdef HAVE_AUDIT
250         if (c->audit_fd >= 0)
251                 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "init", NULL, NULL, NULL, 1) < 0) {
252                         log_error("Failed to send audit message: %m");
253                         r = -errno;
254                 }
255 #endif
256
257         /* If this call fails it will return 0, which
258          * utmp_put_reboot() will then fix to the current time */
259         t = get_startup_time(c);
260
261         if ((q = utmp_put_reboot(t)) < 0) {
262                 log_error("Failed to write utmp record: %s", strerror(-q));
263                 r = q;
264         }
265
266         return r;
267 }
268
269 static int on_shutdown(Context *c) {
270         int r = 0, q;
271
272         assert(c);
273
274         /* We started shut-down, so let's write the utmp
275          * record and send the audit msg */
276
277 #ifdef HAVE_AUDIT
278         if (c->audit_fd >= 0)
279                 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "init", NULL, NULL, NULL, 1) < 0) {
280                         log_error("Failed to send audit message: %m");
281                         r = -errno;
282                 }
283 #endif
284
285         if ((q = utmp_put_shutdown(0)) < 0) {
286                 log_error("Failed to write utmp record: %s", strerror(-q));
287                 r = q;
288         }
289
290         return r;
291 }
292
293 static int on_runlevel(Context *c) {
294         int r = 0, q, previous, runlevel;
295
296         assert(c);
297
298         /* We finished changing runlevel, so let's write the
299          * utmp record and send the audit msg */
300
301         /* First, get last runlevel */
302         if ((q = utmp_get_runlevel(&previous, NULL)) < 0) {
303
304                 if (q != -ESRCH && q != -ENOENT) {
305                         log_error("Failed to get current runlevel: %s", strerror(-q));
306                         return q;
307                 }
308
309                 /* Hmm, we didn't find any runlevel, that means we
310                  * have been rebooted */
311                 r = on_reboot(c);
312                 previous = 0;
313         }
314
315         /* Secondly, get new runlevel */
316         if ((runlevel = get_current_runlevel(c)) < 0)
317                 return runlevel;
318
319         if (previous == runlevel)
320                 return 0;
321
322 #ifdef HAVE_AUDIT
323         if (c->audit_fd >= 0) {
324                 char *s = NULL;
325
326                 if (asprintf(&s, "old-level=%c new-level=%c",
327                              previous > 0 ? previous : 'N',
328                              runlevel > 0 ? runlevel : 'N') < 0)
329                         return -ENOMEM;
330
331                 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, NULL, NULL, NULL, 1) < 0) {
332                         log_error("Failed to send audit message: %m");
333                         r = -errno;
334                 }
335
336                 free(s);
337         }
338 #endif
339
340         if ((q = utmp_put_runlevel(0, runlevel, previous)) < 0) {
341                 log_error("Failed to write utmp record: %s", strerror(-q));
342                 r = q;
343         }
344
345         return r;
346 }
347
348 int main(int argc, char *argv[]) {
349         int r;
350         DBusError error;
351         Context c;
352
353         dbus_error_init(&error);
354
355         zero(c);
356 #ifdef HAVE_AUDIT
357         c.audit_fd = -1;
358 #endif
359
360         if (getppid() != 1) {
361                 log_error("This program should be invoked by init only.");
362                 return 1;
363         }
364
365         if (argc != 2) {
366                 log_error("This program requires one argument.");
367                 return 1;
368         }
369
370         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
371         log_parse_environment();
372
373 #ifdef HAVE_AUDIT
374         if ((c.audit_fd = audit_open()) < 0)
375                 log_error("Failed to connect to audit log: %m");
376 #endif
377
378         if (bus_connect(DBUS_BUS_SYSTEM, &c.bus, NULL, &error) < 0) {
379                 log_error("Failed to get D-Bus connection: %s", error.message);
380                 r = -EIO;
381                 goto finish;
382         }
383
384         log_debug("systemd-update-utmp running as pid %lu", (unsigned long) getpid());
385
386         if (streq(argv[1], "reboot"))
387                 r = on_reboot(&c);
388         else if (streq(argv[1], "shutdown"))
389                 r = on_shutdown(&c);
390         else if (streq(argv[1], "runlevel"))
391                 r = on_runlevel(&c);
392         else {
393                 log_error("Unknown command %s", argv[1]);
394                 r = -EINVAL;
395         }
396
397         log_debug("systemd-update-utmp stopped as pid %lu", (unsigned long) getpid());
398
399 finish:
400 #ifdef HAVE_AUDIT
401         if (c.audit_fd >= 0)
402                 audit_close(c.audit_fd);
403 #endif
404
405         if (c.bus) {
406                dbus_connection_close(c.bus);
407                dbus_connection_unref(c.bus);
408         }
409
410         dbus_error_free(&error);
411         dbus_shutdown();
412
413         return r < 0 ? 1 : 0;
414 }