]>
git.wh0rd.org - sysvinit.git/blob - src/utmpdump.c
2 * utmpdump Simple program to dump UTMP and WTMP files in
3 * raw format, so they can be examined.
5 * Author: Miquel van Smoorenburg, <miquels@cistron.nl>
6 * Danek Duvall <duvall@alumni.princeton.edu>
8 * Version: @(#)utmpdump 2.79 12-Sep-2000
10 * This file is part of the sysvinit suite,
11 * Copyright (C) 1991-2000 Miquel van Smoorenburg.
13 * Additional Copyright on this file 1998 Danek Duvall.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
42 oldtonew(struct oldutmp src
)
46 memset(&dest
, 0, sizeof dest
);
47 dest
.ut_type
= src
.ut_type
;
48 dest
.ut_pid
= src
.ut_pid
;
49 dest
.ut_time
= src
.ut_oldtime
;
50 dest
.ut_addr
= src
.ut_oldaddr
;
51 strncpy(dest
.ut_id
, src
.ut_id
, 4);
52 strncpy(dest
.ut_line
, src
.ut_line
, OLD_LINESIZE
);
53 strncpy(dest
.ut_user
, src
.ut_user
, OLD_NAMESIZE
);
54 strncpy(dest
.ut_host
, src
.ut_host
, OLD_HOSTSIZE
);
60 newtoold(struct utmp src
)
64 memset(&dest
, 0, sizeof dest
);
65 dest
.ut_type
= src
.ut_type
;
66 dest
.ut_pid
= src
.ut_pid
;
67 dest
.ut_oldtime
= src
.ut_time
;
68 dest
.ut_oldaddr
= src
.ut_addr
;
69 strncpy(dest
.ut_id
, src
.ut_id
, 4);
70 strncpy(dest
.ut_line
, src
.ut_line
, OLD_LINESIZE
);
71 strncpy(dest
.ut_user
, src
.ut_user
, OLD_NAMESIZE
);
72 strncpy(dest
.ut_host
, src
.ut_host
, OLD_HOSTSIZE
);
78 timetostr(const time_t time
)
80 static char s
[29]; /* [Sun Sep 01 00:00:00 1998 PST] */
83 strftime(s
, 29, "%a %b %d %T %Y %Z", localtime(&time
));
91 strtotime(const char *s_time
)
95 memset(&tm
, '\0', sizeof(struct tm
));
97 if (s_time
[0] == ' ' || s_time
[0] == '\0')
100 strptime(s_time
, "%a %b %d %T %Y", &tm
);
102 /* Cheesy way of checking for DST */
103 if (s_time
[26] == 'D')
109 #define cleanse(x) xcleanse(x, sizeof(x))
111 xcleanse(char *s
, int len
)
113 for ( ; *s
&& len
-- > 0; s
++)
114 if (!isprint(*s
) || *s
== '[' || *s
== ']')
119 unspace(char *s
, int len
)
121 while (*s
&& *s
!= ' ' && len
--)
129 print_utline(struct utmp ut
)
131 char *addr_string
, *time_string
;
134 in
.s_addr
= ut
.ut_addr
;
135 addr_string
= inet_ntoa(in
);
136 time_string
= timetostr(ut
.ut_time
);
142 /* pid id user line host addr time */
143 printf("[%d] [%05d] [%-4.4s] [%-*.*s] [%-*.*s] [%-*.*s] [%-15.15s] [%-28.28s]\n",
144 ut
.ut_type
, ut
.ut_pid
, ut
.ut_id
, 8, UT_NAMESIZE
, ut
.ut_user
,
145 12, UT_LINESIZE
, ut
.ut_line
, 20, UT_HOSTSIZE
, ut
.ut_host
,
146 addr_string
, time_string
);
150 dump(FILE *fp
, int forever
, int oldfmt
)
156 fseek(fp
, -10 * (oldfmt
? sizeof uto
: sizeof ut
), SEEK_END
);
160 while (fread(&uto
, sizeof uto
, 1, fp
) == 1)
161 print_utline(oldtonew(uto
));
163 while (fread(&ut
, sizeof ut
, 1, fp
) == 1)
165 if (forever
) sleep(1);
169 /* This function won't work properly if there's a ']' or a ' ' in the real
170 * token. Thankfully, this should never happen. */
172 gettok(char *line
, char *dest
, int size
, int eatspace
)
174 int bpos
, epos
, eaten
;
177 bpos
= strchr(line
, '[') - line
;
179 fprintf(stderr
, "Extraneous newline in file. Exiting.");
184 epos
= strchr(line
, ']') - line
;
186 fprintf(stderr
, "Extraneous newline in file. Exiting.");
191 eaten
= bpos
+ epos
+ 1;
194 if ((t
= strchr(line
, ' ')))
197 strncpy(dest
, line
, size
);
203 undump(FILE *fp
, int forever
, int oldfmt
)
207 char s_addr
[16], s_time
[29], *linestart
, *line
;
210 line
= linestart
= malloc(1024 * sizeof *linestart
);
214 while(fgets(linestart
, 1023, fp
))
217 memset(&ut
, '\0', sizeof(ut
));
218 sscanf(line
, "[%hd] [%d] [%4c] ", &ut
.ut_type
, &ut
.ut_pid
, ut
.ut_id
);
221 line
+= gettok(line
, ut
.ut_user
, sizeof(ut
.ut_user
), 1);
222 line
+= gettok(line
, ut
.ut_line
, sizeof(ut
.ut_line
), 1);
223 line
+= gettok(line
, ut
.ut_host
, sizeof(ut
.ut_host
), 1);
224 line
+= gettok(line
, s_addr
, sizeof(s_addr
)-1, 1);
225 line
+= gettok(line
, s_time
, sizeof(s_time
)-1, 0);
227 ut
.ut_addr
= inet_addr(s_addr
);
228 ut
.ut_time
= strtotime(s_time
);
232 fwrite(&uto
, sizeof(uto
), 1, stdout
);
234 fwrite(&ut
, sizeof(ut
), 1, stdout
);
245 printf("Usage: utmpdump [ -froh ] [ filename ]\n");
249 int main(int argc
, char **argv
)
253 int reverse
= 0, forever
= 0, oldfmt
= 0;
255 while ((c
= getopt(argc
, argv
, "froh")) != EOF
) {
279 fprintf(stderr
, "Utmp %sdump of %s\n", reverse
? "un" : "", argv
[optind
]);
280 if ((fp
= fopen(argv
[optind
], "r")) == NULL
) {
281 perror("Unable to open file");
286 fprintf(stderr
, "Utmp %sdump of stdin\n", reverse
? "un" : "");
291 undump(fp
, forever
, oldfmt
);
293 dump(fp
, forever
, oldfmt
);