summaryrefslogtreecommitdiff
path: root/qa/src/anon-sa.c
blob: 2d5672e99ca4501b88f74f37cdca026ee0884f16 (plain)
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
/*
 * Anonymise binary sa data files: rewrite sa files
 * from users that are suitable for sharing/qa'ing.
 *
 * When sniffing out starting offsets of hostnames,
 * something like this command can prove handy:
 *   dd if=rhel5-sa20.bin bs=1 skip=109 count=100
 */

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#define SA_UTSNAME_LEN	65

long
seek_magic_and_reposition_for_hostname(int fd)
{
    int i;
    unsigned short sa_magic;

    struct magic {
	unsigned short	magic_offset;

	/* check magic - not endian neutral in sa ...  :| */
	unsigned short	numeric;
	unsigned short	swabbed;

	/* fortunately, all same length so far (65 bytes) */
	unsigned long	utsname_offset;
    } magic[] = {
	{  0, 0xd596, 0xd596,  89 },	/* sysstat-9.x & -10.x */
	{ 40, 0x216e, 0x6e21,  113 },	/* sysstat-8.x */
	{ 36, 0x2169, 0x6921,  109 },	/* sysstat-7.x */
    };

    for (i = 0; i < sizeof(magic) / sizeof(struct magic); i++) {
	if ((lseek(fd, magic[i].magic_offset, SEEK_SET)) < 0) {
	    perror("lseek magic");
	    continue;
	}
	if (read(fd, &sa_magic, sizeof(sa_magic)) < 0) {
	    perror("read magic");
	    continue;
	}
	if (sa_magic == magic[i].numeric || sa_magic == magic[i].swabbed) {
	    if ((lseek(fd, magic[i].utsname_offset, SEEK_SET)) < 0) {
		perror("hostname lseek");
		return -1;
	    }
	    return magic[i].utsname_offset;	/* all good */
	}
    }
    printf("  non-sa data file - cannot find any sa magic - ignored\n");
    return -1;
}

int main(int argc, char **argv)
{
    int count, fixit = 0, updates = 0;

    for (count = 1; count < argc; count++) {
	char hostname[SA_UTSNAME_LEN] = { 0 };
	char *file = argv[count];
	int fd, bytes, length;
	long offset;

	if (strcmp(file, "-f") == 0) {
	    fixit = 1;
	    continue;
	}
	printf("Filename: %s\n", file);
	if ((fd = open(file, O_RDWR)) < 0) {
	    perror("open");
	    continue;
	}
	if ((offset = seek_magic_and_reposition_for_hostname(fd)) < 0) {
	    close(fd);
	    continue;
	}
	if ((bytes = read(fd, hostname, sizeof(hostname))) < 0) {
	    perror("read");
	    close(fd);
	    continue;
	}
	hostname[sizeof(hostname)-1] = '\0';
	length = strlen(hostname);
	printf("  hostname: %s (%d bytes)\n", hostname, length);

	if (fixit) {
	    memset(hostname, 0, length);	// clear old name
	    strcpy(hostname, "pcp.qa.org");	// reset it
	    if ((lseek(fd, offset, SEEK_SET)) < 0) {
		perror("lseek2");
		close(fd);
		continue;
	    }
	    if (write(fd, hostname, length) < 0) {
		perror("write");
		close(fd);
		continue;
	    }
	    updates++;
	}
	close(fd);
    }
    printf("%d files scanned, %d files updated\n", count-1-fixit, updates);
    return 0;
}