4 * Simple program to dump UTMP and WTMP files in raw format, so they can be
7 * Based on utmpdump dump from sysvinit suite.
9 * Copyright (C) 1991-2000 Miquel van Smoorenburg <miquels@cistron.nl>
11 * Copyright (C) 1998 Danek Duvall <duvall@alumni.princeton.edu>
12 * Copyright (C) 2012 Karel Zak <kzak@redhat.com>
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.
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.
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
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
42 #include "closestream.h"
44 static char *timetostr(const time_t time
)
46 static char s
[29]; /* [Sun Sep 01 00:00:00 1998 PST] */
49 strftime(s
, 29, "%a %b %d %T %Y %Z", localtime(&time
));
56 static time_t strtotime(const char *s_time
)
60 memset(&tm
, '\0', sizeof(struct tm
));
62 if (s_time
[0] == ' ' || s_time
[0] == '\0')
65 strptime(s_time
, "%a %b %d %T %Y", &tm
);
67 /* Cheesy way of checking for DST */
68 if (s_time
[26] == 'D')
74 #define cleanse(x) xcleanse(x, sizeof(x))
75 static void xcleanse(char *s
, int len
)
77 for ( ; *s
&& len
-- > 0; s
++)
78 if (!isprint(*s
) || *s
== '[' || *s
== ']')
82 static void unspace(char *s
, int len
)
84 while (*s
&& *s
!= ' ' && len
--)
91 static void print_utline(struct utmp ut
)
93 char *addr_string
, *time_string
;
96 in
.s_addr
= ut
.ut_addr
;
97 addr_string
= inet_ntoa(in
);
98 time_string
= timetostr(ut
.ut_time
);
104 /* pid id user line host addr time */
105 printf("[%d] [%05d] [%-4.4s] [%-*.*s] [%-*.*s] [%-*.*s] [%-15.15s] [%-28.28s]\n",
106 ut
.ut_type
, ut
.ut_pid
, ut
.ut_id
, 8, UT_NAMESIZE
, ut
.ut_user
,
107 12, UT_LINESIZE
, ut
.ut_line
, 20, UT_HOSTSIZE
, ut
.ut_host
,
108 addr_string
, time_string
);
111 static void dump(FILE *fp
, int forever
)
116 fseek(fp
, -10 * sizeof(ut
), SEEK_END
);
119 while (fread(&ut
, sizeof(ut
), 1, fp
) == 1)
126 /* This function won't work properly if there's a ']' or a ' ' in the real
127 * token. Thankfully, this should never happen. */
128 static int gettok(char *line
, char *dest
, int size
, int eatspace
)
130 int bpos
, epos
, eaten
;
133 bpos
= strchr(line
, '[') - line
;
135 errx(EXIT_FAILURE
, _("Extraneous newline in file. Exiting."));
138 epos
= strchr(line
, ']') - line
;
140 errx(EXIT_FAILURE
,_("Extraneous newline in file. Exiting."));
143 eaten
= bpos
+ epos
+ 1;
146 if ((t
= strchr(line
, ' ')))
149 strncpy(dest
, line
, size
);
154 static void undump(FILE *fp
)
157 char s_addr
[16], s_time
[29], *linestart
, *line
;
160 line
= linestart
= xmalloc(1024 * sizeof(*linestart
));
164 while (fgets(linestart
, 1023, fp
))
167 memset(&ut
, '\0', sizeof(ut
));
168 sscanf(line
, "[%hd] [%d] [%4c] ", &ut
.ut_type
, &ut
.ut_pid
, ut
.ut_id
);
171 line
+= gettok(line
, ut
.ut_user
, sizeof(ut
.ut_user
), 1);
172 line
+= gettok(line
, ut
.ut_line
, sizeof(ut
.ut_line
), 1);
173 line
+= gettok(line
, ut
.ut_host
, sizeof(ut
.ut_host
), 1);
174 line
+= gettok(line
, s_addr
, sizeof(s_addr
)-1, 1);
175 line
+= gettok(line
, s_time
, sizeof(s_time
)-1, 0);
177 ut
.ut_addr
= inet_addr(s_addr
);
178 ut
.ut_time
= strtotime(s_time
);
180 fwrite(&ut
, sizeof(ut
), 1, stdout
);
188 static void __attribute__((__noreturn__
)) usage(FILE *out
)
190 fputs(USAGE_HEADER
, out
);
193 _(" %s [options] [filename]\n"), program_invocation_short_name
);
195 fputs(USAGE_OPTIONS
, out
);
196 fputs(_(" -f, --follow output appended data as the file grows\n"
197 " -r, --reverse write back dumped data into utmp file\n"
198 " -h, --help display this help and exit\n"
199 " -V, --version output version information and exit\n"), out
);
201 fprintf(out
, USAGE_MAN_TAIL("utmpdump(1)"));
202 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
205 int main(int argc
, char **argv
)
209 int reverse
= 0, forever
= 0;
210 const char *filename
= NULL
;
212 static const struct option longopts
[] = {
213 { "follow", 0, 0, 'f' },
214 { "reverse", 0, 0, 'r' },
215 { "help", 0, 0, 'h' },
216 { "version", 0, 0, 'V' },
220 setlocale(LC_ALL
, "");
221 bindtextdomain(PACKAGE
, LOCALEDIR
);
223 atexit(close_stdout
);
225 while ((c
= getopt_long(argc
, argv
, "frhV", longopts
, NULL
)) != -1) {
239 printf(UTIL_LINUX_VERSION
);
247 filename
= argv
[optind
];
248 fp
= fopen(filename
, "r");
250 err(EXIT_FAILURE
, _("%s: open failed"), filename
);
257 fprintf(stderr
, _("Utmp undump of %s\n"), filename
);
260 fprintf(stderr
, _("Utmp dump of %s\n"), filename
);