2 * Copyright (C) 2008 - 2016 The Geeqie Team
4 * Author: Laurent Monin
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
11 * This program 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 along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <glib/gprintf.h>
23 #include <glib/gstdio.h>
27 #include "secure_save.h"
30 /* ABOUT SECURE SAVE */
31 /* This code was borrowed from the ELinks project (http://elinks.cz)
32 * It was originally written by me (Laurent Monin aka Zas) and heavily
33 * modified and improved by all ELinks contributors.
34 * This code was released under the GPLv2 licence.
35 * It was modified to be included in geeqie on 2008/04/05 */
37 /* If ssi->secure_save is TRUE:
38 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40 * A call to secure_open("/home/me/.confdir/filename", mask) will open a file
41 * named "filename.tmp_XXXXXX" in /home/me/.confdir/ and return a pointer to a
42 * structure SecureSaveInfo on success or NULL on error.
44 * filename.tmp_XXXXXX can't conflict with any file since it's created using
45 * mkstemp(). XXXXXX is a random string.
47 * Subsequent write operations are done using returned SecureSaveInfo FILE *
50 * If an error is encountered, SecureSaveInfo int field named err is set
51 * (automatically if using secure_fp*() functions or by programmer)
53 * When secure_close() is called, "filename.tmp_XXXXXX" is flushed and closed,
54 * and if SecureSaveInfo err field has a value of zero, "filename.tmp_XXXXXX"
55 * is renamed to "filename". If this succeeded, then secure_close() returns 0.
57 * WARNING: since rename() is used, any symlink called "filename" may be
58 * replaced by a regular file. If destination file isn't a regular file,
59 * then secsave is disabled for that file.
61 * If ssi->secure_save is FALSE:
62 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64 * No temporary file is created, "filename" is truncated, all operations are
65 * done on it, no rename nor flush occur, symlinks are preserved.
70 * Access rights are affected by secure_open() mask parameter.
73 /* FIXME: locking system on files about to be rewritten ? */
74 /* FIXME: Low risk race conditions about ssi->file_name. */
76 SecureSaveErrno secsave_errno = SS_ERR_NONE;
79 /** Open a file for writing in a secure way. @returns a pointer to a
80 * structure secure_save_info on success, or NULL on failure. */
81 static SecureSaveInfo *
82 secure_open_umask(const gchar *file_name)
87 secsave_errno = SS_ERR_NONE;
89 ssi = g_new0(SecureSaveInfo, 1);
91 secsave_errno = SS_ERR_OUT_OF_MEM;
95 ssi->secure_save = TRUE;
96 ssi->preserve_perms = TRUE;
97 ssi->unlink_on_error = TRUE;
99 ssi->file_name = g_strdup(file_name);
100 if (!ssi->file_name) {
101 secsave_errno = SS_ERR_OUT_OF_MEM;
105 /* Check properties of final file. */
106 #ifndef NO_UNIX_SOFTLINKS
107 if (lstat(ssi->file_name, &st)) {
109 if (stat(ssi->file_name, &st)) {
111 /* We ignore error caused by file inexistence. */
112 if (errno != ENOENT) {
115 secsave_errno = SS_ERR_STAT;
119 if (!S_ISREG(st.st_mode)) {
120 /* Not a regular file, secure_save is disabled. */
121 ssi->secure_save = FALSE;
124 /* XXX: access() do not work with setuid programs. */
125 if (access(ssi->file_name, R_OK | W_OK) < 0) {
127 secsave_errno = SS_ERR_ACCESS;
133 /* We still have a race condition here between
134 * [l]stat() and fopen() */
136 f1 = fopen(ssi->file_name, "rb+");
141 secsave_errno = SS_ERR_OPEN_READ;
148 if (ssi->secure_save) {
149 /* We use a random name for temporary file, mkstemp() opens
150 * the file and return a file descriptor named fd, which is
151 * then converted to FILE * using fdopen().
154 gchar *randname = g_strconcat(ssi->file_name, ".tmp_XXXXXX", NULL);
157 secsave_errno = SS_ERR_OUT_OF_MEM;
161 /* No need to use safe_mkstemp() here. --Zas */
162 fd = g_mkstemp(randname);
164 secsave_errno = SS_ERR_MKSTEMP;
169 ssi->fp = fdopen(fd, "wb");
171 secsave_errno = SS_ERR_OPEN_WRITE;
177 ssi->tmp_file_name = randname;
179 /* No need to create a temporary file here. */
180 ssi->fp = fopen(ssi->file_name, "wb");
182 secsave_errno = SS_ERR_OPEN_WRITE;
191 g_free(ssi->file_name);
192 ssi->file_name = NULL;
203 secure_open(const gchar *file_name)
207 #ifdef CONFIG_OS_WIN32
208 /* There is neither S_IRWXG nor S_IRWXO under crossmingw32-gcc */
209 const mode_t mask = 0177;
211 const mode_t mask = S_IXUSR | S_IRWXG | S_IRWXO;
214 saved_mask = umask(mask);
215 ssi = secure_open_umask(file_name);
221 /** Close a file opened with secure_open(). Rreturns 0 on success,
222 * errno or -1 on failure.
225 secure_close(SecureSaveInfo *ssi)
229 if (!ssi) return ret;
230 if (!ssi->fp) goto free;
232 if (ssi->err) { /* Keep previous errno. */
234 fclose(ssi->fp); /* Close file */
238 /* Ensure data is effectively written to disk, we first flush libc buffers
239 * using fflush(), then fsync() to flush kernel buffers, and finally call
240 * fclose() (which call fflush() again, but the first one is needed since
241 * it doesn't make much sense to flush kernel buffers and then libc buffers,
242 * while closing file releases file descriptor we need to call fsync(). */
243 #if defined(HAVE_FFLUSH) || defined(HAVE_FSYNC)
244 if (ssi->secure_save) {
245 gboolean fail = FALSE;
248 fail = (fflush(ssi->fp) == EOF);
252 if (!fail) fail = fsync(fileno(ssi->fp));
257 secsave_errno = SS_ERR_OTHER;
259 fclose(ssi->fp); /* Close file, ignore errors. */
266 if (fclose(ssi->fp) == EOF) {
268 secsave_errno = SS_ERR_OTHER;
272 if (ssi->secure_save && ssi->file_name && ssi->tmp_file_name) {
275 /* FIXME: Race condition on ssi->file_name. The file
276 * named ssi->file_name may have changed since
277 * secure_open() call (where we stat() file and
279 #ifndef NO_UNIX_SOFTLINKS
280 if (lstat(ssi->file_name, &st) == 0)
282 if (stat(ssi->file_name, &st) == 0)
285 /* set the dest file attributes to that of source (ignoring errors) */
286 if (ssi->preserve_perms)
288 if (chown(ssi->tmp_file_name, st.st_uid, st.st_gid) != 0) log_printf("chown('%s', %d, %d) failed", ssi->tmp_file_name, st.st_uid, st.st_gid);
289 if (chmod(ssi->tmp_file_name, st.st_mode) != 0) log_printf("chmod('%s', %o) failed", ssi->tmp_file_name, st.st_mode);
292 if (ssi->preserve_mtime)
296 tb.actime = st.st_atime;
297 tb.modtime = st.st_mtime;
298 utime(ssi->tmp_file_name, &tb);
301 DEBUG_3("rename %s -> %s", ssi->tmp_file_name, ssi->file_name);
302 if (rename(ssi->tmp_file_name, ssi->file_name) == -1) {
304 secsave_errno = SS_ERR_RENAME;
309 ret = 0; /* Success. */
312 if (ssi->tmp_file_name)
314 if (ret && ssi->unlink_on_error) unlink(ssi->tmp_file_name);
315 g_free(ssi->tmp_file_name);
317 if (ssi->file_name) g_free(ssi->file_name);
318 if (ssi) g_free(ssi);
324 /** fputs() wrapper, set ssi->err to errno on error. If ssi->err is set when
325 * called, it immediatly returns EOF.
328 secure_fputs(SecureSaveInfo *ssi, const gchar *s)
332 if (!ssi || !ssi->fp || ssi->err) return EOF;
334 ret = fputs(s, ssi->fp);
336 secsave_errno = SS_ERR_OTHER;
344 /** fputc() wrapper, set ssi->err to errno on error. If ssi->err is set when
345 * called, it immediatly returns EOF.
348 secure_fputc(SecureSaveInfo *ssi, gint c)
352 if (!ssi || !ssi->fp || ssi->err) return EOF;
354 ret = fputc(c, ssi->fp);
357 secsave_errno = SS_ERR_OTHER;
363 /** fprintf() wrapper, set ssi->err to errno on error and return a negative
364 * value. If ssi->err is set when called, it immediatly returns -1.
367 secure_fprintf(SecureSaveInfo *ssi, const gchar *format, ...)
372 if (!ssi || !ssi->fp || ssi->err) return -1;
374 va_start(ap, format);
375 ret = g_vfprintf(ssi->fp, format, ap);
381 /** fwrite() wrapper, set ssi->err to errno on error and return a value less than
382 * the number of elements to write. If ssi->err is set when called, it immediatly returns 0.
385 secure_fwrite(gconstpointer ptr, size_t size, size_t nmemb, SecureSaveInfo *ssi)
389 if (!ssi || !ssi->fp || ssi->err) return 0;
391 ret = fwrite(ptr, size, nmemb, ssi->fp);
395 secsave_errno = SS_ERR_OTHER;
402 secsave_strerror(SecureSaveErrno secsave_error)
404 switch (secsave_error) {
405 case SS_ERR_OPEN_READ:
406 return _("Cannot read the file");
408 return _("Cannot get file status");
410 return _("Cannot access the file");
412 return _("Cannot create temp file");
414 return _("Cannot rename the file");
415 case SS_ERR_DISABLED:
416 return _("File saving disabled by option");
417 case SS_ERR_OUT_OF_MEM:
418 return _("Out of memory");
419 case SS_ERR_OPEN_WRITE:
420 return _("Cannot write the file");
421 case SS_ERR_NONE: /* Impossible. */
424 return _("Secure file saving error");
427 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */