Include -Wunreachable-code when building.
[sysvinit.git] / src / utmpdump.c
CommitLineData
a74aeac6
PR
1/*
2 * utmpdump Simple program to dump UTMP and WTMP files in
3 * raw format, so they can be examined.
4 *
5 * Author: Miquel van Smoorenburg, <miquels@cistron.nl>
6 * Danek Duvall <duvall@alumni.princeton.edu>
7 *
8 * Version: @(#)utmpdump 2.79 12-Sep-2000
9 *
10 * This file is part of the sysvinit suite,
11 * Copyright (C) 1991-2000 Miquel van Smoorenburg.
12 *
13 * Additional Copyright on this file 1998 Danek Duvall.
14 *
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.
19 *
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.
24 *
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
28 */
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <utmp.h>
34#include <time.h>
35#include <ctype.h>
36#include <unistd.h>
37#include <netinet/in.h>
38#include <arpa/inet.h>
39#include "oldutmp.h"
40
41struct utmp
42oldtonew(struct oldutmp src)
43{
44 struct utmp dest;
45
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);
55
56 return dest;
57}
58
59struct oldutmp
60newtoold(struct utmp src)
61{
62 struct oldutmp dest;
63
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);
73
74 return dest;
75}
76
77char *
78timetostr(const time_t time)
79{
80 static char s[29]; /* [Sun Sep 01 00:00:00 1998 PST] */
81
82 if (time != 0)
83 strftime(s, 29, "%a %b %d %T %Y %Z", localtime(&time));
84 else
85 s[0] = '\0';
86
87 return s;
88}
89
90time_t
91strtotime(const char *s_time)
92{
93 struct tm tm;
94
95 memset(&tm, '\0', sizeof(struct tm));
96
97 if (s_time[0] == ' ' || s_time[0] == '\0')
98 return (time_t)0;
99
100 strptime(s_time, "%a %b %d %T %Y", &tm);
101
102 /* Cheesy way of checking for DST */
103 if (s_time[26] == 'D')
104 tm.tm_isdst = 1;
105
106 return mktime(&tm);
107}
108
109#define cleanse(x) xcleanse(x, sizeof(x))
110void
111xcleanse(char *s, int len)
112{
113 for ( ; *s && len-- > 0; s++)
114 if (!isprint(*s) || *s == '[' || *s == ']')
115 *s = '?';
116}
117
118void
119unspace(char *s, int len)
120{
121 while (*s && *s != ' ' && len--)
122 ++s;
123
124 if (len > 0)
125 *s = '\0';
126}
127
128void
129print_utline(struct utmp ut)
130{
7cbe9cbe
DWF
131 char addr_buf[INET6_ADDRSTRLEN+1];
132 const char *addr_string, *time_string;
133 void *in_addr = &ut.ut_addr_v6;
134 size_t addr_length = INET6_ADDRSTRLEN;
135 int addr_family = AF_INET6;
136
137 if (!ut.ut_addr_v6[1] && !ut.ut_addr_v6[2] && !ut.ut_addr_v6[3]) {
138 addr_family = AF_INET;
139 addr_length = INET_ADDRSTRLEN;
140 in_addr = &ut.ut_addr;
141 }
142 if ((addr_string = inet_ntop(addr_family, in_addr, addr_buf, addr_length)) == 0) {
143 addr_buf[0] = '\0';
144 addr_string = &addr_buf[0];
145 }
a74aeac6
PR
146 time_string = timetostr(ut.ut_time);
147 cleanse(ut.ut_id);
148 cleanse(ut.ut_user);
149 cleanse(ut.ut_line);
150 cleanse(ut.ut_host);
151
152 /* pid id user line host addr time */
153 printf("[%d] [%05d] [%-4.4s] [%-*.*s] [%-*.*s] [%-*.*s] [%-15.15s] [%-28.28s]\n",
154 ut.ut_type, ut.ut_pid, ut.ut_id, 8, UT_NAMESIZE, ut.ut_user,
155 12, UT_LINESIZE, ut.ut_line, 20, UT_HOSTSIZE, ut.ut_host,
156 addr_string, time_string);
157}
158
159void
160dump(FILE *fp, int forever, int oldfmt)
161{
162 struct utmp ut;
163 struct oldutmp uto;
164
165 if (forever)
166 fseek(fp, -10 * (oldfmt ? sizeof uto : sizeof ut), SEEK_END);
167
168 do {
169 if (oldfmt)
170 while (fread(&uto, sizeof uto, 1, fp) == 1)
171 print_utline(oldtonew(uto));
172 else
173 while (fread(&ut, sizeof ut, 1, fp) == 1)
174 print_utline(ut);
175 if (forever) sleep(1);
176 } while (forever);
177}
178
179/* This function won't work properly if there's a ']' or a ' ' in the real
180 * token. Thankfully, this should never happen. */
181int
182gettok(char *line, char *dest, int size, int eatspace)
183{
184 int bpos, epos, eaten;
185 char *t;
186
187 bpos = strchr(line, '[') - line;
188 if (bpos < 0) {
189 fprintf(stderr, "Extraneous newline in file. Exiting.");
190 exit(1);
191 }
192 line += 1 + bpos;
193
194 epos = strchr(line, ']') - line;
195 if (epos < 0) {
196 fprintf(stderr, "Extraneous newline in file. Exiting.");
197 exit(1);
198 }
199 line[epos] = '\0';
200
201 eaten = bpos + epos + 1;
202
203 if (eatspace)
204 if ((t = strchr(line, ' ')))
205 *t = 0;
206
207 strncpy(dest, line, size);
208
209 return eaten + 1;
210}
211
212void
55f242bf
DWF
213# ifdef __GNUC__
214undump(FILE *fp, int forever __attribute__((unused)), int oldfmt)
215#else
a74aeac6 216undump(FILE *fp, int forever, int oldfmt)
55f242bf 217#endif
a74aeac6
PR
218{
219 struct utmp ut;
220 struct oldutmp uto;
3665eec2 221 char s_addr[16], s_time[29], *linestart;
a74aeac6
PR
222 int count = 0;
223
3665eec2 224 linestart = malloc(1024 * sizeof *linestart);
a74aeac6
PR
225 s_addr[15] = 0;
226 s_time[28] = 0;
227
228 while(fgets(linestart, 1023, fp))
229 {
3665eec2 230 char *line = linestart;
a74aeac6
PR
231 memset(&ut, '\0', sizeof(ut));
232 sscanf(line, "[%hd] [%d] [%4c] ", &ut.ut_type, &ut.ut_pid, ut.ut_id);
233
234 line += 19;
235 line += gettok(line, ut.ut_user, sizeof(ut.ut_user), 1);
236 line += gettok(line, ut.ut_line, sizeof(ut.ut_line), 1);
237 line += gettok(line, ut.ut_host, sizeof(ut.ut_host), 1);
238 line += gettok(line, s_addr, sizeof(s_addr)-1, 1);
239 line += gettok(line, s_time, sizeof(s_time)-1, 0);
3665eec2 240 (void)line; /* Quiet down static source analyzers */
a74aeac6
PR
241
242 ut.ut_addr = inet_addr(s_addr);
243 ut.ut_time = strtotime(s_time);
244
245 if (oldfmt) {
246 uto = newtoold(ut);
247 fwrite(&uto, sizeof(uto), 1, stdout);
248 } else
249 fwrite(&ut, sizeof(ut), 1, stdout);
250
251 ++count;
252 }
253
254 free(linestart);
255}
256
257void
258usage(int result)
259{
260 printf("Usage: utmpdump [ -froh ] [ filename ]\n");
261 exit(result);
262}
263
264int main(int argc, char **argv)
265{
266 int c;
267 FILE *fp;
268 int reverse = 0, forever = 0, oldfmt = 0;
269
270 while ((c = getopt(argc, argv, "froh")) != EOF) {
271 switch (c) {
272 case 'r':
273 reverse = 1;
274 break;
275
276 case 'f':
277 forever = 1;
278 break;
279
280 case 'o':
281 oldfmt = 1;
282 break;
283
284 case 'h':
285 usage(0);
286 break;
287
288 default:
289 usage(1);
290 }
291 }
292
293 if (optind < argc) {
294 fprintf(stderr, "Utmp %sdump of %s\n", reverse ? "un" : "", argv[optind]);
295 if ((fp = fopen(argv[optind], "r")) == NULL) {
296 perror("Unable to open file");
297 exit(1);
298 }
299 }
300 else {
301 fprintf(stderr, "Utmp %sdump of stdin\n", reverse ? "un" : "");
302 fp = stdin;
303 }
304
305 if (reverse)
306 undump(fp, forever, oldfmt);
307 else
308 dump(fp, forever, oldfmt);
309
310 fclose(fp);
311
312 return 0;
313}