1 /* backend.c - Dispatcher to the various backends.
2 * Copyright (C) 2009 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
33 #include "be-truecrypt.h"
34 #include "be-dmcrypt.h"
35 #include "call-syshelp.h"
37 #define no_such_backend(a) _no_such_backend ((a), __func__)
39 _no_such_backend (int conttype, const char *func)
41 log_error ("invalid backend %d given in %s - this is most likely a bug\n",
43 return gpg_error (GPG_ERR_INTERNAL);
47 /* Parse NAME and return the corresponding content type. If the name
48 is not known, a error message is printed and zero returned. If
49 NAME is NULL the supported backend types are listed and 0 is
52 be_parse_conttype_name (const char *name)
54 static struct { const char *name; int conttype; } names[] = {
55 { "encfs", CONTTYPE_ENCFS },
56 { "dm-crypt", CONTTYPE_DM_CRYPT }
62 log_info ("Known backend types:\n");
63 for (i=0; i < DIM (names); i++)
64 log_info (" %s\n", names[i].name);
68 for (i=0; i < DIM (names); i++)
70 if (!strcmp (names[i].name, name))
71 return names[i].conttype;
74 log_error ("invalid backend type '%s' given\n", name);
79 /* Return true if CONTTYPE is supported by us. */
81 be_is_supported_conttype (int conttype)
86 case CONTTYPE_DM_CRYPT:
95 /* Create a lock file for the container FNAME and store the lock at
96 * R_LOCK and return 0. On error return an error code and store NULL
99 be_take_lock_for_create (ctrl_t ctrl, const char *fname, dotlock_t *r_lock)
102 dotlock_t lock = NULL;
107 /* A DM-crypt container requires special treatment by using the
108 syshelper fucntions. */
109 if (ctrl->conttype == CONTTYPE_DM_CRYPT)
112 err = call_syshelp_set_device (ctrl, fname);
117 /* A quick check to see that no container with that name already
119 if (!access (fname, F_OK))
121 err = gpg_error (GPG_ERR_EEXIST);
125 /* Take a lock and proceed with the creation. If there is a lock we
126 immediately return an error because for creation it does not make
128 lock = dotlock_create (fname, 0);
131 err = gpg_error_from_syserror ();
134 if (dotlock_take (lock, 0))
136 err = gpg_error_from_syserror ();
140 /* Check again that the file does not exist. */
141 err = stat (fname, &sb)? 0 : gpg_error (GPG_ERR_EEXIST);
149 dotlock_destroy (lock);
154 /* If the backend requires a separate file or directory for the
155 container, return its name by computing it from FNAME which gives
156 the g13 filename. The new file name is allocated and stored at
157 R_NAME, if this is expected to be a directory true is stored at
158 R_ISDIR. If no detached name is expected or an error occurs NULL
159 is stored at R_NAME. The function returns 0 on success or an error
162 be_get_detached_name (int conttype, const char *fname,
163 char **r_name, int *r_isdir)
170 return be_encfs_get_detached_name (fname, r_name, r_isdir);
172 case CONTTYPE_DM_CRYPT:
176 return no_such_backend (conttype);
182 be_create_new_keys (int conttype, membuf_t *mb)
187 return be_encfs_create_new_keys (mb);
189 case CONTTYPE_TRUECRYPT:
190 return be_truecrypt_create_new_keys (mb);
192 case CONTTYPE_DM_CRYPT:
196 return no_such_backend (conttype);
201 /* Dispatcher to the backend's create function. */
203 be_create_container (ctrl_t ctrl, int conttype,
204 const char *fname, int fd, tupledesc_t tuples,
207 (void)fd; /* Not yet used. */
212 return be_encfs_create_container (ctrl, fname, tuples, r_id);
214 case CONTTYPE_DM_CRYPT:
215 return be_dmcrypt_create_container (ctrl);
218 return no_such_backend (conttype);
223 /* Dispatcher to the backend's mount function. */
225 be_mount_container (ctrl_t ctrl, int conttype,
226 const char *fname, const char *mountpoint,
227 tupledesc_t tuples, unsigned int *r_id)
232 return be_encfs_mount_container (ctrl, fname, mountpoint, tuples, r_id);
234 case CONTTYPE_DM_CRYPT:
235 return be_dmcrypt_mount_container (ctrl, fname, mountpoint, tuples);
238 return no_such_backend (conttype);
243 /* Dispatcher to the backend's umount function. */
245 be_umount_container (ctrl_t ctrl, int conttype, const char *fname)
250 return gpg_error (GPG_ERR_NOT_SUPPORTED);
252 case CONTTYPE_DM_CRYPT:
253 return be_dmcrypt_umount_container (ctrl, fname);
256 return no_such_backend (conttype);
261 /* Dispatcher to the backend's suspend function. */
263 be_suspend_container (ctrl_t ctrl, int conttype, const char *fname)
268 return gpg_error (GPG_ERR_NOT_SUPPORTED);
270 case CONTTYPE_DM_CRYPT:
271 return be_dmcrypt_suspend_container (ctrl, fname);
274 return no_such_backend (conttype);
279 /* Dispatcher to the backend's resume function. */
281 be_resume_container (ctrl_t ctrl, int conttype, const char *fname,
287 return gpg_error (GPG_ERR_NOT_SUPPORTED);
289 case CONTTYPE_DM_CRYPT:
290 return be_dmcrypt_resume_container (ctrl, fname, tuples);
293 return no_such_backend (conttype);