File: prkey.c

package info (click to toggle)
multipath-tools 0.14.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,088 kB
  • sloc: ansic: 64,885; perl: 1,622; makefile: 742; sh: 732; pascal: 155
file content (245 lines) | stat: -rw-r--r-- 5,648 bytes parent folder | download
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "structs.h"
#include "file.h"
#include "debug.h"
#include "config.h"
#include "util.h"
#include "propsel.h"
#include "strbuf.h"
#include "prkey.h"
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <errno.h>
#include "mt-udev-wrap.h"
/* MPATH_F_APTPL_MASK is publicly defined in mpath_persist.h */
#include <../libmpathpersist/mpath_persist.h>

#define PRKEY_READ 0
#define PRKEY_WRITE 1

int
print_reservation_key(struct strbuf *buff,
		      struct be64 key, uint8_t flags, int source)
{
	char *flagstr = "";
	if (source == PRKEY_SOURCE_NONE)
		return 0;
	if (source == PRKEY_SOURCE_FILE)
		return append_strbuf_quoted(buff, "file");
	if (flags & MPATH_F_APTPL_MASK)
		flagstr = ":aptpl";
	return print_strbuf(buff, "0x%" PRIx64 "%s", get_be64(key), flagstr);
}

static int parse_prkey(const char *ptr, uint64_t *prkey)
{
	if (!ptr)
		return 1;
	if (*ptr == '0')
		ptr++;
	if (*ptr == 'x' || *ptr == 'X')
		ptr++;
	if (*ptr == '\0' || strlen(ptr) > 16)
		return 1;
	if (strlen(ptr) != strspn(ptr, "0123456789aAbBcCdDeEfF"))
		return 1;
	if (sscanf(ptr, "%" SCNx64 "", prkey) != 1)
		return 1;
	return 0;
}

int parse_prkey_flags(char *ptr, uint64_t *prkey, uint8_t *flags)
{
	char *flagstr;

	flagstr = strchr(ptr, ':');
	*flags = 0;
	if (flagstr) {
		*flagstr++ = '\0';
		if (strlen(flagstr) == 5 && strcmp(flagstr, "aptpl") == 0)
			*flags = MPATH_F_APTPL_MASK;
	}
	return parse_prkey(ptr, prkey);
}

static int do_prkey(int fd, char *wwid, char *keystr, int cmd)
{
	char buf[4097];
	char *ptr;
	off_t start = 0;
	int bytes;

	while (1) {
		if (lseek(fd, start, SEEK_SET) < 0) {
			condlog(0, "prkey file read lseek failed : %s",
				strerror(errno));
			return 1;
		}
		bytes = read(fd, buf, 4096);
		if (bytes < 0) {
			if (errno == EINTR || errno == EAGAIN)
				continue;
			condlog(0, "failed to read from prkey file : %s",
				strerror(errno));
			return 1;
		}
		if (!bytes) {
			ptr = NULL;
			break;
		}
		buf[bytes] = '\0';
		ptr = strstr(buf, wwid);
		while (ptr) {
			if (ptr == buf || *(ptr - 1) != ' ' ||
			    *(ptr + strlen(wwid)) != '\n')
				ptr = strstr(ptr + strlen(wwid), wwid);
			else
				break;
		}
		if (ptr) {
			condlog(3, "found prkey for '%s'", wwid);
			ptr[strlen(wwid)] = '\0';
			if (ptr - PRKEY_SIZE < buf ||
			    (ptr - PRKEY_SIZE != buf &&
			     *(ptr - PRKEY_SIZE - 1) != '\n')) {
				condlog(0, "malformed prkey file line for wwid: '%s'", ptr);
				return 1;
			}
			ptr = ptr - PRKEY_SIZE;
			break;
		}
		ptr = strrchr(buf, '\n');
		if (ptr == NULL) {
			condlog(4, "couldn't file newline, assuming end of file");
			break;
		}
		start = start + (ptr - buf) + 1;
	}
	if (cmd == PRKEY_READ) {
		if (!ptr || *ptr == '#')
			return 1;
		memcpy(keystr, ptr, PRKEY_SIZE - 1);
		keystr[PRKEY_SIZE - 1] = '\0';
		return 0;
	}
	if (!ptr && !keystr)
		return 0;
	if (ptr) {
		if (lseek(fd, start + (ptr - buf), SEEK_SET) < 0) {
			condlog(0, "prkey write lseek failed : %s",
				strerror(errno));
			return 1;
		}
	}
	if (!keystr) {
		if (safe_write(fd, "#", 1) < 0) {
			condlog(0, "failed to write to prkey file : %s",
				strerror(errno));
			return 1;
		}
		return 0;
	}
	if (!ptr) {
		if (lseek(fd, 0, SEEK_END) < 0) {
			condlog(0, "prkey write lseek failed : %s",
				strerror(errno));
			return 1;
		}
	}
	bytes = sprintf(buf, "%s %s\n", keystr, wwid);
	if (safe_write(fd, buf, bytes) < 0) {
		condlog(0, "failed to write to prkey file: %s",
			strerror(errno));
		return 1;
	}
	return 0;
}

