summaryrefslogtreecommitdiff
path: root/utils/iscsi-iname.c
blob: c241aaf12ac68073eb2f5d92e673cad838506cd7 (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
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
/*
 * iSCSI InitiatorName creation utility
 * Copyright (C) 2001 Cisco Systems, Inc.
 * maintained by linux-iscsi-devel@lists.sourceforge.net
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * See the file COPYING included with this distribution for more details.
 *
 * $Id: iscsi-iname.c,v 1.1.2.3 2005/03/15 06:33:44 wysochanski Exp $
 *
 * iscsi-iname.c - Compute an iSCSI InitiatorName for this host.
 * Note that to ensure uniqueness, the system time is
 * a factor.  This name must be cached and only regenerated
 * if there is no cached value.
 */

#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/utsname.h>
#include <sys/time.h>
#include <getopt.h>
#include <stdbool.h>

#include "md5.h"

#define RANDOM_NUM_GENERATOR	"/dev/urandom"

#define DEFAULT_PREFIX		"iqn.2016-04.com.open-iscsi"

/* iSCSI names have a maximum length of 223 characters, we reserve 13 to append
 * a seperator and 12 characters (6 random bytes in hex representation) */
#define PREFIX_MAX_LEN 210

static void usage(void)
{
	fprintf(stderr, "Usage: iscsi-iname [OPTIONS]\n");
	fprintf(stderr, "Where OPTIONS are from:\n");
	fprintf(stderr, "    -p/--prefix <prefix>          -- set IQN prefix [%s]\n",
			DEFAULT_PREFIX);
	fprintf(stderr, "    -g/--generate-iname-prefix    -- generate the InitiatorName= prefix\n");
	fprintf(stderr, "where <prefix> has max length of %d\n",
		PREFIX_MAX_LEN);
}

int
main(int argc, char *argv[])
{
	struct timeval time;
	struct utsname system_info;
	long hostid;
	struct MD5Context context;
	unsigned char digest[16];
	unsigned char *bytes = digest;
	unsigned char entropy[16];
	int e;
	int fd;
	char *prefix = DEFAULT_PREFIX;
	int c;
	char *short_options = "p:gh";
	struct option const long_options[] = {
		{"help", no_argument, NULL, 'h'},
		{"prefix", required_argument, NULL, 'p'},
		{"generate-iname-prefix", no_argument, NULL, 'g'},
		{NULL, 0, NULL, 0}
	};
	bool generate_iname_prefix = false;

	/* initialize */
	memset(digest, 0, sizeof (digest));
	memset(&context, 0, sizeof (context));
	MD5Init(&context);

	/* take a prefix if given, otherwise use a default. */
	while ((c = getopt_long(argc, argv, short_options, long_options, NULL)) >= 0) {
		switch (c) {
		case 'p':
			prefix = optarg;
			if (strnlen(prefix, PREFIX_MAX_LEN + 1) > PREFIX_MAX_LEN) {
				fprintf(stderr, "error: prefix too long\n");
				usage();
				exit(1);
			}
			break;
		case 'h':
			usage();
			exit(0);
		case 'g':
			generate_iname_prefix = true;
			break;
		default:
		case '?':
			usage();
			exit(1);
		}
	}
	if (optind < argc) {
		fprintf(stderr, "unknown argument(s)\n");
		usage();
		exit(1);
	}

	/* try to feed some entropy from the pool to MD5 in order to get
	 * uniqueness properties
	 */

	fd = open(RANDOM_NUM_GENERATOR, O_RDONLY);
	if (fd != -1) {
		e = read(fd, &entropy, 16);
		if (e >= 1)
			MD5Update(&context, (md5byte *)entropy, e);
		close(fd);
	}

	/* time the name is created is a factor in order to get
	 * uniqueness properties
	 */
	if (gettimeofday(&time, NULL) < 0) {
		perror("error: gettimeofday failed");
		return 1;
	}
	MD5Update(&context, (md5byte *) & time.tv_sec, sizeof (time.tv_sec));
	MD5Update(&context, (md5byte *) & time.tv_usec, sizeof (time.tv_usec));

	/* hostid */
	hostid = gethostid();
	MD5Update(&context, (md5byte *) & hostid, sizeof (hostid));

	/* get the hostname and system name */
	if (uname(&system_info) < 0) {
		perror("error: uname failed");
		return 1;
	}
	MD5Update(&context, (md5byte *) system_info.sysname,
		  sizeof (system_info.sysname));
	MD5Update(&context, (md5byte *) system_info.nodename,
		  sizeof (system_info.nodename));
	MD5Update(&context, (md5byte *) system_info.release,
		  sizeof (system_info.release));
	MD5Update(&context, (md5byte *) system_info.version,
		  sizeof (system_info.version));
	MD5Update(&context, (md5byte *) system_info.machine,
		  sizeof (system_info.machine));

	/* compute the md5 hash of all the bits we just collected */
	MD5Final(digest, &context);

	/* vary which md5 bytes we pick (though we probably don't need to do
	 * this, since hopefully MD5 produces results such that each byte is as
	 * good as any other).
	 */

	fd = open(RANDOM_NUM_GENERATOR, O_RDONLY);
	if (fd != -1) {
		if (read(fd, entropy, 1) == 1)
			bytes = &digest[(entropy[0] % (sizeof(digest) - 6))];
		close(fd);
	}

	/* print the prefix followed by 6 bytes of the MD5 hash */
	printf("%s%s:%x%x%x%x%x%x\n",
		generate_iname_prefix ? "InitiatorName=" : "",
		prefix,
		bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5]);
	return 0;
}