]> git.ipfire.org Git - thirdparty/util-linux.git/blob - mount/lomount.c
Imported from util-linux-2.7.1 tarball.
[thirdparty/util-linux.git] / mount / lomount.c
1 /* Taken from Ted's losetup.c - Mitch <m.dsouza@mrc-apu.cam.ac.uk> */
2 /* Added vfs mount options - aeb - 960223 */
3 /* Removed lomount - aeb - 960224 */
4
5 #define PROC_DEVICES "/proc/devices"
6
7 /*
8 * losetup.c - setup and control loop devices
9 */
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <fcntl.h>
15 #include <errno.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <sys/ioctl.h>
19 #include <sys/stat.h>
20
21 #if defined(__GLIBC__)
22 #define _SOCKETBITS_H
23 #endif /* __GLIBC */
24 #include "sundries.h" /* for xstrdup */
25 #include "loop.h"
26 #include "lomount.h"
27
28 #ifdef LOOP_SET_FD
29 struct crypt_type_struct {
30 int id;
31 char *name;
32 } crypt_type_tbl[] = {
33 { LO_CRYPT_NONE, "no" },
34 { LO_CRYPT_NONE, "none" },
35 { LO_CRYPT_XOR, "xor" },
36 { LO_CRYPT_DES, "DES" },
37 { -1, NULL }
38 };
39
40 static int
41 crypt_type (const char *name)
42 {
43 int i;
44
45 if (name)
46 for (i = 0; crypt_type_tbl[i].id != -1; i++)
47 if (!strcasecmp (name, crypt_type_tbl[i].name))
48 return crypt_type_tbl[i].id;
49 return -1;
50 }
51
52 #if 0
53 static char *
54 crypt_name (int id)
55 {
56 int i;
57
58 for (i = 0; crypt_type_tbl[i].id != -1; i++)
59 if (id == crypt_type_tbl[i].id)
60 return crypt_type_tbl[i].name;
61 return "undefined";
62 }
63
64 static void
65 show_loop (char *device)
66 {
67 struct loop_info loopinfo;
68 int fd;
69
70 if ((fd = open (device, O_RDONLY)) < 0) {
71 fprintf(stderr, "loop: can't open device %s: %s\n",
72 device, strerror (errno));
73 return;
74 }
75 if (ioctl (fd, LOOP_GET_STATUS, &loopinfo) < 0) {
76 fprintf(stderr, "loop: can't get info on device %s: %s\n",
77 device, strerror (errno));
78 close (fd);
79 return;
80 }
81 printf ("%s: [%04x]:%ld (%s) offset %d, %s encryption\n",
82 device, loopinfo.lo_device, loopinfo.lo_inode,
83 loopinfo.lo_name, loopinfo.lo_offset,
84 crypt_name (loopinfo.lo_encrypt_type));
85 close (fd);
86 }
87 #endif
88
89 char *
90 find_unused_loop_device (void)
91 {
92 /* Just creating a device, say in /tmp, is probably a bad idea -
93 people might have problems with backup or so.
94 So, we just try /dev/loop[0-7]. */
95 char dev[20];
96 int i, fd, somedev = 0, someloop = 0, loop_known = 0;
97 struct stat statbuf;
98 struct loop_info loopinfo;
99 FILE *procdev;
100
101 for(i = 0; i < 256; i++) {
102 sprintf(dev, "/dev/loop%d", i);
103 if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
104 somedev++;
105 fd = open (dev, O_RDONLY);
106 if (fd >= 0) {
107 if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == 0)
108 someloop++; /* in use */
109 else if (errno == ENXIO) {
110 close (fd);
111 return xstrdup(dev); /* probably free */
112 }
113 close (fd);
114 }
115 continue; /* continue trying as long as devices exist */
116 }
117 if (i >= 7)
118 break;
119 }
120
121 /* Nothing found. Why not? */
122 if ((procdev = fopen(PROC_DEVICES, "r")) != NULL) {
123 char line[100];
124 while (fgets (line, sizeof(line), procdev))
125 if (strstr (line, " loop\n")) {
126 loop_known = 1;
127 break;
128 }
129 fclose(procdev);
130 if (!loop_known)
131 loop_known = -1;
132 }
133
134 if (!somedev)
135 error("mount: could not find any device /dev/loop#");
136 else if(!someloop) {
137 if (loop_known == 1)
138 error(
139 "mount: Could not find any loop device.\n"
140 " Maybe /dev/loop# has a wrong major number?\n");
141 else if (loop_known == -1)
142 error(
143 "mount: Could not find any loop device, and, according to %s,\n"
144 " this kernel does not know about the loop device.\n"
145 " (If so, then recompile or `insmod loop.o'.)\n", PROC_DEVICES);
146 else
147 error(
148 "mount: Could not find any loop device. Maybe this kernel does not know\n"
149 " about the loop device (then recompile or `insmod loop.o'), or\n"
150 " maybe /dev/loop# has the wrong major number?\n");
151 } else
152 error("mount: could not find any free loop device");
153 return 0;
154 }
155
156 int
157 set_loop (const char *device, const char *file, int offset,
158 const char *encryption, int *loopro)
159 {
160 struct loop_info loopinfo;
161 int fd, ffd, mode, i;
162 char *pass;
163
164 mode = (*loopro ? O_RDONLY : O_RDWR);
165 if ((ffd = open (file, mode)) < 0) {
166 if (!*loopro && errno == EROFS)
167 ffd = open (file, mode = O_RDONLY);
168 if (ffd < 0) {
169 perror (file);
170 return 1;
171 }
172 }
173 if ((fd = open (device, mode)) < 0) {
174 perror (device);
175 return 1;
176 }
177 *loopro = (mode == O_RDONLY);
178 memset (&loopinfo, 0, sizeof (loopinfo));
179 strncpy (loopinfo.lo_name, file, LO_NAME_SIZE);
180 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
181 if (encryption && (loopinfo.lo_encrypt_type = crypt_type (encryption))
182 < 0) {
183 fprintf (stderr, "Unsupported encryption type %s", encryption);
184 return 1;
185 }
186 loopinfo.lo_offset = offset;
187 switch (loopinfo.lo_encrypt_type) {
188 case LO_CRYPT_NONE:
189 loopinfo.lo_encrypt_key_size = 0;
190 break;
191 case LO_CRYPT_XOR:
192 pass = getpass ("Password: ");
193 strncpy (loopinfo.lo_encrypt_key, pass, LO_KEY_SIZE);
194 loopinfo.lo_encrypt_key[LO_KEY_SIZE - 1] = 0;
195 loopinfo.lo_encrypt_key_size = strlen (loopinfo.lo_encrypt_key);
196 break;
197 case LO_CRYPT_DES:
198 pass = getpass ("Password: ");
199 strncpy (loopinfo.lo_encrypt_key, pass, 8);
200 loopinfo.lo_encrypt_key[8] = 0;
201 loopinfo.lo_encrypt_key_size = 8;
202 pass = getpass ("Init (up to 16 hex digits): ");
203 for (i = 0; i < 16 && pass[i]; i++)
204 if (isxdigit (pass[i]))
205 loopinfo.lo_init[i >> 3] |= (pass[i] > '9' ?
206 (islower (pass[i]) ? toupper (pass[i]) :
207 pass[i]) - 'A' + 10 : pass[i] - '0') << (i & 7) * 4;
208 else {
209 fprintf (stderr, "Non-hex digit '%c'.\n", pass[i]);
210 return 1;
211 }
212 break;
213 default:
214 fprintf (stderr,
215 "Don't know how to get key for encryption system %d\n",
216 loopinfo.lo_encrypt_type);
217 return 1;
218 }
219 if (ioctl (fd, LOOP_SET_FD, ffd) < 0) {
220 perror ("ioctl: LOOP_SET_FD");
221 return 1;
222 }
223 if (ioctl (fd, LOOP_SET_STATUS, &loopinfo) < 0) {
224 (void) ioctl (fd, LOOP_CLR_FD, 0);
225 perror ("ioctl: LOOP_SET_STATUS");
226 return 1;
227 }
228 close (fd);
229 close (ffd);
230 if (verbose > 1)
231 printf("set_loop(%s,%s,%d): success\n", device, file, offset);
232 return 0;
233 }
234
235 int
236 del_loop (const char *device)
237 {
238 int fd;
239
240 if ((fd = open (device, O_RDONLY)) < 0) {
241 fprintf(stderr, "loop: can't delete device %s: %s\n",
242 device, strerror (errno));
243 return 1;
244 }
245 if (ioctl (fd, LOOP_CLR_FD, 0) < 0) {
246 perror ("ioctl: LOOP_CLR_FD");
247 return 1;
248 }
249 close (fd);
250 if (verbose > 1)
251 printf("del_loop(%s): success\n", device);
252 return 0;
253 }
254
255 #else /* no LOOP_SET_FD defined */
256 static void
257 mutter(void) {
258 fprintf(stderr,
259 "This mount was compiled without loop support. Please recompile.\n");
260 }
261
262 int
263 set_loop (const char *device, const char *file, int offset,
264 const char *encryption, int *loopro) {
265 mutter();
266 return 1;
267 }
268
269 int
270 del_loop (const char *device) {
271 mutter();
272 return 1;
273 }
274
275 char *
276 find_unused_loop_device (void) {
277 mutter();
278 return 0;
279 }
280
281 #endif