int get_prkey(struct multipath *mpp, uint64_t *prkey, uint8_t *sa_flags)
{
	int fd;
	int unused;
	int ret = 1;
	char keystr[PRKEY_SIZE];

	if (!strlen(mpp->wwid))
		goto out;

	fd = open_file(DEFAULT_PRKEYS_FILE, &unused, PRKEYS_FILE_HEADER);
	if (fd < 0)
		goto out;
	ret = do_prkey(fd, mpp->wwid, keystr, PRKEY_READ);
	if (ret)
		goto out_file;
	*sa_flags = 0;
	if (strchr(keystr, 'X'))
		*sa_flags = MPATH_F_APTPL_MASK;
	ret = !!parse_prkey(keystr, prkey);
out_file:
	close(fd);
out:
	return ret;
}

int set_prkey(struct config *conf, struct multipath *mpp, uint64_t prkey,
	      uint8_t sa_flags)
{
	int fd;
	int can_write = 1;
	int ret = 1;
	char keystr[PRKEY_SIZE];

	if (!strlen(mpp->wwid))
		goto out;

	if (sa_flags & ~MPATH_F_APTPL_MASK) {
		condlog(0, "unsupported pr flags, 0x%x",
			sa_flags & ~MPATH_F_APTPL_MASK);
		sa_flags &= MPATH_F_APTPL_MASK;
	}

	fd = open_file(DEFAULT_PRKEYS_FILE, &can_write, PRKEYS_FILE_HEADER);
	if (fd < 0)
		goto out;
	if (!can_write) {
		condlog(0, "cannot set prkey, prkeys file is read-only");
		goto out_file;
	}
	if (prkey) {
		/* using the capitalization of the 'x' is a hack, but
		 * it's unlikely that mpath_persist will support more options
		 * since sg_persist doesn't, and this lets us keep the
		 * same file format as before instead of needing to change
		 * the format of the prkeys file */
		if (sa_flags)
			snprintf(keystr, PRKEY_SIZE, "0X%016" PRIx64, prkey);
		else
			snprintf(keystr, PRKEY_SIZE, "0x%016" PRIx64, prkey);
		keystr[PRKEY_SIZE - 1] = '\0';
		ret = do_prkey(fd, mpp->wwid, keystr, PRKEY_WRITE);
	}
	else
		ret = do_prkey(fd, mpp->wwid, NULL, PRKEY_WRITE);
	if (ret == 0) {
		/*
		 * If you are reverting back to the old key, because you
		 * did not successfully set a new key, don't remember the
		 * key you never successfully set.
		 */
		if (get_be64(mpp->old_pr_key) == prkey)
			memset(&mpp->old_pr_key, 0, 8);
		else
			memcpy(&mpp->old_pr_key, &mpp->reservation_key, 8);
		select_reservation_key(conf, mpp);
	}
	if (get_be64(mpp->reservation_key) != prkey) {
		memset(&mpp->old_pr_key, 0, 8);
		ret = 1;
	}
out_file:
	close(fd);
out:
	return ret;
}