101
err = gcry_md_open(&hd, algo, 0);
102
mdlen = gcry_md_get_algo_dlen(algo);
104
syslog(LOG_ERR, "Failed to open hash algo [%d]: "
105
"[%d]\n", algo, err);
108
gcry_md_write(hd, src, src_size);
109
hash = gcry_md_read(hd, algo);
110
memcpy(dst, hash, mdlen);
112
#endif /* #ifdef ENABLE_NSS */
92
/* Read ecryptfs private mount from file
93
* Allocate and return a string
95
char *ecryptfs_fetch_private_mnt(char *pw_dir) {
96
char *mnt_file = NULL;
97
char *mnt_default = NULL;
100
/* Construct mnt file name */
101
if (asprintf(&mnt_default, "%s/%s", pw_dir, ECRYPTFS_PRIVATE_DIR) < 0
102
|| mnt_default == NULL) {
107
asprintf(&mnt_file, "%s/.ecryptfs/%s.mnt", pw_dir, ECRYPTFS_PRIVATE_DIR) < 0
108
|| mnt_file == NULL) {
112
fh = fopen(mnt_file, "r");
117
if ((mnt = (char *)malloc(MAXPATHLEN+1)) == NULL) {
122
if (fgets(mnt, MAXPATHLEN, fh) == NULL) {
126
/* Ensure that mnt doesn't contain newlines */
127
char *nl = strchr(mnt, '\n');
133
if (mnt_file != NULL)
135
if (mnt_default != NULL && mnt != mnt_default)
141
/* Check if an ecryptfs private device or mount point is mounted.
142
* Return 1 if a filesystem in mtab matches dev && mnt && sig.
143
* Return 0 otherwise.
145
int ecryptfs_private_is_mounted(char *dev, char *mnt, char *sig, int mounting) {
147
struct mntent *m = NULL;
150
if (sig && asprintf(&opt, "ecryptfs_sig=%s", sig) < 0) {
154
fh = setmntent("/proc/mounts", "r");
161
while ((m = getmntent(fh)) != NULL) {
162
if (strcmp(m->mnt_type, "ecryptfs") != 0)
163
/* Skip if this entry is not an ecryptfs mount */
166
/* If mounting, return "already mounted" if EITHER the
167
* dev or the mnt dir shows up in mtab/mounts;
168
* regardless of the signature of such mounts;
170
if (dev != NULL && strcmp(m->mnt_fsname, dev) == 0) {
174
if (mnt != NULL && strcmp(m->mnt_dir, mnt) == 0) {
179
/* Otherwise, we're unmounting, and we need to be
180
* very conservative in finding a perfect match
181
* to unmount. The device, mountpoint, and signature
182
* must *all* match perfectly.
185
strcmp(m->mnt_fsname, dev) == 0 &&
186
strcmp(m->mnt_dir, mnt) == 0 &&
187
(!opt || hasmntopt(m, opt) != NULL)
202
118
* TODO: We need to support more hash algs
203
119
* @fekek: ECRYPTFS_MAX_KEY_BYTES bytes of allocated memory
306
int ecryptfs_mount(char *source, char *target, unsigned long flags, char *opts)
308
FILE *mtab_fd = NULL;
309
struct mntent mountent;
310
char *fullpath_source = NULL;
311
char *fullpath_target = NULL;
314
mountent.mnt_opts = NULL;
317
syslog(LOG_ERR, "Invalid source directory\n");
322
syslog(LOG_ERR, "Invalid target directory\n");
325
if (strlen(opts) > 200) {
327
syslog(LOG_ERR, "Invalid mount options length\n");
331
fullpath_source = realpath(source, NULL);
332
if (!fullpath_source) {
334
syslog(LOG_ERR, "could not resolve full path for source %s [%d]",
338
fullpath_target = realpath(target, NULL);
339
if (!fullpath_target) {
341
syslog(LOG_ERR, "could not resolve full path for target %s [%d]",
346
if (mount(fullpath_source, fullpath_target, "ecryptfs", flags, opts)) {
348
syslog(LOG_ERR, "Failed to perform eCryptfs mount: [%m]\n");
351
mtab_fd = setmntent("/etc/mtab", "a");
354
syslog(LOG_ERR, "Failed to update the mount table\n");
357
mountent.mnt_fsname = fullpath_source;
358
mountent.mnt_dir = fullpath_target;
359
mountent.mnt_type = "ecryptfs";
360
/* we need the following byte count:
362
* 23 max for strings below
365
mountent.mnt_opts = malloc(224);
366
if (!mountent.mnt_opts) {
368
syslog(LOG_ERR, "Failed to allocate memory for mount "
372
memset(mountent.mnt_opts, 0, 224);
373
/* reporting the right mount opts */
374
if (flags & MS_RDONLY)
375
strcat(mountent.mnt_opts,"ro");
377
strcat(mountent.mnt_opts,"rw");
378
if (flags & MS_NOEXEC)
379
strcat(mountent.mnt_opts,",noexec");
380
if (flags & MS_NOSUID)
381
strcat(mountent.mnt_opts,",nosuid");
382
if (flags & MS_NODEV)
383
strcat(mountent.mnt_opts,",nodev");
385
strcat(mountent.mnt_opts, ",");
386
strcat(mountent.mnt_opts, opts);
388
mountent.mnt_freq = 0;
389
mountent.mnt_passno = 0;
390
if (addmntent(mtab_fd, &mountent)) {
392
syslog(LOG_ERR, "Failed to write to the mount "
398
free(fullpath_source);
399
free(fullpath_target);
400
free(mountent.mnt_opts);
386
406
static int zombie_semaphore_get(void)