]>
Commit | Line | Data |
---|---|---|
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_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 | } | |
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 | ||
159 | void | |
160 | dump(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. */ | |
181 | int | |
182 | gettok(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 | ||
212 | void | |
213 | # ifdef __GNUC__ | |
214 | undump(FILE *fp, int forever __attribute__((unused)), int oldfmt) | |
215 | #else | |
216 | undump(FILE *fp, int forever, int oldfmt) | |
217 | #endif | |
218 | { | |
219 | struct utmp ut; | |
220 | struct oldutmp uto; | |
221 | char s_addr[16], s_time[29], *linestart, *line; | |
222 | int count = 0; | |
223 | ||
224 | line = linestart = malloc(1024 * sizeof *linestart); | |
225 | s_addr[15] = 0; | |
226 | s_time[28] = 0; | |
227 | ||
228 | while(fgets(linestart, 1023, fp)) | |
229 | { | |
230 | line = linestart; | |
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); | |
240 | ||
241 | ut.ut_addr = inet_addr(s_addr); | |
242 | ut.ut_time = strtotime(s_time); | |
243 | ||
244 | if (oldfmt) { | |
245 | uto = newtoold(ut); | |
246 | fwrite(&uto, sizeof(uto), 1, stdout); | |
247 | } else | |
248 | fwrite(&ut, sizeof(ut), 1, stdout); | |
249 | ||
250 | ++count; | |
251 | } | |
252 | ||
253 | free(linestart); | |
254 | } | |
255 | ||
256 | void | |
257 | usage(int result) | |
258 | { | |
259 | printf("Usage: utmpdump [ -froh ] [ filename ]\n"); | |
260 | exit(result); | |
261 | } | |
262 | ||
263 | int main(int argc, char **argv) | |
264 | { | |
265 | int c; | |
266 | FILE *fp; | |
267 | int reverse = 0, forever = 0, oldfmt = 0; | |
268 | ||
269 | while ((c = getopt(argc, argv, "froh")) != EOF) { | |
270 | switch (c) { | |
271 | case 'r': | |
272 | reverse = 1; | |
273 | break; | |
274 | ||
275 | case 'f': | |
276 | forever = 1; | |
277 | break; | |
278 | ||
279 | case 'o': | |
280 | oldfmt = 1; | |
281 | break; | |
282 | ||
283 | case 'h': | |
284 | usage(0); | |
285 | break; | |
286 | ||
287 | default: | |
288 | usage(1); | |
289 | } | |
290 | } | |
291 | ||
292 | if (optind < argc) { | |
293 | fprintf(stderr, "Utmp %sdump of %s\n", reverse ? "un" : "", argv[optind]); | |
294 | if ((fp = fopen(argv[optind], "r")) == NULL) { | |
295 | perror("Unable to open file"); | |
296 | exit(1); | |
297 | } | |
298 | } | |
299 | else { | |
300 | fprintf(stderr, "Utmp %sdump of stdin\n", reverse ? "un" : ""); | |
301 | fp = stdin; | |
302 | } | |
303 | ||
304 | if (reverse) | |
305 | undump(fp, forever, oldfmt); | |
306 | else | |
307 | dump(fp, forever, oldfmt); | |
308 | ||
309 | fclose(fp); | |
310 | ||
311 | return 0; | |
312 | } |