File: system_id.c

package info (click to toggle)
diskscan 0.21-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,656 kB
  • sloc: ansic: 11,136; python: 338; xml: 138; sh: 41; makefile: 36
file content (101 lines) | stat: -rw-r--r-- 2,135 bytes parent folder | download | duplicates (7)
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
#include "system_id.h"

#include "sha1.h"
#include "arch.h"

#include <memory.h>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>

static void sha1_calc(const unsigned char *src, int src_len, char *out, int out_size)
{
	assert(out_size >= SHA1_DIGEST_SIZE*2+1);

	unsigned char digest[SHA1_DIGEST_SIZE];
	SHA1_CTX sha1_ctx;
	SHA1_Init(&sha1_ctx);
	SHA1_Update(&sha1_ctx, src, src_len);
	SHA1_Final(&sha1_ctx, digest);

	int i;
	for (i = 0; i < SHA1_DIGEST_SIZE; i++) {
		sprintf(out + i*2, "%02X", digest[i]);
	}
}

static bool cmd_str(const char *cmd, char *buf, int len)
{
	FILE *f = popen(cmd, "r");
	if (!f)
		return false;

	char *ret = fgets(buf, len, f);
	pclose(f);
	return ret != NULL;
}

static void dmidecode_read(const char *field_name, char *buf, int len)
{
	char cmd[128];

	memset(buf, 0, len);

	snprintf(cmd, sizeof(cmd), "dmidecode -s %s", field_name);
	if (!cmd_str(cmd, buf, len))
		return;

	int actual_len = strlen(buf);
	switch (buf[actual_len-1]) {
		case '\r':
		case '\n':
			buf[actual_len-1] = 0;
			actual_len--;
			break;
	}

	sha1_calc((unsigned char *)buf, actual_len, buf, len);
}

static void system_serial_read(char *buf, int len)
{
	dmidecode_read("system-serial-number", buf, len);
}

static void chassis_serial_read(char *buf, int len)
{
	dmidecode_read("chassis-serial-number", buf, len);
}

static void baseboard_serial_read(char *buf, int len)
{
	dmidecode_read("baseboard-serial-number", buf, len);
}

static void os_read(char *buf, int len)
{
	cmd_str("uname -o", buf, len);

	int i;
	for (i = strlen(buf) - 1; i >= 0; i--) {
		if (!isspace(buf[i]))
			break;
		buf[i] = 0;
	}
}

bool system_identifier_read(system_identifier_t *system_id)
{
	os_read(system_id->os, sizeof(system_id->os));
	system_serial_read(system_id->system, sizeof(system_id->system));
	chassis_serial_read(system_id->chassis, sizeof(system_id->chassis));
	baseboard_serial_read(system_id->baseboard, sizeof(system_id->baseboard));

	unsigned char mac[6];
	mac_read(mac, sizeof(mac));
	sha1_calc(mac, sizeof(mac), system_id->mac, sizeof(system_id->mac));

	return true;
}