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>
25 #include "secure-save.h"
28 * @file secure-save.cc
30 * ABOUT SECURE SAVE \n
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
36 * If ssi->secure_save is TRUE:
38 * A call to secure_open("/home/me/.confdir/filename", mask) will open a file
39 * named "filename.tmp_XXXXXX" in /home/me/.confdir/ and return a pointer to a
40 * structure SecureSaveInfo on success or NULL on error.
42 * filename.tmp_XXXXXX can't conflict with any file since it's created using
43 * mkstemp(). XXXXXX is a random string.
45 * Subsequent write operations are done using returned SecureSaveInfo FILE *
48 * If an error is encountered, SecureSaveInfo int field named err is set
49 * (automatically if using secure_fp*() functions or by programmer)
51 * When secure_close() is called, "filename.tmp_XXXXXX" is flushed and closed,
52 * and if SecureSaveInfo err field has a value of zero, "filename.tmp_XXXXXX"
53 * is renamed to "filename". If this succeeded, then secure_close() returns 0.
55 * WARNING: since rename() is used, any symlink called "filename" may be
56 * replaced by a regular file. If destination file isn't a regular file,
57 * then secsave is disabled for that file.
59 * If ssi->secure_save is FALSE:
61 * No temporary file is created, "filename" is truncated, all operations are
62 * done on it, no rename nor flush occur, symlinks are preserved.
66 * Access rights are affected by secure_open() mask parameter.
71 * @FIXME locking system on files about to be rewritten ?
72 * @FIXME Low risk race conditions about ssi->file_name.
75 SecureSaveErrno secsave_errno = SS_ERR_NONE;
78 /** Open a file for writing in a secure way. @returns a pointer to a
79 * structure secure_save_info on success, or NULL on failure. */
80 static SecureSaveInfo *
81 secure_open_umask(const gchar *file_name)
86 secsave_errno = SS_ERR_NONE;
88 ssi = g_new0(SecureSaveInfo, 1);
90 secsave_errno = SS_ERR_OUT_OF_MEM;
94 ssi->secure_save = TRUE;
95 ssi->preserve_perms = TRUE;
96 ssi->unlink_on_error = TRUE;
98 ssi->file_name = g_strdup(file_name);
99 if (!ssi->file_name) {
100 secsave_errno = SS_ERR_OUT_OF_MEM;
104 /* Check properties of final file. */
105 #ifndef NO_UNIX_SOFTLINKS
106 if (lstat(ssi->file_name, &st)) {
108 if (stat(ssi->file_name, &st)) {
110 /* We ignore error caused by file inexistence. */
111 if (errno != ENOENT) {
114 secsave_errno = SS_ERR_STAT;
118 if (!S_ISREG(st.st_mode)) {
119 /* Not a regular file, secure_save is disabled. */
120 ssi->secure_save = FALSE;
123 /* XXX: access() do not work with setuid programs. */
124 if (access(ssi->file_name, R_OK | W_OK) < 0) {
126 secsave_errno = SS_ERR_ACCESS;
132 /* We still have a race condition here between
133 * [l]stat() and fopen() */
135 f1 = fopen(ssi->file_name, "rb+");
140 secsave_errno = SS_ERR_OPEN_READ;
147 if (ssi->secure_save) {
148 /* We use a random name for temporary file, mkstemp() opens
149 * the file and return a file descriptor named fd, which is
150 * then converted to FILE * using fdopen().
153 gchar *randname = g_strconcat(ssi->file_name, ".tmp_XXXXXX", NULL);
156 secsave_errno = SS_ERR_OUT_OF_MEM;
160 /* No need to use safe_mkstemp() here. --Zas */
161 fd = g_mkstemp(randname);
163 secsave_errno = SS_ERR_MKSTEMP;
168 ssi->fp = fdopen(fd, "wb");
170 secsave_errno = SS_ERR_OPEN_WRITE;
176 ssi->tmp_file_name = randname;
178 /* No need to create a temporary file here. */
179 ssi->fp = fopen(ssi->file_name, "wb");
181 secsave_errno = SS_ERR_OPEN_WRITE;
190 g_free(ssi->file_name);
191 ssi->file_name = NULL;
202 secure_open(const gchar *file_name)
206 #ifdef CONFIG_OS_WIN32
207 /* There is neither S_IRWXG nor S_IRWXO under crossmingw32-gcc */
208 const mode_t mask = 0177;
210 const mode_t mask = S_IXUSR | S_IRWXG | S_IRWXO;
213 saved_mask = umask(mask);
214 ssi = secure_open_umask(file_name);
220 /** Close a file opened with secure_open(). Rreturns 0 on success,
221 * errno or -1 on failure.
224 secure_close(SecureSaveInfo *ssi)
228 if (!ssi) return ret;
229 if (!ssi->fp) goto free;
231 if (ssi->err) { /* Keep previous errno. */
233 fclose(ssi->fp); /* Close file */
237 /* Ensure data is effectively written to disk, we first flush libc buffers
238 * using fflush(), then fsync() to flush kernel buffers, and finally call
239 * fclose() (which call fflush() again, but the first one is needed since
240 * it doesn't make much sense to flush kernel buffers and then libc buffers,
241 * while closing file releases file descriptor we need to call fsync(). */
242 #if defined(HAVE_FFLUSH) || defined(HAVE_FSYNC)
243 if (ssi->secure_save) {
244 gboolean fail = FALSE;
247 fail = (fflush(ssi->fp) == EOF);
251 if (!fail) fail = fsync(fileno(ssi->fp));
256 secsave_errno = SS_ERR_OTHER;
258 fclose(ssi->fp); /* Close file, ignore errors. */
265 if (fclose(ssi->fp) == EOF) {
267 secsave_errno = SS_ERR_OTHER;
271 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 if (rename(ssi->tmp_file_name, ssi->file_name) == -1) {
303 secsave_errno = SS_ERR_RENAME;
308 ret = 0; /* Success. */
311 if (ssi->tmp_file_name)
313 if (ret && ssi->unlink_on_error) unlink(ssi->tmp_file_name);
314 g_free(ssi->tmp_file_name);
316 if (ssi->file_name) g_free(ssi->file_name);
317 if (ssi) g_free(ssi);
323 /** fputs() wrapper, set ssi->err to errno on error. If ssi->err is set when
324 * called, it immediately returns EOF.
327 secure_fputs(SecureSaveInfo *ssi, const gchar *s)
331 if (!ssi || !ssi->fp || ssi->err) return EOF;
333 ret = fputs(s, ssi->fp);
335 secsave_errno = SS_ERR_OTHER;
343 /** fputc() wrapper, set ssi->err to errno on error. If ssi->err is set when
344 * called, it immediately returns EOF.
347 secure_fputc(SecureSaveInfo *ssi, gint c)
351 if (!ssi || !ssi->fp || ssi->err) return EOF;
353 ret = fputc(c, ssi->fp);
356 secsave_errno = SS_ERR_OTHER;
362 /** fprintf() wrapper, set ssi->err to errno on error and return a negative
363 * value. If ssi->err is set when called, it immediately returns -1.
366 secure_fprintf(SecureSaveInfo *ssi, const gchar *format, ...)
371 if (!ssi || !ssi->fp || ssi->err) return -1;
373 va_start(ap, format);
374 ret = g_vfprintf(ssi->fp, format, ap);
380 /** fwrite() wrapper, set ssi->err to errno on error and return a value less than
381 * the number of elements to write. If ssi->err is set when called, it immediately returns 0.
384 secure_fwrite(gconstpointer ptr, size_t size, size_t nmemb, SecureSaveInfo *ssi)
388 if (!ssi || !ssi->fp || ssi->err) return 0;
390 ret = fwrite(ptr, size, nmemb, ssi->fp);
394 secsave_errno = SS_ERR_OTHER;
401 secsave_strerror(SecureSaveErrno secsave_error)
403 switch (secsave_error) {
404 case SS_ERR_OPEN_READ:
405 return _("Cannot read the file");
407 return _("Cannot get file status");
409 return _("Cannot access the file");
411 return _("Cannot create temp file");
413 return _("Cannot rename the file");
414 case SS_ERR_DISABLED:
415 return _("File saving disabled by option");
416 case SS_ERR_OUT_OF_MEM:
417 return _("Out of memory");
418 case SS_ERR_OPEN_WRITE:
419 return _("Cannot write the file");
420 case SS_ERR_NONE: /* Impossible. */
423 return _("Secure file saving error");
426 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */