]> git.wh0rd.org - sysvinit.git/blob - src/utmpdump.c
Adjust makefile to make sure the install directories are created before files are...
[sysvinit.git] / src / utmpdump.c
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
41 struct utmp
42 oldtonew(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
59 struct oldutmp
60 newtoold(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
77 char *
78 timetostr(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
90 time_t
91 strtotime(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))
110 void
111 xcleanse(char *s, int len)
112 {
113 for ( ; *s && len-- > 0; s++)
114 if (!isprint(*s) || *s == '[' || *s == ']')
115 *s = '?';
116 }
117
118 void
119 unspace(char *s, int len)
120 {
121 while (*s && *s != ' ' && len--)
122 ++s;
123
124 if (len > 0)
125 *s = '\0';
126 }
127
128 void
129 print_utline(struct utmp ut)
130 {
131 char *addr_string, *time_string;
132 struct in_addr in;
133
134 in.s_addr = ut.ut_addr;
135 addr_string = inet_ntoa(in);
136 time_string = timetostr(ut.ut_time);
137 cleanse(ut.ut_id);
138 cleanse(ut.ut_user);
139 cleanse(ut.ut_line);
140 cleanse(ut.ut_host);
141
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);
147 }
148
149 void
150 dump(FILE *fp, int forever, int oldfmt)
151 {
152 struct utmp ut;
153 struct oldutmp uto;
154
155 if (forever)
156 fseek(fp, -10 * (oldfmt ? sizeof uto : sizeof ut), SEEK_END);
157
158 do {
159 if (oldfmt)
160 while (fread(&uto, sizeof uto, 1, fp) == 1)
161 print_utline(oldtonew(uto));
162 else
163 while (fread(&ut, sizeof ut, 1, fp) == 1)
164 print_utline(ut);
165 if (forever) sleep(1);
166 } while (forever);
167 }
168
169 /* This function won't work properly if there's a ']' or a ' ' in the real
170 * token. Thankfully, this should never happen. */
171 int
172 gettok(char *line, char *dest, int size, int eatspace)
173 {
174 int bpos, epos, eaten;
175 char *t;
176
177 bpos = strchr(line, '[') - line;
178 if (bpos < 0) {
179 fprintf(stderr, "Extraneous newline in file. Exiting.");
180 exit(1);
181 }
182 line += 1 + bpos;
183
184 epos = strchr(line, ']') - line;
185 if (epos < 0) {
186 fprintf(stderr, "Extraneous newline in file. Exiting.");
187 exit(1);
188 }
189 line[epos] = '\0';
190
191 eaten = bpos + epos + 1;
192
193 if (eatspace)
194 if ((t = strchr(line, ' ')))
195 *t = 0;
196
197 strncpy(dest, line, size);
198
199 return eaten + 1;
200 }
201
202 void
203 undump(FILE *fp, int forever, int oldfmt)
204 {
205 struct utmp ut;
206 struct oldutmp uto;
207 char s_addr[16], s_time[29], *linestart, *line;
208 int count = 0;
209
210 line = linestart = malloc(1024 * sizeof *linestart);
211 s_addr[15] = 0;
212 s_time[28] = 0;
213
214 while(fgets(linestart, 1023, fp))
215 {
216 line = linestart;
217 memset(&ut, '\0', sizeof(ut));
218 sscanf(line, "[%hd] [%d] [%4c] ", &ut.ut_type, &ut.ut_pid, ut.ut_id);
219
220 line += 19;
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);
226
227 ut.ut_addr = inet_addr(s_addr);
228 ut.ut_time = strtotime(s_time);
229
230 if (oldfmt) {
231 uto = newtoold(ut);
232 fwrite(&uto, sizeof(uto), 1, stdout);
233 } else
234 fwrite(&ut, sizeof(ut), 1, stdout);
235
236 ++count;
237 }
238
239 free(linestart);
240 }
241
242 void
243 usage(int result)
244 {
245 printf("Usage: utmpdump [ -froh ] [ filename ]\n");
246 exit(result);
247 }
248
249 int main(int argc, char **argv)
250 {
251 int c;
252 FILE *fp;
253 int reverse = 0, forever = 0, oldfmt = 0;
254
255 while ((c = getopt(argc, argv, "froh")) != EOF) {
256 switch (c) {
257 case 'r':
258 reverse = 1;
259 break;
260
261 case 'f':
262 forever = 1;
263 break;
264
265 case 'o':
266 oldfmt = 1;
267 break;
268
269 case 'h':
270 usage(0);
271 break;
272
273 default:
274 usage(1);
275 }
276 }
277
278 if (optind < argc) {
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");
282 exit(1);
283 }
284 }
285 else {
286 fprintf(stderr, "Utmp %sdump of stdin\n", reverse ? "un" : "");
287 fp = stdin;
288 }
289
290 if (reverse)
291 undump(fp, forever, oldfmt);
292 else
293 dump(fp, forever, oldfmt);
294
295 fclose(fp);
296
297 return 0;
298 }