summaryrefslogtreecommitdiffstats
path: root/utmpr.c
blob: 0e04939662212263e5e83bd5aa6bf02be35b0c96 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright (C) 2011 Jason A. Donenfeld <Jason@zx2c4.com>
 * Copyright (C) 2011 Adam Weiss <Adam@signal11.com>
 */

#include <stdio.h>
#include <inttypes.h>
#include <utmp.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

void do_file(char *filename);
void detect_filetype(FILE *fp);
void binary_to_text(FILE *fp);
void text_to_binary(FILE *fp);
void version();
void usage();

#if __WORDSIZE == 64 && defined __WORDSIZE_COMPAT32
#define UTMP_TEXT_FORMAT "%hd\t%d\t%s\t%s\t%s\t%s\t%hd\t%hd\t%"PRId32"\t%"PRId32"\t%"PRId32"\t%s\n"
#else
#define UTMP_TEXT_FORMAT "%hd\t%d\t%s\t%s\t%s\t%s\t%hd\t%hd\t%ld\t%ld\t%ld\t%s\n"
#endif
#define TEXT_SIZE 422

#define VERSION "0.1"
char *program_name;
static int read_binary_mode = -1;
static FILE *output_file;

int main(int argc, char **argv)
{
	program_name = argv[0];
	output_file = stdout;

	static struct option long_options[] = {
		{"version",	no_argument,		0,	'v'},
		{"help",	no_argument,		0,	'h'},
		{"binary",	no_argument,		0,	'b'},
		{"text",	no_argument,		0,	't'},
		{"output",	required_argument,	0,	'o'}
	};
	int c;
	int option_index;
	while (1) {
		c  = getopt_long(argc, argv, "tbhvo:", long_options, &option_index);
		if (c == -1)
			break;

		switch (c) {
			case 'h':
				usage();
				break;
			case 'v':
				version();
				break;
			case 't':
			case 'b':
				if (read_binary_mode == -1)
					read_binary_mode = (c == 'b') ? 1 : 0;
				else {
					fprintf(stderr, "%s: You may not specify both binary and text mode\n", program_name);
					exit(EXIT_FAILURE);
				}
				break;
			case 'o':
				if (!(optarg[0] == '-' && optarg[1] == '\0')) {
					output_file = fopen(optarg, "w");
					if (output_file == NULL) {
						fprintf(stderr, "%s: %s: %s\n", program_name, optarg, strerror(errno));
						exit(EXIT_FAILURE);
					}
				}
				break;
			default:
				exit(EXIT_FAILURE);
		}
	}

	if (optind < argc) {
		while (optind < argc)
			do_file(argv[optind++]);
	}
	else
		do_file("-");

	return 0;
}

void do_file(char *filename)
{
	FILE *fp;
	if (filename[0] == '-' && filename[1] == '\0')
		fp = stdin;
	else {
		fp = fopen(filename, "r");
		if (fp == NULL) {
			fprintf(stderr, "%s: %s: %s\n", program_name, filename, strerror(errno));
			exit(EXIT_FAILURE);
		}
	}

	if (read_binary_mode == -1)
		detect_filetype(fp);

	if (read_binary_mode == 1)
		binary_to_text(fp);
	else
		text_to_binary(fp);

	if (fp != stdin)
		fclose(fp);
}

void detect_filetype(FILE *fp)
{
	// we only read 1 character and then ungetc it because you can't rewind stdin past 1 character
	int character = fgetc(fp);
	// every record should start with something less than ascii zero
	read_binary_mode = character < '0';
	ungetc(character, fp);
}

void binary_to_text(FILE *fp)
{
	struct utmp entry;
	char ip[16];
	while (fread(&entry, sizeof(struct utmp), 1, fp)) {
		if (0) //TODO: determine how to figure out of it's an ipv6. entries 1 2 and 3 are zero perhaps?
			ip[0] = 'a'; //TODO: format ipv6 address
		else
			sprintf(ip, "%d.%d.%d.%d",
				(entry.ut_addr_v6[0] >> 0x0) & 0xFF,
				(entry.ut_addr_v6[0] >> 0x8) & 0xFF,
				(entry.ut_addr_v6[0] >> 0x10) & 0xFF,
				(entry.ut_addr_v6[0] >> 0x18) & 0xFF);

		fprintf(output_file, UTMP_TEXT_FORMAT, entry.ut_type, entry.ut_pid, entry.ut_line,
					 entry.ut_id, entry.ut_user, entry.ut_host,
					 entry.ut_exit.e_termination, entry.ut_exit.e_exit,
					 entry.ut_session, entry.ut_tv.tv_sec,
					 entry.ut_tv.tv_usec, ip);
	}
}

void text_to_binary(FILE *fp)
{
	char parser[] = UTMP_TEXT_FORMAT;
	char *parse_tokens[12];
	char *parse_token;
	int i;
	for (parse_token = strtok(parser, "\t"), i = 0; parse_token && i < 12; parse_token = strtok(NULL, "\t"), ++i)
		parse_tokens[i] = parse_token;
	
	char ip[16];
	struct utmp entry;
	void *entities[] = { &entry.ut_type, &entry.ut_pid, entry.ut_line,
			     entry.ut_id, entry.ut_user, entry.ut_host,
			     &entry.ut_exit.e_termination, &entry.ut_exit.e_exit,
			     &entry.ut_session, &entry.ut_tv.tv_sec,
			     &entry.ut_tv.tv_usec, ip };

	unsigned char ip_n[4];
	char line[TEXT_SIZE];
	char *input_token;
	char *scanner;
	while (fgets(line, TEXT_SIZE, fp)) {
		memset(&entry, 0, sizeof(struct utmp));
		input_token = line;
		for (i = 0; i < 12; ++i) {
			scanner = input_token;
			do {
				if (*scanner == '\t') {
					*scanner = '\0';
					break;
				}
			} while (*++scanner != '\0'); //TODO: unsafe if no null
			//TODO: check to see if token is %s, and then just copy the pointer instead of scanfing it - unsafe otherwise
			sscanf(input_token, parse_tokens[i], entities[i]);
			//TODO: don't go past end of string if there aren't 12 entries - unsafe otherwise
			input_token = scanner + 1;
		}
		sscanf(ip, "%hhu.%hhu.%hhu.%hhu", &ip_n[0], &ip_n[1], &ip_n[2], &ip_n[3]);
		entry.ut_addr_v6[0] = ((ip_n[3] << 24) |
				       (ip_n[2] << 16) |
				       (ip_n[1] << 8)  |
				       (ip_n[0]));
		//TODO: ipv6 addresses...

		fwrite(&entry, sizeof(struct utmp), 1, output_file);
	}
}

void version()
{
	printf("utmpr "VERSION"\n");
	exit(EXIT_SUCCESS);
}

void usage()
{
	printf("Usage %s: [OPTION]... [FILE]...\n", program_name);
	printf("Convert FILE(s), or standard input from/to utmp binary format/text\n");
	printf("and print to standard output\n\n");

	printf("  -t, --text         read text format and write utmp binary\n");
	printf("  -b, --binary       read utmp binary and write text\n");
	printf("  -o, --output FILE  write output to FILE instead of standard out\n");
	printf("      --help         display this help and exit\n");
	printf("      --version      output version and exit\n");

	printf("\nWith no FILE, or when FILE is -, read standard input.\n");
	printf("If neither --text nor --binary specified, auto-detect.\n\n");

	printf("Your system wtmp is located at: "WTMP_FILENAME"\n");
	printf("Your system utmp is located at: "UTMP_FILENAME"\n");
	exit(EXIT_SUCCESS);
}