]> git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/utmpdump.c
Merge branch 'getrandom' of git://github.com/kerolasa/lelux-utiliteetit
[thirdparty/util-linux.git] / login-utils / utmpdump.c
1 /*
2 * utmpdump
3 *
4 * Simple program to dump UTMP and WTMP files in raw format, so they can be
5 * examined.
6 *
7 * Based on utmpdump dump from sysvinit suite.
8 *
9 * Copyright (C) 1991-2000 Miquel van Smoorenburg <miquels@cistron.nl>
10 *
11 * Copyright (C) 1998 Danek Duvall <duvall@alumni.princeton.edu>
12 * Copyright (C) 2012 Karel Zak <kzak@redhat.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <utmpx.h>
32 #include <time.h>
33 #include <ctype.h>
34 #include <getopt.h>
35 #include <unistd.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <sys/stat.h>
39 #ifdef HAVE_INOTIFY_INIT
40 #include <sys/inotify.h>
41 #endif
42
43 #include "c.h"
44 #include "nls.h"
45 #include "timeutils.h"
46 #include "xalloc.h"
47 #include "closestream.h"
48
49 static time_t strtotime(const char *s_time)
50 {
51 struct tm tm;
52
53 memset(&tm, '\0', sizeof(struct tm));
54
55 if (s_time[0] == ' ' || s_time[0] == '\0')
56 return (time_t)0;
57
58 if (isdigit(s_time[0])) {
59 /* [1998-09-01T01:00:00,000000+00:00]
60 * Subseconds are parsed with strtousec(). Timezone is
61 * always UTC-0 */
62 strptime(s_time, "%Y-%m-%dT%H:%M:%S", &tm);
63 } else {
64 /* [Tue Sep 01 00:00:00 1998 GMT] */
65 strptime(s_time, "%a %b %d %T %Y", &tm);
66 /* Cheesy way of checking for DST. This could be needed
67 * with legacy dumps that used localtime(3). */
68 if (s_time[26] == 'D')
69 tm.tm_isdst = 1;
70 }
71 return timegm(&tm);
72 }
73
74 static suseconds_t strtousec(const char *s_time)
75 {
76 const char *s = strchr(s_time, ',');
77 if (s)
78 return (suseconds_t) atoi(s + 1);
79 return 0;
80 }
81
82 #define cleanse(x) xcleanse(x, sizeof(x))
83 static void xcleanse(char *s, int len)
84 {
85 for ( ; *s && len-- > 0; s++)
86 if (!isprint(*s) || *s == '[' || *s == ']')
87 *s = '?';
88 }
89
90 static void print_utline(struct utmpx *ut, FILE *out)
91 {
92 const char *addr_string;
93 char buffer[INET6_ADDRSTRLEN];
94 char time_string[40];
95 struct timeval tv;
96
97 if (ut->ut_addr_v6[1] || ut->ut_addr_v6[2] || ut->ut_addr_v6[3])
98 addr_string = inet_ntop(AF_INET6, &(ut->ut_addr_v6), buffer, sizeof(buffer));
99 else
100 addr_string = inet_ntop(AF_INET, &(ut->ut_addr_v6), buffer, sizeof(buffer));
101
102 tv.tv_sec = ut->ut_tv.tv_sec;
103 tv.tv_usec = ut->ut_tv.tv_usec;
104
105 if (strtimeval_iso(&tv,
106 ISO_8601_DATE | ISO_8601_TIME | ISO_8601_COMMAUSEC |
107 ISO_8601_TIMEZONE | ISO_8601_GMTIME, time_string,
108 sizeof(time_string)) != 0)
109 return;
110 cleanse(ut->ut_id);
111 cleanse(ut->ut_user);
112 cleanse(ut->ut_line);
113 cleanse(ut->ut_host);
114
115 /* type pid id user line host addr time */
116 fprintf(out, "[%d] [%05d] [%-4.4s] [%-*.*s] [%-*.*s] [%-*.*s] [%-15s] [%s]\n",
117 ut->ut_type, ut->ut_pid, ut->ut_id,
118 8, (int)sizeof(ut->ut_user), ut->ut_user,
119 12, (int)sizeof(ut->ut_line), ut->ut_line,
120 20, (int)sizeof(ut->ut_host), ut->ut_host,
121 addr_string, time_string);
122 }
123
124 #ifdef HAVE_INOTIFY_INIT
125 #define EVENTS (IN_MODIFY|IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT)
126 #define NEVENTS 4
127
128 static void roll_file(const char *filename, off_t *size, FILE *out)
129 {
130 FILE *in;
131 struct stat st;
132 struct utmpx ut;
133 off_t pos;
134
135 if (!(in = fopen(filename, "r")))
136 err(EXIT_FAILURE, _("cannot open %s"), filename);
137
138 if (fstat(fileno(in), &st) == -1)
139 err(EXIT_FAILURE, _("stat of %s failed"), filename);
140
141 if (st.st_size == *size)
142 goto done;
143
144 if (fseek(in, *size, SEEK_SET) != (off_t) -1) {
145 while (fread(&ut, sizeof(ut), 1, in) == 1)
146 print_utline(&ut, out);
147 }
148
149 pos = ftello(in);
150 /* If we've successfully read something, use the file position, this
151 * avoids data duplication. If we read nothing or hit an error,
152 * reset to the reported size, this handles truncated files.
153 */
154 *size = (pos != -1 && pos != *size) ? pos : st.st_size;
155
156 done:
157 fclose(in);
158 }
159
160 static int follow_by_inotify(FILE *in, const char *filename, FILE *out)
161 {
162 char buf[NEVENTS * sizeof(struct inotify_event)];
163 int fd, wd, event;
164 ssize_t length;
165 off_t size;
166
167 fd = inotify_init();
168 if (fd == -1)
169 return -1; /* probably reached any limit ... */
170
171 size = ftello(in);
172 fclose(in);
173
174 wd = inotify_add_watch(fd, filename, EVENTS);
175 if (wd == -1)
176 err(EXIT_FAILURE, _("%s: cannot add inotify watch."), filename);
177
178 while (wd >= 0) {
179 errno = 0;
180 length = read(fd, buf, sizeof(buf));
181
182 if (length < 0 && (errno == EINTR || errno == EAGAIN))
183 continue;
184 if (length < 0)
185 err(EXIT_FAILURE, _("%s: cannot read inotify events"),
186 filename);
187
188 for (event = 0; event < length;) {
189 struct inotify_event *ev =
190 (struct inotify_event *) &buf[event];
191
192 if (ev->mask & IN_MODIFY)
193 roll_file(filename, &size, out);
194 else {
195 close(wd);
196 wd = -1;
197 break;
198 }
199 event += sizeof(struct inotify_event) + ev->len;
200 }
201 }
202
203 close(fd);
204 return 0;
205 }
206 #endif /* HAVE_INOTIFY_INIT */
207
208 static FILE *dump(FILE *in, const char *filename, int follow, FILE *out)
209 {
210 struct utmpx ut;
211
212 if (follow)
213 ignore_result( fseek(in, -10 * sizeof(ut), SEEK_END) );
214
215 while (fread(&ut, sizeof(ut), 1, in) == 1)
216 print_utline(&ut, out);
217
218 if (!follow)
219 return in;
220
221 #ifdef HAVE_INOTIFY_INIT
222 if (follow_by_inotify(in, filename, out) == 0)
223 return NULL; /* file already closed */
224 else
225 #endif
226 /* fallback for systems without inotify or with non-free
227 * inotify instances */
228 for (;;) {
229 while (fread(&ut, sizeof(ut), 1, in) == 1)
230 print_utline(&ut, out);
231 sleep(1);
232 }
233
234 return in;
235 }
236
237
238 /* This function won't work properly if there's a ']' or a ' ' in the real
239 * token. Thankfully, this should never happen. */
240 static int gettok(char *line, char *dest, int size, int eatspace)
241 {
242 int bpos, epos, eaten;
243
244 bpos = strchr(line, '[') - line;
245 if (bpos < 0)
246 errx(EXIT_FAILURE, _("Extraneous newline in file. Exiting."));
247
248 line += 1 + bpos;
249 epos = strchr(line, ']') - line;
250 if (epos < 0)
251 errx(EXIT_FAILURE, _("Extraneous newline in file. Exiting."));
252
253 line[epos] = '\0';
254 eaten = bpos + epos + 1;
255
256 if (eatspace) {
257 char *t;
258 if ((t = strchr(line, ' ')))
259 *t = 0;
260 }
261 strncpy(dest, line, size);
262
263 return eaten + 1;
264 }
265
266 static void undump(FILE *in, FILE *out)
267 {
268 struct utmpx ut;
269 char s_addr[INET6_ADDRSTRLEN + 1], s_time[29], *linestart, *line;
270 int count = 0;
271
272 linestart = xmalloc(1024 * sizeof(*linestart));
273 s_time[28] = 0;
274
275 while (fgets(linestart, 1023, in)) {
276 line = linestart;
277 memset(&ut, '\0', sizeof(ut));
278 sscanf(line, "[%hd] [%d] [%4c] ", &ut.ut_type, &ut.ut_pid, ut.ut_id);
279
280 line += 19;
281 line += gettok(line, ut.ut_user, sizeof(ut.ut_user), 1);
282 line += gettok(line, ut.ut_line, sizeof(ut.ut_line), 1);
283 line += gettok(line, ut.ut_host, sizeof(ut.ut_host), 1);
284 line += gettok(line, s_addr, sizeof(s_addr) - 1, 1);
285 gettok(line, s_time, sizeof(s_time) - 1, 0);
286 if (strchr(s_addr, '.'))
287 inet_pton(AF_INET, s_addr, &(ut.ut_addr_v6));
288 else
289 inet_pton(AF_INET6, s_addr, &(ut.ut_addr_v6));
290
291 ut.ut_tv.tv_sec = strtotime(s_time);
292 ut.ut_tv.tv_usec = strtousec(s_time);
293
294 ignore_result( fwrite(&ut, sizeof(ut), 1, out) );
295
296 ++count;
297 }
298
299 free(linestart);
300 }
301
302 static void __attribute__((__noreturn__)) usage(FILE *out)
303 {
304 fputs(USAGE_HEADER, out);
305
306 fprintf(out,
307 _(" %s [options] [filename]\n"), program_invocation_short_name);
308
309 fputs(USAGE_SEPARATOR, out);
310 fputs(_("Dump UTMP and WTMP files in raw format.\n"), out);
311
312 fputs(USAGE_OPTIONS, out);
313 fputs(_(" -f, --follow output appended data as the file grows\n"), out);
314 fputs(_(" -r, --reverse write back dumped data into utmp file\n"), out);
315 fputs(_(" -o, --output <file> write to file instead of standard output\n"), out);
316 fputs(USAGE_HELP, out);
317 fputs(USAGE_VERSION, out);
318
319 fprintf(out, USAGE_MAN_TAIL("utmpdump(1)"));
320 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
321 }
322
323 int main(int argc, char **argv)
324 {
325 int c;
326 FILE *in = NULL, *out = NULL;
327 int reverse = 0, follow = 0;
328 const char *filename = NULL;
329
330 static const struct option longopts[] = {
331 { "follow", 0, 0, 'f' },
332 { "reverse", 0, 0, 'r' },
333 { "output", required_argument, 0, 'o' },
334 { "help", 0, 0, 'h' },
335 { "version", 0, 0, 'V' },
336 { NULL, 0, 0, 0 }
337 };
338
339 setlocale(LC_ALL, "");
340 bindtextdomain(PACKAGE, LOCALEDIR);
341 textdomain(PACKAGE);
342 atexit(close_stdout);
343
344 while ((c = getopt_long(argc, argv, "fro:hV", longopts, NULL)) != -1) {
345 switch (c) {
346 case 'r':
347 reverse = 1;
348 break;
349
350 case 'f':
351 follow = 1;
352 break;
353
354 case 'o':
355 out = fopen(optarg, "w");
356 if (!out)
357 err(EXIT_FAILURE, _("cannot open %s"),
358 optarg);
359 break;
360
361 case 'h':
362 usage(stdout);
363 break;
364 case 'V':
365 printf(UTIL_LINUX_VERSION);
366 return EXIT_SUCCESS;
367 default:
368 errtryhelp(EXIT_FAILURE);
369 }
370 }
371
372 if (!out)
373 out = stdout;
374
375 if (optind < argc) {
376 filename = argv[optind];
377 in = fopen(filename, "r");
378 if (!in)
379 err(EXIT_FAILURE, _("cannot open %s"), filename);
380 } else {
381 if (follow)
382 errx(EXIT_FAILURE, _("following standard input is unsupported"));
383 filename = "/dev/stdin";
384 in = stdin;
385 }
386
387 if (reverse) {
388 fprintf(stderr, _("Utmp undump of %s\n"), filename);
389 undump(in, out);
390 } else {
391 fprintf(stderr, _("Utmp dump of %s\n"), filename);
392 in = dump(in, filename, follow, out);
393 }
394
395 if (out != stdout && close_stream(out))
396 err(EXIT_FAILURE, _("write failed"));
397
398 if (in && in != stdin)
399 fclose(in);
400
401 return EXIT_SUCCESS;
402 }