]> git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/utmpdump.c
utmpdump: document optional filename argument
[thirdparty/util-linux.git] / login-utils / utmpdump.c
1 /*
2 * utmpdump
3 *
4 * Simple program to dump UTMP and WTMP files in raw format, so they can be
5 * examined.
6 *
7 * Based on utmpdump dump from sysvinit suite.
8 *
9 * Copyright (C) 1991-2000 Miquel van Smoorenburg <miquels@cistron.nl>
10 *
11 * Copyright (C) 1998 Danek Duvall <duvall@alumni.princeton.edu>
12 * Copyright (C) 2012 Karel Zak <kzak@redhat.com>
13 *
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.
18 *
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.
23 *
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
27 */
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <utmp.h>
32 #include <time.h>
33 #include <ctype.h>
34 #include <getopt.h>
35 #include <unistd.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38
39 #include "c.h"
40 #include "nls.h"
41 #include "xalloc.h"
42 #include "closestream.h"
43
44 static char *timetostr(const time_t time)
45 {
46 static char s[29]; /* [Sun Sep 01 00:00:00 1998 PST] */
47
48 if (time != 0)
49 strftime(s, 29, "%a %b %d %T %Y %Z", localtime(&time));
50 else
51 s[0] = '\0';
52
53 return s;
54 }
55
56 static time_t strtotime(const char *s_time)
57 {
58 struct tm tm;
59
60 memset(&tm, '\0', sizeof(struct tm));
61
62 if (s_time[0] == ' ' || s_time[0] == '\0')
63 return (time_t)0;
64
65 strptime(s_time, "%a %b %d %T %Y", &tm);
66
67 /* Cheesy way of checking for DST */
68 if (s_time[26] == 'D')
69 tm.tm_isdst = 1;
70
71 return mktime(&tm);
72 }
73
74 #define cleanse(x) xcleanse(x, sizeof(x))
75 static void xcleanse(char *s, int len)
76 {
77 for ( ; *s && len-- > 0; s++)
78 if (!isprint(*s) || *s == '[' || *s == ']')
79 *s = '?';
80 }
81
82 static void unspace(char *s, int len)
83 {
84 while (*s && *s != ' ' && len--)
85 ++s;
86
87 if (len > 0)
88 *s = '\0';
89 }
90
91 static void print_utline(struct utmp ut)
92 {
93 char *addr_string, *time_string;
94 struct in_addr in;
95
96 in.s_addr = ut.ut_addr;
97 addr_string = inet_ntoa(in);
98 time_string = timetostr(ut.ut_time);
99 cleanse(ut.ut_id);
100 cleanse(ut.ut_user);
101 cleanse(ut.ut_line);
102 cleanse(ut.ut_host);
103
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);
109 }
110
111 static void dump(FILE *fp, int forever)
112 {
113 struct utmp ut;
114
115 if (forever)
116 fseek(fp, -10 * sizeof(ut), SEEK_END);
117
118 do {
119 while (fread(&ut, sizeof(ut), 1, fp) == 1)
120 print_utline(ut);
121 if (forever)
122 sleep(1);
123 } while (forever);
124 }
125
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)
129 {
130 int bpos, epos, eaten;
131 char *t;
132
133 bpos = strchr(line, '[') - line;
134 if (bpos < 0)
135 errx(EXIT_FAILURE, _("Extraneous newline in file. Exiting."));
136
137 line += 1 + bpos;
138 epos = strchr(line, ']') - line;
139 if (epos < 0)
140 errx(EXIT_FAILURE,_("Extraneous newline in file. Exiting."));
141
142 line[epos] = '\0';
143 eaten = bpos + epos + 1;
144
145 if (eatspace) {
146 if ((t = strchr(line, ' ')))
147 *t = 0;
148 }
149 strncpy(dest, line, size);
150
151 return eaten + 1;
152 }
153
154 static void undump(FILE *fp)
155 {
156 struct utmp ut;
157 char s_addr[16], s_time[29], *linestart, *line;
158 int count = 0;
159
160 line = linestart = xmalloc(1024 * sizeof(*linestart));
161 s_addr[15] = 0;
162 s_time[28] = 0;
163
164 while (fgets(linestart, 1023, fp))
165 {
166 line = linestart;
167 memset(&ut, '\0', sizeof(ut));
168 sscanf(line, "[%hd] [%d] [%4c] ", &ut.ut_type, &ut.ut_pid, ut.ut_id);
169
170 line += 19;
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);
176
177 ut.ut_addr = inet_addr(s_addr);
178 ut.ut_time = strtotime(s_time);
179
180 fwrite(&ut, sizeof(ut), 1, stdout);
181
182 ++count;
183 }
184
185 free(linestart);
186 }
187
188 static void __attribute__((__noreturn__)) usage(FILE *out)
189 {
190 fputs(USAGE_HEADER, out);
191
192 fprintf(out,
193 _(" %s [options] [filename]\n"), program_invocation_short_name);
194
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);
200
201 fprintf(out, USAGE_MAN_TAIL("utmpdump(1)"));
202 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
203 }
204
205 int main(int argc, char **argv)
206 {
207 int c;
208 FILE *fp;
209 int reverse = 0, forever = 0;
210 const char *filename = NULL;
211
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' },
217 { NULL, 0, 0, 0 }
218 };
219
220 setlocale(LC_ALL, "");
221 bindtextdomain(PACKAGE, LOCALEDIR);
222 textdomain(PACKAGE);
223 atexit(close_stdout);
224
225 while ((c = getopt_long(argc, argv, "frhV", longopts, NULL)) != -1) {
226 switch (c) {
227 case 'r':
228 reverse = 1;
229 break;
230
231 case 'f':
232 forever = 1;
233 break;
234
235 case 'h':
236 usage(stdout);
237 break;
238 case 'V':
239 printf(UTIL_LINUX_VERSION);
240 return EXIT_SUCCESS;
241 default:
242 usage(stderr);
243 }
244 }
245
246 if (optind < argc) {
247 filename = argv[optind];
248 fp = fopen(filename, "r");
249 if (!fp)
250 err(EXIT_FAILURE, _("%s: open failed"), filename);
251 } else {
252 filename = "stdin";
253 fp = stdin;
254 }
255
256 if (reverse) {
257 fprintf(stderr, _("Utmp undump of %s\n"), filename);
258 undump(fp);
259 } else {
260 fprintf(stderr, _("Utmp dump of %s\n"), filename);
261 dump(fp, forever);
262 }
263
264 if (fp != stdin)
265 fclose(fp);
266
267 return EXIT_SUCCESS;
268 }