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