-
Notifications
You must be signed in to change notification settings - Fork 0
/
looputils.c
308 lines (251 loc) · 8.23 KB
/
looputils.c
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Loopback-device utilities for cryptmount
* (C)Copyright 2005-2024, RW Penney
*/
/*
This file is part of cryptmount
cryptmount 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.
cryptmount 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.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#if HAVE_SYS_SYSMACROS_H
# include <sys/sysmacros.h>
#endif
#include <sys/types.h>
#ifdef HAVE_LINUX_LOOP_H
# include <linux/loop.h>
#else
# error loop.h kernel-header is needed to build cryptmount
#endif
#include <linux/major.h>
#include "cryptmount.h"
#include "looputils.h"
char *cm_strdup(const char *orig);
/**
* Format-strings for loopback devices,
* covering legacy /dev/loop/0 style
* as well as current /dev/loop0 style.
*/
static const char *loop_formats[] = {
"/dev/loop%u", "/dev/loop/%u", NULL };
/**
* Search for vacant loopback device.
*
* For recent kernels (>=3.1), this will use the /dev/loop-control
* interface, while for older kernels this will involve explicit
* search through /dev/loop0..255 for a device whose status indicates
* that it not associated with a backing file.
*/
int loop_findfree(char *buff, size_t buffsz)
{ unsigned found = 0;
int devfd, devno;
char loopname[256] = "";
#ifdef LOOP_CTL_GET_FREE
devfd = open("/dev/loop-control", O_RDWR);
devno = ioctl(devfd, LOOP_CTL_GET_FREE);
close(devfd);
if (devfd >=0 && devno >= 0) {
snprintf(loopname, sizeof(loopname), "/dev/loop%d", devno);
found = 1;
}
#else
for (devno=0; devno<256 && !found; ++devno) {
unsigned idx;
struct loop_info64 linfo;
struct stat sbuff;
for (idx=0; loop_formats[idx]!=NULL && !found; ++idx) {
snprintf(loopname, sizeof(loopname),
loop_formats[idx], (unsigned)devno);
if (stat(loopname, &sbuff) || !S_ISBLK(sbuff.st_mode)) continue;
devfd = open(loopname, O_RDONLY);
if (devfd < 0) continue;
if (ioctl(devfd, LOOP_GET_STATUS64, &linfo) && errno == ENXIO) {
found = 1;
}
close(devfd);
}
}
#endif /* LOOP_CTL_GET_FREE */
if (found && buff != NULL) strncpy(buff, loopname, buffsz);
return !found;
}
int loop_setup(const char *dev, const char *file, int flags)
/** Setup loopback device to point to regular file */
{ int devfd = -1, filefd = -1, eflag = ERR_NOERROR;
struct loop_info64 lpinfo;
memset((void*)&lpinfo, 0, sizeof(lpinfo));
strncpy((char*)lpinfo.lo_file_name, file, (size_t)LO_NAME_SIZE);
lpinfo.lo_offset = 0;
lpinfo.lo_encrypt_key_size = 0;
devfd = open(dev, flags);
#ifdef LOOP_CTL_ADD
if (devfd < 0) {
unsigned devno = ~0u;
int ctlfd;
sscanf(dev, loop_formats[0], &devno);
ctlfd = open("/dev/loop-control", O_RDWR);
(void)ioctl(ctlfd, LOOP_CTL_ADD, devno);
close(ctlfd);
devfd = open(dev, flags);
}
#endif
if (devfd < 0) {
fprintf(stderr, "Cannot open \"%s\" for reading\n", dev);
eflag = ERR_BADFILE;
goto bail_out;
}
filefd = open(file, flags);
if (filefd < 0) {
fprintf(stderr, "Cannot open \"%s\" for reading\n", file);
eflag = ERR_BADFILE;
goto bail_out;
}
if (ioctl(devfd, LOOP_SET_FD, filefd)
|| ioctl(devfd, LOOP_SET_STATUS64, &lpinfo)) {
fprintf(stderr, "LOOP_SET_FD ioctl() failed on \"%s\"\n", dev);
eflag = ERR_BADIOCTL;
goto bail_out;
}
bail_out:
if (filefd >= 0) close(filefd);
if (devfd >= 0) close(devfd);
return eflag;
}
int loop_destroy(const char *dev)
/** Detach loopback device from underlying file */
{ int devfd;
int eflag = ERR_NOERROR;
devfd = open(dev, O_RDONLY);
if (devfd < 0) {
fprintf(stderr, "Cannot open \"%s\" for reading\n", dev);
eflag = ERR_BADFILE;
goto bail_out;
}
if (ioctl(devfd, LOOP_CLR_FD, 0)) {
fprintf(stderr, "LOOP_CLR_FD ioctl() failed on \"%s\"\n", dev);
eflag = ERR_BADIOCTL;
goto bail_out;
}
#ifdef LOOP_CTL_REMOVE
{ int devno = -1, ctlfd;
sscanf(dev, loop_formats[0], &devno);
ctlfd = open("/dev/loop-control", O_RDWR);
(void)ioctl(ctlfd, LOOP_CTL_REMOVE, devno);
close(ctlfd);
}
#endif
bail_out:
if (devfd >= 0) (void)close(devfd);
return eflag;
}
int loop_ident(unsigned maj, unsigned min, char *buff, size_t buffsz)
/** Find device node for given minor device number */
{ unsigned idx;
int found=0;
char str[256];
struct stat sbuff;
if (maj != LOOP_MAJOR) return !found;
for (idx=0; loop_formats[idx]!=NULL && !found; ++idx) {
sprintf(str, loop_formats[idx], min);
if (stat(str, &sbuff) || !S_ISBLK(sbuff.st_mode)) continue;
found = ((unsigned)major(sbuff.st_rdev) == maj
&& (unsigned)minor(sbuff.st_rdev) == min);
}
if (found && buff != NULL) strncpy(buff, str, buffsz);
return !found;
}
int loop_dellist(unsigned devcnt, const dev_t *devids)
/** Tidy-up list of loopback devices */
{ unsigned i;
char buff[256];
int eflag = 0;
if (devids == NULL) return eflag;
for (i=0; i<devcnt; ++i) {
if (loop_ident(major(devids[i]), minor(devids[i]), buff, sizeof(buff))
|| (loop_destroy(buff) != ERR_NOERROR)) {
fprintf(stderr, _("Failed to free device (%d,%d)\n"),
major(devids[i]), minor(devids[i]));
eflag = 1;
}
}
return eflag;
}
int blockify_file(const char *filename, int fmode, const char *prefdev,
const char **devname, int *isloop)
/** Convert the given filename to block-device if it isn't already */
{ enum { BUFFMIN = 1024 };
struct stat sbuff;
char *loopdev = NULL;
int eflag = ERR_NOERROR;
if (filename == NULL || stat(filename, &sbuff) != 0) {
*isloop = 0;
eflag = ERR_BADDEVICE;
goto bail_out;
}
if (S_ISBLK(sbuff.st_mode)) {
/* Keyfile is block-special already: */
*devname = cm_strdup(filename);
*isloop = 0;
} else if (S_ISREG(sbuff.st_mode)) {
/* Create loopback device around ordinary file: */
if (prefdev != NULL && strcmp(prefdev, "auto") != 0) {
loopdev = (char*)malloc((size_t)(strlen(prefdev) + 1));
strcpy(loopdev, prefdev);
} else {
loopdev = (char*)malloc((size_t)BUFFMIN);
if (loop_findfree(loopdev, (size_t)BUFFMIN) != 0) {
fprintf(stderr, _("No available loopback devices\n"));
eflag = ERR_BADDEVICE;
goto bail_out;
}
}
if (loop_setup(loopdev, filename, fmode) != ERR_NOERROR) {
eflag = ERR_BADDEVICE;
goto bail_out;
}
*devname = loopdev;
loopdev = NULL;
*isloop = 1;
} else {
fprintf(stderr,
_("Bad device type (%x) for \"%s\" (need block/file)\n"),
(unsigned)sbuff.st_mode, filename);
*devname = NULL;
*isloop = 0;
eflag = ERR_BADDEVICE;
}
bail_out:
if (loopdev != NULL) free((void*)loopdev);
return eflag;
}
int unblockify_file(const char **devname, int isloop)
/** Remove loopback device previously created by blockify_file() */
{ int eflag = ERR_NOERROR;
if (isloop && *devname != NULL) {
eflag = loop_destroy(*devname);
free((void*)*devname);
*devname = NULL;
}
return eflag;
}
/*
* (C)Copyright 2005-2024, RW Penney
*/