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
|
/*
* passdev.c - waits for a given device to appear, mounts it and reads a
* key from it which is piped to stdout.
*
* Copyright (C) 2008 David Härdeman <david@hardeman.nu>
*
* This package 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 package 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 package; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _BSD_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mount.h>
static bool do_debug = false;
static void
debug(const char *fmt, ...)
{
va_list ap;
if (!do_debug)
return;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
static bool
do_mount(const char *device, const char *dir)
{
pid_t pid;
pid_t wpid;
int status;
char *fstypes[] = { "ext3", "ext2", "vfat", "reiserfs", "xfs", "isofs", "udf" };
int fsindex;
if (!device || !dir)
return false;
for (fsindex = 0;
fsindex < (sizeof(fstypes) / sizeof(fstypes[0]));
fsindex++)
{
pid = fork();
if (pid < 0) {
/* Error */
return false;
} else if (pid > 0) {
/* We're in the parent process */
do {
wpid = waitpid(pid, &status, 0);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
if (WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS)
return true;
/* Let's try another fstype */
continue;
} else {
/* We're in the child process */
debug("Mounting %s at %s\n", device, dir);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
open("/dev/null", O_RDONLY, 0);
open("/dev/null", O_WRONLY, 0);
open("/dev/null", O_WRONLY, 0);
execl("/bin/mount", "/bin/mount", "-n", "-t",
fstypes[fsindex],
/*"ext3,ext2,vfat,reiserfs,xfs,isofs,udf",*/
"-o", "noatime,nodiratime,nodev,noexec,nosuid,ro",
device, dir, (char *)NULL);
/* If execl works, we won't end up here */
exit(EXIT_FAILURE);
}
}
/* We've tried all fstypes with no luck */
return false;
}
int
main(int argc, char **argv, char **envp)
{
char *debugval;
char *devpath;
char *filepath;
struct stat st;
char *tmppath;
char tpath[] = "/tmp/passdev.XXXXXX";
char *keypath;
int fd;
size_t toread;
size_t bytesread;
char *keybuffer;
size_t towrite;
size_t byteswritten;
ssize_t bytes;
/* We only take one argument */
if (argc != 2) {
fprintf(stderr, "Incorrect number of arguments");
goto error;
}
/* If DEBUG=1 is in the environment, enable debug messages */
debugval = getenv("DEBUG");
if (debugval && atoi(debugval) > 0)
do_debug = true;
/* Split string into device and path */
devpath = argv[1];
filepath = strchr(devpath, ':');
if (!filepath || !(*filepath) || !(*(filepath + 1))) {
fprintf(stderr, "Invalid key path");
goto error;
}
*filepath = '\0';
filepath++;
debug("Path is %p and filepath is %p\n", devpath, filepath);
/* Wait until device is available */
if (access(devpath, F_OK)) {
debug("Waiting for %s\n", devpath);
while(access(devpath, F_OK))
sleep(1);
}
/* Make sure device is a blockdev */
if (stat(devpath, &st)) {
fprintf(stderr, "Unable to stat %s\n", devpath);
goto error;
} else if (!S_ISBLK(st.st_mode)) {
fprintf(stderr, "%s is no block device\n", devpath);
goto error;
}
/* Create a tmp dir where we mount the device */
tmppath = mkdtemp(tpath);
if (!tmppath) {
fprintf(stderr, "Failed to create temporary directory\n");
goto error;
}
/* Ok, mount it */
if (!do_mount(devpath, tmppath)) {
fprintf(stderr, "Failed to mount %s\n", devpath);
goto error_rmdir;
}
/* Generate the full path to the keyfile */
keypath = malloc(strlen(tmppath) + 1 + strlen(filepath) + 1);
if (!keypath) {
fprintf(stderr, "Failed to allocate memory\n");
goto error_umount;
}
sprintf(keypath, "%s/%s", tmppath, filepath);
/* Check that the keyfile exists */
if (access(keypath, F_OK)) {
fprintf(stderr, "Keyfile doesn't exist\n");
goto error_free;
}
/* Get the size of the keyfile */
if (stat(keypath, &st)) {
fprintf(stderr, "Unable to stat keyfile\n");
goto error_free;
}
/* Check the size of the keyfile */
if (st.st_size < 0) {
fprintf(stderr, "Invalid keyfile size\n");
goto error_free;
}
toread = (size_t)st.st_size;
/* Open the keyfile */
if ((fd = open(keypath, O_RDONLY)) < 0) {
fprintf(stderr, "Failed to open keyfile\n");
goto error_free;
}
/* Allocate a buffer for the keyfile contents */
keybuffer = malloc(toread);
if (!keybuffer) {
fprintf(stderr, "Failed to allocate memory\n");
goto error_close;
exit(EXIT_FAILURE);
}
/* Read the keyfile */
bytesread = 0;
while (bytesread < toread) {
bytes = read(fd, keybuffer + bytesread, toread - bytesread);
if (bytes <= 0) {
fprintf(stderr, "Failed to read entire key\n");
goto error_keybuffer;
}
bytesread += bytes;
}
/* Clean up */
close(fd);
free(keypath);
umount(tmppath);
rmdir(tmppath);
/* Write result */
byteswritten = 0;
towrite = toread;
while (byteswritten < towrite) {
bytes = write(STDOUT_FILENO, keybuffer + byteswritten,
towrite - byteswritten);
if (bytes <= 0) {
fprintf(stderr, "Failed to write entire key\n");
memset(keybuffer, 0, toread);
free(keybuffer);
goto error;
}
byteswritten += bytes;
}
/* Clean up */
memset(keybuffer, 0, toread);
free(keybuffer);
/* Done */
exit(EXIT_SUCCESS);
/* Error handling */
error_keybuffer:
memset(keybuffer, 0, toread);
free(keybuffer);
error_close:
close(fd);
error_free:
free(keypath);
error_umount:
umount(tmppath);
error_rmdir:
rmdir(tmppath);
error:
exit(EXIT_FAILURE);
}
|